14/10/2013 7:48pm

PHP | Working Example - encypt data in DB


<?php


class Client extends DataObject {

public static $db = array (
"FirstName" => "Text",
"LastName" => "Text",
"Email" => "Text",
"PhoneNumber" => "Varchar(9)",
"YearOfBirth" => "Varchar(4)",
"Address1" => "Text",
"Address2" => "Text",
"City" => "Text",
"Province" => "Enum(array(
'BC'=>'British Columbia',
'ON'=>'Ontario',
'NF'=>'Newfoundland',
'NS'=>'Nova Scotia',
'PE'=>'Prince Edward Island',
'NB'=>'New Brunswick',
'QC'=>'Quebec',
'MB'=>'Manitoba',
'SK'=>'Saskatchewan',
'AB'=>'Alberta',
'NT'=>'Northwest Territories',
'YT'=>'Yukon Territory'))",
"PostalCode" => "Varchar(6)",
"AirMilesApproved" => "Boolean",
"AirMilesVerified" => "Boolean",
"AirMilesCounted" => "Boolean"
);

static $has_one = array(
"Trainer" => "Member",
"Supervisor" => "Member",
"Manager" => "Member",
"SerialNumber" => "SerialNumber"
);


static $searchable_fields = array(
"FirstName", "LastName", "AirMilesApproved", "AirMilesVerified", "AirMilesCounted"
);

static $summary_fields = array(
"FirstName", "LastName", "YearOfBirth", "AirMilesApproved", "AirMilesVerified", "AirMilesCounted"
);


function getCMSFields()
{

function trainerList($memberType){

$ListItems = DataObject::get("Member", "MemberType = '$memberType' AND AirMilesID != 0");
if ($ListItems) {
return $ListItems->toDropDownMap('ID', Title, "Please Select", "LastName");
} else {
return false;
}
}

$fields = parent::getCMSFields();



//Main Tab
$fields->addFieldToTab("Root.Main", new TextField('FirstName', 'First Name'));
$fields->addFieldToTab("Root.Main", new TextField('LastName', 'Last Name'));
$fields->addFieldToTab("Root.Main", new TextField('Email', 'Email'));
$fields->addFieldToTab("Root.Main", new TextField('PhoneNumber', 'Phone Number'));
$fields->addFieldToTab("Root.Main", new TextField('YearOfBirth', 'Year Of Birth'));
$fields->addFieldToTab("Root.Main", new TextField('Address1', 'Address'));
$fields->addFieldToTab("Root.Main", new TextField('Address2', 'Apt or Unit'));
$fields->addFieldToTab("Root.Main", new TextField('City'));
$fields->addFieldToTab("Root.Main", new DropDownField("Province", "Province", singleton('Client')->dbObject('Province')->enumValues()), 'Clients');
$fields->addFieldToTab("Root.Main", new TextField('PostalCode', 'Postal Code'));

$fields->push(new DropDownField("TrainerID", "Trainer", trainerList("Trainer")));
$fields->push(new DropDownField("SupervisorID", "Supervisor", trainerList("Supervisor")));
$fields->push(new DropDownField("ManagerID", "Manager", trainerList("Manager")));

return $fields;
}

function getTitle() {
return $this->Surname . ", " . $this->FirstName;
}

protected function getFirstName() {
$toDecrypt = $this->getField("FirstName");

/* Decrypt encrypted string */
$decrypted = $this->decryptInfo($toDecrypt);

return $decrypted;

}

protected function getLastName() {
$toDecrypt = $this->getField("LastName");

/* Decrypt encrypted string */
$decrypted = $this->decryptInfo($toDecrypt);

return $decrypted;

}

protected function getEmail() {
$toDecrypt = $this->getField("Email");

/* Decrypt encrypted string */
$decrypted = $this->decryptInfo($toDecrypt);

return $decrypted;

}

protected function getAddress1() {
$toDecrypt = $this->getField("Address1");

/* Decrypt encrypted string */
$decrypted = $this->decryptInfo($toDecrypt);

return $decrypted;

}

protected function getAddress2() {
$toDecrypt = $this->getField("Address2");

/* Decrypt encrypted string */
$decrypted = $this->decryptInfo($toDecrypt);

return $decrypted;

}

protected function getCity() {
$toDecrypt = $this->getField("City");

/* Decrypt encrypted string */
$decrypted = $this->decryptInfo($toDecrypt);

return $decrypted;

}

protected function getPostalCode() {
$toDecrypt = $this->getField("PostalCode");

/* Decrypt encrypted string */
$decrypted = $this->decryptInfo($toDecrypt);

return $decrypted;

}

protected function getPhoneNumber() {
$toDecrypt = $this->getField("PhoneNumber");

/* Decrypt encrypted string */
$decrypted = $this->decryptInfo($toDecrypt);

return $decrypted;

}


