PageRenderTime 64ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/blog/wp-includes/wpmu-functions.php

https://bitbucket.org/malathisuji/nrcfoss-au-tamil-computing-activities
PHP | 2407 lines | 1871 code | 418 blank | 118 comment | 517 complexity | 00b864d7e0aa2473ec11c92de45ec13b MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1

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

  1. <?php
  2. /*
  3. Helper functions for WPMU
  4. */
  5. function load_muplugin_textdomain($domain, $path = false) {
  6. $locale = get_locale();
  7. if ( empty($locale) )
  8. $locale = 'en_US';
  9. if ( false === $path )
  10. $path = WPMU_PLUGIN_DIR;
  11. $mofile = WPMU_PLUGIN_DIR . "/$domain-$locale.mo";
  12. load_textdomain($domain, $mofile);
  13. }
  14. function wpmu_update_blogs_date() {
  15. global $wpdb;
  16. $wpdb->update( $wpdb->blogs, array('last_updated' => current_time('mysql', true)), array('blog_id' => $wpdb->blogid) );
  17. refresh_blog_details( $wpdb->blogid );
  18. do_action( 'wpmu_blog_updated', $wpdb->blogid );
  19. }
  20. function get_blogaddress_by_id( $blog_id ) {
  21. $bloginfo = get_blog_details( (int) $blog_id, false ); // only get bare details!
  22. return clean_url("http://" . $bloginfo->domain . $bloginfo->path);
  23. }
  24. function get_blogaddress_by_name( $blogname ) {
  25. global $current_site;
  26. if( defined( "VHOST" ) && constant( "VHOST" ) == 'yes' ) {
  27. if( $blogname == 'main' )
  28. $blogname = 'www';
  29. return clean_url( "http://" . $blogname . "." . $current_site->domain . $current_site->path );
  30. } else {
  31. return clean_url( "http://" . $current_site->domain . $current_site->path . $blogname . '/' );
  32. }
  33. }
  34. function get_blogaddress_by_domain( $domain, $path ){
  35. if( defined( "VHOST" ) && constant( "VHOST" ) == 'yes' ) {
  36. $url = "http://".$domain.$path;
  37. } else {
  38. if( $domain != $_SERVER['HTTP_HOST'] ) {
  39. $blogname = substr( $domain, 0, strpos( $domain, '.' ) );
  40. if( $blogname != 'www.' ) {
  41. $url = 'http://' . substr( $domain, strpos( $domain, '.' ) + 1 ) . $path . $blogname . '/';
  42. } else { // we're installing the main blog
  43. $url = 'http://' . substr( $domain, strpos( $domain, '.' ) + 1 ) . $path;
  44. }
  45. } else { // main blog
  46. $url = 'http://' . $domain . $path;
  47. }
  48. }
  49. return clean_url($url);
  50. }
  51. function get_sitestats() {
  52. global $wpdb;
  53. $stats['blogs'] = get_blog_count();
  54. $count_ts = get_site_option( "get_user_count_ts" );
  55. if( time() - $count_ts > 3600 ) {
  56. $count = $wpdb->get_var( "SELECT COUNT(ID) FROM {$wpdb->users}" );
  57. update_site_option( "user_count", $count );
  58. update_site_option( "user_count_ts", time() );
  59. } else {
  60. $count = get_site_option( "user_count" );
  61. }
  62. $stats['users'] = $count;
  63. return $stats;
  64. }
  65. function get_admin_users_for_domain( $sitedomain = '', $path = '' ) {
  66. global $wpdb;
  67. if( $sitedomain == '' ) {
  68. $site_id = $wpdb->siteid;
  69. } else {
  70. $site_id = $wpdb->get_var( $wpdb->prepare("SELECT id FROM $wpdb->site WHERE domain = %s AND path = %s", $sitedomain, $path) );
  71. }
  72. if( $site_id != false ) {
  73. 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 );
  74. }
  75. return false;
  76. }
  77. function get_user_details( $username ) {
  78. global $wpdb;
  79. return $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->users WHERE user_login = %s", $username) );
  80. }
  81. function is_main_blog() {
  82. global $current_blog, $current_site;
  83. if( $current_blog->domain == $current_site->domain && $current_blog->path == $current_site->path )
  84. return true;
  85. return false;
  86. }
  87. function get_id_from_blogname( $name ) {
  88. global $wpdb, $current_site;
  89. $blog_id = wp_cache_get( "get_id_from_blogname_" . $name, 'blog-details' );
  90. if( $blog_id )
  91. return $blog_id;
  92. if( constant( 'VHOST' ) == 'yes' ) {
  93. $domain = $name . '.' . $current_site->domain;
  94. $path = $current_site->path;
  95. } else {
  96. $domain = $current_site->domain;
  97. $path = $current_site->path . $name . '/';
  98. }
  99. $blog_id = $wpdb->get_var( $wpdb->prepare("SELECT blog_id FROM {$wpdb->blogs} WHERE domain = %s AND path = %s", $domain, $path) );
  100. wp_cache_set( 'get_id_from_blogname_' . $name, $blog_id, 'blog-details' );
  101. return $blog_id;
  102. }
  103. function get_blog_details( $id, $getall = true ) {
  104. global $wpdb;
  105. if( !is_numeric( $id ) ) {
  106. $id = get_id_from_blogname( $id );
  107. }
  108. $all = $getall == true ? '' : 'short';
  109. $details = wp_cache_get( $id . $all, 'blog-details' );
  110. if ( $details ) {
  111. if ( $details == -1 )
  112. return false;
  113. elseif ( !is_object($details) ) // Clear old pre-serialized objects. Cache clients do better with that.
  114. wp_cache_delete( $id . $all, 'blog-details' );
  115. else
  116. return $details;
  117. }
  118. $details = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->blogs WHERE blog_id = %d /* get_blog_details */", $id) );
  119. if ( !$details ) {
  120. wp_cache_set( $id . $all, -1, 'blog-details' );
  121. return false;
  122. }
  123. if ( !$getall ) {
  124. wp_cache_set( $id . $all, $details, 'blog-details' );
  125. return $details;
  126. }
  127. $wpdb->suppress_errors();
  128. switch_to_blog( $id );
  129. $details->blogname = get_option( 'blogname' );
  130. $details->siteurl = get_option( 'siteurl' );
  131. $details->post_count = get_option( 'post_count' );
  132. restore_current_blog();
  133. $wpdb->suppress_errors( false );
  134. $details = apply_filters('blog_details', $details);
  135. wp_cache_set( $id . $all, $details, 'blog-details' );
  136. $key = md5( $details->domain . $details->path );
  137. wp_cache_set( $key, $details, 'blog-lookup' );
  138. return $details;
  139. }
  140. function refresh_blog_details( $id ) {
  141. $id = (int) $id;
  142. $details = get_blog_details( $id, false );
  143. wp_cache_delete( $id , 'blog-details' );
  144. wp_cache_delete( $id . 'short' , 'blog-details' );
  145. wp_cache_delete( md5( $details->domain . $details->path ) , 'blog-lookup' );
  146. wp_cache_delete( 'current_blog_' . $details->domain, 'site-options' );
  147. wp_cache_delete( 'current_blog_' . $details->domain . $details->path, 'site-options' );
  148. }
  149. function get_current_user_id() {
  150. global $current_user;
  151. return $current_user->ID;
  152. }
  153. function is_site_admin( $user_login = false ) {
  154. global $current_user;
  155. if ( !$current_user && !$user_login )
  156. return false;
  157. if ( $user_login ) {
  158. $user_login = sanitize_user( $user_login );
  159. } elseif( isset( $current_user->user_login ) ) {
  160. $user_login = $current_user->user_login;
  161. } else {
  162. return false;
  163. }
  164. $site_admins = get_site_option( 'site_admins', array('admin') );
  165. if( is_array( $site_admins ) && in_array( $user_login, $site_admins ) )
  166. return true;
  167. return false;
  168. }
  169. /**
  170. * Retrieve option value based on setting name and blog_id.
  171. *
  172. * If the option does not exist or does not have a value, then the return value
  173. * will be false. This is useful to check whether you need to install an option
  174. * and is commonly used during installation of plugin options and to test
  175. * whether upgrading is required.
  176. *
  177. * There is a filter called 'blog_option_$option' with the $option being
  178. * replaced with the option name. The filter takes two parameters. $value and
  179. * $blog_id. It returns $value.
  180. * The 'option_$option' filter in get_option() is not called.
  181. *
  182. * @since NA
  183. * @package WordPress MU
  184. * @subpackage Option
  185. * @uses apply_filters() Calls 'blog_option_$optionname' with the option name value.
  186. *
  187. * @param int $blog_id is the id of the blog.
  188. * @param string $setting Name of option to retrieve. Should already be SQL-escaped
  189. * @param string $default (optional) Default value returned if option not found.
  190. * @return mixed Value set for the option.
  191. */
  192. function get_blog_option( $blog_id, $setting, $default = false ) {
  193. global $wpdb;
  194. $key = $blog_id."-".$setting."-blog_option";
  195. $value = wp_cache_get( $key, "site-options" );
  196. if ( $value == null ) {
  197. $blog_prefix = $wpdb->get_blog_prefix( $blog_id );
  198. $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$blog_prefix}options WHERE option_name = %s", $setting ) );
  199. if ( is_object( $row ) ) { // Has to be get_row instead of get_var because of funkiness with 0, false, null values
  200. $value = $row->option_value;
  201. if ( $value == false ) {
  202. wp_cache_set( $key, 'falsevalue', 'site-options' );
  203. } else {
  204. wp_cache_set( $key, $value, 'site-options' );
  205. }
  206. } else { // option does not exist, so we must cache its non-existence
  207. wp_cache_set( $key, 'noop', 'site-options' );
  208. $value = $default;
  209. }
  210. } elseif( $value == 'noop' ) {
  211. $value = $default;
  212. } elseif( $value == 'falsevalue' ) {
  213. $value = false;
  214. }
  215. // If home is not set use siteurl.
  216. if ( 'home' == $setting && '' == $value )
  217. return get_blog_option( $blog_id, 'siteurl' );
  218. if ( 'siteurl' == $setting || 'home' == $setting || 'category_base' == $setting )
  219. $value = preg_replace( '|/+$|', '', $value );
  220. if (! @unserialize( $value ) )
  221. $value = stripslashes( $value );
  222. return apply_filters( 'blog_option_' . $setting, maybe_unserialize( $value ), $blog_id );
  223. }
  224. function add_blog_option( $id, $key, $value ) {
  225. $id = (int) $id;
  226. switch_to_blog($id);
  227. add_option( $key, $value );
  228. restore_current_blog();
  229. wp_cache_set( $id."-".$key."-blog_option", $value, 'site-options' );
  230. }
  231. function delete_blog_option( $id, $key ) {
  232. $id = (int) $id;
  233. switch_to_blog($id);
  234. delete_option( $key );
  235. restore_current_blog();
  236. wp_cache_set( $id."-".$key."-blog_option", '', 'site-options' );
  237. }
  238. function update_blog_option( $id, $key, $value, $refresh = true ) {
  239. $id = (int) $id;
  240. switch_to_blog($id);
  241. update_option( $key, $value );
  242. restore_current_blog();
  243. if( $refresh == true )
  244. refresh_blog_details( $id );
  245. wp_cache_set( $id."-".$key."-blog_option", $value, 'site-options');
  246. }
  247. function switch_to_blog( $new_blog ) {
  248. global $wpdb, $table_prefix, $blog_id, $switched, $switched_stack, $wp_roles, $current_user, $wp_object_cache;
  249. if ( empty($new_blog) )
  250. $new_blog = $blog_id;
  251. if ( empty($switched_stack) )
  252. $switched_stack = array();
  253. $switched_stack[] = $blog_id;
  254. /* If we're switching to the same blog id that we're on,
  255. * set the right vars, do the associated actions, but skip
  256. * the extra unnecessary work */
  257. if ( $blog_id == $new_blog ) {
  258. do_action( 'switch_blog', $blog_id, $blog_id );
  259. $switched = true;
  260. return true;
  261. }
  262. $wpdb->set_blog_id($new_blog);
  263. $table_prefix = $wpdb->prefix;
  264. $prev_blog_id = $blog_id;
  265. $blog_id = $new_blog;
  266. if( is_object( $wp_roles ) ) {
  267. $wpdb->suppress_errors();
  268. if ( method_exists( $wp_roles ,'_init' ) ) {
  269. $wp_roles->_init();
  270. } elseif( method_exists( $wp_roles, '__construct' ) ) {
  271. $wp_roles->__construct();
  272. }
  273. $wpdb->suppress_errors( false );
  274. }
  275. if ( is_object( $current_user ) )
  276. $current_user->_init_caps();
  277. if ( is_object( $wp_object_cache ) ) {
  278. $global_groups = $wp_object_cache->global_groups;
  279. } else {
  280. $global_groups = false;
  281. }
  282. wp_cache_init();
  283. if ( function_exists('wp_cache_add_global_groups') ) {
  284. if ( is_array( $global_groups ) ) {
  285. wp_cache_add_global_groups( $global_groups );
  286. } else {
  287. wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'site-transient', 'global-posts' ) );
  288. }
  289. wp_cache_add_non_persistent_groups(array( 'comment', 'counts', 'plugins' ));
  290. }
  291. do_action('switch_blog', $blog_id, $prev_blog_id);
  292. $switched = true;
  293. return true;
  294. }
  295. function restore_current_blog() {
  296. global $table_prefix, $wpdb, $blog_id, $switched, $switched_stack, $wp_roles, $current_user, $wp_object_cache;
  297. if ( !$switched )
  298. return false;
  299. if ( !is_array( $switched_stack ) )
  300. return false;
  301. $blog = array_pop( $switched_stack );
  302. if ( $blog_id == $blog ) {
  303. do_action( 'switch_blog', $blog, $blog );
  304. /* If we still have items in the switched stack, consider ourselves still 'switched' */
  305. $switched = ( is_array( $switched_stack ) && count( $switched_stack ) > 0 );
  306. return true;
  307. }
  308. $wpdb->set_blog_id($blog);
  309. $prev_blog_id = $blog_id;
  310. $blog_id = $blog;
  311. $table_prefix = $wpdb->prefix;
  312. if( is_object( $wp_roles ) ) {
  313. $wpdb->suppress_errors();
  314. if ( method_exists( $wp_roles ,'_init' ) ) {
  315. $wp_roles->_init();
  316. } elseif( method_exists( $wp_roles, '__construct' ) ) {
  317. $wp_roles->__construct();
  318. }
  319. $wpdb->suppress_errors( false );
  320. }
  321. if ( is_object( $current_user ) )
  322. $current_user->_init_caps();
  323. if ( is_object( $wp_object_cache ) ) {
  324. $global_groups = $wp_object_cache->global_groups;
  325. } else {
  326. $global_groups = false;
  327. }
  328. wp_cache_init();
  329. if ( function_exists('wp_cache_add_global_groups') ) {
  330. if ( is_array( $global_groups ) ) {
  331. wp_cache_add_global_groups( $global_groups );
  332. } else {
  333. wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'site-transient' ) );
  334. }
  335. wp_cache_add_non_persistent_groups(array( 'comment', 'counts', 'plugins' ));
  336. }
  337. do_action('switch_blog', $blog_id, $prev_blog_id);
  338. /* If we still have items in the switched stack, consider ourselves still 'switched' */
  339. $switched = ( is_array( $switched_stack ) && count( $switched_stack ) > 0 );
  340. return true;
  341. }
  342. function get_blogs_of_user( $id, $all = false ) {
  343. global $wpdb;
  344. $cache_suffix = $all ? '_all' : '_short';
  345. $return = wp_cache_get( 'blogs_of_user_' . $id . $cache_suffix, 'users' );
  346. if ( $return ) {
  347. return apply_filters( 'get_blogs_of_user', $return, $id, $all );
  348. }
  349. $user = get_userdata( (int) $id );
  350. if ( !$user )
  351. return false;
  352. $blogs = $match = array();
  353. foreach ( (array) $user as $key => $value ) {
  354. if ( false !== strpos( $key, '_capabilities') && 0 === strpos( $key, $wpdb->base_prefix ) && preg_match( '/' . $wpdb->base_prefix . '(\d+)_capabilities/', $key, $match ) ) {
  355. $blog = get_blog_details( $match[1] );
  356. if ( $blog && isset( $blog->domain ) && ( $all == true || $all == false && ( $blog->archived == 0 && $blog->spam == 0 && $blog->deleted == 0 ) ) ) {
  357. $blogs[$match[1]]->userblog_id = $match[1];
  358. $blogs[$match[1]]->blogname = $blog->blogname;
  359. $blogs[$match[1]]->domain = $blog->domain;
  360. $blogs[$match[1]]->path = $blog->path;
  361. $blogs[$match[1]]->site_id = $blog->site_id;
  362. $blogs[$match[1]]->siteurl = $blog->siteurl;
  363. }
  364. }
  365. }
  366. wp_cache_add( 'blogs_of_user_' . $id . $cache_suffix, $blogs, 'users', 5 );
  367. return apply_filters( 'get_blogs_of_user', $blogs, $id, $all );
  368. }
  369. function get_active_blog_for_user( $user_id ) { // get an active blog for user - either primary blog or from blogs list
  370. global $wpdb;
  371. $blogs = get_blogs_of_user( $user_id );
  372. if ( empty( $blogs ) ) {
  373. $details = get_dashboard_blog();
  374. add_user_to_blog( $details->blog_id, $user_id, 'subscriber' );
  375. update_usermeta( $user_id, 'primary_blog', $details->blog_id );
  376. wp_cache_delete( $user_id, 'users' );
  377. return $details;
  378. }
  379. $primary_blog = get_usermeta( $user_id, "primary_blog" );
  380. $details = get_dashboard_blog();
  381. if ( $primary_blog ) {
  382. $blogs = get_blogs_of_user( $user_id );
  383. if ( isset( $blogs[ $primary_blog ] ) == false ) {
  384. add_user_to_blog( $details->blog_id, $user_id, 'subscriber' );
  385. update_usermeta( $user_id, 'primary_blog', $details->blog_id );
  386. wp_cache_delete( $user_id, 'users' );
  387. } else {
  388. $details = get_blog_details( $primary_blog );
  389. }
  390. } else {
  391. add_user_to_blog( $details->blog_id, $user_id, 'subscriber' ); // Add subscriber permission for dashboard blog
  392. update_usermeta( $user_id, 'primary_blog', $details->blog_id );
  393. }
  394. if ( ( is_object( $details ) == false ) || ( is_object( $details ) && $details->archived == 1 || $details->spam == 1 || $details->deleted == 1 ) ) {
  395. $blogs = get_blogs_of_user( $user_id, true ); // if a user's primary blog is shut down, check their other blogs.
  396. $ret = false;
  397. if ( is_array( $blogs ) && count( $blogs ) > 0 ) {
  398. foreach( (array) $blogs as $blog_id => $blog ) {
  399. if ( $blog->site_id != $wpdb->siteid )
  400. continue;
  401. $details = get_blog_details( $blog_id );
  402. if ( is_object( $details ) && $details->archived == 0 && $details->spam == 0 && $details->deleted == 0 ) {
  403. $ret = $blog;
  404. $changed = false;
  405. if ( get_usermeta( $user_id , 'primary_blog' ) != $blog_id ) {
  406. update_usermeta( $user_id, 'primary_blog', $blog_id );
  407. $changed = true;
  408. }
  409. if ( !get_usermeta($user_id , 'source_domain') ) {
  410. update_usermeta( $user_id, 'source_domain', $blog->domain );
  411. $changed = true;
  412. }
  413. if ( $changed )
  414. wp_cache_delete( $user_id, 'users' );
  415. break;
  416. }
  417. }
  418. } else {
  419. // Should never get here
  420. $dashboard_blog = get_dashboard_blog();
  421. add_user_to_blog( $dashboard_blog->blog_id, $user_id, 'subscriber' ); // Add subscriber permission for dashboard blog
  422. update_usermeta( $user_id, 'primary_blog', $dashboard_blog->blog_id );
  423. return $dashboard_blog;
  424. }
  425. return $ret;
  426. } else {
  427. return $details;
  428. }
  429. }
  430. function is_user_member_of_blog( $user_id, $blog_id = 0 ) {
  431. $user_id = (int) $user_id;
  432. $blog_id = (int) $blog_id;
  433. if( $blog_id == 0 ) {
  434. global $wpdb;
  435. $blog_id = $wpdb->blogid;
  436. }
  437. $blogs = get_blogs_of_user( $user_id );
  438. if( is_array( $blogs ) ) {
  439. return array_key_exists( $blog_id, $blogs );
  440. } else {
  441. return false;
  442. }
  443. }
  444. function is_archived( $id ) {
  445. return get_blog_status($id, 'archived');
  446. }
  447. function update_archived( $id, $archived ) {
  448. update_blog_status($id, 'archived', $archived);
  449. return $archived;
  450. }
  451. function update_blog_status( $id, $pref, $value, $refresh = 1 ) {
  452. global $wpdb;
  453. if ( !in_array( $pref, array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id') ) )
  454. return $value;
  455. $wpdb->update( $wpdb->blogs, array($pref => $value, 'last_updated' => current_time('mysql', true)), array('blog_id' => $id) );
  456. if( $refresh == 1 )
  457. refresh_blog_details($id);
  458. if( $pref == 'spam' ) {
  459. if( $value == 1 ) {
  460. do_action( "make_spam_blog", $id );
  461. } else {
  462. do_action( "make_ham_blog", $id );
  463. }
  464. }
  465. return $value;
  466. }
  467. function get_blog_status( $id, $pref ) {
  468. global $wpdb;
  469. $details = get_blog_details( $id, false );
  470. if( $details ) {
  471. return $details->$pref;
  472. }
  473. return $wpdb->get_var( $wpdb->prepare("SELECT $pref FROM {$wpdb->blogs} WHERE blog_id = %d", $id) );
  474. }
  475. function get_last_updated( $deprecated = '', $start = 0, $quantity = 40 ) {
  476. global $wpdb;
  477. return $wpdb->get_results( $wpdb->prepare("SELECT blog_id, domain, path FROM $wpdb->blogs WHERE site_id = %d AND public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0' AND last_updated != '0000-00-00 00:00:00' ORDER BY last_updated DESC limit %d, %d", $wpdb->siteid, $start, $quantity ) , ARRAY_A );
  478. }
  479. function get_most_active_blogs( $num = 10, $display = true ) {
  480. $most_active = get_site_option( "most_active" );
  481. $update = false;
  482. if( is_array( $most_active ) ) {
  483. if( ( $most_active['time'] + 60 ) < time() ) { // cache for 60 seconds.
  484. $update = true;
  485. }
  486. } else {
  487. $update = true;
  488. }
  489. if( $update == true ) {
  490. unset( $most_active );
  491. $blogs = get_blog_list( 0, 'all', false ); // $blog_id -> $details
  492. if( is_array( $blogs ) ) {
  493. reset( $blogs );
  494. foreach ( (array) $blogs as $key => $details ) {
  495. $most_active[ $details['blog_id'] ] = $details['postcount'];
  496. $blog_list[ $details['blog_id'] ] = $details; // array_slice() removes keys!!
  497. }
  498. arsort( $most_active );
  499. reset( $most_active );
  500. foreach ( (array) $most_active as $key => $details ) {
  501. $t[ $key ] = $blog_list[ $key ];
  502. }
  503. unset( $most_active );
  504. $most_active = $t;
  505. }
  506. update_site_option( "most_active", $most_active );
  507. }
  508. if( $display == true ) {
  509. if( is_array( $most_active ) ) {
  510. reset( $most_active );
  511. foreach ( (array) $most_active as $key => $details ) {
  512. $url = clean_url("http://" . $details['domain'] . $details['path']);
  513. echo "<li>" . $details['postcount'] . " <a href='$url'>$url</a></li>";
  514. }
  515. }
  516. }
  517. return array_slice( $most_active, 0, $num );
  518. }
  519. function get_blog_list( $start = 0, $num = 10, $deprecated = '' ) {
  520. global $wpdb;
  521. $blogs = get_site_option( "blog_list" );
  522. $update = false;
  523. if( is_array( $blogs ) ) {
  524. if( ( $blogs['time'] + 60 ) < time() ) { // cache for 60 seconds.
  525. $update = true;
  526. }
  527. } else {
  528. $update = true;
  529. }
  530. if( $update == true ) {
  531. unset( $blogs );
  532. $blogs = $wpdb->get_results( $wpdb->prepare("SELECT blog_id, domain, path FROM $wpdb->blogs WHERE site_id = %d AND public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0' ORDER BY registered DESC", $wpdb->siteid), ARRAY_A );
  533. foreach ( (array) $blogs as $details ) {
  534. $blog_list[ $details['blog_id'] ] = $details;
  535. $blog_list[ $details['blog_id'] ]['postcount'] = $wpdb->get_var( "SELECT COUNT(ID) FROM " . $wpdb->base_prefix . $details['blog_id'] . "_posts WHERE post_status='publish' AND post_type='post'" );
  536. }
  537. unset( $blogs );
  538. $blogs = $blog_list;
  539. update_site_option( "blog_list", $blogs );
  540. }
  541. if( false == is_array( $blogs ) )
  542. return array();
  543. if( $num == 'all' ) {
  544. return array_slice( $blogs, $start, count( $blogs ) );
  545. } else {
  546. return array_slice( $blogs, $start, $num );
  547. }
  548. }
  549. function get_user_count() {
  550. global $wpdb;
  551. $count_ts = get_site_option( "user_count_ts" );
  552. if( time() - $count_ts > 3600 ) {
  553. $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(ID) as c FROM $wpdb->users WHERE spam = '0' AND deleted = '0'") );
  554. update_site_option( "user_count", $count );
  555. update_site_option( "user_count_ts", time() );
  556. }
  557. $count = get_site_option( "user_count" );
  558. return $count;
  559. }
  560. function get_blog_count( $id = 0 ) {
  561. global $wpdb;
  562. if( $id == 0 )
  563. $id = $wpdb->siteid;
  564. $count_ts = get_site_option( "blog_count_ts" );
  565. if( time() - $count_ts > 3600 ) {
  566. $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) );
  567. update_site_option( "blog_count", $count );
  568. update_site_option( "blog_count_ts", time() );
  569. }
  570. $count = get_site_option( "blog_count" );
  571. return $count;
  572. }
  573. function get_blog_post( $blog_id, $post_id ) {
  574. global $wpdb;
  575. $key = $blog_id . "-" . $post_id;
  576. $post = wp_cache_get( $key, "global-posts" );
  577. if( $post == false ) {
  578. $post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM " . $wpdb->get_blog_prefix( $blog_id ) . "posts WHERE ID = %d", $post_id ) );
  579. wp_cache_add( $key, $post, "global-posts" );
  580. }
  581. return $post;
  582. }
  583. function clear_global_post_cache( $post_id ) {
  584. global $wpdb;
  585. wp_cache_delete( $wpdb->blogid . '-' . $post_id, 'global-posts' );
  586. }
  587. add_action( 'publish_post', 'clear_global_post_cache' );
  588. add_action( 'delete_post', 'clear_global_post_cache' );
  589. function add_user_to_blog( $blog_id, $user_id, $role ) {
  590. switch_to_blog($blog_id);
  591. $user = new WP_User($user_id);
  592. if ( empty($user) )
  593. return new WP_Error('user_does_not_exist', __('That user does not exist.'));
  594. if ( !get_usermeta($user_id, 'primary_blog') ) {
  595. update_usermeta($user_id, 'primary_blog', $blog_id);
  596. $details = get_blog_details($blog_id);
  597. update_usermeta($user_id, 'source_domain', $details->domain);
  598. }
  599. $user->set_role($role);
  600. do_action('add_user_to_blog', $user_id, $role, $blog_id);
  601. wp_cache_delete( $user_id, 'users' );
  602. restore_current_blog();
  603. return true;
  604. }
  605. function remove_user_from_blog($user_id, $blog_id = '', $reassign = '') {
  606. global $wpdb;
  607. switch_to_blog($blog_id);
  608. $user_id = (int) $user_id;
  609. do_action('remove_user_from_blog', $user_id, $blog_id);
  610. // If being removed from the primary blog, set a new primary if the user is assigned
  611. // to multiple blogs.
  612. $primary_blog = get_usermeta($user_id, 'primary_blog');
  613. if ( $primary_blog == $blog_id ) {
  614. $new_id = '';
  615. $new_domain = '';
  616. $blogs = get_blogs_of_user($user_id);
  617. foreach ( (array) $blogs as $blog ) {
  618. if ( $blog->userblog_id == $blog_id )
  619. continue;
  620. $new_id = $blog->userblog_id;
  621. $new_domain = $blog->domain;
  622. break;
  623. }
  624. update_usermeta($user_id, 'primary_blog', $new_id);
  625. update_usermeta($user_id, 'source_domain', $new_domain);
  626. }
  627. // wp_revoke_user($user_id);
  628. $user = new WP_User($user_id);
  629. $user->remove_all_caps();
  630. $blogs = get_blogs_of_user($user_id);
  631. if ( count($blogs) == 0 ) {
  632. update_usermeta($user_id, 'primary_blog', '');
  633. update_usermeta($user_id, 'source_domain', '');
  634. }
  635. if( $reassign != '' ) {
  636. $reassign = (int) $reassign;
  637. $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_author = %d WHERE post_author = %d", $reassign, $user_id) );
  638. $wpdb->query( $wpdb->prepare("UPDATE $wpdb->links SET link_owner = %d WHERE link_owner = %d", $reassign, $user_id) );
  639. }
  640. restore_current_blog();
  641. }
  642. function create_empty_blog( $domain, $path, $weblog_title, $site_id = 1 ) {
  643. $domain = addslashes( $domain );
  644. $weblog_title = addslashes( $weblog_title );
  645. if( empty($path) )
  646. $path = '/';
  647. // Check if the domain has been used already. We should return an error message.
  648. if ( domain_exists($domain, $path, $site_id) )
  649. return __('error: Blog URL already taken.');
  650. // Need to backup wpdb table names, and create a new wp_blogs entry for new blog.
  651. // Need to get blog_id from wp_blogs, and create new table names.
  652. // Must restore table names at the end of function.
  653. if ( ! $blog_id = insert_blog($domain, $path, $site_id) )
  654. return __('error: problem creating blog entry');
  655. switch_to_blog($blog_id);
  656. install_blog($blog_id);
  657. restore_current_blog();
  658. return $blog_id;
  659. }
  660. function get_blog_permalink( $_blog_id, $post_id ) {
  661. $key = "{$_blog_id}-{$post_id}-blog_permalink";
  662. $link = wp_cache_get( $key, 'site-options' );
  663. if( $link == false ) {
  664. switch_to_blog( $_blog_id );
  665. $link = get_permalink( $post_id );
  666. restore_current_blog();
  667. wp_cache_add( $key, $link, 'site-options', 360 );
  668. }
  669. return $link;
  670. }
  671. function get_blog_id_from_url( $domain, $path = '/' ) {
  672. global $wpdb;
  673. $domain = strtolower( $wpdb->escape( $domain ) );
  674. $path = strtolower( $wpdb->escape( $path ) );
  675. $id = wp_cache_get( md5( $domain . $path ), 'blog-id-cache' );
  676. if( $id == -1 ) { // blog does not exist
  677. return 0;
  678. } elseif( $id ) {
  679. return (int)$id;
  680. }
  681. $id = $wpdb->get_var( "SELECT blog_id FROM $wpdb->blogs WHERE domain = '$domain' and path = '$path' /* get_blog_id_from_url */" );
  682. if ( !$id ) {
  683. wp_cache_set( md5( $domain . $path ), -1, 'blog-id-cache' );
  684. return false;
  685. }
  686. wp_cache_set( md5( $domain . $path ), $id, 'blog-id-cache' );
  687. return $id;
  688. }
  689. // wpmu admin functions
  690. function wpmu_admin_do_redirect( $url = '' ) {
  691. $ref = '';
  692. if ( isset( $_GET['ref'] ) )
  693. $ref = $_GET['ref'];
  694. if ( isset( $_POST['ref'] ) )
  695. $ref = $_POST['ref'];
  696. if( $ref ) {
  697. $ref = wpmu_admin_redirect_add_updated_param( $ref );
  698. wp_redirect( $ref );
  699. exit();
  700. }
  701. if( empty( $_SERVER['HTTP_REFERER'] ) == false ) {
  702. wp_redirect( $_SERVER['HTTP_REFERER'] );
  703. exit();
  704. }
  705. $url = wpmu_admin_redirect_add_updated_param( $url );
  706. if( isset( $_GET['redirect'] ) ) {
  707. if( substr( $_GET['redirect'], 0, 2 ) == 's_' ) {
  708. $url .= "&action=blogs&s=". wp_specialchars( substr( $_GET['redirect'], 2 ) );
  709. }
  710. } elseif( isset( $_POST['redirect'] ) ) {
  711. $url = wpmu_admin_redirect_add_updated_param( $_POST['redirect'] );
  712. }
  713. wp_redirect( $url );
  714. exit();
  715. }
  716. function wpmu_admin_redirect_add_updated_param( $url = '' ) {
  717. if( strpos( $url, 'updated=true' ) === false ) {
  718. if( strpos( $url, '?' ) === false ) {
  719. return $url . '?updated=true';
  720. } else {
  721. return $url . '&updated=true';
  722. }
  723. }
  724. return $url;
  725. }
  726. function is_blog_user( $blog_id = 0 ) {
  727. global $current_user, $wpdb;
  728. if ( !$blog_id )
  729. $blog_id = $wpdb->blogid;
  730. $cap_key = $wpdb->base_prefix . $blog_id . '_capabilities';
  731. if ( is_array($current_user->$cap_key) && in_array(1, $current_user->$cap_key) )
  732. return true;
  733. return false;
  734. }
  735. function validate_email( $email, $check_domain = true) {
  736. if (ereg('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+'.'@'.
  737. '[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.'.
  738. '[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$', $email))
  739. {
  740. if ($check_domain && function_exists('checkdnsrr')) {
  741. list (, $domain) = explode('@', $email);
  742. if (checkdnsrr($domain.'.', 'MX') || checkdnsrr($domain.'.', 'A')) {
  743. return true;
  744. }
  745. return false;
  746. }
  747. return true;
  748. }
  749. return false;
  750. }
  751. function is_email_address_unsafe( $user_email ) {
  752. $banned_names = get_site_option( "banned_email_domains" );
  753. if ($banned_names && !is_array( $banned_names )) {
  754. $banned_names = explode( "\n", $banned_names);
  755. }
  756. if ( is_array( $banned_names ) && empty( $banned_names ) == false ) {
  757. $email_domain = strtolower( substr( $user_email, 1 + strpos( $user_email, '@' ) ) );
  758. foreach( (array) $banned_names as $banned_domain ) {
  759. if( $banned_domain == '' )
  760. continue;
  761. if (
  762. strstr( $email_domain, $banned_domain ) ||
  763. (
  764. strstr( $banned_domain, '/' ) &&
  765. preg_match( $banned_domain, $email_domain )
  766. )
  767. )
  768. return true;
  769. }
  770. }
  771. return false;
  772. }
  773. function wpmu_validate_user_signup($user_name, $user_email) {
  774. global $wpdb;
  775. $errors = new WP_Error();
  776. $user_name = preg_replace( "/\s+/", '', sanitize_user( $user_name, true ) );
  777. $user_email = sanitize_email( $user_email );
  778. if ( empty( $user_name ) )
  779. $errors->add('user_name', __("Please enter a username"));
  780. $maybe = array();
  781. preg_match( "/[a-z0-9]+/", $user_name, $maybe );
  782. if( $user_name != $maybe[0] ) {
  783. $errors->add('user_name', __("Only lowercase letters and numbers allowed"));
  784. }
  785. $illegal_names = get_site_option( "illegal_names" );
  786. if( is_array( $illegal_names ) == false ) {
  787. $illegal_names = array( "www", "web", "root", "admin", "main", "invite", "administrator" );
  788. add_site_option( "illegal_names", $illegal_names );
  789. }
  790. if( in_array( $user_name, $illegal_names ) == true ) {
  791. $errors->add('user_name', __("That username is not allowed"));
  792. }
  793. if( is_email_address_unsafe( $user_email ) )
  794. $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."));
  795. if( strlen( $user_name ) < 4 ) {
  796. $errors->add('user_name', __("Username must be at least 4 characters"));
  797. }
  798. if ( strpos( " " . $user_name, "_" ) != false )
  799. $errors->add('user_name', __("Sorry, usernames may not contain the character '_'!"));
  800. // all numeric?
  801. $match = array();
  802. preg_match( '/[0-9]*/', $user_name, $match );
  803. if ( $match[0] == $user_name )
  804. $errors->add('user_name', __("Sorry, usernames must have letters too!"));
  805. if ( !is_email( $user_email ) )
  806. $errors->add('user_email', __("Please enter a correct email address"));
  807. if ( !validate_email( $user_email ) )
  808. $errors->add('user_email', __("Please check your email address."));
  809. $limited_email_domains = get_site_option( 'limited_email_domains' );
  810. if ( is_array( $limited_email_domains ) && empty( $limited_email_domains ) == false ) {
  811. $emaildomain = substr( $user_email, 1 + strpos( $user_email, '@' ) );
  812. if( in_array( $emaildomain, $limited_email_domains ) == false ) {
  813. $errors->add('user_email', __("Sorry, that email address is not allowed!"));
  814. }
  815. }
  816. // Check if the username has been used already.
  817. if ( username_exists($user_name) )
  818. $errors->add('user_name', __("Sorry, that username already exists!"));
  819. // Check if the email address has been used already.
  820. if ( email_exists($user_email) )
  821. $errors->add('user_email', __("Sorry, that email address is already used!"));
  822. // Has someone already signed up for this username?
  823. $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE user_login = %s", $user_name) );
  824. if ( $signup != null ) {
  825. $registered_at = mysql2date('U', $signup->registered);
  826. $now = current_time( 'timestamp', true );
  827. $diff = $now - $registered_at;
  828. // If registered more than two days ago, cancel registration and let this signup go through.
  829. if ( $diff > 172800 ) {
  830. $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->signups WHERE user_login = %s", $user_name) );
  831. } else {
  832. $errors->add('user_name', __("That username is currently reserved but may be available in a couple of days."));
  833. }
  834. if( $signup->active == 0 && $signup->user_email == $user_email )
  835. $errors->add('user_email_used', __("username and email used"));
  836. }
  837. $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE user_email = %s", $user_email) );
  838. if ( $signup != null ) {
  839. $diff = current_time( 'timestamp', true ) - mysql2date('U', $signup->registered);
  840. // If registered more than two days ago, cancel registration and let this signup go through.
  841. if ( $diff > 172800 ) {
  842. $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->signups WHERE user_email = %s", $user_email) );
  843. } else {
  844. $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."));
  845. }
  846. }
  847. $result = array('user_name' => $user_name, 'user_email' => $user_email, 'errors' => $errors);
  848. return apply_filters('wpmu_validate_user_signup', $result);
  849. }
  850. function wpmu_validate_blog_signup($blogname, $blog_title, $user = '') {
  851. global $wpdb, $domain, $base, $current_site;
  852. $blogname = preg_replace( "/\s+/", '', sanitize_user( $blogname, true ) );
  853. $blog_title = strip_tags( $blog_title );
  854. $blog_title = substr( $blog_title, 0, 50 );
  855. $errors = new WP_Error();
  856. $illegal_names = get_site_option( "illegal_names" );
  857. if( $illegal_names == false ) {
  858. $illegal_names = array( "www", "web", "root", "admin", "main", "invite", "administrator" );
  859. add_site_option( "illegal_names", $illegal_names );
  860. }
  861. if ( empty( $blogname ) )
  862. $errors->add('blogname', __("Please enter a blog name"));
  863. $maybe = array();
  864. preg_match( "/[a-z0-9]+/", $blogname, $maybe );
  865. if( $blogname != $maybe[0] ) {
  866. $errors->add('blogname', __("Only lowercase letters and numbers allowed"));
  867. }
  868. if( in_array( $blogname, $illegal_names ) == true ) {
  869. $errors->add('blogname', __("That name is not allowed"));
  870. }
  871. if( strlen( $blogname ) < 4 && !is_site_admin() ) {
  872. $errors->add('blogname', __("Blog name must be at least 4 characters"));
  873. }
  874. if ( strpos( " " . $blogname, "_" ) != false )
  875. $errors->add('blogname', __("Sorry, blog names may not contain the character '_'!"));
  876. // do not allow users to create a blog that conflicts with a page on the main blog.
  877. if ( constant( "VHOST" ) == 'no' && $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 ) ) ) {
  878. $errors->add( 'blogname', __( "Sorry, you may not use that blog name" ) );
  879. }
  880. // all numeric?
  881. $match = array();
  882. preg_match( '/[0-9]*/', $blogname, $match );
  883. if ( $match[0] == $blogname )
  884. $errors->add('blogname', __("Sorry, blog names must have letters too!"));
  885. $blogname = apply_filters( "newblogname", $blogname );
  886. $blog_title = stripslashes( $blog_title );
  887. if ( empty( $blog_title ) )
  888. $errors->add('blog_title', __("Please enter a blog title"));
  889. // Check if the domain/path has been used already.
  890. if( constant( "VHOST" ) == 'yes' ) {
  891. $mydomain = "$blogname.$domain";
  892. $path = $base;
  893. } else {
  894. $mydomain = "$domain";
  895. $path = $base.$blogname.'/';
  896. }
  897. if ( domain_exists($mydomain, $path) )
  898. $errors->add('blogname', __("Sorry, that blog already exists!"));
  899. if ( username_exists( $blogname ) ) {
  900. if ( is_object( $user ) == false || ( is_object($user) && ( $user->user_login != $blogname ) ) )
  901. $errors->add( 'blogname', __( "Sorry, that blog is reserved!" ) );
  902. }
  903. // Has someone already signed up for this domain?
  904. $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE domain = %s AND path = %s", $mydomain, $path) ); // TODO: Check email too?
  905. if ( ! empty($signup) ) {
  906. $diff = current_time( 'timestamp', true ) - mysql2date('U', $signup->registered);
  907. // If registered more than two days ago, cancel registration and let this signup go through.
  908. if ( $diff > 172800 ) {
  909. $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->signups WHERE domain = %s AND path = %s", $mydomain, $path) );
  910. } else {
  911. $errors->add('blogname', __("That blog is currently reserved but may be available in a couple days."));
  912. }
  913. }
  914. $result = array('domain' => $mydomain, 'path' => $path, 'blogname' => $blogname, 'blog_title' => $blog_title, 'errors' => $errors);
  915. return apply_filters('wpmu_validate_blog_signup', $result);
  916. }
  917. // Record signup information for future activation. wpmu_validate_signup() should be run
  918. // on the inputs before calling wpmu_signup().
  919. function wpmu_signup_blog($domain, $path, $title, $user, $user_email, $meta = '') {
  920. global $wpdb;
  921. $key = substr( md5( time() . rand() . $domain ), 0, 16 );
  922. $meta = serialize($meta);
  923. $domain = $wpdb->escape($domain);
  924. $path = $wpdb->escape($path);
  925. $title = $wpdb->escape($title);
  926. $wpdb->insert( $wpdb->signups, array(
  927. 'domain' => $domain,
  928. 'path' => $path,
  929. 'title' => $title,
  930. 'user_login' => $user,
  931. 'user_email' => $user_email,
  932. 'registered' => current_time('mysql', true),
  933. 'activation_key' => $key,
  934. 'meta' => $meta
  935. ) );
  936. wpmu_signup_blog_notification($domain, $path, $title, $user, $user_email, $key, $meta);
  937. }
  938. function wpmu_signup_user($user, $user_email, $meta = '') {
  939. global $wpdb;
  940. // Format data
  941. $user = preg_replace( "/\s+/", '', sanitize_user( $user, true ) );
  942. $user_email = sanitize_email( $user_email );
  943. $key = substr( md5( time() . rand() . $user_email ), 0, 16 );
  944. $meta = serialize($meta);
  945. $wpdb->insert( $wpdb->signups, array(
  946. 'domain' => '',
  947. 'path' => '',
  948. 'title' => '',
  949. 'user_login' => $user,
  950. 'user_email' => $user_email,
  951. 'registered' => current_time('mysql', true),
  952. 'activation_key' => $key,
  953. 'meta' => $meta
  954. ) );
  955. wpmu_signup_user_notification($user, $user_email, $key, $meta);
  956. }
  957. // Notify user of signup success.
  958. function wpmu_signup_blog_notification($domain, $path, $title, $user, $user_email, $key, $meta = '') {
  959. global $current_site;
  960. if( !apply_filters('wpmu_signup_blog_notification', $domain, $path, $title, $user, $user_email, $key, $meta) )
  961. return false;
  962. // Send email with activation link.
  963. if( constant( "VHOST" ) == 'no' || $current_site->id != 1 ) {
  964. $activate_url = "http://" . $current_site->domain . $current_site->path . "wp-activate.php?key=$key";
  965. } else {
  966. $activate_url = "http://{$domain}{$path}wp-activate.php?key=$key";
  967. }
  968. $activate_url = clean_url($activate_url);
  969. $admin_email = get_site_option( "admin_email" );
  970. if( $admin_email == '' )
  971. $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
  972. $from_name = get_site_option( "site_name" ) == '' ? 'WordPress' : wp_specialchars( get_site_option( "site_name" ) );
  973. $message_headers = "MIME-Version: 1.0\n" . "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
  974. $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 blog here:\n\n%s" ) ), $activate_url, clean_url( "http://{$domain}{$path}" ), $key );
  975. // TODO: Don't hard code activation link.
  976. $subject = sprintf( apply_filters( 'wpmu_signup_blog_notification_subject', __( '[%1s] Activate %2s' ) ), $from_name, clean_url( 'http://' . $domain . $path ) );
  977. wp_mail($user_email, $subject, $message, $message_headers);
  978. return true;
  979. }
  980. function wpmu_signup_user_notification($user, $user_email, $key, $meta = '') {
  981. global $current_site;
  982. if( !apply_filters('wpmu_signup_user_notification', $user, $user_email, $key, $meta) )
  983. return false;
  984. // Send email with activation link.
  985. $admin_email = get_site_option( "admin_email" );
  986. if( $admin_email == '' )
  987. $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
  988. $from_name = get_site_option( "site_name" ) == '' ? 'WordPress' : wp_specialchars( get_site_option( "site_name" ) );
  989. $message_headers = "MIME-Version: 1.0\n" . "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
  990. $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 );
  991. // TODO: Don't hard code activation link.
  992. $subject = sprintf( __( apply_filters( 'wpmu_signup_user_notification_subject', '[%1s] Activate %2s' ) ), $from_name, $user);
  993. wp_mail($user_email, $subject, $message, $message_headers);
  994. return true;
  995. }
  996. function wpmu_activate_signup($key) {
  997. global $wpdb, $current_site;
  998. $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE activation_key = %s", $key) );
  999. if ( empty($signup) )
  1000. return new WP_Error('invalid_key', __('Invalid activation key.'));
  1001. if ( $signup->active )
  1002. return new WP_Error('already_active', __('The blog is already active.'), $signup);
  1003. $meta = unserialize($signup->meta);
  1004. $user_login = $wpdb->escape($signup->user_login);
  1005. $user_email = $wpdb->escape($signup->user_email);
  1006. wpmu_validate_user_signup($user_login, $user_email);
  1007. $password = generate_random_password();
  1008. $user_id = username_exists($user_login);
  1009. if ( ! $user_id )
  1010. $user_id = wpmu_create_user($user_login, $password, $user_email);
  1011. else
  1012. $user_already_exists = true;
  1013. if ( ! $user_id )
  1014. return new WP_Error('create_user', __('Could not create user'), $signup);
  1015. $now = current_time('mysql', true);
  1016. if ( empty($signup->domain) ) {
  1017. $wpdb->update( $wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key) );
  1018. if ( isset( $user_already_exists ) )
  1019. return new WP_Error( 'user_already_exists', __( 'That username is already activated.' ), $signup);
  1020. wpmu_welcome_user_notification($user_id, $password, $meta);
  1021. $user_site = get_site_option( 'dashboard_blog', $current_site->blog_id );
  1022. if ( $user_site == false ) {
  1023. add_user_to_blog( '1', $user_id, get_site_option( 'default_user_role', 'subscriber' ) );
  1024. } else {
  1025. add_user_to_blog( $user_site, $user_id, get_site_option( 'default_user_role', 'subscriber' ) );
  1026. }
  1027. add_new_user_to_blog( $user_id, $user_email, $meta );
  1028. do_action('wpmu_activate_user', $user_id, $password, $meta);
  1029. return array('user_id' => $user_id, 'password' => $password, 'meta' => $meta);
  1030. }
  1031. wpmu_validate_blog_signup($signup->domain, $signup->title);
  1032. $blog_id = wpmu_create_blog( $signup->domain, $signup->path, $signup->title, $user_id, $meta, $wpdb->siteid );
  1033. // TODO: What to do if we create a user but cannot create a blog?
  1034. if ( is_wp_error($blog_id) ) {
  1035. // If blog is taken, that means a previous attempt to activate this blog failed in between creating the blog and
  1036. // setting the activation flag. Let's just set the active flag and instruct the user to reset their password.
  1037. if ( 'blog_taken' == $blog_id->get_error_code() ) {
  1038. $blog_id->add_data( $signup );
  1039. $wpdb->update( $wpdb->signups, array( 'active' => 1, 'activated' => $now ), array( 'activation_key' => $key ) );
  1040. }
  1041. return $blog_id;
  1042. }
  1043. $wpdb->update( $wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key) );
  1044. wpmu_welcome_notification($blog_id, $user_id, $password, $signup->title, $meta);
  1045. do_action('wpmu_activate_blog', $blog_id, $user_id, $password, $signup->title, $meta);
  1046. return array('blog_id' => $blog_id, 'user_id' => $user_id, 'password' => $password, 'title' => $signup->title, 'meta' => $meta);
  1047. }
  1048. function generate_random_password( $len = 8 ) {
  1049. $random_password = substr(md5(uniqid(microtime())), 0, intval( $len ) );
  1050. $random_password = apply_filters('random_password', $random_password);
  1051. return $random_password;
  1052. }
  1053. function wpmu_create_user( $user_name, $password, $email) {
  1054. $user_name = preg_replace( "/\s+/", '', sanitize_user( $user_name, true ) );
  1055. if ( username_exists($user_name) )
  1056. return false;
  1057. // Check if the email address has been used already.
  1058. if ( email_exists($email) )
  1059. return false;
  1060. $user_id = wp_create_user( $user_name, $password, $email );
  1061. $user = new WP_User($user_id);
  1062. // Newly created users have no roles or caps until they are added to a blog.
  1063. update_user_option($user_id, 'capabilities', '');
  1064. update_user_option($user_id, 'user_level', '');
  1065. do_action( 'wpmu_new_user', $user_id );
  1066. return $user_id;
  1067. }
  1068. function wpmu_create_blog($domain, $path, $title, $user_id, $meta = '', $site_id = 1) {
  1069. $domain = preg_replace( "/\s+/", '', sanitize_user( $domain, true ) );
  1070. if( constant( 'VHOST' ) == 'yes' )
  1071. $domain = str_replace( '@', '', $domain );
  1072. $title = strip_tags( $title );
  1073. $user_id = (int) $user_id;
  1074. if( empty($path) )
  1075. $path = '/';
  1076. // Check if the domain has been used already. We should return an error message.
  1077. if ( domain_exists($domain, $path, $site_id) )
  1078. return new WP_Error('blog_taken', __('Blog already exists.'));
  1079. if ( !defined("WP_INSTALLING") )
  1080. define( "WP_INSTALLING", true );
  1081. if ( ! $blog_id = insert_blog($domain, $path, $site_id) )
  1082. return new WP_Error('insert_blog', __('Could not create blog.'));
  1083. switch_to_blog($blog_id);
  1084. install_blog($blog_id, $title);
  1085. install_blog_defaults($blog_id, $user_id);
  1086. add_user_to_blog($blog_id, $user_id, 'administrator');
  1087. if ( is_array($meta) ) foreach ($meta as $key => $value) {
  1088. if( $key == 'public' || $key == 'archived' || $key == 'mature' || $key == 'spam' || $key == 'deleted' || $key == 'lang_id' ) {
  1089. update_blog_status( $blog_id, $key, $value );
  1090. } else {
  1091. update_option( $key, $value );
  1092. }
  1093. }
  1094. add_option( 'WPLANG', get_site_option( 'WPLANG' ) );
  1095. update_option( 'blog_public', $meta['public'] );
  1096. if ( !is_site_admin() && get_usermeta( $user_id, 'primary_blog' ) == get_site_option( 'dashboard_blog', 1 ) )
  1097. update_usermeta( $user_id, 'primary_blog', $blog_id );
  1098. restore_current_blog();
  1099. do_action( 'wpmu_new_blog', $blog_id, $user_id );
  1100. return $blog_id;
  1101. }
  1102. function newblog_notify_siteadmin( $blog_id, $deprecated = '' ) {
  1103. global $current_site;
  1104. if( get_site_option( 'registrationnotification' ) != 'yes' )
  1105. return false;
  1106. $email = get_site_option( 'admin_email' );
  1107. if( is_email($email) == false )
  1108. return false;
  1109. $options_site_url = clean_url("http://{$current_site->domain}{$current_site->path}wp-admin/wpmu-options.php");
  1110. switch_to_blog( $blog_id );
  1111. $blogname = get_option( 'blogname' );
  1112. $siteurl = get_option( 'siteurl' );
  1113. restore_current_blog();
  1114. $msg = sprintf( __( "New Blog: %1s
  1115. URL: %2s
  1116. Remote IP: %3s
  1117. Disable these notifications: %4s"), $blogname, $siteurl, $_SERVER['REMOTE_ADDR'], $options_site_url);
  1118. $msg = apply_filters( 'newblog_notify_siteadmin', $msg );
  1119. wp_mail( $email, sprintf( __( "New Blog Registration: %s" ), $siteurl ), $msg );
  1120. return true;
  1121. }
  1122. function newuser_notify_siteadmin( $user_id ) {
  1123. global $current_site;
  1124. if( get_site_option( 'registrationnotification' ) != 'yes' )
  1125. return false;
  1126. $email = get_site_option( 'admin_email' );
  1127. if( is_email($email) == false )
  1128. return false;
  1129. $user = new WP_User($user_id);
  1130. $options_site_url = clean_url("http://{$current_site->domain}{$current_site->path}wp-admin/wpmu-options.php");
  1131. $msg = sprintf(__("New User: %1s
  1132. Remote IP: %2s
  1133. Disable these notifications: %3s"), $user->user_login, $_SERVER['REMOTE_ADDR'], $options_site_url);
  1134. $msg = apply_filters( 'newuser_notify_siteadmin', $msg );
  1135. wp_mail( $email, sprintf(__("New User Registration: %s"), $user->user_login), $msg );
  1136. return true;
  1137. }
  1138. function domain_exists($domain, $path, $site_id = 1) {
  1139. global $wpdb;
  1140. 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) );
  1141. }
  1142. function insert_blog($domain, $path, $site_id) {
  1143. global $wpdb;
  1144. $path = trailingslashit($path);
  1145. $site_id = (int) $site_id;
  1146. $result = $wpdb->insert( $wpdb->blogs, array('site_id' => $site_id, 'domain' => $domain, 'path' => $path, 'registered' => current_time('mysql')) );
  1147. if ( ! $result )
  1148. return false;
  1149. refresh_blog_details($wpdb->insert_id);
  1150. return $wpdb->insert_id;
  1151. }
  1152. // Install an empty blog. wpdb should already be switched.
  1153. function install_blog($blog_id, $blog_title = '') {
  1154. global $wpdb, $table_prefix, $wp_roles;
  1155. $wpdb->suppress_errors();
  1156. // Cast for security
  1157. $blog_id = (int) $blog_id;
  1158. require_once( ABSPATH . 'wp-admin/includes/upgrade.php');
  1159. if ( $wpdb->get_results("SELECT ID FROM $wpdb->posts") )
  1160. 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>');
  1161. $wpdb->suppress_errors( false);
  1162. $url = get_blogaddress_by_id($blog_id);
  1163. // Set everything up
  1164. make_db_current_silent();
  1165. populate_options();
  1166. populate_roles();
  1167. $wp_roles->_init();
  1168. // fix url.
  1169. update_option('siteurl', $url);
  1170. update_option('home', $url);
  1171. update_option('fileupload_url', $url . "files" );
  1172. update_option('upload_path', "wp-content/blogs.dir/" . $blog_id . "/files");
  1173. update_option('blogname', stripslashes( $blog_title ) );
  1174. update_option('admin_email', '');
  1175. $wpdb->update( $wpdb->options, array('option_value' => ''), array('option_name' => 'admin_email') );
  1176. // Default category
  1177. $wpdb->insert( $wpdb->terms, array('term_id' => 1, 'name' => __('Uncategorized'), 'slug' => sanitize_title(__('Uncategorized')), 'term_group' => 0) );
  1178. $wpdb->insert( $wpdb->term_taxonomy, array('term_id' => 1, 'taxonomy' => 'category', 'description' => '', 'parent' => 0, 'count' => 1) );
  1179. // Default link category
  1180. $cat_name = __('Blogroll');
  1181. $cat_slug = sanitize_title($cat_name);
  1182. $blogroll_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM {$wpdb->sitecategories} WHERE category_nicename = %s", $cat_slug ) );
  1183. if( $blogroll_id == null ) {
  1184. $wpdb->insert( $wpdb->sitecategories, array('cat_ID' => 0, 'cat_name' => $cat_name, 'category_nicename' => $cat_slug, 'last_updated' => current_time('mysql', true)) );
  1185. $blogroll_id = $wpdb->insert_id;
  1186. }
  1187. $wpdb->insert( $wpdb->terms, array('term_id' => $blogroll_id, 'name' => $cat_name, 'slug' => $cat_slug, 'term_group' => 0) );
  1188. $wpdb->insert( $wpdb->term_taxonomy, array('term_id' => $blogroll_id, 'taxonomy' => 'link_category', 'description' => '', 'parent' => 0, 'count' => 2) );
  1189. update_option('default_link_category', $blogroll_id);
  1190. // remove all perms
  1191. $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE meta_key = %s", $tab

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