Ticket #2591: serialize-TAKE_3.diff

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

    old new  
    261261        return 0; 
    262262} 
    263263 
    264  
    265264function maybe_unserialize($original) { 
    266         if ( false !== $gm = @ unserialize($original) ) 
    267                 return $gm; 
    268         else 
    269                return $original; 
     265        if ( is_serialized($original) ) 
     266                if ( false !== $gm = @ unserialize($original) ) 
     267                       return $gm; 
     268        return $original; 
    270269} 
    271270 
     271function is_serialized($data) { 
     272        if ( !is_string($data) ) { 
     273                // if it isn't a string, it isn't serialized 
     274                return false; 
     275        } 
     276        $data = trim($data); 
     277        if ( preg_match("/^(a|d|o|b|i|s):[0-9]+:(.*)[;}]/si",$data) ) 
     278                return true; 
     279        return false; 
     280} 
     281 
    272282/* Options functions */ 
    273283 
    274284function get_settings($setting) { 
     
    365375                return true; 
    366376        } 
    367377 
    368         if ( is_array($newvalue) || is_object($newvalue) ) 
    369                 $newvalue = serialize($newvalue); 
     378        $newvalue = prepare_data($newvalue); 
    370379 
    371380        wp_cache_set($option_name, $newvalue, 'options'); 
    372381 
     
    395404        if ( false !== get_option($name) ) 
    396405                return; 
    397406 
    398         if ( is_array($value) || is_object($value) ) 
    399                 $value = serialize($value); 
     407        $value = prepare_data($value); 
    400408 
    401409        wp_cache_set($name, $value, 'options'); 
    402410 
     
    418426        return true; 
    419427} 
    420428 
     429function prepare_data($data) { 
     430        if ( is_string($data) ) 
     431                $data = trim($data); 
     432        elseif ( is_array($data) || is_object($data) ) 
     433                return serialize($data); 
     434        if ( is_serialized($data) ) 
     435                return serialize($data); 
     436        return $data; 
     437} 
     438 
    421439function add_post_meta($post_id, $key, $value, $unique = false) { 
    422440        global $wpdb, $post_meta_cache; 
    423441 
     
    431449        $original = $value; 
    432450        if ( is_array($value) || is_object($value) ) 
    433451                $value = $wpdb->escape(serialize($value)); 
     452        else 
     453                $value = prepare_data($value); 
    434454 
    435455        $wpdb->query("INSERT INTO $wpdb->postmeta (post_id,meta_key,meta_value) VALUES ('$post_id','$key','$value')"); 
    436456 
     
    510530        $original_value = $value; 
    511531        if ( is_array($value) || is_object($value) ) 
    512532                $value = $wpdb->escape(serialize($value)); 
     533        else 
     534                $value = prepare_data($value); 
    513535 
    514536        $original_prev = $prev_value; 
    515537        if ( is_array($prev_value) || is_object($prev_value) ) 
     
    21652187                return false; 
    21662188        $meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key); 
    21672189 
    2168         if ( is_array($meta_value) || is_object($meta_value) ) 
    2169                 $meta_value = serialize($meta_value); 
    2170         $meta_value = trim( $meta_value ); 
     2190        $meta_value = prepare_data($meta_value); 
    21712191 
    2172         if (empty($meta_value)) { 
     2192        if ( empty($meta_value) ) 
    21732193                delete_usermeta($user_id, $meta_key); 
    2174         } 
    21752194 
    21762195        $cur = $wpdb->get_row("SELECT * FROM $wpdb->usermeta WHERE user_id = '$user_id' AND meta_key = '$meta_key'"); 
    21772196        if ( !$cur ) { 
     
    21972216                return false; 
    21982217        $meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key); 
    21992218 
    2200         if ( is_array($meta_value) || is_object($meta_value) ) 
    2201                 $meta_value = serialize($meta_value); 
    2202         $meta_value = trim( $meta_value ); 
     2219        $meta_value = prepare_data($meta_value); 
    22032220 
    22042221        if ( ! empty($meta_value) ) 
    22052222                $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  
    849849                        $style = ''; 
    850850                if ('_' == $entry['meta_key'] { 0 }) 
    851851                        $style .= ' hidden'; 
     852                if ( is_serialized($entry['meta_value']) ) { 
     853                        if ( 's' == $entry['meta_value']{0} ) { 
     854                                // It is a serialized string, so we should display it 
     855                                $entry['meta_value'] = maybe_unserialize($entry['meta_value']); 
     856                        } else { 
     857                                -- $count; 
     858                                continue; 
     859                        } 
     860                } 
    852861                echo " 
    853862                        <tr class='$style'> 
    854863                                <td valign='top'><input name='meta[{$entry['meta_id']}][key]' tabindex='6' type='text' size='20' value='{$entry['meta_key']}' /></td> 
     
    920929 
    921930        $metakeyselect = $wpdb->escape(stripslashes(trim($_POST['metakeyselect']))); 
    922931        $metakeyinput = $wpdb->escape(stripslashes(trim($_POST['metakeyinput']))); 
    923         $metavalue = $wpdb->escape(stripslashes(trim($_POST['metavalue']))); 
     932        $metavalue = prepare_data(stripslashes((trim($_POST['metavalue'])))); 
     933        $metavalue = $wpdb->escape($metavalue); 
    924934 
    925935        if ( ('0' === $metavalue || !empty ($metavalue)) && ((('#NONE#' != $metakeyselect) && !empty ($metakeyselect)) || !empty ($metakeyinput)) ) { 
    926936                // We have a key/value pair. If both the select and the  
     
    948958 
    949959function update_meta($mid, $mkey, $mvalue) { 
    950960        global $wpdb; 
    951  
     961        if ( is_serialized(stripslashes($mvalue)) ) 
     962                $mvalue = serialize($mvalue); 
    952963        return $wpdb->query("UPDATE $wpdb->postmeta SET meta_key = '$mkey', meta_value = '$mvalue' WHERE meta_id = '$mid'"); 
    953964} 
    954965 
  • 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        $disabled = ''; 
     100        if ( is_serialized($option->option_value) ) { 
     101                if ( 's' == $option->option_value{0} ) { 
     102                        // It is a serialized string, so we should display it 
     103                        $value = wp_specialchars(maybe_unserialize($option->option_value)); 
     104                } else { 
     105                        $value = '%SERIALIZED_DATA%'; 
     106                        $disabled = ' disabled="disabled"'; 
     107                } 
     108        } else { 
     109                $value = wp_specialchars($option->option_value); 
     110        } 
    99111        echo " 
    100112<tr> 
    101113        <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> 
     114        <td><input type='text' name='$option->option_name' id='$option->option_name' size='30' value='" . $value . "'$disabled /></td> 
    103115        <td>$option->option_description</td> 
    104116</tr>"; 
    105117endforeach;