Skip to content

Instantly share code, notes, and snippets.

@Cumquat
Created July 29, 2014 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Cumquat/e42102ca812cee88729b to your computer and use it in GitHub Desktop.
Save Cumquat/e42102ca812cee88729b to your computer and use it in GitHub Desktop.
Basic encryption on Silverstripe
*****************
*The Data Object*
*****************
<?php
class TheData extends DataObject {
public static $db = array(
'Name' => 'Varchar',
'SecretData' => 'Varchar'
);
public static $has_many = array(
);
public static $searchable_fields = array(
'Name'
);
public static $summary_fields = array(
'Name' => 'Name',
);
public static $default_sort = "ID";
function getCMSFields() {
$fields = parent::getCMSFields();
return $fields;
}
public function decryptedInfo($toDecrypt) {
$key = '10$b1cf626001';
return rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($toDecrypt), MCRYPT_MODE_CBC, md5(md5($key))), "\0");
}
public function encryptInfo($password) {
$key = '10$b1cf626001';
return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $password, MCRYPT_MODE_CBC, md5(md5($key))));
}
function ELink($action = 'editsecret'){
$cp = DataObject::get_one('TheDataPage');
if(!$action) $action = 'editsecret';
return Controller::join_links($cp->Link(), "$action/" . $this->ID);
}
}
*********************
*The controller page*
*********************
<?php
class TheDataPage extends Page {
private static $db = array(
);
private static $has_one = array(
);
}
class TheDataPage_Controller extends Page_Controller {
public static $allowed_actions = array(
'AddData', 'doAddData', 'encryptInfo', ' decryptedInfo', 'EditData', 'doEditData', 'editsecret'
);
public function init() {
parent::init();
}
function getsecrets() {
return TheData::get();
}
public function decryptedInfo($toDecrypt) {
$key = '10$b1cf626001';
return rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($toDecrypt), MCRYPT_MODE_CBC, md5(md5($key))), "\0");
}
public function encryptInfo($password) {
$key = '10$b1cf626001';
return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $password, MCRYPT_MODE_CBC, md5(md5($key))));
}
function editsecret() {
return $this->renderWith(array('TheDataPage','Page'));
}
public function AddData() {
$form = Form::create(
$this,
"AddData",
FieldList::create(
LiteralField::create("LiteralField","<legend>Add Data</legend>" ),
LiteralField::create("LiteralField","<div class='row'>" ),
LiteralField::create("LiteralField","<div class='large-12 medium-12 columns'>" ),
TextField::create('SecretData', 'Add Secret Data'),
LiteralField::create("LiteralField","</div>" ),
LiteralField::create("LiteralField","</div>" ) //end
),
FieldList::create(
FormAction::create("doAddData")->setTitle("Add Data")->addExtraClass('button tiny right')
)
);
return $form;
}
function doAddData($data, $form){
$submission = new TheData();
$form->saveInto($submission );
$submission->Name = $submission->SecretData;
$submission->SecretData = self::encryptInfo($submission->SecretData);
$submission ->write();
Controller::curr()->redirectback();
}
public function EditData() {
$thesecret = '';
$Params = $this->getURLParams();
if(is_numeric($Params['ID']) && $secrets = TheData::get()->filter(array(
'ID' => $Params['ID']
))->First())
if($secrets){
$thesecret = self::decryptedInfo($secrets->SecretData);
}
$form = Form::create(
$this,
"EditData",
FieldList::create(
LiteralField::create("LiteralField","<legend>Edit Data</legend>" ),
LiteralField::create("LiteralField","<div class='row'>" ),
HiddenField::create('ID', 'aID'),
LiteralField::create("LiteralField","<div class='large-12 medium-12 columns'>" ),
TextField::create("SecretDataEnc")->setTitle("Secrets")
->setValue($thesecret),
LiteralField::create("LiteralField","</div>" ),
LiteralField::create("LiteralField","</div>" ) //end
),
FieldList::create(
FormAction::create("doEditData")->setTitle("Edit Data")->addExtraClass('button tiny right')
)
);
$Params = $this->getURLParams();
if(is_numeric($Params['ID']) && $secrets = TheData::get()->filter(array(
'ID' => $Params['ID']
))->First())
$form->loadDataFrom($secrets->data());
return $form;
}
function doEditData($data, $form){
$theID = $_POST["ID"];
$submission = TheData::get()->byID($theID);
$form->saveInto($submission );
$submission->Name = $submission->SecretDataEnc;
$submission->SecretData = self::encryptInfo($submission->SecretDataEnc);
$submission ->write();
Controller::curr()->redirectback();
}
}
*******************
*The Display Page *
*******************
<div class="large-12 medium-12 columns">
<article>
<h2>The Data</h2>
<table class="full-width">
<thead>
<tr>
<th>ID</th>
<th>Copied Data</th>
<th>Secret Data</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<% if $getsecrets %>
<% loop $getsecrets %>
<tr>
<td>$ID</td>
<td >$Name</td>
<td>$SecretData</td>
<td><a href="$ELink">edit</a>
</tr>
<% end_loop %>
<% else %>
<tr>
<td colspan="7"><p>No results</p></td>
</tr>
<% end_if %>
</tbody>
</table>
<% if $Action = 'index' %>
$AddData
<% else %>
<% if $Action = 'editsecret' %>
$EditData
<% end_if %>
<% end_if %>
</article>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment