Ticket #2591: serialization.diff

File serialization.diff, 6.4 kB (added by markjaquith, 2 years ago)
  • wp-includes/functions.php

    old new  
    261261        return 0; 
    262262} 
    263263 
    264  
     264/* 
    265265function maybe_unserialize($original) { 
    266266        if ( false !== $gm = @ unserialize($original) ) 
    267267                return $gm; 
    268268        else 
    269269                return $original; 
    270270} 
     271*/ 
    271272 
     273function maybe_unserialize($original) { 
     274        if ( is_serialized($original) ) 
     275                if ( false !== $gm = @ unserialize($original) ) 
     276                        return $gm; 
     277        return $original; 
     278} 
     279 
     280function is_serialized($data) { 
     281        if ( !is_string($data) ) { 
     282                // if it isn't a string, it isn't serialized 
     283                return false; 
     284        } 
     285        $data = trim($data); 
     286        if ( preg_match("/^(a|d|o|b|i|s):[0-9]+:(.*)[;}]/si",$data) ) 
     287                return true; 
     288        return false; 
     289} 
     290 
     291function throw_serialization_error($data) { 
     292        die(__('<strong>Error:</strong> serialized data was detected!')); 
     293} 
     294 
    272295/* Options functions */ 
    273296 
    274297function get_settings($setting) { 
     
    348371        return apply_filters('all_options', $all_options); 
    349372} 
    350373 
    351 function update_option($option_name, $newvalue) { 
     374function update_option($option_name, $newvalue, $accept_serialized=false) { 
    352375        global $wpdb; 
    353376 
    354377        if ( is_string($newvalue) ) 
     
    365388                return true; 
    366389        } 
    367390 
    368         if ( is_array($newvalue) || is_object($newvalue) ) 
    369                 $newvalue = serialize($newvalue); 
     391        $newvalue = prepare_data($newvalue, $accept_serialized); 
    370392 
    371393        wp_cache_set($option_name, $newvalue, 'options'); 
    372394 
     
    395417        if ( false !== get_option($name) ) 
    396418                return; 
    397419 
    398         if ( is_array($value) || is_object($value) ) 
    399                 $value = serialize($value); 
     420        $value = prepare_data($value); 
    400421 
    401422        wp_cache_set($name, $value, 'options'); 
    402423 
     
    418439        return true; 
    419440} 
    420441 
     442function prepare_data($data, $accept_serialized=false) { 
     443        if ( is_string($data) ) 
     444                $data = trim($data); 
     445        elseif ( is_array($data) || is_object($data) ) 
     446                return serialize($data); 
     447        if ( !$accept_serialized && is_serialized($data) ) 
     448                throw_serialization_error($data); 
     449        return $data; 
     450} 
     451 
    421452function add_post_meta($post_id, $key, $value, $unique = false) { 
    422453        global $wpdb, $post_meta_cache; 
    423454 
     
    431462        $original = $value; 
    432463        if ( is_array($value) || is_object($value) ) 
    433464                $value = $wpdb->escape(serialize($value)); 
     465        else 
     466                $value = prepare_data($value); 
    434467 
    435468        $wpdb->query("INSERT INTO $wpdb->postmeta (post_id,meta_key,meta_value) VALUES ('$post_id','$key','$value')"); 
    436469 
     
    510543        $original_value = $value; 
    511544        if ( is_array($value) || is_object($value) ) 
    512545                $value = $wpdb->escape(serialize($value)); 
     546        else 
     547                $value = prepare_data($value); 
    513548 
    514549        $original_prev = $prev_value; 
    515550        if ( is_array($prev_value) || is_object($prev_value) ) 
     
    21652200                return false; 
    21662201        $meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key); 
    21672202 
    2168         if ( is_array($meta_value) || is_object($meta_value) ) 
    2169                 $meta_value = serialize($meta_value); 
    2170         $meta_value = trim( $meta_value ); 
     2203        $meta_value = prepare_data($meta_value); 
    21712204 
    21722205        if (empty($meta_value)) { 
    21732206                delete_usermeta($user_id, $meta_key); 
     
    21972230                return false; 
    21982231        $meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key); 
    21992232 
    2200         if ( is_array($meta_value) || is_object($meta_value) ) 
    2201                 $meta_value = serialize($meta_value); 
    2202         $meta_value = trim( $meta_value ); 
     2233        $meta_value = prepare_data($meta_value); 
    22032234 
    22042235        if ( ! empty($meta_value) ) 
    22052236                $wpdb->query("DELETE FROM $wpdb->usermeta WHERE user_id = '$user_id' AND meta_key = '$meta_key' AND meta_value = '$meta_value'"); 
  • wp-admin/admin-functions.php

    old new  
    226226        // Meta Stuff 
    227227        if ($_POST['meta']) { 
    228228                foreach ($_POST['meta'] as $key => $value) 
    229                         update_meta($key, $value['key'], $value['value']); 
     229                        if ( '%SERIALIZED_DATA%' != $value ) 
     230                                update_meta($key, $value['key'], $value['value']); 
    230231        } 
    231232 
    232233        if ($_POST['deletemeta']) { 
     
    849850                        $style = ''; 
    850851                if ('_' == $entry['meta_key'] { 0 }) 
    851852                        $style .= ' hidden'; 
     853                if ( is_serialized($entry['meta_value']) ) { 
     854                        -- $count; 
     855                        continue; 
     856                } 
    852857                echo " 
    853858                        <tr class='$style'> 
    854859                                <td valign='top'><input name='meta[{$entry['meta_id']}][key]' tabindex='6' type='text' size='20' value='{$entry['meta_key']}' /></td> 
     
    920925 
    921926        $metakeyselect = $wpdb->escape(stripslashes(trim($_POST['metakeyselect']))); 
    922927        $metakeyinput = $wpdb->escape(stripslashes(trim($_POST['metakeyinput']))); 
    923         $metavalue = $wpdb->escape(stripslashes(trim($_POST['metavalue']))); 
     928        $metavalue = prepare_data(stripslashes((trim($_POST['metavalue'])))); 
     929        if ( '%SERIALIZED_DATA%' == $metavalue ) 
     930                return; 
     931        $metavalue = $wpdb->escape($metavalue); 
    924932 
    925933        if ( ('0' === $metavalue || !empty ($metavalue)) && ((('#NONE#' != $metakeyselect) && !empty ($metakeyselect)) || !empty ($metakeyinput)) ) { 
    926934                // We have a key/value pair. If both the select and the  
     
    948956 
    949957function update_meta($mid, $mkey, $mvalue) { 
    950958        global $wpdb; 
    951  
     959        if ( is_serialized(stripslashes($mvalue)) ) 
     960                return false; 
    952961        return $wpdb->query("UPDATE $wpdb->postmeta SET meta_key = '$mkey', meta_value = '$mvalue' WHERE meta_id = '$mid'"); 
    953962} 
    954963 
  • wp-admin/options.php

    old new  
    3333 
    3434        if (!$_POST['page_options']) { 
    3535                foreach ($_POST as $key => $value) { 
    36                         $options[] = $key; 
     36                        if ( $value != '%SERIALIZED_DATA%') 
     37                                $options[] = $key; 
    3738                } 
    3839        } else { 
    3940                $options = explode(',', stripslashes($_POST['page_options'])); 
     
    9596$options = $wpdb->get_results("SELECT * FROM $wpdb->options ORDER BY option_name"); 
    9697 
    9798foreach ($options as $option) : 
    98         $value = wp_specialchars($option->option_value); 
     99        if ( is_serialized($option->option_value) ) { 
     100                $value = '%SERIALIZED_DATA%'; 
     101                $disabled = ' disabled="disabled"'; 
     102        } else { 
     103                $value = wp_specialchars($option->option_value); 
     104                $disabled = ''; 
     105        } 
    99106        echo " 
    100107<tr> 
    101108        <th scope='row'><label for='$option->option_name'>$option->option_name</label></th> 
    102         <td><input type='text' name='$option->option_name' id='$option->option_name' size='30' value='" . $value . "' /></td> 
     109        <td><input type='text' name='$option->option_name' id='$option->option_name' size='30' value='" . $value . "'$disabled /></td> 
    103110        <td>$option->option_description</td> 
    104111</tr>"; 
    105112endforeach;