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();

}




}


Post Comment