07.28 php saved
rpeterson
Tags add more
1.2 MySQL AESENCRYPT  
Note
reffers to http://groups.google.com/group/cake-php/browse_thread/thread/f38b7ddd1f770b3e
  1. <?php
  2. class AesBehavior extends ModelBehavior
  3. {
  4.     var $aesKey;
  5.    
  6.     function setup(&$model, $settings = array())
  7.     {     
  8.         $this->aesKey = AES_SECURE_KEY;     
  9.     }
  10.    
  11.     function beforeFind(&$model, $queryData)
  12.     {
  13.         foreach($model->cryptedFields AS $key => $field)
  14.         {
  15.             if(strstr($queryData['fields'],$field))
  16.             {
  17.                 $queryData['fields'] = str_replace($field,"AES_DECRYPT({$field}, {$this->aesKey})", $queryData['fields']);
  18.             }
  19.         }
  20.        
  21.         return $queryData;
  22.     }
  23.    
  24.     function beforeSave(&$model)
  25.     {      
  26.         foreach($model->data[$model->name] AS $key => $value)
  27.         {
  28.             if(in_array($key,$model->cryptedFields))
  29.             {
  30.                 $model->data[$model->name][$key] = "AES_ENCRYPT({$value}, {$this->aesKey})";
  31.             }
  32.         }      
  33.     }
  34. }
  35. ?>
Parsed in 0.036 seconds, using GeSHi 1.0.7.14

Modify this Paste