]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/ms-load.php
WordPress 3.5.1
[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;
73
74         $blog = get_blog_details();
75
76         // Allow short-circuiting
77         $check = apply_filters('ms_site_check', null);
78         if ( null !== $check )
79                 return true;
80
81         // Allow super admins to see blocked sites
82         if ( is_super_admin() )
83                 return true;
84
85         if ( '1' == $blog->deleted ) {
86                 if ( file_exists( WP_CONTENT_DIR . '/blog-deleted.php' ) )
87                         return WP_CONTENT_DIR . '/blog-deleted.php';
88                 else
89                         wp_die( __( 'This user has elected to delete their account and the content is no longer available.' ), '', array( 'response' => 410 ) );
90         }
91
92         if ( '2' == $blog->deleted ) {
93                 if ( file_exists( WP_CONTENT_DIR . '/blog-inactive.php' ) )
94                         return WP_CONTENT_DIR . '/blog-inactive.php';
95                 else
96                         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}" ) ) ) );
97         }
98
99         if ( $blog->archived == '1' || $blog->spam == '1' ) {
100                 if ( file_exists( WP_CONTENT_DIR . '/blog-suspended.php' ) )
101                         return WP_CONTENT_DIR . '/blog-suspended.php';
102                 else
103                         wp_die( __( 'This site has been archived or suspended.' ), '', array( 'response' => 410 ) );
104         }
105
106         return true;
107 }
108
109 /**
110  * Sets current site name.
111  *
112  * @access private
113  * @since 3.0.0
114  * @return object $current_site object with site_name
115  */
116 function get_current_site_name( $current_site ) {
117         global $wpdb;
118
119         $current_site->site_name = wp_cache_get( $current_site->id . ':site_name', 'site-options' );
120         if ( ! $current_site->site_name ) {
121                 $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 ) );
122                 if ( ! $current_site->site_name )
123                         $current_site->site_name = ucfirst( $current_site->domain );
124                 wp_cache_set( $current_site->id . ':site_name', $current_site->site_name, 'site-options' );
125         }
126
127         return $current_site;
128 }
129
130 /**
131  * Sets current_site object.
132  *
133  * @access private
134  * @since 3.0.0
135  * @return object $current_site object
136  */
137 function wpmu_current_site() {
138         global $wpdb, $current_site, $domain, $path, $sites, $cookie_domain;
139
140         if ( empty( $current_site ) )
141                 $current_site = new stdClass;
142
143         if ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) ) {
144                 $current_site->id = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : 1;
145                 $current_site->domain = DOMAIN_CURRENT_SITE;
146                 $current_site->path   = $path = PATH_CURRENT_SITE;
147                 if ( defined( 'BLOG_ID_CURRENT_SITE' ) )
148                         $current_site->blog_id = BLOG_ID_CURRENT_SITE;
149                 elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) // deprecated.
150                         $current_site->blog_id = BLOGID_CURRENT_SITE;
151                 if ( DOMAIN_CURRENT_SITE == $domain )
152                         $current_site->cookie_domain = $cookie_domain;
153                 elseif ( substr( $current_site->domain, 0, 4 ) == 'www.' )
154                         $current_site->cookie_domain = substr( $current_site->domain, 4 );
155                 else
156                         $current_site->cookie_domain = $current_site->domain;
157
158                 wp_load_core_site_options( $current_site->id );
159
160                 return $current_site;
161         }
162
163         $current_site = wp_cache_get( 'current_site', 'site-options' );
164         if ( $current_site )
165                 return $current_site;
166
167         $sites = $wpdb->get_results( "SELECT * FROM $wpdb->site" ); // usually only one site
168         if ( 1 == count( $sites ) ) {
169                 $current_site = $sites[0];
170                 wp_load_core_site_options( $current_site->id );
171                 $path = $current_site->path;
172                 $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 ) );
173                 $current_site = get_current_site_name( $current_site );
174                 if ( substr( $current_site->domain, 0, 4 ) == 'www.' )
175                         $current_site->cookie_domain = substr( $current_site->domain, 4 );
176                 wp_cache_set( 'current_site', $current_site, 'site-options' );
177                 return $current_site;
178         }
179         $path = substr( $_SERVER[ 'REQUEST_URI' ], 0, 1 + strpos( $_SERVER[ 'REQUEST_URI' ], '/', 1 ) );
180
181         if ( $domain == $cookie_domain )
182                 $current_site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->site WHERE domain = %s AND path = %s", $domain, $path ) );
183         else
184                 $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 ) );
185
186         if ( ! $current_site ) {
187                 if ( $domain == $cookie_domain )
188                         $current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE domain = %s AND path='/'", $domain ) );
189                 else
190                         $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 ) );
191         }
192
193         if ( $current_site ) {
194                 $path = $current_site->path;
195                 $current_site->cookie_domain = $cookie_domain;
196                 return $current_site;
197         }
198
199         if ( is_subdomain_install() ) {
200                 $sitedomain = substr( $domain, 1 + strpos( $domain, '.' ) );
201                 $current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE domain = %s AND path = %s", $sitedomain, $path) );
202                 if ( $current_site ) {
203                         $current_site->cookie_domain = $current_site->domain;
204                         return $current_site;
205                 }
206
207                 $current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE domain = %s AND path='/'", $sitedomain) );
208         }
209
210         if ( $current_site || defined( 'WP_INSTALLING' ) ) {
211                 $path = '/';
212                 return $current_site;
213         }
214
215         // Still no dice.
216         wp_load_translations_early();
217
218         if ( 1 == count( $sites ) )
219                 wp_die( sprintf( __( 'That site does not exist. Please try <a href="%s">%s</a>.' ), 'http://' . $sites[0]->domain . $sites[0]->path ) );
220         else
221                 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.' ) );
222 }
223
224 /**
225  * Displays a failure message.
226  *
227  * Used when a blog's tables do not exist. Checks for a missing $wpdb->site table as well.
228  *
229  * @access private
230  * @since 3.0.0
231  */
232 function ms_not_installed() {
233         global $wpdb, $domain, $path;
234
235         wp_load_translations_early();
236
237         $title = __( 'Error establishing a database connection' );
238         $msg  = '<h1>' . $title . '</h1>';
239         if ( ! is_admin() )
240                 die( $msg );
241         $msg .= '<p>' . __( 'If your site does not display, please contact the owner of this network.' ) . '';
242         $msg .= ' ' . __( 'If you are the owner of this network please check that MySQL is running properly and all tables are error free.' ) . '</p>';
243         if ( false && !$wpdb->get_var( "SHOW TABLES LIKE '$wpdb->site'" ) )
244                 $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>';
245         else
246                 $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>';
247         $msg .= '<p><strong>' . __( 'What do I do now?' ) . '</strong> ';
248         $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.' );
249         $msg .= ' ' . __( 'If you&#8217;re still stuck with this message, then check that your database contains the following tables:' ) . '</p><ul>';
250         foreach ( $wpdb->tables('global') as $t => $table ) {
251                 if ( 'sitecategories' == $t )
252                         continue;
253                 $msg .= '<li>' . $table . '</li>';
254         }
255         $msg .= '</ul>';
256
257         wp_die( $msg, $title );
258 }