root/tags/2.3.2/wp-includes/functions.php

Revision 6448, 44.5 kB (checked in by ryan, 1 year ago)

Custom DB error page. fixes #5500 for 2.3

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2
3 function mysql2date($dateformatstring, $mysqlstring, $translate = true) {
4     global $wp_locale;
5     $m = $mysqlstring;
6     if ( empty($m) ) {
7         return false;
8     }
9     $i = mktime(
10         (int) substr( $m, 11, 2 ), (int) substr( $m, 14, 2 ), (int) substr( $m, 17, 2 ),
11         (int) substr( $m, 5, 2 ), (int) substr( $m, 8, 2 ), (int) substr( $m, 0, 4 )
12     );
13
14     if( 'U' == $dateformatstring )
15         return $i;
16
17     if ( -1 == $i || false == $i )
18         $i = 0;
19
20     if ( !empty($wp_locale->month) && !empty($wp_locale->weekday) && $translate ) {
21         $datemonth = $wp_locale->get_month(date('m', $i));
22         $datemonth_abbrev = $wp_locale->get_month_abbrev($datemonth);
23         $dateweekday = $wp_locale->get_weekday(date('w', $i));
24         $dateweekday_abbrev = $wp_locale->get_weekday_abbrev($dateweekday);
25         $datemeridiem = $wp_locale->get_meridiem(date('a', $i));
26         $datemeridiem_capital = $wp_locale->get_meridiem(date('A', $i));
27         $dateformatstring = ' '.$dateformatstring;
28         $dateformatstring = preg_replace("/([^\\\])D/", "\\1".backslashit($dateweekday_abbrev), $dateformatstring);
29         $dateformatstring = preg_replace("/([^\\\])F/", "\\1".backslashit($datemonth), $dateformatstring);
30         $dateformatstring = preg_replace("/([^\\\])l/", "\\1".backslashit($dateweekday), $dateformatstring);
31         $dateformatstring = preg_replace("/([^\\\])M/", "\\1".backslashit($datemonth_abbrev), $dateformatstring);
32         $dateformatstring = preg_replace("/([^\\\])a/", "\\1".backslashit($datemeridiem), $dateformatstring);
33         $dateformatstring = preg_replace("/([^\\\])A/", "\\1".backslashit($datemeridiem_capital), $dateformatstring);
34
35         $dateformatstring = substr($dateformatstring, 1, strlen($dateformatstring)-1);
36     }
37     $j = @date($dateformatstring, $i);
38     if ( !$j ) {
39     // for debug purposes
40     //    echo $i." ".$mysqlstring;
41     }
42     return $j;
43 }
44
45 function current_time($type, $gmt = 0) {
46     switch ($type) {
47         case 'mysql':
48             if ( $gmt ) $d = gmdate('Y-m-d H:i:s');
49             else $d = gmdate('Y-m-d H:i:s', (time() + (get_option('gmt_offset') * 3600)));
50             return $d;
51             break;
52         case 'timestamp':
53             if ( $gmt ) $d = time();
54             else $d = time() + (get_option('gmt_offset') * 3600);
55             return $d;
56             break;
57     }
58 }
59
60 function date_i18n($dateformatstring, $unixtimestamp) {
61     global $wp_locale;
62     $i = $unixtimestamp;
63     if ( (!empty($wp_locale->month)) && (!empty($wp_locale->weekday)) ) {
64         $datemonth = $wp_locale->get_month(date('m', $i));
65         $datemonth_abbrev = $wp_locale->get_month_abbrev($datemonth);
66         $dateweekday = $wp_locale->get_weekday(date('w', $i));
67         $dateweekday_abbrev = $wp_locale->get_weekday_abbrev($dateweekday);
68         $datemeridiem = $wp_locale->get_meridiem(date('a', $i));
69         $datemeridiem_capital = $wp_locale->get_meridiem(date('A', $i));
70         $dateformatstring = ' '.$dateformatstring;
71         $dateformatstring = preg_replace("/([^\\\])D/", "\\1".backslashit($dateweekday_abbrev), $dateformatstring);
72         $dateformatstring = preg_replace("/([^\\\])F/", "\\1".backslashit($datemonth), $dateformatstring);
73         $dateformatstring = preg_replace("/([^\\\])l/", "\\1".backslashit($dateweekday), $dateformatstring);
74         $dateformatstring = preg_replace("/([^\\\])M/", "\\1".backslashit($datemonth_abbrev), $dateformatstring);
75         $dateformatstring = preg_replace("/([^\\\])a/", "\\1".backslashit($datemeridiem), $dateformatstring);
76         $dateformatstring = preg_replace("/([^\\\])A/", "\\1".backslashit($datemeridiem_capital), $dateformatstring);
77
78         $dateformatstring = substr($dateformatstring, 1, strlen($dateformatstring)-1);
79     }
80     $j = @date($dateformatstring, $i);
81     return $j;
82 }
83
84 function number_format_i18n($number, $decimals = null) {
85     global $wp_locale;
86     // let the user override the precision only
87     $decimals = is_null($decimals)? $wp_locale->number_format['decimals'] : intval($decimals);
88
89     return number_format($number, $decimals, $wp_locale->number_format['decimal_point'], $wp_locale->number_format['thousands_sep']);
90 }
91
92 function size_format($bytes, $decimals = null) {
93     // technically the correct unit names for powers of 1024 are KiB, MiB etc
94     // see http://en.wikipedia.org/wiki/Byte
95     $quant = array(
96         'TB' => pow(1024, 4),
97         'GB' => pow(1024, 3),
98         'MB' => pow(1024, 2),
99         'kB' => pow(1024, 1),
100         'B'  => pow(1024, 0),
101     );
102
103     foreach ($quant as $unit => $mag)
104         if ( intval($bytes) >= $mag )
105             return number_format_i18n($bytes / $mag, $decimals) . ' ' . $unit;
106 }
107
108 function get_weekstartend($mysqlstring, $start_of_week) {
109     $my = substr($mysqlstring,0,4);
110     $mm = substr($mysqlstring,8,2);
111     $md = substr($mysqlstring,5,2);
112     $day = mktime(0,0,0, $md, $mm, $my);
113     $weekday = date('w',$day);
114     $i = 86400;
115
116     if ( $weekday < get_option('start_of_week') )
117         $weekday = 7 - (get_option('start_of_week') - $weekday);
118
119     while ($weekday > get_option('start_of_week')) {
120         $weekday = date('w',$day);
121         if ( $weekday < get_option('start_of_week') )
122             $weekday = 7 - (get_option('start_of_week') - $weekday);
123
124         $day = $day - 86400;
125         $i = 0;
126     }
127     $week['start'] = $day + 86400 - $i;
128     // $week['end'] = $day - $i + 691199;
129     $week['end'] = $week['start'] + 604799;
130     return $week;
131 }
132
133 function maybe_unserialize($original) {
134     if ( is_serialized($original) ) // don't attempt to unserialize data that wasn't serialized going in
135         if ( false !== $gm = @ unserialize($original) )
136             return $gm;
137     return $original;
138 }
139
140 function is_serialized($data) {
141     // if it isn't a string, it isn't serialized
142     if ( !is_string($data) )
143         return false;
144     $data = trim($data);
145     if ( 'N;' == $data )
146         return true;
147     if ( !preg_match('/^([adObis]):/', $data, $badions) )
148         return false;
149     switch ( $badions[1] ) :
150     case 'a' :
151     case 'O' :
152     case 's' :
153         if ( preg_match("/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data) )
154             return true;
155         break;
156     case 'b' :
157     case 'i' :
158     case 'd' :
159         if ( preg_match("/^{$badions[1]}:[0-9.E-]+;\$/", $data) )
160             return true;
161         break;
162     endswitch;
163     return false;
164 }
165
166 function is_serialized_string($data) {
167     // if it isn't a string, it isn't a serialized string
168     if ( !is_string($data) )
169         return false;
170     $data = trim($data);
171     if ( preg_match('/^s:[0-9]+:.*;$/s',$data) ) // this should fetch all serialized strings
172         return true;
173     return false;
174 }
175
176 /* Options functions */
177
178 // expects $setting to already be SQL-escaped
179 function get_option($setting) {
180     global $wpdb;
181
182     // Allow plugins to short-circuit options.
183     $pre = apply_filters( 'pre_option_' . $setting, false );
184     if ( false !== $pre )
185         return $pre;
186
187     // prevent non-existent options from triggering multiple queries
188     $notoptions = wp_cache_get('notoptions', 'options');
189     if ( isset($notoptions[$setting]) )
190         return false;
191
192     $alloptions = wp_load_alloptions();
193
194     if ( isset($alloptions[$setting]) ) {
195         $value = $alloptions[$setting];
196     } else {
197         $value = wp_cache_get($setting, 'options');
198
199         if ( false === $value ) {
200             if ( defined('WP_INSTALLING') )
201                 $show = $wpdb->hide_errors();
202             $row = $wpdb->get_row("SELECT option_value FROM $wpdb->options WHERE option_name = '$setting' LIMIT 1");
203             if ( defined('WP_INSTALLING') )
204                 $wpdb->show_errors($show);
205
206             if( is_object( $row) ) { // Has to be get_row instead of get_var because of funkiness with 0, false, null values
207                 $value = $row->option_value;
208                 wp_cache_add($setting, $value, 'options');
209             } else { // option does not exist, so we must cache its non-existence
210                 $notoptions[$setting] = true;
211                 wp_cache_set('notoptions', $notoptions, 'options');
212                 return false;
213             }
214         }
215     }
216
217     // If home is not set use siteurl.
218     if ( 'home' == $setting && '' == $value )
219         return get_option('siteurl');
220
221     if ( in_array($setting, array('siteurl', 'home', 'category_base', 'tag_base')) )
222         $value = untrailingslashit($value);
223
224     return apply_filters( 'option_' . $setting, maybe_unserialize($value) );
225 }
226
227 function wp_protect_special_option($option) {
228     $protected = array('alloptions', 'notoptions');
229     if ( in_array($option, $protected) )
230         die(sprintf(__('%s is a protected WP option and may not be modified'), wp_specialchars($option)));
231 }
232
233 function form_option($option) {
234     echo attribute_escape(get_option($option));
235 }
236
237 function get_alloptions() {
238     global $wpdb, $wp_queries;
239     $show = $wpdb->hide_errors();
240     if ( !$options = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'") ) {
241         $options = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options");
242     }
243     $wpdb->show_errors($show);
244
245     foreach ($options as $option) {
246         // "When trying to design a foolproof system,
247         //  never underestimate the ingenuity of the fools :)" -- Dougal
248         if ( 'siteurl' == $option->option_name )
249             $option->option_value = preg_replace('|/+$|', '', $option->option_value);
250         if ( 'home' == $option->option_name )
251             $option->option_value = preg_replace('|/+$|', '', $option->option_value);
252         if ( 'category_base' == $option->option_name )
253             $option->option_value = preg_replace('|/+$|', '', $option->option_value);
254         $value = maybe_unserialize($option->option_value);
255         $all_options->{$option->option_name} = apply_filters('pre_option_' . $option->option_name, $value);
256     }
257     return apply_filters('all_options', $all_options);
258 }
259
260 function wp_load_alloptions() {
261     global $wpdb;
262
263     $alloptions = wp_cache_get('alloptions', 'options');
264
265     if ( !$alloptions ) {
266         $show = $wpdb->hide_errors();
267         if ( !$alloptions_db = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'") )
268             $alloptions_db = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options");
269         $wpdb->show_errors($show);
270         $alloptions = array();
271         foreach ( (array) $alloptions_db as $o )
272             $alloptions[$o->option_name] = $o->option_value;
273         wp_cache_add('alloptions', $alloptions, 'options');
274     }
275     return $alloptions;
276 }
277
278 // expects $option_name to NOT be SQL-escaped
279 function update_option($option_name, $newvalue) {
280     global $wpdb;
281
282     wp_protect_special_option($option_name);
283
284     $safe_option_name = $wpdb->escape($option_name);
285     $newvalue = sanitize_option($option_name, $newvalue);
286
287     if ( is_string($newvalue) )
288         $newvalue = trim($newvalue);
289
290     // If the new and old values are the same, no need to update.
291     $oldvalue = get_option($safe_option_name);
292     if ( $newvalue === $oldvalue ) {
293         return false;
294     }
295
296     if ( false === $oldvalue ) {
297         add_option($option_name, $newvalue);
298         return true;
299     }
300
301     $notoptions = wp_cache_get('notoptions', 'options');
302     if ( is_array($notoptions) && isset($notoptions[$option_name]) ) {
303         unset($notoptions[$option_name]);
304         wp_cache_set('notoptions', $notoptions, 'options');
305     }
306
307     $_newvalue = $newvalue;
308     $newvalue = maybe_serialize($newvalue);
309
310     $alloptions = wp_load_alloptions();
311     if ( isset($alloptions[$option_name]) ) {
312         $alloptions[$option_name] = $newvalue;
313         wp_cache_set('alloptions', $alloptions, 'options');
314     } else {
315         wp_cache_set($option_name, $newvalue, 'options');
316     }
317
318     $newvalue = $wpdb->escape($newvalue);
319     $option_name = $wpdb->escape($option_name);
320     $wpdb->query("UPDATE $wpdb->options SET option_value = '$newvalue' WHERE option_name = '$option_name'");
321     if ( $wpdb->rows_affected == 1 ) {
322         do_action("update_option_{$option_name}", $oldvalue, $_newvalue);
323         return true;
324     }
325     return false;
326 }
327
328 // thx Alex Stapleton, http://alex.vort-x.net/blog/
329 // expects $name to NOT be SQL-escaped
330 function add_option($name, $value = '', $deprecated = '', $autoload = 'yes') {
331     global $wpdb;
332
333     wp_protect_special_option($name);
334     $safe_name = $wpdb->escape($name);
335
336     // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
337     $notoptions = wp_cache_get('notoptions', 'options');
338     if ( !is_array($notoptions) || !isset($notoptions[$name]) )
339         if ( false !== get_option($safe_name) )
340             return;
341
342     $value = maybe_serialize($value);
343     $autoload = ( 'no' === $autoload ) ? 'no' : 'yes';
344
345     if ( 'yes' == $autoload ) {
346         $alloptions = wp_load_alloptions();
347         $alloptions[$name] = $value;
348         wp_cache_set('alloptions', $alloptions, 'options');
349     } else {
350         wp_cache_set($name, $value, 'options');
351     }
352
353     // This option exists now
354     $notoptions = wp_cache_get('notoptions', 'options'); // yes, again... we need it to be fresh
355     if ( is_array($notoptions) && isset($notoptions[$name]) ) {
356         unset($notoptions[$name]);
357         wp_cache_set('notoptions', $notoptions, 'options');
358     }
359
360     $name = $wpdb->escape($name);
361     $value = $wpdb->escape($value);
362     $wpdb->query("INSERT INTO $wpdb->options (option_name, option_value, autoload) VALUES ('$name', '$value', '$autoload')");
363
364     return;
365 }
366
367 function delete_option($name) {
368     global $wpdb;
369
370     wp_protect_special_option($name);
371
372     // Get the ID, if no ID then return
373     $option = $wpdb->get_row("SELECT option_id, autoload FROM $wpdb->options WHERE option_name = '$name'");
374     if ( !$option->option_id ) return false;
375     $wpdb->query("DELETE FROM $wpdb->options WHERE option_name = '$name'");
376     if ( 'yes' == $option->autoload ) {
377         $alloptions = wp_load_alloptions();
378         if ( isset($alloptions[$name]) ) {
379             unset($alloptions[$name]);
380             wp_cache_set('alloptions', $alloptions, 'options');
381         }
382     } else {
383         wp_cache_delete($name, 'options');
384     }
385     return true;
386 }
387
388 function maybe_serialize($data) {
389     if ( is_string($data) )
390         $data = trim($data);
391     elseif ( is_array($data) || is_object($data) )
392         return serialize($data);
393     if ( is_serialized($data) )
394         return serialize($data);
395     return $data;
396 }
397
398 function gzip_compression() {
399     if ( !get_option( 'gzipcompression' ) ) {
400         return false;
401     }
402
403     if ( ( ini_get( 'zlib.output_compression' ) == 'On' || ini_get( 'zlib.output_compression_level' ) > 0 ) || ini_get( 'output_handler' ) == 'ob_gzhandler' ) {
404         return false;
405     }
406
407     if ( extension_loaded( 'zlib' ) ) {
408         ob_start( 'ob_gzhandler' );
409     }
410 }
411
412 function make_url_footnote($content) {
413     preg_match_all('/<a(.+?)href=\"(.+?)\"(.*?)>(.+?)<\/a>/', $content, $matches);
414     $j = 0;
415     for ($i=0; $i<count($matches[0]); $i++) {
416         $links_summary = (!$j) ? "\n" : $links_summary;
417         $j++;
418         $link_match = $matches[0][$i];
419         $link_number = '['.($i+1).']';
420         $link_url = $matches[2][$i];
421         $link_text = $matches[4][$i];
422         $content = str_replace($link_match, $link_text.' '.$link_number, $content);
423         $link_url = ((strtolower(substr($link_url,0,7)) != 'http://') && (strtolower(substr($link_url,0,8)) != 'https://')) ? get_option('home') . $link_url : $link_url;
424         $links_summary .= "\n".$link_number.' '.$link_url;
425     }
426     $content = strip_tags($content);
427     $content .= $links_summary;
428     return $content;
429 }
430
431
432 function xmlrpc_get