function getManager() {
$managerID = $this->getField("ManagerID");

if ($mangerID != 0) {
$manager = DataObject::get_one("Member","ID = ". $managerID);

$name = $manager->FirstName;
return $name;
} else {
return "No Manager";
}
}

function getSupervisor() {
$supervisorID = $this->getField("SupervisorID");

if ($supervisorID != 0) {
$supervisor = DataObject::get_one("Member","ID = ". $supervisorID);

$name = $supervisor->FirstName;
return $name;
} else {
return "No Supervisor";
}
}

function getTrainer() {
$trainerID = $this->getField("TrainerID");

if ($trainerID != 0) {
$trainer = DataObject::get_one("Member","ID = ". $trainerID);

$name = $trainer->FirstName;
return $name;
} else {
return "No Trainer";
}
}

function getCompany() {
$trainerID = $this->getField("TrainerID");

if ($trainerID != 0) {
$trainer = DataObject::get_one("Member","ID = ". $trainerID);

$companyID = $trainer->Company;

$company = DataObject::get_by_id("Company",$companyID);
return $company->CompanyName;
} else {
return "No Company";
}
}

protected function decryptInfo($toDecrypt) {
/* Open the cipher */
$td = mcrypt_module_open('rijndael-128', '', 'ecb', '');

/* Create the IV and determine the keysize length, use MCRYPT_RAND
* on Windows instead */
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_DEV_RANDOM);
$ks = mcrypt_enc_get_key_size($td);

/* Create key */
$key = substr(md5('very secret key'), 0, $ks);

/* Intialize encryption */
mcrypt_generic_init($td, $key, $iv);

//$sendToDec = base64_decode($toDecrypt2);

/* Decrypt encrypted string */
if(!$toDecrypt){
return "";
}
$decrypted = mdecrypt_generic($td, base64_decode($toDecrypt));

/* Terminate decryption handle and close module */
mcrypt_generic_deinit($td);
mcrypt_module_close($td);

return $decrypted;

}


function onBeforeWrite() {
/* Open the cipher */
$td = mcrypt_module_open('rijndael-128', '', 'ecb', '');

/* Create the IV and determine the keysize length, use MCRYPT_RAND
* on Windows instead */
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_DEV_RANDOM);
$ks = mcrypt_enc_get_key_size($td);

/* Create key */
$key = substr(md5('very secret key'), 0, $ks);

/* Intialize encryption */
mcrypt_generic_init($td, $key, $iv);

//$toEncrypt = $this->getField("FirstName");
$toEncrypt = array (
$this->getField("FirstName"),
$this->getField("LastName"),
$this->getField("Email"),
$this->getField("Address1"),
$this->getField("Address2"),
$this->getField("City"),
$this->getField("PostalCode"),
$this->getField("PhoneNumber")
);

$encrypted = array();

//$encrypted = mcrypt_generic($td, $toEncrypt);

foreach ($toEncrypt as $toBeEncrypted) {
if(!$toBeEncrypted){
array_push($encrypted, $toBeEncrypted);
} else {
$encryptedValue = mcrypt_generic($td, $toBeEncrypted);
array_push($encrypted, $encryptedValue);
}

}


//$toWrite = base64_encode($encrypted);
$this->FirstName = base64_encode($encrypted[0]);
$this->LastName = base64_encode($encrypted[1]);
$this->Email = base64_encode($encrypted[2]);
$this->Address1 = base64_encode($encrypted[3]);
$this->Address2 = base64_encode($encrypted[4]);
$this->City = base64_encode($encrypted[5]);
$this->PostalCode = base64_encode($encrypted[6]);
$this->PhoneNumber = base64_encode($encrypted[7]);

/* Terminate encryption handler */
mcrypt_generic_deinit($td);
mcrypt_module_close($td);



parent::onBeforeWrite();

}

function onAfterWrite(){
$serialID = $this->getField("SerialNumberID");

if ($serialID != 0) {
$serialNumber = DataObject::get_one("SerialNumber","ID = '$serialID'");

$serialNumber->ClientID = $this->getField("ID");

$serialNumber->write();
}

return parent::onAfterWrite();

}




}


49 Comments 49 Solutions

05/01/2015 7:55pm

PHP | Solution - ZnAhJYzxqR

<a href=https://www.facebook.com/buyxanax>buy xanax bars online no prescription</a> xanax expiration - buy xanax online no prescription canada


05/01/2015 7:28pm

PHP | Solution - MzVyYNOqwR

<a href=https://www.facebook.com/buyxanax>xanax for anxiety</a> xanax overdose recovery - generic equivalent of xanax


05/01/2015 8:34am

PHP | Solution - yLJMfZkaWi

