kenken999/php
0
1<?php2class RunnerCipherer3{4 public $key = '';5 public $alg = '';6 public $mode = '';7 8 protected $strTableName = '';9 10 /**11 * Instance of RunnerCiphererES class for code-based ciphering12 */13 protected $ESFunctions = null;14 15 /**16 * Instance of ProjectSettings class17 */18 protected $pSet = null;19 20 /**21 * Array of fields which encrypted status already determined22 */23 protected $encryptedFields = array();24 25 /**26 * @type Connection27 */28 protected $connection;29 30 31 function __construct($strTableName, $pSet = null)32 {33 34 $this->strTableName = $strTableName;35 36 $this->setConnection();37 38 if( $this->connection->dbBased() ) {39 $this->key = $this->connection->_encryptInfo["key"];40 $this->alg = $this->connection->_encryptInfo["alg"];41 $this->mode = $this->connection->_encryptInfo["mode"];42 }43 44 if($pSet != null)45 $this->pSet = $pSet;46 else 47 $this->pSet = new ProjectSettings($strTableName);48 }49 50 /**51 * Set the 'connection' property52 */53 protected function setConnection()54 {55 global $cman;56 57 if( $this->strTableName != GLOBAL_PAGES )58 $this->connection = $cman->byTable( $this->strTableName );59 else60 $this->connection = getDefaultConnection();61 62 if ( $this->connection->dbType == nDATABASE_MSSQLServer && $this->connection->_encryptInfo["alg"] == ENCRYPTION_ALG_AES && $this->connection->_encryptInfo["mode"] == ENCRYPTION_DB )63 {64 $symmetricSql = mysprintf("OPEN SYMMETRIC KEY [%s] DECRYPTION BY CERTIFICATE [%s];", array($this->connection->_encryptInfo["slqserverkey"], $this->connection->_encryptInfo["slqservercert"]));65 $this->connection->setInitializingSQL($symmetricSql);66 }67 }68 69 function isEncryptionByPHPEnabled() 70 {71 return $this->connection->isEncryptionByPHPEnabled();72 }73 74 /**75 * DecryptFetchedArray76 * Fetching record from sql result, looking through array of fetched values and decrypted all encrypted fields77 * @param {array} fetchedArray 78 * @return {array} decrypted array79 */80 public function DecryptFetchedArray( $fetchedArray )81 { 82 $result = array();83 84 if($fetchedArray)85 {86 if( !$this->pSet->hasEncryptedFields() || !$this->connection->isEncryptionByPHPEnabled() )87 return $fetchedArray;88 89 foreach ($fetchedArray as $fieldName => $fieldValue)90 {91 $result[ $fieldName ] = $this->DecryptField($fieldName, $fieldValue);92 }93 }94 95 return $result;96 }97 98 /**99 * @param String field100 * @param String101 */102 public function isFieldEncrypted($field)103 { 104 $table = $this->strTableName;105 106 if( array_key_exists($table, $this->encryptedFields) && array_key_exists($field, $this->encryptedFields[ $table ]) )107 return $this->encryptedFields[ $table ][ $field ];108 109 if( !array_key_exists($table, $this->encryptedFields) )110 $this->encryptedFields[ $table ] = array();111 112 $this->encryptedFields[ $table ][ $field ] = $this->pSet->isFieldEncrypted($field);113 return $this->encryptedFields[ $table ][ $field ];114 }115 116 /**117 * @param String field118 * @return Boolean119 */ 120 public function isFieldPHPEncrypted($field)121 {122 return $this->connection->isEncryptionByPHPEnabled() && $this->isFieldEncrypted($field);123 }124 125 /**126 * @param String field127 * @param Mixed value128 * @param String controltype (optional)129 * @param Boolean phpEncryptionOnly (optional)130 */ 131 public function MakeDBValue($field, $value, $controltype = "", $phpEncryptionOnly = false)132 { 133 $ret = prepare_for_db($field, $value, $controltype, "", $this->strTableName);134 if( $ret === false )135 return $ret;136 137 $ret = add_db_quotes($field, $this->EncryptField($field, $ret), $this->strTableName ); 138 139 if( $phpEncryptionOnly )140 return $ret;141 142 return $this->EncryptValueByDB($field, $ret);143 } 144 145 /**146 * @param String field147 * @param Mixed value148 */149 public function AddDBQuotes($field, $value)150 {151 return $this->EncryptValueByDB( $field, add_db_quotes($field, $this->EncryptField($field, $value), $this->strTableName) );152 }153 154 155 /**156 * GetFieldName157 * Add to field name decryption function if field is encrypted by database 158 * @param {string} field name159 * @param {string} alias of field name160 * @param {bool} shows if 'as' construction needed161 * @return {string}162 */163 public function GetFieldName($field, $alias = null, $addAs = false)164 {165 if($this->connection->isEncryptionByPHPEnabled() || !$this->isFieldEncrypted($alias != null ? $alias : $field))166 return $field;167 168 return $this->GetEncryptedFieldName($field, $alias, $addAs);169 }170 171 /**172 * Get an SQL expression retriving the encrypted field's value173 * Please note, when changing this function you should make appropriate changes in wizard method (dynamic permissions --> add new user) #8923174 * @param {string} field175 * @param {string} alias176 * @param {string} addAs177 * @return {string}178 */179 public function GetEncryptedFieldName($field, $alias = null, $addAs = false)180 {181 $result = "";182 if ( $this->connection->_encryptInfo["alg"] == ENCRYPTION_ALG_DES)183 {184 if( $this->connection->dbType == nDATABASE_Oracle ) 185 $result = "utl_raw.cast_to_varchar2(DBMS_CRYPTO.DECRYPT(utl_raw.cast_to_raw(%s), 4353, utl_raw.cast_to_raw('%s')))";186 elseif( $this->connection->dbType == nDATABASE_MSSQLServer ) 187 $result = "CAST(DecryptByPassPhrase(N'%s', %s) as nvarchar)";188 elseif( $this->connection->dbType == nDATABASE_MySQL )189 $result = "cast(DES_DECRYPT(unhex(%s), '%s') as char)";190 elseif( $this->connection->dbType == nDATABASE_PostgreSQL )191 $result = "pgp_sym_decrypt(CAST(%s as bytea), '%s')";192 }193 else if ( $this->connection->_encryptInfo["alg"] == ENCRYPTION_ALG_AES)194 {195 if( $this->connection->dbType == nDATABASE_Oracle )196 {197 $result = "utl_raw.cast_to_varchar2(DBMS_CRYPTO.DECRYPT(utl_raw.cast_to_raw(%s), 4358, utl_raw.cast_to_raw('%s')))";198 $this->key = substr($this->key, 0, 16);199 }200 elseif( $this->connection->dbType == nDATABASE_MSSQLServer )201 { 202 $result = "CAST(DecryptByKey(%s) as nvarchar(4000))";203 $this->key = $field; // for use in first as parametr in mysprintf func204 }205 elseif( $this->connection->dbType == nDATABASE_MySQL )206 $result = "cast(AES_DECRYPT(unhex(%s), '%s') as char)";207 elseif( $this->connection->dbType == nDATABASE_PostgreSQL )208 $result = "pgp_sym_decrypt(CAST(%s as bytea), '%s', 'cipher-algo=aes128')";209 }210 211 if($result == "")212 return $field;213 214 if( $this->connection->dbType == nDATABASE_MSSQLServer ) 215 $result = mysprintf($result, array($this->key, $field));216 else217 $result = mysprintf($result, array($field, $this->key));218 219 return $addAs ? $result." as ".$this->connection->addFieldWrappers($alias != null ? $alias : $field) : $result;220 }221 222 /**223 * EncryptValueByDB224 * Add to field name encryption function if field is encrypted by database 225 * Please note, when changing this function you should make appropriate changes in wizard method (dynamic permissions --> add new user) #8923226 * @param {string} field name227 * @param {mixed} value228 * @return {string}229 */230 public function EncryptValueByDB($field, $value)231 {232 if( !$this->isFieldEncrypted($field) || $this->connection->isEncryptionByPHPEnabled() )233 return $value;234 235 $result = "";236 237 if ( $this->connection->_encryptInfo["alg"] == ENCRYPTION_ALG_DES)238 {239 if( $this->connection->dbType == nDATABASE_Oracle )240 $result = "utl_raw.cast_to_varchar2(DBMS_CRYPTO.ENCRYPT(utl_raw.cast_to_raw(%s), 4353, utl_raw.cast_to_raw('%s')))"; 241 elseif( $this->connection->dbType == nDATABASE_MSSQLServer )242 $result = "EncryptByPassPhrase(N'%s', %s)"; 243 elseif( $this->connection->dbType == nDATABASE_MySQL ) 244 $result = "hex(DES_ENCRYPT(%s, '%s'))"; 245 elseif( $this->connection->dbType == nDATABASE_PostgreSQL ) 246 $result = "pgp_sym_encrypt(%s, '%s')";247 }248 else if ( $this->connection->_encryptInfo["alg"] == ENCRYPTION_ALG_AES)249 {250 if( $this->connection->dbType == nDATABASE_Oracle )251 {252 $result = "utl_raw.cast_to_varchar2(DBMS_CRYPTO.ENCRYPT(utl_raw.cast_to_raw(%s), 4358, utl_raw.cast_to_raw('%s')))"; 253 $this->key = substr($this->key, 0, 16);254 }255 elseif( $this->connection->dbType == nDATABASE_MSSQLServer )256 { 257 $result = "EncryptByKey(Key_GUID('%s'), %s)";258 $this->key = $this->connection->_encryptInfo["slqserverkey"];259 }260 elseif( $this->connection->dbType == nDATABASE_MySQL ) 261 $result = "hex(AES_ENCRYPT(%s, '%s'))"; 262 elseif( $this->connection->dbType == nDATABASE_PostgreSQL ) 263 $result = "pgp_sym_encrypt(%s, '%s', 'cipher-algo=aes128')";264 }265 266 if($result != "")267 {268 if( $this->connection->dbType == nDATABASE_MSSQLServer )269 $result = mysprintf($result, array($this->key, $value));270 else271 $result = mysprintf($result, array($value, $this->key));272 }273 else 274 $result = $value;275 276 return $result;277 }278 279 /**280 * EncryptField281 * Determine if field need to be encrypted and encrypt value if it so 282 * @param {string} field name283 * @param {string} value284 * @return {string} encrypted or plain value285 */286 public function EncryptField($field, $value)287 { 288 if( $this->isFieldEncrypted($field) && $this->connection->isEncryptionByPHPEnabled() )289 {290 if( is_null($this->ESFunctions) )291 $this->ESFunctions = $this->getESFunctions();292 293 return $this->ESFunctions->Encrypt($value);294 }295 296 return $value; 297 }298 299 /**300 * DecryptField301 * Determine if field encrypted and decrypt value if it so 302 * Please note, when changing this function you should make appropriate changes in wizard method (dynamic permissions --> add new user) #8923303 * @param {string} field name304 * @param {string} value305 * @return {string} decrypted or plain value306 */307 public function DecryptField($field, $value)308 {309 if($this->isFieldEncrypted($field) && $this->connection->isEncryptionByPHPEnabled())310 {311 if(is_null($this->ESFunctions))312 $this->ESFunctions = $this->getESFunctions();313 314 return $this->ESFunctions->Decrypt($value);315 }316 return $value; 317 }318 319 function getESFunctions()320 {321 if ( $this->connection->_encryptInfo["alg"] == ENCRYPTION_ALG_DES )322 return new RunnerCiphererDES($this->key);323 if ( $this->connection->_encryptInfo["alg"] == ENCRYPTION_ALG_AES )324 return new RunnerCiphererAES($this->key);325 if ( $this->connection->_encryptInfo["alg"] == ENCRYPTION_ALG_AES_256 )326 return new RunnerCiphererAES($this->key, true); 327 }328 329 /**330 * @param Mixed loginSet (optional)331 * @return Mixed332 */333 public static function getForLogin( $loginSet = null )334 { 335 if( !!Security::loginTable() ) {336 return new RunnerCipherer( "chat126_users1", $loginSet);337 }338 return new RunnerCipherer( GLOBAL_PAGES, null);339 } 340}341?>