Changeset 6223
- Timestamp:
- 10/11/07 05:46:57 (1 year ago)
- Files:
-
- trunk/wp-includes/functions.php (modified) (46 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/wp-includes/functions.php
r6222 r6223 1 1 <?php 2 2 3 function mysql2date( $dateformatstring, $mysqlstring, $translate = true) {3 function mysql2date( $dateformatstring, $mysqlstring, $translate = true ) { 4 4 global $wp_locale; 5 5 $m = $mysqlstring; 6 if ( empty( $m) ) {6 if ( empty( $m ) ) 7 7 return false; 8 }9 8 $i = mktime( 10 9 (int) substr( $m, 11, 2 ), (int) substr( $m, 14, 2 ), (int) substr( $m, 17, 2 ), … … 18 17 $i = 0; 19 18 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 } 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 38 /* 39 if ( !$j ) // for debug purposes 40 echo $i." ".$mysqlstring; 41 */ 42 42 43 return $j; 43 44 } 44 45 45 function current_time($type, $gmt = 0) { 46 switch ($type) { 46 47 function current_time( $type, $gmt = 0 ) { 48 switch ( $type ) { 47 49 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; 50 return ( $gmt ) ? gmdate( 'Y-m-d H:i:s' ) : gmdate( 'Y-m-d H:i:s', ( time() + ( get_option( 'gmt_offset' ) * 3600 ) ) ); 51 51 break; 52 52 case 'timestamp': 53 if ( $gmt ) $d = time(); 54 else $d = time() + (get_option('gmt_offset') * 3600); 55 return $d; 53 return ( $gmt ) ? time() : time() + ( get_option( 'gmt_offset' ) * 3600 ); 56 54 break; 57 55 } 58 56 } 59 57 60 function date_i18n($dateformatstring, $unixtimestamp) { 58 59 function date_i18n( $dateformatstring, $unixtimestamp ) { 61 60 global $wp_locale; 62 61 $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));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 ) ); 70 69 $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);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 ); 81 80 return $j; 82 81 } 83 82 84 function number_format_i18n($number, $decimals = null) { 83 84 function number_format_i18n( $number, $decimals = null ) { 85 85 global $wp_locale; 86 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) { 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 93 function size_format( $bytes, $decimals = null ) { 93 94 // technically the correct unit names for powers of 1024 are KiB, MiB etc 94 95 // see http://en.wikipedia.org/wiki/Byte 95 96 $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), 97 // ========================= Origin ==== 98 'TB' => 1099511627776, // pow( 1024, 4) 99 'GB' => 1073741824, // pow( 1024, 3) 100 'MB' => 1048576, // pow( 1024, 2) 101 'kB' => 1024, // pow( 1024, 1) 102 'B ' => 1, // pow( 1024, 0) 101 103 ); 102 104 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); 105 foreach ( $quant as $unit => $mag ) 106 if ( intval( $bytes ) >= $mag ) 107 return number_format_i18n( $bytes / $mag, $decimals ) . ' ' . $unit; 108 } 109 110 111 function get_weekstartend( $mysqlstring, $start_of_week ) { 112 $my = substr( $mysqlstring, 0, 4 ); 113 $mm = substr( $mysqlstring, 8, 2 ); 114 $md = substr( $mysqlstring, 5, 2 ); 115 $day = mktime( 0, 0, 0, $md, $mm, $my ); 116 $weekday = date( 'w', $day ); 114 117 $i = 86400; 115 118 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);119 if ( $weekday < get_option( 'start_of_week' ) ) 120 $weekday = 7 - ( get_option( 'start_of_week' ) - $weekday ); 121 122 while ( $weekday > get_option( 'start_of_week' ) ) { 123 $weekday = date( 'w', $day ); 124 if ( $weekday < get_option( 'start_of_week' ) ) 125 $weekday = 7 - ( get_option( 'start_of_week' ) - $weekday ); 123 126 124 127 $day = $day - 86400; … … 126 129 } 127 130 $week['start'] = $day + 86400 - $i; 128 // $week['end'] = $day - $i + 691199;129 131 $week['end'] = $week['start'] + 604799; 130 132 return $week; 131 133 } 132 134 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) ) 135 136 function maybe_unserialize( $original ) { 137 if ( is_serialized( $original ) ) // don't attempt to unserialize data that wasn't serialized going in 138 if ( false !== $gm = @unserialize( $original ) ) 136 139 return $gm; 137 140 return $original; 138 141 } 139 142 140 function is_serialized($data) { 143 144 function is_serialized( $data ) { 141 145 // if it isn't a string, it isn't serialized 142 if ( !is_string( $data) )146 if ( !is_string( $data ) ) 143 147 return false; 144 $data = trim( $data);148 $data = trim( $data ); 145 149 if ( 'N;' == $data ) 146 150 return true; 147 if ( !preg_match( '/^([adObis]):/', $data, $badions) )151 if ( !preg_match( '/^([adObis]):/', $data, $badions ) ) 148 152 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;153 switch ( $badions[1] ) { 154 case 'a' : 155 case 'O' : 156 case 's' : 157 if ( preg_match( "/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data ) ) 158 return true; 159 break; 160 case 'b' : 161 case 'i' : 162 case 'd' : 163 if ( preg_match( "/^{$badions[1]}:[0-9.E-]+;\$/", $data ) ) 164 return true; 165 break; 166 } 163 167 return false; 164 168 } 165 169 166 function is_serialized_string($data) { 170 171 function is_serialized_string( $data ) { 167 172 // if it isn't a string, it isn't a serialized string 168 if ( !is_string( $data) )173 if ( !is_string( $data ) ) 169 174 return false; 170 $data = trim( $data);171 if ( preg_match( '/^s:[0-9]+:.*;$/s',$data) ) // this should fetch all serialized strings175 $data = trim( $data ); 176 if ( preg_match( '/^s:[0-9]+:.*;$/s', $data ) ) // this should fetch all serialized strings 172 177 return true; 173 178 return false; 174 179 } 175 180 181 176 182 /* Options functions */ 177 183 178 184 // expects $setting to already be SQL-escaped 179 function get_option( $setting) {185 function get_option( $setting ) { 180 186 global $wpdb; 181 187 … … 186 192 187 193 // prevent non-existent options from triggering multiple queries 188 $notoptions = wp_cache_get( 'notoptions', 'options');189 if ( isset( $notoptions[$setting]) )194 $notoptions = wp_cache_get( 'notoptions', 'options' ); 195 if ( isset( $notoptions[$setting] ) ) 190 196 return false; 191 197 192 198 $alloptions = wp_load_alloptions(); 193 199 194 if ( isset( $alloptions[$setting]) ) {200 if ( isset( $alloptions[$setting] ) ) { 195 201 $value = $alloptions[$setting]; 196 202 } else { 197 $value = wp_cache_get( $setting, 'options');203 $value = wp_cache_get( $setting, 'options' ); 198 204 199 205 if ( false === $value ) { 200 if ( defined( 'WP_INSTALLING') )206 if ( defined( 'WP_INSTALLING' ) ) 201 207 $wpdb->hide_errors(); 202 208 // expected_slashed ($setting) 203 $row = $wpdb->get_row( "SELECT option_value FROM $wpdb->options WHERE option_name = '$setting' LIMIT 1");204 if ( defined( 'WP_INSTALLING') )209 $row = $wpdb->get_row( "SELECT option_value FROM $wpdb->options WHERE option_name = '$setting' LIMIT 1" ); 210 if ( defined( 'WP_INSTALLING' ) ) 205 211 $wpdb->show_errors(); 206 212 207 if ( is_object( $row) ) { // Has to be get_row instead of get_var because of funkiness with 0, false, null values213 if ( is_object( $row) ) { // Has to be get_row instead of get_var because of funkiness with 0, false, null values 208 214 $value = $row->option_value; 209 wp_cache_add( $setting, $value, 'options');215 wp_cache_add( $setting, $value, 'options' ); 210 216 } else { // option does not exist, so we must cache its non-existence 211 217 $notoptions[$setting] = true; 212 wp_cache_set( 'notoptions', $notoptions, 'options');218 wp_cache_set( 'notoptions', $notoptions, 'options' ); 213 219 return false; 214 220 } … … 218 224 // If home is not set use siteurl. 219 225 if ( 'home' == $setting && '' == $value ) 220 return get_option('siteurl'); 221 222 if ( in_array($setting, array('siteurl', 'home', 'category_base', 'tag_base')) ) 223 $value = untrailingslashit($value); 224 225 return apply_filters( 'option_' . $setting, maybe_unserialize($value) ); 226 } 227 228 function wp_protect_special_option($option) { 229 $protected = array('alloptions', 'notoptions'); 230 if ( in_array($option, $protected) ) 231 die(sprintf(__('%s is a protected WP option and may not be modified'), wp_specialchars($option))); 232 } 233 234 function form_option($option) { 235 echo attribute_escape(get_option($option)); 226 return get_option( 'siteurl' ); 227 228 if ( in_array( $setting, array('siteurl', 'home', 'category_base', 'tag_base') ) ) 229 $value = untrailingslashit( $value ); 230 231 return apply_filters( 'option_' . $setting, maybe_unserialize( $value ) ); 232 } 233 234 235 function wp_protect_special_option( $option ) { 236 $protected = array( 'alloptions', 'notoptions' ); 237 if ( in_array( $option, $protected ) ) 238 die( sprintf( __( '%s is a protected WP option and may not be modified' ), wp_specialchars( $option ) ) ); 239 } 240 241 function form_option( $option ) { 242 echo attribute_escape (get_option( $option ) ); 236 243 } 237 244 … … 239 246 global $wpdb, $wp_queries; 240 247 $wpdb->hide_errors(); 241 if ( !$options = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'") ) { 242 $options = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options"); 243 } 248 if ( !$options = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'" ) ) 249 $options = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" ); 244 250 $wpdb->show_errors(); 245 251 246 foreach ( $options as $option) {252 foreach ( $options as $option ) { 247 253 // "When trying to design a foolproof system, 248 254 // never underestimate the ingenuity of the fools :)" -- Dougal 249 if ( 'siteurl' == $option->option_name ) 250 $option->option_value = preg_replace('|/+$|', '', $option->option_value); 251 if ( 'home' == $option->option_name ) 252 $option->option_value = preg_replace('|/+$|', '', $option->option_value); 253 if ( 'category_base' == $option->option_name ) 254 $option->option_value = preg_replace('|/+$|', '', $option->option_value); 255 $value = maybe_unserialize($option->option_value); 256 $all_options->{$option->option_name} = apply_filters('pre_option_' . $option->option_name, $value); 257 } 258 return apply_filters('all_options', $all_options); 259 } 255 if ( in_array( $option->option_name, array( 'siteurl', 'home', 'category_base' ) ) ) 256 $option->option_value = untrailingslashit( $option->option_value ); 257 $value = maybe_unserialize( $option->option_value ); 258 $all_options->{$option->option_name} = apply_filters( 'pre_option_' . $option->option_name, $value ); 259 } 260 return apply_filters( 'all_options', $all_options ); 261 } 262 260 263 261 264 function wp_load_alloptions() { 262 265 global $wpdb; 263 266 264 $alloptions = wp_cache_get( 'alloptions', 'options');267 $alloptions = wp_cache_get( 'alloptions', 'options' ); 265 268 266 269 if ( !$alloptions ) { 267 270 $wpdb->hide_errors(); 268 if ( !$alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'") )269 $alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options");271 if ( !$alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'" ) ) 272 $alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" ); 270 273 $wpdb->show_errors(); 271 274 $alloptions = array(); 272 275 foreach ( (array) $alloptions_db as $o ) 273 276 $alloptions[$o->option_name] = $o->option_value; 274 wp_cache_add( 'alloptions', $alloptions, 'options');277 wp_cache_add( 'alloptions', $alloptions, 'options' ); 275 278 } 276 279 return $alloptions; 277 280 } 278 281 282 279 283 // expects $option_name to NOT be SQL-escaped 280 function update_option( $option_name, $newvalue) {284 function update_option( $option_name, $newvalue ) { 281 285 global $wpdb; 282 286 283 wp_protect_special_option($option_name); 284 285 $safe_option_name = $wpdb->escape($option_name); 286 $newvalue = sanitize_option($option_name, $newvalue); 287 288 if ( is_string($newvalue) ) 289 $newvalue = trim($newvalue); 287 wp_protect_special_option( $option_name ); 288 289 $safe_option_name = $wpdb->escape( $option_name ); 290 $newvalue = sanitize_option( $option_name, $newvalue ); 291 292 // Likely legacy -- can we drop this? 293 if ( is_string( $newvalue ) ) 294 $newvalue = trim( $newvalue ); 290 295 291 296 // If the new and old values are the same, no need to update. 292 $oldvalue = get_option( $safe_option_name);293 if ( $newvalue === $oldvalue ) {297 $oldvalue = get_option( $safe_option_name ); 298 if ( $newvalue === $oldvalue ) 294 299 return false; 295 }296 300 297 301 if ( false === $oldvalue ) { 298 add_option( $option_name, $newvalue);302 add_option( $option_name, $newvalue ); 299 303 return true; 300 304 } 301 305 302 $notoptions = wp_cache_get( 'notoptions', 'options');303 if ( is_array( $notoptions) && isset($notoptions[$option_name]) ) {304 unset( $notoptions[$option_name]);305 wp_cache_set( 'notoptions', $notoptions, 'options');306 $notoptions = wp_cache_get( 'notoptions', 'options' ); 307 if ( is_array( $notoptions ) && isset( $notoptions[$option_name] ) ) { 308 unset( $notoptions[$option_name] ); 309 wp_cache_set( 'notoptions', $notoptions, 'options' ); 306 310 } 307 311 308 312 $_newvalue = $newvalue; 309 $newvalue = maybe_serialize( $newvalue);313 $newvalue = maybe_serialize( $newvalue ); 310 314 311 315 $alloptions = wp_load_alloptions(); 312 if ( isset( $alloptions[$option_name]) ) {316 if ( isset( $alloptions[$option_name] ) ) { 313 317 $alloptions[$option_name] = $newvalue; 314 wp_cache_set( 'alloptions', $alloptions, 'options');318 wp_cache_set( 'alloptions', $alloptions, 'options' ); 315 319 } else { 316 wp_cache_set( $option_name, $newvalue, 'options');317 } 318 319 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->options SET option_value = %s WHERE option_name = %s", $newvalue, $option_name));320 wp_cache_set( $option_name, $newvalue, 'options' ); 321 } 322 323 $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->options SET option_value = %s WHERE option_name = %s", $newvalue, $option_name ) ); 320 324 if ( $wpdb->rows_affected == 1 ) { 321 do_action( "update_option_{$option_name}", $oldvalue, $_newvalue);325 do_action( "update_option_{$option_name}", $oldvalue, $_newvalue ); 322 326 return true; 323 327 } 324 328 return false; 325 329 } 330 326 331 327 332 // thx Alex Stapleton, http://alex.vort-x.net/blog/ 328 333 // expects $name to NOT be SQL-escaped 329 function add_option( $name, $value = '', $deprecated = '', $autoload = 'yes') {334 function add_option( $name, $value = '', $deprecated = '', $autoload = 'yes' ) { 330 335 global $wpdb; 331 336 332 wp_protect_special_option( $name);333 $safe_name = $wpdb->escape( $name);337 wp_protect_special_option( $name ); 338 $safe_name = $wpdb->escape( $name ); 334 339 335 340 // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query 336 $notoptions = wp_cache_get( 'notoptions', 'options');337 if ( !is_array( $notoptions) || !isset($notoptions[$name]) )338 if ( false !== get_option( $safe_name) )341 $notoptions = wp_cache_get( 'notoptions', 'options' ); 342 if ( !is_array( $notoptions ) || !isset( $notoptions[$name] ) ) 343 if ( false !== get_option( $safe_name ) ) 339 344 return; 340 345 341 $value = maybe_serialize( $value);346 $value = maybe_serialize( $value ); 342 347 $autoload = ( 'no' === $autoload ) ? 'no' : 'yes'; 343 348 … … 345 350 $alloptions = wp_load_alloptions(); 346 351 $alloptions[$name] = $value; 347 wp_cache_set( 'alloptions', $alloptions, 'options');352 wp_cache_set( 'alloptions', $alloptions, 'options' ); 348 353 } else { 349 wp_cache_set( $name, $value, 'options');354 wp_cache_set( $name, $value, 'options' ); 350 355 } 351 356 352 357 // This option exists now 353 $notoptions = wp_cache_get( 'notoptions', 'options'); // yes, again... we need it to be fresh354 if ( is_array( $notoptions) && isset($notoptions[$name]) ) {355 unset( $notoptions[$name]);356 wp_cache_set( 'notoptions', $notoptions, 'options');357 } 358 359 $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->options (option_name, option_value, autoload) VALUES (%s, %s, %s)", $name, $value, $autoload));358 $notoptions = wp_cache_get( 'notoptions', 'options' ); // yes, again... we need it to be fresh 359 if ( is_array( $notoptions ) && isset( $notoptions[$name] ) ) { 360 unset( $notoptions[$name] ); 361 wp_cache_set( 'notoptions', $notoptions, 'options' ); 362 } 363 364 $wpdb->query( $wpdb->prepare( "INSERT INTO $wpdb->options (option_name, option_value, autoload) VALUES (%s, %s, %s)", $name, $value, $autoload ) ); 360 365 361 366 return; 362 367 } 363 368 364 function delete_option($name) { 369 370 function delete_option( $name ) { 365 371 global $wpdb; 366 372 367 wp_protect_special_option( $name);373 wp_protect_special_option( $name ); 368 374 369 375 // Get the ID, if no ID then return 370 376 // expected_slashed ($name) 371 $option = $wpdb->get_row("SELECT option_id, autoload FROM $wpdb->options WHERE option_name = '$name'"); 372 if ( !$option->option_id ) return false; 377 $option = $wpdb->get_row( "SELECT option_id, autoload FROM $wpdb->options WHERE option_name = '$name'" ); 378 if ( !$option->option_id ) 379 return false; 373 380 // expected_slashed ($name) 374 $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name = '$name'");381 $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name = '$name'" ); 375 382 if ( 'yes' == $option->autoload ) { 376 383 $alloptions = wp_load_alloptions(); 377 if ( isset( $alloptions[$name]) ) {378 unset( $alloptions[$name]);379 wp_cache_set( 'alloptions', $alloptions, 'options');384 if ( isset( $alloptions[$name] ) ) { 385 unset( $alloptions[$name] ); 386 wp_cache_set( 'alloptions', $alloptions, 'options' ); 380 387 } 381 388 } else { 382 wp_cache_delete( $name, 'options');389 wp_cache_delete( $name, 'options' ); 383 390 } 384 391 return true; 385 392 } 386 393 387 function maybe_serialize($data) { 388 if ( is_string($data) ) 389 $data = trim($data); 390 elseif ( is_array($data) || is_object($data) ) 391 return serialize($data); 392 if ( is_serialized($data) ) 393 return serialize($data); 394 395 function maybe_serialize( $data ) { 396 if ( is_string( $data ) ) 397 $data = trim( $data ); 398 elseif ( is_array( $data ) || is_object( $data ) ) 399 return serialize( $data ); 400 if ( is_serialized( $data ) ) 401 return serialize( $data ); 394 402 return $data; 395 403 } 396 404 405 397 406 function gzip_compression() { 398 if ( !get_option( 'gzipcompression' ) ) {407 if ( !get_option( 'gzipcompression' ) || ini_get( 'zlib.output_compression' ) == 'On' || ini_get( 'zlib.output_compression_level' ) > 0 || ini_get( 'output_handler' ) == 'ob_gzhandler' || !extension_loaded( 'zlib' ) ) 399 408 return false; 400 } 401 402 if ( ( ini_get( 'zlib.output_compression' ) == 'On' || ini_get( 'zlib.output_compression_level' ) > 0 ) || ini_get( 'output_handler' ) == 'ob_gzhandler' ) { 403 return false; 404 } 405 406 if ( extension_loaded( 'zlib' ) ) { 407 ob_start( 'ob_gzhandler' ); 408 } 409 } 410 411 function make_url_footnote($content) { 412 preg_match_all('/<a(.+?)href=\"(.+?)\"(.*?)>(.+?)<\/a>/', $content, $matches); 409 ob_start( 'ob_gzhandler' ); 410 } 411 412 413 function make_url_footnote( $content ) { 414 preg_match_all( '/<a(.+?)href=\"(.+?)\"(.*?)>(.+?)<\/a>/', $content, $matches ); 413 415 $j = 0; 414 for ( $i=0; $i<count($matches[0]); $i++) {415 $links_summary = ( !$j) ? "\n" : $links_summary;416 for ( $i=0; $i<count($matches[0]); $i++ ) { 417 $links_summary = ( !$j ) ? "\n" : $links_summary; 416 418 $j++; 417 419 $link_match = $matches[0][$i]; … … 419 421 $link_url = $matches[2][$i]; 420 422 $link_text = $matches[4][$i]; 421 $content = str_replace( $link_match, $link_text.' '.$link_number, $content);422 $link_url = ( (strtolower(substr($link_url,0,7)) != 'http://') && (strtolower(substr($link_url,0,8)) != 'https://')) ? get_option('home') . $link_url : $link_url;423 $links_summary .= "\n" .$link_number.' '.$link_url;424 } 425 $content = strip_tags($content);423 $content = str_replace( $link_match, $link_text . ' ' . $link_number, $content ); 424 $link_url = ( ( strtolower( substr( $link_url, 0, 7 ) ) != 'http://' ) && ( strtolower( substr( $link_url, 0, 8 ) ) != 'https://' ) ) ? get_option( 'home' ) . $link_url : $link_url; 425 $links_summary .= "\n" . $link_number . ' ' . $link_url; 426 } 427 $content = strip_tags( $content ); 426 428 $content .= $links_summary; 427 429 return $content; … … 429 431 430 432 431 function xmlrpc_getposttitle( $content) {433 function xmlrpc_getposttitle( $content ) { 432 434 global $post_default_title; 433 if ( preg_match( '/<title>(.+?)<\/title>/is', $content, $matchtitle) ) {435 if ( preg_match( '/<title>(.+?)<\/title>/is', $content, $matchtitle ) ) { 434 436 $post_title = $matchtitle[0]; 435 $post_title = preg_replace( '/<title>/si', '', $post_title);436 $post_title = preg_replace( '/<\/title>/si', '', $post_title);437 $post_title = preg_replace( '/<title>/si', '', $post_title ); 438 $post_title = preg_replace( '/<\/title>/si', '', $post_title ); 437 439 } else { 438 440 $post_title = $post_default_title; … … 441 443 } 442 444 443 function xmlrpc_getpostcategory($content) { 445 446 function xmlrpc_getpostcategory( $content ) { 444 447 global $post_default_category; 445 if ( preg_match( '/<category>(.+?)<\/category>/is', $content, $matchcat) ) {446 $post_category = trim( $matchcat[1], ',');447 $post_category = explode( ',', $post_category);448 if ( preg_match( '/<category>(.+?)<\/category>/is', $content, $matchcat ) ) { 449 $post_category = trim( $matchcat[1], ',' ); 450 $post_category = explode( ',', $post_category ); 448 451 } else { 449 452 $post_category = $post_default_category; … … 452 455 } 453 456 454 function xmlrpc_removepostdata($content) { 455 $content = preg_replace('/<title>(.+?)<\/title>/si', '', $content); 456 $content = preg_replace('/<category>(.+?)<\/category>/si', '', $content); 457 $content = trim($content); 457 458 function xmlrpc_removepostdata( $content ) { 459 $content = preg_replace( '/<title>(.+?)<\/title>/si', '', $content ); 460 $content = preg_replace( '/<category>(.+?)<\/category>/si', '', $content ); 461 $content = trim( $content ); 458 462 return $content; 459 463 } 460 464 461 function debug_fopen($filename, $mode) { 465 466 function debug_fopen( $filename, $mode ) { 462 467 global $debug; 463 if ( $debug == 1) {464 $fp = fopen( $filename, $mode);468 if ( 1 == $debug ) { 469 $fp = fopen( $filename, $mode ); 465 470 return $fp; 466 471 } else { … … 469 474 } 470 475 471 function debug_fwrite($fp, $string) { 476 477 function debug_fwrite( $fp, $string ) { 472 478 global $debug; 473 if ( $debug == 1 ) {474 fwrite( $fp, $string);475 }476 } 477 478 function debug_fclose( $fp) {479 if ( 1 == $debug ) 480 fwrite( $fp, $string ); 481 } 482 483 484 function debug_fclose( $fp ) { 479 485 global $debug; 480 if ( $debug == 1 ) { 481 fclose($fp); 482 } 486 if ( 1 == $debug ) 487 fclose( $fp ); 483 488 } 484 489 485 490 function do_enclose( $content, $post_ID ) { 486 491 global $wp_version, $wpdb; 487 include_once (ABSPATH . WPINC . '/class-IXR.php');488 489 $log = debug_fopen( ABSPATH . 'enclosures.log', 'a');492 include_once( ABSPATH . WPINC . '/class-IXR.php' ); 493 494 $log = debug_fopen( ABSPATH . 'enclosures.log', 'a' ); 490 495 $post_links = array(); 491 debug_fwrite( $log, 'BEGIN '.date('YmdHis', time())."\n");496 debug_fwrite( $log, 'BEGIN ' . date( 'YmdHis', time() ) . "\n" ); 492 497 493 498 $pung = get_enclosed( $post_ID ); … … 498 503 $any = $ltrs . $gunk . $punc; 499 504 500 preg_match_all( "{\b http : [$any] +? (?= [$punc] * [^$any] | $)}x", $content, $post_links_temp);501 502 debug_fwrite( $log, 'Post contents:');503 debug_fwrite( $log, $content."\n");504 505 foreach ($post_links_temp[0] as $link_test) :506 if ( !in_array( $link_test, $pung) ) :// If we haven't pung it already507 $test = parse_url( $link_test);508 if ( isset( $test['query']) )505 preg_match_all( "{\b http : [$any] +? (?= [$punc] * [^$any] | $)}x", $content, $post_links_temp ); 506 507 debug_fwrite( $log, 'Post contents:' ); 508 debug_fwrite( $log, $content . "\n" ); 509 510 foreach ( $post_links_temp[0] as $link_test ) { 511 if ( !in_array( $link_test, $pung ) ) { // If we haven't pung it already 512 $test = parse_url( $link_test ); 513 if ( isset( $test['query'] ) ) 509 514 $post_links[] = $link_test; 510 elseif ( ($test['path'] != '/') && ($test['path'] != ''))515 elseif ( $test['path'] != '/' && $test['path'] != '' ) 511 516 $post_links[] = $link_test; 512 endif;513 endforeach;514 515 foreach ( $post_links as $url) :516 if ( $url != '' && !$wpdb->get_var( $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE (%s)", $post_ID, $url.'%')) ) {517 } 518 } 519 520 foreach ( $post_links as $url ) { 521 if ( $url != '' && !$wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE (%s)", $post_ID, $url . '%' ) ) ) { 517 522 if ( $headers = wp_get_http_headers( $url) ) { 518 523 $len = (int) $headers['content-length']; … … 521 526 if ( in_array( substr( $type, 0, strpos( $type, "/" ) ), $allowed_types ) ) { 522 527 $meta_value = "$url\n$len\n$type\n"; 523 $wpdb->query( $wpdb->prepare("INSERT INTO `$wpdb->postmeta` ( `post_id` , `meta_key` , `meta_value` )524 VALUES ( %d, 'enclosure' , %s)", $post_ID, $meta_value ));528 $wpdb->query( $wpdb->prepare( "INSERT INTO `$wpdb->postmeta` ( `post_id` , `meta_key` , `meta_value` ) 529 VALUES ( %d, 'enclosure' , %s)", $post_ID, $meta_value ) ); 525 530 } 526 531 } 527 532 } 528 endforeach; 529 } 533 } 534 } 535 530 536 531 537 function wp_get_http_headers( $url, $red = 1 ) { … … 537 543 538 544 $parts = parse_url( $url ); 539 $file = $parts['path'] . ( $parts['query'] ? '?'.$parts['query'] : '');545 $file = $parts['path'] . ( ( $parts['query'] ) ? '?' . $parts['query'] : '' ); 540 546 $host = $parts['host']; 541 547 if ( !isset( $parts['port'] ) ) … … 544 550 $head = "HEAD $file HTTP/1.1\r\nHOST: $host\r\nUser-Agent: WordPress/" . $wp_version . "\r\n\r\n"; 545 551 546 $fp = @fsockopen( $host, $parts['port'], $err_num, $err_msg, 3);552 $fp = @fsockopen( $host, $parts['port'], $err_num, $err_msg, 3 ); 547 553 if ( !$fp ) 548 554 return false; … … 553 559 $response .= fgets( $fp, 2048 ); 554 560 fclose( $fp ); 555 preg_match_all( '/(.*?): (.*)\r/', $response, $matches);556 $count = count( $matches[1]);557 for ( $i = 0; $i < $count; $i++ ) {558 $key = strtolower( $matches[1][$i]);561 preg_match_all( '/(.*?): (.*)\r/', $response, $matches ); 562 $count = count( $matches[1] ); 563 for ( $i = 0; $i < $count; $i++ ) { 564 $key = strtolower( $matches[1][$i] ); 559 565 $headers["$key"] = $matches[2][$i]; 560 566 } 561 567 562 preg_match( '/.*([0-9]{3}).*/', $response, $return);568 preg_match( '/.*([0-9]{3}).*/', $response, $return ); 563 569 $headers['response'] = $return[1]; // HTTP response code eg 204, 200, 404 564 570 565 571 $code = $headers['response']; 566 if ( ( '302' == $code || '301' == $code) && isset($headers['location']) )572 if ( ( '302' == $code || '301' == $code ) && isset( $headers['location'] ) ) 567 573 return wp_get_http_headers( $headers['location'], ++$red ); 568 574 569 575 return $headers; 570 576 } 577 571 578 572 579 function is_new_day() { 573 580 global $day, $previousday; 574 if ( $day != $previousday ) { 575 return(1); 576 } else { 577 return(0); 578 } 579 } 580 581 function build_query($data) { 582 return _http_build_query($data, NULL, '&', '', false); 583 } 581 if ( $day != $previousday ) 582 return 1; 583 else 584 return 0; 585 } 586 587 588 function build_query( $data ) { 589 return _http_build_query( $data, NULL, '&', '', false ); 590 } 591 584 592 585 593 /* … … 595 603 function add_query_arg() { 596 604 $ret = ''; 597 if ( is_array( func_get_arg(0)) ) {598 if ( @func_num_args() < 2 || false === @func_get_arg( 1) )605 if ( is_array( func_get_arg(0) ) ) { 606 if ( @func_num_args() < 2 || false === @func_get_arg( 1 ) ) 599 607 $uri = $_SERVER['REQUEST_URI']; 600 608 else 601 $uri = @func_get_arg( 1);609 $uri = @func_get_arg( 1 ); 602 610 } else { 603 if ( @func_num_args() < 3 || false === @func_get_arg( 2) )611 if ( @func_num_args() < 3 || false === @func_get_arg( 2 ) ) 604 612 $uri = $_SERVER['REQUEST_URI']; 605 613 else 606 $uri = @func_get_arg( 2);607 } 608 609 if ( $frag = strstr( $uri, '#') )610 $uri = substr( $uri, 0, -strlen($frag));614 $uri = @func_get_arg( 2 ); 615 } 616 617 if ( $frag = strstr( $uri, '#' ) ) 618 $uri = substr( $uri, 0, -strlen( $frag ) ); 611 619 else 612 620 $frag = ''; 613 621 614 if ( preg_match( '|^https?://|i', $uri, $matches) ) {622 if ( preg_match( '|^https?://|i', $uri, $matches ) ) { 615 623 $protocol = $matches[0]; 616 $uri = substr( $uri, strlen($protocol));624 $uri = substr( $uri, strlen( $protocol ) ); 617 625 } else { 618 626 $protocol = ''; 619 627 } 620 628 621 if ( strpos($uri, '?') !== false) {622 $parts = explode( '?', $uri, 2);623 if ( 1 == count( $parts) ) {629 if ( strpos( $uri, '?' ) !== false ) { 630 $parts = explode( '?', $uri, 2 ); 631 if ( 1 == count( $parts ) ) { 624 632 $base = '?'; 625 633 $query = $parts[0]; … … 628 636 $query = $parts[1]; 629 637 } 630 } elseif ( !empty($protocol) || strpos($uri, '=') === false ) {638 } elseif ( !empty( $protocol ) || strpos( $uri, '=' ) === false ) { 631 639 $base = $uri . '?'; 632 640 $query = ''; … … 636 644 } 637 645 638 wp_parse_str( $query, $qs);639 $qs = urlencode_deep( $qs); // this re-URL-encodes things that were already in the query string640 if ( is_array( func_get_arg(0)) ) {641 $kayvees = func_get_arg( 0);642 $qs = array_merge( $qs, $kayvees);646 wp_parse_str( $query, $qs ); 647 $qs = urlencode_deep( $qs ); // this re-URL-encodes things that were already in the query string 648 if ( is_array( func_get_arg( 0 ) ) ) { 649 $kayvees = func_get_arg( 0 ); 650 $qs = array_merge( $qs, $kayvees ); 643 651 } else { 644 $qs[func_get_arg( 0)] = func_get_arg(1);652 $qs[func_get_arg( 0 )] = func_get_arg( 1 ); 645 653 } 646 654 647 655 foreach ( $qs as $k => $v ) { 648 656 if ( $v === false ) 649 unset( $qs[$k]);650 } 651 652 $ret = build_query( $qs);653 $ret = trim( $ret, '?');654 $ret = preg_replace( '#=(&|$)#', '$1', $ret);657 unset( $qs[$k] ); 658 } 659 660 $ret = build_query( $qs ); 661 $ret = trim( $ret, '?' ); 662 $ret = preg_replace( '#=(&|$)#', '$1', $ret ); 655 663 $ret = $protocol . $base . $ret . $frag; 656 $ret = rtrim( $ret, '?');664 $ret = rtrim( $ret, '?' ); 657 665 return $ret; 658 666 } 667 659 668 660 669 /* … … 668 677 */ 669 678 670 function remove_query_arg( $key, $query=FALSE) {671 if ( is_array( $key) ) { // removing multiple keys679 function remove_query_arg( $key, $query=FALSE ) { 680 if ( is_array( $key ) ) { // removing multiple keys 672 681 foreach ( (array) $key as $k ) 673 $query = add_query_arg( $k, FALSE, $query);682 $query = add_query_arg( $k, FALSE, $query ); 674 683 return $query; 675 684 } 676 return add_query_arg($key, FALSE, $query); 677 } 678 679 function add_magic_quotes($array) { 685 return add_query_arg( $key, FALSE, $query ); 686 } 687 688 689 function add_magic_quotes( $array ) { 680 690 global $wpdb; 681 691 682 foreach ( $array as $k => $v