<a href=https://www.facebook.com/buyxanax>buy xanax online</a> buy brand xanax online no prescription - xanax lyrics


05/01/2015 6:50am

PHP | Solution - jLxcIFrCPV

<a href=https://www.facebook.com/buyxanax>generic xanax</a> usual dosage xanax anxiety - drug interactions xanax naproxen


04/01/2015 9:56pm

PHP | Solution - oALQcvbbUH

<a href=https://www.facebook.com/buyxanax>buy xanax</a> xanax side effects yahoo answers - xanax and alcohol drugs forum


04/01/2015 7:41pm

PHP | Solution - vGHqkfyKQJ

<a href=https://www.facebook.com/buyxanax>generic xanax</a> xanax keeps me awake - xanax and alcohol poisoning


04/01/2015 3:39pm

PHP | Solution - RTzcurPGXo

<a href=https://www.facebook.com/buyambien>buy ambien</a> ambien withdrawal xanax - generic ambien cost walmart


04/01/2015 12:56pm

PHP | Solution - AAReFurRZl

<a href=https://www.facebook.com/buyambien>zolpidem without prescription</a> ambien pill price - ambien side effects on liver


04/01/2015 12:03pm

PHP | Solution - AYheFigdUS

<a href=https://www.facebook.com/buyxanax>xanax 1mg</a> xanax side effects urination - xanax pills definition


04/01/2015 6:16am

PHP | Solution - UzydPsoDsx

<a href=https://www.facebook.com/buyxanax>buy xanax</a> buy xanax online no prescription mastercard - xanax side effects bad dreams


04/01/2015 1:11am

PHP | Solution - cllFoExYJx

<a href=https://www.facebook.com/buyambien>buy ambien</a> ambien side effects user reviews - ambien cr


03/01/2015 11:37pm

PHP | Solution - yOTsMTLltU

<a href=https://www.facebook.com/buyxanax>xanax 2mg</a> xanax 1 mg . description - norco xanax high


03/01/2015 8:47pm

PHP | Solution - oseGvScXpo

<a href=https://www.facebook.com/buyambien>buy ambien</a> ambien generic version - order online ambien


03/01/2015 5:01pm

PHP | Solution - oHNkrDzfVu

<a href=https://www.facebook.com/buyxanax>buy xanax</a> generic xanax 3 mg - 6 xanax overdose


03/01/2015 2:11pm

PHP | Solution - iOQMFnXjhQ

<a href=https://www.facebook.com/buyambien>order ambien</a> klonopin vs ambien sleep - generic of ambien


03/01/2015 1:38pm

PHP | Solution - BAotBRzISz

<a href=https://www.facebook.com/buyxanax>xanax antidepressant</a> over counter drug test xanax - xanax for anxiety and panic


03/01/2015 8:44am

PHP | Solution - cKJFkzazLX

<a href=https://www.facebook.com/buyambien>buy ambien online without prescription</a> will ambien show up random drug test - when does generic ambien cr come out


03/01/2015 5:08am

PHP | Solution - klTQXaTpIs

<a href=https://www.facebook.com/buyxanax>xanax online prescription</a> xanax generic manufacturers - will 1mg xanax do anything


03/01/2015 3:18am

PHP | Solution - iFRdVTDkXq

<a href=https://www.facebook.com/buyambien>buy ambien</a> how much ambien cost - buy brand name ambien online


03/01/2015 2:51am

PHP | Solution - MRnHWbaHkx

<a href=https://www.facebook.com/buyxanax>where can i buy xanax online</a> buy xanax online from canada - xanax 0.25


02/01/2015 8:21pm

PHP | Solution - wFcpOgpHTk

<a href=https://www.facebook.com/buyambien>buy ambien</a> ambien withdrawal paranoia - ambien side effects on the elderly


02/01/2015 6:26pm

PHP | Solution - yNScOJGOtV

<a href=https://www.facebook.com/buyxanax>xanax generic</a> xanax generic pills look like - xanax dosage in elderly


02/01/2015 6:03pm

PHP | Solution - wcvSCUiPMq

<a href=https://www.facebook.com/buyambien>online ambien</a> ambien vs restoril - ambien side effects if you stay awake


02/01/2015 5:45pm

PHP | Solution - eERISxHItF

<a href=https://www.facebook.com/buyxanax>buy xanax</a> many xanax bars kill you - xanax for social anxiety


02/01/2015 8:26am

PHP | Solution - TKhuQRpkMZ

<a href=https://www.facebook.com/buyambien>buy ambien</a> zolpidem tartrate versus ambien - ambien cr discount card


02/01/2015 7:42am

PHP | Solution - tUTpqYRjiu

<a href=https://www.facebook.com/buyxanax>buy xanax online no prescription</a> xanax 4mg bars - xanax buy online mastercard


