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

Revision 4752, 39.8 kB (checked in by ryan, 2 years ago)

Restrict wp_remote_fopen to remote files.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2
3 require_once(dirname(__FILE__).'/compat.php');
4
5 function mysql2date($dateformatstring, $mysqlstring, $translate = true) {
6     global $wp_locale;
7     $m = $mysqlstring;
8     if ( empty($m) ) {
9         return false;
10     }
11     $i = mktime(substr($m,11,2),substr($m,14,2),substr($m,17,2),substr($m,5,2),substr($m,8,2),substr($m,0,4));
12
13     if( 'U' == $dateformatstring )
14         return $i;
15
16     if ( -1 == $i || false == $i )
17         $i = 0;
18
19     if ( !empty($wp_locale->month) && !empty($wp_locale->weekday) && $translate ) {
20         $datemonth = $wp_locale->get_month(date('m', $i));
21         $datemonth_abbrev = $wp_locale->get_month_abbrev($datemonth);
22         $dateweekday = $wp_locale->get_weekday(date('w', $i));
23         $dateweekday_abbrev = $wp_locale->get_weekday_abbrev($dateweekday);
24         $datemeridiem = $wp_locale->get_meridiem(date('a', $i));
25         $datemeridiem_capital = $wp_locale->get_meridiem(date('A', $i));
26         $dateformatstring = ' '.$dateformatstring;
27         $dateformatstring = preg_replace("/([^\\\])D/", "\\1".backslashit($dateweekday_abbrev), $dateformatstring);
28         $dateformatstring = preg_replace("/([^\\\])F/", "\\1".backslashit($datemonth), $dateformatstring);
29         $dateformatstring = preg_replace("/([^\\\])l/", "\\1".backslashit($dateweekday), $dateformatstring);
30         $dateformatstring = preg_replace("/([^\\\])M/", "\\1".backslashit($datemonth_abbrev), $dateformatstring);
31         $dateformatstring = preg_replace("/([^\\\])a/", "\\1".backslashit($datemeridiem), $dateformatstring);
32         $dateformatstring = preg_replace("/([^\\\])A/", "\\1".backslashit($datemeridiem_capital), $dateformatstring);
33
34         $dateformatstring = substr($dateformatstring, 1, strlen($dateformatstring)-1);
35     }
36     $j = @date($dateformatstring, $i);
37     if ( !$j ) {
38     // for debug purposes
39     //    echo $i." ".$mysqlstring;
40     }
41     return $j;
42 }
43
44 function current_time($type, $gmt = 0) {
45     switch ($type) {
46         case 'mysql':
47             if ( $gmt ) $d = gmdate('Y-m-d H:i:s');
48             else $d = gmdate('Y-m-d H:i:s', (time() + (get_option('gmt_offset') * 3600)));
49             return $d;
50             break;
51         case 'timestamp':
52             if ( $gmt ) $d = time();
53             else $d = time() + (get_option('gmt_offset') * 3600);
54             return $d;
55             break;
56     }
57 }
58
59 function date_i18n($dateformatstring, $unixtimestamp) {
60     global $wp_locale;
61     $i = $unixtimestamp;
62     if ( (!empty($wp_locale->month)) && (!empty($wp_locale->weekday)) ) {
63         $datemonth = $wp_locale->get_month(date('m', $i));
64         $datemonth_abbrev = $wp_locale->get_month_abbrev($datemonth);
65         $dateweekday = $wp_locale->get_weekday(date('w', $i));
66         $dateweekday_abbrev = $wp_locale->get_weekday_abbrev($dateweekday);
67         $datemeridiem = $wp_locale->get_meridiem(date('a', $i));
68         $datemeridiem_capital = $wp_locale->get_meridiem(date('A', $i));
69         $dateformatstring = ' '.$dateformatstring;
70         $dateformatstring = preg_replace("/([^\\\])D/", "\\1".backslashit($dateweekday_abbrev), $dateformatstring);
71         $dateformatstring = preg_replace("/([^\\\])F/", "\\1".backslashit($datemonth), $dateformatstring);
72         $dateformatstring = preg_replace("/([^\\\])l/", "\\1".backslashit($dateweekday), $dateformatstring);
73         $dateformatstring = preg_replace("/([^\\\])M/", "\\1".backslashit($datemonth_abbrev), $dateformatstring);
74         $dateformatstring = preg_replace("/([^\\\])a/", "\\1".backslashit($datemeridiem), $dateformatstring);
75         $dateformatstring = preg_replace("/([^\\\])A/", "\\1".backslashit($datemeridiem_capital), $dateformatstring);
76
77         $dateformatstring = substr($dateformatstring, 1, strlen($dateformatstring)-1);
78     }
79     $j = @date($dateformatstring, $i);
80     return $j;
81 }
82
83 function get_weekstartend($mysqlstring, $start_of_week) {
84     $my = substr($mysqlstring,0,4);
85     $mm = substr($mysqlstring,8,2);
86     $md = substr($mysqlstring,5,2);
87     $day = mktime(0,0,0, $md, $mm, $my);
88     $weekday = date('w',$day);
89     $i = 86400;
90
91     if ( $weekday < get_option('start_of_week') )
92         $weekday = 7 - (get_option('start_of_week') - $weekday);
93
94     while ($weekday > get_option('start_of_week')) {
95         $weekday = date('w',$day);
96         if ( $weekday < get_option('start_of_week') )
97             $weekday = 7 - (get_option('start_of_week') - $weekday);
98
99         $day = $day - 86400;
100         $i = 0;
101     }
102     $week['start'] = $day + 86400 - $i;
103     // $week['end'] = $day - $i + 691199;
104     $week['end'] = $week['start'] + 604799;
105     return $week;
106 }
107
108 function get_lastpostdate($timezone = 'server') {
109     global $cache_lastpostdate, $pagenow, $wpdb, $blog_id;
110     $add_seconds_blog = get_option('gmt_offset') * 3600;
111     $add_seconds_server = date('Z');
112     if ( !isset($cache_lastpostdate[$blog_id][$timezone]) ) {
113         switch(strtolower($timezone)) {
114             case 'gmt':
115                 $lastpostdate = $wpdb->get_var("SELECT post_date_gmt FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1");
116                 break;
117             case 'blog':
118                 $lastpostdate = $wpdb->get_var("SELECT post_date FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1");
119                 break;
120             case 'server':
121                 $lastpostdate = $wpdb->get_var("SELECT DATE_ADD(post_date_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1");
122                 break;
123         }
124         $cache_lastpostdate[$blog_id][$timezone] = $lastpostdate;
125     } else {
126         $lastpostdate = $cache_lastpostdate[$blog_id][$timezone];
127     }
128     return $lastpostdate;
129 }
130
131 function get_lastpostmodified($timezone = 'server') {
132     global $cache_lastpostmodified, $pagenow, $wpdb, $blog_id;
133     $add_seconds_blog = get_option('gmt_offset') * 3600;
134     $add_seconds_server = date('Z');
135     if ( !isset($cache_lastpostmodified[$blog_id][$timezone]) ) {
136         switch(strtolower($timezone)) {
137             case 'gmt':
138                 $lastpostmodified = $wpdb->get_var("SELECT post_modified_gmt FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1");
139                 break;
140             case 'blog':
141                 $lastpostmodified = $wpdb->get_var("SELECT post_modified FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1");
142                 break;
143             case 'server':
144                 $lastpostmodified = $wpdb->get_var("SELECT DATE_ADD(post_modified_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1");
145                 break;
146         }
147         $lastpostdate = get_lastpostdate($timezone);
148         if ( $lastpostdate > $lastpostmodified ) {
149             $lastpostmodified = $lastpostdate;
150         }
151         $cache_lastpostmodified[$blog_id][$timezone] = $lastpostmodified;
152     } else {
153         $lastpostmodified = $cache_lastpostmodified[$blog_id][$timezone];
154     }
155     return $lastpostmodified;
156 }
157
158 function maybe_unserialize($original) {
159     if ( is_serialized($original) ) // don't attempt to unserialize data that wasn't serialized going in
160         if ( false !== $gm = @ unserialize($original) )
161             return $gm;
162     return $original;
163 }
164
165 function is_serialized($data) {
166     // if it isn't a string, it isn't serialized
167     if ( !is_string($data) )
168         return false;
169     $data = trim($data);
170     if ( 'N;' == $data )
171         return true;
172     if ( !preg_match('/^([adObis]):/', $data, $badions) )
173         return false;
174     switch ( $badions[1] ) :
175     case 'a' :
176     case 'O' :
177     case 's' :
178         if ( preg_match("/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data) )
179             return true;
180         break;
181     case 'b' :
182     case 'i' :
183     case 'd' :
184         if ( preg_match("/^{$badions[1]}:[0-9.E-]+;\$/", $data) )
185             return true;
186         break;
187     endswitch;
188     return false;
189 }
190
191 function is_serialized_string($data) {
192     // if it isn't a string, it isn't a serialized string
193     if ( !is_string($data) )
194         return false;
195     $data = trim($data);
196     if ( preg_match('/^s:[0-9]+:.*;$/s',$data) ) // this should fetch all serialized strings
197         return true;
198     return false;
199 }
200
201 /* Options functions */
202
203 function get_option($setting) {
204     global $wpdb;
205
206     $value = wp_cache_get($setting, 'options');
207
208     if ( false === $value ) {
209         if ( defined('WP_INSTALLING') )
210             $wpdb->hide_errors();
211         $row = $wpdb->get_row("SELECT option_value FROM $wpdb->options WHERE option_name = '$setting' LIMIT 1");
212         if ( defined('WP_INSTALLING') )
213             $wpdb->show_errors();
214
215         if( is_object( $row) ) { // Has to be get_row instead of get_var because of funkiness with 0, false, null values
216             $value = $row->option_value;
217             wp_cache_set($setting, $value, 'options');
218         } else {
219             return false;
220         }
221     }
222
223     // If home is not set use siteurl.
224     if ( 'home' == $setting && '' == $value )
225         return get_option('siteurl');
226
227     if ( 'siteurl' == $setting || 'home' == $setting || 'category_base' == $setting )
228         $value = preg_replace('|/+$|', '', $value);
229
230     return apply_filters( 'option_' . $setting, maybe_unserialize($value) );
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     $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();
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 update_option($option_name, $newvalue) {
261     global $wpdb;
262
263     if ( is_string($newvalue) )
264         $newvalue = trim($newvalue);
265
266     // If the new and old values are the same, no need to update.
267     $oldvalue = get_option($option_name);
268     if ( $newvalue == $oldvalue ) {
269         return false;
270     }
271
272     if ( false === $oldvalue ) {
273         add_option($option_name, $newvalue);
274         return true;
275     }
276
277     $_newvalue = $newvalue;
278     $newvalue = maybe_serialize($newvalue);
279
280     wp_cache_set($option_name, $newvalue, 'options');
281
282     $newvalue = $wpdb->escape($newvalue);
283     $option_name = $wpdb->escape($option_name);
284     $wpdb->query("UPDATE $wpdb->options SET option_value = '$newvalue' WHERE option_name = '$option_name'");
285     if ( $wpdb->rows_affected == 1 ) {
286         do_action("update_option_{$option_name}", $oldvalue, $_newvalue);
287         return true;
288     }
289     return false;
290 }
291
292 // thx Alex Stapleton, http://alex.vort-x.net/blog/
293 function add_option($name, $value = '', $description = '', $autoload = 'yes') {
294     global $wpdb;
295
296     // Make sure the option doesn't already exist
297     if ( false !== get_option($name) )
298         return;
299
300     $value = maybe_serialize($value);
301
302     wp_cache_set($name, $value, 'options');
303
304     $name = $wpdb->escape($name);
305     $value = $wpdb->escape($value);
306     $description = $wpdb->escape($description);
307     $wpdb->query("INSERT INTO $wpdb->options (option_name, option_value, option_description, autoload) VALUES ('$name', '$value', '$description', '$autoload')");
308
309     return;
310 }
311
312 function delete_option($name) {
313     global $wpdb;
314     // Get the ID, if no ID then return
315     $option_id = $wpdb->get_var("SELECT option_id FROM $wpdb->options WHERE option_name = '$name'");
316     if ( !$option_id ) return false;
317     $wpdb->query("DELETE FROM $wpdb->options WHERE option_name = '$name'");
318     wp_cache_delete($name, 'options');
319     return true;
320 }
321
322 function maybe_serialize($data) {
323     if ( is_string($data) )
324         $data = trim($data);
325     elseif ( is_array($data) || is_object($data) )
326         return serialize($data);
327     if ( is_serialized($data) )
328         return serialize($data);
329     return $data;
330 }
331
332 function gzip_compression() {
333     if ( !get_option('gzipcompression') ) return false;
334
335     if ( extension_loaded('zlib') ) {
336         ob_start('ob_gzhandler');
337     }
338 }
339
340 function make_url_footnote($content) {
341     preg_match_all('/<a(.+?)href=\"(.+?)\"(.*?)>(.+?)<\/a>/', $content, $matches);
342     $j = 0;
343     for ($i=0; $i<count($matches[0]); $i++) {
344         $links_summary = (!$j) ? "\n" : $links_summary;
345         $j++;
346         $link_match = $matches[0][$i];
347         $link_number = '['.($i+1).']';
348         $link_url = $matches[2][$i];
349         $link_text = $matches[4][$i];
350         $content = str_replace($link_match, $link_text.' '.$link_number, $content);
351         $link_url = ((strtolower(substr($link_url,0,7)) != 'http://') && (strtolower(substr($link_url,0,8)) != 'https://')) ? get_option('home') . $link_url : $link_url;
352         $links_summary .= "\n".$link_number.' '.$link_url;
353     }
354     $content = strip_tags($content);
355     $content .= $links_summary;
356     return $content;
357 }
358
359
360 function xmlrpc_getposttitle($content) {
361     global $post_default_title;
362     if ( preg_match('/<title>(.+?)<\/title>/is', $content, $matchtitle) ) {
363         $post_title = $matchtitle[0];
364         $post_title = preg_replace('/<title>/si', '', $post_title);
365         $post_title = preg_replace('/<\/title>/si', '', $post_title);
366     } else {
367         $post_title = $post_default_title;
368     }
369     return $post_title;
370 }
371
372 function xmlrpc_getpostcategory($content) {
373     global $post_default_category;
374     if ( preg_match('/<category>(.+?)<\/category>/is', $content, $matchcat) ) {
375         $post_category = trim($matchcat[1], ',');
376         $post_category = explode(',', $post_category);
377     } else {
378         $post_category = $post_default_category;
379     }
380     return $post_category;
381 }
382
383 function xmlrpc_removepostdata($content) {
384     $content = preg_replace('/<title>(.+?)<\/title>/si', '', $content);
385     $content = preg_replace('/<category>(.+?)<\/category>/si', '', $content);
386     $content = trim($content);
387     return $content;
388 }
389
390 function debug_fopen($filename, $mode) {
391     global $debug;
392     if ( $debug == 1 ) {
393         $fp = fopen($filename, $mode);
394         return $fp;
395     } else {
396         return false;
397     }
398 }
399
400 function debug_fwrite($fp, $string) {
401     global $debug;
402     if ( $debug == 1 ) {
403         fwrite($fp, $string);
404     }
405 }
406
407 function debug_fclose($fp) {
408     global $debug;
409     if ( $debug == 1 ) {
410         fclose($fp);
411     }
412 }
413
414 function do_enclose( $content, $post_ID ) {
415     global $wp_version, $wpdb;
416     include_once (ABSPATH . WPINC . '/class-IXR.php');
417
418     $log = debug_fopen(ABSPATH . '/enclosures.log', 'a');
419     $post_links = array();
420     debug_fwrite($log, 'BEGIN '.date('YmdHis', time())."\n");
421
422     $pung = get_enclosed( $post_ID );
423
424     $ltrs = '\w';
425     $gunk = '/#~:.?+=&%@!\-';
426     $punc = '.:?\-';
427     $any = $ltrs . $gunk . $punc;
428
429     preg_match_all("{\b http : [$any] +? (?= [$punc] * [^$any] | $)}x", $content, $post_links_temp);
430
431     debug_fwrite($log, 'Post contents:');
432     debug_fwrite($log, $content."\n");
433
434     foreach($post_links_temp[0] as $link_test) :
435         if ( !in_array($link_test, $pung) ) : // If we haven't pung it already
436             $test