PageRenderTime 66ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/ms-functions.php

https://bitbucket.org/MaheshDhaduk/androidmobiles
PHP | 1447 lines | 1077 code | 289 blank | 81 comment | 334 complexity | 6aed96c038ac30808563987ee6d62da2 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. /**
  3. * Multi-site WordPress API
  4. *
  5. * @package WordPress
  6. * @subpackage Multisite
  7. * @since 3.0.0
  8. */
  9. function get_sitestats() {
  10. global $wpdb;
  11. $stats['blogs'] = get_blog_count();
  12. $count_ts = get_site_option( 'user_count_ts' );
  13. if ( time() - $count_ts > 3600 ) {
  14. $count = $wpdb->get_var( "SELECT COUNT(ID) FROM $wpdb->users" );
  15. update_site_option( 'user_count', $count );
  16. update_site_option( 'user_count_ts', time() );
  17. } else {
  18. $count = get_site_option( 'user_count' );
  19. }
  20. $stats['users'] = $count;
  21. return $stats;
  22. }
  23. function get_admin_users_for_domain( $sitedomain = '', $path = '' ) {
  24. global $wpdb;
  25. if ( ! $sitedomain )
  26. $site_id = $wpdb->siteid;
  27. else
  28. $site_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->site WHERE domain = %s AND path = %s", $sitedomain, $path ) );
  29. if ( $site_id )
  30. return $wpdb->get_results( $wpdb->prepare( "SELECT u.ID, u.user_login, u.user_pass FROM $wpdb->users AS u, $wpdb->sitemeta AS sm WHERE sm.meta_key = 'admin_user_id' AND u.ID = sm.meta_value AND sm.site_id = %d", $site_id ), ARRAY_A );
  31. return false;
  32. }
  33. function get_blogs_of_user( $id, $all = false ) {
  34. global $wpdb;
  35. $cache_suffix = $all ? '_all' : '_short';
  36. $return = wp_cache_get( 'blogs_of_user_' . $id . $cache_suffix, 'users' );
  37. if ( $return )
  38. return apply_filters( 'get_blogs_of_user', $return, $id, $all );
  39. $user = get_userdata( (int) $id );
  40. if ( !$user )
  41. return false;
  42. $blogs = $match = array();
  43. $prefix_length = strlen($wpdb->base_prefix);
  44. foreach ( (array) $user as $key => $value ) {
  45. if ( $prefix_length && substr($key, 0, $prefix_length) != $wpdb->base_prefix )
  46. continue;
  47. if ( substr($key, -12, 12) != 'capabilities' )
  48. continue;
  49. if ( preg_match( '/^' . $wpdb->base_prefix . '((\d+)_)?capabilities$/', $key, $match ) ) {
  50. if ( count( $match ) > 2 )
  51. $blog_id = $match[ 2 ];
  52. else
  53. $blog_id = 1;
  54. $blog = get_blog_details( $blog_id );
  55. if ( $blog && isset( $blog->domain ) && ( $all == true || $all == false && ( $blog->archived == 0 && $blog->spam == 0 && $blog->deleted == 0 ) ) ) {
  56. $blogs[ $blog_id ]->userblog_id = $blog_id;
  57. $blogs[ $blog_id ]->blogname = $blog->blogname;
  58. $blogs[ $blog_id ]->domain = $blog->domain;
  59. $blogs[ $blog_id ]->path = $blog->path;
  60. $blogs[ $blog_id ]->site_id = $blog->site_id;
  61. $blogs[ $blog_id ]->siteurl = $blog->siteurl;
  62. }
  63. }
  64. }
  65. wp_cache_add( 'blogs_of_user_' . $id . $cache_suffix, $blogs, 'users', 5 );
  66. return apply_filters( 'get_blogs_of_user', $blogs, $id, $all );
  67. }
  68. function get_active_blog_for_user( $user_id ) { // get an active blog for user - either primary blog or from blogs list
  69. global $wpdb;
  70. $blogs = get_blogs_of_user( $user_id );
  71. if ( empty( $blogs ) ) {
  72. $details = get_dashboard_blog();
  73. add_user_to_blog( $details->blog_id, $user_id, 'subscriber' );
  74. update_user_meta( $user_id, 'primary_blog', $details->blog_id );
  75. wp_cache_delete( $user_id, 'users' );
  76. return $details;
  77. }
  78. $primary_blog = get_user_meta( $user_id, 'primary_blog', true );
  79. $details = get_dashboard_blog();
  80. if ( $primary_blog ) {
  81. $blogs = get_blogs_of_user( $user_id );
  82. if ( isset( $blogs[ $primary_blog ] ) == false ) {
  83. add_user_to_blog( $details->blog_id, $user_id, 'subscriber' );
  84. update_user_meta( $user_id, 'primary_blog', $details->blog_id );
  85. wp_cache_delete( $user_id, 'users' );
  86. } else {
  87. $details = get_blog_details( $primary_blog );
  88. }
  89. } else {
  90. add_user_to_blog( $details->blog_id, $user_id, 'subscriber' ); // Add subscriber permission for dashboard blog
  91. update_user_meta( $user_id, 'primary_blog', $details->blog_id );
  92. }
  93. if ( ( is_object( $details ) == false ) || ( is_object( $details ) && $details->archived == 1 || $details->spam == 1 || $details->deleted == 1 ) ) {
  94. $blogs = get_blogs_of_user( $user_id, true ); // if a user's primary blog is shut down, check their other blogs.
  95. $ret = false;
  96. if ( is_array( $blogs ) && count( $blogs ) > 0 ) {
  97. foreach ( (array) $blogs as $blog_id => $blog ) {
  98. if ( $blog->site_id != $wpdb->siteid )
  99. continue;
  100. $details = get_blog_details( $blog_id );
  101. if ( is_object( $details ) && $details->archived == 0 && $details->spam == 0 && $details->deleted == 0 ) {
  102. $ret = $blog;
  103. $changed = false;
  104. if ( get_user_meta( $user_id , 'primary_blog', true ) != $blog_id ) {
  105. update_user_meta( $user_id, 'primary_blog', $blog_id );
  106. $changed = true;
  107. }
  108. if ( !get_user_meta($user_id , 'source_domain', true) ) {
  109. update_user_meta( $user_id, 'source_domain', $blog->domain );
  110. $changed = true;
  111. }
  112. if ( $changed )
  113. wp_cache_delete( $user_id, 'users' );
  114. break;
  115. }
  116. }
  117. } else {
  118. // Should never get here
  119. $dashboard_blog = get_dashboard_blog();
  120. add_user_to_blog( $dashboard_blog->blog_id, $user_id, 'subscriber' ); // Add subscriber permission for dashboard blog
  121. update_user_meta( $user_id, 'primary_blog', $dashboard_blog->blog_id );
  122. return $dashboard_blog;
  123. }
  124. return $ret;
  125. } else {
  126. return $details;
  127. }
  128. }
  129. function is_user_member_of_blog( $user_id, $blog_id = 0 ) {
  130. $user_id = (int) $user_id;
  131. $blog_id = (int) $blog_id;
  132. if ( $blog_id == 0 ) {
  133. global $wpdb;
  134. $blog_id = $wpdb->blogid;
  135. }
  136. $blogs = get_blogs_of_user( $user_id );
  137. if ( is_array( $blogs ) )
  138. return array_key_exists( $blog_id, $blogs );
  139. else
  140. return false;
  141. }
  142. function get_user_count() {
  143. global $wpdb;
  144. $count_ts = get_site_option( 'user_count_ts' );
  145. if ( time() - $count_ts > 3600 ) {
  146. $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(ID) as c FROM $wpdb->users WHERE spam = '0' AND deleted = '0'") );
  147. update_site_option( 'user_count', $count );
  148. update_site_option( 'user_count_ts', time() );
  149. }
  150. $count = get_site_option( 'user_count' );
  151. return $count;
  152. }
  153. function get_blog_count( $id = 0 ) {
  154. global $wpdb;
  155. if ( $id == 0 )
  156. $id = $wpdb->siteid;
  157. $count_ts = get_site_option( 'blog_count_ts' );
  158. if ( time() - $count_ts > 3600 ) {
  159. $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(blog_id) as c FROM $wpdb->blogs WHERE site_id = %d AND spam = '0' AND deleted = '0' and archived = '0'", $id) );
  160. update_site_option( 'blog_count', $count );
  161. update_site_option( 'blog_count_ts', time() );
  162. }
  163. $count = get_site_option( 'blog_count' );
  164. return $count;
  165. }
  166. function get_blog_post( $blog_id, $post_id ) {
  167. global $wpdb;
  168. $key = $blog_id . '-' . $post_id;
  169. $post = wp_cache_get( $key, 'global-posts' );
  170. if ( $post == false ) {
  171. $post = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM ' . $wpdb->get_blog_prefix( $blog_id ) . 'posts WHERE ID = %d', $post_id ) );
  172. wp_cache_add( $key, $post, 'global-posts' );
  173. }
  174. return $post;
  175. }
  176. function add_user_to_blog( $blog_id, $user_id, $role ) {
  177. switch_to_blog($blog_id);
  178. $user = new WP_User($user_id);
  179. if ( empty( $user->ID ) )
  180. return new WP_Error('user_does_not_exist', __('That user does not exist.'));
  181. if ( !get_user_meta($user_id, 'primary_blog', true) ) {
  182. update_user_meta($user_id, 'primary_blog', $blog_id);
  183. $details = get_blog_details($blog_id);
  184. update_user_meta($user_id, 'source_domain', $details->domain);
  185. }
  186. $user->set_role($role);
  187. do_action('add_user_to_blog', $user_id, $role, $blog_id);
  188. wp_cache_delete( $user_id, 'users' );
  189. restore_current_blog();
  190. return true;
  191. }
  192. function remove_user_from_blog($user_id, $blog_id = '', $reassign = '') {
  193. global $wpdb;
  194. switch_to_blog($blog_id);
  195. $user_id = (int) $user_id;
  196. do_action('remove_user_from_blog', $user_id, $blog_id);
  197. // If being removed from the primary blog, set a new primary if the user is assigned
  198. // to multiple blogs.
  199. $primary_blog = get_user_meta($user_id, 'primary_blog', true);
  200. if ( $primary_blog == $blog_id ) {
  201. $new_id = '';
  202. $new_domain = '';
  203. $blogs = get_blogs_of_user($user_id);
  204. foreach ( (array) $blogs as $blog ) {
  205. if ( $blog->userblog_id == $blog_id )
  206. continue;
  207. $new_id = $blog->userblog_id;
  208. $new_domain = $blog->domain;
  209. break;
  210. }
  211. update_user_meta($user_id, 'primary_blog', $new_id);
  212. update_user_meta($user_id, 'source_domain', $new_domain);
  213. }
  214. // wp_revoke_user($user_id);
  215. $user = new WP_User($user_id);
  216. if ( empty( $user->ID ) )
  217. return new WP_Error('user_does_not_exist', __('That user does not exist.'));
  218. $user->remove_all_caps();
  219. $blogs = get_blogs_of_user($user_id);
  220. if ( count($blogs) == 0 ) {
  221. update_user_meta($user_id, 'primary_blog', '');
  222. update_user_meta($user_id, 'source_domain', '');
  223. }
  224. if ( $reassign != '' ) {
  225. $reassign = (int) $reassign;
  226. $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_author = %d WHERE post_author = %d", $reassign, $user_id) );
  227. $wpdb->query( $wpdb->prepare("UPDATE $wpdb->links SET link_owner = %d WHERE link_owner = %d", $reassign, $user_id) );
  228. }
  229. restore_current_blog();
  230. }
  231. function create_empty_blog( $domain, $path, $weblog_title, $site_id = 1 ) {
  232. $domain = addslashes( $domain );
  233. $weblog_title = addslashes( $weblog_title );
  234. if ( empty($path) )
  235. $path = '/';
  236. // Check if the domain has been used already. We should return an error message.
  237. if ( domain_exists($domain, $path, $site_id) )
  238. return __( 'Error: Site URL already taken.' );
  239. // Need to backup wpdb table names, and create a new wp_blogs entry for new blog.
  240. // Need to get blog_id from wp_blogs, and create new table names.
  241. // Must restore table names at the end of function.
  242. if ( ! $blog_id = insert_blog($domain, $path, $site_id) )
  243. return __( 'Error: problem creating site entry.' );
  244. switch_to_blog($blog_id);
  245. install_blog($blog_id);
  246. restore_current_blog();
  247. return $blog_id;
  248. }
  249. function get_blog_permalink( $_blog_id, $post_id ) {
  250. $key = "{$_blog_id}-{$post_id}-blog_permalink";
  251. $link = wp_cache_get( $key, 'site-options' );
  252. if ( $link == false ) {
  253. switch_to_blog( $_blog_id );
  254. $link = get_permalink( $post_id );
  255. restore_current_blog();
  256. wp_cache_add( $key, $link, 'site-options', 360 );
  257. }
  258. return $link;
  259. }
  260. function get_blog_id_from_url( $domain, $path = '/' ) {
  261. global $wpdb;
  262. $domain = strtolower( $wpdb->escape( $domain ) );
  263. $path = strtolower( $wpdb->escape( $path ) );
  264. $id = wp_cache_get( md5( $domain . $path ), 'blog-id-cache' );
  265. if ( $id == -1 ) { // blog does not exist
  266. return 0;
  267. } elseif ( $id ) {
  268. return (int)$id;
  269. }
  270. $id = $wpdb->get_var( "SELECT blog_id FROM $wpdb->blogs WHERE domain = '$domain' and path = '$path' /* get_blog_id_from_url */" );
  271. if ( !$id ) {
  272. wp_cache_set( md5( $domain . $path ), -1, 'blog-id-cache' );
  273. return false;
  274. }
  275. wp_cache_set( md5( $domain . $path ), $id, 'blog-id-cache' );
  276. return $id;
  277. }
  278. // wpmu admin functions
  279. function wpmu_admin_do_redirect( $url = '' ) {
  280. $ref = '';
  281. if ( isset( $_GET['ref'] ) )
  282. $ref = $_GET['ref'];
  283. if ( isset( $_POST['ref'] ) )
  284. $ref = $_POST['ref'];
  285. if ( $ref ) {
  286. $ref = wpmu_admin_redirect_add_updated_param( $ref );
  287. wp_redirect( $ref );
  288. exit();
  289. }
  290. if ( empty( $_SERVER['HTTP_REFERER'] ) == false ) {
  291. wp_redirect( $_SERVER['HTTP_REFERER'] );
  292. exit();
  293. }
  294. $url = wpmu_admin_redirect_add_updated_param( $url );
  295. if ( isset( $_GET['redirect'] ) ) {
  296. if ( substr( $_GET['redirect'], 0, 2 ) == 's_' )
  297. $url .= '&action=blogs&s='. esc_html( substr( $_GET['redirect'], 2 ) );
  298. } elseif ( isset( $_POST['redirect'] ) ) {
  299. $url = wpmu_admin_redirect_add_updated_param( $_POST['redirect'] );
  300. }
  301. wp_redirect( $url );
  302. exit();
  303. }
  304. function wpmu_admin_redirect_add_updated_param( $url = '' ) {
  305. if ( strpos( $url, 'updated=true' ) === false ) {
  306. if ( strpos( $url, '?' ) === false )
  307. return $url . '?updated=true';
  308. else
  309. return $url . '&updated=true';
  310. }
  311. return $url;
  312. }
  313. function is_blog_user( $blog_id = 0 ) {
  314. global $wpdb;
  315. $current_user = wp_get_current_user();
  316. if ( !$blog_id )
  317. $blog_id = $wpdb->blogid;
  318. $cap_key = $wpdb->base_prefix . $blog_id . '_capabilities';
  319. if ( is_array($current_user->$cap_key) && in_array(1, $current_user->$cap_key) )
  320. return true;
  321. return false;
  322. }
  323. function is_email_address_unsafe( $user_email ) {
  324. $banned_names = get_site_option( 'banned_email_domains' );
  325. if ($banned_names && !is_array( $banned_names ))
  326. $banned_names = explode( "\n", $banned_names);
  327. if ( is_array( $banned_names ) && empty( $banned_names ) == false ) {
  328. $email_domain = strtolower( substr( $user_email, 1 + strpos( $user_email, '@' ) ) );
  329. foreach ( (array) $banned_names as $banned_domain ) {
  330. if ( $banned_domain == '' )
  331. continue;
  332. if (
  333. strstr( $email_domain, $banned_domain ) ||
  334. (
  335. strstr( $banned_domain, '/' ) &&
  336. preg_match( $banned_domain, $email_domain )
  337. )
  338. )
  339. return true;
  340. }
  341. }
  342. return false;
  343. }
  344. function wpmu_validate_user_signup($user_name, $user_email) {
  345. global $wpdb;
  346. $errors = new WP_Error();
  347. $orig_username = $user_name;
  348. $user_name = preg_replace( '/\s+/', '', sanitize_user( $user_name, true ) );
  349. $maybe = array();
  350. preg_match( '/[a-z0-9]+/', $user_name, $maybe );
  351. if ( $user_name != $orig_username || $user_name != $maybe[0] ) {
  352. $errors->add( 'user_name', __( "Only the lowercase letters a-z and numbers allowed" ) );
  353. $user_name = $orig_username;
  354. }
  355. $user_email = sanitize_email( $user_email );
  356. if ( empty( $user_name ) )
  357. $errors->add('user_name', __('Please enter a username'));
  358. $illegal_names = get_site_option( 'illegal_names' );
  359. if ( is_array( $illegal_names ) == false ) {
  360. $illegal_names = array( 'www', 'web', 'root', 'admin', 'main', 'invite', 'administrator' );
  361. add_site_option( 'illegal_names', $illegal_names );
  362. }
  363. if ( in_array( $user_name, $illegal_names ) == true )
  364. $errors->add('user_name', __('That username is not allowed'));
  365. if ( is_email_address_unsafe( $user_email ) )
  366. $errors->add('user_email', __('You cannot use that email address to signup. We are having problems with them blocking some of our email. Please use another email provider.'));
  367. if ( strlen( $user_name ) < 4 )
  368. $errors->add('user_name', __('Username must be at least 4 characters'));
  369. if ( strpos( ' ' . $user_name, '_' ) != false )
  370. $errors->add( 'user_name', __( 'Sorry, usernames may not contain the character &#8220;_&#8221;!' ) );
  371. // all numeric?
  372. $match = array();
  373. preg_match( '/[0-9]*/', $user_name, $match );
  374. if ( $match[0] == $user_name )
  375. $errors->add('user_name', __('Sorry, usernames must have letters too!'));
  376. if ( !is_email( $user_email ) )
  377. $errors->add('user_email', __('Please enter a correct email address'));
  378. $limited_email_domains = get_site_option( 'limited_email_domains' );
  379. if ( is_array( $limited_email_domains ) && empty( $limited_email_domains ) == false ) {
  380. $emaildomain = substr( $user_email, 1 + strpos( $user_email, '@' ) );
  381. if ( in_array( $emaildomain, $limited_email_domains ) == false )
  382. $errors->add('user_email', __('Sorry, that email address is not allowed!'));
  383. }
  384. // Check if the username has been used already.
  385. if ( username_exists($user_name) )
  386. $errors->add('user_name', __('Sorry, that username already exists!'));
  387. // Check if the email address has been used already.
  388. if ( email_exists($user_email) )
  389. $errors->add('user_email', __('Sorry, that email address is already used!'));
  390. // Has someone already signed up for this username?
  391. $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE user_login = %s", $user_name) );
  392. if ( $signup != null ) {
  393. $registered_at = mysql2date('U', $signup->registered);
  394. $now = current_time( 'timestamp', true );
  395. $diff = $now - $registered_at;
  396. // If registered more than two days ago, cancel registration and let this signup go through.
  397. if ( $diff > 172800 )
  398. $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->signups WHERE user_login = %s", $user_name) );
  399. else
  400. $errors->add('user_name', __('That username is currently reserved but may be available in a couple of days.'));
  401. if ( $signup->active == 0 && $signup->user_email == $user_email )
  402. $errors->add('user_email_used', __('username and email used'));
  403. }
  404. $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE user_email = %s", $user_email) );
  405. if ( $signup != null ) {
  406. $diff = current_time( 'timestamp', true ) - mysql2date('U', $signup->registered);
  407. // If registered more than two days ago, cancel registration and let this signup go through.
  408. if ( $diff > 172800 )
  409. $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->signups WHERE user_email = %s", $user_email) );
  410. else
  411. $errors->add('user_email', __('That email address has already been used. Please check your inbox for an activation email. It will become available in a couple of days if you do nothing.'));
  412. }
  413. $result = array('user_name' => $user_name, 'orig_username' => $orig_username, 'user_email' => $user_email, 'errors' => $errors);
  414. return apply_filters('wpmu_validate_user_signup', $result);
  415. }
  416. function wpmu_validate_blog_signup($blogname, $blog_title, $user = '') {
  417. global $wpdb, $domain, $base, $current_site;
  418. $blog_title = strip_tags( $blog_title );
  419. $blog_title = substr( $blog_title, 0, 50 );
  420. $errors = new WP_Error();
  421. $illegal_names = get_site_option( 'illegal_names' );
  422. if ( $illegal_names == false ) {
  423. $illegal_names = array( 'www', 'web', 'root', 'admin', 'main', 'invite', 'administrator' );
  424. add_site_option( 'illegal_names', $illegal_names );
  425. }
  426. // On sub dir installs, Some names are so illegal, only a filter can spring them from jail
  427. if (! is_subdomain_install() )
  428. $illegal_names = array_merge($illegal_names, apply_filters( 'subdirectory_reserved_names', array( 'page', 'comments', 'blog', 'files', 'feed' ) ) );
  429. if ( empty( $blogname ) )
  430. $errors->add('blogname', __('Please enter a site name'));
  431. $maybe = array();
  432. preg_match( '/[a-z0-9]+/', $blogname, $maybe );
  433. if ( $blogname != $maybe[0] )
  434. $errors->add('blogname', __('Only lowercase letters and numbers allowed'));
  435. if ( in_array( $blogname, $illegal_names ) == true )
  436. $errors->add('blogname', __('That name is not allowed'));
  437. if ( strlen( $blogname ) < 4 && !is_super_admin() )
  438. $errors->add('blogname', __('Site name must be at least 4 characters'));
  439. if ( strpos( ' ' . $blogname, '_' ) != false )
  440. $errors->add( 'blogname', __( 'Sorry, site names may not contain the character &#8220;_&#8221;!' ) );
  441. // do not allow users to create a blog that conflicts with a page on the main blog.
  442. if ( !is_subdomain_install() && $wpdb->get_var( $wpdb->prepare( "SELECT post_name FROM " . $wpdb->get_blog_prefix( $current_site->blog_id ) . "posts WHERE post_type = 'page' AND post_name = %s", $blogname ) ) )
  443. $errors->add( 'blogname', __( 'Sorry, you may not use that site name.' ) );
  444. // all numeric?
  445. $match = array();
  446. preg_match( '/[0-9]*/', $blogname, $match );
  447. if ( $match[0] == $blogname )
  448. $errors->add('blogname', __('Sorry, site names must have letters too!'));
  449. $blogname = apply_filters( 'newblogname', $blogname );
  450. $blog_title = stripslashes( $blog_title );
  451. if ( empty( $blog_title ) )
  452. $errors->add('blog_title', __('Please enter a site title'));
  453. // Check if the domain/path has been used already.
  454. if ( is_subdomain_install() ) {
  455. $mydomain = $blogname . '.' . preg_replace( '|^www\.|', '', $domain );
  456. $path = $base;
  457. } else {
  458. $mydomain = "$domain";
  459. $path = $base.$blogname.'/';
  460. }
  461. if ( domain_exists($mydomain, $path) )
  462. $errors->add('blogname', __('Sorry, that site already exists!'));
  463. if ( username_exists( $blogname ) ) {
  464. if ( is_object( $user ) == false || ( is_object($user) && ( $user->user_login != $blogname ) ) )
  465. $errors->add( 'blogname', __( 'Sorry, that site is reserved!' ) );
  466. }
  467. // Has someone already signed up for this domain?
  468. $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE domain = %s AND path = %s", $mydomain, $path) ); // TODO: Check email too?
  469. if ( ! empty($signup) ) {
  470. $diff = current_time( 'timestamp', true ) - mysql2date('U', $signup->registered);
  471. // If registered more than two days ago, cancel registration and let this signup go through.
  472. if ( $diff > 172800 )
  473. $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->signups WHERE domain = %s AND path = %s", $mydomain, $path) );
  474. else
  475. $errors->add('blogname', __('That site is currently reserved but may be available in a couple days.'));
  476. }
  477. $result = array('domain' => $mydomain, 'path' => $path, 'blogname' => $blogname, 'blog_title' => $blog_title, 'errors' => $errors);
  478. return apply_filters('wpmu_validate_blog_signup', $result);
  479. }
  480. // Record signup information for future activation. wpmu_validate_signup() should be run
  481. // on the inputs before calling wpmu_signup().
  482. function wpmu_signup_blog($domain, $path, $title, $user, $user_email, $meta = '') {
  483. global $wpdb;
  484. $key = substr( md5( time() . rand() . $domain ), 0, 16 );
  485. $meta = serialize($meta);
  486. $domain = $wpdb->escape($domain);
  487. $path = $wpdb->escape($path);
  488. $title = $wpdb->escape($title);
  489. $wpdb->insert( $wpdb->signups, array(
  490. 'domain' => $domain,
  491. 'path' => $path,
  492. 'title' => $title,
  493. 'user_login' => $user,
  494. 'user_email' => $user_email,
  495. 'registered' => current_time('mysql', true),
  496. 'activation_key' => $key,
  497. 'meta' => $meta
  498. ) );
  499. wpmu_signup_blog_notification($domain, $path, $title, $user, $user_email, $key, $meta);
  500. }
  501. function wpmu_signup_user($user, $user_email, $meta = '') {
  502. global $wpdb;
  503. // Format data
  504. $user = preg_replace( '/\s+/', '', sanitize_user( $user, true ) );
  505. $user_email = sanitize_email( $user_email );
  506. $key = substr( md5( time() . rand() . $user_email ), 0, 16 );
  507. $meta = serialize($meta);
  508. $wpdb->insert( $wpdb->signups, array(
  509. 'domain' => '',
  510. 'path' => '',
  511. 'title' => '',
  512. 'user_login' => $user,
  513. 'user_email' => $user_email,
  514. 'registered' => current_time('mysql', true),
  515. 'activation_key' => $key,
  516. 'meta' => $meta
  517. ) );
  518. wpmu_signup_user_notification($user, $user_email, $key, $meta);
  519. }
  520. // Notify user of signup success.
  521. function wpmu_signup_blog_notification($domain, $path, $title, $user, $user_email, $key, $meta = '') {
  522. global $current_site;
  523. if ( !apply_filters('wpmu_signup_blog_notification', $domain, $path, $title, $user, $user_email, $key, $meta) )
  524. return false;
  525. // Send email with activation link.
  526. if ( !is_subdomain_install() || $current_site->id != 1 )
  527. $activate_url = network_site_url("wp-activate.php?key=$key");
  528. else
  529. $activate_url = "http://{$domain}{$path}wp-activate.php?key=$key"; // @todo use *_url() API
  530. $activate_url = esc_url($activate_url);
  531. $admin_email = get_site_option( 'admin_email' );
  532. if ( $admin_email == '' )
  533. $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
  534. $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
  535. $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
  536. $message = sprintf( apply_filters( 'wpmu_signup_blog_notification_email', __( "To activate your blog, please click the following link:\n\n%s\n\nAfter you activate, you will receive *another email* with your login.\n\nAfter you activate, you can visit your site here:\n\n%s" ) ), $activate_url, esc_url( "http://{$domain}{$path}" ), $key );
  537. // TODO: Don't hard code activation link.
  538. $subject = sprintf( apply_filters( 'wpmu_signup_blog_notification_subject', __( '[%1$s] Activate %2$s' ) ), $from_name, esc_url( 'http://' . $domain . $path ) );
  539. wp_mail($user_email, $subject, $message, $message_headers);
  540. return true;
  541. }
  542. function wpmu_signup_user_notification($user, $user_email, $key, $meta = '') {
  543. if ( !apply_filters('wpmu_signup_user_notification', $user, $user_email, $key, $meta) )
  544. return false;
  545. // Send email with activation link.
  546. $admin_email = get_site_option( 'admin_email' );
  547. if ( $admin_email == '' )
  548. $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
  549. $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
  550. $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
  551. $message = sprintf( apply_filters( 'wpmu_signup_user_notification_email', __( "To activate your user, please click the following link:\n\n%s\n\nAfter you activate, you will receive *another email* with your login.\n\n" ) ), site_url( "wp-activate.php?key=$key" ), $key );
  552. // TODO: Don't hard code activation link.
  553. $subject = sprintf( __( apply_filters( 'wpmu_signup_user_notification_subject', '[%1$s] Activate %2$s' ) ), $from_name, $user);
  554. wp_mail($user_email, $subject, $message, $message_headers);
  555. return true;
  556. }
  557. function wpmu_activate_signup($key) {
  558. global $wpdb, $current_site;
  559. $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE activation_key = %s", $key) );
  560. if ( empty($signup) )
  561. return new WP_Error('invalid_key', __('Invalid activation key.'));
  562. if ( $signup->active )
  563. return new WP_Error('already_active', __('The site is already active.'), $signup);
  564. $meta = unserialize($signup->meta);
  565. $user_login = $wpdb->escape($signup->user_login);
  566. $user_email = $wpdb->escape($signup->user_email);
  567. $password = wp_generate_password();
  568. $user_id = username_exists($user_login);
  569. if ( ! $user_id )
  570. $user_id = wpmu_create_user($user_login, $password, $user_email);
  571. else
  572. $user_already_exists = true;
  573. if ( ! $user_id )
  574. return new WP_Error('create_user', __('Could not create user'), $signup);
  575. $now = current_time('mysql', true);
  576. if ( empty($signup->domain) ) {
  577. $wpdb->update( $wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key) );
  578. if ( isset( $user_already_exists ) )
  579. return new WP_Error( 'user_already_exists', __( 'That username is already activated.' ), $signup);
  580. wpmu_welcome_user_notification($user_id, $password, $meta);
  581. $user_site = get_site_option( 'dashboard_blog', $current_site->blog_id );
  582. if ( $user_site == false )
  583. add_user_to_blog( '1', $user_id, get_site_option( 'default_user_role', 'subscriber' ) );
  584. else
  585. add_user_to_blog( $user_site, $user_id, get_site_option( 'default_user_role', 'subscriber' ) );
  586. add_new_user_to_blog( $user_id, $user_email, $meta );
  587. do_action('wpmu_activate_user', $user_id, $password, $meta);
  588. return array('user_id' => $user_id, 'password' => $password, 'meta' => $meta);
  589. }
  590. $blog_id = wpmu_create_blog( $signup->domain, $signup->path, $signup->title, $user_id, $meta, $wpdb->siteid );
  591. // TODO: What to do if we create a user but cannot create a blog?
  592. if ( is_wp_error($blog_id) ) {
  593. // If blog is taken, that means a previous attempt to activate this blog failed in between creating the blog and
  594. // setting the activation flag. Let's just set the active flag and instruct the user to reset their password.
  595. if ( 'blog_taken' == $blog_id->get_error_code() ) {
  596. $blog_id->add_data( $signup );
  597. $wpdb->update( $wpdb->signups, array( 'active' => 1, 'activated' => $now ), array( 'activation_key' => $key ) );
  598. }
  599. return $blog_id;
  600. }
  601. $wpdb->update( $wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key) );
  602. wpmu_welcome_notification($blog_id, $user_id, $password, $signup->title, $meta);
  603. do_action('wpmu_activate_blog', $blog_id, $user_id, $password, $signup->title, $meta);
  604. return array('blog_id' => $blog_id, 'user_id' => $user_id, 'password' => $password, 'title' => $signup->title, 'meta' => $meta);
  605. }
  606. function wpmu_create_user( $user_name, $password, $email) {
  607. $user_name = preg_replace( '/\s+/', '', sanitize_user( $user_name, true ) );
  608. $user_id = wp_create_user( $user_name, $password, $email );
  609. if ( is_wp_error($user_id) )
  610. return false;
  611. // Newly created users have no roles or caps until they are added to a blog.
  612. delete_user_option( $user_id, 'capabilities' );
  613. delete_user_option( $user_id, 'user_level' );
  614. do_action( 'wpmu_new_user', $user_id );
  615. return $user_id;
  616. }
  617. function wpmu_create_blog($domain, $path, $title, $user_id, $meta = '', $site_id = 1) {
  618. $domain = preg_replace( '/\s+/', '', sanitize_user( $domain, true ) );
  619. if ( is_subdomain_install() )
  620. $domain = str_replace( '@', '', $domain );
  621. $title = strip_tags( $title );
  622. $user_id = (int) $user_id;
  623. if ( empty($path) )
  624. $path = '/';
  625. // Check if the domain has been used already. We should return an error message.
  626. if ( domain_exists($domain, $path, $site_id) )
  627. return new WP_Error('blog_taken', __('Site already exists.'));
  628. if ( !defined('WP_INSTALLING') )
  629. define( 'WP_INSTALLING', true );
  630. if ( ! $blog_id = insert_blog($domain, $path, $site_id) )
  631. return new WP_Error('insert_blog', __('Could not create site.'));
  632. switch_to_blog($blog_id);
  633. install_blog($blog_id, $title);
  634. wp_install_defaults($user_id);
  635. add_user_to_blog($blog_id, $user_id, 'administrator');
  636. if ( is_array($meta) ) foreach ($meta as $key => $value) {
  637. if ( $key == 'public' || $key == 'archived' || $key == 'mature' || $key == 'spam' || $key == 'deleted' || $key == 'lang_id' )
  638. update_blog_status( $blog_id, $key, $value );
  639. else
  640. update_option( $key, $value );
  641. }
  642. add_option( 'WPLANG', get_site_option( 'WPLANG' ) );
  643. update_option( 'blog_public', (int)$meta['public'] );
  644. if ( !is_super_admin() && get_user_meta( $user_id, 'primary_blog', true ) == get_site_option( 'dashboard_blog', 1 ) )
  645. update_user_meta( $user_id, 'primary_blog', $blog_id );
  646. restore_current_blog();
  647. do_action( 'wpmu_new_blog', $blog_id, $user_id, $domain, $path, $site_id, $meta );
  648. return $blog_id;
  649. }
  650. function newblog_notify_siteadmin( $blog_id, $deprecated = '' ) {
  651. if ( get_site_option( 'registrationnotification' ) != 'yes' )
  652. return false;
  653. $email = get_site_option( 'admin_email' );
  654. if ( is_email($email) == false )
  655. return false;
  656. $options_site_url = esc_url(network_admin_url('ms-options.php'));
  657. switch_to_blog( $blog_id );
  658. $blogname = get_option( 'blogname' );
  659. $siteurl = site_url();
  660. restore_current_blog();
  661. $msg = sprintf( __( 'New Site: %1s
  662. URL: %2s
  663. Remote IP: %3s
  664. Disable these notifications: %4s' ), $blogname, $siteurl, $_SERVER['REMOTE_ADDR'], $options_site_url);
  665. $msg = apply_filters( 'newblog_notify_siteadmin', $msg );
  666. wp_mail( $email, sprintf( __( 'New Site Registration: %s' ), $siteurl ), $msg );
  667. return true;
  668. }
  669. function newuser_notify_siteadmin( $user_id ) {
  670. if ( get_site_option( 'registrationnotification' ) != 'yes' )
  671. return false;
  672. $email = get_site_option( 'admin_email' );
  673. if ( is_email($email) == false )
  674. return false;
  675. $user = new WP_User($user_id);
  676. $options_site_url = esc_url(network_admin_url('ms-options.php'));
  677. $msg = sprintf(__('New User: %1s
  678. Remote IP: %2s
  679. Disable these notifications: %3s'), $user->user_login, $_SERVER['REMOTE_ADDR'], $options_site_url);
  680. $msg = apply_filters( 'newuser_notify_siteadmin', $msg );
  681. wp_mail( $email, sprintf(__('New User Registration: %s'), $user->user_login), $msg );
  682. return true;
  683. }
  684. function domain_exists($domain, $path, $site_id = 1) {
  685. global $wpdb;
  686. return $wpdb->get_var( $wpdb->prepare("SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s AND site_id = %d", $domain, $path, $site_id) );
  687. }
  688. function insert_blog($domain, $path, $site_id) {
  689. global $wpdb;
  690. $path = trailingslashit($path);
  691. $site_id = (int) $site_id;
  692. $result = $wpdb->insert( $wpdb->blogs, array('site_id' => $site_id, 'domain' => $domain, 'path' => $path, 'registered' => current_time('mysql')) );
  693. if ( ! $result )
  694. return false;
  695. refresh_blog_details($wpdb->insert_id);
  696. return $wpdb->insert_id;
  697. }
  698. // Install an empty blog. wpdb should already be switched.
  699. function install_blog($blog_id, $blog_title = '') {
  700. global $wpdb, $table_prefix, $wp_roles;
  701. $wpdb->suppress_errors();
  702. // Cast for security
  703. $blog_id = (int) $blog_id;
  704. require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
  705. if ( $wpdb->get_results("SELECT ID FROM $wpdb->posts") )
  706. die(__('<h1>Already Installed</h1><p>You appear to have already installed WordPress. To reinstall please clear your old database tables first.</p>') . '</body></html>');
  707. $wpdb->suppress_errors(false);
  708. $url = get_blogaddress_by_id($blog_id);
  709. // Set everything up
  710. make_db_current_silent();
  711. populate_options();
  712. populate_roles();
  713. $wp_roles->_init();
  714. // fix url.
  715. update_option('siteurl', $url);
  716. update_option('home', $url);
  717. update_option('fileupload_url', $url . "files" );
  718. update_option('upload_path', "wp-content/blogs.dir/" . $blog_id . "/files");
  719. update_option('blogname', stripslashes( $blog_title ) );
  720. update_option('admin_email', '');
  721. $wpdb->update( $wpdb->options, array('option_value' => ''), array('option_name' => 'admin_email') );
  722. // remove all perms
  723. $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE meta_key = %s", $table_prefix.'user_level') );
  724. $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE meta_key = %s", $table_prefix.'capabilities') );
  725. $wpdb->suppress_errors( false );
  726. }
  727. // Deprecated, use wp_install_defaults()
  728. // should be switched already as $blog_id is ignored.
  729. function install_blog_defaults($blog_id, $user_id) {
  730. global $wpdb;
  731. require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
  732. $wpdb->suppress_errors();
  733. wp_install_defaults($user_id);
  734. $wpdb->suppress_errors( false );
  735. }
  736. function wpmu_welcome_notification($blog_id, $user_id, $password, $title, $meta = '') {
  737. global $current_site;
  738. if ( !apply_filters('wpmu_welcome_notification', $blog_id, $user_id, $password, $title, $meta) )
  739. return false;
  740. $welcome_email = stripslashes( get_site_option( 'welcome_email' ) );
  741. if ( $welcome_email == false )
  742. $welcome_email = stripslashes( __( 'Dear User,
  743. Your new SITE_NAME site has been successfully set up at:
  744. BLOG_URL
  745. You can log in to the administrator account with the following information:
  746. Username: USERNAME
  747. Password: PASSWORD
  748. Login Here: BLOG_URLwp-login.php
  749. We hope you enjoy your new site.
  750. Thanks!
  751. --The Team @ SITE_NAME' ) );
  752. $url = get_blogaddress_by_id($blog_id);
  753. $user = new WP_User($user_id);
  754. $welcome_email = str_replace( 'SITE_NAME', $current_site->site_name, $welcome_email );
  755. $welcome_email = str_replace( 'BLOG_TITLE', $title, $welcome_email );
  756. $welcome_email = str_replace( 'BLOG_URL', $url, $welcome_email );
  757. $welcome_email = str_replace( 'USERNAME', $user->user_login, $welcome_email );
  758. $welcome_email = str_replace( 'PASSWORD', $password, $welcome_email );
  759. $welcome_email = apply_filters( 'update_welcome_email', $welcome_email, $blog_id, $user_id, $password, $title, $meta);
  760. $admin_email = get_site_option( 'admin_email' );
  761. if ( $admin_email == '' )
  762. $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
  763. $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
  764. $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
  765. $message = $welcome_email;
  766. if ( empty( $current_site->site_name ) )
  767. $current_site->site_name = 'WordPress MU';
  768. $subject = apply_filters( 'update_welcome_subject', sprintf(__('New %1$s Site: %2$s'), $current_site->site_name, stripslashes( $title ) ) );
  769. wp_mail($user->user_email, $subject, $message, $message_headers);
  770. return true;
  771. }
  772. function wpmu_welcome_user_notification($user_id, $password, $meta = '') {
  773. global $current_site;
  774. if ( !apply_filters('wpmu_welcome_user_notification', $user_id, $password, $meta) )
  775. return false;
  776. $welcome_email = get_site_option( 'welcome_user_email' );
  777. $user = new WP_User($user_id);
  778. $welcome_email = apply_filters( 'update_welcome_user_email', $welcome_email, $user_id, $password, $meta);
  779. $welcome_email = str_replace( 'SITE_NAME', $current_site->site_name, $welcome_email );
  780. $welcome_email = str_replace( 'USERNAME', $user->user_login, $welcome_email );
  781. $welcome_email = str_replace( 'PASSWORD', $password, $welcome_email );
  782. $welcome_email = str_replace( 'LOGINLINK', wp_login_url(), $welcome_email );
  783. $admin_email = get_site_option( 'admin_email' );
  784. if ( $admin_email == '' )
  785. $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
  786. $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
  787. $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
  788. $message = $welcome_email;
  789. if ( empty( $current_site->site_name ) )
  790. $current_site->site_name = 'WordPress MU';
  791. $subject = apply_filters( 'update_welcome_user_subject', sprintf(__('New %1$s User: %2$s'), $current_site->site_name, $user->user_login) );
  792. wp_mail($user->user_email, $subject, $message, $message_headers);
  793. return true;
  794. }
  795. function get_current_site() {
  796. global $current_site;
  797. return $current_site;
  798. }
  799. function get_user_id_from_string( $string ) {
  800. $user_id = 0;
  801. if ( is_email( $string ) ) {
  802. $user = get_user_by('email', $string);
  803. if ( $user )
  804. $user_id = $user->ID;
  805. } elseif ( is_numeric( $string ) ) {
  806. $user_id = $string;
  807. } else {
  808. $user = get_user_by('login', $string);
  809. if ( $user )
  810. $user_id = $user->ID;
  811. }
  812. return $user_id;
  813. }
  814. function get_most_recent_post_of_user( $user_id ) {
  815. global $wpdb;
  816. $user_blogs = get_blogs_of_user( (int) $user_id );
  817. $most_recent_post = array();
  818. // Walk through each blog and get the most recent post
  819. // published by $user_id
  820. foreach ( (array) $user_blogs as $blog ) {
  821. $recent_post = $wpdb->get_row( $wpdb->prepare("SELECT ID, post_date_gmt FROM {$wpdb->base_prefix}{$blog->userblog_id}_posts WHERE post_author = %d AND post_type = 'post' AND post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1", $user_id ), ARRAY_A);
  822. // Make sure we found a post
  823. if ( isset($recent_post['ID']) ) {
  824. $post_gmt_ts = strtotime($recent_post['post_date_gmt']);
  825. // If this is the first post checked or if this post is
  826. // newer than the current recent post, make it the new
  827. // most recent post.
  828. if ( !isset($most_recent_post['post_gmt_ts']) || ( $post_gmt_ts > $most_recent_post['post_gmt_ts'] ) ) {
  829. $most_recent_post = array(
  830. 'blog_id' => $blog->userblog_id,
  831. 'post_id' => $recent_post['ID'],
  832. 'post_date_gmt' => $recent_post['post_date_gmt'],
  833. 'post_gmt_ts' => $post_gmt_ts
  834. );
  835. }
  836. }
  837. }
  838. return $most_recent_post;
  839. }
  840. /* Misc functions */
  841. function get_dirsize( $directory ) {
  842. $dirsize = get_transient( 'dirsize_cache' );
  843. if ( is_array( $dirsize ) && isset( $dirsize[ $directory ][ 'size' ] ) )
  844. return $dirsize[ $directory ][ 'size' ];
  845. if ( false == is_array( $dirsize ) )
  846. $dirsize = array();
  847. $dirsize[ $directory ][ 'size' ] = recurse_dirsize( $directory );
  848. set_transient( 'dirsize_cache', $dirsize, 3600 );
  849. return $dirsize[ $directory ][ 'size' ];
  850. }
  851. function recurse_dirsize( $directory ) {
  852. $size = 0;
  853. if ( substr( $directory, -1 ) == '/' )
  854. $directory = substr($directory,0,-1);
  855. if ( !file_exists($directory) || !is_dir( $directory ) || !is_readable( $directory ) )
  856. return false;
  857. if ($handle = opendir($directory)) {
  858. while(($file = readdir($handle)) !== false) {
  859. $path = $directory.'/'.$file;
  860. if ($file != '.' && $file != '..') {
  861. if (is_file($path)) {
  862. $size += filesize($path);
  863. } elseif (is_dir($path)) {
  864. $handlesize = recurse_dirsize($path);
  865. if ($handlesize > 0)
  866. $size += $handlesize;
  867. }
  868. }
  869. }
  870. closedir($handle);
  871. }
  872. return $size;
  873. }
  874. function upload_is_user_over_quota( $echo = true ) {
  875. if ( get_site_option( 'upload_space_check_disabled' ) )
  876. return true;
  877. $spaceAllowed = get_space_allowed();
  878. if ( empty( $spaceAllowed ) || !is_numeric( $spaceAllowed ) )
  879. $spaceAllowed = 10; // Default space allowed is 10 MB
  880. $dirName = BLOGUPLOADDIR;
  881. $size = get_dirsize($dirName) / 1024 / 1024;
  882. if ( ($spaceAllowed-$size) < 0 ) {
  883. if ( $echo )
  884. _e( 'Sorry, you have used your space allocation. Please delete some files to upload more files.' ); // No space left
  885. return true;
  886. } else {
  887. return false;
  888. }
  889. }
  890. function check_upload_mimes( $mimes ) {
  891. $site_exts = explode( ' ', get_site_option( 'upload_filetypes' ) );
  892. foreach ( $site_exts as $ext ) {
  893. foreach ( $mimes as $ext_pattern => $mime ) {
  894. if ( $ext != '' && strpos( $ext_pattern, $ext ) !== false )
  895. $site_mimes[$ext_pattern] = $mime;
  896. }
  897. }
  898. return $site_mimes;
  899. }
  900. function update_posts_count( $deprecated = '' ) {
  901. global $wpdb;
  902. update_option( 'post_count', (int) $wpdb->get_var( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_status = 'publish' and post_type = 'post'" ) );
  903. }
  904. function wpmu_log_new_registrations( $blog_id, $user_id ) {
  905. global $wpdb;
  906. $user = new WP_User( (int) $user_id );
  907. $wpdb->insert( $wpdb->registration_log, array('email' => $user->user_email, 'IP' => preg_replace( '/[^0-9., ]/', '',$_SERVER['REMOTE_ADDR'] ), 'blog_id' => $blog_id, 'date_registered' => current_time('mysql')) );
  908. }
  909. function fix_import_form_size( $size ) {
  910. if ( upload_is_user_over_quota( false ) == true )
  911. return 0;
  912. $spaceAllowed = 1024 * 1024 * get_space_allowed();
  913. $dirName = BLOGUPLOADDIR;
  914. $dirsize = get_dirsize($dirName) ;
  915. if ( $size > $spaceAllowed - $dirsize )
  916. return $spaceAllowed - $dirsize; // remaining space
  917. else
  918. return $size; // default
  919. }
  920. /**
  921. * Maintains a canonical list of terms by syncing terms created for each blog with the global terms table.
  922. *
  923. * @since 3.0.0
  924. *
  925. * @see term_id_filter
  926. *
  927. * @param int $term_id An ID for a term on the current blog.
  928. * @return int An ID from the global terms table mapped from $term_id.
  929. */
  930. function global_terms( $term_id, $deprecated = '' ) {
  931. global $wpdb;
  932. static $global_terms_recurse = null;
  933. if ( !global_terms_enabled() )
  934. return $term_id;
  935. // prevent a race condition
  936. $recurse_start = false;
  937. if ( $global_terms_recurse === null ) {
  938. $recurse_start = true;
  939. $global_terms_recurse = 1;
  940. } elseif ( 10 < $global_terms_recurse++ ) {
  941. return $term_id;
  942. }
  943. $term_id = intval( $term_id );
  944. $c = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->terms WHERE term_id = %d", $term_id ) );
  945. $global_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM $wpdb->sitecategories WHERE category_nicename = %s", $c->slug ) );
  946. if ( $global_id == null ) {
  947. $used_global_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM $wpdb->sitecategories WHERE cat_ID = %d", $c->term_id ) );
  948. if ( null == $used_global_id ) {
  949. $wpdb->insert( $wpdb->sitecategories, array( 'cat_ID' => $term_id, 'cat_name' => $c->name, 'category_nicename' => $c->slug ) );
  950. $global_id = $wpdb->insert_id;
  951. if ( empty( $global_id ) )
  952. return $term_id;
  953. } else {
  954. $max_global_id = $wpdb->get_var( "SELECT MAX(cat_ID) FROM $wpdb->sitecategories" );
  955. $max_local_id = $wpdb->get_var( "SELECT MAX(term_id) FROM $wpdb->terms" );
  956. $new_global_id = max( $max_global_id, $max_local_id ) + mt_rand( 100, 400 );
  957. $wpdb->insert( $wpdb->sitecategories, array( 'cat_ID' => $new_global_id, 'cat_name' => $c->name, 'category_nicename' => $c->slug ) );
  958. $global_id = $wpdb->insert_id;
  959. }
  960. } elseif ( $global_id != $term_id ) {
  961. $local_id = $wpdb->get_row( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms WHERE term_id = %d", $global_id ) );
  962. if ( null != $local_id )
  963. $local_id = global_terms( $local_id );
  964. if ( 10 < $global_terms_recurse )
  965. $global_id = $term_id;
  966. }
  967. if ( $global_id != $term_id ) {
  968. if ( get_option( 'default_category' ) == $term_id )
  969. update_option( 'default_category', $global_id );
  970. $wpdb->update( $wpdb->terms, array('term_id' => $global_id), array('term_id' => $term_id) );
  971. $wpdb->update( $wpdb->term_taxonomy, array('term_id' => $global_id), array('term_id' => $term_id) );
  972. $wpdb->update( $wpdb->term_taxonomy, array('parent' => $global_id), array('parent' => $term_id) );
  973. clean_term_cache($term_id);
  974. }
  975. if( $recurse_start )
  976. $global_terms_recurse = null;
  977. return $global_id;
  978. }
  979. function redirect_this_site( $deprecated = '' ) {
  980. global $current_site;
  981. return array( $current_site->domain );
  982. }
  983. function upload_is_file_too_big( $upload ) {
  984. if ( is_array( $upload ) == false || defined( 'WP_IMPORTING' ) )
  985. return $upload;
  986. if ( strlen( $upload['bits'] ) > ( 1024 * get_site_option( 'fileupload_maxk', 1500 ) ) )
  987. return sprintf( __( 'This file is too big. Files must be less than %d KB in size.' ) . '<br />', get_site_option( 'fileupload_maxk', 1500 ));
  988. return $upload;
  989. }
  990. function wordpressmu_wp_mail_from( $email ) {
  991. if ( strpos( $email, 'wordpress@' ) !== false )
  992. $email = get_option( 'admin_email' );
  993. return $email;
  994. }
  995. function signup_nonce_fields() {
  996. $id = mt_rand();
  997. echo "<input type='hidden' name='signup_form_id' value='{$id}' />";
  998. wp_nonce_field('signup_form_' . $id, '_signup_form', false);
  999. }
  1000. function signup_nonce_check( $result ) {
  1001. if ( !strpos( $_SERVER[ 'PHP_SELF' ], 'wp-signup.php' ) )
  1002. return $result;
  1003. if ( wp_create_nonce('signup_form_' . $_POST[ 'signup_form_id' ]) != $_POST['_signup_form'] )
  1004. wp_die( __('Please try again!') );
  1005. return $result;
  1006. }
  1007. function maybe_redirect_404() {
  1008. global $current_site;
  1009. if ( is_main_site() && is_404() && defined( 'NOBLOGREDIRECT' ) && ( $destination = apply_filters( 'blog_redirect_404', NOBLOGREDIRECT ) ) ) {
  1010. if ( $destination == '%siteurl%' )
  1011. $destination = network_home_url();
  1012. wp_redirect( $destination );
  1013. exit();
  1014. }
  1015. }
  1016. function maybe_add_existing_user_to_blog() {
  1017. if ( false === strpos( $_SERVER[ 'REQUEST_URI' ], '/newbloguser/' ) )
  1018. return false;
  1019. $parts = explode( '/', $_SERVER[ 'REQUEST_URI' ] );
  1020. $key = array_pop( $parts );
  1021. if ( $key == '' )
  1022. $key = array_pop( $parts );
  1023. $details = get_option( 'new_user_' . $key );
  1024. if ( !empty( $details ) )
  1025. delete_option( 'new_user_' . $key );
  1026. if ( empty( $details ) || is_wp_error( add_existing_user_to_blog( $details ) ) )
  1027. wp_die( sprintf(__('An error occurred adding you to this site. Back to the <a href="%s">homepage</a>.'), site_url() ) );
  1028. wp_die( sprintf(__('You have been added to this site. Please visit the <a href="%s">homepage</a> or <a href="%s">login</a> using your username and password.'), site_url(), admin_url() ), __('Success') );
  1029. }
  1030. function add_existing_user_to_blog( $details = false ) {
  1031. if ( is_array( $details ) ) {
  1032. $result = add_user_to_blog( '', $details[ 'user_id' ], $details[ 'role' ] );
  1033. do_action( 'added_existing_user', $details[ 'user_id' ], $result );
  1034. }
  1035. return $result;
  1036. }
  1037. function add_new_user_to_blog( $user_id, $email, $meta ) {
  1038. global $current_site;
  1039. if ( $meta[ 'add_to_blog' ] ) {
  1040. $blog_id = $meta[ 'add_to_blog' ];
  1041. $role = $meta[ 'new_role' ];
  1042. remove_user_from_blog($user_id, $current_site->blogid); // remove user from main blog.
  1043. add_user_to_blog( $blog_id, $user_id, $role );
  1044. update_user_meta( $user_id, 'primary_blog', $blog_id );
  1045. }
  1046. }
  1047. function fix_phpmailer_messageid( $phpmailer ) {
  1048. global $current_site;
  1049. $phpmailer->Hostname = $current_site->domain;
  1050. }
  1051. function is_user_spammy( $username = 0 ) {
  1052. if ( $username == 0 ) {
  1053. $user_id = get_current_user_id();
  1054. } else {
  1055. $user_id = get_user_id_from_string( $username );
  1056. }
  1057. $u = new WP_User( $user_id );
  1058. return ( isset( $u->spam ) && $u->spam == 1 );
  1059. }
  1060. function update_blog_public( $old_value, $value ) {
  1061. global $wpdb;
  1062. do_action('update_blog_public');
  1063. update_blog_status( $wpdb->blogid, 'public', (int) $value );
  1064. }
  1065. add_action('update_option_blog_public', 'update_blog_public', 10, 2);
  1066. /* Redirect all hits to "dashboard" blog to wp-admin/ Dashboard. */
  1067. function redirect_mu_dashboard() {
  1068. global $current_site, $current_blog;
  1069. $dashboard_blog = get_dashboard_blog();
  1070. if ( $current_blog->blog_id == $dashboard_blog->blog_id && $dashboard_blog->blog_id != $current_site->blog_id ) {
  1071. $protocol = ( is_ssl() ? 'https://' : 'http://' );
  1072. wp_redirect( $protocol . $dashboard_blog->domain . trailingslashit( $dashboard_blog->path ) . 'wp-admin/' );
  1073. die();
  1074. }
  1075. }
  1076. add_action( 'template_redirect', 'redirect_mu_dashboard' );
  1077. function get_dashboard_blog() {
  1078. if ( $blog = get_site_option( 'dashboard_blog' ) )
  1079. return get_blog_details( $blog );
  1080. return get_blog_details( $GLOBALS['current_site']->blog_id );
  1081. }
  1082. function is_user_option_local( $key, $user_id = 0, $blog_id = 0 ) {
  1083. global $wpdb;
  1084. $current_user = wp_get_current_user();
  1085. if ( $user_id == 0 )
  1086. $user_id = $current_user->ID;
  1087. if ( $blog_id == 0 )
  1088. $blog_id = $wpdb->blogid;
  1089. $local_key = $wpdb->base_prefix . $blog_id . '_' . $key;
  1090. if ( isset( $current_user->$local_key ) )
  1091. return true;
  1092. return false;
  1093. }
  1094. function users_can_register_signup_filter() {
  1095. $registration = get_site_option('registration');
  1096. if ( $registration == 'all' || $registration == 'user' )
  1097. return true;
  1098. return false;
  1099. }
  1100. add_filter('option_users_can_register', 'users_can_register_signup_filter');
  1101. function welcome_user_msg_filter( $text ) {
  1102. if ( !$text ) {
  1103. return __( 'Dear User,
  1104. Your new account is set up.
  1105. You can log in with the following information:
  1106. Username: USERNAME
  1107. Password: PASSWORD
  1108. LOGINLINK
  1109. Thanks!
  1110. --The Team @ SITE_NAME' );
  1111. }
  1112. return $text;
  1113. }
  1114. add_filter( 'site_option_welcome_user_email', 'welcome_user_msg_filter' );
  1115. /**
  1116. * Whether to force SSL on content.
  1117. *
  1118. * @since 2.8.5
  1119. *
  1120. * @param string|bool $force
  1121. * @return bool True if forced, false if not forced.
  1122. */
  1123. function force_ssl_content( $force = '' ) {
  1124. static $forced_content;
  1125. if ( '' != $force ) {
  1126. $old_forced = $forced_content;
  1127. $forced_content = $force;
  1128. return $old_forced;
  1129. }
  1130. return $forced_content;
  1131. }
  1132. /**
  1133. * Formats an String URL to use HTTPS if HTTP is found.
  1134. * Useful as a filter.
  1135. *
  1136. * @since 2.8.5
  1137. **/
  1138. function filter_SSL( $url ) {
  1139. if ( !is_string( $url ) )
  1140. return get_bloginfo( 'url' ); //return home blog url with proper scheme
  1141. $arrURL = parse_url( $url );
  1142. if ( force_ssl_content() && is_ssl() ) {
  1143. if ( 'http' === $arrURL['scheme'] && 'https' !== $arrURL['scheme'] )
  1144. $url = str_replace( $arrURL['scheme'], 'https', $url );
  1145. }
  1146. return $url;
  1147. }
  1148. ?>