02/01/2015 6:33am

PHP | Solution - gSQuPYlprO

<a href=https://www.facebook.com/buyxanax>buy xanax</a> xanax dose for recreational use - xanax bars in smoke shops


02/01/2015 4:23am

PHP | Solution - HRapqPGNXp

<a href=https://www.facebook.com/buyambien>buy ambien</a> ambien side effects in elderly - what is zolpidem ambien used for


01/01/2015 10:20pm

PHP | Solution - XrpxSbdPnK

<a href=https://www.facebook.com/buyambien>ambien online</a> ambien cr memory loss - buy ambien cod


01/01/2015 9:43pm

PHP | Solution - SHUGKrYwSh

<a href=https://www.facebook.com/buyxanax>is it illegal to buy xanax online</a> alprazolam0 5mg germed ms - buy xanax online mexico


01/01/2015 7:31pm

PHP | Solution - tHuwLjciMY

<a href=https://www.facebook.com/buyxanax>buy xanax</a> what does generic xanax look like pill - generic xanax rating


01/01/2015 12:40pm

PHP | Solution - izXGzJTqbb

<a href=https://www.facebook.com/buyambien>buy ambien</a> generic ambien online pharmacy - ambien side effects irritability


01/01/2015 12:29pm

PHP | Solution - BqUIfxBJqa

<a href=https://www.facebook.com/buyambien>buy ambien</a> ambien cr and sleepwalking - duration of ambien withdrawal


01/01/2015 7:52am

PHP | Solution - lDkGjKfVZD

<a href=https://www.facebook.com/buyxanax>buy xanax</a> xanax buy online mastercard - xanax generic . 5 mg


01/01/2015 1:21am

PHP | Solution - QrllKubwYI

<a href=https://www.facebook.com/buyambien>cheap ambien</a> order ambien online - ambien cr withdrawal


31/12/2014 9:42pm

PHP | Solution - bSrGuWGlIk

<a href=https://www.facebook.com/buyambien>buy ambien</a> 40 mg ambien high - ambien sleep cd


31/12/2014 8:54pm

PHP | Solution - DyGdGXQHMA

<a href=https://www.facebook.com/buyxanax>how to buy xanax online legally</a> 3 1mg xanax - alprazolam 0 5mg ems ms


31/12/2014 2:46pm

PHP | Solution - eKAZKxQHHF

<a href=https://www.facebook.com/buyambien>buy ambien</a> ambien cr side effects hair loss - seroquel vs ambien sleep


31/12/2014 9:49am

PHP | Solution - LviDcYripk

<a href=https://www.facebook.com/buyxanax>xanax</a> klonopin xanax and alcohol - xanax bars 3mg


31/12/2014 9:39am

PHP | Solution - NyHuTowUGw

<a href=https://www.facebook.com/buyambien>buy ambien</a> buy ambien in mexico - buy ambien online legally


31/12/2014 2:49am

PHP | Solution - QndoDqwSAG

<a href=https://www.facebook.com/buyambien>ambien pill</a> ambien cr hangover - ambien walrus meme


30/12/2014 10:26pm

PHP | Solution - mAFyCOPgLK

<a href=https://www.facebook.com/buyxanax>buy xanax online no rx</a> xanax dosage 257 - xanax dosage airplane


30/12/2014 8:56pm

PHP | Solution - dFXgtrVkpr

<a href=https://www.facebook.com/buyambien>buy ambien</a> buy ambien online with prescription - ambien sale usa


30/12/2014 6:07pm

PHP | Solution - xubcgbXVDf

<a href=https://www.facebook.com/buyambien>ambien sleep medication</a> when does ambien withdrawal start - buy ambien online overnight delivery no prescription


30/12/2014 10:08am

PHP | Solution - tNwweVHLUV

<a href=https://www.facebook.com/buyxanax>buy xanax</a> buy xanax pills online - order xanax cheap online


30/12/2014 12:16am

PHP | Solution - gAaCPIYYho

<a href=https://www.facebook.com/buyambien>ambien no prescription</a> ambien cr recreational use - order ambien from mexico


29/12/2014 5:34am

PHP | Solution - KVdjSNDMJu

<a href=https://www.facebook.com/buyambien>buy ambien</a> ambien side effects sweating - ambien over counter drug


28/12/2014 12:38pm

PHP | Solution - MLnOgaSdwR

<a href=https://www.facebook.com/buyambien>ambien online</a> ambien lounge bean bags - order ambien overnight


27/12/2014 11:00pm

PHP | Solution - OYXyKjNMtZ

<a href=http://www.lanepl.org/buytramadol/#55704>tramadol onlineВ no prescription</a> tramadol 50 mg premature ejaculation - tramadol hcl shelf life


Post Comment