Advertisement
Guest User

Untitled

a guest
Jul 12th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.83 KB | None | 0 0
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Muhammad
  5. * Date: 05/07/2016
  6. * Time: 01:20 PM
  7. */
  8.  
  9.  
  10. add_filter('get_user_metadata', 'decrypt_user_meta',10,4);
  11. add_filter('add_user_metadata', 'encrypt_user_meta',10,5);
  12. add_filter('update_user_metadata', 'encrypt_user_meta_in_update',10,5);
  13.  
  14.  
  15. function get_meta_keys_to_encrypt(){
  16. return array('first_name', 'last_name', 'display_name' ); // meta_key s from wp_usermeta table that you want to encrypte
  17. }
  18.  
  19.  
  20. function decrypt_user_meta($value = null ,$object_id,$meta_key,$single){
  21. if(!in_array($meta_key, get_meta_keys_to_encrypt()))
  22. return null;
  23.  
  24. $meta_cache = wp_cache_get($object_id, 'user' . '_meta');
  25.  
  26. if ( !$meta_cache ) {
  27. $meta_cache = update_meta_cache( 'user', array( $object_id ) );
  28. $meta_cache = $meta_cache[$object_id];
  29. }
  30.  
  31. if ( ! $meta_key ) {
  32. return decrypt($meta_cache);
  33. }
  34.  
  35. if ( isset($meta_cache[$meta_key]) ) {
  36. if ( $single )
  37. return decrypt(maybe_unserialize( $meta_cache[$meta_key][0]));
  38. else
  39. return array_map('maybe_unserialize', decrypt($meta_cache[$meta_key]));
  40. }
  41.  
  42. if ($single)
  43. return '';
  44. else
  45. return array();
  46. }
  47.  
  48.  
  49. function encrypt_user_meta($value = null ,$object_id,$meta_key, $meta_value, $unique){
  50.  
  51. if(!in_array($meta_key, get_meta_keys_to_encrypt()))
  52. return null;
  53.  
  54. global $wpdb;
  55. $table = _get_meta_table( 'user' );
  56. $column = sanitize_key('user' . '_id');
  57.  
  58.  
  59.  
  60. if ( $unique && $wpdb->get_var( $wpdb->prepare(
  61. "SELECT COUNT(*) FROM $table WHERE meta_key = %s AND $column = %d",
  62. $meta_key, $object_id ) ) )
  63. return false;
  64.  
  65. $_meta_value = $meta_value;
  66. $meta_value = encrypt(maybe_serialize( $meta_value ));
  67.  
  68. /**
  69. * Fires immediately before meta of a specific type is added.
  70. *
  71. * The dynamic portion of the hook, `$meta_type`, refers to the meta
  72. * object type (comment, post, or user).
  73. *
  74. * @since 3.1.0
  75. *
  76. * @param int $object_id Object ID.
  77. * @param string $meta_key Meta key.
  78. * @param mixed $meta_value Meta value.
  79. */
  80. do_action( "add_user_meta", $object_id, $meta_key, $_meta_value );
  81.  
  82. $result = $wpdb->insert( $table, array(
  83. $column => $object_id,
  84. 'meta_key' => $meta_key,
  85. 'meta_value' => $meta_value
  86. ) );
  87.  
  88. if ( ! $result )
  89. return false;
  90.  
  91. $mid = (int) $wpdb->insert_id;
  92.  
  93. wp_cache_delete($object_id, 'user' . '_meta');
  94.  
  95. /**
  96. * Fires immediately after meta of a specific type is added.
  97. *
  98. * The dynamic portion of the hook, `$meta_type`, refers to the meta
  99. * object type (comment, post, or user).
  100. *
  101. * @since 2.9.0
  102. *
  103. * @param int $mid The meta ID after successful update.
  104. * @param int $object_id Object ID.
  105. * @param string $meta_key Meta key.
  106. * @param mixed $meta_value Meta value.
  107. */
  108. do_action( "added_user_meta", $mid, $object_id, $meta_key, $_meta_value );
  109.  
  110. return $mid;
  111. }
  112.  
  113.  
  114.  
  115. function encrypt_user_meta_in_update($value = null ,$object_id,$meta_key, $meta_value, $prev_value){
  116. if(!in_array($meta_key, get_meta_keys_to_encrypt()))
  117. return null;
  118.  
  119. global $wpdb;
  120. $meta_type = 'user';
  121. $table = _get_meta_table( 'user' );
  122. $column = sanitize_key('user' . '_id');
  123. $id_column = 'umeta_id';
  124.  
  125. $raw_meta_key = $meta_key;
  126. $passed_value = $meta_value;
  127.  
  128.  
  129. // Compare existing value to new value if no prev value given and the key exists only once.
  130. if ( empty($prev_value) ) {
  131. $old_value = get_metadata('user', $object_id, $meta_key);
  132. if ( count($old_value) == 1 ) {
  133. if ( $old_value[0] === $meta_value )
  134. return false;
  135. }
  136. }
  137.  
  138. $meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s AND $column = %d", $meta_key, $object_id ) );
  139. if ( empty( $meta_ids ) ) {
  140. return add_metadata( $meta_type, $object_id, $raw_meta_key, $passed_value );
  141. }
  142.  
  143. $_meta_value = $meta_value;
  144. $meta_value = maybe_serialize( encrypt($meta_value ));
  145.  
  146. $data = compact( 'meta_value' );
  147. $where = array( $column => $object_id, 'meta_key' => $meta_key );
  148.  
  149. if ( !empty( $prev_value ) ) {
  150. $prev_value = maybe_serialize($prev_value);
  151. $where['meta_value'] = encrypt($prev_value);
  152. }
  153.  
  154. foreach ( $meta_ids as $meta_id ) {
  155. /**
  156. * Fires immediately before updating metadata of a specific type.
  157. *
  158. * The dynamic portion of the hook, `$meta_type`, refers to the meta
  159. * object type (comment, post, or user).
  160. *
  161. * @since 2.9.0
  162. *
  163. * @param int $meta_id ID of the metadata entry to update.
  164. * @param int $object_id Object ID.
  165. * @param string $meta_key Meta key.
  166. * @param mixed $meta_value Meta value.
  167. */
  168. do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
  169.  
  170. if ( 'post' == $meta_type ) {
  171. /**
  172. * Fires immediately before updating a post's metadata.
  173. *
  174. * @since 2.9.0
  175. *
  176. * @param int $meta_id ID of metadata entry to update.
  177. * @param int $object_id Object ID.
  178. * @param string $meta_key Meta key.
  179. * @param mixed $meta_value Meta value.
  180. */
  181. do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
  182. }
  183. }
  184.  
  185. $result = $wpdb->update( $table, $data, $where );
  186. if ( ! $result )
  187. return false;
  188.  
  189. wp_cache_delete($object_id, $meta_type . '_meta');
  190.  
  191. foreach ( $meta_ids as $meta_id ) {
  192. /**
  193. * Fires immediately after updating metadata of a specific type.
  194. *
  195. * The dynamic portion of the hook, `$meta_type`, refers to the meta
  196. * object type (comment, post, or user).
  197. *
  198. * @since 2.9.0
  199. *
  200. * @param int $meta_id ID of updated metadata entry.
  201. * @param int $object_id Object ID.
  202. * @param string $meta_key Meta key.
  203. * @param mixed $meta_value Meta value.
  204. */
  205. do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
  206.  
  207. if ( 'post' == $meta_type ) {
  208. /**
  209. * Fires immediately after updating a post's metadata.
  210. *
  211. * @since 2.9.0
  212. *
  213. * @param int $meta_id ID of updated metadata entry.
  214. * @param int $object_id Object ID.
  215. * @param string $meta_key Meta key.
  216. * @param mixed $meta_value Meta value.
  217. */
  218. do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
  219. }
  220. }
  221.  
  222. return true;
  223. }
  224.  
  225. function encrypt($src){
  226. if(is_array($src)){
  227. foreach ($src as $key => $val)
  228. $src[$key] = encrypt_value($val);
  229.  
  230. return $src;
  231. }else
  232. return encrypt_value($src);
  233. }
  234.  
  235. function encrypt_value($data){
  236. $password = "SuperSecretEncryptionKey";
  237.  
  238. $salt = substr(md5(mt_rand(), true), 8);
  239.  
  240. $key = md5($password . $salt, true);
  241. $iv = md5($key . $password . $salt, true);
  242.  
  243. $ct = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_CBC, $iv);
  244.  
  245. return base64_encode('Salted__' . $salt . $ct);
  246. }
  247.  
  248. function decrypt($src){
  249. if(is_array($src)){
  250. foreach ($src as $key => $val)
  251. $src[$key] = decrypt_value($val);
  252. return $src;
  253. }else
  254. return decrypt_value($src);
  255.  
  256. }
  257.  
  258. function decrypt_value($data){
  259. $password = "SuperSecretEncryptionKey";
  260. $data = base64_decode($data);
  261. $salt = substr($data, 8, 8);
  262. $ct = substr($data, 16);
  263.  
  264. $key = md5($password . $salt, true);
  265. $iv = md5($key . $password . $salt, true);
  266.  
  267. $pt = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $ct, MCRYPT_MODE_CBC, $iv);
  268.  
  269. return trim($pt);
  270. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement