root/trunk/wpmu-settings.php

Revision 1810, 11.1 kB (checked in by donncha, 2 days ago)

Cache get_current_site_name()

Line 
1 <?php
2 if( isset( $current_site ) && isset( $current_blog ) )
3     return;
4
5 // depreciated
6 $wpmuBaseTablePrefix = $table_prefix;
7
8 $domain = addslashes( $_SERVER['HTTP_HOST'] );
9 if( substr( $domain, 0, 4 ) == 'www.' )
10     $domain = substr( $domain, 4 );
11 if( strpos( $domain, ':' ) ) {
12     if( substr( $domain, -3 ) == ':80' ) {
13         $domain = substr( $domain, 0, -3 );
14         $_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -3 );
15     } elseif( substr( $domain, -4 ) == ':443' ) {
16         $domain = substr( $domain, 0, -4 );
17         $_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -4 );
18     } else {
19         die( 'WPMU only works without the port number in the URL.' );
20     }
21 }
22 $domain = preg_replace('/:.*$/', '', $domain); // Strip ports
23 if( substr( $domain, -1 ) == '.' )
24     $domain = substr( $domain, 0, -1 );
25
26 $path = preg_replace( '|([a-z0-9-]+.php.*)|', '', $_SERVER['REQUEST_URI'] );
27 $path = str_replace ( '/wp-admin/', '/', $path );
28 $path = preg_replace( '|(/[a-z0-9-]+?/).*|', '$1', $path );
29
30 function get_current_site_name( $current_site ) {
31     global $wpdb;
32     $current_site->site_name = wp_cache_get( $current_site->id . ':current_site_name', "site-options" );
33     if ( !$current->site->site_name ) {
34         $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 ) );
35         if( $current_site->site_name == null )
36             $current_site->site_name = ucfirst( $current_site->domain );
37         wp_cache_set( $current_site->id . ':current_site_name', $current_site->site_name, 'site-options');
38     }
39     return $current_site;
40 }
41
42 function wpmu_current_site() {
43     global $wpdb, $current_site, $domain, $path, $sites;
44     if( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) ) {
45         $current_site->id = (defined( 'SITE_ID_CURRENT_SITE' ) ? constant('SITE_ID_CURRENT_SITE') : 1);
46         $current_site->domain = DOMAIN_CURRENT_SITE;
47         $current_site->path   = $path = PATH_CURRENT_SITE;
48         if( defined( 'BLOGID_CURRENT_SITE' ) )
49             $current_site->blog_id = BLOGID_CURRENT_SITE;
50         return $current_site;
51     }
52
53     $current_site = wp_cache_get( "current_site", "site-options" );
54     if( $current_site )
55         return $current_site;
56         
57     $wpdb->suppress_errors();
58     $sites = $wpdb->get_results( "SELECT * FROM $wpdb->site" ); // usually only one site
59     if( count( $sites ) == 1 ) {
60         $current_site = $sites[0];
61         $path = $current_site->path;
62         $current_site->blog_id = $wpdb->get_var( "SELECT blog_id FROM {$wpdb->blogs} WHERE domain='{$current_site->domain}' AND path='{$current_site->path}'" );
63         $current_site = get_current_site_name( $current_site );
64         wp_cache_set( "current_site", $current_site, "site-options" );
65         return $current_site;
66     }
67     $path = substr( $_SERVER[ 'REQUEST_URI' ], 0, 1 + strpos( $_SERVER[ 'REQUEST_URI' ], '/', 1 ) );
68     if( constant( 'VHOST' ) == 'yes' ) {
69         $current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE domain = %s AND path = %s", $domain, $path) );
70         if( $current_site != null )
71             return $current_site;
72         $current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE domain = %s AND path='/'", $domain) );
73         if( $current_site != null ) {
74             $path = '/';
75             return $current_site;
76         }
77
78         $sitedomain = substr( $domain, 1 + strpos( $domain, '.' ) );
79         $current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE domain = %s AND path = %s", $sitedomain, $path) );
80         if( $current_site != null )
81             return $current_site;
82         $current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE domain = %s AND path='/'", $sitedomain) );
83         if( $current_site == null && defined( "WP_INSTALLING" ) == false ) {
84             if( count( $sites ) == 1 ) {
85                 $current_site = $sites[0];
86                 die( "That blog does not exist. Please try <a href='http://{$current_site->domain}{$current_site->path}'>http://{$current_site->domain}{$current_site->path}</a>" );
87             } else {
88                 die( "No WPMU site defined on this host. If you are the owner of this site, please check <a href='http://codex.wordpress.org/Debugging_WPMU'>Debugging WPMU</a> for further assistance." );
89             }
90         } else {
91             $path = '/';
92         }
93     } else {
94         $current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE domain = %s AND path = %s", $domain, $path) );
95         if( $current_site != null )
96             return $current_site;
97         $current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE domain = %s AND path='/'", $domain) );
98         if( $current_site == null && defined( "WP_INSTALLING" ) == false ) {
99             if( count( $sites ) == 1 ) {
100                 $current_site = $sites[0];
101                 die( "That blog does not exist. Please try <a href='http://{$current_site->domain}{$current_site->path}'>http://{$current_site->domain}{$current_site->path}</a>" );
102             } else {
103                 die( "No WPMU site defined on this host. If you are the owner of this site, please check <a href='http://codex.wordpress.org/Debugging_WPMU'>Debugging WPMU</a> for further assistance." );
104             }
105         } else {
106             $path = '/';
107         }
108     }
109     return $current_site;
110 }
111
112 $current_site = wpmu_current_site();
113 if( !isset( $current_site->blog_id ) )
114     $current_site->blog_id = $wpdb->get_var( "SELECT blog_id FROM {$wpdb->blogs} WHERE domain='{$current_site->domain}' AND path='{$current_site->path}'" );
115
116 if( constant( 'VHOST' ) == 'yes' ) {
117     $current_blog = wp_cache_get( 'current_blog_' . $domain, 'site-options' );
118     if( !$current_blog ) {
119         $current_blog = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->blogs WHERE domain = %s", $domain) );
120         if( $current_blog )
121             wp_cache_set( 'current_blog_' . $domain, $current_blog, 'site-options' );
122     }
123     if( $current_blog != null && $current_blog->site_id != $current_site->id ) {
124         $current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE id = %d", $current_blog->site_id) );
125     } else {
126         $blogname = substr( $domain, 0, strpos( $domain, '.' ) );
127     }
128 } else {
129     $blogname = htmlspecialchars( substr( $_SERVER[ 'REQUEST_URI' ], strlen( $path ) ) );
130     if( strpos( $blogname, '/' ) )
131         $blogname = substr( $blogname, 0, strpos( $blogname, '/' ) );
132     if( strpos( " ".$blogname, '?' ) )
133         $blogname = substr( $blogname, 0, strpos( $blogname, '?' ) );
134     $reserved_blognames = array( 'page', 'comments', 'blog', 'wp-admin', 'wp-includes', 'wp-content', 'files', 'feed' );
135     if ( $blogname != '' && !in_array( $blogname, $reserved_blognames ) && !is_file( $blogname ) ) {
136         $path = $path . $blogname . '/';
137     }
138     $current_blog = wp_cache_get( 'current_blog_' . $domain . $path, 'site-options' );
139     if( !$current_blog ) {
140         $current_blog = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $domain, $path) );
141         if( $current_blog )
142             wp_cache_set( 'current_blog_' . $domain . $path, $current_blog, 'site-options' );
143     }
144 }
145
146 if( defined( "WP_INSTALLING" ) == false && constant( 'VHOST' ) == 'yes' && !is_object( $current_blog ) ) {
147     if( defined( 'NOBLOGREDIRECT' ) ) {
148         $destination = constant( 'NOBLOGREDIRECT' );
149         if ( $destination == '%siteurl%' )
150             $destination = "http://" . $current_site->domain . $current_site->path;
151         header( "Location: " $destination);
152         die();
153     } else {
154         if( constant( 'VHOST' ) == 'yes' ) {
155             header( "Location: http://" . $current_site->domain . $current_site->path . "wp-signup.php?new=" . str_replace( '.' . $current_site->domain, '', $domain ) );
156         } else {
157             header( "Location: http://" . $current_site->domain . $current_site->path . "wp-signup.php?new=" . str_replace( '/', '', $_SERVER[ 'REQUEST_URI' ] ) );
158         }
159         die();
160     }
161
162 }
163
164 if( defined( "WP_INSTALLING" ) == false ) {
165     if( $current_site && $current_blog == null ) {
166         if( $current_site->domain != $_SERVER[ 'HTTP_HOST' ] ) {
167             header( "Location: http://" . $current_site->domain . $current_site->path );
168             exit;
169         }
170         $current_blog = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $current_site->domain, $current_site->path) );
171     }
172     if( $current_blog == false || $current_site == false )
173         is_installed();
174 }
175
176 $blog_id = $current_blog->blog_id;
177 $public  = $current_blog->public;
178
179 if( $current_blog->site_id == 0 || $current_blog->site_id == '' )
180     $current_blog->site_id = 1;
181 $site_id = $current_blog->site_id;
182
183 $current_site = get_current_site_name( $current_site );
184
185 if( $blog_id == false ) {
186     // no blog found, are we installing? Check if the table exists.
187     if ( defined('WP_INSTALLING') ) {
188     $blog_id = $wpdb->get_var( "SELECT blog_id FROM $wpdb->blogs LIMIT 0,1" );
189     if( $blog_id == false ) {
190         // table doesn't exist. This is the first blog
191         $blog_id = 1;
192     } else {
193         // table exists
194         // don't create record at this stage. we're obviously installing so it doesn't matter what the table vars below are like.
195         // default to using the "main" blog.
196         $blog_id = 1;
197     }
198     $current_blog->blog_id = $blog_id;
199     } else {
200     $check = $wpdb->get_results( "SELECT * FROM $wpdb->site" );
201     if( $check == false ) {
202         $msg = ': DB Tables Missing';
203     } else {
204         $msg = '';
205     }
206     die( "No Blog by that name on this system." . $msg );
207     }
208 }
209
210 $wpdb->suppress_errors( false );
211
212 if( '0' == $current_blog->public ) {
213     // This just means the blog shouldn't show up in google, etc. Only to registered members
214 }
215
216 function is_installed() {
217     global $wpdb, $domain, $path;
218     $base = stripslashes( $base );
219     if( defined( "WP_INSTALLING" ) == false ) {
220         $check = $wpdb->get_results( "SELECT * FROM $wpdb->site" );
221         $msg = "If your blog does not display, please contact the owner of this site.<br /><br />If you are the owner of this site please check that MySQL is running properly and all tables are error free.<br /><br />";
222         if( $check == false ) {
223             $msg .= "<strong>Database Tables Missing.</strong><br />Database tables are missing. This means that MySQL is either not running, WPMU was not installed properly, or someone deleted {$wpdb->site}. You really <em>should</em> look at your database now.<br />";
224         } else {
225             $msg .= '<strong>Could Not Find Blog!</strong><br />';
226             $msg .= "Searched for <em>" . $domain . $path . "</em> in " . DB_NAME . "::" . $wpdb->blogs . " table. Is that right?<br />";
227         }
228         $msg .= "<br />\n<h1>What do I do now?</h1>";
229         $msg .= "Read the <a target='_blank' href='http://codex.wordpress.org/Debugging_WPMU'>bug report</a> page. Some of the guidelines there may help you figure out what went wrong.<br />";
230         $msg .= "If you're still stuck with this message, then check that your database contains the following tables:<ul>
231             <li> $wpdb->blogs </li>
232             <li> $wpdb->users </li>
233             <li> $wpdb->usermeta </li>
234             <li> $wpdb->site </li>
235             <li> $wpdb->sitemeta </li>
236             <li> $wpdb->sitecategories </li>
237             </ul>";
238         $msg .= "If you suspect a problem please report it to the support forums but you must include the information asked for in the <a href='http://codex.wordpress.org/Debugging_WPMU'>WPMU bug reporting guidelines</a>!<br /><br />";
239         if( is_file( 'release-info.txt' ) ) {
240             $msg .= 'Your bug report must include the following text: "';
241             $info = file( 'release-info.txt' );
242             $msg .= $info[ 4 ] . '"';
243         }
244
245         die( "<h1>Fatal Error</h1> " . $msg );
246     }
247 }
248
249 ?>
250
Note: See TracBrowser for help on using the browser.