PageRenderTime 84ms CodeModel.GetById 39ms RepoModel.GetById 0ms app.codeStats 1ms

/web/wp-includes/ms-functions.php

https://bitbucket.org/abreuleonel64/comoprogramarphp
PHP | 1984 lines | 972 code | 269 blank | 743 comment | 281 complexity | 90a7d301ff4048ad5b37434fb793b6f2 MD5 | raw file
Possible License(s): LGPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * Multisite WordPress API
  4. *
  5. * @package WordPress
  6. * @subpackage Multisite
  7. * @since 3.0.0
  8. */
  9. /**
  10. * Gets the network's site and user counts.
  11. *
  12. * @since MU 1.0
  13. * @uses get_blog_count()
  14. * @uses get_user_count()
  15. *
  16. * @return array Site and user count for the network.
  17. */
  18. function get_sitestats() {
  19. global $wpdb;
  20. $stats = array(
  21. 'blogs' => get_blog_count(),
  22. 'users' => get_user_count(),
  23. );
  24. return $stats;
  25. }
  26. /**
  27. * Get the admin for a domain/path combination.
  28. *
  29. * @since MU 1.0
  30. *
  31. * @param string $sitedomain Optional. Site domain.
  32. * @param string $path Optional. Site path.
  33. * @return array The network admins
  34. */
  35. function get_admin_users_for_domain( $sitedomain = '', $path = '' ) {
  36. global $wpdb;
  37. if ( ! $sitedomain )
  38. $site_id = $wpdb->siteid;
  39. else
  40. $site_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->site WHERE domain = %s AND path = %s", $sitedomain, $path ) );
  41. if ( $site_id )
  42. 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 );
  43. return false;
  44. }
  45. /**
  46. * Get one of a user's active blogs
  47. *
  48. * Returns the user's primary blog, if she has one and
  49. * it is active. If it's inactive, function returns another
  50. * active blog of the user. If none are found, the user
  51. * is added as a Subscriber to the Dashboard Blog and that blog
  52. * is returned.
  53. *
  54. * @since MU 1.0
  55. * @uses get_blogs_of_user()
  56. * @uses add_user_to_blog()
  57. * @uses get_blog_details()
  58. *
  59. * @param int $user_id The unique ID of the user
  60. * @return object The blog object
  61. */
  62. function get_active_blog_for_user( $user_id ) {
  63. global $wpdb;
  64. $blogs = get_blogs_of_user( $user_id );
  65. if ( empty( $blogs ) )
  66. return null;
  67. if ( !is_multisite() )
  68. return $blogs[$wpdb->blogid];
  69. $primary_blog = get_user_meta( $user_id, 'primary_blog', true );
  70. $first_blog = current($blogs);
  71. if ( false !== $primary_blog ) {
  72. if ( ! isset( $blogs[ $primary_blog ] ) ) {
  73. update_user_meta( $user_id, 'primary_blog', $first_blog->userblog_id );
  74. $primary = get_blog_details( $first_blog->userblog_id );
  75. } else {
  76. $primary = get_blog_details( $primary_blog );
  77. }
  78. } else {
  79. //TODO Review this call to add_user_to_blog too - to get here the user must have a role on this blog?
  80. add_user_to_blog( $first_blog->userblog_id, $user_id, 'subscriber' );
  81. update_user_meta( $user_id, 'primary_blog', $first_blog->userblog_id );
  82. $primary = $first_blog;
  83. }
  84. if ( ( ! is_object( $primary ) ) || ( $primary->archived == 1 || $primary->spam == 1 || $primary->deleted == 1 ) ) {
  85. $blogs = get_blogs_of_user( $user_id, true ); // if a user's primary blog is shut down, check their other blogs.
  86. $ret = false;
  87. if ( is_array( $blogs ) && count( $blogs ) > 0 ) {
  88. foreach ( (array) $blogs as $blog_id => $blog ) {
  89. if ( $blog->site_id != $wpdb->siteid )
  90. continue;
  91. $details = get_blog_details( $blog_id );
  92. if ( is_object( $details ) && $details->archived == 0 && $details->spam == 0 && $details->deleted == 0 ) {
  93. $ret = $blog;
  94. if ( get_user_meta( $user_id , 'primary_blog', true ) != $blog_id )
  95. update_user_meta( $user_id, 'primary_blog', $blog_id );
  96. if ( !get_user_meta($user_id , 'source_domain', true) )
  97. update_user_meta( $user_id, 'source_domain', $blog->domain );
  98. break;
  99. }
  100. }
  101. } else {
  102. return null;
  103. }
  104. return $ret;
  105. } else {
  106. return $primary;
  107. }
  108. }
  109. /**
  110. * The number of active users in your installation.
  111. *
  112. * The count is cached and updated twice daily. This is not a live count.
  113. *
  114. * @since MU 2.7
  115. *
  116. * @return int
  117. */
  118. function get_user_count() {
  119. return get_site_option( 'user_count' );
  120. }
  121. /**
  122. * The number of active sites on your installation.
  123. *
  124. * The count is cached and updated twice daily. This is not a live count.
  125. *
  126. * @since MU 1.0
  127. *
  128. * @param int $id Optional. A site_id.
  129. * @return int
  130. */
  131. function get_blog_count( $id = 0 ) {
  132. return get_site_option( 'blog_count' );
  133. }
  134. /**
  135. * Get a blog post from any site on the network.
  136. *
  137. * @since MU 1.0
  138. *
  139. * @param int $blog_id ID of the blog.
  140. * @param int $post_id ID of the post you're looking for.
  141. * @return object The post.
  142. */
  143. function get_blog_post( $blog_id, $post_id ) {
  144. global $wpdb;
  145. $key = $blog_id . '-' . $post_id;
  146. $post = wp_cache_get( $key, 'global-posts' );
  147. if ( $post == false ) {
  148. $post = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM ' . $wpdb->get_blog_prefix( $blog_id ) . 'posts WHERE ID = %d', $post_id ) );
  149. wp_cache_add( $key, $post, 'global-posts' );
  150. }
  151. return $post;
  152. }
  153. /**
  154. * Add a user to a blog.
  155. *
  156. * Use the 'add_user_to_blog' action to fire an event when
  157. * users are added to a blog.
  158. *
  159. * @since MU 1.0
  160. *
  161. * @param int $blog_id ID of the blog you're adding the user to.
  162. * @param int $user_id ID of the user you're adding.
  163. * @param string $role The role you want the user to have
  164. * @return bool
  165. */
  166. function add_user_to_blog( $blog_id, $user_id, $role ) {
  167. switch_to_blog($blog_id);
  168. $user = new WP_User($user_id);
  169. if ( empty( $user->ID ) ) {
  170. restore_current_blog();
  171. return new WP_Error('user_does_not_exist', __('That user does not exist.'));
  172. }
  173. if ( !get_user_meta($user_id, 'primary_blog', true) ) {
  174. update_user_meta($user_id, 'primary_blog', $blog_id);
  175. $details = get_blog_details($blog_id);
  176. update_user_meta($user_id, 'source_domain', $details->domain);
  177. }
  178. $user->set_role($role);
  179. do_action('add_user_to_blog', $user_id, $role, $blog_id);
  180. wp_cache_delete( $user_id, 'users' );
  181. restore_current_blog();
  182. return true;
  183. }
  184. /**
  185. * Remove a user from a blog.
  186. *
  187. * Use the 'remove_user_from_blog' action to fire an event when
  188. * users are removed from a blog.
  189. *
  190. * Accepts an optional $reassign parameter, if you want to
  191. * reassign the user's blog posts to another user upon removal.
  192. *
  193. * @since MU 1.0
  194. *
  195. * @param int $user_id ID of the user you're removing.
  196. * @param int $blog_id ID of the blog you're removing the user from.
  197. * @param string $reassign Optional. A user to whom to reassign posts.
  198. * @return bool
  199. */
  200. function remove_user_from_blog($user_id, $blog_id = '', $reassign = '') {
  201. global $wpdb;
  202. switch_to_blog($blog_id);
  203. $user_id = (int) $user_id;
  204. do_action('remove_user_from_blog', $user_id, $blog_id);
  205. // If being removed from the primary blog, set a new primary if the user is assigned
  206. // to multiple blogs.
  207. $primary_blog = get_user_meta($user_id, 'primary_blog', true);
  208. if ( $primary_blog == $blog_id ) {
  209. $new_id = '';
  210. $new_domain = '';
  211. $blogs = get_blogs_of_user($user_id);
  212. foreach ( (array) $blogs as $blog ) {
  213. if ( $blog->userblog_id == $blog_id )
  214. continue;
  215. $new_id = $blog->userblog_id;
  216. $new_domain = $blog->domain;
  217. break;
  218. }
  219. update_user_meta($user_id, 'primary_blog', $new_id);
  220. update_user_meta($user_id, 'source_domain', $new_domain);
  221. }
  222. // wp_revoke_user($user_id);
  223. $user = new WP_User($user_id);
  224. if ( empty( $user->ID ) ) {
  225. restore_current_blog();
  226. return new WP_Error('user_does_not_exist', __('That user does not exist.'));
  227. }
  228. $user->remove_all_caps();
  229. $blogs = get_blogs_of_user($user_id);
  230. if ( count($blogs) == 0 ) {
  231. update_user_meta($user_id, 'primary_blog', '');
  232. update_user_meta($user_id, 'source_domain', '');
  233. }
  234. if ( $reassign != '' ) {
  235. $reassign = (int) $reassign;
  236. $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_author = %d WHERE post_author = %d", $reassign, $user_id) );
  237. $wpdb->query( $wpdb->prepare("UPDATE $wpdb->links SET link_owner = %d WHERE link_owner = %d", $reassign, $user_id) );
  238. }
  239. restore_current_blog();
  240. return true;
  241. }
  242. /**
  243. * Create an empty blog.
  244. *
  245. * @since MU 1.0
  246. * @uses install_blog()
  247. *
  248. * @param string $domain The new blog's domain.
  249. * @param string $path The new blog's path.
  250. * @param string $string The new blog's title.
  251. * @param int $site Optional. Defaults to 1.
  252. * @return int The ID of the newly created blog
  253. */
  254. function create_empty_blog( $domain, $path, $weblog_title, $site_id = 1 ) {
  255. $domain = addslashes( $domain );
  256. $weblog_title = addslashes( $weblog_title );
  257. if ( empty($path) )
  258. $path = '/';
  259. // Check if the domain has been used already. We should return an error message.
  260. if ( domain_exists($domain, $path, $site_id) )
  261. return __( '<strong>ERROR</strong>: Site URL already taken.' );
  262. // Need to back up wpdb table names, and create a new wp_blogs entry for new blog.
  263. // Need to get blog_id from wp_blogs, and create new table names.
  264. // Must restore table names at the end of function.
  265. if ( ! $blog_id = insert_blog($domain, $path, $site_id) )
  266. return __( '<strong>ERROR</strong>: problem creating site entry.' );
  267. switch_to_blog($blog_id);
  268. install_blog($blog_id);
  269. restore_current_blog();
  270. return $blog_id;
  271. }
  272. /**
  273. * Get the permalink for a post on another blog.
  274. *
  275. * @since MU 1.0
  276. *
  277. * @param int $_blog_id ID of the source blog.
  278. * @param int $post_id ID of the desired post.
  279. * @return string The post's permalink
  280. */
  281. function get_blog_permalink( $_blog_id, $post_id ) {
  282. $key = "{$_blog_id}-{$post_id}-blog_permalink";
  283. $link = wp_cache_get( $key, 'site-options' );
  284. if ( $link == false ) {
  285. switch_to_blog( $_blog_id );
  286. $link = get_permalink( $post_id );
  287. restore_current_blog();
  288. wp_cache_add( $key, $link, 'site-options', 360 );
  289. }
  290. return $link;
  291. }
  292. /**
  293. * Get a blog's numeric ID from its URL.
  294. *
  295. * On a subdirectory installation like example.com/blog1/,
  296. * $domain will be the root 'example.com' and $path the
  297. * subdirectory '/blog1/'. With subdomains like blog1.example.com,
  298. * $domain is 'blog1.example.com' and $path is '/'.
  299. *
  300. * @since MU 2.6.5
  301. *
  302. * @param string $domain
  303. * @param string $path Optional. Not required for subdomain installations.
  304. * @return int
  305. */
  306. function get_blog_id_from_url( $domain, $path = '/' ) {
  307. global $wpdb;
  308. $domain = strtolower( $wpdb->escape( $domain ) );
  309. $path = strtolower( $wpdb->escape( $path ) );
  310. $id = wp_cache_get( md5( $domain . $path ), 'blog-id-cache' );
  311. if ( $id == -1 ) { // blog does not exist
  312. return 0;
  313. } elseif ( $id ) {
  314. return (int)$id;
  315. }
  316. $id = $wpdb->get_var( "SELECT blog_id FROM $wpdb->blogs WHERE domain = '$domain' and path = '$path' /* get_blog_id_from_url */" );
  317. if ( !$id ) {
  318. wp_cache_set( md5( $domain . $path ), -1, 'blog-id-cache' );
  319. return false;
  320. }
  321. wp_cache_set( md5( $domain . $path ), $id, 'blog-id-cache' );
  322. return $id;
  323. }
  324. // Admin functions
  325. /**
  326. * Checks an email address against a list of banned domains.
  327. *
  328. * This function checks against the Banned Email Domains list
  329. * at wp-admin/network/settings.php. The check is only run on
  330. * self-registrations; user creation at wp-admin/network/users.php
  331. * bypasses this check.
  332. *
  333. * @since MU
  334. *
  335. * @param string $user_email The email provided by the user at registration.
  336. * @return bool Returns true when the email address is banned.
  337. */
  338. function is_email_address_unsafe( $user_email ) {
  339. $banned_names = get_site_option( 'banned_email_domains' );
  340. if ($banned_names && !is_array( $banned_names ))
  341. $banned_names = explode( "\n", $banned_names);
  342. if ( is_array( $banned_names ) && empty( $banned_names ) == false ) {
  343. $email_domain = strtolower( substr( $user_email, 1 + strpos( $user_email, '@' ) ) );
  344. foreach ( (array) $banned_names as $banned_domain ) {
  345. if ( $banned_domain == '' )
  346. continue;
  347. if (
  348. strstr( $email_domain, $banned_domain ) ||
  349. (
  350. strstr( $banned_domain, '/' ) &&
  351. preg_match( $banned_domain, $email_domain )
  352. )
  353. )
  354. return true;
  355. }
  356. }
  357. return false;
  358. }
  359. /**
  360. * Processes new user registrations.
  361. *
  362. * Checks the data provided by the user during signup. Verifies
  363. * the validity and uniqueness of user names and user email addresses,
  364. * and checks email addresses against admin-provided domain
  365. * whitelists and blacklists.
  366. *
  367. * The hook 'wpmu_validate_user_signup' provides an easy way
  368. * to modify the signup process. The value $result, which is passed
  369. * to the hook, contains both the user-provided info and the error
  370. * messages created by the function. 'wpmu_validate_user_signup' allows
  371. * you to process the data in any way you'd like, and unset the
  372. * relevant errors if necessary.
  373. *
  374. * @since MU
  375. * @uses is_email_address_unsafe()
  376. * @uses username_exists()
  377. * @uses email_exists()
  378. *
  379. * @param string $user_name The login name provided by the user.
  380. * @param string $user_email The email provided by the user.
  381. * @return array Contains username, email, and error messages.
  382. */
  383. function wpmu_validate_user_signup($user_name, $user_email) {
  384. global $wpdb;
  385. $errors = new WP_Error();
  386. $orig_username = $user_name;
  387. $user_name = preg_replace( '/\s+/', '', sanitize_user( $user_name, true ) );
  388. $maybe = array();
  389. preg_match( '/[a-z0-9]+/', $user_name, $maybe );
  390. if ( $user_name != $orig_username || $user_name != $maybe[0] ) {
  391. $errors->add( 'user_name', __( 'Only lowercase letters (a-z) and numbers are allowed.' ) );
  392. $user_name = $orig_username;
  393. }
  394. $user_email = sanitize_email( $user_email );
  395. if ( empty( $user_name ) )
  396. $errors->add('user_name', __('Please enter a username'));
  397. $illegal_names = get_site_option( 'illegal_names' );
  398. if ( is_array( $illegal_names ) == false ) {
  399. $illegal_names = array( 'www', 'web', 'root', 'admin', 'main', 'invite', 'administrator' );
  400. add_site_option( 'illegal_names', $illegal_names );
  401. }
  402. if ( in_array( $user_name, $illegal_names ) == true )
  403. $errors->add('user_name', __('That username is not allowed'));
  404. if ( is_email_address_unsafe( $user_email ) )
  405. $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.'));
  406. if ( strlen( $user_name ) < 4 )
  407. $errors->add('user_name', __('Username must be at least 4 characters'));
  408. if ( strpos( ' ' . $user_name, '_' ) != false )
  409. $errors->add( 'user_name', __( 'Sorry, usernames may not contain the character &#8220;_&#8221;!' ) );
  410. // all numeric?
  411. $match = array();
  412. preg_match( '/[0-9]*/', $user_name, $match );
  413. if ( $match[0] == $user_name )
  414. $errors->add('user_name', __('Sorry, usernames must have letters too!'));
  415. if ( !is_email( $user_email ) )
  416. $errors->add('user_email', __('Please enter a correct email address'));
  417. $limited_email_domains = get_site_option( 'limited_email_domains' );
  418. if ( is_array( $limited_email_domains ) && empty( $limited_email_domains ) == false ) {
  419. $emaildomain = substr( $user_email, 1 + strpos( $user_email, '@' ) );
  420. if ( in_array( $emaildomain, $limited_email_domains ) == false )
  421. $errors->add('user_email', __('Sorry, that email address is not allowed!'));
  422. }
  423. // Check if the username has been used already.
  424. if ( username_exists($user_name) )
  425. $errors->add('user_name', __('Sorry, that username already exists!'));
  426. // Check if the email address has been used already.
  427. if ( email_exists($user_email) )
  428. $errors->add('user_email', __('Sorry, that email address is already used!'));
  429. // Has someone already signed up for this username?
  430. $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE user_login = %s", $user_name) );
  431. if ( $signup != null ) {
  432. $registered_at = mysql2date('U', $signup->registered);
  433. $now = current_time( 'timestamp', true );
  434. $diff = $now - $registered_at;
  435. // If registered more than two days ago, cancel registration and let this signup go through.
  436. if ( $diff > 172800 )
  437. $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->signups WHERE user_login = %s", $user_name) );
  438. else
  439. $errors->add('user_name', __('That username is currently reserved but may be available in a couple of days.'));
  440. if ( $signup->active == 0 && $signup->user_email == $user_email )
  441. $errors->add('user_email_used', __('username and email used'));
  442. }
  443. $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE user_email = %s", $user_email) );
  444. if ( $signup != null ) {
  445. $diff = current_time( 'timestamp', true ) - mysql2date('U', $signup->registered);
  446. // If registered more than two days ago, cancel registration and let this signup go through.
  447. if ( $diff > 172800 )
  448. $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->signups WHERE user_email = %s", $user_email) );
  449. else
  450. $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.'));
  451. }
  452. $result = array('user_name' => $user_name, 'orig_username' => $orig_username, 'user_email' => $user_email, 'errors' => $errors);
  453. return apply_filters('wpmu_validate_user_signup', $result);
  454. }
  455. /**
  456. * Processes new site registrations.
  457. *
  458. * Checks the data provided by the user during blog signup. Verifies
  459. * the validity and uniqueness of blog paths and domains.
  460. *
  461. * This function prevents the current user from registering a new site
  462. * with a blogname equivalent to another user's login name. Passing the
  463. * $user parameter to the function, where $user is the other user, is
  464. * effectively an override of this limitation.
  465. *
  466. * Filter 'wpmu_validate_blog_signup' if you want to modify
  467. * the way that WordPress validates new site signups.
  468. *
  469. * @since MU
  470. * @uses domain_exists()
  471. * @uses username_exists()
  472. *
  473. * @param string $blogname The blog name provided by the user. Must be unique.
  474. * @param string $blog_title The blog title provided by the user.
  475. * @return array Contains the new site data and error messages.
  476. */
  477. function wpmu_validate_blog_signup($blogname, $blog_title, $user = '') {
  478. global $wpdb, $domain, $base, $current_site;
  479. $blog_title = strip_tags( $blog_title );
  480. $blog_title = substr( $blog_title, 0, 50 );
  481. $errors = new WP_Error();
  482. $illegal_names = get_site_option( 'illegal_names' );
  483. if ( $illegal_names == false ) {
  484. $illegal_names = array( 'www', 'web', 'root', 'admin', 'main', 'invite', 'administrator' );
  485. add_site_option( 'illegal_names', $illegal_names );
  486. }
  487. // On sub dir installs, Some names are so illegal, only a filter can spring them from jail
  488. if (! is_subdomain_install() )
  489. $illegal_names = array_merge($illegal_names, apply_filters( 'subdirectory_reserved_names', array( 'page', 'comments', 'blog', 'files', 'feed' ) ) );
  490. if ( empty( $blogname ) )
  491. $errors->add('blogname', __('Please enter a site name'));
  492. if ( preg_match( '/[^a-z0-9]+/', $blogname ) )
  493. $errors->add('blogname', __('Only lowercase letters and numbers allowed'));
  494. if ( in_array( $blogname, $illegal_names ) == true )
  495. $errors->add('blogname', __('That name is not allowed'));
  496. if ( strlen( $blogname ) < 4 && !is_super_admin() )
  497. $errors->add('blogname', __('Site name must be at least 4 characters'));
  498. if ( strpos( ' ' . $blogname, '_' ) != false )
  499. $errors->add( 'blogname', __( 'Sorry, site names may not contain the character &#8220;_&#8221;!' ) );
  500. // do not allow users to create a blog that conflicts with a page on the main blog.
  501. 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 ) ) )
  502. $errors->add( 'blogname', __( 'Sorry, you may not use that site name.' ) );
  503. // all numeric?
  504. $match = array();
  505. preg_match( '/[0-9]*/', $blogname, $match );
  506. if ( $match[0] == $blogname )
  507. $errors->add('blogname', __('Sorry, site names must have letters too!'));
  508. $blogname = apply_filters( 'newblogname', $blogname );
  509. $blog_title = stripslashes( $blog_title );
  510. if ( empty( $blog_title ) )
  511. $errors->add('blog_title', __('Please enter a site title'));
  512. // Check if the domain/path has been used already.
  513. if ( is_subdomain_install() ) {
  514. $mydomain = $blogname . '.' . preg_replace( '|^www\.|', '', $domain );
  515. $path = $base;
  516. } else {
  517. $mydomain = "$domain";
  518. $path = $base.$blogname.'/';
  519. }
  520. if ( domain_exists($mydomain, $path) )
  521. $errors->add('blogname', __('Sorry, that site already exists!'));
  522. if ( username_exists( $blogname ) ) {
  523. if ( is_object( $user ) == false || ( is_object($user) && ( $user->user_login != $blogname ) ) )
  524. $errors->add( 'blogname', __( 'Sorry, that site is reserved!' ) );
  525. }
  526. // Has someone already signed up for this domain?
  527. $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE domain = %s AND path = %s", $mydomain, $path) ); // TODO: Check email too?
  528. if ( ! empty($signup) ) {
  529. $diff = current_time( 'timestamp', true ) - mysql2date('U', $signup->registered);
  530. // If registered more than two days ago, cancel registration and let this signup go through.
  531. if ( $diff > 172800 )
  532. $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->signups WHERE domain = %s AND path = %s", $mydomain, $path) );
  533. else
  534. $errors->add('blogname', __('That site is currently reserved but may be available in a couple days.'));
  535. }
  536. $result = array('domain' => $mydomain, 'path' => $path, 'blogname' => $blogname, 'blog_title' => $blog_title, 'errors' => $errors);
  537. return apply_filters('wpmu_validate_blog_signup', $result);
  538. }
  539. /**
  540. * Record site signup information for future activation.
  541. *
  542. * @since MU
  543. * @uses wpmu_signup_blog_notification()
  544. *
  545. * @param string $domain The requested domain.
  546. * @param string $path The requested path.
  547. * @param string $title The requested site title.
  548. * @param string $user The user's requested login name.
  549. * @param string $user_email The user's email address.
  550. * @param array $meta By default, contains the requested privacy setting and lang_id.
  551. */
  552. function wpmu_signup_blog($domain, $path, $title, $user, $user_email, $meta = '') {
  553. global $wpdb;
  554. $key = substr( md5( time() . rand() . $domain ), 0, 16 );
  555. $meta = serialize($meta);
  556. $domain = $wpdb->escape($domain);
  557. $path = $wpdb->escape($path);
  558. $title = $wpdb->escape($title);
  559. $wpdb->insert( $wpdb->signups, array(
  560. 'domain' => $domain,
  561. 'path' => $path,
  562. 'title' => $title,
  563. 'user_login' => $user,
  564. 'user_email' => $user_email,
  565. 'registered' => current_time('mysql', true),
  566. 'activation_key' => $key,
  567. 'meta' => $meta
  568. ) );
  569. wpmu_signup_blog_notification($domain, $path, $title, $user, $user_email, $key, $meta);
  570. }
  571. /**
  572. * Record user signup information for future activation.
  573. *
  574. * This function is used when user registration is open but
  575. * new site registration is not.
  576. *
  577. * @since MU
  578. * @uses wpmu_signup_user_notification()
  579. *
  580. * @param string $user The user's requested login name.
  581. * @param string $user_email The user's email address.
  582. * @param array $meta By default, this is an empty array.
  583. */
  584. function wpmu_signup_user($user, $user_email, $meta = '') {
  585. global $wpdb;
  586. // Format data
  587. $user = preg_replace( '/\s+/', '', sanitize_user( $user, true ) );
  588. $user_email = sanitize_email( $user_email );
  589. $key = substr( md5( time() . rand() . $user_email ), 0, 16 );
  590. $meta = serialize($meta);
  591. $wpdb->insert( $wpdb->signups, array(
  592. 'domain' => '',
  593. 'path' => '',
  594. 'title' => '',
  595. 'user_login' => $user,
  596. 'user_email' => $user_email,
  597. 'registered' => current_time('mysql', true),
  598. 'activation_key' => $key,
  599. 'meta' => $meta
  600. ) );
  601. wpmu_signup_user_notification($user, $user_email, $key, $meta);
  602. }
  603. /**
  604. * Notify user of signup success.
  605. *
  606. * This is the notification function used when site registration
  607. * is enabled.
  608. *
  609. * Filter 'wpmu_signup_blog_notification' to bypass this function or
  610. * replace it with your own notification behavior.
  611. *
  612. * Filter 'wpmu_signup_blog_notification_email' and
  613. * 'wpmu_signup_blog_notification_subject' to change the content
  614. * and subject line of the email sent to newly registered users.
  615. *
  616. * @since MU
  617. *
  618. * @param string $domain The new blog domain.
  619. * @param string $path The new blog path.
  620. * @param string $title The site title.
  621. * @param string $user The user's login name.
  622. * @param string $user_email The user's email address.
  623. * @param array $meta By default, contains the requested privacy setting and lang_id.
  624. * @param string $key The activation key created in wpmu_signup_blog()
  625. * @return bool
  626. */
  627. function wpmu_signup_blog_notification($domain, $path, $title, $user, $user_email, $key, $meta = '') {
  628. global $current_site;
  629. if ( !apply_filters('wpmu_signup_blog_notification', $domain, $path, $title, $user, $user_email, $key, $meta) )
  630. return false;
  631. // Send email with activation link.
  632. if ( !is_subdomain_install() || $current_site->id != 1 )
  633. $activate_url = network_site_url("wp-activate.php?key=$key");
  634. else
  635. $activate_url = "http://{$domain}{$path}wp-activate.php?key=$key"; // @todo use *_url() API
  636. $activate_url = esc_url($activate_url);
  637. $admin_email = get_site_option( 'admin_email' );
  638. if ( $admin_email == '' )
  639. $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
  640. $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
  641. $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
  642. $message = sprintf(
  643. apply_filters( 'wpmu_signup_blog_notification_email',
  644. __( "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" ),
  645. $domain, $path, $title, $user, $user_email, $key, $meta
  646. ),
  647. $activate_url,
  648. esc_url( "http://{$domain}{$path}" ),
  649. $key
  650. );
  651. // TODO: Don't hard code activation link.
  652. $subject = sprintf(
  653. apply_filters( 'wpmu_signup_blog_notification_subject',
  654. __( '[%1$s] Activate %2$s' ),
  655. $domain, $path, $title, $user, $user_email, $key, $meta
  656. ),
  657. $from_name,
  658. esc_url( 'http://' . $domain . $path )
  659. );
  660. wp_mail($user_email, $subject, $message, $message_headers);
  661. return true;
  662. }
  663. /**
  664. * Notify user of signup success.
  665. *
  666. * This is the notification function used when no new site has
  667. * been requested.
  668. *
  669. * Filter 'wpmu_signup_user_notification' to bypass this function or
  670. * replace it with your own notification behavior.
  671. *
  672. * Filter 'wpmu_signup_user_notification_email' and
  673. * 'wpmu_signup_user_notification_subject' to change the content
  674. * and subject line of the email sent to newly registered users.
  675. *
  676. * @since MU
  677. *
  678. * @param string $user The user's login name.
  679. * @param string $user_email The user's email address.
  680. * @param array $meta By default, an empty array.
  681. * @param string $key The activation key created in wpmu_signup_user()
  682. * @return bool
  683. */
  684. function wpmu_signup_user_notification($user, $user_email, $key, $meta = '') {
  685. if ( !apply_filters('wpmu_signup_user_notification', $user, $user_email, $key, $meta) )
  686. return false;
  687. // Send email with activation link.
  688. $admin_email = get_site_option( 'admin_email' );
  689. if ( $admin_email == '' )
  690. $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
  691. $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
  692. $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
  693. $message = sprintf(
  694. apply_filters( 'wpmu_signup_user_notification_email',
  695. __( "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" ),
  696. $user, $user_email, $key, $meta
  697. ),
  698. site_url( "wp-activate.php?key=$key" )
  699. );
  700. // TODO: Don't hard code activation link.
  701. $subject = sprintf(
  702. apply_filters( 'wpmu_signup_user_notification_subject',
  703. __( '[%1$s] Activate %2$s' ),
  704. $user, $user_email, $key, $meta
  705. ),
  706. $from_name,
  707. $user
  708. );
  709. wp_mail($user_email, $subject, $message, $message_headers);
  710. return true;
  711. }
  712. /**
  713. * Activate a signup.
  714. *
  715. * Hook to 'wpmu_activate_user' or 'wpmu_activate_blog' for events
  716. * that should happen only when users or sites are self-created (since
  717. * those actions are not called when users and sites are created
  718. * by a Super Admin).
  719. *
  720. * @since MU
  721. * @uses wp_generate_password()
  722. * @uses wpmu_welcome_user_notification()
  723. * @uses add_user_to_blog()
  724. * @uses add_new_user_to_blog()
  725. * @uses wpmu_create_user()
  726. * @uses wpmu_create_blog()
  727. * @uses wpmu_welcome_notification()
  728. *
  729. * @param string $key The activation key provided to the user.
  730. * @return array An array containing information about the activated user and/or blog
  731. */
  732. function wpmu_activate_signup($key) {
  733. global $wpdb, $current_site;
  734. $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE activation_key = %s", $key) );
  735. if ( empty( $signup ) )
  736. return new WP_Error( 'invalid_key', __( 'Invalid activation key.' ) );
  737. if ( $signup->active ) {
  738. if ( empty( $signup->domain ) )
  739. return new WP_Error( 'already_active', __( 'The user is already active.' ), $signup );
  740. else
  741. return new WP_Error( 'already_active', __( 'The site is already active.' ), $signup );
  742. }
  743. $meta = unserialize($signup->meta);
  744. $user_login = $wpdb->escape($signup->user_login);
  745. $user_email = $wpdb->escape($signup->user_email);
  746. $password = wp_generate_password( 12, false );
  747. $user_id = username_exists($user_login);
  748. if ( ! $user_id )
  749. $user_id = wpmu_create_user($user_login, $password, $user_email);
  750. else
  751. $user_already_exists = true;
  752. if ( ! $user_id )
  753. return new WP_Error('create_user', __('Could not create user'), $signup);
  754. $now = current_time('mysql', true);
  755. if ( empty($signup->domain) ) {
  756. $wpdb->update( $wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key) );
  757. if ( isset( $user_already_exists ) )
  758. return new WP_Error( 'user_already_exists', __( 'That username is already activated.' ), $signup);
  759. wpmu_welcome_user_notification($user_id, $password, $meta);
  760. add_new_user_to_blog( $user_id, $user_email, $meta );
  761. do_action('wpmu_activate_user', $user_id, $password, $meta);
  762. return array('user_id' => $user_id, 'password' => $password, 'meta' => $meta);
  763. }
  764. $blog_id = wpmu_create_blog( $signup->domain, $signup->path, $signup->title, $user_id, $meta, $wpdb->siteid );
  765. // TODO: What to do if we create a user but cannot create a blog?
  766. if ( is_wp_error($blog_id) ) {
  767. // If blog is taken, that means a previous attempt to activate this blog failed in between creating the blog and
  768. // setting the activation flag. Let's just set the active flag and instruct the user to reset their password.
  769. if ( 'blog_taken' == $blog_id->get_error_code() ) {
  770. $blog_id->add_data( $signup );
  771. $wpdb->update( $wpdb->signups, array( 'active' => 1, 'activated' => $now ), array( 'activation_key' => $key ) );
  772. }
  773. return $blog_id;
  774. }
  775. $wpdb->update( $wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key) );
  776. wpmu_welcome_notification($blog_id, $user_id, $password, $signup->title, $meta);
  777. do_action('wpmu_activate_blog', $blog_id, $user_id, $password, $signup->title, $meta);
  778. return array('blog_id' => $blog_id, 'user_id' => $user_id, 'password' => $password, 'title' => $signup->title, 'meta' => $meta);
  779. }
  780. /**
  781. * Create a user.
  782. *
  783. * This function runs when a user self-registers as well as when
  784. * a Super Admin creates a new user. Hook to 'wpmu_new_user' for events
  785. * that should affect all new users, but only on Multisite (otherwise
  786. * use 'user_register').
  787. *
  788. * @since MU
  789. * @uses wp_create_user()
  790. *
  791. * @param string $user_name The new user's login name.
  792. * @param string $password The new user's password.
  793. * @param string $email The new user's email address.
  794. * @return mixed Returns false on failure, or int $user_id on success
  795. */
  796. function wpmu_create_user( $user_name, $password, $email) {
  797. $user_name = preg_replace( '/\s+/', '', sanitize_user( $user_name, true ) );
  798. $user_id = wp_create_user( $user_name, $password, $email );
  799. if ( is_wp_error($user_id) )
  800. return false;
  801. // Newly created users have no roles or caps until they are added to a blog.
  802. delete_user_option( $user_id, 'capabilities' );
  803. delete_user_option( $user_id, 'user_level' );
  804. do_action( 'wpmu_new_user', $user_id );
  805. return $user_id;
  806. }
  807. /**
  808. * Create a site.
  809. *
  810. * This function runs when a user self-registers a new site as well
  811. * as when a Super Admin creates a new site. Hook to 'wpmu_new_blog'
  812. * for events that should affect all new sites.
  813. *
  814. * On subdirectory installs, $domain is the same as the main site's
  815. * domain, and the path is the subdirectory name (eg 'example.com'
  816. * and '/blog1/'). On subdomain installs, $domain is the new subdomain +
  817. * root domain (eg 'blog1.example.com'), and $path is '/'.
  818. *
  819. * @since MU
  820. * @uses domain_exists()
  821. * @uses insert_blog()
  822. * @uses wp_install_defaults()
  823. * @uses add_user_to_blog()
  824. *
  825. * @param string $domain The new site's domain.
  826. * @param string $path The new site's path.
  827. * @param string $title The new site's title.
  828. * @param int $user_id The user ID of the new site's admin.
  829. * @param array $meta Optional. Used to set initial site options.
  830. * @param int $site_id Optional. Only relevant on multi-network installs.
  831. * @return mixed Returns WP_Error object on failure, int $blog_id on success
  832. */
  833. function wpmu_create_blog($domain, $path, $title, $user_id, $meta = '', $site_id = 1) {
  834. $domain = preg_replace( '/\s+/', '', sanitize_user( $domain, true ) );
  835. if ( is_subdomain_install() )
  836. $domain = str_replace( '@', '', $domain );
  837. $title = strip_tags( $title );
  838. $user_id = (int) $user_id;
  839. if ( empty($path) )
  840. $path = '/';
  841. // Check if the domain has been used already. We should return an error message.
  842. if ( domain_exists($domain, $path, $site_id) )
  843. return new WP_Error('blog_taken', __('Site already exists.'));
  844. if ( !defined('WP_INSTALLING') )
  845. define( 'WP_INSTALLING', true );
  846. if ( ! $blog_id = insert_blog($domain, $path, $site_id) )
  847. return new WP_Error('insert_blog', __('Could not create site.'));
  848. switch_to_blog($blog_id);
  849. install_blog($blog_id, $title);
  850. wp_install_defaults($user_id);
  851. add_user_to_blog($blog_id, $user_id, 'administrator');
  852. if ( is_array($meta) ) foreach ($meta as $key => $value) {
  853. if ( $key == 'public' || $key == 'archived' || $key == 'mature' || $key == 'spam' || $key == 'deleted' || $key == 'lang_id' )
  854. update_blog_status( $blog_id, $key, $value );
  855. else
  856. update_option( $key, $value );
  857. }
  858. add_option( 'WPLANG', get_site_option( 'WPLANG' ) );
  859. update_option( 'blog_public', (int)$meta['public'] );
  860. if ( ! is_super_admin( $user_id ) && ! get_user_meta( $user_id, 'primary_blog', true ) )
  861. update_user_meta( $user_id, 'primary_blog', $blog_id );
  862. restore_current_blog();
  863. do_action( 'wpmu_new_blog', $blog_id, $user_id, $domain, $path, $site_id, $meta );
  864. return $blog_id;
  865. }
  866. /**
  867. * Notifies the network admin that a new site has been activated.
  868. *
  869. * Filter 'newblog_notify_siteadmin' to change the content of
  870. * the notification email.
  871. *
  872. * @since MU
  873. *
  874. * @param int $blog_id The new site's ID.
  875. * @return bool
  876. */
  877. function newblog_notify_siteadmin( $blog_id, $deprecated = '' ) {
  878. if ( get_site_option( 'registrationnotification' ) != 'yes' )
  879. return false;
  880. $email = get_site_option( 'admin_email' );
  881. if ( is_email($email) == false )
  882. return false;
  883. $options_site_url = esc_url(network_admin_url('settings.php'));
  884. switch_to_blog( $blog_id );
  885. $blogname = get_option( 'blogname' );
  886. $siteurl = site_url();
  887. restore_current_blog();
  888. $msg = sprintf( __( 'New Site: %1s
  889. URL: %2s
  890. Remote IP: %3s
  891. Disable these notifications: %4s' ), $blogname, $siteurl, $_SERVER['REMOTE_ADDR'], $options_site_url);
  892. $msg = apply_filters( 'newblog_notify_siteadmin', $msg );
  893. wp_mail( $email, sprintf( __( 'New Site Registration: %s' ), $siteurl ), $msg );
  894. return true;
  895. }
  896. /**
  897. * Notifies the network admin that a new user has been activated.
  898. *
  899. * Filter 'newuser_notify_siteadmin' to change the content of
  900. * the notification email.
  901. *
  902. * @since MU
  903. *
  904. * @param int $user_id The new user's ID.
  905. * @return bool
  906. */
  907. function newuser_notify_siteadmin( $user_id ) {
  908. if ( get_site_option( 'registrationnotification' ) != 'yes' )
  909. return false;
  910. $email = get_site_option( 'admin_email' );
  911. if ( is_email($email) == false )
  912. return false;
  913. $user = new WP_User($user_id);
  914. $options_site_url = esc_url(network_admin_url('settings.php'));
  915. $msg = sprintf(__('New User: %1s
  916. Remote IP: %2s
  917. Disable these notifications: %3s'), $user->user_login, $_SERVER['REMOTE_ADDR'], $options_site_url);
  918. $msg = apply_filters( 'newuser_notify_siteadmin', $msg );
  919. wp_mail( $email, sprintf(__('New User Registration: %s'), $user->user_login), $msg );
  920. return true;
  921. }
  922. /**
  923. * Check whether a blogname is already taken.
  924. *
  925. * Used during the new site registration process to ensure
  926. * that each blogname is unique.
  927. *
  928. * @since MU
  929. *
  930. * @param string $domain The domain to be checked.
  931. * @param string $path The path to be checked.
  932. * @param int $site_id Optional. Relevant only on multi-network installs.
  933. * @return int
  934. */
  935. function domain_exists($domain, $path, $site_id = 1) {
  936. global $wpdb;
  937. 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) );
  938. }
  939. /**
  940. * Store basic site info in the blogs table.
  941. *
  942. * This function creates a row in the wp_blogs table and returns
  943. * the new blog's ID. It is the first step in creating a new blog.
  944. *
  945. * @since MU
  946. *
  947. * @param string $domain The domain of the new site.
  948. * @param string $path The path of the new site.
  949. * @param int $site_id Unless you're running a multi-network install, be sure to set this value to 1.
  950. * @return int The ID of the new row
  951. */
  952. function insert_blog($domain, $path, $site_id) {
  953. global $wpdb;
  954. $path = trailingslashit($path);
  955. $site_id = (int) $site_id;
  956. $result = $wpdb->insert( $wpdb->blogs, array('site_id' => $site_id, 'domain' => $domain, 'path' => $path, 'registered' => current_time('mysql')) );
  957. if ( ! $result )
  958. return false;
  959. refresh_blog_details($wpdb->insert_id);
  960. return $wpdb->insert_id;
  961. }
  962. /**
  963. * Install an empty blog.
  964. *
  965. * Creates the new blog tables and options. If calling this function
  966. * directly, be sure to use switch_to_blog() first, so that $wpdb
  967. * points to the new blog.
  968. *
  969. * @since MU
  970. * @uses make_db_current_silent()
  971. * @uses populate_roles()
  972. *
  973. * @param int $blog_id The value returned by insert_blog().
  974. * @param string $blog_title The title of the new site.
  975. */
  976. function install_blog($blog_id, $blog_title = '') {
  977. global $wpdb, $table_prefix, $wp_roles;
  978. $wpdb->suppress_errors();
  979. // Cast for security
  980. $blog_id = (int) $blog_id;
  981. require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
  982. if ( $wpdb->get_results("SELECT ID FROM $wpdb->posts") )
  983. 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>');
  984. $wpdb->suppress_errors(false);
  985. $url = get_blogaddress_by_id($blog_id);
  986. // Set everything up
  987. make_db_current_silent( 'blog' );
  988. populate_options();
  989. populate_roles();
  990. $wp_roles->_init();
  991. // fix url.
  992. update_option('siteurl', $url);
  993. update_option('home', $url);
  994. update_option('fileupload_url', $url . "files" );
  995. update_option('upload_path', UPLOADBLOGSDIR . "/$blog_id/files");
  996. update_option('blogname', stripslashes( $blog_title ) );
  997. update_option('admin_email', '');
  998. $wpdb->update( $wpdb->options, array('option_value' => ''), array('option_name' => 'admin_email') );
  999. // remove all perms
  1000. $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE meta_key = %s", $table_prefix.'user_level') );
  1001. $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE meta_key = %s", $table_prefix.'capabilities') );
  1002. $wpdb->suppress_errors( false );
  1003. }
  1004. /**
  1005. * Set blog defaults.
  1006. *
  1007. * This function creates a row in the wp_blogs table.
  1008. *
  1009. * @since MU
  1010. * @deprecated MU
  1011. * @deprecated Use wp_install_defaults()
  1012. * @uses wp_install_defaults()
  1013. *
  1014. * @param int $blog_id Ignored in this function.
  1015. * @param int $user_id
  1016. */
  1017. function install_blog_defaults($blog_id, $user_id) {
  1018. global $wpdb;
  1019. require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
  1020. $wpdb->suppress_errors();
  1021. wp_install_defaults($user_id);
  1022. $wpdb->suppress_errors( false );
  1023. }
  1024. /**
  1025. * Notify a user that her blog activation has been successful.
  1026. *
  1027. * Filter 'wpmu_welcome_notification' to disable or bypass.
  1028. *
  1029. * Filter 'update_welcome_email' and 'update_welcome_subject' to
  1030. * modify the content and subject line of the notification email.
  1031. *
  1032. * @since MU
  1033. *
  1034. * @param int $blog_id
  1035. * @param int $user_id
  1036. * @param string $password
  1037. * @param string $title The new blog's title
  1038. * @param array $meta Optional. Not used in the default function, but is passed along to hooks for customization.
  1039. * @return bool
  1040. */
  1041. function wpmu_welcome_notification($blog_id, $user_id, $password, $title, $meta = '') {
  1042. global $current_site;
  1043. if ( !apply_filters('wpmu_welcome_notification', $blog_id, $user_id, $password, $title, $meta) )
  1044. return false;
  1045. $welcome_email = stripslashes( get_site_option( 'welcome_email' ) );
  1046. if ( $welcome_email == false )
  1047. $welcome_email = stripslashes( __( 'Dear User,
  1048. Your new SITE_NAME site has been successfully set up at:
  1049. BLOG_URL
  1050. You can log in to the administrator account with the following information:
  1051. Username: USERNAME
  1052. Password: PASSWORD
  1053. Log in here: BLOG_URLwp-login.php
  1054. We hope you enjoy your new site. Thanks!
  1055. --The Team @ SITE_NAME' ) );
  1056. $url = get_blogaddress_by_id($blog_id);
  1057. $user = new WP_User($user_id);
  1058. $welcome_email = str_replace( 'SITE_NAME', $current_site->site_name, $welcome_email );
  1059. $welcome_email = str_replace( 'BLOG_TITLE', $title, $welcome_email );
  1060. $welcome_email = str_replace( 'BLOG_URL', $url, $welcome_email );
  1061. $welcome_email = str_replace( 'USERNAME', $user->user_login, $welcome_email );
  1062. $welcome_email = str_replace( 'PASSWORD', $password, $welcome_email );
  1063. $welcome_email = apply_filters( 'update_welcome_email', $welcome_email, $blog_id, $user_id, $password, $title, $meta);
  1064. $admin_email = get_site_option( 'admin_email' );
  1065. if ( $admin_email == '' )
  1066. $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
  1067. $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
  1068. $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
  1069. $message = $welcome_email;
  1070. if ( empty( $current_site->site_name ) )
  1071. $current_site->site_name = 'WordPress';
  1072. $subject = apply_filters( 'update_welcome_subject', sprintf(__('New %1$s Site: %2$s'), $current_site->site_name, stripslashes( $title ) ) );
  1073. wp_mail($user->user_email, $subject, $message, $message_headers);
  1074. return true;
  1075. }
  1076. /**
  1077. * Notify a user that her account activation has been successful.
  1078. *
  1079. * Filter 'wpmu_welcome_user_notification' to disable or bypass.
  1080. *
  1081. * Filter 'update_welcome_user_email' and 'update_welcome_user_subject' to
  1082. * modify the content and subject line of the notification email.
  1083. *
  1084. * @since MU
  1085. *
  1086. * @param int $user_id
  1087. * @param string $password
  1088. * @param array $meta Optional. Not used in the default function, but is passed along to hooks for customization.
  1089. * @return bool
  1090. */
  1091. function wpmu_welcome_user_notification($user_id, $password, $meta = '') {
  1092. global $current_site;
  1093. if ( !apply_filters('wpmu_welcome_user_notification', $user_id, $password, $meta) )
  1094. return false;
  1095. $welcome_email = get_site_option( 'welcome_user_email' );
  1096. $user = new WP_User($user_id);
  1097. $welcome_email = apply_filters( 'update_welcome_user_email', $welcome_email, $user_id, $password, $meta);
  1098. $welcome_email = str_replace( 'SITE_NAME', $current_site->site_name, $welcome_email );
  1099. $welcome_email = str_replace( 'USERNAME', $user->user_login, $welcome_email );
  1100. $welcome_email = str_replace( 'PASSWORD', $password, $welcome_email );
  1101. $welcome_email = str_replace( 'LOGINLINK', wp_login_url(), $welcome_email );
  1102. $admin_email = get_site_option( 'admin_email' );
  1103. if ( $admin_email == '' )
  1104. $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
  1105. $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
  1106. $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
  1107. $message = $welcome_email;
  1108. if ( empty( $current_site->site_name ) )
  1109. $current_site->site_name = 'WordPress';
  1110. $subject = apply_filters( 'update_welcome_user_subject', sprintf(__('New %1$s User: %2$s'), $current_site->site_name, $user->user_login) );
  1111. wp_mail($user->user_email, $subject, $message, $message_headers);
  1112. return true;
  1113. }
  1114. /**
  1115. * Get the current site info.
  1116. *
  1117. * Returns an object containing the ID, domain, path, and site_name
  1118. * of the site being viewed.
  1119. *
  1120. * @since MU
  1121. *
  1122. * @return object
  1123. */
  1124. function get_current_site() {
  1125. global $current_site;
  1126. return $current_site;
  1127. }
  1128. /**
  1129. * Get a numeric user ID from either an email address or a login.
  1130. *
  1131. * @since MU
  1132. * @uses is_email()
  1133. *
  1134. * @param string $string
  1135. * @return int
  1136. */
  1137. function get_user_id_from_string( $string ) {
  1138. $user_id = 0;
  1139. if ( is_email( $string ) ) {
  1140. $user = get_user_by('email', $string);
  1141. if ( $user )
  1142. $user_id = $user->ID;
  1143. } elseif ( is_numeric( $string ) ) {
  1144. $user_id = $string;
  1145. } else {
  1146. $user = get_user_by('login', $string);
  1147. if ( $user )
  1148. $user_id = $user->ID;
  1149. }
  1150. return $user_id;
  1151. }
  1152. /**
  1153. * Get a user's most recent post.
  1154. *
  1155. * Walks through each of a user's blogs to find the post with
  1156. * the most recent post_date_gmt.
  1157. *
  1158. * @since MU
  1159. * @uses get_blogs_of_user()
  1160. *
  1161. * @param int $user_id
  1162. * @return array Contains the blog_id, post_id, post_date_gmt, and post_gmt_ts
  1163. */
  1164. function get_most_recent_post_of_user( $user_id ) {
  1165. global $wpdb;
  1166. $user_blogs = get_blogs_of_user( (int) $user_id );
  1167. $most_recent_post = array();
  1168. // Walk through each blog and get the most recent post
  1169. // published by $user_id
  1170. foreach ( (array) $user_blogs as $blog ) {
  1171. $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);
  1172. // Make sure we found a post
  1173. if ( isset($recent_post['ID']) ) {
  1174. $post_gmt_ts = strtotime($recent_post['post_date_gmt']);
  1175. // If this is the first post checked or if this post is
  1176. // newer than the current recent post, make it the new
  1177. // most recent post.
  1178. if ( !isset($most_recent_post['post_gmt_ts']) || ( $post_gmt_ts > $most_recent_post['post_gmt_ts'] ) ) {
  1179. $most_recent_post = array(
  1180. 'blog_id' => $blog->userblog_id,
  1181. 'post_id' => $recent_post['ID'],
  1182. 'post_date_gmt' => $recent_post['post_date_gmt'],
  1183. 'post_gmt_ts' => $post_gmt_ts
  1184. );
  1185. }
  1186. }
  1187. }
  1188. return $most_recent_post;
  1189. }
  1190. // Misc functions
  1191. /**
  1192. * Get the size of a directory.
  1193. *
  1194. * A helper function that is used primarily to check whether
  1195. * a blog has exceeded its allowed upload space.
  1196. *
  1197. * @since MU
  1198. * @uses recurse_dirsize()
  1199. *
  1200. * @param string $directory
  1201. * @return int
  1202. */
  1203. function get_dirsize( $directory ) {
  1204. $dirsize = get_transient( 'dirsize_cache' );
  1205. if ( is_array( $dirsize ) && isset( $dirsize[ $directory ][ 'size' ] ) )
  1206. return $dirsize[ $directory ][ 'size' ];
  1207. if ( false == is_array( $dirsize ) )
  1208. $dirsize = array();
  1209. $dirsize[ $directory ][ 'size' ] = recurse_dirsize( $directory );
  1210. set_transient( 'dirsize_cache', $dirsize, 3600 );
  1211. return $dirsize[ $directory ][ 'size' ];
  1212. }
  1213. /**
  1214. * Get the size of a directory recursively.
  1215. *
  1216. * Used by get_dirsize() to get a directory's size when it contains
  1217. * other directories.
  1218. *
  1219. * @since MU
  1220. *
  1221. * @param string $directory
  1222. * @return int
  1223. */
  1224. function recurse_dirsize( $directory ) {
  1225. $size = 0;
  1226. $directory = untrailingslashit( $directory );
  1227. if ( !file_exists($directory) || !is_dir( $directory ) || !is_readable( $directory ) )
  1228. return false;
  1229. if ($handle = opendir($directory)) {
  1230. while(($file = readdir($handle)) !== false) {
  1231. $path = $directory.'/'.$file;
  1232. if ($file != '.' && $file != '..') {
  1233. if (is_file($path)) {
  1234. $size += filesize($path);
  1235. } elseif (is_dir($path)) {
  1236. $handlesize = recurse_dirsize($path);
  1237. if ($handlesize > 0)
  1238. $size += $handlesize;
  1239. }
  1240. }
  1241. }
  1242. closedir($handle);
  1243. }
  1244. return $size;
  1245. }
  1246. /**
  1247. * Check whether a blog has used its allotted upload space.
  1248. *
  1249. * Used by get_dirsize() to get a directory's size when it contains
  1250. * other directories.
  1251. *
  1252. * @since MU
  1253. * @uses get_dirsize()
  1254. *
  1255. * @param bool $echo Optional. If $echo is set and the quota is exceeded, a warning message is echoed. Default is true.
  1256. * @return int
  1257. */
  1258. function upload_is_user_over_quota( $echo = true ) {
  1259. if ( get_site_option( 'upload_space_check_disabled' ) )
  1260. return false;
  1261. $spaceAllowed = get_space_allowed();
  1262. if ( empty( $spaceAllowed ) || !is_numeric( $spaceAllowed ) )
  1263. $spaceAllowed = 10; // Default space allowed is 10 MB
  1264. $size = get_dirsize( BLOGUPLOADDIR ) / 1024 / 1024;
  1265. if ( ($spaceAllowed-$size) < 0 ) {
  1266. if ( $echo )
  1267. _e( 'Sorry, you have used your space allocation. Please delete some files to upload more files.' ); // No space left
  1268. return true;
  1269. } else {
  1270. return false;
  1271. }
  1272. }
  1273. /**
  1274. * Check an array of MIME types against a whitelist.
  1275. *
  1276. * WordPress ships with a set of allowed upload filetypes,
  1277. * which is defined in wp-includes/functions.php in
  1278. * get_allowed_mime_types(). This function is used to filter
  1279. * that list against the filetype whitelist provided by Multisite
  1280. * Super Admins at wp-admin/network/settings.php.
  1281. *
  1282. * @since MU
  1283. *
  1284. * @param array $mimes
  1285. * @return array
  1286. */
  1287. function check_upload_mimes( $mimes ) {
  1288. $site_exts = explode( ' ', get_site_option( 'upload_filetypes' ) );
  1289. foreach ( $site_exts as $ext ) {
  1290. foreach ( $mimes as $ext_pattern => $mime ) {
  1291. if ( $ext != '' && strpos( $ext_pattern, $ext ) !== false )
  1292. $site_mimes[$ext_pattern] = $mime;
  1293. }
  1294. }
  1295. return $site_mimes;
  1296. }
  1297. /**
  1298. * Update a blog's post count.
  1299. *
  1300. * WordPress MS stores a blog's post count as an option so as
  1301. * to avoid extraneous COUNTs when a blog's details are fetched
  1302. * with get_blog_details(). This function is called when pos…

Large files files are truncated, but you can click here to view the full file