views:

65

answers:

2

I'm running a query to mysql that returns encrypted data. I'd like, if possible, to decode the results before sending it to the view. It seems like better form to handle the decoding in the controller (or even the model) rather than inside the view.

I can't seem to wrap my head around how to do it, though.

I was thinking I could iterate through the object, decodode it, and push it to another array that would be sent to the view. Problem with this is I won't know (and need to keep) the indexes of the query.

So the query might return something like:

[id] => 742 
[client_id] => 000105 
[last] => dNXcw6mQPaGQ4rXfgIGJMq1pZ1dYAim0 
[first] => dDF7VoO37qdtYoYKfp1ena5mjBXXU0K3dDlcq1ssSvCgpOx75y0A== 
[middle] =>iXy6OWa48kCamViDZFv++K6okIkalC0am3OMPcBwK8sA==
[phone] => eRY3zBhAw2H8tKE

Any ideas?


Ended up with:

function name(){
    $data['e_key']=$this->e_key;
    $clid = $this->uri->segment(3);
    $name = $this->Clients_model->getNameData('*','client_id='.$clid,'');
    $nameArray= array();
    foreach ($name->result() as $row){
        $x = $row;
        $keys = array('id','client_id');
        $unenc = array();
        foreach ($x as $key=>$value){
            if(! in_array($key, $keys)){
                $unenc[$key]=$this->encrypt->decode($value,$this->e_key);
            }else{
                $unenc[$key]=$value;
            }
        }
        array_push($nameArray,$unenc);
     }
    $data['name'] = $nameArray;
    $this->load->view('names/name_view',$data);

}
+1  A: 

if it's a particular index, you could decode it like

$result['last'] = base64_decode($result['last']);

or in the model, use mutators and accessors:

 public function setUp() {
       $this->setTableName('tablename');
        $this->actAs('Timestampable');
        $this->hasMutator('last', '_encode64');
        $this->hasAccessor('last', '_decode64');
   }

 protected function _encode($value) {
     $this->_set('last',base64_encode($value));
 }
protected function _decode($value) { 
     return base64_decode($value); // not sure on this one - might have to
    //  return  $this->set('last', base64_decode($value));
 }
Dan Heberden
+1  A: 

Assuming you know how to decrypt the data, it's but a matter of iterating over the object, decrypting the encrypted fields.

If $YOUR_OBJECT is your object and your function for decryption is decode() then the following code should do the trick.

// The keys corresponding to the encrypted fields
$encoded = array('last', 'first', 'middle', 'phone');

$decoded = array();
foreach($YOUR_OBJECT as $key => $value)
{
    if (in_array($key, $encoded))
    {
        $decoded[$key] = decode($value);
    }
}
Zackman
So I *have* to know the keys? I was just thinking it would be nice if there was a way to iterate through the object without knowing the keys so it would be reusable. Is this possible? Thanks!
stormdrain
How much of your data is encrypted? You'll need to either specify the keys that are encrypted or the keys that aren't. Using ` $unencoded = array('id');` and `if ( ! in_array($key, $unencoded))` might be better in case you have only a few unencrypted fields.How do you encrypt the data? Depending on the encryption, you might be able to detect which fields to decrypt without specifying the fields.
Zackman