11/10/2013 11:48am

PHP | Problem - onBeforeWrite Problem


<?php

class ClientAccountList extends DataObject {

private static $singular_name = 'Accounts';

//////////////////////////////////////////////

static $db = array (
'Type' => 'Text',
'Username' => 'Text',
'Password' => 'Text',
'URL' => 'Text',
'Webadmin' => 'Text',
'Editable' => 'Text'
);

static $has_one = array (
'Member' => 'Member'
);

private static $summary_fields = array(
'Member.Email',
'Type',
'URL',
'Username',
'Editable'
);

public function getCMSFields() {

$typeDropdown = new DropdownField(
'Type',
_t('Dict.ACCOUNT_TYPE', 'Account type'),
array(
'mysql' => 'MySQL',
'ftp' => 'FTP',
'web' => 'Web',
'email' => 'EMail'
)
);

$editableDropdown = new DropdownField(
'Editable',
_t('Dict.EDITABLE', 'Editable by user?'),
array(
'false' => _t('Dict.NO', 'No'),
'true' => _t('Dict.YES', 'Yes'),
)
);

$member = Member::get()->map('ID', 'Name');

$memberDropdown = new DropdownField(
'MemberID',
_t('ClientAccountList.MEMBER', 'Client (Owner of the Account)'),
$member
);

return new FieldList(
$memberDropdown,
$typeDropdown,
new TextField('Username', _t('Dict.USERNAME', 'Username')),
new TextField('Password', _t('Dict.PASSWORD', 'Password')),
new TextField('URL', 'URL / Server / DB / EMail'),
new TextField('Webadmin', _t('Dict.WEB_ADMIN', 'Link to Webinterface')),
$editableDropdown
);

}

public function onBeforeWrite() {
$decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($this->Member()->Salt), base64_decode($this->Password), MCRYPT_MODE_CBC, md5(md5($this->Member()->Salt))), "\0");
$this->Password=$decrypted;

parent::onBeforeWrite();
}

public function decryptedPassword() {
return rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($this->Member()->Salt), base64_decode($this->Password), MCRYPT_MODE_CBC, md5(md5($this->Member()->Salt))), "\0");
}

public function encryptPassword($key, $password) {
return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $password, MCRYPT_MODE_CBC, md5(md5($key))));
}

}


Post Comment