root/tags/1.2.4/wp-includes/wpmu-functions.php

Revision 1036, 52.0 kB (checked in by donncha, 1 year ago)

No more linkcategories
Make sure all fields are accounted for. props Vimm, fixes #389

  • Property svn:eol-style set to native
Line 
1 <?php
2 /*
3     Helper functions for WPMU
4 */
5 function load_muplugin_textdomain($domain, $path = false) {
6     $locale = get_locale();
7     if ( empty($locale) )
8         $locale = 'en_US';
9
10     if ( false === $path )
11         $path = MUPLUGINDIR;
12
13     $mofile = ABSPATH . "$path/$domain-$locale.mo";
14     load_textdomain($domain, $mofile);
15 }
16
17 function wpmu_update_blogs_date() {
18     global $wpdb;
19
20     $wpdb->query( "UPDATE {$wpdb->blogs} SET last_updated = NOW() WHERE  blog_id = '{$wpdb->blogid}'" );
21     refresh_blog_details( $wpdb->blogid );
22 }
23
24 add_action('delete_post', 'wpmu_update_blogs_date');
25 add_action('private_to_published', 'wpmu_update_blogs_date');
26 add_action('publish_phone', 'wpmu_update_blogs_date');
27 add_action('publish_post', 'wpmu_update_blogs_date');
28
29 /*
30   Determines if the available space defined by the admin has been exceeded by the user
31 */
32 /**
33  * Returns how much space is available (also shows a picture) for the current client blog, retrieving the value from the master blog 'main' option table
34  *
35  * @param string $action
36  * @return string
37  */
38 function wpmu_checkAvailableSpace($action) {
39     // Using the action.
40     // Set the action to 'not-writable' to block the upload
41
42     // Default space allowed is 10 MB
43     $spaceAllowed = get_site_option( "blog_upload_space" );
44     if( $spaceAllowed == false )
45         $spaceAllowed = 10;
46
47     $dirName = constant( "ABSPATH" ) . constant( "UPLOADS" );
48
49       $dir  = dir($dirName);
50        $size = 0;
51
52     while($file = $dir->read()) {
53         if ($file != '.' && $file != '..') {
54             if (is_dir($file)) {
55                $size += dirsize($dirName . '/' . $file);
56            } else {
57                $size += filesize($dirName . '/' . $file);
58            }
59        }
60     }
61     $dir->close();
62     $size = $size / 1024 / 1024;
63     $spaceAvailable = sprintf( "%2.2f", ( ($spaceAllowed-$size) ) );
64     echo sprintf(__('Space Available (<i>%2.2fMB</i>)'), $spaceAvailable);
65
66
67     if (($spaceAllowed-$size)>0) {
68         return $action;
69     } else {
70         // No space left
71         return 'not-writable';
72     }
73 }
74 add_filter('fileupload_init','wpmu_checkAvailableSpace');
75
76 if( defined( "WP_INSTALLING" ) == false ) {
77     header( "X-totalblogs: " . get_blog_count() );
78     header( "X-rootblog: http://" . $current_site->domain . $current_site->path );
79     header( "X-created-on: " . $current_blog->registered );
80
81     if( empty( $WPMU_date ) == false )
82         header( "X-wpmu-date: $WPMU_date" );
83 }
84
85
86 function get_blogaddress_by_id( $blog_id ) {
87     global $hostname, $domain, $base, $wpdb;
88
89     $bloginfo = get_blog_details( $blog_id, false ); // only get bare details!
90     return "http://" . $bloginfo->domain . $bloginfo->path;
91 }
92
93 function get_blogaddress_by_name( $blogname ) {
94     global $hostname, $domain, $base, $wpdb;
95
96     if( defined( "VHOST" ) && constant( "VHOST" ) == 'yes' ) {
97         if( $blogname == 'main' )
98             $blogname = 'www';
99         return "http://".$blogname.".".$domain.$base;
100     } else {
101         return "http://".$hostname.$base.$blogname;
102     }
103 }
104
105 function get_blogaddress_by_domain( $domain, $path ){
106     if( defined( "VHOST" ) && constant( "VHOST" ) == 'yes' ) {
107         $url = "http://".$domain.$path;
108     } else {
109         if( $domain != $_SERVER[ 'HTTP_HOST' ] ) {
110             $blogname = substr( $domain, 0, strpos( $domain, '.' ) );
111             if( $blogname != 'www.' ) {
112                 $url = 'http://' . substr( $domain, strpos( $domain, '.' ) + 1 ) . $path . $blogname . '/';
113             } else { // we're installing the main blog
114                 $url = 'http://' . substr( $domain, strpos( $domain, '.' ) + 1 ) . $path;
115             }
116         } else { // main blog
117             $url = 'http://' . $domain . $path;
118         }
119     }
120
121     return $url;
122 }
123
124 function get_sitestats() {
125     global $wpdb, $basedomain, $base;
126
127     $stats[ 'blogs' ] = get_blog_count();
128
129     $count_ts = get_site_option( "get_user_count_ts" );
130     if( time() - $count_ts > 3600 ) {
131         $count = $wpdb->get_var( "SELECT count(*) as c FROM {$wpdb->users}" );
132         update_site_option( "user_count", $count );
133         update_site_option( "user_count_ts", time() );
134     } else {
135         $count = get_site_option( "user_count" );
136     }
137     $stats[ 'users' ] = $count;
138
139     return $stats;
140 }
141
142 function get_admin_users_for_domain( $sitedomain = '', $path = '' ) {
143     global $domain, $base, $basedomain, $wpdb, $wpmuBaseTablePrefix;
144     if( $sitedomain == '' ) {
145         $sitedomain = $basedomain;
146         $path = $base;
147         $site_id = $wpdb->siteid;
148     } else {
149         $query = "SELECT id FROM ".$wpdb->site." WHERE domain = '".$sitedomain."' AND path = '".$path."'";
150         $site_id = $wpdb->get_var( $query );
151     }
152     if( $site_id != false ) {
153         $query = "SELECT ID, user_login, user_pass FROM ".$wpdb->users.", ".$wpdb->sitemeta." WHERE meta_key = 'admin_user_id' AND ".$wpdb->users.".ID = ".$wpdb->sitemeta.".meta_value AND ".$wpdb->sitemeta.".site_id = '".$site_id."'";
154         $details = $wpdb->get_results( $query, ARRAY_A );
155     } else {
156         $details = false;
157     }
158
159     return $details;
160 }
161
162 function get_user_details( $username ) {
163     global $wpdb;
164
165     return $wpdb->get_row( "SELECT * FROM $wpdb->users WHERE user_login = '$username'" );
166 }
167
168 function get_blog_details( $id, $all = true ) {
169     global $wpdb;
170
171     $details = wp_cache_get( $id, 'blog-details' );
172
173     if ( $details ) {
174         if ( $details == -1 )
175             return false;
176         elseif ( !is_object($details) ) // Clear old pre-serialized objects. Cache clients do better with that.
177             wp_cache_delete( $id, 'blog-details' );
178         else
179             return $details;
180     }
181
182     $details = $wpdb->get_row( "SELECT * FROM $wpdb->blogs WHERE blog_id = '$id' /* get_blog_details */" );
183
184     if ( !$details ) {
185         wp_cache_set( $id, -1, 'blog-details' );
186         return false;
187     }
188
189     if( $all == true ) {
190         $wpdb->hide_errors();
191         $details->blogname   = get_blog_option($id, 'blogname');
192         $details->siteurl    = get_blog_option($id, 'siteurl');
193         $details->post_count = get_blog_option($id, 'post_count');
194         $wpdb->show_errors();
195
196         wp_cache_add( $id, serialize( $details ), 'blog-details' );
197
198         $key = md5( $details->domain . $details->path );
199         wp_cache_add( $key, serialize( $details ), 'blog-lookup' );
200     }
201
202     return $details;
203 }
204
205 function refresh_blog_details( $id ) {
206     global $wpdb, $wpmuBaseTablePrefix;
207
208     $details = get_blog_details( $id );
209     wp_cache_delete( $id , 'blog-details' );
210
211     $key = md5( $details->domain . $details->path );
212     wp_cache_delete( $key , 'blog-lookup' );
213
214     //return $details;
215 }
216
217 function get_current_user_id() {
218     global $current_user;
219     return $current_user->ID;
220 }
221
222 function is_site_admin( $user_login = false ) {
223     global $wpdb, $current_user;
224
225     if ( !$current_user && !$user_login )
226         return false;
227
228     if ( $user_login )
229         $user_login = sanitize_user( $user_login );
230     else
231         $user_login = $current_user->user_login;
232
233     $site_admins = get_site_option( 'site_admins', array('admin') );
234     if( is_array( $site_admins ) && in_array( $user_login, $site_admins ) )
235         return true;
236
237     return false;
238 }
239
240 // expects key not to be SQL escaped
241 function get_site_option( $key, $default = false, $use_cache = true ) {
242     global $wpdb;
243
244     $safe_key = $wpdb->escape( $key );
245
246     if( $use_cache == true ) {
247         $value = wp_cache_get($wpdb->siteid . $key, 'site-options');
248     } else {
249         $value = false;
250     }
251
252     if ( false === $value ) {
253         $value = $wpdb->get_var("SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = '$safe_key' AND site_id = '{$wpdb->siteid}'");
254         if ( ! is_null($value) ) {
255             wp_cache_add($wpdb->siteid . $key, $value, 'site-options');
256         } elseif ( $default ) {
257             wp_cache_add($wpdb->siteid . $key, addslashes( $default ), 'site-options');
258             return $default;
259         } else {
260             wp_cache_add($wpdb->siteid . $key, false, 'site-options');
261             return false;
262         }
263     }
264
265     $value = stripslashes( $value );
266     @ $kellogs = unserialize($value);
267     if ( $kellogs !== FALSE )
268         return $kellogs;
269     else
270         return $value;
271 }
272
273 // expects $key, $value not to be SQL escaped
274 function add_site_option( $key, $value ) {
275     global $wpdb;
276
277     $safe_key = $wpdb->escape( $key );
278
279     $exists = $wpdb->get_row("SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = '$safe_key' AND site_id = '{$wpdb->siteid}'");
280
281     if ( null !== $exists ) {// If we already have it
282         update_site_option( $key, $value );
283         return false;
284     }
285
286     if ( is_array($value) || is_object($value) )
287         $value = serialize($value);
288     wp_cache_delete($wpdb->siteid . $key, 'site-options');
289     $wpdb->query( "INSERT INTO $wpdb->sitemeta ( site_id , meta_key , meta_value ) VALUES ( '{$wpdb->siteid}', '$safe_key', '" . $wpdb->escape( $value ) . "')" );
290     return $wpdb->insert_id;
291 }
292
293 // expects $key, $value not to be SQL escaped
294 function update_site_option( $key, $value ) {
295     global $wpdb;
296
297     if ( $value == get_site_option( $key ) )
298          return;
299
300     if ( get_site_option( $key, false, false ) === false )
301         add_site_option( $key, $value );
302
303     if ( is_array($value) || is_object($value) )
304         $value = serialize($value);
305
306     $safe_key = $wpdb->escape( $key );
307
308     $wpdb->query( "UPDATE $wpdb->sitemeta SET meta_value = '" . $wpdb->escape( $value ) . "' WHERE site_id='{$wpdb->siteid}' AND meta_key = '$safe_key'" );
309     wp_cache_delete( $wpdb->siteid . $key, 'site-options' );
310 }
311
312 /*
313 function get_blog_option( $id, $key, $default='na' ) {
314     switch_to_blog($id);
315     $opt = get_option( $key );
316     restore_current_blog();
317
318     return $opt;
319 }
320 */
321
322 function get_blog_option( $blog_id, $setting, $default='na' ) {
323     global $wpdb, $wpmuBaseTablePrefix;
324
325     $key = $blog_id."-".$setting."-blog_option";
326     $value = wp_cache_get( $key, "site-options" );
327     if( $value == null ) {
328         $row = $wpdb->get_row( "SELECT * FROM {$wpmuBaseTablePrefix}{$blog_id}_options WHERE option_name = '{$setting}'" );
329         if( is_object( $row) ) { // Has to be get_row instead of get_var because of funkiness with 0, false, null values
330             $value = $row->option_value;
331             if( $value == false )
332                 $value = 'falsevalue';
333             wp_cache_set($key, $value, 'site-options');
334         } else { // option does not exist, so we must cache its non-existence
335             wp_cache_set($key, 'noop', 'site-options');
336         }
337     } elseif( $value == 'noop' ) {
338         return false;
339     } elseif( $value == 'falsevalue' ) {
340         return false;
341     }
342     // If home is not set use siteurl.
343     if ( 'home' == $setting && '' == $value )
344         return get_blog_option($blog_id, 'siteurl');
345
346     if ( 'siteurl' == $setting || 'home' == $setting || 'category_base' == $setting )
347         $value = preg_replace('|/+$|', '', $value);
348
349     if (! unserialize($value) )
350         $value = stripslashes( $value );
351
352     return apply_filters( 'option_' . $setting, maybe_unserialize($value) );
353 }
354
355 function add_blog_option( $id, $key, $value ) {
356     switch_to_blog($id);
357     add_option( $key, $value );
358     restore_current_blog();
359     $opt = $id."-".$key."-blog_option";
360     wp_cache_set($opt, $value, 'site-options');
361 }
362
363 function delete_blog_option( $id, $key ) {
364     switch_to_blog($id);
365     delete_option( $key );
366     restore_current_blog();
367     $opt = $id."-".$key."-blog_option";
368     wp_cache_set($opt, '', 'site-options');
369 }
370
371 function update_blog_option( $id, $key, $value, $refresh = true ) {
372     switch_to_blog($id);
373     $opt = update_option( $key, $value );
374     restore_current_blog();
375     if( $refresh == true )
376         refresh_blog_details( $id );
377     $opt = $id."-".$key."-blog_option";
378     wp_cache_set($opt, $value, 'site-options');
379 }
380
381 function switch_to_blog( $new_blog ) {
382     global $tmpoldblogdetails, $wpdb, $wpmuBaseTablePrefix, $table_prefix, $blog_id, $switched, $switched_stack, $wp_roles, $current_user;
383
384     if ( empty($new_blog) )
385         $new_blog = $blog_id;
386
387     if ( empty($switched_stack) )
388         $switched_stack = array();
389
390     $switched_stack[] = $blog_id;
391
392     // backup
393     $tmpoldblogdetails[ 'blogid' ]         = $wpdb->blogid;
394     $tmpoldblogdetails[ 'posts' ]          = $wpdb->posts;
395     $tmpoldblogdetails[ 'categories' ]     = $wpdb->categories;
396     $tmpoldblogdetails[ 'post2cat' ]       = $wpdb->post2cat;
397     $tmpoldblogdetails[ 'comments' ]       = $wpdb->comments;
398     $tmpoldblogdetails[ 'links' ]          = $wpdb->links;
399     $tmpoldblogdetails[ 'link2cat' ]       = $wpdb->link2cat;
400     $tmpoldblogdetails[ 'linkcategories' ] = $wpdb->linkcategories;
401     $tmpoldblogdetails[ 'options' ]         = $wpdb->options;
402     $tmpoldblogdetails[ 'postmeta' ]       = $wpdb->postmeta;
403     $tmpoldblogdetails[ 'prefix' ]         = $wpdb->prefix;
404     $tmpoldblogdetails[ 'table_prefix' ] = $table_prefix;
405     $tmpoldblogdetails[ 'blog_id' ] = $blog_id;
406
407     // fix the new prefix.
408     $table_prefix = $wpmuBaseTablePrefix . $new_blog . "_";
409     $wpdb->prefix            = $table_prefix;
410     $wpdb->blogid           = $new_blog;
411     $wpdb->posts            = $table_prefix . 'posts';
412     $wpdb->categories       = $table_prefix . 'categories';
413     $wpdb->post2cat         = $table_prefix . 'post2cat';
414     $wpdb->comments         = $table_prefix . 'comments';
415     $wpdb->links            = $table_prefix . 'links';
416     $wpdb->link2cat         = $table_prefix . 'link2cat';
417     $wpdb->linkcategories   = $table_prefix . 'linkcategories';
418     $wpdb->options          = $table_prefix . 'options';
419     $wpdb->postmeta         = $table_prefix . 'postmeta';
420     $blog_id = $new_blog;
421
422     if( is_object( $wp_roles ) ) {
423         $wpdb->hide_errors();
424         $wp_roles->_init();
425         $wpdb->show_errors();
426     }
427     if ( is_object( $current_user ) ) {
428         $current_user->_init_caps();
429     }
430
431     do_action('switch_blog', $blog_id, $tmpoldblogdetails[ 'blog_id' ]);
432
433     $switched = true;
434 }
435
436 function restore_current_blog() {
437     global $table_prefix, $tmpoldblogdetails, $wpdb, $wpmuBaseTablePrefix, $blog_id, $switched, $switched_stack, $wp_roles, $current_user;
438
439     if ( !$switched )
440         return;
441
442     $blog = array_pop($switched_stack);
443
444     if ( $blog_id == $blog )
445         return;
446
447     // backup
448     $wpdb->blogid = $tmpoldblogdetails[ 'blogid' ];
449     $wpdb->posts = $tmpoldblogdetails[ 'posts' ];
450     $wpdb->categories = $tmpoldblogdetails[ 'categories' ];
451     $wpdb->post2cat = $tmpoldblogdetails[ 'post2cat' ];
452     $wpdb->comments = $tmpoldblogdetails[ 'comments' ];
453     $wpdb->links = $tmpoldblogdetails[ 'links' ];
454     $wpdb->link2cat = $tmpoldblogdetails[ 'link2cat' ];
455     $wpdb->linkcategories = $tmpoldblogdetails[ 'linkcategories' ];
456     $wpdb->options = $tmpoldblogdetails[ 'options' ];
457     $wpdb->postmeta = $tmpoldblogdetails[ 'postmeta' ];
458     $wpdb->prefix = $tmpoldblogdetails[ 'prefix' ];
459     $table_prefix = $tmpoldblogdetails[ 'table_prefix' ];
460     $prev_blog_id = $blog_id;
461     $blog_id = $tmpoldblogdetails[ 'blog_id' ];
462     unset( $tmpoldblogdetails );
463
464     if( is_object( $wp_roles ) ) {
465         $wpdb->hide_errors();
466         $wp_roles->_init();
467         $wpdb->show_errors();
468     }
469     if ( is_object( $current_user ) ) {
470         $current_user->_init_caps();
471     }
472     do_action('switch_blog', $blog_id, $prev_blog_id);