]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/ms-load.php
WordPress 3.4.2
[autoinstalls/wordpress.git] / wp-includes / ms-load.php
1 <?php
2 /**
3  * These functions are needed to load Multisite.
4  *
5  * @since 3.0.0
6  *
7  * @package WordPress
8  * @subpackage Multisite
9  */
10
11 /**
12  * Whether a subdomain configuration is enabled.
13  *
14  * @since 3.0.0
15  *
16  * @return bool True if subdomain configuration is enabled, false otherwise.
17  */
18 function is_subdomain_install() {
19         if ( defined('SUBDOMAIN_INSTALL') )
20                 return SUBDOMAIN_INSTALL;
21
22         if ( defined('VHOST') && VHOST == 'yes' )
23                 return true;
24
25         return false;
26 }
27
28 /**
29  * Returns array of network plugin files to be included in global scope.
30  *
31  * The default directory is wp-content/plugins. To change the default directory
32  * manually, define <code>WP_PLUGIN_DIR</code> and <code>WP_PLUGIN_URL</code>
33  * in wp-config.php.
34  *
35  * @access private
36  * @since 3.1.0
37  * @return array Files to include
38  */
39 function wp_get_active_network_plugins() {
40         $active_plugins = (array) get_site_option( 'active_sitewide_plugins', array() );
41         if ( empty( $active_plugins ) )
42                 return array();
43
44         $plugins = array();
45         $active_plugins = array_keys( $active_plugins );
46         sort( $active_plugins );
47
48         foreach ( $active_plugins as $plugin ) {
49                 if ( ! validate_file( $plugin ) // $plugin must validate as file
50                         && '.php' == substr( $plugin, -4 ) // $plugin must end with '.php'
51                         && file_exists( WP_PLUGIN_DIR . '/' . $plugin ) // $plugin must exist
52                         )
53                 $plugins[] = WP_PLUGIN_DIR . '/' . $plugin;
54         }
55         return $plugins;
56 }
57
58 /**
59  * Checks status of current blog.
60  *
61  * Checks if the blog is deleted, inactive, archived, or spammed.
62  *
63  * Dies with a default message if the blog does not pass the check.
64  *
65  * To change the default message when a blog does not pass the check,
66  * use the wp-content/blog-deleted.php, blog-inactive.php and
67  * blog-suspended.php drop-ins.
68  *
69  * @return bool|string Returns true on success, or drop-in file to include.
70  */
71 function ms_site_check() {
72         global $wpdb, $current_blog;
73
74         // Allow short-circuiting
75         $check = apply_filters('ms_site_check', null);
76         if ( null !== $check )
77                 return true;
78
79         // Allow super admins to see blocked sites
80         if ( is_super_admin() )
81                 return true;
82
83         if ( '1' == $current_blog->deleted ) {
84                 if ( file_exists( WP_CONTENT_DIR . '/blog-deleted.php' ) )
85                         return WP_CONTENT_DIR . '/blog-deleted.php';
86                 else
87                         wp_die( __( 'This user has elected to delete their account and the content is no longer available.' ), '', array( 'response' => 410 ) );
88         }
89
90         if ( '2' == $current_blog->deleted ) {
91                 if ( file_exists( WP_CONTENT_DIR . '/blog-inactive.php' ) )
92                         return WP_CONTENT_DIR . '/blog-inactive.php';
93                 else
94                         wp_die( sprintf( __( 'This site has not been activated yet. If you are having problems activating your site, please contact <a href="mailto:%1$s">%1$s</a>.' ), str_replace( '@', ' AT ', get_site_option( 'admin_email', "support@{$current_site->domain}" ) ) ) );
95         }
96
97         if ( $current_blog->archived == '1' || $current_blog->spam == '1' ) {
98                 if ( file_exists( WP_CONTENT_DIR . '/blog-suspended.php' ) )
99                         return WP_CONTENT_DIR . '/blog-suspended.php';
100                 else
101                         wp_die( __( 'This site has been archived or suspended.' ), '', array( 'response' => 410 ) );
102         }
103
104         return true;
105 }
106
107 /**
108  * Sets current site name.
109  *
110  * @access private
111  * @since 3.0.0
112  * @return object $current_site object with site_name
113  */
114 function get_current_site_name( $current_site ) {
115         global $wpdb;
116
117         $current_site->site_name = wp_cache_get( $current_site->id . ':site_name', 'site-options' );
118         if ( ! $current_site->site_name ) {
119                 $current_site->site_name = $wpdb->get_var( $wpdb->prepare( "SELECT meta_value FROM $wpdb->sitemeta WHERE site_id = %d AND meta_key = 'site_name'", $current_site->id ) );
120                 if ( ! $current_site->site_name )
121                         $current_site->site_name = ucfirst( $current_site->domain );
122                 wp_cache_set( $current_site->id . ':site_name', $current_site->site_name, 'site-options' );
123         }
124
125         return $current_site;
126 }
127
128 /**
129  * Sets current_site object.
130  *
131  * @access private
132  * @since 3.0.0
133  * @return object $current_site object
134  */
135 function wpmu_current_site() {
136         global $wpdb, $current_site, $domain, $path, $sites, $cookie_domain;
137
138         if ( empty( $current_site ) )
139                 $current_site = new stdClass;
140
141         if ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) ) {
142                 $current_site->id = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : 1;
143                 $current_site->domain = DOMAIN_CURRENT_SITE;
144                 $current_site->path   = $path = PATH_CURRENT_SITE;
145                 if ( defined( 'BLOG_ID_CURRENT_SITE' ) )
146                         $current_site->blog_id = BLOG_ID_CURRENT_SITE;
147                 elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) // deprecated.
148                         $current_site->blog_id = BLOGID_CURRENT_SITE;
149                 if ( DOMAIN_CURRENT_SITE == $domain )
150                         $current_site->cookie_domain = $cookie_domain;
151                 elseif ( substr( $current_site->domain, 0, 4 ) == 'www.' )
152                         $current_site->cookie_domain = substr( $current_site->domain, 4 );
153                 else
154                         $current_site->cookie_domain = $current_site->domain;
155
156                 wp_load_core_site_options( $current_site->id );
157
158                 return $current_site;
159         }
160
161         $current_site = wp_cache_get( 'current_site', 'site-options' );
162         if ( $current_site )
163                 return $current_site;
164
165         $sites = $wpdb->get_results( "SELECT * FROM $wpdb->site" ); // usually only one site
166         if ( 1 == count( $sites ) ) {
167                 $current_site = $sites[0];
168                 wp_load_core_site_options( $current_site->id );
169                 $path = $current_site->path;
170                 $current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s", $current_site->domain, $current_site->path ) );
171                 $current_site = get_current_site_name( $current_site );
172                 if ( substr( $current_site->domain, 0, 4 ) == 'www.' )
173                         $current_site->cookie_domain = substr( $current_site->domain, 4 );
174                 wp_cache_set( 'current_site', $current_site, 'site-options' );
175                 return $current_site;
176         }
177         $path = substr( $_SERVER[ 'REQUEST_URI' ], 0, 1 + strpos( $_SERVER[ 'REQUEST_URI' ], '/', 1 ) );
178
179         if ( $domain == $cookie_domain )
180                 $current_site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->site WHERE domain = %s AND path = %s", $domain, $path ) );
181         else
182                 $current_site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->site WHERE domain IN ( %s, %s ) AND path = %s ORDER BY CHAR_LENGTH( domain ) DESC LIMIT 1", $domain, $cookie_domain, $path ) );
183
184         if ( ! $current_site ) {
185                 if ( $domain == $cookie_domain )
186                         $current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE domain = %s AND path='/'", $domain ) );
187                 else
188                         $current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE domain IN ( %s, %s ) AND path = '/' ORDER BY CHAR_LENGTH( domain ) DESC LIMIT 1", $domain, $cookie_domain, $path ) );
189         }
190
191         if ( $current_site ) {
192                 $path = $current_site->path;
193                 $current_site->cookie_domain = $cookie_domain;
194                 return $current_site;
195         }
196
197         if ( is_subdomain_install() ) {
198                 $sitedomain = substr( $domain, 1 + strpos( $domain, '.' ) );
199                 $current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE domain = %s AND path = %s", $sitedomain, $path) );
200                 if ( $current_site ) {
201                         $current_site->cookie_domain = $current_site->domain;
202                         return $current_site;
203                 }
204
205                 $current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE domain = %s AND path='/'", $sitedomain) );
206         }
207
208         if ( $current_site || defined( 'WP_INSTALLING' ) ) {
209                 $path = '/';
210                 return $current_site;
211         }
212
213         // Still no dice.
214         wp_load_translations_early();
215
216         if ( 1 == count( $sites ) )
217                 wp_die( sprintf( __( 'That site does not exist. Please try <a href="%s">%s</a>.' ), 'http://' . $sites[0]->domain . $sites[0]->path ) );
218         else
219                 wp_die( __( 'No site defined on this host. If you are the owner of this site, please check <a href="http://codex.wordpress.org/Debugging_a_WordPress_Network">Debugging a WordPress Network</a> for help.' ) );
220 }
221
222 /**
223  * Displays a failure message.
224  *
225  * Used when a blog's tables do not exist. Checks for a missing $wpdb->site table as well.
226  *
227  * @access private
228  * @since 3.0.0
229  */
230 function ms_not_installed() {
231         global $wpdb, $domain, $path;
232
233         wp_load_translations_early();
234
235         $title = __( 'Error establishing database connection' );
236         $msg  = '<h1>' . $title . '</h1>';
237         if ( ! is_admin() )
238                 die( $msg );
239         $msg .= '<p>' . __( 'If your site does not display, please contact the owner of this network.' ) . '';
240         $msg .= ' ' . __( 'If you are the owner of this network please check that MySQL is running properly and all tables are error free.' ) . '</p>';
241         if ( false && !$wpdb->get_var( "SHOW TABLES LIKE '$wpdb->site'" ) )
242                 $msg .= '<p>' . sprintf( __( '<strong>Database tables are missing.</strong> This means that MySQL is not running, WordPress was not installed properly, or someone deleted <code>%s</code>. You really should look at your database now.' ), $wpdb->site ) . '</p>';
243         else
244                 $msg .= '<p>' . sprintf( __( '<strong>Could not find site <code>%1$s</code>.</strong> Searched for table <code>%2$s</code> in database <code>%3$s</code>. Is that right?' ), rtrim( $domain . $path, '/' ), $wpdb->blogs, DB_NAME ) . '</p>';
245         $msg .= '<p><strong>' . __( 'What do I do now?' ) . '</strong> ';
246         $msg .= __( 'Read the <a target="_blank" href="http://codex.wordpress.org/Debugging_a_WordPress_Network">bug report</a> page. Some of the guidelines there may help you figure out what went wrong.' );
247         $msg .= ' ' . __( 'If you&#8217;re still stuck with this message, then check that your database contains the following tables:' ) . '</p><ul>';
248         foreach ( $wpdb->tables('global') as $t => $table ) {
249                 if ( 'sitecategories' == $t )
250                         continue;
251                 $msg .= '<li>' . $table . '</li>';
252         }
253         $msg .= '</ul>';
254
255         wp_die( $msg, $title );
256 }