root/tags/1.3/wp-admin/includes/mu.php

Revision 1137, 15.8 kB (checked in by donncha, 1 year ago)

added new "pre_update_term" filter to wp_update_term()
Added "sync_slugs" function to make slugs = sanitized name

Line 
1 <?php
2
3 function check_upload_size( $file ) {
4
5     if( $file[ 'error' ] != '0' ) // there's already an error
6         return $file;
7
8     $space_allowed = 1048576*get_space_allowed();
9     $space_used = get_dirsize( constant( "ABSPATH" ) . constant( "UPLOADS" ) );
10     $space_left = $space_allowed - $space_used;
11     $file_size = filesize( $file[ 'tmp_name' ]);
12     if( $space_left < $file_size )
13         $file[ 'error' ] = sprintf( __( 'Not enough space to upload. %1$sKb needed.' ), number_format( ($file_size - $space_left) /1024 ) );
14     if( $file_size > ( 1024 * get_site_option( 'fileupload_maxk', 1500 ) ) )
15         $file['error'] = sprintf(__('This file is too big. Files must be less than %1$s Kb in size.'), get_site_option( 'fileupload_maxk', 1500 ) );
16     if( upload_is_user_over_quota( false ) ) {
17         $file['error'] = __('You have used your space quota. Please delete files before uploading.');
18     }
19     if( $file[ 'error' ] != '0' )
20         wp_die( $file[ 'error' ] . ' <a href="javascript:history.go(-1)">' . __( 'Back' ) . '</a>' );
21
22     return $file;
23
24 }
25 add_filter( 'wp_handle_upload_prefilter', 'check_upload_size' );
26
27 function wpmu_delete_blog($blog_id, $drop = false) {
28     global $wpdb;
29
30     if ( $blog_id != $wpdb->blogid ) {
31         $switch = true;
32         switch_to_blog($blog_id);   
33     }
34
35     do_action('delete_blog', $blog_id, $drop);
36
37     $users = get_users_of_blog($blog_id);
38
39     // Remove users from this blog.
40     if ( !empty($users) ) foreach ($users as $user) {
41         remove_user_from_blog($user->user_id, $blog_id);
42     }
43
44     update_blog_status( $blog_id, 'deleted', 1 );
45
46     if ( $drop ) {
47         $drop_tables = array( $wpdb->base_prefix . $blog_id . "_categories",
48         $wpdb->base_prefix . $blog_id . "_comments",
49         $wpdb->base_prefix . $blog_id . "_linkcategories",
50         $wpdb->base_prefix . $blog_id . "_links",
51         $wpdb->base_prefix . $blog_id . "_link2cat",
52         $wpdb->base_prefix . $blog_id . "_options",
53         $wpdb->base_prefix . $blog_id . "_post2cat",
54         $wpdb->base_prefix . $blog_id . "_postmeta",
55         $wpdb->base_prefix . $blog_id . "_posts",
56         $wpdb->base_prefix . $blog_id . "_referer_visitLog",
57         $wpdb->base_prefix . $blog_id . "_referer_blacklist" );
58         reset( $drop_tables );
59
60         foreach ( (array) $drop_tables as $drop_table) {
61             $wpdb->query( "DROP TABLE IF EXISTS $drop_table" );
62         }
63
64         $wpdb->query( "DELETE FROM $wpdb->blogs WHERE blog_id = '$blog_id'" );
65         $dir = constant( "ABSPATH" ) . "wp-content/blogs.dir/" . $blog_id ."/files/";
66         $dir = rtrim($dir, DIRECTORY_SEPARATOR);
67         $top_dir = $dir;
68         $stack = array($dir);
69         $index = 0;
70
71         while ($index < count($stack)) {
72             # Get indexed directory from stack
73             $dir = $stack[$index];
74
75             $dh = @ opendir($dir);
76             if ($dh) {
77                 while (($file = @ readdir($dh)) !== false) {
78                     if ($file == '.' or $file == '..')
79                         continue;
80
81                     if (@ is_dir($dir . DIRECTORY_SEPARATOR . $file))
82                         $stack[] = $dir . DIRECTORY_SEPARATOR . $file;
83                     else if (@ is_file($dir . DIRECTORY_SEPARATOR . $file))
84                         @ unlink($dir . DIRECTORY_SEPARATOR . $file);
85                 }
86             }
87             $index++;
88         }
89
90         $stack = array_reverse($stack);  // Last added dirs are deepest
91         foreach( (array) $stack as $dir) {
92             if ( $dir != $top_dir)
93             @rmdir($dir);
94         }
95     }
96     $wpdb->query("DELETE FROM {$wpdb->usermeta} WHERE meta_key='wp_{$blog_id}_autosave_draft_ids'");
97
98     if ( $switch )
99         restore_current_blog();
100 }
101
102 function update_blog_public($old_value, $value) {
103     global $wpdb;
104     do_action('update_blog_public');
105     update_blog_status( $wpdb->blogid, 'public', (int) $value );
106 }
107 add_action('update_option_blog_public', 'update_blog_public', 10, 2);
108
109 function wpmu_delete_user($id) {
110     global $wpdb;
111
112     $id = (int) $id;
113     $user = get_userdata($id);
114
115     do_action('wpmu_delete_user', $id);
116
117     $blogs = get_blogs_of_user($id);
118
119     if ( ! empty($blogs) )
120         foreach ($blogs as $blog) {
121             switch_to_blog($blog->userblog_id);
122             remove_user_from_blog($id, $blog->userblog_id);
123
124             $post_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_author = $id");
125             foreach ( (array) $post_ids as $post_id )
126                 wp_delete_post($post_id);
127
128             // Clean links
129             $wpdb->query("DELETE FROM $wpdb->links WHERE link_owner = $id");
130
131             restore_current_blog();
132         }
133
134     $wpdb->query("DELETE FROM $wpdb->users WHERE ID = $id");
135     $wpdb->query("DELETE FROM $wpdb->usermeta WHERE user_id = '$id'");
136
137     wp_cache_delete($id, 'users');
138     wp_cache_delete($user->user_login, 'userlogins');
139
140     return true;
141 }
142
143 function wpmu_get_blog_allowedthemes( $blog_id = 0 ) {
144     $themes = get_themes();
145     if( $blog_id == 0 )
146         $blog_allowed_themes = get_option( "allowedthemes" );
147     else
148         $blog_allowed_themes = get_blog_option( $blog_id, "allowedthemes" );
149     if( !is_array( $blog_allowed_themes ) || empty( $blog_allowed_themes ) ) { // convert old allowed_themes to new allowedthemes
150         if( $blog_id == 0 )
151             $blog_allowed_themes = get_option( "allowed_themes" );
152         else
153             $blog_allowed_themes = get_blog_option( $blog_id, "allowed_themes" );
154             
155         if( is_array( $blog_allowed_themes ) ) {
156             foreach( (array) $themes as $key => $theme ) {
157                 $theme_key = wp_specialchars( $theme[ 'Stylesheet' ] );
158                 if( isset( $blog_allowed_themes[ $key ] ) == true ) {
159                     $blog_allowedthemes[ $theme_key ] = 1;
160                 }
161             }
162             $blog_allowed_themes = $blog_allowedthemes;
163             if( $blog_id == 0 ) {
164                 add_option( "allowedthemes", $blog_allowed_themes );
165                 delete_option( "allowed_themes" );
166             } else {
167                 add_blog_option( $blog_id, "allowedthemes", $blog_allowed_themes );
168                 delete_blog_option( $blog_id, "allowed_themes" );
169             }
170         }
171     }
172     return $blog_allowed_themes;
173 }
174
175 function update_option_new_admin_email($old_value, $value) {
176     if ( $value == get_option( 'admin_email' ) || !is_email( $value ) )
177         return;
178
179     $hash = md5( $value.time().mt_rand() );
180     $newadminemail = array(
181         "hash" => $hash,
182         "newemail" => $value
183     );
184     update_option( 'adminhash', $newadminemail );
185     
186     $content = __("Dear user,\n\n
187 You recently requested to have the administration email address on
188 your blog changed.\n
189 If this is correct, please click on the following link to change it:\n
190 ###ADMIN_URL###\n\n
191 You can safely ignore and delete this email if you do not want to take this action.\n\n
192 This email has been sent to ###EMAIL###\n\n
193 Regards,\n
194 The Webmaster");
195     
196     $content = str_replace('###ADMIN_URL###', get_option( "siteurl" ).'/wp-admin/options.php?adminhash='.$hash, $content);
197     $content = str_replace('###EMAIL###', $value, $content);
198     
199     wp_mail( $value, sprintf(__('[%s] New Admin Email Address'), get_option('blogname')), $content );
200 }
201 add_action('update_option_new_admin_email', 'update_option_new_admin_email', 10, 2);
202
203 function get_site_allowed_themes() {
204     $themes = get_themes();
205     $allowed_themes = get_site_option( 'allowedthemes' );
206     if( !is_array( $allowed_themes ) || empty( $allowed_themes ) ) {
207         $allowed_themes = get_site_option( "allowed_themes" ); // convert old allowed_themes format
208         if( !is_array( $allowed_themes ) ) {
209             $allowed_themes = array();
210         } else {
211             foreach( (array) $themes as $key => $theme ) {
212                 $theme_key = wp_specialchars( $theme[ 'Stylesheet' ] );
213                 if( isset( $allowed_themes[ $key ] ) == true ) {
214                     $allowedthemes[ $theme_key ] = 1;
215                 }
216             }
217             $allowed_themes = $allowedthemes;
218         }
219     }
220     return $allowed_themes;
221 }
222
223 function get_space_allowed() {
224     $spaceAllowed = get_option("blog_upload_space");
225     if( $spaceAllowed == false )
226         $spaceAllowed = get_site_option("blog_upload_space");
227     if( empty($spaceAllowed) || !is_numeric($spaceAllowed) )
228         $spaceAllowed = 50;
229
230     return $spaceAllowed;
231 }
232
233 function display_space_usage() {
234     $space = get_space_allowed();
235     $used = get_dirsize( constant( "ABSPATH" ) . constant( "UPLOADS" ) )/1024/1024;
236
237     if ($used > $space) $percentused = '100';
238     else $percentused = ( $used / $space ) * 100;
239
240     if( $space > 1000 ) {
241         $space = number_format( $space / 1024 );
242         $space .= __('GB');
243     } else {
244         $space .= __('MB');
245     }
246     ?>
247     <strong><?php printf(__('Used: %1s%% of %2s'), number_format($percentused), $space );?></strong>
248     <?php
249 }
250
251 // Display File upload quota on dashboard
252 function dashboard_quota() {   
253     $quota = get_space_allowed();
254     $used = get_dirsize( constant( "ABSPATH" ) . constant( "UPLOADS" ) )/1024/1024;
255
256     if ($used > $quota) $percentused = '100';
257     else $percentused = ( $used / $quota ) * 100;
258
259     ?>
260     <div id='spaceused'>
261         <h3><?php _e("Storage Space <a href='upload.php' title='Manage Uploads...'>&raquo;</a>"); ?></h3>
262         <p><?php _e('Total space available:'); ?> <strong><?php echo $quota . __('MB'); ?></strong></p>   
263         <p><?php _e('Upload space used:');     
264         ?>
265         <strong><?php printf(__('%1sMB (%2s%%)'), round($used,2), number_format($percentused) ); ?></strong></p>
266     </div>
267     <?php
268 }
269 if( current_user_can('edit_posts') )
270     add_action('activity_box_end', 'dashboard_quota');
271
272 // Edit blog upload space setting on Edit Blog page
273 function upload_space_setting( $id ) {
274     $quota = get_blog_option($id, "blog_upload_space");
275     if( !$quota )
276         $quota = '';
277     
278     ?>
279     <strong><?php _e('Blog Upload Space Quota'); ?></strong>
280     <input type="text" size="3" name="option[blog_upload_space]" value="<?php echo $quota; ?>" /><?php _e('MB (Leave blank for site default)'); ?><br />
281     <?php
282 }
283 add_action('wpmueditblogaction', 'upload_space_setting');
284
285 // Edit XMLRPC active setting on Edit Blog page
286 function xmlrpc_active_setting( $id ) {
287     $site_xmlrpc = get_site_option( 'xmlrpc_active' );
288     $xmlrpc_active = get_blog_option($id, 'xmlrpc_active');
289     
290     if( $site_xmlrpc == 'yes' ) { ?>
291         <p><strong><?php _e('XMLRPC Posting is enabled sitewide.'); ?></strong></p>
292     <?php } else { ?>
293         <p><strong><?php _e('XMLRPC Posting is disabled sitewide.'); ?></strong></p>
294     <?php } ?>
295    
296     <input type='radio' name='option[xmlrpc_active]' value='' <?php if( !$xmlrpc_active || $xmlrpc_active == '' ) echo "checked='checked'"; ?> /> <?php _e('Do nothing, accept sitewide default'); ?><br />
297     <input type='radio' name='option[xmlrpc_active]' value='yes' <?php if( $xmlrpc_active == "yes" ) echo "checked='checked'"; ?> /> <?php _e('XMLRPC always on for this blog'); ?><br />
298     <input type='radio' name='option[xmlrpc_active]' value='no' <?php if( $xmlrpc_active == "no" ) echo "checked='checked'"; ?> /> <?php _e('XMLRPC always off for this blog'); ?><br />
299     <?php
300 }
301 add_action('wpmueditblogaction', 'xmlrpc_active_setting');
302
303 function update_user_status( $id, $pref, $value, $refresh = 1 ) {
304     global $wpdb;
305
306     $wpdb->query( "UPDATE {$wpdb->users} SET {$pref} = '{$value}' WHERE ID = '$id'" );
307
308     if( $refresh == 1 )
309         refresh_user_details($id);
310     
311     if( $pref == 'spam' ) {
312         if( $value == 1 )
313             do_action( "make_spam_user", $id );
314         else
315             do_action( "make_ham_user", $id );
316     }
317
318     return $value;
319 }
320
321 function refresh_user_details($id) {
322     $id = (int) $id;
323     
324     if ( !$user = get_userdata( $id ) )
325         return false;
326
327     wp_cache_delete($id, 'users');
328     wp_cache_delete($user->user_login, 'userlogins');
329     return $id;
330 }
331
332 /*
333   Determines if the available space defined by the admin has been exceeded by the user
334 */
335 function wpmu_checkAvailableSpace() {
336     $spaceAllowed = get_space_allowed();
337
338     $dirName = trailingslashit( constant( "ABSPATH" ) . constant( "UPLOADS" ) );
339     if (!(is_dir($dirName) && is_readable($dirName)))
340         return;
341
342       $dir = dir($dirName);
343        $size = 0;
344
345     while($file = $dir->read()) {
346         if ($file != '.' && $file != '..') {
347             if (is_dir( $dirName . $file)) {
348                 $size += get_dirsize($dirName . $file);
349             } else {
350                 $size += filesize($dirName . $file);
351             }
352         }
353     }
354     $dir->close();
355     $size = $size / 1024 / 1024;
356
357     if( ($spaceAllowed - $size) <= 0 ) {
358         define( 'DISABLE_UPLOADS', true );
359         define( 'DISABLE_UPLOADS_MESSAGE', __('Sorry, you must delete files before you can upload any more.') );
360     }
361 }
362 add_action('upload_files_upload','wpmu_checkAvailableSpace');
363
364 function format_code_lang( $code = '' ) {
365     $code = strtolower(substr($code, 0, 2));
366     $lang_codes = array('aa' => 'Afar''ab' => 'Abkhazian''af' => 'Afrikaans''ak' => 'Akan''sq' => 'Albanian''am' => 'Amharic''ar' => 'Arabic''an' => 'Aragonese''hy' => 'Armenian''as' => 'Assamese''av' => 'Avaric''ae' => 'Avestan''ay' => 'Aymara''az' => 'Azerbaijani''ba' => 'Bashkir''bm' => 'Bambara''eu' => 'Basque''be' => 'Belarusian''bn' => 'Bengali''bh' => 'Bihari''bi' => 'Bislama''bs' => 'Bosnian''br' => 'Breton''bg' => 'Bulgarian''my' => 'Burmese''ca' => 'Catalan; Valencian''ch' => 'Chamorro''ce' => 'Chechen''zh' => 'Chinese''cu' => 'Church Slavic; Old Slavonic; Church Slavonic; Old Bulgarian; Old Church Slavonic''cv' => 'Chuvash''kw' => 'Cornish''co' => 'Corsican''cr' => 'Cree''cs' => 'Czech''da' => 'Danish''dv' => 'Divehi; Dhivehi; Maldivian''nl' => 'Dutch; Flemish''dz' => 'Dzongkha''en' => 'English''eo' => 'Esperanto''et' => 'Estonian''ee' => 'Ewe''fo' => 'Faroese''fj' => 'Fijian''fi' => 'Finnish''fr' => 'French''fy' => 'Western Frisian''ff' => 'Fulah''ka' => 'Georgian''de' => 'German''gd' => 'Gaelic; Scottish Gaelic''ga' => 'Irish''gl' => 'Galician''gv' => 'Manx''el' => 'Greek, Modern''gn' => 'Guarani''gu' => 'Gujarati''ht' => 'Haitian; Haitian Creole''ha' => 'Hausa''he' => 'Hebrew''hz' => 'Herero''hi' => 'Hindi''ho' => 'Hiri Motu''hu' => 'Hungarian''ig' => 'Igbo''is' => 'Icelandic''io' => 'Ido''ii' => 'Sichuan Yi''iu' => 'Inuktitut''ie' => 'Interlingue''ia' => 'Interlingua (International Auxiliary Language Association)''id' => 'Indonesian''ik' => 'Inupiaq''it' => 'Italian''jv' => 'Javanese''ja' => 'Japanese''kl' => 'Kalaallisut; Greenlandic''kn' => 'Kannada''ks' => 'Kashmiri''kr' => 'Kanuri''kk' => 'Kazakh''km' => 'Central Khmer''ki' => 'Kikuyu; Gikuyu''rw' => 'Kinyarwanda''ky' => 'Kirghiz; Kyrgyz''kv' => 'Komi''kg' => 'Kongo''ko' => 'Korean''kj' => 'Kuanyama; Kwanyama''ku' => 'Kurdish''lo' => 'Lao''la' => 'Latin''lv' => 'Latvian''li' => 'Limburgan; Limburger; Limburgish''ln' => 'Lingala''lt' => 'Lithuanian''lb' => 'Luxembourgish; Letzeburgesch''lu' => 'Luba-Katanga''lg' => 'Ganda''mk' => 'Macedonian''mh' => 'Marshallese''ml' => 'Malayalam''mi' => 'Maori''mr' => 'Marathi''ms' => 'Malay''mg' => 'Malagasy''mt' => 'Maltese''mo' => 'Moldavian''mn' => 'Mongolian''na' => 'Nauru''nv' => 'Navajo; Navaho''nr' => 'Ndebele, South; South Ndebele''nd' => 'Ndebele, North; North Ndebele''ng' => 'Ndonga''ne' => 'Nepali''nn' => 'Norwegian Nynorsk; Nynorsk, Norwegian''nb' => 'Bokmål, Norwegian, Norwegian Bokmål''no' => 'Norwegian''ny' => 'Chichewa; Chewa; Nyanja''oc' => 'Occitan, Provençal''oj' => 'Ojibwa''or' => 'Oriya''om' => 'Oromo''os' => 'Ossetian; Ossetic''pa' => 'Panjabi; Punjabi''fa' => 'Persian''pi' => 'Pali''pl' => 'Polish''pt' => 'Portuguese''ps' => 'Pushto''qu' => 'Quechua''rm' => 'Romansh''ro' => 'Romanian''rn' => 'Rundi''ru' => 'Russian''sg' => 'Sango''sa' => 'Sanskrit''sr' => 'Serbian''hr' => 'Croatian''si' => 'Sinhala; Sinhalese''sk' => 'Slovak''sl' => 'Slovenian''se' => 'Northern Sami''sm' => 'Samoan''sn' => 'Shona''sd' => 'Sindhi''so' => 'Somali''st' => 'Sotho, Southern''es' => 'Spanish; Castilian''sc' => 'Sardinian''ss' => 'Swati''su' => 'Sundanese''sw' => 'Swahili''sv' => 'Swedish''ty' => 'Tahitian''ta' => 'Tamil''tt' => 'Tatar''te' => 'Telugu''tg' => 'Tajik''tl' => 'Tagalog''th' => 'Thai''bo' => 'Tibetan''ti' => 'Tigrinya''to' => 'Tonga (Tonga Islands)''tn' => 'Tswana''ts' => 'Tsonga''tk' => 'Turkmen''tr' => 'Turkish''tw' => 'Twi''ug' => 'Uighur; Uyghur''uk' => 'Ukrainian''ur' => 'Urdu''uz' => 'Uzbek''ve' =>