root/tags/1.5.1/wpmu-settings.php

Revision 1274, 8.8 kB (checked in by donncha, 7 months ago)

Suppress instead of "hide" db errors

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