PageRenderTime 48ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/wordpress-mu-domain-mapping/domain_mapping.php

https://github.com/mikesherov/web-base-template
PHP | 848 lines | 743 code | 65 blank | 40 comment | 205 complexity | 251e99379aa81817d925b2cc480f4ad4 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /*
  3. Plugin Name: WordPress MU Domain Mapping
  4. Plugin URI: http://ocaoimh.ie/wordpress-mu-domain-mapping/
  5. Description: Map any blog on a WordPress website to another domain.
  6. Version: 0.5.4.1
  7. Author: Donncha O Caoimh
  8. Author URI: http://ocaoimh.ie/
  9. */
  10. /* Copyright Donncha O Caoimh (http://ocaoimh.ie/)
  11. With contributions by Ron Rennick(http://wpmututorials.com/), Greg Sidberry and others.
  12. This program is free software; you can redistribute it and/or modify
  13. it under the terms of the GNU General Public License as published by
  14. the Free Software Foundation; either version 2 of the License, or
  15. (at your option) any later version.
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. GNU General Public License for more details.
  20. You should have received a copy of the GNU General Public License
  21. along with this program; if not, write to the Free Software
  22. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. function dm_text_domain() {
  25. load_plugin_textdomain( 'wordpress-mu-domain-mapping', basename( dirname( __FILE__ ) ) . 'languages', 'wordpress-mu-domain-mapping/languages' );
  26. }
  27. add_action( 'init', 'dm_text_domain' );
  28. function domain_mapping_warning() {
  29. echo "<div id='domain-mapping-warning' class='updated fade'><p><strong>".__( 'Domain Mapping Disabled.', 'wordpress-mu-domain-mapping' )."</strong> ".sprintf(__('You must <a href="%1$s">create a network</a> for it to work.', 'wordpress-mu-domain-mapping' ), "http://codex.wordpress.org/Create_A_Network")."</p></div>";
  30. }
  31. function dm_add_pages() {
  32. global $current_site, $wpdb, $wp_db_version, $wp_version;
  33. if ( !isset( $current_site ) && $wp_db_version >= 15260 ) { // WP 3.0 network hasn't been configured
  34. add_action('admin_notices', 'domain_mapping_warning');
  35. return false;
  36. }
  37. if ( $current_site->path != "/" ) {
  38. wp_die( __( "The domain mapping plugin only works if the site is installed in /. This is a limitation of how virtual servers work and is very difficult to work around.", 'wordpress-mu-domain-mapping' ) );
  39. }
  40. if ( get_site_option( 'dm_user_settings' ) && $current_site->blog_id != $wpdb->blogid && !dm_sunrise_warning( false ) ) {
  41. add_management_page(__( 'Domain Mapping', 'wordpress-mu-domain-mapping'), __( 'Domain Mapping', 'wordpress-mu-domain-mapping'), 'manage_options', 'domainmapping', 'dm_manage_page' );
  42. }
  43. if ( dm_site_admin() && version_compare( $wp_version, '3.0.9', '<=' ) ) {
  44. if ( version_compare( $wp_version, '3.0.1', '<=' ) ) {
  45. add_submenu_page('wpmu-admin.php', __( 'Domain Mapping', 'wordpress-mu-domain-mapping' ), __( 'Domain Mapping', 'wordpress-mu-domain-mapping'), 'manage_options', 'dm_admin_page', 'dm_admin_page');
  46. add_submenu_page('wpmu-admin.php', __( 'Domains', 'wordpress-mu-domain-mapping' ), __( 'Domains', 'wordpress-mu-domain-mapping'), 'manage_options', 'dm_domains_admin', 'dm_domains_admin');
  47. } else {
  48. add_submenu_page('ms-admin.php', __( 'Domain Mapping', 'wordpress-mu-domain-mapping' ), 'Domain Mapping', 'manage_options', 'dm_admin_page', 'dm_admin_page');
  49. add_submenu_page('ms-admin.php', __( 'Domains', 'wordpress-mu-domain-mapping' ), 'Domains', 'manage_options', 'dm_domains_admin', 'dm_domains_admin');
  50. }
  51. }
  52. }
  53. add_action( 'admin_menu', 'dm_add_pages' );
  54. function dm_network_pages() {
  55. add_submenu_page('settings.php', 'Domain Mapping', 'Domain Mapping', 'manage_options', 'dm_admin_page', 'dm_admin_page');
  56. add_submenu_page('settings.php', 'Domains', 'Domains', 'manage_options', 'dm_domains_admin', 'dm_domains_admin');
  57. }
  58. add_action( 'network_admin_menu', 'dm_network_pages' );
  59. // Default Messages for the users Domain Mapping management page
  60. // This can now be replaced by using:
  61. // remove_action('dm_echo_updated_msg','dm_echo_default_updated_msg');
  62. // add_action('dm_echo_updated_msg','my_custom_updated_msg_function');
  63. function dm_echo_default_updated_msg() {
  64. switch( $_GET[ 'updated' ] ) {
  65. case "add":
  66. $msg = __( 'New domain added.', 'wordpress-mu-domain-mapping' );
  67. break;
  68. case "exists":
  69. $msg = __( 'New domain already exists.', 'wordpress-mu-domain-mapping' );
  70. break;
  71. case "primary":
  72. $msg = __( 'New primary domain.', 'wordpress-mu-domain-mapping' );
  73. break;
  74. case "del":
  75. $msg = __( 'Domain deleted.', 'wordpress-mu-domain-mapping' );
  76. break;
  77. }
  78. echo "<div class='updated fade'><p>$msg</p></div>";
  79. }
  80. add_action('dm_echo_updated_msg','dm_echo_default_updated_msg');
  81. function maybe_create_db() {
  82. global $wpdb;
  83. get_dm_hash(); // initialise the remote login hash
  84. $wpdb->dmtable = $wpdb->base_prefix . 'domain_mapping';
  85. $wpdb->dmtablelogins = $wpdb->base_prefix . 'domain_mapping_logins';
  86. if ( dm_site_admin() ) {
  87. $created = 0;
  88. if ( $wpdb->get_var("SHOW TABLES LIKE '{$wpdb->dmtable}'") != $wpdb->dmtable ) {
  89. $wpdb->query( "CREATE TABLE IF NOT EXISTS `{$wpdb->dmtable}` (
  90. `id` bigint(20) NOT NULL auto_increment,
  91. `blog_id` bigint(20) NOT NULL,
  92. `domain` varchar(255) NOT NULL,
  93. `active` tinyint(4) default '1',
  94. PRIMARY KEY (`id`),
  95. KEY `blog_id` (`blog_id`,`domain`,`active`)
  96. );" );
  97. $created = 1;
  98. }
  99. if ( $wpdb->get_var("SHOW TABLES LIKE '{$wpdb->dmtablelogins}'") != $wpdb->dmtablelogins ) {
  100. $wpdb->query( "CREATE TABLE IF NOT EXISTS `{$wpdb->dmtablelogins}` (
  101. `id` varchar(32) NOT NULL,
  102. `user_id` bigint(20) NOT NULL,
  103. `blog_id` bigint(20) NOT NULL,
  104. `t` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  105. PRIMARY KEY (`id`)
  106. );" );
  107. $created = 1;
  108. }
  109. if ( $created ) {
  110. ?> <div id="message" class="updated fade"><p><strong><?php _e( 'Domain mapping database table created.', 'wordpress-mu-domain-mapping' ) ?></strong></p></div> <?php
  111. }
  112. }
  113. }
  114. function dm_domains_admin() {
  115. global $wpdb, $current_site;
  116. if ( false == dm_site_admin() ) { // paranoid? moi?
  117. return false;
  118. }
  119. dm_sunrise_warning();
  120. if ( $current_site->path != "/" ) {
  121. wp_die( sprintf( __( "<strong>Warning!</strong> This plugin will only work if WordPress is installed in the root directory of your webserver. It is currently installed in &#8217;%s&#8217;.", "wordpress-mu-domain-mapping" ), $current_site->path ) );
  122. }
  123. switch( $_POST[ 'action' ] ) {
  124. default:
  125. }
  126. echo '<h2>' . __( 'Domain Mapping: Domains', 'wordpress-mu-domain-mapping' ) . '</h2>';
  127. if ( !empty( $_POST[ 'action' ] ) ) {
  128. check_admin_referer( 'domain_mapping' );
  129. switch( $_POST[ 'action' ] ) {
  130. case "edit":
  131. $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->dmtable} WHERE domain = %s", $_POST[ 'domain' ] ) );
  132. if ( $row ) {
  133. dm_edit_domain( $row );
  134. } else {
  135. echo "<h3>" . __( 'Domain not found', 'wordpress-mu-domain-mapping' ) . "</h3>";
  136. }
  137. break;
  138. case "save":
  139. if ( $_POST[ 'blog_id' ] != 0 AND
  140. $_POST[ 'blog_id' ] != 1 AND
  141. null == $wpdb->get_var( $wpdb->prepare( "SELECT domain FROM {$wpdb->dmtable} WHERE blog_id != %d AND domain = %s", $_POST[ 'blog_id' ], $_POST[ 'domain' ] ) )
  142. ) {
  143. if ( $_POST[ 'orig_domain' ] == '' ) {
  144. $wpdb->query( $wpdb->prepare( "INSERT INTO {$wpdb->dmtable} ( `blog_id`, `domain`, `active` ) VALUES ( %d, %s, %d )", $_POST[ 'blog_id' ], $_POST[ 'domain' ], $_POST[ 'active' ] ) );
  145. echo "<p><strong>" . __( 'Domain Add', 'wordpress-mu-domain-mapping' ) . "</strong></p>";
  146. } else {
  147. $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->dmtable} SET blog_id = %d, domain = %s, active = %d WHERE domain = %s", $_POST[ 'blog_id' ], $_POST[ 'domain' ], $_POST[ 'active' ], $_POST[ 'orig_domain' ] ) );
  148. echo "<p><strong>" . __( 'Domain Updated', 'wordpress-mu-domain-mapping' ) . "</strong></p>";
  149. }
  150. }
  151. break;
  152. case "del":
  153. $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->dmtable} WHERE domain = %s", $_POST[ 'domain' ] ) );
  154. echo "<p><strong>" . __( 'Domain Deleted', 'wordpress-mu-domain-mapping' ) . "</strong></p>";
  155. break;
  156. case "search":
  157. $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->dmtable} WHERE domain LIKE %s", $_POST[ 'domain' ] ) );
  158. dm_domain_listing( $rows, sprintf( __( "Searching for %s", 'wordpress-mu-domain-mapping' ), esc_html( $_POST[ 'domain' ] ) ) );
  159. break;
  160. }
  161. if ( $_POST[ 'action' ] == 'update' ) {
  162. if ( preg_match( '|^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$|', $_POST[ 'ipaddress' ] ) )
  163. add_site_option( 'dm_ipaddress', $_POST[ 'ipaddress' ] );
  164. if ( ! preg_match( '/(--|\.\.)/', $_POST[ 'cname' ] ) && preg_match( '|^([a-zA-Z0-9-\.])+$|', $_POST[ 'cname' ] ) )
  165. add_site_option( 'dm_cname', stripslashes( $_POST[ 'cname' ] ) );
  166. else
  167. add_site_option( 'dm_cname', '' );
  168. add_site_option( 'dm_301_redirect', intval( $_POST[ 'permanent_redirect' ] ) );
  169. }
  170. }
  171. echo "<h3>" . __( 'Search Domains', 'wordpress-mu-domain-mapping' ) . "</h3>";
  172. echo '<form method="POST">';
  173. wp_nonce_field( 'domain_mapping' );
  174. echo '<input type="hidden" name="action" value="search" />';
  175. echo '<p>';
  176. echo _e( "Domain:", 'wordpress-mu-domain-mapping' );
  177. echo " <input type='text' name='domain' value='' /></p>";
  178. echo "<p><input type='submit' class='button-secondary' value='" . __( 'Search', 'wordpress-mu-domain-mapping' ) . "' /></p>";
  179. echo "</form><br />";
  180. dm_edit_domain();
  181. $rows = $wpdb->get_results( "SELECT * FROM {$wpdb->dmtable} ORDER BY id DESC LIMIT 0,20" );
  182. dm_domain_listing( $rows );
  183. echo '<p>' . sprintf( __( '<strong>Note:</strong> %s', 'wordpress-mu-domain-mapping' ), dm_idn_warning() ) . "</p>";
  184. }
  185. function dm_edit_domain( $row = false ) {
  186. if ( is_object( $row ) ) {
  187. echo "<h3>" . __( 'Edit Domain', 'wordpress-mu-domain-mapping' ) . "</h3>";
  188. } else {
  189. echo "<h3>" . __( 'New Domain', 'wordpress-mu-domain-mapping' ) . "</h3>";
  190. $row->blog_id = '';
  191. $row->domain = '';
  192. $_POST[ 'domain' ] = '';
  193. $row->active = 1;
  194. }
  195. echo "<form method='POST'><input type='hidden' name='action' value='save' /><input type='hidden' name='orig_domain' value='" . esc_attr( $_POST[ 'domain' ] ) . "' />";
  196. wp_nonce_field( 'domain_mapping' );
  197. echo "<table class='form-table'>\n";
  198. echo "<tr><th>" . __( 'Site ID', 'wordpress-mu-domain-mapping' ) . "</th><td><input type='text' name='blog_id' value='{$row->blog_id}' /></td></tr>\n";
  199. echo "<tr><th>" . __( 'Domain', 'wordpress-mu-domain-mapping' ) . "</th><td><input type='text' name='domain' value='{$row->domain}' /></td></tr>\n";
  200. echo "<tr><th>" . __( 'Primary', 'wordpress-mu-domain-mapping' ) . "</th><td><input type='checkbox' name='active' value='1' ";
  201. echo $row->active == 1 ? 'checked=1 ' : ' ';
  202. echo "/></td></tr>\n";
  203. if ( get_site_option( 'dm_no_primary_domain' ) == 1 ) {
  204. echo "<tr><td colspan='2'>" . __( '<strong>Warning!</strong> Primary domains are currently disabled.', 'wordpress-mu-domain-mapping' ) . "</td></tr>";
  205. }
  206. echo "</table>";
  207. echo "<p><input type='submit' class='button-primary' value='" .__( 'Save', 'wordpress-mu-domain-mapping' ). "' /></p></form><br /><br />";
  208. }
  209. function dm_domain_listing( $rows, $heading = '' ) {
  210. if ( $rows ) {
  211. if ( file_exists( ABSPATH . 'wp-admin/network/site-info.php' ) ) {
  212. $edit_url = network_admin_url( 'site-info.php' );
  213. } elseif ( file_exists( ABSPATH . 'wp-admin/ms-sites.php' ) ) {
  214. $edit_url = admin_url( 'ms-sites.php' );
  215. } else {
  216. $edit_url = admin_url( 'wpmu-blogs.php' );
  217. }
  218. if ( $heading != '' )
  219. echo "<h3>$heading</h3>";
  220. echo '<table class="widefat" cellspacing="0"><thead><tr><th>'.__( 'Site ID', 'wordpress-mu-domain-mapping' ).'</th><th>'.__( 'Domain', 'wordpress-mu-domain-mapping' ).'</th><th>'.__( 'Primary', 'wordpress-mu-domain-mapping' ).'</th><th>'.__( 'Edit', 'wordpress-mu-domain-mapping' ).'</th><th>'.__( 'Delete', 'wordpress-mu-domain-mapping' ).'</th></tr></thead><tbody>';
  221. foreach( $rows as $row ) {
  222. echo "<tr><td><a href='" . add_query_arg( array( 'action' => 'editblog', 'id' => $row->blog_id ), $edit_url ) . "'>{$row->blog_id}</a></td><td><a href='http://{$row->domain}/'>{$row->domain}</a></td><td>";
  223. echo $row->active == 1 ? __( 'Yes', 'wordpress-mu-domain-mapping' ) : __( 'No', 'wordpress-mu-domain-mapping' );
  224. echo "</td><td><form method='POST'><input type='hidden' name='action' value='edit' /><input type='hidden' name='domain' value='{$row->domain}' />";
  225. wp_nonce_field( 'domain_mapping' );
  226. echo "<input type='submit' class='button-secondary' value='" .__( 'Edit', 'wordpress-mu-domain-mapping' ). "' /></form></td><td><form method='POST'><input type='hidden' name='action' value='del' /><input type='hidden' name='domain' value='{$row->domain}' />";
  227. wp_nonce_field( 'domain_mapping' );
  228. echo "<input type='submit' class='button-secondary' value='" .__( 'Del', 'wordpress-mu-domain-mapping' ). "' /></form>";
  229. echo "</td></tr>";
  230. }
  231. echo '</table>';
  232. if ( get_site_option( 'dm_no_primary_domain' ) == 1 ) {
  233. echo "<p>" . __( '<strong>Warning!</strong> Primary domains are currently disabled.', 'wordpress-mu-domain-mapping' ) . "</p>";
  234. }
  235. }
  236. }
  237. function dm_admin_page() {
  238. global $wpdb, $current_site;
  239. if ( false == dm_site_admin() ) { // paranoid? moi?
  240. return false;
  241. }
  242. dm_sunrise_warning();
  243. maybe_create_db();
  244. if ( $current_site->path != "/" ) {
  245. wp_die( sprintf( __( "<strong>Warning!</strong> This plugin will only work if WordPress is installed in the root directory of your webserver. It is currently installed in &#8217;%s&#8217;.", "wordpress-mu-domain-mapping" ), $current_site->path ) );
  246. }
  247. // set up some defaults
  248. if ( get_site_option( 'dm_remote_login', 'NA' ) == 'NA' )
  249. add_site_option( 'dm_remote_login', 1 );
  250. if ( get_site_option( 'dm_redirect_admin', 'NA' ) == 'NA' )
  251. add_site_option( 'dm_redirect_admin', 1 );
  252. if ( get_site_option( 'dm_user_settings', 'NA' ) == 'NA' )
  253. add_site_option( 'dm_user_settings', 1 );
  254. if ( !empty( $_POST[ 'action' ] ) ) {
  255. check_admin_referer( 'domain_mapping' );
  256. if ( $_POST[ 'action' ] == 'update' ) {
  257. $ipok = true;
  258. $ipaddresses = explode( ',', $_POST[ 'ipaddress' ] );
  259. foreach( $ipaddresses as $address ) {
  260. if ( ( $ip = trim( $address ) ) && !preg_match( '|^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$|', $ip ) ) {
  261. $ipok = false;
  262. break;
  263. }
  264. }
  265. if( $ipok )
  266. add_site_option( 'dm_ipaddress', $_POST[ 'ipaddress' ] );
  267. if ( intval( $_POST[ 'always_redirect_admin' ] ) == 0 )
  268. $_POST[ 'dm_remote_login' ] = 0; // disable remote login if redirecting to mapped domain
  269. add_site_option( 'dm_remote_login', intval( $_POST[ 'dm_remote_login' ] ) );
  270. if ( ! preg_match( '/(--|\.\.)/', $_POST[ 'cname' ] ) && preg_match( '|^([a-zA-Z0-9-\.])+$|', $_POST[ 'cname' ] ) )
  271. add_site_option( 'dm_cname', stripslashes( $_POST[ 'cname' ] ) );
  272. else
  273. add_site_option( 'dm_cname', '' );
  274. add_site_option( 'dm_301_redirect', intval( $_POST[ 'permanent_redirect' ] ) );
  275. add_site_option( 'dm_redirect_admin', intval( $_POST[ 'always_redirect_admin' ] ) );
  276. add_site_option( 'dm_user_settings', intval( $_POST[ 'dm_user_settings' ] ) );
  277. add_site_option( 'dm_no_primary_domain', intval( $_POST[ 'dm_no_primary_domain' ] ) );
  278. }
  279. }
  280. echo '<h3>' . __( 'Domain Mapping Configuration', 'wordpress-mu-domain-mapping' ) . '</h3>';
  281. echo '<form method="POST">';
  282. echo '<input type="hidden" name="action" value="update" />';
  283. echo "<p>" . __( "As a super admin on this network you can set the IP address users need to point their DNS A records at <em>or</em> the domain to point CNAME record at. If you don't know what the IP address is, ping this blog to get it.", 'wordpress-mu-domain-mapping' ) . "</p>";
  284. echo "<p>" . __( "If you use round robin DNS or another load balancing technique with more than one IP, enter each address, separating them by commas.", 'wordpress-mu-domain-mapping' ) . "</p>";
  285. _e( "Server IP Address: ", 'wordpress-mu-domain-mapping' );
  286. echo "<input type='text' name='ipaddress' value='" . get_site_option( 'dm_ipaddress' ) . "' /><br />";
  287. // Using a CNAME is a safer method than using IP adresses for some people (IMHO)
  288. echo "<p>" . __( "If you prefer the use of a CNAME record, you can set the domain here. This domain must be configured with an A record or ANAME pointing at an IP address. Visitors may experience problems if it is a CNAME of another domain.", 'wordpress-mu-domain-mapping' ) . "</p>";
  289. echo "<p>" . __( "NOTE, this voids the use of any IP address set above", 'wordpress-mu-domain-mapping' ) . "</p>";
  290. _e( "Server CNAME domain: ", 'wordpress-mu-domain-mapping' );
  291. echo "<input type='text' name='cname' value='" . get_site_option( 'dm_cname' ) . "' /> (" . dm_idn_warning() . ")<br />";
  292. echo '<p>' . __( 'The information you enter here will be shown to your users so they can configure their DNS correctly. It is for informational purposes only', 'wordpress-mu-domain-mapping' ) . '</p>';
  293. echo "<h3>" . __( 'Domain Options', 'wordpress-mu-domain-mapping' ) . "</h3>";
  294. echo "<ol><li><input type='checkbox' name='dm_remote_login' value='1' ";
  295. echo get_site_option( 'dm_remote_login' ) == 1 ? "checked='checked'" : "";
  296. echo " /> " . __( 'Remote Login', 'wordpress-mu-domain-mapping' ) . "</li>";
  297. echo "<li><input type='checkbox' name='permanent_redirect' value='1' ";
  298. echo get_site_option( 'dm_301_redirect' ) == 1 ? "checked='checked'" : "";
  299. echo " /> " . __( "Permanent redirect (better for your blogger's pagerank)", 'wordpress-mu-domain-mapping' ) . "</li>";
  300. echo "<li><input type='checkbox' name='dm_user_settings' value='1' ";
  301. echo get_site_option( 'dm_user_settings' ) == 1 ? "checked='checked'" : "";
  302. echo " /> " . __( 'User domain mapping page', 'wordpress-mu-domain-mapping' ) . "</li> ";
  303. echo "<li><input type='checkbox' name='always_redirect_admin' value='1' ";
  304. echo get_site_option( 'dm_redirect_admin' ) == 1 ? "checked='checked'" : "";
  305. echo " /> " . __( "Redirect administration pages to site's original domain (remote login disabled if this redirect is disabled)", 'wordpress-mu-domain-mapping' ) . "</li>";
  306. echo "<li><input type='checkbox' name='dm_no_primary_domain' value='1' ";
  307. echo get_site_option( 'dm_no_primary_domain' ) == 1 ? "checked='checked'" : "";
  308. echo " /> " . __( "Disable primary domain check. Sites will not redirect to one domain name. May cause duplicate content issues.", 'wordpress-mu-domain-mapping' ) . "</li></ol>";
  309. wp_nonce_field( 'domain_mapping' );
  310. echo "<p><input class='button-primary' type='submit' value='" . __( "Save", 'wordpress-mu-domain-mapping' ) . "' /></p>";
  311. echo "</form><br />";
  312. }
  313. function dm_handle_actions() {
  314. global $wpdb, $parent_file;
  315. $url = add_query_arg( array( 'page' => 'domainmapping' ), admin_url( $parent_file ) );
  316. if ( !empty( $_POST[ 'action' ] ) ) {
  317. $domain = $wpdb->escape( $_POST[ 'domain' ] );
  318. if ( $domain == '' ) {
  319. wp_die( "You must enter a domain" );
  320. }
  321. check_admin_referer( 'domain_mapping' );
  322. do_action('dm_handle_actions_init', $domain);
  323. switch( $_POST[ 'action' ] ) {
  324. case "add":
  325. do_action('dm_handle_actions_add', $domain);
  326. if( null == $wpdb->get_row( "SELECT blog_id FROM {$wpdb->blogs} WHERE domain = '$domain'" ) && null == $wpdb->get_row( "SELECT blog_id FROM {$wpdb->dmtable} WHERE domain = '$domain'" ) ) {
  327. if ( $_POST[ 'primary' ] ) {
  328. $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->dmtable} SET active = 0 WHERE blog_id = %d", $wpdb->blogid ) );
  329. }
  330. $wpdb->query( $wpdb->prepare( "INSERT INTO {$wpdb->dmtable} ( `id` , `blog_id` , `domain` , `active` ) VALUES ( NULL, %d, %s, %d )", $wpdb->blogid, $domain, $_POST[ 'primary' ] ) );
  331. wp_redirect( add_query_arg( array( 'updated' => 'add' ), $url ) );
  332. exit;
  333. } else {
  334. wp_redirect( add_query_arg( array( 'updated' => 'exists' ), $url ) );
  335. exit;
  336. }
  337. break;
  338. case "primary":
  339. do_action('dm_handle_actions_primary', $domain);
  340. $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->dmtable} SET active = 0 WHERE blog_id = %d", $wpdb->blogid ) );
  341. $orig_url = parse_url( get_original_url( 'siteurl' ) );
  342. if( $domain != $orig_url[ 'host' ] ) {
  343. $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->dmtable} SET active = 1 WHERE domain = %s", $domain ) );
  344. }
  345. wp_redirect( add_query_arg( array( 'updated' => 'primary' ), $url ) );
  346. exit;
  347. break;
  348. }
  349. } elseif( $_GET[ 'action' ] == 'delete' ) {
  350. $domain = $wpdb->escape( $_GET[ 'domain' ] );
  351. if ( $domain == '' ) {
  352. wp_die( __( "You must enter a domain", 'wordpress-mu-domain-mapping' ) );
  353. }
  354. check_admin_referer( "delete" . $_GET['domain'] );
  355. do_action('dm_handle_actions_del', $domain);
  356. $wpdb->query( "DELETE FROM {$wpdb->dmtable} WHERE domain = '$domain'" );
  357. wp_redirect( add_query_arg( array( 'updated' => 'del' ), $url ) );
  358. exit;
  359. }
  360. }
  361. if ( isset( $_GET[ 'page' ] ) && $_GET[ 'page' ] == 'domainmapping' )
  362. add_action( 'admin_init', 'dm_handle_actions' );
  363. function dm_sunrise_warning( $die = true ) {
  364. if ( !file_exists( WP_CONTENT_DIR . '/sunrise.php' ) ) {
  365. if ( !$die )
  366. return true;
  367. if ( dm_site_admin() ) {
  368. wp_die( sprintf( __( "Please copy sunrise.php to %s/sunrise.php and ensure the SUNRISE definition is in %swp-config.php", 'wordpress-mu-domain-mapping' ), WP_CONTENT_DIR, ABSPATH ) );
  369. } else {
  370. wp_die( __( "This plugin has not been configured correctly yet.", 'wordpress-mu-domain-mapping' ) );
  371. }
  372. } elseif ( !defined( 'SUNRISE' ) ) {
  373. if ( !$die )
  374. return true;
  375. if ( dm_site_admin() ) {
  376. wp_die( sprintf( __( "Please uncomment the line <em>define( 'SUNRISE', 'on' );</em> or add it to your %swp-config.php", 'wordpress-mu-domain-mapping' ), ABSPATH ) );
  377. } else {
  378. wp_die( __( "This plugin has not been configured correctly yet.", 'wordpress-mu-domain-mapping' ) );
  379. }
  380. } elseif ( !defined( 'SUNRISE_LOADED' ) ) {
  381. if ( !$die )
  382. return true;
  383. if ( dm_site_admin() ) {
  384. wp_die( sprintf( __( "Please edit your %swp-config.php and move the line <em>define( 'SUNRISE', 'on' );</em> above the last require_once() in that file or make sure you updated sunrise.php.", 'wordpress-mu-domain-mapping' ), ABSPATH ) );
  385. } else {
  386. wp_die( __( "This plugin has not been configured correctly yet.", 'wordpress-mu-domain-mapping' ) );
  387. }
  388. }
  389. return false;
  390. }
  391. function dm_manage_page() {
  392. global $wpdb, $parent_file;
  393. if ( isset( $_GET[ 'updated' ] ) ) {
  394. do_action('dm_echo_updated_msg');
  395. }
  396. dm_sunrise_warning();
  397. echo "<div class='wrap'><h2>" . __( 'Domain Mapping', 'wordpress-mu-domain-mapping' ) . "</h2>";
  398. if ( false == get_site_option( 'dm_ipaddress' ) && false == get_site_option( 'dm_cname' ) ) {
  399. if ( dm_site_admin() ) {
  400. _e( "Please set the IP address or CNAME of your server in the <a href='wpmu-admin.php?page=dm_admin_page'>site admin page</a>.", 'wordpress-mu-domain-mapping' );
  401. } else {
  402. _e( "This plugin has not been configured correctly yet.", 'wordpress-mu-domain-mapping' );
  403. }
  404. echo "</div>";
  405. return false;
  406. }
  407. if ( false == isset( $_SERVER[ 'HTTPS' ] ) )
  408. $_SERVER[ 'HTTPS' ] = 'Off';
  409. $protocol = ( 'on' == strtolower( $_SERVER[ 'HTTPS' ] ) ) ? 'https://' : 'http://';
  410. $domains = $wpdb->get_results( "SELECT * FROM {$wpdb->dmtable} WHERE blog_id = '{$wpdb->blogid}'", ARRAY_A );
  411. if ( is_array( $domains ) && !empty( $domains ) ) {
  412. $orig_url = parse_url( get_original_url( 'siteurl' ) );
  413. $domains[] = array( 'domain' => $orig_url[ 'host' ], 'path' => $orig_url[ 'path' ], 'active' => 0 );
  414. echo "<h3>" . __( 'Active domains on this blog', 'wordpress-mu-domain-mapping' ) . "</h3>";
  415. echo '<form method="POST">';
  416. echo "<table><tr><th>" . __( 'Primary', 'wordpress-mu-domain-mapping' ) . "</th><th>" . __( 'Domain', 'wordpress-mu-domain-mapping' ) . "</th><th>" . __( 'Delete', 'wordpress-mu-domain-mapping' ) . "</th></tr>\n";
  417. $primary_found = 0;
  418. $del_url = add_query_arg( array( 'page' => 'domainmapping', 'action' => 'delete' ), admin_url( $parent_file ) );
  419. foreach( $domains as $details ) {
  420. if ( 0 == $primary_found && $details[ 'domain' ] == $orig_url[ 'host' ] ) {
  421. $details[ 'active' ] = 1;
  422. }
  423. echo "<tr><td>";
  424. echo "<input type='radio' name='domain' value='{$details[ 'domain' ]}' ";
  425. if ( $details[ 'active' ] == 1 )
  426. echo "checked='1' ";
  427. echo "/>";
  428. $url = "{$protocol}{$details[ 'domain' ]}{$details[ 'path' ]}";
  429. echo "</td><td><a href='$url'>$url</a></td><td style='text-align: center'>";
  430. if ( $details[ 'domain' ] != $orig_url[ 'host' ] && $details[ 'active' ] != 1 ) {
  431. echo "<a href='" . wp_nonce_url( add_query_arg( array( 'domain' => $details[ 'domain' ] ), $del_url ), "delete" . $details[ 'domain' ] ) . "'>Del</a>";
  432. }
  433. echo "</td></tr>";
  434. if ( 0 == $primary_found )
  435. $primary_found = $details[ 'active' ];
  436. }
  437. ?></table><?php
  438. echo '<input type="hidden" name="action" value="primary" />';
  439. echo "<p><input type='submit' class='button-primary' value='" . __( 'Set Primary Domain', 'wordpress-mu-domain-mapping' ) . "' /></p>";
  440. wp_nonce_field( 'domain_mapping' );
  441. echo "</form>";
  442. echo "<p>" . __( "* The primary domain cannot be deleted.", 'wordpress-mu-domain-mapping' ) . "</p>";
  443. if ( get_site_option( 'dm_no_primary_domain' ) == 1 ) {
  444. echo __( '<strong>Warning!</strong> Primary domains are currently disabled.', 'wordpress-mu-domain-mapping' );
  445. }
  446. }
  447. echo "<h3>" . __( 'Add new domain', 'wordpress-mu-domain-mapping' ) . "</h3>";
  448. echo '<form method="POST">';
  449. echo '<input type="hidden" name="action" value="add" />';
  450. echo "<p>http://<input type='text' name='domain' value='' />/<br />";
  451. wp_nonce_field( 'domain_mapping' );
  452. echo "<input type='checkbox' name='primary' value='1' /> " . __( 'Primary domain for this blog', 'wordpress-mu-domain-mapping' ) . "</p>";
  453. echo "<p><input type='submit' class='button-secondary' value='" . __( "Add", 'wordpress-mu-domain-mapping' ) . "' /></p>";
  454. echo "</form><br />";
  455. if ( get_site_option( 'dm_cname' ) ) {
  456. $dm_cname = get_site_option( 'dm_cname');
  457. echo "<p>" . sprintf( __( 'If you want to redirect a domain you will need to add a DNS "CNAME" record pointing to the following domain name for this server: <strong>%s</strong>', 'wordpress-mu-domain-mapping' ), $dm_cname ) . "</p>";
  458. echo "<p>" . __( 'Google have published <a href="http://www.google.com/support/blogger/bin/answer.py?hl=en&answer=58317" target="_blank">instructions</a> for creating CNAME records on various hosting platforms such as GoDaddy and others.', 'wordpress-mu-domain-mapping' ) . "</p>";
  459. } else {
  460. echo "<p>" . __( 'If your domain name includes a hostname like "www", "blog" or some other prefix before the actual domain name you will need to add a CNAME for that hostname in your DNS pointing at this blog URL.', 'wordpress-mu-domain-mapping' ) . "</p>";
  461. $dm_ipaddress = get_site_option( 'dm_ipaddress', 'IP not set by admin yet.' );
  462. if ( strpos( $dm_ipaddress, ',' ) ) {
  463. echo "<p>" . sprintf( __( 'If you want to redirect a domain you will need to add DNS "A" records pointing at the IP addresses of this server: <strong>%s</strong>', 'wordpress-mu-domain-mapping' ), $dm_ipaddress ) . "</p>";
  464. } else {
  465. echo "<p>" . sprintf( __( 'If you want to redirect a domain you will need to add a DNS "A" record pointing at the IP address of this server: <strong>%s</strong>', 'wordpress-mu-domain-mapping' ), $dm_ipaddress ) . "</p>";
  466. }
  467. }
  468. echo '<p>' . sprintf( __( '<strong>Note:</strong> %s', 'wordpress-mu-domain-mapping' ), dm_idn_warning() ) . "</p>";
  469. echo "</div>";
  470. }
  471. function domain_mapping_siteurl( $setting ) {
  472. global $wpdb, $current_blog;
  473. // To reduce the number of database queries, save the results the first time we encounter each blog ID.
  474. static $return_url = array();
  475. $wpdb->dmtable = $wpdb->base_prefix . 'domain_mapping';
  476. if ( !isset( $return_url[ $wpdb->blogid ] ) ) {
  477. $s = $wpdb->suppress_errors();
  478. if ( get_site_option( 'dm_no_primary_domain' ) == 1 ) {
  479. $domain = $wpdb->get_var( "SELECT domain FROM {$wpdb->dmtable} WHERE blog_id = '{$wpdb->blogid}' AND domain = '" . $wpdb->escape( $_SERVER[ 'HTTP_HOST' ] ) . "' LIMIT 1" );
  480. if ( null == $domain ) {
  481. $return_url[ $wpdb->blogid ] = untrailingslashit( get_original_url( "siteurl" ) );
  482. return $return_url[ $wpdb->blogid ];
  483. }
  484. } else {
  485. // get primary domain, if we don't have one then return original url.
  486. $domain = $wpdb->get_var( "SELECT domain FROM {$wpdb->dmtable} WHERE blog_id = '{$wpdb->blogid}' AND active = 1 LIMIT 1" );
  487. if ( null == $domain ) {
  488. $return_url[ $wpdb->blogid ] = untrailingslashit( get_original_url( "siteurl" ) );
  489. return $return_url[ $wpdb->blogid ];
  490. }
  491. }
  492. $wpdb->suppress_errors( $s );
  493. if ( false == isset( $_SERVER[ 'HTTPS' ] ) )
  494. $_SERVER[ 'HTTPS' ] == 'Off';
  495. $protocol = ( 'on' == strtolower( $_SERVER[ 'HTTPS' ] ) ) ? 'https://' : 'http://';
  496. if ( $domain ) {
  497. $return_url[ $wpdb->blogid ] = untrailingslashit( $protocol . $domain );
  498. $setting = $return_url[ $wpdb->blogid ];
  499. } else {
  500. $return_url[ $wpdb->blogid ] = false;
  501. }
  502. } elseif ( $return_url[ $wpdb->blogid ] !== FALSE) {
  503. $setting = $return_url[ $wpdb->blogid ];
  504. }
  505. return $setting;
  506. }
  507. // url is siteurl or home
  508. function get_original_url( $url, $blog_id = 0 ) {
  509. global $wpdb;
  510. if ( $blog_id != 0 ) {
  511. $id = $blog_id;
  512. } else {
  513. $id = $wpdb->blogid;
  514. }
  515. static $orig_urls = array();
  516. if ( ! isset( $orig_urls[ $id ] ) ) {
  517. if ( defined( 'DOMAIN_MAPPING' ) )
  518. remove_filter( 'pre_option_' . $url, 'domain_mapping_' . $url );
  519. if ( $blog_id == 0 ) {
  520. $orig_url = get_option( $url );
  521. } else {
  522. $orig_url = get_blog_option( $blog_id, $url );
  523. }
  524. if ( isset( $_SERVER[ 'HTTPS' ] ) && 'on' == strtolower( $_SERVER[ 'HTTPS' ] ) ) {
  525. $orig_url = str_replace( "http://", "https://", $orig_url );
  526. } else {
  527. $orig_url = str_replace( "https://", "http://", $orig_url );
  528. }
  529. if ( $blog_id == 0 ) {
  530. $orig_urls[ $wpdb->blogid ] = $orig_url;
  531. } else {
  532. $orig_urls[ $blog_id ] = $orig_url;
  533. }
  534. if ( defined( 'DOMAIN_MAPPING' ) )
  535. add_filter( 'pre_option_' . $url, 'domain_mapping_' . $url );
  536. }
  537. return $orig_urls[ $id ];
  538. }
  539. function domain_mapping_adminurl( $url, $path, $blog_id = 0 ) {
  540. $index = strpos( $url, '/wp-admin' );
  541. if( $index !== false ) {
  542. $url = get_original_url( 'siteurl', $blog_id ) . substr( $url, $index );
  543. // make sure admin_url is ssl if current page is ssl, or admin ssl is forced
  544. if( ( is_ssl() || force_ssl_admin() ) && 0 === strpos( $url, 'http://' ) ) {
  545. $url = 'https://' . substr( $url, 7 );
  546. }
  547. }
  548. return $url;
  549. }
  550. function domain_mapping_post_content( $post_content ) {
  551. global $wpdb;
  552. $orig_url = get_original_url( 'siteurl' );
  553. $url = domain_mapping_siteurl( 'NA' );
  554. if ( $url == 'NA' )
  555. return $post_content;
  556. return str_replace( $orig_url, $url, $post_content );
  557. }
  558. function dm_redirect_admin() {
  559. if ( get_site_option( 'dm_redirect_admin' ) ) {
  560. // redirect mapped domain admin page to original url
  561. $url = get_original_url( 'siteurl' );
  562. if ( false === strpos( $url, $_SERVER[ 'HTTP_HOST' ] ) ) {
  563. wp_redirect( untrailingslashit( $url ) . $_SERVER[ 'REQUEST_URI' ] );
  564. exit;
  565. }
  566. } else {
  567. global $current_blog;
  568. // redirect original url to primary domain wp-admin/ - remote login is disabled!
  569. $url = domain_mapping_siteurl( false );
  570. $request_uri = str_replace( $current_blog->path, '/', $_SERVER[ 'REQUEST_URI' ] );
  571. if ( false === strpos( $url, $_SERVER[ 'HTTP_HOST' ] ) ) {
  572. wp_redirect( str_replace( '//wp-admin', '/wp-admin', trailingslashit( $url ) . $request_uri ) );
  573. exit;
  574. }
  575. }
  576. }
  577. function redirect_login_to_orig() {
  578. if ( !get_site_option( 'dm_remote_login' ) || $_GET[ 'action' ] == 'logout' || isset( $_GET[ 'loggedout' ] ) ) {
  579. return false;
  580. }
  581. $url = get_original_url( 'siteurl' );
  582. if ( $url != site_url() ) {
  583. $url .= "/wp-login.php";
  584. echo "<script type='text/javascript'>\nwindow.location = '$url'</script>";
  585. }
  586. }
  587. // fixes the plugins_url
  588. function domain_mapping_plugins_uri( $full_url, $path=NULL, $plugin=NULL ) {
  589. return get_option( 'siteurl' ) . substr( $full_url, stripos( $full_url, PLUGINDIR ) - 1 );
  590. }
  591. function domain_mapping_themes_uri( $full_url ) {
  592. return str_replace( get_original_url ( 'siteurl' ), get_option( 'siteurl' ), $full_url );
  593. }
  594. if ( defined( 'DOMAIN_MAPPING' ) ) {
  595. add_filter( 'plugins_url', 'domain_mapping_plugins_uri', 1 );
  596. add_filter( 'theme_root_uri', 'domain_mapping_themes_uri', 1 );
  597. add_filter( 'pre_option_siteurl', 'domain_mapping_siteurl' );
  598. add_filter( 'pre_option_home', 'domain_mapping_siteurl' );
  599. add_filter( 'the_content', 'domain_mapping_post_content' );
  600. add_action( 'wp_head', 'remote_login_js_loader' );
  601. add_action( 'login_head', 'redirect_login_to_orig' );
  602. add_action( 'wp_logout', 'remote_logout_loader', 9999 );
  603. add_filter( 'stylesheet_uri', 'domain_mapping_post_content' );
  604. add_filter( 'stylesheet_directory', 'domain_mapping_post_content' );
  605. add_filter( 'stylesheet_directory_uri', 'domain_mapping_post_content' );
  606. add_filter( 'template_directory', 'domain_mapping_post_content' );
  607. add_filter( 'template_directory_uri', 'domain_mapping_post_content' );
  608. add_filter( 'plugins_url', 'domain_mapping_post_content' );
  609. } else {
  610. if ( $wp_version == '2.9.2' ) {
  611. add_filter( 'admin_url', 'domain_mapping_adminurl', 10, 2 );
  612. } else {
  613. add_filter( 'admin_url', 'domain_mapping_adminurl', 10, 3 );
  614. }
  615. }
  616. add_action( 'admin_init', 'dm_redirect_admin' );
  617. if ( isset( $_GET[ 'dm' ] ) )
  618. add_action( 'template_redirect', 'remote_login_js' );
  619. function remote_logout_loader() {
  620. global $current_site, $current_blog, $wpdb;
  621. $wpdb->dmtablelogins = $wpdb->base_prefix . 'domain_mapping_logins';
  622. if ( false == isset( $_SERVER[ 'HTTPS' ] ) )
  623. $_SERVER[ 'HTTPS' ] == 'Off';
  624. $protocol = ( 'on' == strtolower( $_SERVER[ 'HTTPS' ] ) ) ? 'https://' : 'http://';
  625. $hash = get_dm_hash();
  626. $key = md5( time() );
  627. $wpdb->query( $wpdb->prepare( "INSERT INTO {$wpdb->dmtablelogins} ( `id`, `user_id`, `blog_id`, `t` ) VALUES( %s, 0, %d, NOW() )", $key, $current_blog->blog_id ) );
  628. if ( get_site_option( 'dm_redirect_admin' ) ) {
  629. wp_redirect( $protocol . $current_site->domain . $current_site->path . "?dm={$hash}&action=logout&blogid={$current_blog->blog_id}&k={$key}&t=" . mt_rand() );
  630. exit;
  631. }
  632. }
  633. function redirect_to_mapped_domain() {
  634. global $current_blog, $wpdb;
  635. if ( !isset( $_SERVER[ 'HTTPS' ] ) )
  636. $_SERVER[ 'HTTPS' ] = "off";
  637. $protocol = ( 'on' == strtolower($_SERVER['HTTPS']) ) ? 'https://' : 'http://';
  638. $url = domain_mapping_siteurl( false );
  639. if ( $url && $url != untrailingslashit( $protocol . $current_blog->domain . $current_blog->path ) ) {
  640. $redirect = get_site_option( 'dm_301_redirect' ) ? '301' : '302';
  641. if ( ( defined( 'VHOST' ) && constant( "VHOST" ) != 'yes' ) || ( defined( 'SUBDOMAIN_INSTALL' ) && constant( 'SUBDOMAIN_INSTALL' ) == false ) ) {
  642. $_SERVER[ 'REQUEST_URI' ] = str_replace( $current_blog->path, '/', $_SERVER[ 'REQUEST_URI' ] );
  643. }
  644. header( "Location: {$url}{$_SERVER[ 'REQUEST_URI' ]}", true, $redirect );
  645. exit;
  646. }
  647. }
  648. add_action( 'template_redirect', 'redirect_to_mapped_domain' );
  649. function get_dm_hash() {
  650. $remote_login_hash = get_site_option( 'dm_hash' );
  651. if ( null == $remote_login_hash ) {
  652. $remote_login_hash = md5( time() );
  653. update_site_option( 'dm_hash', $remote_login_hash );
  654. }
  655. return $remote_login_hash;
  656. }
  657. function remote_login_js() {
  658. global $current_blog, $current_user, $wpdb;
  659. if ( 0 == get_site_option( 'dm_remote_login' ) )
  660. return false;
  661. $wpdb->dmtablelogins = $wpdb->base_prefix . 'domain_mapping_logins';
  662. $hash = get_dm_hash();
  663. if ( false == isset( $_SERVER[ 'HTTPS' ] ) )
  664. $_SERVER[ 'HTTPS' ] == 'Off';
  665. $protocol = ( 'on' == strtolower( $_SERVER[ 'HTTPS' ] ) ) ? 'https://' : 'http://';
  666. if ( $_GET[ 'dm' ] == $hash ) {
  667. if ( $_GET[ 'action' ] == 'load' ) {
  668. if ( !is_user_logged_in() )
  669. exit;
  670. $key = md5( time() . mt_rand() );
  671. $wpdb->query( $wpdb->prepare( "INSERT INTO {$wpdb->dmtablelogins} ( `id`, `user_id`, `blog_id`, `t` ) VALUES( %s, %d, %d, NOW() )", $key, $current_user->ID, $_GET[ 'blogid' ] ) );
  672. $url = add_query_arg( array( 'action' => 'login', 'dm' => $hash, 'k' => $key, 't' => mt_rand() ), $_GET[ 'back' ] );
  673. echo "window.location = '$url'";
  674. exit;
  675. } elseif ( $_GET[ 'action' ] == 'login' ) {
  676. if ( $details = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->dmtablelogins} WHERE id = %s AND blog_id = %d", $_GET[ 'k' ], $wpdb->blogid ) ) ) {
  677. if ( $details->blog_id == $wpdb->blogid ) {
  678. $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->dmtablelogins} WHERE id = %s", $_GET[ 'k' ] ) );
  679. $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->dmtablelogins} WHERE t < %d", ( time() - 120 ) ) ); // remote logins survive for only 2 minutes if not used.
  680. wp_set_auth_cookie( $details->user_id );
  681. wp_redirect( remove_query_arg( array( 'dm', 'action', 'k', 't', $protocol . $current_blog->domain . $_SERVER[ 'REQUEST_URI' ] ) ) );
  682. exit;
  683. } else {
  684. wp_die( __( "Incorrect or out of date login key", 'wordpress-mu-domain-mapping' ) );
  685. }
  686. } else {
  687. wp_die( __( "Unknown login key", 'wordpress-mu-domain-mapping' ) );
  688. }
  689. } elseif ( $_GET[ 'action' ] == 'logout' ) {
  690. if ( $details = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->dmtablelogins} WHERE id = %d AND blog_id = %d", $_GET[ 'k' ], $_GET[ 'blogid' ] ) ) ) {
  691. $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->dmtablelogins} WHERE id = %s", $_GET[ 'k' ] ) );
  692. $blog = get_blog_details( $_GET[ 'blogid' ] );
  693. wp_clear_auth_cookie();
  694. wp_redirect( trailingslashit( $blog->siteurl ) . "wp-login.php?loggedout=true" );
  695. exit;
  696. } else {
  697. wp_die( __( "Unknown logout key", 'wordpress-mu-domain-mapping' ) );
  698. }
  699. }
  700. }
  701. }
  702. function remote_login_js_loader() {
  703. global $current_site, $current_blog;
  704. if ( 0 == get_site_option( 'dm_remote_login' ) || is_user_logged_in() )
  705. return false;
  706. if ( false == isset( $_SERVER[ 'HTTPS' ] ) )
  707. $_SERVER[ 'HTTPS' ] == 'Off';
  708. $protocol = ( 'on' == strtolower( $_SERVER[ 'HTTPS' ] ) ) ? 'https://' : 'http://';
  709. $hash = get_dm_hash();
  710. echo "<script src='{$protocol}{$current_site->domain}{$current_site->path}?dm={$hash}&amp;action=load&amp;blogid={$current_blog->blog_id}&amp;siteid={$current_blog->site_id}&amp;t=" . mt_rand() . "&amp;back=" . urlencode( $protocol . $current_blog->domain . $_SERVER[ 'REQUEST_URI' ] ) . "' type='text/javascript'></script>";
  711. }
  712. // delete mapping if blog is deleted
  713. function delete_blog_domain_mapping( $blog_id, $drop ) {
  714. global $wpdb;
  715. $wpdb->dmtable = $wpdb->base_prefix . 'domain_mapping';
  716. if ( $blog_id && $drop ) {
  717. // Get an array of domain names to pass onto any delete_blog_domain_mapping actions
  718. $domains = $wpdb->get_col( $wpdb->prepare( "SELECT domain FROM {$wpdb->dmtable} WHERE blog_id = %d", $blog_id ) );
  719. do_action('dm_delete_blog_domain_mappings', $domains);
  720. $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->dmtable} WHERE blog_id = %d", $blog_id ) );
  721. }
  722. }
  723. add_action( 'delete_blog', 'delete_blog_domain_mapping', 1, 2 );
  724. // show mapping on site admin blogs screen
  725. function ra_domain_mapping_columns( $columns ) {
  726. $columns[ 'map' ] = __( 'Mapping' );
  727. return $columns;
  728. }
  729. add_filter( 'wpmu_blogs_columns', 'ra_domain_mapping_columns' );
  730. function ra_domain_mapping_field( $column, $blog_id ) {
  731. global $wpdb;
  732. static $maps = false;
  733. if ( $column == 'map' ) {
  734. if ( $maps === false ) {
  735. $wpdb->dmtable = $wpdb->base_prefix . 'domain_mapping';
  736. $work = $wpdb->get_results( "SELECT blog_id, domain FROM {$wpdb->dmtable} ORDER BY blog_id" );
  737. $maps = array();
  738. if($work) {
  739. foreach( $work as $blog ) {
  740. $maps[ $blog->blog_id ][] = $blog->domain;
  741. }
  742. }
  743. }
  744. if( !empty( $maps[ $blog_id ] ) && is_array( $maps[ $blog_id ] ) ) {
  745. foreach( $maps[ $blog_id ] as $blog ) {
  746. echo $blog . '<br />';
  747. }
  748. }
  749. }
  750. }
  751. add_action( 'manage_blogs_custom_column', 'ra_domain_mapping_field', 1, 3 );
  752. add_action( 'manage_sites_custom_column', 'ra_domain_mapping_field', 1, 3 );
  753. function dm_site_admin() {
  754. if ( function_exists( 'is_super_admin' ) ) {
  755. return is_super_admin();
  756. } elseif ( function_exists( 'is_site_admin' ) ) {
  757. return is_site_admin();
  758. } else {
  759. return true;
  760. }
  761. }
  762. function dm_idn_warning() {
  763. return sprintf( __( 'International Domain Names should be in <a href="%s">punycode</a> format.', 'wordpress-mu-domain-mapping' ), "http://api.webnic.cc/idnconversion.html" );
  764. }
  765. ?>