PageRenderTime 64ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

/output/classes/cipherer.php

https://gitlab.com/Lidbary/PHPRunner
PHP | 297 lines | 156 code | 47 blank | 94 comment | 49 complexity | 7f8849122e55482ffeb4565e7ebbca9d MD5 | raw file
  1. <?php
  2. class RunnerCipherer
  3. {
  4. public $key = '';
  5. protected $strTableName = '';
  6. /**
  7. * Instance of RunnerCiphererDES class for code-based ciphering
  8. */
  9. protected $DESFunctions = null;
  10. /**
  11. * Instance of ProjectSettings class
  12. */
  13. protected $pSet = null;
  14. /**
  15. * Array of fields which encrypted status already determined
  16. */
  17. protected $encryptedFields = array();
  18. /**
  19. * @type Connection
  20. */
  21. protected $connection;
  22. function RunnerCipherer($strTableName, $pSet = null)
  23. {
  24. $this->key = GetGlobalData("encryptionKey", 'emptykey');
  25. $this->strTableName = $strTableName;
  26. $this->setConnection();
  27. if($pSet != null)
  28. $this->pSet = $pSet;
  29. else
  30. $this->pSet = new ProjectSettings($strTableName);
  31. }
  32. /**
  33. * Set the 'connection' property
  34. */
  35. protected function setConnection()
  36. {
  37. global $cman;
  38. if( $this->strTableName != NOT_TABLE_BASED_TNAME )
  39. $this->connection = $cman->byTable( $this->strTableName );
  40. else
  41. $this->connection = getDefaultConnection();
  42. }
  43. /**
  44. * DecryptFetchedArray
  45. * Fetching record from sql result, looking through array of fetched values and decrypted all encrypted fields
  46. * @param {array} fetchedArray
  47. * @return {array} decrypted array
  48. */
  49. public function DecryptFetchedArray( $fetchedArray )
  50. {
  51. $result = array();
  52. if($fetchedArray)
  53. {
  54. if( !$this->pSet->hasEncryptedFields() || !isEncryptionByPHPEnabled() )
  55. return $fetchedArray;
  56. foreach ($fetchedArray as $fieldName => $fieldValue)
  57. {
  58. $result[ $fieldName ] = $this->DecryptField($fieldName, $fieldValue);
  59. }
  60. }
  61. return $result;
  62. }
  63. /**
  64. * @param String field
  65. * @param String
  66. */
  67. public function isFieldEncrypted($field)
  68. {
  69. $table = $this->strTableName;
  70. if( array_key_exists($table, $this->encryptedFields) && array_key_exists($field, $this->encryptedFields[ $table ]) )
  71. return $this->encryptedFields[ $table ][ $field ];
  72. if( !array_key_exists($table, $this->encryptedFields) )
  73. $this->encryptedFields[ $table ] = array();
  74. $this->encryptedFields[ $table ][ $field ] = $this->pSet->isFieldEncrypted($field);
  75. return $this->encryptedFields[ $table ][ $field ];
  76. }
  77. /**
  78. * @param String field
  79. * @return Boolean
  80. */
  81. public function isFieldPHPEncrypted($field)
  82. {
  83. return isEncryptionByPHPEnabled() && $this->isFieldEncrypted($field);
  84. }
  85. /**
  86. * @param String field
  87. * @param Mixed value
  88. * @param String controltype (optional)
  89. * @param Boolean phpEncryptionOnly (optional)
  90. */
  91. public function MakeDBValue($field, $value, $controltype = "", $phpEncryptionOnly = false)
  92. {
  93. $ret = prepare_for_db($field, $value, $controltype, "", $this->strTableName);
  94. if( $ret === false )
  95. return $ret;
  96. $ret = add_db_quotes($field, $this->EncryptField($field, $ret), $this->strTableName );
  97. if( $phpEncryptionOnly )
  98. return $ret;
  99. return $this->EncryptValueByDB($field, $ret);
  100. }
  101. /**
  102. * @param String field
  103. * @param Mixed value
  104. */
  105. public function AddDBQuotes($field, $value)
  106. {
  107. return $this->EncryptValueByDB( $field, add_db_quotes($field, $this->EncryptField($field, $value), $this->strTableName) );
  108. }
  109. /**
  110. *
  111. */
  112. public function GetLikeClause($field, $value)
  113. {
  114. if( isEncryptionByPHPEnabled() && $this->isFieldEncrypted($field) )
  115. return "=".$this->connection->prepareString( $this->EncryptField($field, $value) );
  116. return " LIKE ".$this->connection->prepareString($value."%");
  117. }
  118. /**
  119. * GetLookupFieldName
  120. * Add to lookup and autofil field name decryption function if master field is encrypted by database
  121. * @param {string} field name
  122. * @param {string} master field name
  123. * @param {string} alias of field name
  124. * @param {bool} shows if 'as' construction needed
  125. * @return {string}
  126. */
  127. public function GetLookupFieldName($field, $fieldForCheck, $alias = null, $addAs = false)
  128. {
  129. if( isEncryptionByPHPEnabled() || !$this->isFieldEncrypted($fieldForCheck) )
  130. return $field;
  131. return $this->GetEncryptedFieldName($field, $alias, $addAs);
  132. }
  133. /**
  134. * GetFieldName
  135. * Add to field name decryption function if field is encrypted by database
  136. * @param {string} field name
  137. * @param {string} alias of field name
  138. * @param {bool} shows if 'as' construction needed
  139. * @return {string}
  140. */
  141. public function GetFieldName($field, $alias = null, $addAs = false)
  142. {
  143. if(isEncryptionByPHPEnabled() || !$this->isFieldEncrypted($alias != null ? $alias : $field))
  144. return $field;
  145. return $this->GetEncryptedFieldName($field, $alias, $addAs);
  146. }
  147. /**
  148. * Get an SQL expression retriving the encrypted field's value
  149. * Please note, when changing this function you should make appropriate changes in wizard method (dynamic permissions --> add new user) #8923
  150. * @param {string} field
  151. * @param {string} alias
  152. * @param {string} addAs
  153. * @return {string}
  154. */
  155. public function GetEncryptedFieldName($field, $alias = null, $addAs = false)
  156. {
  157. $result = "";
  158. if( $this->connection->dbType == nDATABASE_Oracle )
  159. $result = "utl_raw.cast_to_varchar2(DBMS_CRYPTO.DECRYPT(utl_raw.cast_to_raw(%s), 4353, utl_raw.cast_to_raw('%s')))";
  160. elseif( $this->connection->dbType == nDATABASE_MSSQLServer )
  161. $result = "CAST(DecryptByPassPhrase(N'%s', %s) as nvarchar)";
  162. elseif( $this->connection->dbType == nDATABASE_MySQL )
  163. $result = "cast(DES_DECRYPT(unhex(%s), '%s') as char)";
  164. elseif( $this->connection->dbType == nDATABASE_PostgreSQL )
  165. $result = "pgp_sym_decrypt(CAST(%s as bytea), '%s')";
  166. if($result == "")
  167. return $field;
  168. if( $this->connection->dbType == nDATABASE_MSSQLServer )
  169. $result = mysprintf($result, array($this->key, $field));
  170. else
  171. $result = mysprintf($result, array($field, $this->key));
  172. return $addAs ? $result." as ".$this->connection->addFieldWrappers($alias != null ? $alias : $field) : $result;
  173. }
  174. /**
  175. * EncryptValueByDB
  176. * Add to field name encryption function if field is encrypted by database
  177. * Please note, when changing this function you should make appropriate changes in wizard method (dynamic permissions --> add new user) #8923
  178. * @param {string} field name
  179. * @param {mixed} value
  180. * @return {string}
  181. */
  182. public function EncryptValueByDB($field, $value)
  183. {
  184. if( !$this->isFieldEncrypted($field) || isEncryptionByPHPEnabled() )
  185. return $value;
  186. $result = "";
  187. if( $this->connection->dbType == nDATABASE_Oracle )
  188. $result = "utl_raw.cast_to_varchar2(DBMS_CRYPTO.ENCRYPT(utl_raw.cast_to_raw(%s), 4353, utl_raw.cast_to_raw('%s')))";
  189. elseif( $this->connection->dbType == nDATABASE_MSSQLServer )
  190. $result = "EncryptByPassPhrase(N'%s', %s)";
  191. elseif( $this->connection->dbType == nDATABASE_MySQL )
  192. $result = "hex(DES_ENCRYPT(%s, '%s'))";
  193. elseif( $this->connection->dbType == nDATABASE_PostgreSQL )
  194. $result = "pgp_sym_encrypt(%s, '%s')";
  195. if($result != "")
  196. {
  197. if( $this->connection->dbType == nDATABASE_MSSQLServer )
  198. $result = mysprintf($result, array($this->key, $value));
  199. else
  200. $result = mysprintf($result, array($value, $this->key));
  201. }
  202. else
  203. $result = $value;
  204. return $result;
  205. }
  206. /**
  207. * EncryptField
  208. * Determine if field need to be encrypted and encrypt value if it so
  209. * @param {string} field name
  210. * @param {string} value
  211. * @return {string} encrypted or plain value
  212. */
  213. public function EncryptField($field, $value)
  214. {
  215. if( $this->isFieldEncrypted($field) && isEncryptionByPHPEnabled() )
  216. {
  217. if( is_null($this->DESFunctions) )
  218. $this->DESFunctions = new RunnerCiphererDES($this->key);
  219. return $this->DESFunctions->DESEncrypt($value);
  220. }
  221. return $value;
  222. }
  223. /**
  224. * DecryptField
  225. * Determine if field encrypted and decrypt value if it so
  226. * Please note, when changing this function you should make appropriate changes in wizard method (dynamic permissions --> add new user) #8923
  227. * @param {string} field name
  228. * @param {string} value
  229. * @return {string} decrypted or plain value
  230. */
  231. public function DecryptField($field, $value)
  232. {
  233. if($this->isFieldEncrypted($field) && isEncryptionByPHPEnabled())
  234. {
  235. if(is_null($this->DESFunctions))
  236. $this->DESFunctions = new RunnerCiphererDES($this->key);
  237. return $this->DESFunctions->DESDecrypt($value);
  238. }
  239. return $value;
  240. }
  241. /**
  242. * @param Mixed loginSet (optional)
  243. * @return Mixed
  244. */
  245. public static function getForLogin( $loginSet = null )
  246. {
  247. return new RunnerCipherer( NOT_TABLE_BASED_TNAME, null);
  248. }
  249. }
  250. ?>