root/trunk/wpmu-settings.php

Revision 1447, 9.7 kB (checked in by donncha, 2 weeks ago)

Prepare SQL, fixes #719, props momo360modena

Line 
1 <?php
2 if( $current_site && $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 wpmu_current_site() {
31     global $wpdb, $current_site, $domain, $path, $sites;
32     if( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) ) {
33         $current_site->id = 1;
34         $current_site->domain = DOMAIN_CURRENT_SITE;
35         $current_site->path   = PATH_CURRENT_SITE;
36         return $current_site;
37     }
38
39     $current_site = wp_cache_get( "current_site", "site-options" );
40     if( $current_site )
41         return $current_site;
42         
43     $wpdb->suppress_errors();
44     $sites = $wpdb->get_results( "SELECT * FROM $wpdb->site" ); // usually only one site
45     if( count( $sites ) == 1 ) {
46         $current_site = $sites[0];
47         $path = $current_site->path;
48         wp_cache_set( "current_site", $current_site, "site-options" );
49         return $current_site;
50     }
51     $path = substr( $_SERVER[ 'REQUEST_URI' ], 0, 1 + strpos( $_SERVER[ 'REQUEST_URI' ], '/', 1 ) );
52     if( constant( 'VHOST' ) == 'yes' ) {
53         $current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE domain = %s AND path = %s", $domain, $path) );
54         if( $current_site != null )
55             return $current_site;
56         $current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE domain = %s AND path='/'", $domain) );
57         if( $current_site != null ) {
58             $path = '/';
59             return $current_site;
60         }
61
62         $sitedomain = substr( $domain, 1 + strpos( $domain, '.' ) );
63         $current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE domain = %s AND path = %s", $sitedomain, $path) );
64         if( $current_site != null )
65             return $current_site;
66         $current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE domain = %s AND path='/'", $sitedomain) );
67         if( $current_site == null && defined( "WP_INSTALLING" ) == false ) {
68             if( count( $sites ) == 1 ) {
69                 $current_site = $sites[0];
70                 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>" );
71             } else {
72                 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." );
73             }
74         } else {
75             $path = '/';
76         }
77     } else {
78         $current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE domain = %s AND path = %s", $domain, $path) );
79         if( $current_site != null )
80             return $current_site;
81         $current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE domain = %s AND path='/'", $domain) );
82         if( $current_site == null && defined( "WP_INSTALLING" ) == false ) {
83             if( count( $sites ) == 1 ) {
84                 $current_site = $sites[0];
85                 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>" );
86             } else {
87                 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." );
88             }
89         } else {
90             $path = '/';
91         }
92     }
93     return $current_site;
94 }
95
96 $current_site = wpmu_current_site();
97
98 if( constant( 'VHOST' ) == 'yes' ) {
99     $current_blog = wp_cache_get( 'current_blog_' . $domain, 'site-options' );
100     if( !$current_blog ) {
101         $current_blog = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->blogs WHERE domain = %s", $domain) );
102         if( $current_blog )
103             wp_cache_set( 'current_blog_' . $domain, $current_blog, 'site-options' );
104     }
105     if( $current_blog != null && $current_blog->site_id != $current_site->id ) {
106         $current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE id = %d", $current_blog->site_id) );
107     } else {
108         $blogname = substr( $domain, 0, strpos( $domain, '.' ) );
109     }
110 } else {
111     $blogname = htmlspecialchars( substr( $_SERVER[ 'REQUEST_URI' ], strlen( $path ) ) );
112     if( strpos( $blogname, '/' ) )
113         $blogname = substr( $blogname, 0, strpos( $blogname, '/' ) );
114     if( strpos( " ".$blogname, '?' ) )
115         $blogname = substr( $blogname, 0, strpos( $blogname, '?' ) );
116     $blognames = array( 'page', 'comments', 'blog', 'wp-admin', 'wp-includes', 'wp-content', 'files', 'feed' );
117     if( $blogname == '' || in_array( $blogname, $blognames ) || is_file( $blogname ) || is_blogname_page( $blogname ) ) {
118         $current_blog = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $domain, $path) );
119     } else {
120         $current_blog = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $domain, $path.$blogname.'/') );
121     }
122 }
123
124 if( defined( "WP_INSTALLING" ) == false ) {
125     if( $current_site && $current_blog == null ) {
126         $current_blog = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $current_site->domain, $current_site->path) );
127     }
128     if( $current_blog == false || $current_site == false )
129         is_installed();
130 }
131
132 function is_blogname_page( $blogname ) {
133     global $wpdb, $table_prefix, $domain, $path;
134
135     $blog_id = $wpdb->get_var( $wpdb->prepare("SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s", $domain, $path) );
136
137     // is the request for a page of the main blog? We need to cache this information somewhere to save a request
138     $pages = $wpdb->get_col( "SELECT LOWER(post_name) FROM {$table_prefix}{$blog_id}_posts WHERE post_type='page'" );
139
140     if( is_array( $pages ) == false )
141         return false;
142
143     if( in_array( strtolower( $blogname ), $pages ) ) {
144         return true;
145     } else {
146         return false;
147     }
148 }
149
150 $blog_id = $current_blog->blog_id;
151 $public  = $current_blog->public;
152
153 if( $current_blog->site_id == 0 || $current_blog->site_id == '' )
154     $current_blog->site_id = 1;
155 $site_id = $current_blog->site_id;
156
157
158 $current_site->site_name = $wpdb->get_var( $wpdb->prepare("SELECT meta_value FROM $wpdb->sitemeta WHERE site_id = %d AND meta_key = 'site_name'", $site_id) );
159 if( $current_site->site_name == null )
160     $current_site->site_name = ucfirst( $current_site->domain );
161
162 if( $blog_id == false ) {
163     // no blog found, are we installing? Check if the table exists.
164     if ( defined('WP_INSTALLING') ) {
165     $blog_id = $wpdb->get_var( "SELECT blog_id FROM $wpdb->blogs LIMIT 0,1" );
166     if( $blog_id == false ) {
167         // table doesn't exist. This is the first blog
168         $blog_id = 1;
169     } else {
170         // table exists
171         // don't create record at this stage. we're obviously installing so it doesn't matter what the table vars below are like.
172         // default to using the "main" blog.
173         $blog_id = 1;
174     }
175     $current_blog->blog_id = $blog_id;
176     } else {
177     $check = $wpdb->get_results( "SELECT * FROM $wpdb->site" );
178     if( $check == false ) {
179         $msg = ': DB Tables Missing';
180     } else {
181         $msg = '';
182     }
183     die( "No Blog by that name on this system." . $msg );
184     }
185 }
186
187 $wpdb->suppress_errors( false );
188
189 if( '0' == $current_blog->public ) {
190     // This just means the blog shouldn't show up in google, etc. Only to registered members
191 }
192
193 function is_installed() {
194     global $wpdb, $domain, $path;
195     $base = stripslashes( $base );
196     if( defined( "WP_INSTALLING" ) == false ) {
197         $check = $wpdb->get_results( "SELECT * FROM $wpdb->site" );
198         $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 />";
199         if( $check == false ) {
200             $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 />";
201         } else {
202             $msg .= '<strong>Could Not Find Blog!</strong><br />';
203             $msg .= "Searched for <em>" . $domain . $path . "</em> in " . DB_NAME . "::" . $wpdb->blogs . " table. Is that right?<br />";
204         }
205         $msg .= "<br />\n<h1>What do I do now?</h1>";
206         $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 />";
207         $msg .= "If you're still stuck with this message, then check that your database contains the following tables:<ul>
208             <li> $wpdb->blogs </li>
209             <li> $wpdb->users </li>
210             <li> $wpdb->usermeta </li>
211             <li> $wpdb->site </li>
212             <li> $wpdb->sitemeta </li>
213             <li> $wpdb->sitecategories </li>
214             </ul>";
215         $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 />";
216         if( is_file( 'release-info.txt' ) ) {
217             $msg .= 'Your bug report must include the following text: "';
218             $info = file( 'release-info.txt' );
219             $msg .= $info[ 4 ] . '"';
220         }
221
222         die( "<h1>Fatal Error</h1> " . $msg );
223     }
224 }
225
226 ?>
227
Note: See TracBrowser for help on using the browser.