| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | Plugin Name: Bug test case. |
|---|
| 4 | Plugin URI: http://kccricket.net/ |
|---|
| 5 | Description: Demonstrates a bug. |
|---|
| 6 | Version: 1.0 |
|---|
| 7 | Author: Keith "kccricket" Constable |
|---|
| 8 | Author URI: http://kccricket.net/ |
|---|
| 9 | */ |
|---|
| 10 | |
|---|
| 11 | function inject_panel() { |
|---|
| 12 | global $user_ID, $wpdb; |
|---|
| 13 | |
|---|
| 14 | $testarray = array("This is one", "This is 'two'", "This is \"Three'"); |
|---|
| 15 | $testkey = 'kccricket_bugtest_20060220'; |
|---|
| 16 | |
|---|
| 17 | ?> |
|---|
| 18 | |
|---|
| 19 | <div class="wrap"> |
|---|
| 20 | |
|---|
| 21 | <p>Dataset that contains some quotes:<br/> |
|---|
| 22 | <pre><?php var_dump($testarray) ?></pre></p> |
|---|
| 23 | |
|---|
| 24 | <hr/> |
|---|
| 25 | |
|---|
| 26 | <p>Add that array to the current user's metadata:<br/> |
|---|
| 27 | <pre>update_usermeta($user_ID, $testkey, $testarray)</pre></p> |
|---|
| 28 | |
|---|
| 29 | <?php update_usermeta($user_ID, $testkey, $testarray); ?> |
|---|
| 30 | |
|---|
| 31 | <p>That should have just triggered a DB error.</p> |
|---|
| 32 | |
|---|
| 33 | <hr/> |
|---|
| 34 | |
|---|
| 35 | <p>Okay, that's fine. I can just escape the data before it's used:<br/> |
|---|
| 36 | <pre><?php $testarray = array("This is one", "This is \'two\'", "This is \\\"Three\'"); |
|---|
| 37 | var_dump($testarray); ?></pre></p> |
|---|
| 38 | |
|---|
| 39 | <hr/> |
|---|
| 40 | |
|---|
| 41 | <p>Add the new array to the current user's metadata:<br/> |
|---|
| 42 | <pre>update_usermeta($user_ID, $testkey, $testarray) |
|---|
| 43 | |
|---|
| 44 | <?php var_dump( update_usermeta($user_ID, $testkey, $testarray) ); ?></pre></p> |
|---|
| 45 | |
|---|
| 46 | <hr/> |
|---|
| 47 | |
|---|
| 48 | <p>Attempt to retrieve the array from the usermeta:<br/> |
|---|
| 49 | <pre>get_usermeta($user_ID, $testkey) |
|---|
| 50 | |
|---|
| 51 | <?php var_dump( get_usermeta($user_ID, $testkey) ); ?></pre></p> |
|---|
| 52 | |
|---|
| 53 | <p>What the heck? A string? I gave it an array! That's a problem.</p> |
|---|
| 54 | |
|---|
| 55 | <hr/> |
|---|
| 56 | |
|---|
| 57 | <p>Delete the testkey:<br/> |
|---|
| 58 | <pre><?php var_dump( $wpdb->query("DELETE FROM $wpdb->usermeta WHERE user_id = '$user_ID' AND meta_key = '$testkey'") ) ?></pre></p> |
|---|
| 59 | |
|---|
| 60 | </div> |
|---|
| 61 | |
|---|
| 62 | <?php |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | function add_inject_panel() { |
|---|
| 66 | add_submenu_page('plugins.php', 'Bug Test', 'Bug Test', 1, 'bug-test', 'inject_panel'); |
|---|
| 67 | } |
|---|
| 68 | add_action('admin_menu', 'add_inject_panel'); |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | function kccricket_update_usermeta( $user_id, $meta_key, $meta_value ) { |
|---|
| 72 | global $wpdb; |
|---|
| 73 | if ( !is_numeric( $user_id ) ) |
|---|
| 74 | return false; |
|---|
| 75 | $meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key); |
|---|
| 76 | |
|---|
| 77 | if ( is_array($meta_value) || is_object($meta_value) ) |
|---|
| 78 | $meta_value = serialize($meta_value); |
|---|
| 79 | $meta_value = trim( $meta_value ); |
|---|
| 80 | |
|---|
| 81 | if (empty($meta_value)) { |
|---|
| 82 | delete_usermeta($user_id, $meta_key); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | // This is the only change: |
|---|
| 86 | $meta_value = $wpdb->escape($meta_value); |
|---|
| 87 | |
|---|
| 88 | $cur = $wpdb->get_row("SELECT * FROM $wpdb->usermeta WHERE user_id = '$user_id' AND meta_key = '$meta_key'"); |
|---|
| 89 | if ( !$cur ) { |
|---|
| 90 | $wpdb->query("INSERT INTO $wpdb->usermeta ( user_id, meta_key, meta_value ) |
|---|
| 91 | VALUES |
|---|
| 92 | ( '$user_id', '$meta_key', '$meta_value' )"); |
|---|
| 93 | } else if ( $cur->meta_value != $meta_value ) { |
|---|
| 94 | $wpdb->query("UPDATE $wpdb->usermeta SET meta_value = '$meta_value' WHERE user_id = '$user_id' AND meta_key = '$meta_key'"); |
|---|
| 95 | } else { |
|---|
| 96 | return false; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | $user = get_userdata($user_id); |
|---|
| 100 | wp_cache_delete($user_id, 'users'); |
|---|
| 101 | wp_cache_delete($user->user_login, 'userlogins'); |
|---|
| 102 | |
|---|
| 103 | return true; |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | /** PART TWO **/ |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | function inject_panel2() { |
|---|
| 111 | global $wpdb; |
|---|
| 112 | |
|---|
| 113 | $testarray = array("This is one", "This is 'two'", "This is \"Three'"); |
|---|
| 114 | $testkey = 'kccricket_bugtest2_20060220'; |
|---|
| 115 | |
|---|
| 116 | ?> |
|---|
| 117 | |
|---|
| 118 | <div class="wrap"> |
|---|
| 119 | |
|---|
| 120 | <p>Dataset that contains some quotes:<br/> |
|---|
| 121 | <pre><?php var_dump($testarray); ?></pre></p> |
|---|
| 122 | |
|---|
| 123 | <hr/> |
|---|
| 124 | |
|---|
| 125 | <p>Add that array to the options:<br/> |
|---|
| 126 | <pre>update_option($testkey, $testarray) |
|---|
| 127 | |
|---|
| 128 | <?php var_dump( update_option($testkey, $testarray) ); ?></pre></p> |
|---|
| 129 | |
|---|
| 130 | <hr/> |
|---|
| 131 | |
|---|
| 132 | <p>Delete the dataset from the cache:<br/> |
|---|
| 133 | <pre>wp_cache_delete($testkey, 'options') |
|---|
| 134 | |
|---|
| 135 | <?php var_dump( wp_cache_delete($testkey, 'options') ); ?></pre> |
|---|
| 136 | |
|---|
| 137 | <hr/> |
|---|
| 138 | |
|---|
| 139 | <p>Attempt to retrieve the array from the options:<br/> |
|---|
| 140 | <pre>get_option($testkey) |
|---|
| 141 | |
|---|
| 142 | <?php var_dump( get_option($testkey) ); ?></pre></p> |
|---|
| 143 | |
|---|
| 144 | <p>Works as expected.</p> |
|---|
| 145 | |
|---|
| 146 | <hr/> |
|---|
| 147 | |
|---|
| 148 | <p>Delete the testkey:<br/> |
|---|
| 149 | <pre>delete_option($testkey) |
|---|
| 150 | |
|---|
| 151 | <?php var_dump( delete_option($testkey) ) ?></pre></p> |
|---|
| 152 | <?php } |
|---|
| 153 | |
|---|
| 154 | function add_inject_panel2() { |
|---|
| 155 | add_submenu_page('plugins.php', 'Bug Test 2', 'Bug Test 2', 1, 'bug-test2', 'inject_panel2'); |
|---|
| 156 | } |
|---|
| 157 | add_action('admin_menu', 'add_inject_panel2'); |
|---|
| 158 | ?> |
|---|