@@ -18,33 +18,33 @@ discard block |
||
| 18 | 18 | |
| 19 | 19 | |
| 20 | 20 | class DB { |
| 21 | - // initial connection |
|
| 22 | - public static $dbName = ''; |
|
| 23 | - public static $user = ''; |
|
| 24 | - public static $password = ''; |
|
| 25 | - public static $host = 'localhost'; |
|
| 26 | - public static $port = null; |
|
| 27 | - public static $encoding = 'utf8'; |
|
| 28 | - |
|
| 29 | - // configure workings |
|
| 30 | - public static $param_char = '%'; |
|
| 31 | - public static $named_param_seperator = '_'; |
|
| 32 | - public static $success_handler = false; |
|
| 33 | - public static $error_handler = true; |
|
| 34 | - public static $throw_exception_on_error = false; |
|
| 35 | - public static $nonsql_error_handler = null; |
|
| 36 | - public static $throw_exception_on_nonsql_error = false; |
|
| 37 | - public static $nested_transactions = false; |
|
| 38 | - public static $usenull = true; |
|
| 39 | - |
|
| 40 | - // internal |
|
| 41 | - protected static $mdb = null; |
|
| 42 | - |
|
| 43 | - public static function getMDB() { |
|
| 21 | + // initial connection |
|
| 22 | + public static $dbName = ''; |
|
| 23 | + public static $user = ''; |
|
| 24 | + public static $password = ''; |
|
| 25 | + public static $host = 'localhost'; |
|
| 26 | + public static $port = null; |
|
| 27 | + public static $encoding = 'utf8'; |
|
| 28 | + |
|
| 29 | + // configure workings |
|
| 30 | + public static $param_char = '%'; |
|
| 31 | + public static $named_param_seperator = '_'; |
|
| 32 | + public static $success_handler = false; |
|
| 33 | + public static $error_handler = true; |
|
| 34 | + public static $throw_exception_on_error = false; |
|
| 35 | + public static $nonsql_error_handler = null; |
|
| 36 | + public static $throw_exception_on_nonsql_error = false; |
|
| 37 | + public static $nested_transactions = false; |
|
| 38 | + public static $usenull = true; |
|
| 39 | + |
|
| 40 | + // internal |
|
| 41 | + protected static $mdb = null; |
|
| 42 | + |
|
| 43 | + public static function getMDB() { |
|
| 44 | 44 | $mdb = DB::$mdb; |
| 45 | 45 | |
| 46 | 46 | if ($mdb === null) { |
| 47 | - $mdb = DB::$mdb = new MeekroDB(); |
|
| 47 | + $mdb = DB::$mdb = new MeekroDB(); |
|
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | if ($mdb->param_char !== DB::$param_char) $mdb->param_char = DB::$param_char; |
@@ -58,91 +58,91 @@ discard block |
||
| 58 | 58 | if ($mdb->usenull !== DB::$usenull) $mdb->usenull = DB::$usenull; |
| 59 | 59 | |
| 60 | 60 | return $mdb; |
| 61 | - } |
|
| 62 | - |
|
| 63 | - public static function get() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'get'), $args); } |
|
| 64 | - public static function disconnect() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'disconnect'), $args); } |
|
| 65 | - public static function query() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'query'), $args); } |
|
| 66 | - public static function queryFirstRow() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryFirstRow'), $args); } |
|
| 67 | - public static function queryOneRow() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryOneRow'), $args); } |
|
| 68 | - public static function queryAllLists() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryAllLists'), $args); } |
|
| 69 | - public static function queryFullColumns() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryFullColumns'), $args); } |
|
| 70 | - public static function queryFirstList() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryFirstList'), $args); } |
|
| 71 | - public static function queryOneList() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryOneList'), $args); } |
|
| 72 | - public static function queryFirstColumn() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryFirstColumn'), $args); } |
|
| 73 | - public static function queryOneColumn() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryOneColumn'), $args); } |
|
| 74 | - public static function queryFirstField() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryFirstField'), $args); } |
|
| 75 | - public static function queryOneField() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryOneField'), $args); } |
|
| 76 | - public static function queryRaw() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryRaw'), $args); } |
|
| 77 | - public static function queryRawUnbuf() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryRawUnbuf'), $args); } |
|
| 78 | - |
|
| 79 | - public static function insert() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'insert'), $args); } |
|
| 80 | - public static function insertIgnore() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'insertIgnore'), $args); } |
|
| 81 | - public static function insertUpdate() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'insertUpdate'), $args); } |
|
| 82 | - public static function replace() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'replace'), $args); } |
|
| 83 | - public static function update() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'update'), $args); } |
|
| 84 | - public static function delete() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'delete'), $args); } |
|
| 85 | - |
|
| 86 | - public static function insertId() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'insertId'), $args); } |
|
| 87 | - public static function count() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'count'), $args); } |
|
| 88 | - public static function affectedRows() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'affectedRows'), $args); } |
|
| 89 | - |
|
| 90 | - public static function useDB() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'useDB'), $args); } |
|
| 91 | - public static function startTransaction() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'startTransaction'), $args); } |
|
| 92 | - public static function commit() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'commit'), $args); } |
|
| 93 | - public static function rollback() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'rollback'), $args); } |
|
| 94 | - public static function tableList() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'tableList'), $args); } |
|
| 95 | - public static function columnList() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'columnList'), $args); } |
|
| 96 | - |
|
| 97 | - public static function sqlEval() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'sqlEval'), $args); } |
|
| 98 | - public static function nonSQLError() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'nonSQLError'), $args); } |
|
| 99 | - |
|
| 100 | - public static function serverVersion() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'serverVersion'), $args); } |
|
| 101 | - public static function transactionDepth() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'transactionDepth'), $args); } |
|
| 102 | - |
|
| 103 | - |
|
| 104 | - public static function debugMode($handler = true) { |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + public static function get() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'get'), $args); } |
|
| 64 | + public static function disconnect() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'disconnect'), $args); } |
|
| 65 | + public static function query() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'query'), $args); } |
|
| 66 | + public static function queryFirstRow() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryFirstRow'), $args); } |
|
| 67 | + public static function queryOneRow() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryOneRow'), $args); } |
|
| 68 | + public static function queryAllLists() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryAllLists'), $args); } |
|
| 69 | + public static function queryFullColumns() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryFullColumns'), $args); } |
|
| 70 | + public static function queryFirstList() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryFirstList'), $args); } |
|
| 71 | + public static function queryOneList() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryOneList'), $args); } |
|
| 72 | + public static function queryFirstColumn() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryFirstColumn'), $args); } |
|
| 73 | + public static function queryOneColumn() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryOneColumn'), $args); } |
|
| 74 | + public static function queryFirstField() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryFirstField'), $args); } |
|
| 75 | + public static function queryOneField() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryOneField'), $args); } |
|
| 76 | + public static function queryRaw() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryRaw'), $args); } |
|
| 77 | + public static function queryRawUnbuf() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryRawUnbuf'), $args); } |
|
| 78 | + |
|
| 79 | + public static function insert() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'insert'), $args); } |
|
| 80 | + public static function insertIgnore() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'insertIgnore'), $args); } |
|
| 81 | + public static function insertUpdate() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'insertUpdate'), $args); } |
|
| 82 | + public static function replace() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'replace'), $args); } |
|
| 83 | + public static function update() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'update'), $args); } |
|
| 84 | + public static function delete() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'delete'), $args); } |
|
| 85 | + |
|
| 86 | + public static function insertId() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'insertId'), $args); } |
|
| 87 | + public static function count() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'count'), $args); } |
|
| 88 | + public static function affectedRows() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'affectedRows'), $args); } |
|
| 89 | + |
|
| 90 | + public static function useDB() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'useDB'), $args); } |
|
| 91 | + public static function startTransaction() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'startTransaction'), $args); } |
|
| 92 | + public static function commit() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'commit'), $args); } |
|
| 93 | + public static function rollback() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'rollback'), $args); } |
|
| 94 | + public static function tableList() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'tableList'), $args); } |
|
| 95 | + public static function columnList() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'columnList'), $args); } |
|
| 96 | + |
|
| 97 | + public static function sqlEval() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'sqlEval'), $args); } |
|
| 98 | + public static function nonSQLError() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'nonSQLError'), $args); } |
|
| 99 | + |
|
| 100 | + public static function serverVersion() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'serverVersion'), $args); } |
|
| 101 | + public static function transactionDepth() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'transactionDepth'), $args); } |
|
| 102 | + |
|
| 103 | + |
|
| 104 | + public static function debugMode($handler = true) { |
|
| 105 | 105 | DB::$success_handler = $handler; |
| 106 | - } |
|
| 106 | + } |
|
| 107 | 107 | |
| 108 | - public function version() { |
|
| 108 | + public function version() { |
|
| 109 | 109 | return $this->version_info(); |
| 110 | - } |
|
| 110 | + } |
|
| 111 | 111 | |
| 112 | 112 | } |
| 113 | 113 | |
| 114 | 114 | |
| 115 | 115 | class MeekroDB { |
| 116 | - // initial connection |
|
| 117 | - public $dbName = ''; |
|
| 118 | - public $user = ''; |
|
| 119 | - public $password = ''; |
|
| 120 | - public $host = 'localhost'; |
|
| 121 | - public $port = null; |
|
| 122 | - public $encoding = 'latin1'; |
|
| 123 | - |
|
| 124 | - // configure workings |
|
| 125 | - public $param_char = '%'; |
|
| 126 | - public $named_param_seperator = '_'; |
|
| 127 | - public $success_handler = false; |
|
| 128 | - public $error_handler = true; |
|
| 129 | - public $throw_exception_on_error = false; |
|
| 130 | - public $nonsql_error_handler = null; |
|
| 131 | - public $throw_exception_on_nonsql_error = false; |
|
| 132 | - public $nested_transactions = false; |
|
| 133 | - public $usenull = true; |
|
| 134 | - |
|
| 135 | - // internal |
|
| 136 | - public $internal_mysql = null; |
|
| 137 | - public $server_info = null; |
|
| 138 | - public $insert_id = 0; |
|
| 139 | - public $num_rows = 0; |
|
| 140 | - public $affected_rows = 0; |
|
| 141 | - public $current_db = null; |
|
| 142 | - public $nested_transactions_count = 0; |
|
| 143 | - |
|
| 144 | - |
|
| 145 | - public function __construct($host=null, $user=null, $password=null, $dbName=null, $port=null, $encoding=null) { |
|
| 116 | + // initial connection |
|
| 117 | + public $dbName = ''; |
|
| 118 | + public $user = ''; |
|
| 119 | + public $password = ''; |
|
| 120 | + public $host = 'localhost'; |
|
| 121 | + public $port = null; |
|
| 122 | + public $encoding = 'latin1'; |
|
| 123 | + |
|
| 124 | + // configure workings |
|
| 125 | + public $param_char = '%'; |
|
| 126 | + public $named_param_seperator = '_'; |
|
| 127 | + public $success_handler = false; |
|
| 128 | + public $error_handler = true; |
|
| 129 | + public $throw_exception_on_error = false; |
|
| 130 | + public $nonsql_error_handler = null; |
|
| 131 | + public $throw_exception_on_nonsql_error = false; |
|
| 132 | + public $nested_transactions = false; |
|
| 133 | + public $usenull = true; |
|
| 134 | + |
|
| 135 | + // internal |
|
| 136 | + public $internal_mysql = null; |
|
| 137 | + public $server_info = null; |
|
| 138 | + public $insert_id = 0; |
|
| 139 | + public $num_rows = 0; |
|
| 140 | + public $affected_rows = 0; |
|
| 141 | + public $current_db = null; |
|
| 142 | + public $nested_transactions_count = 0; |
|
| 143 | + |
|
| 144 | + |
|
| 145 | + public function __construct($host=null, $user=null, $password=null, $dbName=null, $port=null, $encoding=null) { |
|
| 146 | 146 | if ($host === null) $host = DB::$host; |
| 147 | 147 | if ($user === null) $user = DB::$user; |
| 148 | 148 | if ($password === null) $password = DB::$password; |
@@ -156,131 +156,131 @@ discard block |
||
| 156 | 156 | $this->dbName = $dbName; |
| 157 | 157 | $this->port = $port; |
| 158 | 158 | $this->encoding = $encoding; |
| 159 | - } |
|
| 159 | + } |
|
| 160 | 160 | |
| 161 | - public function get() { |
|
| 161 | + public function get() { |
|
| 162 | 162 | $mysql = $this->internal_mysql; |
| 163 | 163 | |
| 164 | 164 | if (!($mysql instanceof MySQLi)) { |
| 165 | - if (! $this->port) $this->port = ini_get('mysqli.default_port'); |
|
| 166 | - $this->current_db = $this->dbName; |
|
| 165 | + if (! $this->port) $this->port = ini_get('mysqli.default_port'); |
|
| 166 | + $this->current_db = $this->dbName; |
|
| 167 | 167 | |
| 168 | - $mysql = new mysqli($this->host, $this->user, $this->password, $this->dbName, $this->port); |
|
| 168 | + $mysql = new mysqli($this->host, $this->user, $this->password, $this->dbName, $this->port); |
|
| 169 | 169 | |
| 170 | - if ($mysql->connect_error) { |
|
| 170 | + if ($mysql->connect_error) { |
|
| 171 | 171 | $this->nonSQLError('Unable to connect to MySQL server! Error: ' . $mysql->connect_error); |
| 172 | - } |
|
| 172 | + } |
|
| 173 | 173 | |
| 174 | - $mysql->set_charset($this->encoding); |
|
| 175 | - $this->internal_mysql = $mysql; |
|
| 176 | - $this->server_info = $mysql->server_info; |
|
| 174 | + $mysql->set_charset($this->encoding); |
|
| 175 | + $this->internal_mysql = $mysql; |
|
| 176 | + $this->server_info = $mysql->server_info; |
|
| 177 | 177 | } |
| 178 | 178 | |
| 179 | 179 | return $mysql; |
| 180 | - } |
|
| 180 | + } |
|
| 181 | 181 | |
| 182 | - public function disconnect() { |
|
| 182 | + public function disconnect() { |
|
| 183 | 183 | $mysqli = $this->internal_mysql; |
| 184 | 184 | if ($mysqli instanceof MySQLi) { |
| 185 | - if ($thread_id = $mysqli->thread_id) $mysqli->kill($thread_id); |
|
| 186 | - $mysqli->close(); |
|
| 185 | + if ($thread_id = $mysqli->thread_id) $mysqli->kill($thread_id); |
|
| 186 | + $mysqli->close(); |
|
| 187 | 187 | } |
| 188 | 188 | $this->internal_mysql = null; |
| 189 | - } |
|
| 189 | + } |
|
| 190 | 190 | |
| 191 | - public function nonSQLError($message) { |
|
| 191 | + public function nonSQLError($message) { |
|
| 192 | 192 | if ($this->throw_exception_on_nonsql_error) { |
| 193 | - $e = new MeekroDBException($message); |
|
| 194 | - throw $e; |
|
| 193 | + $e = new MeekroDBException($message); |
|
| 194 | + throw $e; |
|
| 195 | 195 | } |
| 196 | 196 | |
| 197 | 197 | $error_handler = is_callable($this->nonsql_error_handler) ? $this->nonsql_error_handler : 'meekrodb_error_handler'; |
| 198 | 198 | |
| 199 | 199 | call_user_func($error_handler, array( |
| 200 | - 'type' => 'nonsql', |
|
| 201 | - 'error' => $message |
|
| 200 | + 'type' => 'nonsql', |
|
| 201 | + 'error' => $message |
|
| 202 | 202 | )); |
| 203 | - } |
|
| 203 | + } |
|
| 204 | 204 | |
| 205 | - public function debugMode($handler = true) { |
|
| 205 | + public function debugMode($handler = true) { |
|
| 206 | 206 | $this->success_handler = $handler; |
| 207 | - } |
|
| 207 | + } |
|
| 208 | 208 | |
| 209 | - public function serverVersion() { $this->get(); return $this->server_info; } |
|
| 210 | - public function transactionDepth() { return $this->nested_transactions_count; } |
|
| 211 | - public function insertId() { return $this->insert_id; } |
|
| 212 | - public function affectedRows() { return $this->affected_rows; } |
|
| 213 | - public function count() { $args = func_get_args(); return call_user_func_array(array($this, 'numRows'), $args); } |
|
| 214 | - public function numRows() { return $this->num_rows; } |
|
| 209 | + public function serverVersion() { $this->get(); return $this->server_info; } |
|
| 210 | + public function transactionDepth() { return $this->nested_transactions_count; } |
|
| 211 | + public function insertId() { return $this->insert_id; } |
|
| 212 | + public function affectedRows() { return $this->affected_rows; } |
|
| 213 | + public function count() { $args = func_get_args(); return call_user_func_array(array($this, 'numRows'), $args); } |
|
| 214 | + public function numRows() { return $this->num_rows; } |
|
| 215 | 215 | |
| 216 | - public function useDB() { $args = func_get_args(); return call_user_func_array(array($this, 'setDB'), $args); } |
|
| 217 | - public function setDB($dbName) { |
|
| 216 | + public function useDB() { $args = func_get_args(); return call_user_func_array(array($this, 'setDB'), $args); } |
|
| 217 | + public function setDB($dbName) { |
|
| 218 | 218 | $db = $this->get(); |
| 219 | 219 | if (! $db->select_db($dbName)) $this->nonSQLError("Unable to set database to $dbName"); |
| 220 | 220 | $this->current_db = $dbName; |
| 221 | - } |
|
| 221 | + } |
|
| 222 | 222 | |
| 223 | 223 | |
| 224 | - public function startTransaction() { |
|
| 224 | + public function startTransaction() { |
|
| 225 | 225 | if ($this->nested_transactions && $this->serverVersion() < '5.5') { |
| 226 | - return $this->nonSQLError("Nested transactions are only available on MySQL 5.5 and greater. You are using MySQL " . $this->serverVersion()); |
|
| 226 | + return $this->nonSQLError("Nested transactions are only available on MySQL 5.5 and greater. You are using MySQL " . $this->serverVersion()); |
|
| 227 | 227 | } |
| 228 | 228 | |
| 229 | 229 | if (!$this->nested_transactions || $this->nested_transactions_count == 0) { |
| 230 | - $this->query('START TRANSACTION'); |
|
| 231 | - $this->nested_transactions_count = 1; |
|
| 230 | + $this->query('START TRANSACTION'); |
|
| 231 | + $this->nested_transactions_count = 1; |
|
| 232 | 232 | } else { |
| 233 | - $this->query("SAVEPOINT LEVEL{$this->nested_transactions_count}"); |
|
| 234 | - $this->nested_transactions_count++; |
|
| 233 | + $this->query("SAVEPOINT LEVEL{$this->nested_transactions_count}"); |
|
| 234 | + $this->nested_transactions_count++; |
|
| 235 | 235 | } |
| 236 | 236 | |
| 237 | 237 | return $this->nested_transactions_count; |
| 238 | - } |
|
| 238 | + } |
|
| 239 | 239 | |
| 240 | - public function commit($all=false) { |
|
| 240 | + public function commit($all=false) { |
|
| 241 | 241 | if ($this->nested_transactions && $this->serverVersion() < '5.5') { |
| 242 | - return $this->nonSQLError("Nested transactions are only available on MySQL 5.5 and greater. You are using MySQL " . $this->serverVersion()); |
|
| 242 | + return $this->nonSQLError("Nested transactions are only available on MySQL 5.5 and greater. You are using MySQL " . $this->serverVersion()); |
|
| 243 | 243 | } |
| 244 | 244 | |
| 245 | 245 | if ($this->nested_transactions && $this->nested_transactions_count > 0) |
| 246 | - $this->nested_transactions_count--; |
|
| 246 | + $this->nested_transactions_count--; |
|
| 247 | 247 | |
| 248 | 248 | if (!$this->nested_transactions || $all || $this->nested_transactions_count == 0) { |
| 249 | - $this->nested_transactions_count = 0; |
|
| 250 | - $this->query('COMMIT'); |
|
| 249 | + $this->nested_transactions_count = 0; |
|
| 250 | + $this->query('COMMIT'); |
|
| 251 | 251 | } else { |
| 252 | - $this->query("RELEASE SAVEPOINT LEVEL{$this->nested_transactions_count}"); |
|
| 252 | + $this->query("RELEASE SAVEPOINT LEVEL{$this->nested_transactions_count}"); |
|
| 253 | 253 | } |
| 254 | 254 | |
| 255 | 255 | return $this->nested_transactions_count; |
| 256 | - } |
|
| 256 | + } |
|
| 257 | 257 | |
| 258 | - public function rollback($all=false) { |
|
| 258 | + public function rollback($all=false) { |
|
| 259 | 259 | if ($this->nested_transactions && $this->serverVersion() < '5.5') { |
| 260 | - return $this->nonSQLError("Nested transactions are only available on MySQL 5.5 and greater. You are using MySQL " . $this->serverVersion()); |
|
| 260 | + return $this->nonSQLError("Nested transactions are only available on MySQL 5.5 and greater. You are using MySQL " . $this->serverVersion()); |
|
| 261 | 261 | } |
| 262 | 262 | |
| 263 | 263 | if ($this->nested_transactions && $this->nested_transactions_count > 0) |
| 264 | - $this->nested_transactions_count--; |
|
| 264 | + $this->nested_transactions_count--; |
|
| 265 | 265 | |
| 266 | 266 | if (!$this->nested_transactions || $all || $this->nested_transactions_count == 0) { |
| 267 | - $this->nested_transactions_count = 0; |
|
| 268 | - $this->query('ROLLBACK'); |
|
| 267 | + $this->nested_transactions_count = 0; |
|
| 268 | + $this->query('ROLLBACK'); |
|
| 269 | 269 | } else { |
| 270 | - $this->query("ROLLBACK TO SAVEPOINT LEVEL{$this->nested_transactions_count}"); |
|
| 270 | + $this->query("ROLLBACK TO SAVEPOINT LEVEL{$this->nested_transactions_count}"); |
|
| 271 | 271 | } |
| 272 | 272 | |
| 273 | 273 | return $this->nested_transactions_count; |
| 274 | - } |
|
| 274 | + } |
|
| 275 | 275 | |
| 276 | - protected function formatTableName($table) { |
|
| 276 | + protected function formatTableName($table) { |
|
| 277 | 277 | $table = trim($table, '`'); |
| 278 | 278 | |
| 279 | 279 | if (strpos($table, '.')) return implode('.', array_map(array($this, 'formatTableName'), explode('.', $table))); |
| 280 | 280 | else return '`' . str_replace('`', '``', $table) . '`'; |
| 281 | - } |
|
| 281 | + } |
|
| 282 | 282 | |
| 283 | - public function update() { |
|
| 283 | + public function update() { |
|
| 284 | 284 | $args = func_get_args(); |
| 285 | 285 | $table = array_shift($args); |
| 286 | 286 | $params = array_shift($args); |
@@ -292,95 +292,95 @@ discard block |
||
| 292 | 292 | array_unshift($args, $table); |
| 293 | 293 | array_unshift($args, $query); |
| 294 | 294 | return call_user_func_array(array($this, 'query'), $args); |
| 295 | - } |
|
| 295 | + } |
|
| 296 | 296 | |
| 297 | - public function insertOrReplace($which, $table, $datas, $options=array()) { |
|
| 297 | + public function insertOrReplace($which, $table, $datas, $options=array()) { |
|
| 298 | 298 | $datas = unserialize(serialize($datas)); // break references within array |
| 299 | 299 | $keys = $values = array(); |
| 300 | 300 | |
| 301 | 301 | if (isset($datas[0]) && is_array($datas[0])) { |
| 302 | - foreach ($datas as $datum) { |
|
| 302 | + foreach ($datas as $datum) { |
|
| 303 | 303 | ksort($datum); |
| 304 | 304 | if (! $keys) $keys = array_keys($datum); |
| 305 | 305 | $values[] = array_values($datum); |
| 306 | - } |
|
| 306 | + } |
|
| 307 | 307 | |
| 308 | 308 | } else { |
| 309 | - $keys = array_keys($datas); |
|
| 310 | - $values = array_values($datas); |
|
| 309 | + $keys = array_keys($datas); |
|
| 310 | + $values = array_values($datas); |
|
| 311 | 311 | } |
| 312 | 312 | |
| 313 | 313 | if (isset($options['ignore']) && $options['ignore']) $which = 'INSERT IGNORE'; |
| 314 | 314 | |
| 315 | 315 | if (isset($options['update']) && is_array($options['update']) && $options['update'] && strtolower($which) == 'insert') { |
| 316 | - if (array_values($options['update']) !== $options['update']) { |
|
| 316 | + if (array_values($options['update']) !== $options['update']) { |
|
| 317 | 317 | return $this->query("INSERT INTO %b %lb VALUES %? ON DUPLICATE KEY UPDATE %?", $table, $keys, $values, $options['update']); |
| 318 | - } else { |
|
| 318 | + } else { |
|
| 319 | 319 | $update_str = array_shift($options['update']); |
| 320 | 320 | $query_param = array("INSERT INTO %b %lb VALUES %? ON DUPLICATE KEY UPDATE $update_str", $table, $keys, $values); |
| 321 | 321 | $query_param = array_merge($query_param, $options['update']); |
| 322 | 322 | return call_user_func_array(array($this, 'query'), $query_param); |
| 323 | - } |
|
| 323 | + } |
|
| 324 | 324 | |
| 325 | 325 | } |
| 326 | 326 | |
| 327 | 327 | return $this->query("%l INTO %b %lb VALUES %?", $which, $table, $keys, $values); |
| 328 | - } |
|
| 328 | + } |
|
| 329 | 329 | |
| 330 | - public function insert($table, $data) { return $this->insertOrReplace('INSERT', $table, $data); } |
|
| 331 | - public function insertIgnore($table, $data) { return $this->insertOrReplace('INSERT', $table, $data, array('ignore' => true)); } |
|
| 332 | - public function replace($table, $data) { return $this->insertOrReplace('REPLACE', $table, $data); } |
|
| 330 | + public function insert($table, $data) { return $this->insertOrReplace('INSERT', $table, $data); } |
|
| 331 | + public function insertIgnore($table, $data) { return $this->insertOrReplace('INSERT', $table, $data, array('ignore' => true)); } |
|
| 332 | + public function replace($table, $data) { return $this->insertOrReplace('REPLACE', $table, $data); } |
|
| 333 | 333 | |
| 334 | - public function insertUpdate() { |
|
| 334 | + public function insertUpdate() { |
|
| 335 | 335 | $args = func_get_args(); |
| 336 | 336 | $table = array_shift($args); |
| 337 | 337 | $data = array_shift($args); |
| 338 | 338 | |
| 339 | 339 | if (! isset($args[0])) { // update will have all the data of the insert |
| 340 | - if (isset($data[0]) && is_array($data[0])) { //multiple insert rows specified -- failing! |
|
| 340 | + if (isset($data[0]) && is_array($data[0])) { //multiple insert rows specified -- failing! |
|
| 341 | 341 | $this->nonSQLError("Badly formatted insertUpdate() query -- you didn't specify the update component!"); |
| 342 | - } |
|
| 342 | + } |
|
| 343 | 343 | |
| 344 | - $args[0] = $data; |
|
| 344 | + $args[0] = $data; |
|
| 345 | 345 | } |
| 346 | 346 | |
| 347 | 347 | if (is_array($args[0])) $update = $args[0]; |
| 348 | 348 | else $update = $args; |
| 349 | 349 | |
| 350 | 350 | return $this->insertOrReplace('INSERT', $table, $data, array('update' => $update)); |
| 351 | - } |
|
| 351 | + } |
|
| 352 | 352 | |
| 353 | - public function delete() { |
|
| 353 | + public function delete() { |
|
| 354 | 354 | $args = func_get_args(); |
| 355 | 355 | $table = $this->formatTableName(array_shift($args)); |
| 356 | 356 | $where = array_shift($args); |
| 357 | 357 | $buildquery = "DELETE FROM $table WHERE $where"; |
| 358 | 358 | array_unshift($args, $buildquery); |
| 359 | 359 | return call_user_func_array(array($this, 'query'), $args); |
| 360 | - } |
|
| 360 | + } |
|
| 361 | 361 | |
| 362 | - public function sqleval() { |
|
| 362 | + public function sqleval() { |
|
| 363 | 363 | $args = func_get_args(); |
| 364 | 364 | $text = call_user_func_array(array($this, 'parseQueryParams'), $args); |
| 365 | 365 | return new MeekroDBEval($text); |
| 366 | - } |
|
| 366 | + } |
|
| 367 | 367 | |
| 368 | - public function columnList($table) { |
|
| 368 | + public function columnList($table) { |
|
| 369 | 369 | return $this->queryOneColumn('Field', "SHOW COLUMNS FROM $table"); |
| 370 | - } |
|
| 370 | + } |
|
| 371 | 371 | |
| 372 | - public function tableList($db = null) { |
|
| 372 | + public function tableList($db = null) { |
|
| 373 | 373 | if ($db) { |
| 374 | - $olddb = $this->current_db; |
|
| 375 | - $this->useDB($db); |
|
| 374 | + $olddb = $this->current_db; |
|
| 375 | + $this->useDB($db); |
|
| 376 | 376 | } |
| 377 | 377 | |
| 378 | 378 | $result = $this->queryFirstColumn('SHOW TABLES'); |
| 379 | 379 | if (isset($olddb)) $this->useDB($olddb); |
| 380 | 380 | return $result; |
| 381 | - } |
|
| 381 | + } |
|
| 382 | 382 | |
| 383 | - protected function preparseQueryParams() { |
|
| 383 | + protected function preparseQueryParams() { |
|
| 384 | 384 | $args = func_get_args(); |
| 385 | 385 | $sql = trim(strval(array_shift($args))); |
| 386 | 386 | $args_all = $args; |
@@ -391,32 +391,32 @@ discard block |
||
| 391 | 391 | $named_seperator_length = strlen($this->named_param_seperator); |
| 392 | 392 | |
| 393 | 393 | $types = array( |
| 394 | - $this->param_char . 'll', // list of literals |
|
| 395 | - $this->param_char . 'ls', // list of strings |
|
| 396 | - $this->param_char . 'l', // literal |
|
| 397 | - $this->param_char . 'li', // list of integers |
|
| 398 | - $this->param_char . 'ld', // list of decimals |
|
| 399 | - $this->param_char . 'lb', // list of backticks |
|
| 400 | - $this->param_char . 'lt', // list of timestamps |
|
| 401 | - $this->param_char . 's', // string |
|
| 402 | - $this->param_char . 'i', // integer |
|
| 403 | - $this->param_char . 'd', // double / decimal |
|
| 404 | - $this->param_char . 'b', // backtick |
|
| 405 | - $this->param_char . 't', // timestamp |
|
| 406 | - $this->param_char . '?', // infer type |
|
| 407 | - $this->param_char . 'ss' // search string (like string, surrounded with %'s) |
|
| 394 | + $this->param_char . 'll', // list of literals |
|
| 395 | + $this->param_char . 'ls', // list of strings |
|
| 396 | + $this->param_char . 'l', // literal |
|
| 397 | + $this->param_char . 'li', // list of integers |
|
| 398 | + $this->param_char . 'ld', // list of decimals |
|
| 399 | + $this->param_char . 'lb', // list of backticks |
|
| 400 | + $this->param_char . 'lt', // list of timestamps |
|
| 401 | + $this->param_char . 's', // string |
|
| 402 | + $this->param_char . 'i', // integer |
|
| 403 | + $this->param_char . 'd', // double / decimal |
|
| 404 | + $this->param_char . 'b', // backtick |
|
| 405 | + $this->param_char . 't', // timestamp |
|
| 406 | + $this->param_char . '?', // infer type |
|
| 407 | + $this->param_char . 'ss' // search string (like string, surrounded with %'s) |
|
| 408 | 408 | ); |
| 409 | 409 | |
| 410 | 410 | // generate list of all MeekroDB variables in our query, and their position |
| 411 | 411 | // in the form "offset => variable", sorted by offsets |
| 412 | 412 | $posList = array(); |
| 413 | 413 | foreach ($types as $type) { |
| 414 | - $lastPos = 0; |
|
| 415 | - while (($pos = strpos($sql, $type, $lastPos)) !== false) { |
|
| 414 | + $lastPos = 0; |
|
| 415 | + while (($pos = strpos($sql, $type, $lastPos)) !== false) { |
|
| 416 | 416 | $lastPos = $pos + 1; |
| 417 | 417 | if (isset($posList[$pos]) && strlen($posList[$pos]) > strlen($type)) continue; |
| 418 | 418 | $posList[$pos] = $type; |
| 419 | - } |
|
| 419 | + } |
|
| 420 | 420 | } |
| 421 | 421 | |
| 422 | 422 | ksort($posList); |
@@ -425,24 +425,24 @@ discard block |
||
| 425 | 425 | $chunkyQuery = array(); // preparsed query |
| 426 | 426 | $pos_adj = 0; // how much we've added or removed from the original sql string |
| 427 | 427 | foreach ($posList as $pos => $type) { |
| 428 | - $type = substr($type, $param_char_length); // variable, without % in front of it |
|
| 429 | - $length_type = strlen($type) + $param_char_length; // length of variable w/o % |
|
| 428 | + $type = substr($type, $param_char_length); // variable, without % in front of it |
|
| 429 | + $length_type = strlen($type) + $param_char_length; // length of variable w/o % |
|
| 430 | 430 | |
| 431 | - $new_pos = $pos + $pos_adj; // position of start of variable |
|
| 432 | - $new_pos_back = $new_pos + $length_type; // position of end of variable |
|
| 433 | - $arg_number_length = 0; // length of any named or numbered parameter addition |
|
| 431 | + $new_pos = $pos + $pos_adj; // position of start of variable |
|
| 432 | + $new_pos_back = $new_pos + $length_type; // position of end of variable |
|
| 433 | + $arg_number_length = 0; // length of any named or numbered parameter addition |
|
| 434 | 434 | |
| 435 | - // handle numbered parameters |
|
| 436 | - if ($arg_number_length = strspn($sql, '0123456789', $new_pos_back)) { |
|
| 435 | + // handle numbered parameters |
|
| 436 | + if ($arg_number_length = strspn($sql, '0123456789', $new_pos_back)) { |
|
| 437 | 437 | $arg_number = substr($sql, $new_pos_back, $arg_number_length); |
| 438 | 438 | if (! array_key_exists($arg_number, $args_all)) $this->nonSQLError("Non existent argument reference (arg $arg_number): $sql"); |
| 439 | 439 | |
| 440 | 440 | $arg = $args_all[$arg_number]; |
| 441 | 441 | |
| 442 | - // handle named parameters |
|
| 443 | - } else if (substr($sql, $new_pos_back, $named_seperator_length) == $this->named_param_seperator) { |
|
| 442 | + // handle named parameters |
|
| 443 | + } else if (substr($sql, $new_pos_back, $named_seperator_length) == $this->named_param_seperator) { |
|
| 444 | 444 | $arg_number_length = strspn($sql, 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_', |
| 445 | - $new_pos_back + $named_seperator_length) + $named_seperator_length; |
|
| 445 | + $new_pos_back + $named_seperator_length) + $named_seperator_length; |
|
| 446 | 446 | |
| 447 | 447 | $arg_number = substr($sql, $new_pos_back + $named_seperator_length, $arg_number_length - $named_seperator_length); |
| 448 | 448 | if (count($args_all) != 1 || !is_array($args_all[0])) $this->nonSQLError("If you use named parameters, the second argument must be an array of parameters"); |
@@ -450,38 +450,38 @@ discard block |
||
| 450 | 450 | |
| 451 | 451 | $arg = $args_all[0][$arg_number]; |
| 452 | 452 | |
| 453 | - } else { |
|
| 453 | + } else { |
|
| 454 | 454 | $arg_number = 0; |
| 455 | 455 | $arg = array_shift($args); |
| 456 | - } |
|
| 456 | + } |
|
| 457 | 457 | |
| 458 | - if ($new_pos > 0) $chunkyQuery[] = substr($sql, 0, $new_pos); |
|
| 458 | + if ($new_pos > 0) $chunkyQuery[] = substr($sql, 0, $new_pos); |
|
| 459 | 459 | |
| 460 | - if (is_object($arg) && ($arg instanceof WhereClause)) { |
|
| 460 | + if (is_object($arg) && ($arg instanceof WhereClause)) { |
|
| 461 | 461 | list($clause_sql, $clause_args) = $arg->textAndArgs(); |
| 462 | 462 | array_unshift($clause_args, $clause_sql); |
| 463 | 463 | $preparsed_sql = call_user_func_array(array($this, 'preparseQueryParams'), $clause_args); |
| 464 | 464 | $chunkyQuery = array_merge($chunkyQuery, $preparsed_sql); |
| 465 | - } else { |
|
| 465 | + } else { |
|
| 466 | 466 | $chunkyQuery[] = array('type' => $type, 'value' => $arg); |
| 467 | - } |
|
| 467 | + } |
|
| 468 | 468 | |
| 469 | - $sql = substr($sql, $new_pos_back + $arg_number_length); |
|
| 470 | - $pos_adj -= $new_pos_back + $arg_number_length; |
|
| 469 | + $sql = substr($sql, $new_pos_back + $arg_number_length); |
|
| 470 | + $pos_adj -= $new_pos_back + $arg_number_length; |
|
| 471 | 471 | } |
| 472 | 472 | |
| 473 | 473 | if (strlen($sql) > 0) $chunkyQuery[] = $sql; |
| 474 | 474 | |
| 475 | 475 | return $chunkyQuery; |
| 476 | - } |
|
| 476 | + } |
|
| 477 | 477 | |
| 478 | - protected function escape($str) { return "'" . $this->get()->real_escape_string(strval($str)) . "'"; } |
|
| 478 | + protected function escape($str) { return "'" . $this->get()->real_escape_string(strval($str)) . "'"; } |
|
| 479 | 479 | |
| 480 | - protected function sanitize($value) { |
|
| 480 | + protected function sanitize($value) { |
|
| 481 | 481 | if (is_object($value)) { |
| 482 | - if ($value instanceof MeekroDBEval) return $value->text; |
|
| 483 | - else if ($value instanceof DateTime) return $this->escape($value->format('Y-m-d H:i:s')); |
|
| 484 | - else return ''; |
|
| 482 | + if ($value instanceof MeekroDBEval) return $value->text; |
|
| 483 | + else if ($value instanceof DateTime) return $this->escape($value->format('Y-m-d H:i:s')); |
|
| 484 | + else return ''; |
|
| 485 | 485 | } |
| 486 | 486 | |
| 487 | 487 | if (is_null($value)) return $this->usenull ? 'NULL' : "''"; |
@@ -490,33 +490,33 @@ discard block |
||
| 490 | 490 | else if (is_float($value)) return $value; |
| 491 | 491 | |
| 492 | 492 | else if (is_array($value)) { |
| 493 | - // non-assoc array? |
|
| 494 | - if (array_values($value) === $value) { |
|
| 493 | + // non-assoc array? |
|
| 494 | + if (array_values($value) === $value) { |
|
| 495 | 495 | if (is_array($value[0])) return implode(', ', array_map(array($this, 'sanitize'), $value)); |
| 496 | 496 | else return '(' . implode(', ', array_map(array($this, 'sanitize'), $value)) . ')'; |
| 497 | - } |
|
| 497 | + } |
|
| 498 | 498 | |
| 499 | - $pairs = array(); |
|
| 500 | - foreach ($value as $k => $v) { |
|
| 499 | + $pairs = array(); |
|
| 500 | + foreach ($value as $k => $v) { |
|
| 501 | 501 | $pairs[] = $this->formatTableName($k) . '=' . $this->sanitize($v); |
| 502 | - } |
|
| 502 | + } |
|
| 503 | 503 | |
| 504 | - return implode(', ', $pairs); |
|
| 504 | + return implode(', ', $pairs); |
|
| 505 | 505 | } |
| 506 | 506 | else return $this->escape($value); |
| 507 | - } |
|
| 507 | + } |
|
| 508 | 508 | |
| 509 | - protected function parseTS($ts) { |
|
| 509 | + protected function parseTS($ts) { |
|
| 510 | 510 | if (is_string($ts)) return date('Y-m-d H:i:s', strtotime($ts)); |
| 511 | 511 | else if (is_object($ts) && ($ts instanceof DateTime)) return $ts->format('Y-m-d H:i:s'); |
| 512 | - } |
|
| 512 | + } |
|
| 513 | 513 | |
| 514 | - protected function intval($var) { |
|
| 514 | + protected function intval($var) { |
|
| 515 | 515 | if (PHP_INT_SIZE == 8) return intval($var); |
| 516 | 516 | return floor(doubleval($var)); |
| 517 | - } |
|
| 517 | + } |
|
| 518 | 518 | |
| 519 | - protected function parseQueryParams() { |
|
| 519 | + protected function parseQueryParams() { |
|
| 520 | 520 | $args = func_get_args(); |
| 521 | 521 | $chunkyQuery = call_user_func_array(array($this, 'preparseQueryParams'), $args); |
| 522 | 522 | |
@@ -524,57 +524,57 @@ discard block |
||
| 524 | 524 | $array_types = array('ls', 'li', 'ld', 'lb', 'll', 'lt'); |
| 525 | 525 | |
| 526 | 526 | foreach ($chunkyQuery as $chunk) { |
| 527 | - if (is_string($chunk)) { |
|
| 527 | + if (is_string($chunk)) { |
|
| 528 | 528 | $query .= $chunk; |
| 529 | 529 | continue; |
| 530 | - } |
|
| 530 | + } |
|
| 531 | 531 | |
| 532 | - $type = $chunk['type']; |
|
| 533 | - $arg = $chunk['value']; |
|
| 534 | - $result = ''; |
|
| 532 | + $type = $chunk['type']; |
|
| 533 | + $arg = $chunk['value']; |
|
| 534 | + $result = ''; |
|
| 535 | 535 | |
| 536 | - if ($type != '?') { |
|
| 536 | + if ($type != '?') { |
|
| 537 | 537 | $is_array_type = in_array($type, $array_types, true); |
| 538 | 538 | if ($is_array_type && !is_array($arg)) $this->nonSQLError("Badly formatted SQL query: Expected array, got scalar instead!"); |
| 539 | 539 | else if (!$is_array_type && is_array($arg)) $this->nonSQLError("Badly formatted SQL query: Expected scalar, got array instead!"); |
| 540 | - } |
|
| 540 | + } |
|
| 541 | 541 | |
| 542 | - if ($type == 's') $result = $this->escape($arg); |
|
| 543 | - else if ($type == 'i') $result = $this->intval($arg); |
|
| 544 | - else if ($type == 'd') $result = doubleval($arg); |
|
| 545 | - else if ($type == 'b') $result = $this->formatTableName($arg); |
|
| 546 | - else if ($type == 'l') $result = $arg; |
|
| 547 | - else if ($type == 'ss') $result = $this->escape("%" . str_replace(array('%', '_'), array('\%', '\_'), $arg) . "%"); |
|
| 548 | - else if ($type == 't') $result = $this->escape($this->parseTS($arg)); |
|
| 542 | + if ($type == 's') $result = $this->escape($arg); |
|
| 543 | + else if ($type == 'i') $result = $this->intval($arg); |
|
| 544 | + else if ($type == 'd') $result = doubleval($arg); |
|
| 545 | + else if ($type == 'b') $result = $this->formatTableName($arg); |
|
| 546 | + else if ($type == 'l') $result = $arg; |
|
| 547 | + else if ($type == 'ss') $result = $this->escape("%" . str_replace(array('%', '_'), array('\%', '\_'), $arg) . "%"); |
|
| 548 | + else if ($type == 't') $result = $this->escape($this->parseTS($arg)); |
|
| 549 | 549 | |
| 550 | - else if ($type == 'ls') $result = array_map(array($this, 'escape'), $arg); |
|
| 551 | - else if ($type == 'li') $result = array_map(array($this, 'intval'), $arg); |
|
| 552 | - else if ($type == 'ld') $result = array_map('doubleval', $arg); |
|
| 553 | - else if ($type == 'lb') $result = array_map(array($this, 'formatTableName'), $arg); |
|
| 554 | - else if ($type == 'll') $result = $arg; |
|
| 555 | - else if ($type == 'lt') $result = array_map(array($this, 'escape'), array_map(array($this, 'parseTS'), $arg)); |
|
| 550 | + else if ($type == 'ls') $result = array_map(array($this, 'escape'), $arg); |
|
| 551 | + else if ($type == 'li') $result = array_map(array($this, 'intval'), $arg); |
|
| 552 | + else if ($type == 'ld') $result = array_map('doubleval', $arg); |
|
| 553 | + else if ($type == 'lb') $result = array_map(array($this, 'formatTableName'), $arg); |
|
| 554 | + else if ($type == 'll') $result = $arg; |
|
| 555 | + else if ($type == 'lt') $result = array_map(array($this, 'escape'), array_map(array($this, 'parseTS'), $arg)); |
|
| 556 | 556 | |
| 557 | - else if ($type == '?') $result = $this->sanitize($arg); |
|
| 557 | + else if ($type == '?') $result = $this->sanitize($arg); |
|
| 558 | 558 | |
| 559 | - else $this->nonSQLError("Badly formatted SQL query: Invalid MeekroDB param $type"); |
|
| 559 | + else $this->nonSQLError("Badly formatted SQL query: Invalid MeekroDB param $type"); |
|
| 560 | 560 | |
| 561 | - if (is_array($result)) $result = '(' . implode(',', $result) . ')'; |
|
| 561 | + if (is_array($result)) $result = '(' . implode(',', $result) . ')'; |
|
| 562 | 562 | |
| 563 | - $query .= $result; |
|
| 563 | + $query .= $result; |
|
| 564 | 564 | } |
| 565 | 565 | |
| 566 | 566 | return $query; |
| 567 | - } |
|
| 567 | + } |
|
| 568 | 568 | |
| 569 | - protected function prependCall($function, $args, $prepend) { array_unshift($args, $prepend); return call_user_func_array($function, $args); } |
|
| 570 | - public function query() { $args = func_get_args(); return $this->prependCall(array($this, 'queryHelper'), $args, 'assoc'); } |
|
| 571 | - public function queryAllLists() { $args = func_get_args(); return $this->prependCall(array($this, 'queryHelper'), $args, 'list'); } |
|
| 572 | - public function queryFullColumns() { $args = func_get_args(); return $this->prependCall(array($this, 'queryHelper'), $args, 'full'); } |
|
| 569 | + protected function prependCall($function, $args, $prepend) { array_unshift($args, $prepend); return call_user_func_array($function, $args); } |
|
| 570 | + public function query() { $args = func_get_args(); return $this->prependCall(array($this, 'queryHelper'), $args, 'assoc'); } |
|
| 571 | + public function queryAllLists() { $args = func_get_args(); return $this->prependCall(array($this, 'queryHelper'), $args, 'list'); } |
|
| 572 | + public function queryFullColumns() { $args = func_get_args(); return $this->prependCall(array($this, 'queryHelper'), $args, 'full'); } |
|
| 573 | 573 | |
| 574 | - public function queryRaw() { $args = func_get_args(); return $this->prependCall(array($this, 'queryHelper'), $args, 'raw_buf'); } |
|
| 575 | - public function queryRawUnbuf() { $args = func_get_args(); return $this->prependCall(array($this, 'queryHelper'), $args, 'raw_unbuf'); } |
|
| 574 | + public function queryRaw() { $args = func_get_args(); return $this->prependCall(array($this, 'queryHelper'), $args, 'raw_buf'); } |
|
| 575 | + public function queryRawUnbuf() { $args = func_get_args(); return $this->prependCall(array($this, 'queryHelper'), $args, 'raw_unbuf'); } |
|
| 576 | 576 | |
| 577 | - protected function queryHelper() { |
|
| 577 | + protected function queryHelper() { |
|
| 578 | 578 | $args = func_get_args(); |
| 579 | 579 | $type = array_shift($args); |
| 580 | 580 | $db = $this->get(); |
@@ -584,23 +584,23 @@ discard block |
||
| 584 | 584 | $full_names = false; |
| 585 | 585 | |
| 586 | 586 | switch ($type) { |
| 587 | - case 'assoc': |
|
| 587 | + case 'assoc': |
|
| 588 | 588 | break; |
| 589 | - case 'list': |
|
| 589 | + case 'list': |
|
| 590 | 590 | $row_type = 'list'; |
| 591 | 591 | break; |
| 592 | - case 'full': |
|
| 592 | + case 'full': |
|
| 593 | 593 | $row_type = 'list'; |
| 594 | 594 | $full_names = true; |
| 595 | 595 | break; |
| 596 | - case 'raw_buf': |
|
| 596 | + case 'raw_buf': |
|
| 597 | 597 | $row_type = 'raw'; |
| 598 | 598 | break; |
| 599 | - case 'raw_unbuf': |
|
| 599 | + case 'raw_unbuf': |
|
| 600 | 600 | $is_buffered = false; |
| 601 | 601 | $row_type = 'raw'; |
| 602 | 602 | break; |
| 603 | - default: |
|
| 603 | + default: |
|
| 604 | 604 | $this->nonSQLError('Error -- invalid argument to queryHelper!'); |
| 605 | 605 | } |
| 606 | 606 | |
@@ -613,41 +613,41 @@ discard block |
||
| 613 | 613 | |
| 614 | 614 | // ----- BEGIN ERROR HANDLING |
| 615 | 615 | if (!$sql || $db->error) { |
| 616 | - if ($this->error_handler) { |
|
| 616 | + if ($this->error_handler) { |
|
| 617 | 617 | $db_error = $db->error; |
| 618 | 618 | $db_errno = $db->errno; |
| 619 | 619 | $db->query( |
| 620 | - "INSERT INTO ".$GLOBALS['pre']."log_system SET |
|
| 620 | + "INSERT INTO ".$GLOBALS['pre']."log_system SET |
|
| 621 | 621 | date=".time().", |
| 622 | 622 | qui=".$_SESSION['user_id'].", |
| 623 | 623 | label='Query: ".addslashes($sql)."<br />Error: ".addslashes($db_error)."<br />@ ".mysqli_real_escape_string($link, filter_var($_SERVER['REQUEST_URI'], FILTER_SANITIZE_STRING))."', |
| 624 | 624 | type='error'", |
| 625 | - MYSQLI_USE_RESULT |
|
| 625 | + MYSQLI_USE_RESULT |
|
| 626 | 626 | ); |
| 627 | 627 | |
| 628 | 628 | $error_handler = is_callable($this->error_handler) ? $this->error_handler : 'meekrodb_error_handler'; |
| 629 | 629 | |
| 630 | 630 | call_user_func($error_handler, array( |
| 631 | - 'type' => 'sql', |
|
| 632 | - 'query' => $sql, |
|
| 633 | - 'error' => $db_error, |
|
| 634 | - 'code' => $db_errno |
|
| 631 | + 'type' => 'sql', |
|
| 632 | + 'query' => $sql, |
|
| 633 | + 'error' => $db_error, |
|
| 634 | + 'code' => $db_errno |
|
| 635 | 635 | )); |
| 636 | - } |
|
| 636 | + } |
|
| 637 | 637 | |
| 638 | - if ($this->throw_exception_on_error) { |
|
| 638 | + if ($this->throw_exception_on_error) { |
|
| 639 | 639 | $e = new MeekroDBException($db_error, $sql, $db_errno); |
| 640 | 640 | throw $e; |
| 641 | - } |
|
| 641 | + } |
|
| 642 | 642 | } else if ($this->success_handler) { |
| 643 | - $runtime = sprintf('%f', $runtime * 1000); |
|
| 644 | - $success_handler = is_callable($this->success_handler) ? $this->success_handler : 'meekrodb_debugmode_handler'; |
|
| 643 | + $runtime = sprintf('%f', $runtime * 1000); |
|
| 644 | + $success_handler = is_callable($this->success_handler) ? $this->success_handler : 'meekrodb_debugmode_handler'; |
|
| 645 | 645 | |
| 646 | - call_user_func($success_handler, array( |
|
| 646 | + call_user_func($success_handler, array( |
|
| 647 | 647 | 'query' => $sql, |
| 648 | 648 | 'runtime' => $runtime, |
| 649 | 649 | 'affected' => $db->affected_rows |
| 650 | - )); |
|
| 650 | + )); |
|
| 651 | 651 | } |
| 652 | 652 | |
| 653 | 653 | // ----- END ERROR HANDLING |
@@ -664,45 +664,45 @@ discard block |
||
| 664 | 664 | $return = array(); |
| 665 | 665 | |
| 666 | 666 | if ($full_names) { |
| 667 | - $infos = array(); |
|
| 668 | - foreach ($result->fetch_fields() as $info) { |
|
| 667 | + $infos = array(); |
|
| 668 | + foreach ($result->fetch_fields() as $info) { |
|
| 669 | 669 | if (strlen($info->table)) $infos[] = $info->table . '.' . $info->name; |
| 670 | 670 | else $infos[] = $info->name; |
| 671 | - } |
|
| 671 | + } |
|
| 672 | 672 | } |
| 673 | 673 | |
| 674 | 674 | while ($row = ($row_type == 'assoc' ? $result->fetch_assoc() : $result->fetch_row())) { |
| 675 | - if ($full_names) $row = array_combine($infos, $row); |
|
| 676 | - $return[] = $row; |
|
| 675 | + if ($full_names) $row = array_combine($infos, $row); |
|
| 676 | + $return[] = $row; |
|
| 677 | 677 | } |
| 678 | 678 | |
| 679 | 679 | // free results |
| 680 | 680 | $result->free(); |
| 681 | 681 | while ($db->more_results()) { |
| 682 | - $db->next_result(); |
|
| 683 | - if ($result = $db->use_result()) $result->free(); |
|
| 682 | + $db->next_result(); |
|
| 683 | + if ($result = $db->use_result()) $result->free(); |
|
| 684 | 684 | } |
| 685 | 685 | |
| 686 | 686 | return $return; |
| 687 | - } |
|
| 687 | + } |
|
| 688 | 688 | |
| 689 | - public function queryOneRow() { $args = func_get_args(); return call_user_func_array(array($this, 'queryFirstRow'), $args); } |
|
| 690 | - public function queryFirstRow() { |
|
| 689 | + public function queryOneRow() { $args = func_get_args(); return call_user_func_array(array($this, 'queryFirstRow'), $args); } |
|
| 690 | + public function queryFirstRow() { |
|
| 691 | 691 | $args = func_get_args(); |
| 692 | 692 | $result = call_user_func_array(array($this, 'query'), $args); |
| 693 | 693 | if (! $result) return null; |
| 694 | 694 | return reset($result); |
| 695 | - } |
|
| 695 | + } |
|
| 696 | 696 | |
| 697 | - public function queryOneList() { $args = func_get_args(); return call_user_func_array(array($this, 'queryFirstList'), $args); } |
|
| 698 | - public function queryFirstList() { |
|
| 697 | + public function queryOneList() { $args = func_get_args(); return call_user_func_array(array($this, 'queryFirstList'), $args); } |
|
| 698 | + public function queryFirstList() { |
|
| 699 | 699 | $args = func_get_args(); |
| 700 | 700 | $result = call_user_func_array(array($this, 'queryAllLists'), $args); |
| 701 | 701 | if (! $result) return null; |
| 702 | 702 | return reset($result); |
| 703 | - } |
|
| 703 | + } |
|
| 704 | 704 | |
| 705 | - public function queryFirstColumn() { |
|
| 705 | + public function queryFirstColumn() { |
|
| 706 | 706 | $args = func_get_args(); |
| 707 | 707 | $results = call_user_func_array(array($this, 'queryAllLists'), $args); |
| 708 | 708 | $ret = array(); |
@@ -710,13 +710,13 @@ discard block |
||
| 710 | 710 | if (!count($results) || !count($results[0])) return $ret; |
| 711 | 711 | |
| 712 | 712 | foreach ($results as $row) { |
| 713 | - $ret[] = $row[0]; |
|
| 713 | + $ret[] = $row[0]; |
|
| 714 | 714 | } |
| 715 | 715 | |
| 716 | 716 | return $ret; |
| 717 | - } |
|
| 717 | + } |
|
| 718 | 718 | |
| 719 | - public function queryOneColumn() { |
|
| 719 | + public function queryOneColumn() { |
|
| 720 | 720 | $args = func_get_args(); |
| 721 | 721 | $column = array_shift($args); |
| 722 | 722 | $results = call_user_func_array(array($this, 'query'), $args); |
@@ -724,103 +724,103 @@ discard block |
||
| 724 | 724 | |
| 725 | 725 | if (!count($results) || !count($results[0])) return $ret; |
| 726 | 726 | if ($column === null) { |
| 727 | - $keys = array_keys($results[0]); |
|
| 728 | - $column = $keys[0]; |
|
| 727 | + $keys = array_keys($results[0]); |
|
| 728 | + $column = $keys[0]; |
|
| 729 | 729 | } |
| 730 | 730 | |
| 731 | 731 | foreach ($results as $row) { |
| 732 | - $ret[] = $row[$column]; |
|
| 732 | + $ret[] = $row[$column]; |
|
| 733 | 733 | } |
| 734 | 734 | |
| 735 | 735 | return $ret; |
| 736 | - } |
|
| 736 | + } |
|
| 737 | 737 | |
| 738 | - public function queryFirstField() { |
|
| 738 | + public function queryFirstField() { |
|
| 739 | 739 | $args = func_get_args(); |
| 740 | 740 | $row = call_user_func_array(array($this, 'queryFirstList'), $args); |
| 741 | 741 | if ($row == null) return null; |
| 742 | 742 | return $row[0]; |
| 743 | - } |
|
| 743 | + } |
|
| 744 | 744 | |
| 745 | - public function queryOneField() { |
|
| 745 | + public function queryOneField() { |
|
| 746 | 746 | $args = func_get_args(); |
| 747 | 747 | $column = array_shift($args); |
| 748 | 748 | |
| 749 | 749 | $row = call_user_func_array(array($this, 'queryOneRow'), $args); |
| 750 | 750 | if ($row == null) { |
| 751 | - return null; |
|
| 751 | + return null; |
|
| 752 | 752 | } else if ($column === null) { |
| 753 | - $keys = array_keys($row); |
|
| 754 | - $column = $keys[0]; |
|
| 753 | + $keys = array_keys($row); |
|
| 754 | + $column = $keys[0]; |
|
| 755 | 755 | } |
| 756 | 756 | |
| 757 | 757 | return $row[$column]; |
| 758 | - } |
|
| 758 | + } |
|
| 759 | 759 | } |
| 760 | 760 | |
| 761 | 761 | class WhereClause { |
| 762 | - public $type = 'and'; //AND or OR |
|
| 763 | - public $negate = false; |
|
| 764 | - public $clauses = array(); |
|
| 762 | + public $type = 'and'; //AND or OR |
|
| 763 | + public $negate = false; |
|
| 764 | + public $clauses = array(); |
|
| 765 | 765 | |
| 766 | - function __construct($type) { |
|
| 766 | + function __construct($type) { |
|
| 767 | 767 | $type = strtolower($type); |
| 768 | 768 | if ($type !== 'or' && $type !== 'and') DB::nonSQLError('you must use either WhereClause(and) or WhereClause(or)'); |
| 769 | 769 | $this->type = $type; |
| 770 | - } |
|
| 770 | + } |
|
| 771 | 771 | |
| 772 | - function add() { |
|
| 772 | + function add() { |
|
| 773 | 773 | $args = func_get_args(); |
| 774 | 774 | $sql = array_shift($args); |
| 775 | 775 | |
| 776 | 776 | if ($sql instanceof WhereClause) { |
| 777 | - $this->clauses[] = $sql; |
|
| 777 | + $this->clauses[] = $sql; |
|
| 778 | 778 | } else { |
| 779 | - $this->clauses[] = array('sql' => $sql, 'args' => $args); |
|
| 779 | + $this->clauses[] = array('sql' => $sql, 'args' => $args); |
|
| 780 | + } |
|
| 780 | 781 | } |
| 781 | - } |
|
| 782 | 782 | |
| 783 | - function negateLast() { |
|
| 783 | + function negateLast() { |
|
| 784 | 784 | $i = count($this->clauses) - 1; |
| 785 | 785 | if (!isset($this->clauses[$i])) return; |
| 786 | 786 | |
| 787 | 787 | if ($this->clauses[$i] instanceof WhereClause) { |
| 788 | - $this->clauses[$i]->negate(); |
|
| 788 | + $this->clauses[$i]->negate(); |
|
| 789 | 789 | } else { |
| 790 | - $this->clauses[$i]['sql'] = 'NOT (' . $this->clauses[$i]['sql'] . ')'; |
|
| 790 | + $this->clauses[$i]['sql'] = 'NOT (' . $this->clauses[$i]['sql'] . ')'; |
|
| 791 | + } |
|
| 791 | 792 | } |
| 792 | - } |
|
| 793 | 793 | |
| 794 | - function negate() { |
|
| 794 | + function negate() { |
|
| 795 | 795 | $this->negate = ! $this->negate; |
| 796 | - } |
|
| 796 | + } |
|
| 797 | 797 | |
| 798 | - function addClause($type) { |
|
| 798 | + function addClause($type) { |
|
| 799 | 799 | $r = new WhereClause($type); |
| 800 | 800 | $this->add($r); |
| 801 | 801 | return $r; |
| 802 | - } |
|
| 802 | + } |
|
| 803 | 803 | |
| 804 | - function count() { |
|
| 804 | + function count() { |
|
| 805 | 805 | return count($this->clauses); |
| 806 | - } |
|
| 806 | + } |
|
| 807 | 807 | |
| 808 | - function textAndArgs() { |
|
| 808 | + function textAndArgs() { |
|
| 809 | 809 | $sql = array(); |
| 810 | 810 | $args = array(); |
| 811 | 811 | |
| 812 | 812 | if (count($this->clauses) == 0) return array('(1)', $args); |
| 813 | 813 | |
| 814 | 814 | foreach ($this->clauses as $clause) { |
| 815 | - if ($clause instanceof WhereClause) { |
|
| 815 | + if ($clause instanceof WhereClause) { |
|
| 816 | 816 | list($clause_sql, $clause_args) = $clause->textAndArgs(); |
| 817 | - } else { |
|
| 817 | + } else { |
|
| 818 | 818 | $clause_sql = $clause['sql']; |
| 819 | 819 | $clause_args = $clause['args']; |
| 820 | - } |
|
| 820 | + } |
|
| 821 | 821 | |
| 822 | - $sql[] = "($clause_sql)"; |
|
| 823 | - $args = array_merge($args, $clause_args); |
|
| 822 | + $sql[] = "($clause_sql)"; |
|
| 823 | + $args = array_merge($args, $clause_args); |
|
| 824 | 824 | } |
| 825 | 825 | |
| 826 | 826 | if ($this->type == 'and') $sql = implode(' AND ', $sql); |
@@ -828,111 +828,111 @@ discard block |
||
| 828 | 828 | |
| 829 | 829 | if ($this->negate) $sql = '(NOT ' . $sql . ')'; |
| 830 | 830 | return array($sql, $args); |
| 831 | - } |
|
| 831 | + } |
|
| 832 | 832 | |
| 833 | - // backwards compatability |
|
| 834 | - // we now return full WhereClause object here and evaluate it in preparseQueryParams |
|
| 835 | - function text() { return $this; } |
|
| 833 | + // backwards compatability |
|
| 834 | + // we now return full WhereClause object here and evaluate it in preparseQueryParams |
|
| 835 | + function text() { return $this; } |
|
| 836 | 836 | } |
| 837 | 837 | |
| 838 | 838 | class DBTransaction { |
| 839 | - private $committed = false; |
|
| 839 | + private $committed = false; |
|
| 840 | 840 | |
| 841 | - function __construct() { |
|
| 841 | + function __construct() { |
|
| 842 | 842 | DB::startTransaction(); |
| 843 | - } |
|
| 844 | - function __destruct() { |
|
| 843 | + } |
|
| 844 | + function __destruct() { |
|
| 845 | 845 | if (! $this->committed) DB::rollback(); |
| 846 | - } |
|
| 847 | - function commit() { |
|
| 846 | + } |
|
| 847 | + function commit() { |
|
| 848 | 848 | DB::commit(); |
| 849 | 849 | $this->committed = true; |
| 850 | - } |
|
| 850 | + } |
|
| 851 | 851 | |
| 852 | 852 | |
| 853 | 853 | } |
| 854 | 854 | |
| 855 | 855 | class MeekroDBException extends Exception { |
| 856 | - protected $query = ''; |
|
| 856 | + protected $query = ''; |
|
| 857 | 857 | |
| 858 | - function __construct($message='', $query='', $code = 0) { |
|
| 858 | + function __construct($message='', $query='', $code = 0) { |
|
| 859 | 859 | parent::__construct($message); |
| 860 | 860 | $this->query = $query; |
| 861 | - $this->code = $code; |
|
| 862 | - } |
|
| 861 | + $this->code = $code; |
|
| 862 | + } |
|
| 863 | 863 | |
| 864 | - public function getQuery() { return $this->query; } |
|
| 864 | + public function getQuery() { return $this->query; } |
|
| 865 | 865 | } |
| 866 | 866 | |
| 867 | 867 | class DBHelper { |
| 868 | - /* |
|
| 868 | + /* |
|
| 869 | 869 | verticalSlice |
| 870 | 870 | 1. For an array of assoc rays, return an array of values for a particular key |
| 871 | 871 | 2. if $keyfield is given, same as above but use that hash key as the key in new array |
| 872 | 872 | */ |
| 873 | 873 | |
| 874 | - public static function verticalSlice($array, $field, $keyfield = null) { |
|
| 874 | + public static function verticalSlice($array, $field, $keyfield = null) { |
|
| 875 | 875 | $array = (array) $array; |
| 876 | 876 | |
| 877 | 877 | $R = array(); |
| 878 | 878 | foreach ($array as $obj) { |
| 879 | - if (! array_key_exists($field, $obj)) die("verticalSlice: array doesn't have requested field\n"); |
|
| 879 | + if (! array_key_exists($field, $obj)) die("verticalSlice: array doesn't have requested field\n"); |
|
| 880 | 880 | |
| 881 | - if ($keyfield) { |
|
| 881 | + if ($keyfield) { |
|
| 882 | 882 | if (! array_key_exists($keyfield, $obj)) die("verticalSlice: array doesn't have requested field\n"); |
| 883 | 883 | $R[$obj[$keyfield]] = $obj[$field]; |
| 884 | - } else { |
|
| 884 | + } else { |
|
| 885 | 885 | $R[] = $obj[$field]; |
| 886 | - } |
|
| 886 | + } |
|
| 887 | 887 | } |
| 888 | 888 | return $R; |
| 889 | - } |
|
| 889 | + } |
|
| 890 | 890 | |
| 891 | - /* |
|
| 891 | + /* |
|
| 892 | 892 | reIndex |
| 893 | 893 | For an array of assoc rays, return a new array of assoc rays using a certain field for keys |
| 894 | 894 | */ |
| 895 | 895 | |
| 896 | - public static function reIndex() { |
|
| 896 | + public static function reIndex() { |
|
| 897 | 897 | $fields = func_get_args(); |
| 898 | 898 | $array = array_shift($fields); |
| 899 | 899 | $array = (array) $array; |
| 900 | 900 | |
| 901 | 901 | $R = array(); |
| 902 | 902 | foreach ($array as $obj) { |
| 903 | - $target =& $R; |
|
| 903 | + $target =& $R; |
|
| 904 | 904 | |
| 905 | - foreach ($fields as $field) { |
|
| 905 | + foreach ($fields as $field) { |
|
| 906 | 906 | if (! array_key_exists($field, $obj)) die("reIndex: array doesn't have requested field\n"); |
| 907 | 907 | |
| 908 | 908 | $nextkey = $obj[$field]; |
| 909 | 909 | $target =& $target[$nextkey]; |
| 910 | - } |
|
| 911 | - $target = $obj; |
|
| 910 | + } |
|
| 911 | + $target = $obj; |
|
| 912 | 912 | } |
| 913 | 913 | return $R; |
| 914 | - } |
|
| 914 | + } |
|
| 915 | 915 | } |
| 916 | 916 | |
| 917 | 917 | function meekrodb_error_handler($params) { |
| 918 | - echo prepareExchangedData('[{"error" : "'.$params['error'].'"}]', "encode"); |
|
| 918 | + echo prepareExchangedData('[{"error" : "'.$params['error'].'"}]', "encode"); |
|
| 919 | 919 | |
| 920 | - die; |
|
| 920 | + die; |
|
| 921 | 921 | } |
| 922 | 922 | |
| 923 | 923 | function meekrodb_debugmode_handler($params) { |
| 924 | - echo "QUERY: " . $params['query'] . " [" . $params['runtime'] . " ms]"; |
|
| 925 | - if (php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) { |
|
| 924 | + echo "QUERY: " . $params['query'] . " [" . $params['runtime'] . " ms]"; |
|
| 925 | + if (php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) { |
|
| 926 | 926 | echo "\n"; |
| 927 | - } else { |
|
| 927 | + } else { |
|
| 928 | 928 | echo "<br>\n"; |
| 929 | - } |
|
| 929 | + } |
|
| 930 | 930 | } |
| 931 | 931 | |
| 932 | 932 | class MeekroDBEval { |
| 933 | - public $text = ''; |
|
| 933 | + public $text = ''; |
|
| 934 | 934 | |
| 935 | - function __construct($text) { |
|
| 935 | + function __construct($text) { |
|
| 936 | 936 | $this->text = $text; |
| 937 | - } |
|
| 937 | + } |
|
| 938 | 938 | } |
| 939 | 939 | \ No newline at end of file |
@@ -33,590 +33,590 @@ |
||
| 33 | 33 | */ |
| 34 | 34 | class Core |
| 35 | 35 | { |
| 36 | - /** @type integer HASH_LEN The length of md5() hash string */ |
|
| 37 | - const HASH_LEN = 16; |
|
| 38 | - |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Constructor |
|
| 42 | - * |
|
| 43 | - */ |
|
| 44 | - protected function __construct() |
|
| 45 | - { |
|
| 46 | - |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Destructor |
|
| 52 | - * |
|
| 53 | - */ |
|
| 54 | - protected function __destruct() |
|
| 55 | - { |
|
| 56 | - |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * Convert hexidecimal to a binary string (ex: "00110110") |
|
| 62 | - * |
|
| 63 | - * @param string $hex A string containing a hexidecimal number |
|
| 64 | - * @return string A string representation of a binary |
|
| 65 | - */ |
|
| 66 | - public static function hex2Bin($hex) |
|
| 67 | - { |
|
| 68 | - // if we do not have an even number of hex characters |
|
| 69 | - // append a 0 to the beginning to make it even |
|
| 70 | - if(strlen($hex) % 2) |
|
| 71 | - $hex = "0$hex"; |
|
| 72 | - |
|
| 73 | - $parts = str_split($hex, 2); |
|
| 74 | - $parts = array_map(function($v) { |
|
| 75 | - $v = base_convert($v, 16, 2); |
|
| 76 | - return str_pad($v, 8, "0", STR_PAD_LEFT); |
|
| 77 | - }, $parts); |
|
| 78 | - |
|
| 79 | - return implode("", $parts); |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * Convert hex to a string |
|
| 85 | - * |
|
| 86 | - * @param string $hex A string representation of Hex (IE: "1a2b3c" not 0x1a2b3c) |
|
| 87 | - * @return string a string |
|
| 88 | - */ |
|
| 89 | - public static function hex2Str($hex) |
|
| 90 | - { |
|
| 91 | - // php version >= 5.4 have a hex2bin function, use it |
|
| 92 | - // if it exists |
|
| 93 | - if(function_exists("hex2bin")) |
|
| 94 | - return hex2bin($hex); |
|
| 95 | - |
|
| 96 | - $parts = str_split($hex, 2); |
|
| 97 | - $parts = array_map(function($v) { |
|
| 98 | - return chr(Core::hex2Dec($v)); |
|
| 99 | - }, $parts); |
|
| 100 | - |
|
| 101 | - return implode("", $parts); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * Converts Hex to Decimal |
|
| 107 | - * This function just calls php's hexdec() function, but I |
|
| 108 | - * encapsulated it in this function to keep things uniform |
|
| 109 | - * and have all possible conversion function available in |
|
| 110 | - * the Cipher class |
|
| 111 | - * |
|
| 112 | - * @param string $hex A hex number to convert to decimal |
|
| 113 | - * @return integer A decimal number |
|
| 114 | - */ |
|
| 115 | - public static function hex2Dec($hex) |
|
| 116 | - { |
|
| 117 | - return hexdec($hex); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Convert binary string (ie 00110110) to hex |
|
| 123 | - * |
|
| 124 | - * @param string $bin A binary string |
|
| 125 | - * @return string A string representation of hexidecimal number |
|
| 126 | - */ |
|
| 127 | - public static function bin2Hex($bin) |
|
| 128 | - { |
|
| 129 | - $parts = str_split($bin, 8); |
|
| 130 | - |
|
| 131 | - $parts = array_map(function($v) { |
|
| 132 | - $v = str_pad($v, 8, "0", STR_PAD_LEFT); |
|
| 133 | - $v = dechex(bindec($v)); |
|
| 134 | - return str_pad($v, 2, "0", STR_PAD_LEFT); |
|
| 135 | - }, $parts); |
|
| 136 | - |
|
| 137 | - return implode("", $parts); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * Converts a binary representation (ie 01101011) back to a string |
|
| 143 | - * |
|
| 144 | - * @param string $bin a binary representation string |
|
| 145 | - * @return string A string of characters representing the binary |
|
| 146 | - */ |
|
| 147 | - public static function bin2Str($bin) |
|
| 148 | - { |
|
| 149 | - $hex = self::bin2Hex($bin); |
|
| 150 | - return self::hex2Str($hex); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * Convert a binary string (ie: 01101011) to a decimal number |
|
| 156 | - * |
|
| 157 | - * @param string A string representation of a binary number |
|
| 158 | - * @return integer The number converted from the binary string |
|
| 159 | - */ |
|
| 160 | - public static function bin2Dec($bin) |
|
| 161 | - { |
|
| 162 | - return bindec($bin); |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * Convert a string to hex |
|
| 168 | - * This function calls the PHP bin2hex(), and is here |
|
| 169 | - * for consistency with the other string functions |
|
| 170 | - * |
|
| 171 | - * @param string $str A string |
|
| 172 | - * @return string A string representation of hexidecimal number |
|
| 173 | - */ |
|
| 174 | - public static function str2Hex($str) |
|
| 175 | - { |
|
| 176 | - return bin2hex($str); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * Convert a string of characters to a decimal number |
|
| 182 | - * |
|
| 183 | - * @param string $str The string to convert to decimal |
|
| 184 | - * @return integer The integer converted from the string |
|
| 185 | - */ |
|
| 186 | - public static function str2Dec($str) |
|
| 187 | - { |
|
| 188 | - $hex = self::str2Hex($str); |
|
| 189 | - return self::hex2Dec($hex); |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * Converts a string to binary representation (ie 01101011) |
|
| 195 | - * |
|
| 196 | - * @param string $str A string |
|
| 197 | - * @return string A binary representation of the the string |
|
| 198 | - */ |
|
| 199 | - public static function str2Bin($str) |
|
| 200 | - { |
|
| 201 | - $hex = self::str2Hex($str); |
|
| 202 | - $parts = str_split($hex, 2); |
|
| 203 | - |
|
| 204 | - $parts = array_map(function($v) { |
|
| 205 | - return Core::hex2Bin($v); |
|
| 206 | - }, $parts); |
|
| 207 | - |
|
| 208 | - return implode("", $parts); |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * Converts Decimal to Hex |
|
| 214 | - * This function just calls php's dechex() function, but I |
|
| 215 | - * encapsulated it in this function to keep things uniform |
|
| 216 | - * and have all possible conversion function available in |
|
| 217 | - * the Cipher class |
|
| 218 | - * |
|
| 219 | - * The parameter $req_bytes will pad the return hex with NULL (00) |
|
| 220 | - * until the hex represents the number of bytes given to $req_bytes |
|
| 221 | - * This is because dechex() drops null bytes from the Hex, which may |
|
| 222 | - * be needed in some cases |
|
| 223 | - * |
|
| 224 | - * @param integer $dec A decimal number to convert |
|
| 225 | - * @param integer $req_bytes Optional, forces the string to be at least |
|
| 226 | - * $req_bytes in size, this is needed because on occasion left most null bytes |
|
| 227 | - * are dropped in dechex(), causing the string to have a shorter byte |
|
| 228 | - * size than the initial integer. |
|
| 229 | - * @return string A hexidecimal representation of the decimal number |
|
| 230 | - */ |
|
| 231 | - public static function dec2Hex($dec, $req_bytes = 0) |
|
| 232 | - { |
|
| 233 | - $hex = dechex($dec); |
|
| 234 | - |
|
| 235 | - // if we do not have an even number of hex characters |
|
| 236 | - // append a 0 to the beginning. dechex() drops leading 0's |
|
| 237 | - if(strlen($hex) % 2) |
|
| 238 | - $hex = "0$hex"; |
|
| 239 | - |
|
| 240 | - // if the number of bytes in the hex is less than |
|
| 241 | - // what we need it to be, add null bytes to the |
|
| 242 | - // front of the hex to padd it to the required size |
|
| 243 | - if(($req_bytes * 2) > strlen($hex)) |
|
| 244 | - $hex = str_pad($hex, ($req_bytes * 2), "0", STR_PAD_LEFT); |
|
| 245 | - |
|
| 246 | - return $hex; |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * Converts Decimal to Binary |
|
| 252 | - * This function just calls php's decbin() function, but I |
|
| 253 | - * encapsulated it in this function to keep things uniform |
|
| 254 | - * and have all possible conversion function available in |
|
| 255 | - * the Cipher class |
|
| 256 | - * |
|
| 257 | - * @param integer $dec A decimal number to convert |
|
| 258 | - * @param integer $req_bytes Optional, forces the string to be at least |
|
| 259 | - * $req_bytes in size, this is needed because on occasion left most null bytes |
|
| 260 | - * are dropped in dechex(), causing the string to have a shorter byte |
|
| 261 | - * size than the initial integer. |
|
| 262 | - * @return string A binary representation of the decimal number |
|
| 263 | - */ |
|
| 264 | - public static function dec2Bin($dec, $req_bytes = 0) |
|
| 265 | - { |
|
| 266 | - $hex = self::dec2Hex($dec, $req_bytes); |
|
| 267 | - return self::hex2Bin($hex); |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - |
|
| 271 | - /** |
|
| 272 | - * Convert a decimal to a string of bytes |
|
| 273 | - * |
|
| 274 | - * @param integer $dec A decimal number |
|
| 275 | - * @param integer $req_bytes Optional, forces the string to be at least |
|
| 276 | - * $req_bytes in size, this is needed because on occasion left most null bytes |
|
| 277 | - * are dropped in dechex(), causing the string to have a shorter byte |
|
| 278 | - * size than the initial integer. |
|
| 279 | - * @return string A string with the number of bytes equal to $dec |
|
| 280 | - */ |
|
| 281 | - public static function dec2Str($dec, $req_bytes = 0) |
|
| 282 | - { |
|
| 283 | - $hex = self::dec2Hex($dec, $req_bytes); |
|
| 284 | - return self::hex2Str($hex); |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - |
|
| 288 | - /** |
|
| 289 | - * XORs two binary strings (representation of binary, ie 01101011), |
|
| 290 | - * assumed to be equal length |
|
| 291 | - * |
|
| 292 | - * @param string $a A string that represents binary |
|
| 293 | - * @param string $b A string that represents binary |
|
| 294 | - * @return string A representation of binary |
|
| 295 | - */ |
|
| 296 | - public static function xorBin($a, $b) |
|
| 297 | - { |
|
| 298 | - $len_a = strlen($a); |
|
| 299 | - $len_b = strlen($b); |
|
| 300 | - $width = $len_a; |
|
| 301 | - |
|
| 302 | - // first determine if the two binary strings are the same length, |
|
| 303 | - // and if not get them to the same length |
|
| 304 | - if($len_a > $len_b) |
|
| 305 | - { |
|
| 306 | - $width = $len_a; |
|
| 307 | - $b = str_pad($b, $width, "0", STR_PAD_LEFT); |
|
| 308 | - } |
|
| 309 | - else if($len_a < $len_b) |
|
| 310 | - { |
|
| 311 | - $width = $len_b; |
|
| 312 | - $a = str_pad($a, $width, "0", STR_PAD_LEFT); |
|
| 313 | - } |
|
| 314 | - |
|
| 315 | - // fortunately PHP knows how to XOR each byte in a string |
|
| 316 | - // so we don't have to loop to do it |
|
| 317 | - $bin = self::bin2Str($a) ^ self::bin2Str($b); |
|
| 318 | - return self::str2Bin($bin); |
|
| 319 | - } |
|
| 320 | - |
|
| 321 | - |
|
| 322 | - /** |
|
| 323 | - * ExclusiveOR hex values. Supports an unlimited number of parameters. |
|
| 324 | - * The values are string representations of hex values |
|
| 325 | - * IE: "0a1b2c3d" not 0x0a1b2c3d |
|
| 326 | - * |
|
| 327 | - * @param string Unlimited number parameters, each a string representation of hex |
|
| 328 | - * @return string A string representation of the result in Hex |
|
| 329 | - */ |
|
| 330 | - public static function xorHex() |
|
| 331 | - { |
|
| 332 | - $hex = func_get_args(); |
|
| 333 | - $count = func_num_args(); |
|
| 334 | - |
|
| 335 | - // we need a minimum of 2 values |
|
| 336 | - if($count < 2) |
|
| 337 | - return false; |
|
| 338 | - |
|
| 339 | - // first get all hex values to an even number |
|
| 340 | - array_walk($hex, function(&$val, $i){ |
|
| 341 | - if(strlen($val) % 2) |
|
| 342 | - $val = "0".$val; |
|
| 343 | - }); |
|
| 344 | - |
|
| 345 | - $res = 0; |
|
| 346 | - for($i = 0; $i < $count; ++$i) |
|
| 347 | - { |
|
| 348 | - // if this is the first loop, set the 'result' to the first |
|
| 349 | - // hex value |
|
| 350 | - if($i == 0) |
|
| 351 | - $res = $hex[0]; |
|
| 352 | - else |
|
| 353 | - { |
|
| 354 | - // to make the code easier to follow |
|
| 355 | - $h1 = $res; |
|
| 356 | - $h2 = $hex[$i]; |
|
| 357 | - |
|
| 358 | - // get lengths |
|
| 359 | - $len1 = strlen($h1); |
|
| 360 | - $len2 = strlen($h2); |
|
| 361 | - |
|
| 362 | - // now check that both hex values are the same length, |
|
| 363 | - // if not pad them with 0's until they are |
|
| 364 | - if($len1 > $len2) |
|
| 365 | - $h2 = str_pad($h2, $len1, "0", STR_PAD_LEFT); |
|
| 366 | - else if($len1 < $len2) |
|
| 367 | - $h1 = str_pad($h1, $len2, "0", STR_PAD_LEFT); |
|
| 368 | - |
|
| 369 | - // PHP knows how to XOR each byte in a string, so convert the |
|
| 370 | - // hex to a string, XOR, and convert back |
|
| 371 | - $res = self::hex2Str($h1) ^ self::hex2Str($h2); |
|
| 372 | - $res = self::str2Hex($res); |
|
| 373 | - } |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - return $res; |
|
| 377 | - } |
|
| 378 | - |
|
| 379 | - |
|
| 380 | - /** |
|
| 381 | - * Forces an integer to be signed |
|
| 382 | - * |
|
| 383 | - * @param integer $int An integer |
|
| 384 | - * @return integer An signed integer |
|
| 385 | - */ |
|
| 386 | - public static function sInt($int) |
|
| 387 | - { |
|
| 388 | - $arr = unpack("i", pack("i", $int)); |
|
| 389 | - return $arr[1]; |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - |
|
| 393 | - /** |
|
| 394 | - * Forces an integer to be unsigned |
|
| 395 | - * |
|
| 396 | - * @param integer $int A signed integer |
|
| 397 | - * @return integer An unsigned integer |
|
| 398 | - */ |
|
| 399 | - public static function uInt($int) |
|
| 400 | - { |
|
| 401 | - $arr = unpack("I", pack("I", $int)); |
|
| 402 | - $ret = $arr[1]; |
|
| 403 | - |
|
| 404 | - // On 32 bit platforms unpack() and pack() do not convert |
|
| 405 | - // from signed to unsigned properly all the time, it will return |
|
| 406 | - // the same negative number given to it, the work around is |
|
| 407 | - // to use sprintf(). |
|
| 408 | - // Tested with php 5.3.x on Windows XP & Linux 32bit |
|
| 409 | - if($ret < 0) |
|
| 410 | - $ret = sprintf("%u", $ret) + 0; // convert from string to int |
|
| 411 | - |
|
| 412 | - return $ret; |
|
| 413 | - } |
|
| 414 | - |
|
| 415 | - |
|
| 416 | - /** |
|
| 417 | - * Forces an integer to be a 32 bit signed integer |
|
| 418 | - * |
|
| 419 | - * @param integer $int An integer |
|
| 420 | - * @return integer An signed 32 bit integer |
|
| 421 | - */ |
|
| 422 | - public static function sInt32($int) |
|
| 423 | - { |
|
| 424 | - if(PHP_INT_SIZE === 4) // 32 bit |
|
| 425 | - return self::sInt($int); |
|
| 426 | - else // PHP_INT_SIZE === 8 // 64 bit |
|
| 427 | - { |
|
| 428 | - $arr = unpack("l", pack("l", $int)); |
|
| 429 | - return $arr[1]; |
|
| 430 | - } |
|
| 431 | - } |
|
| 432 | - |
|
| 433 | - |
|
| 434 | - /** |
|
| 435 | - * Force an integer to be a 32 bit unsigned integer |
|
| 436 | - * |
|
| 437 | - * @param integer $int An integer |
|
| 438 | - * @return integer An unsigned 32 bit integer |
|
| 439 | - */ |
|
| 440 | - public static function uInt32($int) |
|
| 441 | - { |
|
| 442 | - if(PHP_INT_SIZE === 4) // 32 bit |
|
| 443 | - return self::uInt($int); |
|
| 444 | - else // PHP_INT_SIZE === 8 // 64 bit |
|
| 445 | - { |
|
| 446 | - $arr = unpack("L", pack("L", $int)); |
|
| 447 | - return $arr[1]; |
|
| 448 | - } |
|
| 449 | - } |
|
| 450 | - |
|
| 451 | - |
|
| 452 | - /** |
|
| 453 | - * Converts an integer to the value for an signed char |
|
| 454 | - * |
|
| 455 | - * @param integer $int The integer to convert to a signed char |
|
| 456 | - * @return integer A signed integer, representing a signed char |
|
| 457 | - */ |
|
| 458 | - public static function sChar($int) |
|
| 459 | - { |
|
| 460 | - $arr = unpack("c", pack("c", $int)); |
|
| 461 | - return $arr[1]; |
|
| 462 | - } |
|
| 463 | - |
|
| 464 | - |
|
| 465 | - /** |
|
| 466 | - * Converts an integer to the value for an unsigned char |
|
| 467 | - * |
|
| 468 | - * @param integer $int The integer to convert to a unsigned char |
|
| 469 | - * @return integer An unsigned integer, representing a unsigned char |
|
| 470 | - */ |
|
| 471 | - public static function uChar($int) |
|
| 472 | - { |
|
| 473 | - $arr = unpack("C", pack("C", $int)); |
|
| 474 | - return $arr[1]; |
|
| 475 | - } |
|
| 476 | - |
|
| 477 | - |
|
| 478 | - /** |
|
| 479 | - * Rotates bits Left, appending the bits pushed off the left onto the right |
|
| 480 | - * |
|
| 481 | - * @param integer $n The integer to rotate bits to the left |
|
| 482 | - * @param integer $shifts The number of shifts left to make |
|
| 483 | - * @return integer The resulting value from the rotation |
|
| 484 | - */ |
|
| 485 | - public static function rotBitsLeft32($i, $shifts) |
|
| 486 | - { |
|
| 487 | - if($shifts <= 0) |
|
| 488 | - return $i; |
|
| 489 | - |
|
| 490 | - $shifts &= 0x1f; /* higher rotates would not bring anything */ |
|
| 491 | - |
|
| 492 | - // this is causing problems on 32 bit platform |
|
| 493 | - //return self::uInt32(($i << $shifts) | ($i >> (32 - $shifts))); |
|
| 494 | - |
|
| 495 | - // so lets cheat: convert to binary string, rotate left, and |
|
| 496 | - // convert back to decimal |
|
| 497 | - $i = self::dec2Bin(self::uInt32($i), 4); |
|
| 498 | - $i = substr($i, $shifts).substr($i, 0, $shifts); |
|
| 499 | - return self::bin2Dec($i); |
|
| 500 | - |
|
| 501 | - } |
|
| 502 | - |
|
| 503 | - |
|
| 504 | - /** |
|
| 505 | - * Rotates bits right, appending the bits pushed off the right onto the left |
|
| 506 | - * |
|
| 507 | - * @param integer $n The integer to rotate bits to the right |
|
| 508 | - * @param integer $shifts The number of shifts right to make |
|
| 509 | - * @return integer The resulting value from the rotation |
|
| 510 | - */ |
|
| 511 | - public static function rotBitsRight32($i, $shifts) |
|
| 512 | - { |
|
| 513 | - if($shifts <= 0) |
|
| 514 | - return $i; |
|
| 515 | - |
|
| 516 | - $shifts &= 0x1f; /* higher rotates would not bring anything */ |
|
| 517 | - |
|
| 518 | - // this might cause problems on 32 bit platforms since rotBitsLeft32 was |
|
| 519 | - // having a problem with some bit shifts on 32 bits |
|
| 520 | - // return self::uInt32(($i >> $shifts) | ($i << (32 - $shifts))); |
|
| 521 | - |
|
| 522 | - // so lets cheat: convert to binary string, rotate right, |
|
| 523 | - // and convert back to decimal |
|
| 524 | - $i = self::dec2Bin($i, 4); |
|
| 525 | - $i = substr($i, (-1 * $shifts)).substr($i, 0, (-1 * $shifts)); |
|
| 526 | - return self::bin2Dec($i); |
|
| 527 | - } |
|
| 528 | - |
|
| 529 | - |
|
| 530 | - /** |
|
| 531 | - * Create a string of random bytes, used for creating an IV |
|
| 532 | - * and a random key. See PHP_Crypt::createKey() and PHP_Crypt::createIV() |
|
| 533 | - * There are 4 ways to auto generate random bytes by setting $src parameter |
|
| 534 | - * PHP_Crypt::RAND - Default, uses mt_rand() |
|
| 535 | - * PHP_Crypt::RAND_DEV_RAND - Unix only, uses /dev/random |
|
| 536 | - * PHP_Crypt::RAND_DEV_URAND - Unix only, uses /dev/urandom |
|
| 537 | - * PHP_Crypt::RAND_WIN_COM - Windows only, uses Microsoft's CAPICOM SDK |
|
| 538 | - * |
|
| 539 | - * @param string $src Optional, Use the $src to create the random bytes |
|
| 540 | - * by default PHP_Crypt::RAND is used when $src is not specified |
|
| 541 | - * @param integer $byte_len The length of the byte string to create |
|
| 542 | - * @return string A random string of bytes |
|
| 543 | - */ |
|
| 544 | - public static function randBytes($src = PHP_Crypt::RAND, $byte_len = PHP_Crypt::RAND_DEFAULT_SZ) |
|
| 545 | - { |
|
| 546 | - $bytes = ""; |
|
| 547 | - $err_msg = ""; |
|
| 548 | - |
|
| 549 | - if($src == PHP_Crypt::RAND_DEV_RAND) |
|
| 550 | - { |
|
| 551 | - if(file_exists(PHP_Crypt::RAND_DEV_RAND)) |
|
| 552 | - $bytes = file_get_contents(PHP_CRYPT::RAND_DEV_RAND, false, null, 0, $byte_len); |
|
| 553 | - else |
|
| 554 | - $err_msg = PHP_Crypt::RAND_DEV_RAND." not found"; |
|
| 555 | - } |
|
| 556 | - else if($src == PHP_Crypt::RAND_DEV_URAND) |
|
| 557 | - { |
|
| 558 | - if(file_exists(PHP_Crypt::RAND_DEV_URAND)) |
|
| 559 | - $bytes = file_get_contents(PHP_CRYPT::RAND_DEV_URAND, false, null, 0, $byte_len); |
|
| 560 | - else |
|
| 561 | - $err_msg = PHP_Crypt::RAND_DEV_URAND." not found"; |
|
| 562 | - } |
|
| 563 | - else if($src == PHP_Crypt::RAND_WIN_COM) |
|
| 564 | - { |
|
| 565 | - if(extension_loaded('com_dotnet')) |
|
| 566 | - { |
|
| 567 | - // http://msdn.microsoft.com/en-us/library/aa388176(VS.85).aspx |
|
| 568 | - try |
|
| 569 | - { |
|
| 570 | - // request a random number in $byte_len bytes, returned |
|
| 571 | - // as base_64 encoded string. This is because PHP munges the |
|
| 572 | - // binary data on Windows |
|
| 573 | - $com = @new \COM("CAPICOM.Utilities.1"); |
|
| 574 | - $bytes = $com->GetRandom($byte_len, 0); |
|
| 575 | - } |
|
| 576 | - catch(Exception $e) |
|
| 577 | - { |
|
| 578 | - $err_msg = "Windows COM exception: ".$e->getMessage(); |
|
| 579 | - } |
|
| 580 | - |
|
| 581 | - if(!$bytes) |
|
| 582 | - $err_msg = "Windows COM failed to create random string of bytes"; |
|
| 583 | - } |
|
| 584 | - else |
|
| 585 | - $err_msg = "The COM_DOTNET extension is not loaded"; |
|
| 586 | - } |
|
| 587 | - |
|
| 588 | - // trigger a warning if something went wrong |
|
| 589 | - if($err_msg != "") |
|
| 590 | - trigger_error("$err_msg. Defaulting to PHP_Crypt::RAND", E_USER_WARNING); |
|
| 591 | - |
|
| 592 | - // if the random bytes where not created properly or PHP_Crypt::RAND was |
|
| 593 | - // passed as the $src param, create the bytes using mt_rand(). It's not |
|
| 594 | - // the most secure option but we have no other choice |
|
| 595 | - if(strlen($bytes) < $byte_len) |
|
| 596 | - { |
|
| 597 | - $bytes = ""; |
|
| 598 | - |
|
| 599 | - // md5() hash a random number to get a 16 byte string, keep looping |
|
| 600 | - // until we have a string as long or longer than the ciphers block size |
|
| 601 | - for($i = 0; ($i * self::HASH_LEN) < $byte_len; ++$i) |
|
| 602 | - $bytes .= md5(mt_rand(), true); |
|
| 603 | - } |
|
| 604 | - |
|
| 605 | - // because $bytes may have come from mt_rand() or /dev/urandom which are not |
|
| 606 | - // cryptographically secure, lets add another layer of 'randomness' before |
|
| 607 | - // the final md5() below |
|
| 608 | - $bytes = str_shuffle($bytes); |
|
| 609 | - |
|
| 610 | - // md5() the $bytes to add extra randomness. Since md5() only returns |
|
| 611 | - // 16 bytes, we may need to loop to generate a string of $bytes big enough for |
|
| 612 | - // some ciphers which have a block size larger than 16 bytes |
|
| 613 | - $tmp = ""; |
|
| 614 | - $loop = ceil(strlen($bytes) / self::HASH_LEN); |
|
| 615 | - for($i = 0; $i < $loop; ++$i) |
|
| 616 | - $tmp .= md5(substr($bytes, ($i * self::HASH_LEN), self::HASH_LEN), true); |
|
| 617 | - |
|
| 618 | - // grab the number of bytes equal to the requested $byte_len |
|
| 619 | - return substr($tmp, 0, $byte_len); |
|
| 620 | - } |
|
| 36 | + /** @type integer HASH_LEN The length of md5() hash string */ |
|
| 37 | + const HASH_LEN = 16; |
|
| 38 | + |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Constructor |
|
| 42 | + * |
|
| 43 | + */ |
|
| 44 | + protected function __construct() |
|
| 45 | + { |
|
| 46 | + |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * Destructor |
|
| 52 | + * |
|
| 53 | + */ |
|
| 54 | + protected function __destruct() |
|
| 55 | + { |
|
| 56 | + |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * Convert hexidecimal to a binary string (ex: "00110110") |
|
| 62 | + * |
|
| 63 | + * @param string $hex A string containing a hexidecimal number |
|
| 64 | + * @return string A string representation of a binary |
|
| 65 | + */ |
|
| 66 | + public static function hex2Bin($hex) |
|
| 67 | + { |
|
| 68 | + // if we do not have an even number of hex characters |
|
| 69 | + // append a 0 to the beginning to make it even |
|
| 70 | + if(strlen($hex) % 2) |
|
| 71 | + $hex = "0$hex"; |
|
| 72 | + |
|
| 73 | + $parts = str_split($hex, 2); |
|
| 74 | + $parts = array_map(function($v) { |
|
| 75 | + $v = base_convert($v, 16, 2); |
|
| 76 | + return str_pad($v, 8, "0", STR_PAD_LEFT); |
|
| 77 | + }, $parts); |
|
| 78 | + |
|
| 79 | + return implode("", $parts); |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * Convert hex to a string |
|
| 85 | + * |
|
| 86 | + * @param string $hex A string representation of Hex (IE: "1a2b3c" not 0x1a2b3c) |
|
| 87 | + * @return string a string |
|
| 88 | + */ |
|
| 89 | + public static function hex2Str($hex) |
|
| 90 | + { |
|
| 91 | + // php version >= 5.4 have a hex2bin function, use it |
|
| 92 | + // if it exists |
|
| 93 | + if(function_exists("hex2bin")) |
|
| 94 | + return hex2bin($hex); |
|
| 95 | + |
|
| 96 | + $parts = str_split($hex, 2); |
|
| 97 | + $parts = array_map(function($v) { |
|
| 98 | + return chr(Core::hex2Dec($v)); |
|
| 99 | + }, $parts); |
|
| 100 | + |
|
| 101 | + return implode("", $parts); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * Converts Hex to Decimal |
|
| 107 | + * This function just calls php's hexdec() function, but I |
|
| 108 | + * encapsulated it in this function to keep things uniform |
|
| 109 | + * and have all possible conversion function available in |
|
| 110 | + * the Cipher class |
|
| 111 | + * |
|
| 112 | + * @param string $hex A hex number to convert to decimal |
|
| 113 | + * @return integer A decimal number |
|
| 114 | + */ |
|
| 115 | + public static function hex2Dec($hex) |
|
| 116 | + { |
|
| 117 | + return hexdec($hex); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Convert binary string (ie 00110110) to hex |
|
| 123 | + * |
|
| 124 | + * @param string $bin A binary string |
|
| 125 | + * @return string A string representation of hexidecimal number |
|
| 126 | + */ |
|
| 127 | + public static function bin2Hex($bin) |
|
| 128 | + { |
|
| 129 | + $parts = str_split($bin, 8); |
|
| 130 | + |
|
| 131 | + $parts = array_map(function($v) { |
|
| 132 | + $v = str_pad($v, 8, "0", STR_PAD_LEFT); |
|
| 133 | + $v = dechex(bindec($v)); |
|
| 134 | + return str_pad($v, 2, "0", STR_PAD_LEFT); |
|
| 135 | + }, $parts); |
|
| 136 | + |
|
| 137 | + return implode("", $parts); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * Converts a binary representation (ie 01101011) back to a string |
|
| 143 | + * |
|
| 144 | + * @param string $bin a binary representation string |
|
| 145 | + * @return string A string of characters representing the binary |
|
| 146 | + */ |
|
| 147 | + public static function bin2Str($bin) |
|
| 148 | + { |
|
| 149 | + $hex = self::bin2Hex($bin); |
|
| 150 | + return self::hex2Str($hex); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * Convert a binary string (ie: 01101011) to a decimal number |
|
| 156 | + * |
|
| 157 | + * @param string A string representation of a binary number |
|
| 158 | + * @return integer The number converted from the binary string |
|
| 159 | + */ |
|
| 160 | + public static function bin2Dec($bin) |
|
| 161 | + { |
|
| 162 | + return bindec($bin); |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * Convert a string to hex |
|
| 168 | + * This function calls the PHP bin2hex(), and is here |
|
| 169 | + * for consistency with the other string functions |
|
| 170 | + * |
|
| 171 | + * @param string $str A string |
|
| 172 | + * @return string A string representation of hexidecimal number |
|
| 173 | + */ |
|
| 174 | + public static function str2Hex($str) |
|
| 175 | + { |
|
| 176 | + return bin2hex($str); |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * Convert a string of characters to a decimal number |
|
| 182 | + * |
|
| 183 | + * @param string $str The string to convert to decimal |
|
| 184 | + * @return integer The integer converted from the string |
|
| 185 | + */ |
|
| 186 | + public static function str2Dec($str) |
|
| 187 | + { |
|
| 188 | + $hex = self::str2Hex($str); |
|
| 189 | + return self::hex2Dec($hex); |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * Converts a string to binary representation (ie 01101011) |
|
| 195 | + * |
|
| 196 | + * @param string $str A string |
|
| 197 | + * @return string A binary representation of the the string |
|
| 198 | + */ |
|
| 199 | + public static function str2Bin($str) |
|
| 200 | + { |
|
| 201 | + $hex = self::str2Hex($str); |
|
| 202 | + $parts = str_split($hex, 2); |
|
| 203 | + |
|
| 204 | + $parts = array_map(function($v) { |
|
| 205 | + return Core::hex2Bin($v); |
|
| 206 | + }, $parts); |
|
| 207 | + |
|
| 208 | + return implode("", $parts); |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * Converts Decimal to Hex |
|
| 214 | + * This function just calls php's dechex() function, but I |
|
| 215 | + * encapsulated it in this function to keep things uniform |
|
| 216 | + * and have all possible conversion function available in |
|
| 217 | + * the Cipher class |
|
| 218 | + * |
|
| 219 | + * The parameter $req_bytes will pad the return hex with NULL (00) |
|
| 220 | + * until the hex represents the number of bytes given to $req_bytes |
|
| 221 | + * This is because dechex() drops null bytes from the Hex, which may |
|
| 222 | + * be needed in some cases |
|
| 223 | + * |
|
| 224 | + * @param integer $dec A decimal number to convert |
|
| 225 | + * @param integer $req_bytes Optional, forces the string to be at least |
|
| 226 | + * $req_bytes in size, this is needed because on occasion left most null bytes |
|
| 227 | + * are dropped in dechex(), causing the string to have a shorter byte |
|
| 228 | + * size than the initial integer. |
|
| 229 | + * @return string A hexidecimal representation of the decimal number |
|
| 230 | + */ |
|
| 231 | + public static function dec2Hex($dec, $req_bytes = 0) |
|
| 232 | + { |
|
| 233 | + $hex = dechex($dec); |
|
| 234 | + |
|
| 235 | + // if we do not have an even number of hex characters |
|
| 236 | + // append a 0 to the beginning. dechex() drops leading 0's |
|
| 237 | + if(strlen($hex) % 2) |
|
| 238 | + $hex = "0$hex"; |
|
| 239 | + |
|
| 240 | + // if the number of bytes in the hex is less than |
|
| 241 | + // what we need it to be, add null bytes to the |
|
| 242 | + // front of the hex to padd it to the required size |
|
| 243 | + if(($req_bytes * 2) > strlen($hex)) |
|
| 244 | + $hex = str_pad($hex, ($req_bytes * 2), "0", STR_PAD_LEFT); |
|
| 245 | + |
|
| 246 | + return $hex; |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * Converts Decimal to Binary |
|
| 252 | + * This function just calls php's decbin() function, but I |
|
| 253 | + * encapsulated it in this function to keep things uniform |
|
| 254 | + * and have all possible conversion function available in |
|
| 255 | + * the Cipher class |
|
| 256 | + * |
|
| 257 | + * @param integer $dec A decimal number to convert |
|
| 258 | + * @param integer $req_bytes Optional, forces the string to be at least |
|
| 259 | + * $req_bytes in size, this is needed because on occasion left most null bytes |
|
| 260 | + * are dropped in dechex(), causing the string to have a shorter byte |
|
| 261 | + * size than the initial integer. |
|
| 262 | + * @return string A binary representation of the decimal number |
|
| 263 | + */ |
|
| 264 | + public static function dec2Bin($dec, $req_bytes = 0) |
|
| 265 | + { |
|
| 266 | + $hex = self::dec2Hex($dec, $req_bytes); |
|
| 267 | + return self::hex2Bin($hex); |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + |
|
| 271 | + /** |
|
| 272 | + * Convert a decimal to a string of bytes |
|
| 273 | + * |
|
| 274 | + * @param integer $dec A decimal number |
|
| 275 | + * @param integer $req_bytes Optional, forces the string to be at least |
|
| 276 | + * $req_bytes in size, this is needed because on occasion left most null bytes |
|
| 277 | + * are dropped in dechex(), causing the string to have a shorter byte |
|
| 278 | + * size than the initial integer. |
|
| 279 | + * @return string A string with the number of bytes equal to $dec |
|
| 280 | + */ |
|
| 281 | + public static function dec2Str($dec, $req_bytes = 0) |
|
| 282 | + { |
|
| 283 | + $hex = self::dec2Hex($dec, $req_bytes); |
|
| 284 | + return self::hex2Str($hex); |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + |
|
| 288 | + /** |
|
| 289 | + * XORs two binary strings (representation of binary, ie 01101011), |
|
| 290 | + * assumed to be equal length |
|
| 291 | + * |
|
| 292 | + * @param string $a A string that represents binary |
|
| 293 | + * @param string $b A string that represents binary |
|
| 294 | + * @return string A representation of binary |
|
| 295 | + */ |
|
| 296 | + public static function xorBin($a, $b) |
|
| 297 | + { |
|
| 298 | + $len_a = strlen($a); |
|
| 299 | + $len_b = strlen($b); |
|
| 300 | + $width = $len_a; |
|
| 301 | + |
|
| 302 | + // first determine if the two binary strings are the same length, |
|
| 303 | + // and if not get them to the same length |
|
| 304 | + if($len_a > $len_b) |
|
| 305 | + { |
|
| 306 | + $width = $len_a; |
|
| 307 | + $b = str_pad($b, $width, "0", STR_PAD_LEFT); |
|
| 308 | + } |
|
| 309 | + else if($len_a < $len_b) |
|
| 310 | + { |
|
| 311 | + $width = $len_b; |
|
| 312 | + $a = str_pad($a, $width, "0", STR_PAD_LEFT); |
|
| 313 | + } |
|
| 314 | + |
|
| 315 | + // fortunately PHP knows how to XOR each byte in a string |
|
| 316 | + // so we don't have to loop to do it |
|
| 317 | + $bin = self::bin2Str($a) ^ self::bin2Str($b); |
|
| 318 | + return self::str2Bin($bin); |
|
| 319 | + } |
|
| 320 | + |
|
| 321 | + |
|
| 322 | + /** |
|
| 323 | + * ExclusiveOR hex values. Supports an unlimited number of parameters. |
|
| 324 | + * The values are string representations of hex values |
|
| 325 | + * IE: "0a1b2c3d" not 0x0a1b2c3d |
|
| 326 | + * |
|
| 327 | + * @param string Unlimited number parameters, each a string representation of hex |
|
| 328 | + * @return string A string representation of the result in Hex |
|
| 329 | + */ |
|
| 330 | + public static function xorHex() |
|
| 331 | + { |
|
| 332 | + $hex = func_get_args(); |
|
| 333 | + $count = func_num_args(); |
|
| 334 | + |
|
| 335 | + // we need a minimum of 2 values |
|
| 336 | + if($count < 2) |
|
| 337 | + return false; |
|
| 338 | + |
|
| 339 | + // first get all hex values to an even number |
|
| 340 | + array_walk($hex, function(&$val, $i){ |
|
| 341 | + if(strlen($val) % 2) |
|
| 342 | + $val = "0".$val; |
|
| 343 | + }); |
|
| 344 | + |
|
| 345 | + $res = 0; |
|
| 346 | + for($i = 0; $i < $count; ++$i) |
|
| 347 | + { |
|
| 348 | + // if this is the first loop, set the 'result' to the first |
|
| 349 | + // hex value |
|
| 350 | + if($i == 0) |
|
| 351 | + $res = $hex[0]; |
|
| 352 | + else |
|
| 353 | + { |
|
| 354 | + // to make the code easier to follow |
|
| 355 | + $h1 = $res; |
|
| 356 | + $h2 = $hex[$i]; |
|
| 357 | + |
|
| 358 | + // get lengths |
|
| 359 | + $len1 = strlen($h1); |
|
| 360 | + $len2 = strlen($h2); |
|
| 361 | + |
|
| 362 | + // now check that both hex values are the same length, |
|
| 363 | + // if not pad them with 0's until they are |
|
| 364 | + if($len1 > $len2) |
|
| 365 | + $h2 = str_pad($h2, $len1, "0", STR_PAD_LEFT); |
|
| 366 | + else if($len1 < $len2) |
|
| 367 | + $h1 = str_pad($h1, $len2, "0", STR_PAD_LEFT); |
|
| 368 | + |
|
| 369 | + // PHP knows how to XOR each byte in a string, so convert the |
|
| 370 | + // hex to a string, XOR, and convert back |
|
| 371 | + $res = self::hex2Str($h1) ^ self::hex2Str($h2); |
|
| 372 | + $res = self::str2Hex($res); |
|
| 373 | + } |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + return $res; |
|
| 377 | + } |
|
| 378 | + |
|
| 379 | + |
|
| 380 | + /** |
|
| 381 | + * Forces an integer to be signed |
|
| 382 | + * |
|
| 383 | + * @param integer $int An integer |
|
| 384 | + * @return integer An signed integer |
|
| 385 | + */ |
|
| 386 | + public static function sInt($int) |
|
| 387 | + { |
|
| 388 | + $arr = unpack("i", pack("i", $int)); |
|
| 389 | + return $arr[1]; |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + |
|
| 393 | + /** |
|
| 394 | + * Forces an integer to be unsigned |
|
| 395 | + * |
|
| 396 | + * @param integer $int A signed integer |
|
| 397 | + * @return integer An unsigned integer |
|
| 398 | + */ |
|
| 399 | + public static function uInt($int) |
|
| 400 | + { |
|
| 401 | + $arr = unpack("I", pack("I", $int)); |
|
| 402 | + $ret = $arr[1]; |
|
| 403 | + |
|
| 404 | + // On 32 bit platforms unpack() and pack() do not convert |
|
| 405 | + // from signed to unsigned properly all the time, it will return |
|
| 406 | + // the same negative number given to it, the work around is |
|
| 407 | + // to use sprintf(). |
|
| 408 | + // Tested with php 5.3.x on Windows XP & Linux 32bit |
|
| 409 | + if($ret < 0) |
|
| 410 | + $ret = sprintf("%u", $ret) + 0; // convert from string to int |
|
| 411 | + |
|
| 412 | + return $ret; |
|
| 413 | + } |
|
| 414 | + |
|
| 415 | + |
|
| 416 | + /** |
|
| 417 | + * Forces an integer to be a 32 bit signed integer |
|
| 418 | + * |
|
| 419 | + * @param integer $int An integer |
|
| 420 | + * @return integer An signed 32 bit integer |
|
| 421 | + */ |
|
| 422 | + public static function sInt32($int) |
|
| 423 | + { |
|
| 424 | + if(PHP_INT_SIZE === 4) // 32 bit |
|
| 425 | + return self::sInt($int); |
|
| 426 | + else // PHP_INT_SIZE === 8 // 64 bit |
|
| 427 | + { |
|
| 428 | + $arr = unpack("l", pack("l", $int)); |
|
| 429 | + return $arr[1]; |
|
| 430 | + } |
|
| 431 | + } |
|
| 432 | + |
|
| 433 | + |
|
| 434 | + /** |
|
| 435 | + * Force an integer to be a 32 bit unsigned integer |
|
| 436 | + * |
|
| 437 | + * @param integer $int An integer |
|
| 438 | + * @return integer An unsigned 32 bit integer |
|
| 439 | + */ |
|
| 440 | + public static function uInt32($int) |
|
| 441 | + { |
|
| 442 | + if(PHP_INT_SIZE === 4) // 32 bit |
|
| 443 | + return self::uInt($int); |
|
| 444 | + else // PHP_INT_SIZE === 8 // 64 bit |
|
| 445 | + { |
|
| 446 | + $arr = unpack("L", pack("L", $int)); |
|
| 447 | + return $arr[1]; |
|
| 448 | + } |
|
| 449 | + } |
|
| 450 | + |
|
| 451 | + |
|
| 452 | + /** |
|
| 453 | + * Converts an integer to the value for an signed char |
|
| 454 | + * |
|
| 455 | + * @param integer $int The integer to convert to a signed char |
|
| 456 | + * @return integer A signed integer, representing a signed char |
|
| 457 | + */ |
|
| 458 | + public static function sChar($int) |
|
| 459 | + { |
|
| 460 | + $arr = unpack("c", pack("c", $int)); |
|
| 461 | + return $arr[1]; |
|
| 462 | + } |
|
| 463 | + |
|
| 464 | + |
|
| 465 | + /** |
|
| 466 | + * Converts an integer to the value for an unsigned char |
|
| 467 | + * |
|
| 468 | + * @param integer $int The integer to convert to a unsigned char |
|
| 469 | + * @return integer An unsigned integer, representing a unsigned char |
|
| 470 | + */ |
|
| 471 | + public static function uChar($int) |
|
| 472 | + { |
|
| 473 | + $arr = unpack("C", pack("C", $int)); |
|
| 474 | + return $arr[1]; |
|
| 475 | + } |
|
| 476 | + |
|
| 477 | + |
|
| 478 | + /** |
|
| 479 | + * Rotates bits Left, appending the bits pushed off the left onto the right |
|
| 480 | + * |
|
| 481 | + * @param integer $n The integer to rotate bits to the left |
|
| 482 | + * @param integer $shifts The number of shifts left to make |
|
| 483 | + * @return integer The resulting value from the rotation |
|
| 484 | + */ |
|
| 485 | + public static function rotBitsLeft32($i, $shifts) |
|
| 486 | + { |
|
| 487 | + if($shifts <= 0) |
|
| 488 | + return $i; |
|
| 489 | + |
|
| 490 | + $shifts &= 0x1f; /* higher rotates would not bring anything */ |
|
| 491 | + |
|
| 492 | + // this is causing problems on 32 bit platform |
|
| 493 | + //return self::uInt32(($i << $shifts) | ($i >> (32 - $shifts))); |
|
| 494 | + |
|
| 495 | + // so lets cheat: convert to binary string, rotate left, and |
|
| 496 | + // convert back to decimal |
|
| 497 | + $i = self::dec2Bin(self::uInt32($i), 4); |
|
| 498 | + $i = substr($i, $shifts).substr($i, 0, $shifts); |
|
| 499 | + return self::bin2Dec($i); |
|
| 500 | + |
|
| 501 | + } |
|
| 502 | + |
|
| 503 | + |
|
| 504 | + /** |
|
| 505 | + * Rotates bits right, appending the bits pushed off the right onto the left |
|
| 506 | + * |
|
| 507 | + * @param integer $n The integer to rotate bits to the right |
|
| 508 | + * @param integer $shifts The number of shifts right to make |
|
| 509 | + * @return integer The resulting value from the rotation |
|
| 510 | + */ |
|
| 511 | + public static function rotBitsRight32($i, $shifts) |
|
| 512 | + { |
|
| 513 | + if($shifts <= 0) |
|
| 514 | + return $i; |
|
| 515 | + |
|
| 516 | + $shifts &= 0x1f; /* higher rotates would not bring anything */ |
|
| 517 | + |
|
| 518 | + // this might cause problems on 32 bit platforms since rotBitsLeft32 was |
|
| 519 | + // having a problem with some bit shifts on 32 bits |
|
| 520 | + // return self::uInt32(($i >> $shifts) | ($i << (32 - $shifts))); |
|
| 521 | + |
|
| 522 | + // so lets cheat: convert to binary string, rotate right, |
|
| 523 | + // and convert back to decimal |
|
| 524 | + $i = self::dec2Bin($i, 4); |
|
| 525 | + $i = substr($i, (-1 * $shifts)).substr($i, 0, (-1 * $shifts)); |
|
| 526 | + return self::bin2Dec($i); |
|
| 527 | + } |
|
| 528 | + |
|
| 529 | + |
|
| 530 | + /** |
|
| 531 | + * Create a string of random bytes, used for creating an IV |
|
| 532 | + * and a random key. See PHP_Crypt::createKey() and PHP_Crypt::createIV() |
|
| 533 | + * There are 4 ways to auto generate random bytes by setting $src parameter |
|
| 534 | + * PHP_Crypt::RAND - Default, uses mt_rand() |
|
| 535 | + * PHP_Crypt::RAND_DEV_RAND - Unix only, uses /dev/random |
|
| 536 | + * PHP_Crypt::RAND_DEV_URAND - Unix only, uses /dev/urandom |
|
| 537 | + * PHP_Crypt::RAND_WIN_COM - Windows only, uses Microsoft's CAPICOM SDK |
|
| 538 | + * |
|
| 539 | + * @param string $src Optional, Use the $src to create the random bytes |
|
| 540 | + * by default PHP_Crypt::RAND is used when $src is not specified |
|
| 541 | + * @param integer $byte_len The length of the byte string to create |
|
| 542 | + * @return string A random string of bytes |
|
| 543 | + */ |
|
| 544 | + public static function randBytes($src = PHP_Crypt::RAND, $byte_len = PHP_Crypt::RAND_DEFAULT_SZ) |
|
| 545 | + { |
|
| 546 | + $bytes = ""; |
|
| 547 | + $err_msg = ""; |
|
| 548 | + |
|
| 549 | + if($src == PHP_Crypt::RAND_DEV_RAND) |
|
| 550 | + { |
|
| 551 | + if(file_exists(PHP_Crypt::RAND_DEV_RAND)) |
|
| 552 | + $bytes = file_get_contents(PHP_CRYPT::RAND_DEV_RAND, false, null, 0, $byte_len); |
|
| 553 | + else |
|
| 554 | + $err_msg = PHP_Crypt::RAND_DEV_RAND." not found"; |
|
| 555 | + } |
|
| 556 | + else if($src == PHP_Crypt::RAND_DEV_URAND) |
|
| 557 | + { |
|
| 558 | + if(file_exists(PHP_Crypt::RAND_DEV_URAND)) |
|
| 559 | + $bytes = file_get_contents(PHP_CRYPT::RAND_DEV_URAND, false, null, 0, $byte_len); |
|
| 560 | + else |
|
| 561 | + $err_msg = PHP_Crypt::RAND_DEV_URAND." not found"; |
|
| 562 | + } |
|
| 563 | + else if($src == PHP_Crypt::RAND_WIN_COM) |
|
| 564 | + { |
|
| 565 | + if(extension_loaded('com_dotnet')) |
|
| 566 | + { |
|
| 567 | + // http://msdn.microsoft.com/en-us/library/aa388176(VS.85).aspx |
|
| 568 | + try |
|
| 569 | + { |
|
| 570 | + // request a random number in $byte_len bytes, returned |
|
| 571 | + // as base_64 encoded string. This is because PHP munges the |
|
| 572 | + // binary data on Windows |
|
| 573 | + $com = @new \COM("CAPICOM.Utilities.1"); |
|
| 574 | + $bytes = $com->GetRandom($byte_len, 0); |
|
| 575 | + } |
|
| 576 | + catch(Exception $e) |
|
| 577 | + { |
|
| 578 | + $err_msg = "Windows COM exception: ".$e->getMessage(); |
|
| 579 | + } |
|
| 580 | + |
|
| 581 | + if(!$bytes) |
|
| 582 | + $err_msg = "Windows COM failed to create random string of bytes"; |
|
| 583 | + } |
|
| 584 | + else |
|
| 585 | + $err_msg = "The COM_DOTNET extension is not loaded"; |
|
| 586 | + } |
|
| 587 | + |
|
| 588 | + // trigger a warning if something went wrong |
|
| 589 | + if($err_msg != "") |
|
| 590 | + trigger_error("$err_msg. Defaulting to PHP_Crypt::RAND", E_USER_WARNING); |
|
| 591 | + |
|
| 592 | + // if the random bytes where not created properly or PHP_Crypt::RAND was |
|
| 593 | + // passed as the $src param, create the bytes using mt_rand(). It's not |
|
| 594 | + // the most secure option but we have no other choice |
|
| 595 | + if(strlen($bytes) < $byte_len) |
|
| 596 | + { |
|
| 597 | + $bytes = ""; |
|
| 598 | + |
|
| 599 | + // md5() hash a random number to get a 16 byte string, keep looping |
|
| 600 | + // until we have a string as long or longer than the ciphers block size |
|
| 601 | + for($i = 0; ($i * self::HASH_LEN) < $byte_len; ++$i) |
|
| 602 | + $bytes .= md5(mt_rand(), true); |
|
| 603 | + } |
|
| 604 | + |
|
| 605 | + // because $bytes may have come from mt_rand() or /dev/urandom which are not |
|
| 606 | + // cryptographically secure, lets add another layer of 'randomness' before |
|
| 607 | + // the final md5() below |
|
| 608 | + $bytes = str_shuffle($bytes); |
|
| 609 | + |
|
| 610 | + // md5() the $bytes to add extra randomness. Since md5() only returns |
|
| 611 | + // 16 bytes, we may need to loop to generate a string of $bytes big enough for |
|
| 612 | + // some ciphers which have a block size larger than 16 bytes |
|
| 613 | + $tmp = ""; |
|
| 614 | + $loop = ceil(strlen($bytes) / self::HASH_LEN); |
|
| 615 | + for($i = 0; $i < $loop; ++$i) |
|
| 616 | + $tmp .= md5(substr($bytes, ($i * self::HASH_LEN), self::HASH_LEN), true); |
|
| 617 | + |
|
| 618 | + // grab the number of bytes equal to the requested $byte_len |
|
| 619 | + return substr($tmp, 0, $byte_len); |
|
| 620 | + } |
|
| 621 | 621 | } |
| 622 | 622 | ?> |
@@ -35,423 +35,423 @@ |
||
| 35 | 35 | */ |
| 36 | 36 | class PHP_Crypt |
| 37 | 37 | { |
| 38 | - // Ciphers |
|
| 39 | - const CIPHER_3DES = "3DES"; |
|
| 40 | - const CIPHER_3WAY = "3-Way"; |
|
| 41 | - const CIPHER_AES_128 = "AES-128"; |
|
| 42 | - const CIPHER_AES_192 = "AES-192"; |
|
| 43 | - const CIPHER_AES_256 = "AES-256"; |
|
| 44 | - const CIPHER_ARC4 = "ARC4"; // Alternative RC4 |
|
| 45 | - const CIPHER_BLOWFISH = "Blowfish"; |
|
| 46 | - const CIPHER_CAST_128 = "CAST-128"; |
|
| 47 | - const CIPHER_CAST_256 = "CAST-256"; |
|
| 48 | - const CIPHER_DES = "DES"; |
|
| 49 | - const CIPHER_ENIGMA = "Enigma"; |
|
| 50 | - const CIPHER_GOST = "GOST"; |
|
| 51 | - const CIPHER_RC2 = "RC2"; |
|
| 52 | - const CIPHER_RIJNDAEL_128 = "Rijndael-128"; |
|
| 53 | - const CIPHER_RIJNDAEL_192 = "Rijndael-192"; |
|
| 54 | - const CIPHER_RIJNDAEL_256 = "Rijndael-256"; |
|
| 55 | - const CIPHER_SKIPJACK = "Skipjack"; |
|
| 56 | - const CIPHER_SIMPLEXOR = "SimpleXOR"; |
|
| 57 | - const CIPHER_VIGENERE = "Vigenere"; // historical |
|
| 58 | - |
|
| 59 | - // Modes |
|
| 60 | - const MODE_CBC = "CBC"; |
|
| 61 | - const MODE_CFB = "CFB"; // 8 bit cfb mode |
|
| 62 | - const MODE_CTR = "CTR"; |
|
| 63 | - const MODE_ECB = "ECB"; |
|
| 64 | - const MODE_NCFB = "NCFB"; // blocksize cfb mode |
|
| 65 | - const MODE_NOFB = "NOFB"; // blocksize ofb mode |
|
| 66 | - const MODE_OFB = "OFB"; // 8 bit ofb mode |
|
| 67 | - const MODE_PCBC = "PCBC"; |
|
| 68 | - const MODE_RAW = "Raw"; // raw encryption, with no mode |
|
| 69 | - const MODE_STREAM = "Stream"; // used only for stream ciphers |
|
| 70 | - |
|
| 71 | - // The source of random data used to create keys and IV's |
|
| 72 | - // Used for PHP_Crypt::createKey(), PHP_Crypt::createIV() |
|
| 73 | - const RAND = "rand"; // uses mt_rand(), windows & unix |
|
| 74 | - const RAND_DEV_RAND = "/dev/random"; // unix only |
|
| 75 | - const RAND_DEV_URAND= "/dev/urandom";// unix only |
|
| 76 | - const RAND_WIN_COM = "wincom"; // windows only, COM extension |
|
| 77 | - const RAND_DEFAULT_SZ = 32; // the default number of bytes returned |
|
| 78 | - |
|
| 79 | - // Padding types |
|
| 80 | - const PAD_ZERO = 0; |
|
| 81 | - const PAD_ANSI_X923 = 1; |
|
| 82 | - const PAD_ISO_10126 = 2; |
|
| 83 | - const PAD_PKCS7 = 3; |
|
| 84 | - const PAD_ISO_7816_4 = 4; |
|
| 85 | - |
|
| 86 | - |
|
| 87 | - /** @type object $cipher An instance of the cipher object selected */ |
|
| 88 | - private $cipher = null; |
|
| 89 | - |
|
| 90 | - /** @type object $mode An instance of the mode object selected */ |
|
| 91 | - private $mode = null; |
|
| 92 | - |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * Constructor |
|
| 96 | - * |
|
| 97 | - * @param string $key The key to use for the selected Cipher |
|
| 98 | - * @param string $cipher The type of cipher to use |
|
| 99 | - * @param string $mode The encrypt mode to use with the cipher |
|
| 100 | - * @param string $padding The padding type to use. Defaults to PAD_ZERO |
|
| 101 | - * @return void |
|
| 102 | - */ |
|
| 103 | - public function __construct($key, $cipher = self::CIPHER_AES_128, $mode = self::MODE_ECB, $padding = self::PAD_ZERO) |
|
| 104 | - { |
|
| 105 | - /* |
|
| 38 | + // Ciphers |
|
| 39 | + const CIPHER_3DES = "3DES"; |
|
| 40 | + const CIPHER_3WAY = "3-Way"; |
|
| 41 | + const CIPHER_AES_128 = "AES-128"; |
|
| 42 | + const CIPHER_AES_192 = "AES-192"; |
|
| 43 | + const CIPHER_AES_256 = "AES-256"; |
|
| 44 | + const CIPHER_ARC4 = "ARC4"; // Alternative RC4 |
|
| 45 | + const CIPHER_BLOWFISH = "Blowfish"; |
|
| 46 | + const CIPHER_CAST_128 = "CAST-128"; |
|
| 47 | + const CIPHER_CAST_256 = "CAST-256"; |
|
| 48 | + const CIPHER_DES = "DES"; |
|
| 49 | + const CIPHER_ENIGMA = "Enigma"; |
|
| 50 | + const CIPHER_GOST = "GOST"; |
|
| 51 | + const CIPHER_RC2 = "RC2"; |
|
| 52 | + const CIPHER_RIJNDAEL_128 = "Rijndael-128"; |
|
| 53 | + const CIPHER_RIJNDAEL_192 = "Rijndael-192"; |
|
| 54 | + const CIPHER_RIJNDAEL_256 = "Rijndael-256"; |
|
| 55 | + const CIPHER_SKIPJACK = "Skipjack"; |
|
| 56 | + const CIPHER_SIMPLEXOR = "SimpleXOR"; |
|
| 57 | + const CIPHER_VIGENERE = "Vigenere"; // historical |
|
| 58 | + |
|
| 59 | + // Modes |
|
| 60 | + const MODE_CBC = "CBC"; |
|
| 61 | + const MODE_CFB = "CFB"; // 8 bit cfb mode |
|
| 62 | + const MODE_CTR = "CTR"; |
|
| 63 | + const MODE_ECB = "ECB"; |
|
| 64 | + const MODE_NCFB = "NCFB"; // blocksize cfb mode |
|
| 65 | + const MODE_NOFB = "NOFB"; // blocksize ofb mode |
|
| 66 | + const MODE_OFB = "OFB"; // 8 bit ofb mode |
|
| 67 | + const MODE_PCBC = "PCBC"; |
|
| 68 | + const MODE_RAW = "Raw"; // raw encryption, with no mode |
|
| 69 | + const MODE_STREAM = "Stream"; // used only for stream ciphers |
|
| 70 | + |
|
| 71 | + // The source of random data used to create keys and IV's |
|
| 72 | + // Used for PHP_Crypt::createKey(), PHP_Crypt::createIV() |
|
| 73 | + const RAND = "rand"; // uses mt_rand(), windows & unix |
|
| 74 | + const RAND_DEV_RAND = "/dev/random"; // unix only |
|
| 75 | + const RAND_DEV_URAND= "/dev/urandom";// unix only |
|
| 76 | + const RAND_WIN_COM = "wincom"; // windows only, COM extension |
|
| 77 | + const RAND_DEFAULT_SZ = 32; // the default number of bytes returned |
|
| 78 | + |
|
| 79 | + // Padding types |
|
| 80 | + const PAD_ZERO = 0; |
|
| 81 | + const PAD_ANSI_X923 = 1; |
|
| 82 | + const PAD_ISO_10126 = 2; |
|
| 83 | + const PAD_PKCS7 = 3; |
|
| 84 | + const PAD_ISO_7816_4 = 4; |
|
| 85 | + |
|
| 86 | + |
|
| 87 | + /** @type object $cipher An instance of the cipher object selected */ |
|
| 88 | + private $cipher = null; |
|
| 89 | + |
|
| 90 | + /** @type object $mode An instance of the mode object selected */ |
|
| 91 | + private $mode = null; |
|
| 92 | + |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * Constructor |
|
| 96 | + * |
|
| 97 | + * @param string $key The key to use for the selected Cipher |
|
| 98 | + * @param string $cipher The type of cipher to use |
|
| 99 | + * @param string $mode The encrypt mode to use with the cipher |
|
| 100 | + * @param string $padding The padding type to use. Defaults to PAD_ZERO |
|
| 101 | + * @return void |
|
| 102 | + */ |
|
| 103 | + public function __construct($key, $cipher = self::CIPHER_AES_128, $mode = self::MODE_ECB, $padding = self::PAD_ZERO) |
|
| 104 | + { |
|
| 105 | + /* |
|
| 106 | 106 | * CIPHERS |
| 107 | 107 | */ |
| 108 | - switch($cipher) |
|
| 109 | - { |
|
| 110 | - case self::CIPHER_3DES: |
|
| 111 | - $this->cipher = new Cipher_3DES($key); |
|
| 112 | - break; |
|
| 108 | + switch($cipher) |
|
| 109 | + { |
|
| 110 | + case self::CIPHER_3DES: |
|
| 111 | + $this->cipher = new Cipher_3DES($key); |
|
| 112 | + break; |
|
| 113 | 113 | |
| 114 | - case self::CIPHER_3WAY: |
|
| 115 | - $this->cipher = new Cipher_3WAY($key); |
|
| 116 | - break; |
|
| 114 | + case self::CIPHER_3WAY: |
|
| 115 | + $this->cipher = new Cipher_3WAY($key); |
|
| 116 | + break; |
|
| 117 | 117 | |
| 118 | - case self::CIPHER_AES_128: |
|
| 119 | - $this->cipher = new Cipher_AES_128($key); |
|
| 120 | - break; |
|
| 118 | + case self::CIPHER_AES_128: |
|
| 119 | + $this->cipher = new Cipher_AES_128($key); |
|
| 120 | + break; |
|
| 121 | 121 | |
| 122 | - case self::CIPHER_AES_192: |
|
| 123 | - $this->cipher = new Cipher_AES_192($key); |
|
| 124 | - break; |
|
| 122 | + case self::CIPHER_AES_192: |
|
| 123 | + $this->cipher = new Cipher_AES_192($key); |
|
| 124 | + break; |
|
| 125 | 125 | |
| 126 | - case self::CIPHER_AES_256: |
|
| 127 | - $this->cipher = new Cipher_AES_256($key); |
|
| 128 | - break; |
|
| 126 | + case self::CIPHER_AES_256: |
|
| 127 | + $this->cipher = new Cipher_AES_256($key); |
|
| 128 | + break; |
|
| 129 | 129 | |
| 130 | - case self::CIPHER_ARC4: // an alternative to RC4 |
|
| 131 | - $this->cipher = new Cipher_ARC4($key); |
|
| 132 | - break; |
|
| 130 | + case self::CIPHER_ARC4: // an alternative to RC4 |
|
| 131 | + $this->cipher = new Cipher_ARC4($key); |
|
| 132 | + break; |
|
| 133 | 133 | |
| 134 | - case self::CIPHER_BLOWFISH: |
|
| 135 | - $this->cipher = new Cipher_Blowfish($key); |
|
| 136 | - break; |
|
| 134 | + case self::CIPHER_BLOWFISH: |
|
| 135 | + $this->cipher = new Cipher_Blowfish($key); |
|
| 136 | + break; |
|
| 137 | 137 | |
| 138 | - case self::CIPHER_CAST_128: |
|
| 139 | - $this->cipher = new Cipher_CAST_128($key); |
|
| 140 | - break; |
|
| 138 | + case self::CIPHER_CAST_128: |
|
| 139 | + $this->cipher = new Cipher_CAST_128($key); |
|
| 140 | + break; |
|
| 141 | 141 | |
| 142 | - case self::CIPHER_CAST_256: |
|
| 143 | - $this->cipher = new Cipher_CAST_256($key); |
|
| 144 | - break; |
|
| 142 | + case self::CIPHER_CAST_256: |
|
| 143 | + $this->cipher = new Cipher_CAST_256($key); |
|
| 144 | + break; |
|
| 145 | 145 | |
| 146 | - case self::CIPHER_DES: |
|
| 147 | - $this->cipher = new Cipher_DES($key); |
|
| 148 | - break; |
|
| 146 | + case self::CIPHER_DES: |
|
| 147 | + $this->cipher = new Cipher_DES($key); |
|
| 148 | + break; |
|
| 149 | 149 | |
| 150 | - case self::CIPHER_ENIGMA: |
|
| 151 | - $this->cipher = new Cipher_Enigma($key); |
|
| 152 | - break; |
|
| 150 | + case self::CIPHER_ENIGMA: |
|
| 151 | + $this->cipher = new Cipher_Enigma($key); |
|
| 152 | + break; |
|
| 153 | 153 | |
| 154 | - case self::CIPHER_GOST: |
|
| 155 | - $this->cipher = new Cipher_GOST($key); |
|
| 156 | - break; |
|
| 154 | + case self::CIPHER_GOST: |
|
| 155 | + $this->cipher = new Cipher_GOST($key); |
|
| 156 | + break; |
|
| 157 | 157 | |
| 158 | - case self::CIPHER_RC2: |
|
| 159 | - $this->cipher = new Cipher_RC2($key); |
|
| 160 | - break; |
|
| 158 | + case self::CIPHER_RC2: |
|
| 159 | + $this->cipher = new Cipher_RC2($key); |
|
| 160 | + break; |
|
| 161 | 161 | |
| 162 | - case self::CIPHER_RIJNDAEL_128: |
|
| 163 | - $this->cipher = new Cipher_Rijndael_128($key); |
|
| 164 | - break; |
|
| 162 | + case self::CIPHER_RIJNDAEL_128: |
|
| 163 | + $this->cipher = new Cipher_Rijndael_128($key); |
|
| 164 | + break; |
|
| 165 | 165 | |
| 166 | - case self::CIPHER_RIJNDAEL_192: |
|
| 167 | - $this->cipher = new Cipher_Rijndael_192($key); |
|
| 168 | - break; |
|
| 166 | + case self::CIPHER_RIJNDAEL_192: |
|
| 167 | + $this->cipher = new Cipher_Rijndael_192($key); |
|
| 168 | + break; |
|
| 169 | 169 | |
| 170 | - case self::CIPHER_RIJNDAEL_256: |
|
| 171 | - $this->cipher = new Cipher_Rijndael_256($key); |
|
| 172 | - break; |
|
| 170 | + case self::CIPHER_RIJNDAEL_256: |
|
| 171 | + $this->cipher = new Cipher_Rijndael_256($key); |
|
| 172 | + break; |
|
| 173 | 173 | |
| 174 | - case self::CIPHER_SIMPLEXOR: |
|
| 175 | - $this->cipher = new Cipher_Simple_XOR($key); |
|
| 176 | - break; |
|
| 174 | + case self::CIPHER_SIMPLEXOR: |
|
| 175 | + $this->cipher = new Cipher_Simple_XOR($key); |
|
| 176 | + break; |
|
| 177 | 177 | |
| 178 | - case self::CIPHER_SKIPJACK: |
|
| 179 | - $this->cipher = new Cipher_Skipjack($key); |
|
| 180 | - break; |
|
| 178 | + case self::CIPHER_SKIPJACK: |
|
| 179 | + $this->cipher = new Cipher_Skipjack($key); |
|
| 180 | + break; |
|
| 181 | 181 | |
| 182 | - case self::CIPHER_VIGENERE: |
|
| 183 | - $this->cipher = new Cipher_Vigenere($key); |
|
| 184 | - break; |
|
| 182 | + case self::CIPHER_VIGENERE: |
|
| 183 | + $this->cipher = new Cipher_Vigenere($key); |
|
| 184 | + break; |
|
| 185 | 185 | |
| 186 | - default: |
|
| 187 | - trigger_error("$cipher is not a valid cipher", E_USER_WARNING); |
|
| 188 | - } |
|
| 186 | + default: |
|
| 187 | + trigger_error("$cipher is not a valid cipher", E_USER_WARNING); |
|
| 188 | + } |
|
| 189 | 189 | |
| 190 | 190 | |
| 191 | - /* |
|
| 191 | + /* |
|
| 192 | 192 | * MODES |
| 193 | 193 | */ |
| 194 | - switch($mode) |
|
| 195 | - { |
|
| 196 | - case self::MODE_CBC: |
|
| 197 | - $this->mode = new Mode_CBC($this->cipher); |
|
| 198 | - break; |
|
| 199 | - |
|
| 200 | - case self::MODE_CFB: |
|
| 201 | - $this->mode = new Mode_CFB($this->cipher); |
|
| 202 | - break; |
|
| 203 | - |
|
| 204 | - case self::MODE_CTR: |
|
| 205 | - $this->mode = new Mode_CTR($this->cipher); |
|
| 206 | - break; |
|
| 207 | - |
|
| 208 | - case self::MODE_ECB: |
|
| 209 | - $this->mode = new Mode_ECB($this->cipher); |
|
| 210 | - break; |
|
| 211 | - |
|
| 212 | - case self::MODE_NCFB: |
|
| 213 | - $this->mode = new Mode_NCFB($this->cipher); |
|
| 214 | - break; |
|
| 215 | - |
|
| 216 | - case self::MODE_NOFB: |
|
| 217 | - $this->mode = new Mode_NOFB($this->cipher); |
|
| 218 | - break; |
|
| 219 | - |
|
| 220 | - case self::MODE_OFB: |
|
| 221 | - $this->mode = new Mode_OFB($this->cipher); |
|
| 222 | - break; |
|
| 223 | - |
|
| 224 | - case self::MODE_PCBC: |
|
| 225 | - $this->mode = new Mode_PCBC($this->cipher); |
|
| 226 | - break; |
|
| 227 | - |
|
| 228 | - case self::MODE_RAW: |
|
| 229 | - $this->mode = new Mode_RAW($this->cipher); |
|
| 230 | - break; |
|
| 231 | - |
|
| 232 | - case self::MODE_STREAM: |
|
| 233 | - $this->mode = new Mode_Stream($this->cipher); |
|
| 234 | - break; |
|
| 235 | - |
|
| 236 | - default: |
|
| 237 | - trigger_error("$mode is not a valid mode", E_USER_WARNING); |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - // set the default padding |
|
| 241 | - $this->padding($padding); |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - |
|
| 245 | - /** |
|
| 246 | - * Destructor |
|
| 247 | - * |
|
| 248 | - * @return void |
|
| 249 | - */ |
|
| 250 | - public function __destruct() |
|
| 251 | - { |
|
| 252 | - |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - |
|
| 256 | - /** |
|
| 257 | - * Encrypt a plain text message using the Mode and Cipher selected. |
|
| 258 | - * Some stream modes require this function to be called in a loop |
|
| 259 | - * which requires the use of $result parameter to retrieve |
|
| 260 | - * the decrypted data. |
|
| 261 | - * |
|
| 262 | - * @param string $text The plain text string |
|
| 263 | - * @return string The encrypted string |
|
| 264 | - */ |
|
| 265 | - public function encrypt($text) |
|
| 266 | - { |
|
| 267 | - // check that an iv is set, if required by the mode |
|
| 268 | - $this->mode->checkIV(); |
|
| 269 | - |
|
| 270 | - // the encryption is done inside the mode |
|
| 271 | - $this->mode->encrypt($text); |
|
| 272 | - return $text; |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - |
|
| 276 | - /** |
|
| 277 | - * Decrypt an encrypted message using the Mode and Cipher selected. |
|
| 278 | - * Some stream modes require this function to be called in a loop |
|
| 279 | - * which requires the use of $result parameter to retrieve |
|
| 280 | - * the decrypted data. |
|
| 281 | - * |
|
| 282 | - * @param string $text The encrypted string |
|
| 283 | - * @return string The decrypted string |
|
| 284 | - */ |
|
| 285 | - public function decrypt($text) |
|
| 286 | - { |
|
| 287 | - // check that an iv is set, if required by the mode |
|
| 288 | - $this->mode->checkIV(); |
|
| 289 | - |
|
| 290 | - // the decryption is done inside the mode |
|
| 291 | - $this->mode->decrypt($text); |
|
| 292 | - return $text; |
|
| 293 | - } |
|
| 294 | - |
|
| 295 | - |
|
| 296 | - /** |
|
| 297 | - * Return the cipher object being used |
|
| 298 | - * |
|
| 299 | - * @return object The Cipher object |
|
| 300 | - */ |
|
| 301 | - public function cipher() |
|
| 302 | - { |
|
| 303 | - return $this->cipher; |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - |
|
| 307 | - /** |
|
| 308 | - * Return the mode object being used |
|
| 309 | - * |
|
| 310 | - * @return object The Mode object |
|
| 311 | - */ |
|
| 312 | - public function mode() |
|
| 313 | - { |
|
| 314 | - return $this->mode; |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - |
|
| 318 | - /** |
|
| 319 | - * Returns the name of the cipher being used |
|
| 320 | - * |
|
| 321 | - * @return string The name of the cipher currently in use, |
|
| 322 | - * it will be one of the predefined phpCrypt cipher constants |
|
| 323 | - */ |
|
| 324 | - public function cipherName() |
|
| 325 | - { |
|
| 326 | - return $this->cipher->name(); |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - |
|
| 330 | - /** |
|
| 331 | - * Return the name of the mode being used |
|
| 332 | - * |
|
| 333 | - * @return string The name of the mode in use, it will |
|
| 334 | - * be one of the predefined phpCrypt mode constants |
|
| 335 | - */ |
|
| 336 | - public function modeName() |
|
| 337 | - { |
|
| 338 | - return $this->mode->name(); |
|
| 339 | - } |
|
| 340 | - |
|
| 341 | - |
|
| 342 | - /** |
|
| 343 | - * Returns Ciphers required block size in bytes |
|
| 344 | - * |
|
| 345 | - * @return integer The cipher data block size, in bytes |
|
| 346 | - */ |
|
| 347 | - public function cipherBlockSize() |
|
| 348 | - { |
|
| 349 | - return $this->cipher->blockSize(); |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - |
|
| 353 | - /** |
|
| 354 | - * Returns the cipher's required key size, in bytes |
|
| 355 | - * |
|
| 356 | - * @return integer The cipher's key size requirement, in bytes |
|
| 357 | - */ |
|
| 358 | - public function cipherKeySize() |
|
| 359 | - { |
|
| 360 | - return $this->cipher->keySize(); |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - |
|
| 364 | - /** |
|
| 365 | - * Sets and/or returns the key to be used. Normally you set |
|
| 366 | - * the key in the phpCrypt constructor. This can be usefully |
|
| 367 | - * if you need to change the key on the fly and don't want |
|
| 368 | - * to create a new instance of phpCrypt. |
|
| 369 | - * |
|
| 370 | - * If the $key parameter is not given, this function will simply |
|
| 371 | - * return the key currently in use. |
|
| 372 | - * |
|
| 373 | - * @param string $key Optional, The key to set |
|
| 374 | - * @return string The key being used |
|
| 375 | - */ |
|
| 376 | - public function cipherKey($key = "") |
|
| 377 | - { |
|
| 378 | - return $this->cipher->key($key); |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - |
|
| 382 | - /** |
|
| 383 | - * A helper function which will create a random key. Calls |
|
| 384 | - * Core::randBytes(). By default it will use PHP_Crypt::RAND for |
|
| 385 | - * the random source of bytes, and return a PHP_Crypt::RAND_DEFAULT_SZ |
|
| 386 | - * byte string. There are 4 ways to create a random byte string by |
|
| 387 | - * setting the $src parameter: |
|
| 388 | - * PHP_Crypt::RAND - Default, uses mt_rand() |
|
| 389 | - * PHP_Crypt::RAND_DEV_RAND - Unix only, uses /dev/random |
|
| 390 | - * PHP_Crypt::RAND_DEV_URAND - Unix only, uses /dev/urandom |
|
| 391 | - * PHP_Crypt::RAND_WIN_COM - Windows only, uses Microsoft's CAPICOM SDK |
|
| 392 | - * |
|
| 393 | - * @param string $src Optional, The source to use to create random bytes |
|
| 394 | - * @param integer $len Optional, The number of random bytes to return |
|
| 395 | - * @return string A random string of bytes |
|
| 396 | - */ |
|
| 397 | - public static function createKey($src = self::RAND, $len = self::RAND_DEFAULT_SZ) |
|
| 398 | - { |
|
| 399 | - return Core::randBytes($src, $len); |
|
| 400 | - } |
|
| 401 | - |
|
| 402 | - |
|
| 403 | - /** |
|
| 404 | - * Sets the IV to use. Note that you do not need to call |
|
| 405 | - * this function if creating an IV using createIV(). This |
|
| 406 | - * function is used when an IV has already been created |
|
| 407 | - * outside of phpCrypt and needs to be set. Alternatively |
|
| 408 | - * you can just pass the $iv parameter to the encrypt() |
|
| 409 | - * or decrypt() functions |
|
| 410 | - * |
|
| 411 | - * When the $iv parameter is not given, the function will |
|
| 412 | - * return the current IV being used. See createIV() if you |
|
| 413 | - * need to create an IV. |
|
| 414 | - * |
|
| 415 | - * @param string $iv Optional, The IV to use during Encryption/Decryption |
|
| 416 | - * @return void |
|
| 417 | - */ |
|
| 418 | - public function IV($iv = "") |
|
| 419 | - { |
|
| 420 | - return $this->mode->IV($iv); |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - |
|
| 424 | - /** |
|
| 425 | - * Creates an IV for the the Cipher selected, if one is required. |
|
| 426 | - * If you already have an IV to use, this function does not need |
|
| 427 | - * to be called, instead set it with setIV(). If you create an |
|
| 428 | - * IV with createIV(), you do not need to set it with setIV(), |
|
| 429 | - * as it is automatically set in this function |
|
| 430 | - * |
|
| 431 | - * $src values are: |
|
| 432 | - * PHP_Crypt::RAND - Default, uses mt_rand() |
|
| 433 | - * PHP_Crypt::RAND_DEV_RAND - Unix only, uses /dev/random |
|
| 434 | - * PHP_Crypt::RAND_DEV_URAND - Unix only, uses /dev/urandom |
|
| 435 | - * PHP_Crypt::RAND_WIN_COM - Windows only, uses Microsoft's CAPICOM SDK |
|
| 436 | - * |
|
| 437 | - * @param string $src Optional, how the IV is generated |
|
| 438 | - * @return string The IV that was created, and set for the mode |
|
| 439 | - */ |
|
| 440 | - public function createIV($src = self::RAND) |
|
| 441 | - { |
|
| 442 | - return $this->mode->createIV($src); |
|
| 443 | - } |
|
| 444 | - |
|
| 445 | - |
|
| 446 | - /** |
|
| 447 | - * Sets the type of padding to be used within the specified Mode |
|
| 448 | - * |
|
| 449 | - * @param string $type One of the predefined padding types |
|
| 450 | - * @return void |
|
| 451 | - */ |
|
| 452 | - public function padding($type = "") |
|
| 453 | - { |
|
| 454 | - return $this->mode->padding($type); |
|
| 455 | - } |
|
| 194 | + switch($mode) |
|
| 195 | + { |
|
| 196 | + case self::MODE_CBC: |
|
| 197 | + $this->mode = new Mode_CBC($this->cipher); |
|
| 198 | + break; |
|
| 199 | + |
|
| 200 | + case self::MODE_CFB: |
|
| 201 | + $this->mode = new Mode_CFB($this->cipher); |
|
| 202 | + break; |
|
| 203 | + |
|
| 204 | + case self::MODE_CTR: |
|
| 205 | + $this->mode = new Mode_CTR($this->cipher); |
|
| 206 | + break; |
|
| 207 | + |
|
| 208 | + case self::MODE_ECB: |
|
| 209 | + $this->mode = new Mode_ECB($this->cipher); |
|
| 210 | + break; |
|
| 211 | + |
|
| 212 | + case self::MODE_NCFB: |
|
| 213 | + $this->mode = new Mode_NCFB($this->cipher); |
|
| 214 | + break; |
|
| 215 | + |
|
| 216 | + case self::MODE_NOFB: |
|
| 217 | + $this->mode = new Mode_NOFB($this->cipher); |
|
| 218 | + break; |
|
| 219 | + |
|
| 220 | + case self::MODE_OFB: |
|
| 221 | + $this->mode = new Mode_OFB($this->cipher); |
|
| 222 | + break; |
|
| 223 | + |
|
| 224 | + case self::MODE_PCBC: |
|
| 225 | + $this->mode = new Mode_PCBC($this->cipher); |
|
| 226 | + break; |
|
| 227 | + |
|
| 228 | + case self::MODE_RAW: |
|
| 229 | + $this->mode = new Mode_RAW($this->cipher); |
|
| 230 | + break; |
|
| 231 | + |
|
| 232 | + case self::MODE_STREAM: |
|
| 233 | + $this->mode = new Mode_Stream($this->cipher); |
|
| 234 | + break; |
|
| 235 | + |
|
| 236 | + default: |
|
| 237 | + trigger_error("$mode is not a valid mode", E_USER_WARNING); |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + // set the default padding |
|
| 241 | + $this->padding($padding); |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + |
|
| 245 | + /** |
|
| 246 | + * Destructor |
|
| 247 | + * |
|
| 248 | + * @return void |
|
| 249 | + */ |
|
| 250 | + public function __destruct() |
|
| 251 | + { |
|
| 252 | + |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + |
|
| 256 | + /** |
|
| 257 | + * Encrypt a plain text message using the Mode and Cipher selected. |
|
| 258 | + * Some stream modes require this function to be called in a loop |
|
| 259 | + * which requires the use of $result parameter to retrieve |
|
| 260 | + * the decrypted data. |
|
| 261 | + * |
|
| 262 | + * @param string $text The plain text string |
|
| 263 | + * @return string The encrypted string |
|
| 264 | + */ |
|
| 265 | + public function encrypt($text) |
|
| 266 | + { |
|
| 267 | + // check that an iv is set, if required by the mode |
|
| 268 | + $this->mode->checkIV(); |
|
| 269 | + |
|
| 270 | + // the encryption is done inside the mode |
|
| 271 | + $this->mode->encrypt($text); |
|
| 272 | + return $text; |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + |
|
| 276 | + /** |
|
| 277 | + * Decrypt an encrypted message using the Mode and Cipher selected. |
|
| 278 | + * Some stream modes require this function to be called in a loop |
|
| 279 | + * which requires the use of $result parameter to retrieve |
|
| 280 | + * the decrypted data. |
|
| 281 | + * |
|
| 282 | + * @param string $text The encrypted string |
|
| 283 | + * @return string The decrypted string |
|
| 284 | + */ |
|
| 285 | + public function decrypt($text) |
|
| 286 | + { |
|
| 287 | + // check that an iv is set, if required by the mode |
|
| 288 | + $this->mode->checkIV(); |
|
| 289 | + |
|
| 290 | + // the decryption is done inside the mode |
|
| 291 | + $this->mode->decrypt($text); |
|
| 292 | + return $text; |
|
| 293 | + } |
|
| 294 | + |
|
| 295 | + |
|
| 296 | + /** |
|
| 297 | + * Return the cipher object being used |
|
| 298 | + * |
|
| 299 | + * @return object The Cipher object |
|
| 300 | + */ |
|
| 301 | + public function cipher() |
|
| 302 | + { |
|
| 303 | + return $this->cipher; |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + |
|
| 307 | + /** |
|
| 308 | + * Return the mode object being used |
|
| 309 | + * |
|
| 310 | + * @return object The Mode object |
|
| 311 | + */ |
|
| 312 | + public function mode() |
|
| 313 | + { |
|
| 314 | + return $this->mode; |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + |
|
| 318 | + /** |
|
| 319 | + * Returns the name of the cipher being used |
|
| 320 | + * |
|
| 321 | + * @return string The name of the cipher currently in use, |
|
| 322 | + * it will be one of the predefined phpCrypt cipher constants |
|
| 323 | + */ |
|
| 324 | + public function cipherName() |
|
| 325 | + { |
|
| 326 | + return $this->cipher->name(); |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + |
|
| 330 | + /** |
|
| 331 | + * Return the name of the mode being used |
|
| 332 | + * |
|
| 333 | + * @return string The name of the mode in use, it will |
|
| 334 | + * be one of the predefined phpCrypt mode constants |
|
| 335 | + */ |
|
| 336 | + public function modeName() |
|
| 337 | + { |
|
| 338 | + return $this->mode->name(); |
|
| 339 | + } |
|
| 340 | + |
|
| 341 | + |
|
| 342 | + /** |
|
| 343 | + * Returns Ciphers required block size in bytes |
|
| 344 | + * |
|
| 345 | + * @return integer The cipher data block size, in bytes |
|
| 346 | + */ |
|
| 347 | + public function cipherBlockSize() |
|
| 348 | + { |
|
| 349 | + return $this->cipher->blockSize(); |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + |
|
| 353 | + /** |
|
| 354 | + * Returns the cipher's required key size, in bytes |
|
| 355 | + * |
|
| 356 | + * @return integer The cipher's key size requirement, in bytes |
|
| 357 | + */ |
|
| 358 | + public function cipherKeySize() |
|
| 359 | + { |
|
| 360 | + return $this->cipher->keySize(); |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + |
|
| 364 | + /** |
|
| 365 | + * Sets and/or returns the key to be used. Normally you set |
|
| 366 | + * the key in the phpCrypt constructor. This can be usefully |
|
| 367 | + * if you need to change the key on the fly and don't want |
|
| 368 | + * to create a new instance of phpCrypt. |
|
| 369 | + * |
|
| 370 | + * If the $key parameter is not given, this function will simply |
|
| 371 | + * return the key currently in use. |
|
| 372 | + * |
|
| 373 | + * @param string $key Optional, The key to set |
|
| 374 | + * @return string The key being used |
|
| 375 | + */ |
|
| 376 | + public function cipherKey($key = "") |
|
| 377 | + { |
|
| 378 | + return $this->cipher->key($key); |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + |
|
| 382 | + /** |
|
| 383 | + * A helper function which will create a random key. Calls |
|
| 384 | + * Core::randBytes(). By default it will use PHP_Crypt::RAND for |
|
| 385 | + * the random source of bytes, and return a PHP_Crypt::RAND_DEFAULT_SZ |
|
| 386 | + * byte string. There are 4 ways to create a random byte string by |
|
| 387 | + * setting the $src parameter: |
|
| 388 | + * PHP_Crypt::RAND - Default, uses mt_rand() |
|
| 389 | + * PHP_Crypt::RAND_DEV_RAND - Unix only, uses /dev/random |
|
| 390 | + * PHP_Crypt::RAND_DEV_URAND - Unix only, uses /dev/urandom |
|
| 391 | + * PHP_Crypt::RAND_WIN_COM - Windows only, uses Microsoft's CAPICOM SDK |
|
| 392 | + * |
|
| 393 | + * @param string $src Optional, The source to use to create random bytes |
|
| 394 | + * @param integer $len Optional, The number of random bytes to return |
|
| 395 | + * @return string A random string of bytes |
|
| 396 | + */ |
|
| 397 | + public static function createKey($src = self::RAND, $len = self::RAND_DEFAULT_SZ) |
|
| 398 | + { |
|
| 399 | + return Core::randBytes($src, $len); |
|
| 400 | + } |
|
| 401 | + |
|
| 402 | + |
|
| 403 | + /** |
|
| 404 | + * Sets the IV to use. Note that you do not need to call |
|
| 405 | + * this function if creating an IV using createIV(). This |
|
| 406 | + * function is used when an IV has already been created |
|
| 407 | + * outside of phpCrypt and needs to be set. Alternatively |
|
| 408 | + * you can just pass the $iv parameter to the encrypt() |
|
| 409 | + * or decrypt() functions |
|
| 410 | + * |
|
| 411 | + * When the $iv parameter is not given, the function will |
|
| 412 | + * return the current IV being used. See createIV() if you |
|
| 413 | + * need to create an IV. |
|
| 414 | + * |
|
| 415 | + * @param string $iv Optional, The IV to use during Encryption/Decryption |
|
| 416 | + * @return void |
|
| 417 | + */ |
|
| 418 | + public function IV($iv = "") |
|
| 419 | + { |
|
| 420 | + return $this->mode->IV($iv); |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + |
|
| 424 | + /** |
|
| 425 | + * Creates an IV for the the Cipher selected, if one is required. |
|
| 426 | + * If you already have an IV to use, this function does not need |
|
| 427 | + * to be called, instead set it with setIV(). If you create an |
|
| 428 | + * IV with createIV(), you do not need to set it with setIV(), |
|
| 429 | + * as it is automatically set in this function |
|
| 430 | + * |
|
| 431 | + * $src values are: |
|
| 432 | + * PHP_Crypt::RAND - Default, uses mt_rand() |
|
| 433 | + * PHP_Crypt::RAND_DEV_RAND - Unix only, uses /dev/random |
|
| 434 | + * PHP_Crypt::RAND_DEV_URAND - Unix only, uses /dev/urandom |
|
| 435 | + * PHP_Crypt::RAND_WIN_COM - Windows only, uses Microsoft's CAPICOM SDK |
|
| 436 | + * |
|
| 437 | + * @param string $src Optional, how the IV is generated |
|
| 438 | + * @return string The IV that was created, and set for the mode |
|
| 439 | + */ |
|
| 440 | + public function createIV($src = self::RAND) |
|
| 441 | + { |
|
| 442 | + return $this->mode->createIV($src); |
|
| 443 | + } |
|
| 444 | + |
|
| 445 | + |
|
| 446 | + /** |
|
| 447 | + * Sets the type of padding to be used within the specified Mode |
|
| 448 | + * |
|
| 449 | + * @param string $type One of the predefined padding types |
|
| 450 | + * @return void |
|
| 451 | + */ |
|
| 452 | + public function padding($type = "") |
|
| 453 | + { |
|
| 454 | + return $this->mode->padding($type); |
|
| 455 | + } |
|
| 456 | 456 | } |
| 457 | 457 | ?> |
@@ -36,299 +36,299 @@ |
||
| 36 | 36 | */ |
| 37 | 37 | abstract class Mode |
| 38 | 38 | { |
| 39 | - /** |
|
| 40 | - * @type object $cipher The cipher object used within the mode |
|
| 41 | - */ |
|
| 42 | - protected $cipher = null; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * @type string $iv The IV used for the mode, not all Modes |
|
| 46 | - * use an IV so this may be empty |
|
| 47 | - */ |
|
| 48 | - protected $iv = ""; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * @type string $register For modes that use a register to do |
|
| 52 | - * encryption/decryption. This stores the unencrypted register. |
|
| 53 | - */ |
|
| 54 | - protected $register = ""; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * @type string $enc_register For modes that use a register to do |
|
| 58 | - * encryption/decryption. This stores the encrypted register |
|
| 59 | - */ |
|
| 60 | - protected $enc_register = ""; |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * @type integer $block_size The byte size of the block to |
|
| 64 | - * encrypt/decrypt for the Mode |
|
| 65 | - */ |
|
| 66 | - private $block_size = 0; |
|
| 67 | - |
|
| 68 | - /** @type string $mode_name The name of mode currently used */ |
|
| 69 | - private $mode_name = ""; |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * @type string $padding The type of padding to use when required. |
|
| 73 | - * Padding types are defined in phpCrypt class. Defaults to |
|
| 74 | - * PHP_Crypt::PAD_ZERO |
|
| 75 | - */ |
|
| 76 | - private $padding = PHP_Crypt::PAD_ZERO; |
|
| 77 | - |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * Constructor |
|
| 81 | - * Sets the cipher object that will be used for encryption |
|
| 82 | - * |
|
| 83 | - * @param object $cipher One of the phpCrypt encryption cipher objects |
|
| 84 | - * @param string $mode_name The name of phpCrypt's modes, as defined in the mode |
|
| 85 | - * @return void |
|
| 86 | - */ |
|
| 87 | - protected function __construct($mode_name, $cipher) |
|
| 88 | - { |
|
| 89 | - $this->name($mode_name); |
|
| 90 | - $this->cipher($cipher); |
|
| 91 | - $this->block_size = $this->cipher->blockSize(); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * Destructor |
|
| 97 | - * |
|
| 98 | - * @return void |
|
| 99 | - */ |
|
| 100 | - protected function __destruct() |
|
| 101 | - { |
|
| 102 | - |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - |
|
| 106 | - |
|
| 107 | - /********************************************************************** |
|
| 39 | + /** |
|
| 40 | + * @type object $cipher The cipher object used within the mode |
|
| 41 | + */ |
|
| 42 | + protected $cipher = null; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * @type string $iv The IV used for the mode, not all Modes |
|
| 46 | + * use an IV so this may be empty |
|
| 47 | + */ |
|
| 48 | + protected $iv = ""; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * @type string $register For modes that use a register to do |
|
| 52 | + * encryption/decryption. This stores the unencrypted register. |
|
| 53 | + */ |
|
| 54 | + protected $register = ""; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * @type string $enc_register For modes that use a register to do |
|
| 58 | + * encryption/decryption. This stores the encrypted register |
|
| 59 | + */ |
|
| 60 | + protected $enc_register = ""; |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * @type integer $block_size The byte size of the block to |
|
| 64 | + * encrypt/decrypt for the Mode |
|
| 65 | + */ |
|
| 66 | + private $block_size = 0; |
|
| 67 | + |
|
| 68 | + /** @type string $mode_name The name of mode currently used */ |
|
| 69 | + private $mode_name = ""; |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * @type string $padding The type of padding to use when required. |
|
| 73 | + * Padding types are defined in phpCrypt class. Defaults to |
|
| 74 | + * PHP_Crypt::PAD_ZERO |
|
| 75 | + */ |
|
| 76 | + private $padding = PHP_Crypt::PAD_ZERO; |
|
| 77 | + |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * Constructor |
|
| 81 | + * Sets the cipher object that will be used for encryption |
|
| 82 | + * |
|
| 83 | + * @param object $cipher One of the phpCrypt encryption cipher objects |
|
| 84 | + * @param string $mode_name The name of phpCrypt's modes, as defined in the mode |
|
| 85 | + * @return void |
|
| 86 | + */ |
|
| 87 | + protected function __construct($mode_name, $cipher) |
|
| 88 | + { |
|
| 89 | + $this->name($mode_name); |
|
| 90 | + $this->cipher($cipher); |
|
| 91 | + $this->block_size = $this->cipher->blockSize(); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * Destructor |
|
| 97 | + * |
|
| 98 | + * @return void |
|
| 99 | + */ |
|
| 100 | + protected function __destruct() |
|
| 101 | + { |
|
| 102 | + |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + |
|
| 106 | + |
|
| 107 | + /********************************************************************** |
|
| 108 | 108 | * ABSTRACT METHODS |
| 109 | 109 | * |
| 110 | 110 | * The abstract methods required by inheriting classes to implement |
| 111 | 111 | **********************************************************************/ |
| 112 | 112 | |
| 113 | - /** |
|
| 114 | - * All modes must have encrypt(), which implements |
|
| 115 | - * the mode using the cipher's encrypiton algorithm |
|
| 116 | - * |
|
| 117 | - * @param string $text A String to encrypt |
|
| 118 | - * @return boolean Always returns true |
|
| 119 | - */ |
|
| 120 | - abstract public function encrypt(&$text); |
|
| 113 | + /** |
|
| 114 | + * All modes must have encrypt(), which implements |
|
| 115 | + * the mode using the cipher's encrypiton algorithm |
|
| 116 | + * |
|
| 117 | + * @param string $text A String to encrypt |
|
| 118 | + * @return boolean Always returns true |
|
| 119 | + */ |
|
| 120 | + abstract public function encrypt(&$text); |
|
| 121 | 121 | |
| 122 | 122 | |
| 123 | - /** |
|
| 124 | - * All modes must have decrypt(), which implements |
|
| 125 | - * the mode using the cipher's encryption algorithm |
|
| 126 | - * |
|
| 127 | - * @param string $text A String to decrypt |
|
| 128 | - * @return boolean Always returns true |
|
| 129 | - */ |
|
| 130 | - abstract public function decrypt(&$text); |
|
| 123 | + /** |
|
| 124 | + * All modes must have decrypt(), which implements |
|
| 125 | + * the mode using the cipher's encryption algorithm |
|
| 126 | + * |
|
| 127 | + * @param string $text A String to decrypt |
|
| 128 | + * @return boolean Always returns true |
|
| 129 | + */ |
|
| 130 | + abstract public function decrypt(&$text); |
|
| 131 | 131 | |
| 132 | 132 | |
| 133 | - /** |
|
| 134 | - * Indicates whether or not a mode requires an IV |
|
| 135 | - * |
|
| 136 | - * @return boolean Always returns true or false |
|
| 137 | - */ |
|
| 138 | - abstract public function requiresIV(); |
|
| 133 | + /** |
|
| 134 | + * Indicates whether or not a mode requires an IV |
|
| 135 | + * |
|
| 136 | + * @return boolean Always returns true or false |
|
| 137 | + */ |
|
| 138 | + abstract public function requiresIV(); |
|
| 139 | 139 | |
| 140 | 140 | |
| 141 | 141 | |
| 142 | 142 | |
| 143 | - /********************************************************************** |
|
| 143 | + /********************************************************************** |
|
| 144 | 144 | * PUBLIC METHODS |
| 145 | 145 | * |
| 146 | 146 | **********************************************************************/ |
| 147 | 147 | |
| 148 | - /** |
|
| 149 | - * Create an IV if the Mode used requires an IV. |
|
| 150 | - * The IV should be saved and used for Encryption/Decryption |
|
| 151 | - * of the same blocks of data. |
|
| 152 | - * There are 3 ways to auto generate an IV by setting $src parameter |
|
| 153 | - * PHP_Crypt::RAND - Default, uses mt_rand() |
|
| 154 | - * PHP_Crypt::RAND_DEV_RAND - Unix only, uses /dev/random |
|
| 155 | - * PHP_Crypt::RAND_DEV_URAND - Unix only, uses /dev/urandom |
|
| 156 | - * PHP_Crypt::RAND_WIN_COM - Windows only, uses Microsoft's CAPICOM SDK |
|
| 157 | - * |
|
| 158 | - * @param string $src Optional, Sets how the IV is generated, must be |
|
| 159 | - * one of the predefined PHP_Crypt RAND constants. Defaults to |
|
| 160 | - * PHP_Crypt::RAND if none is given. |
|
| 161 | - * @return string The IV that is being used by the mode |
|
| 162 | - */ |
|
| 163 | - public function createIV($src = PHP_Crypt::RAND) |
|
| 164 | - { |
|
| 165 | - // if the mode does not use an IV, lets not waste time |
|
| 166 | - if(!$this->requiresIV()) |
|
| 167 | - return false; |
|
| 168 | - |
|
| 169 | - $iv = Core::randBytes($src, $this->block_size); |
|
| 170 | - return $this->IV($iv); |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * Sets or Returns an IV for the mode to use. If the $iv parameter |
|
| 176 | - * is not given, this function only returns the current IV in use. |
|
| 177 | - * |
|
| 178 | - * @param string $iv Optional, An IV to use for the mode and cipher selected |
|
| 179 | - * @return string The current IV being used |
|
| 180 | - */ |
|
| 181 | - public function IV($iv = null) |
|
| 182 | - { |
|
| 183 | - if($iv != null) |
|
| 184 | - { |
|
| 185 | - // check that the iv is the correct length, |
|
| 186 | - $len = strlen($iv); |
|
| 187 | - if($len != $this->block_size) |
|
| 188 | - { |
|
| 189 | - $msg = "Incorrect IV size. Supplied length: $len bytes, Required: {$this->block_size} bytes"; |
|
| 190 | - trigger_error($msg, E_USER_WARNING); |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - $this->clearRegisters(); |
|
| 194 | - $this->register = $iv; |
|
| 195 | - $this->iv = $iv; |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - return $this->iv; |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - |
|
| 202 | - /** |
|
| 203 | - * Checks to see if the current mode requires an IV and that it is set |
|
| 204 | - * if it is required. Triggers E_USER_WARNING an IV is required and not set |
|
| 205 | - * |
|
| 206 | - * @return void |
|
| 207 | - */ |
|
| 208 | - public function checkIV() |
|
| 209 | - { |
|
| 210 | - if($this->requiresIV() && strlen($this->register) == 0) |
|
| 211 | - { |
|
| 212 | - $msg = strtoupper($this->mode_name)." mode requires an IV or the IV is empty"; |
|
| 213 | - trigger_error($msg, E_USER_WARNING); |
|
| 214 | - } |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - |
|
| 218 | - /** |
|
| 219 | - * Sets and returns the name of the mode being used |
|
| 220 | - * If $name parameter is set, sets the mode. If |
|
| 221 | - * $name is not set, returns the current mode in use |
|
| 222 | - * |
|
| 223 | - * @param string $name Optional, One of the predefined |
|
| 224 | - * phpCrypt mode constant names |
|
| 225 | - * @return string One of the predefined phpCrypt mode |
|
| 226 | - * constant mode names |
|
| 227 | - */ |
|
| 228 | - public function name($name = "") |
|
| 229 | - { |
|
| 230 | - if($name != "") |
|
| 231 | - $this->mode_name = $name; |
|
| 232 | - |
|
| 233 | - return $this->mode_name; |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - |
|
| 237 | - /** |
|
| 238 | - * Sets or Returns the padding type used with the mode |
|
| 239 | - * If the $type parameter is not given, this function |
|
| 240 | - * returns the the padding type only. |
|
| 241 | - * |
|
| 242 | - * @param string $type One of the predefined padding types |
|
| 243 | - * @return void |
|
| 244 | - */ |
|
| 245 | - public function padding($type = "") |
|
| 246 | - { |
|
| 247 | - if($type != "") |
|
| 248 | - $this->padding = $type; |
|
| 249 | - |
|
| 250 | - return $this->padding; |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - |
|
| 254 | - /** |
|
| 255 | - * Returns or Sets the cipher object being used |
|
| 256 | - * If the $cipher parameter is set with a cipher object, |
|
| 257 | - * the cipher current cipher will be set to this cipher. |
|
| 258 | - * If the $cipher parameter is not set, returns the |
|
| 259 | - * current cipher object being used. |
|
| 260 | - * |
|
| 261 | - * @param object $cipher Optional, An object of type Cipher |
|
| 262 | - * @return object An object of type Cipher |
|
| 263 | - */ |
|
| 264 | - public function cipher($cipher = null) |
|
| 265 | - { |
|
| 266 | - if(is_object($cipher)) |
|
| 267 | - $this->cipher = $cipher; |
|
| 268 | - |
|
| 269 | - return $this->cipher; |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - |
|
| 273 | - |
|
| 274 | - |
|
| 275 | - /********************************************************************** |
|
| 148 | + /** |
|
| 149 | + * Create an IV if the Mode used requires an IV. |
|
| 150 | + * The IV should be saved and used for Encryption/Decryption |
|
| 151 | + * of the same blocks of data. |
|
| 152 | + * There are 3 ways to auto generate an IV by setting $src parameter |
|
| 153 | + * PHP_Crypt::RAND - Default, uses mt_rand() |
|
| 154 | + * PHP_Crypt::RAND_DEV_RAND - Unix only, uses /dev/random |
|
| 155 | + * PHP_Crypt::RAND_DEV_URAND - Unix only, uses /dev/urandom |
|
| 156 | + * PHP_Crypt::RAND_WIN_COM - Windows only, uses Microsoft's CAPICOM SDK |
|
| 157 | + * |
|
| 158 | + * @param string $src Optional, Sets how the IV is generated, must be |
|
| 159 | + * one of the predefined PHP_Crypt RAND constants. Defaults to |
|
| 160 | + * PHP_Crypt::RAND if none is given. |
|
| 161 | + * @return string The IV that is being used by the mode |
|
| 162 | + */ |
|
| 163 | + public function createIV($src = PHP_Crypt::RAND) |
|
| 164 | + { |
|
| 165 | + // if the mode does not use an IV, lets not waste time |
|
| 166 | + if(!$this->requiresIV()) |
|
| 167 | + return false; |
|
| 168 | + |
|
| 169 | + $iv = Core::randBytes($src, $this->block_size); |
|
| 170 | + return $this->IV($iv); |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * Sets or Returns an IV for the mode to use. If the $iv parameter |
|
| 176 | + * is not given, this function only returns the current IV in use. |
|
| 177 | + * |
|
| 178 | + * @param string $iv Optional, An IV to use for the mode and cipher selected |
|
| 179 | + * @return string The current IV being used |
|
| 180 | + */ |
|
| 181 | + public function IV($iv = null) |
|
| 182 | + { |
|
| 183 | + if($iv != null) |
|
| 184 | + { |
|
| 185 | + // check that the iv is the correct length, |
|
| 186 | + $len = strlen($iv); |
|
| 187 | + if($len != $this->block_size) |
|
| 188 | + { |
|
| 189 | + $msg = "Incorrect IV size. Supplied length: $len bytes, Required: {$this->block_size} bytes"; |
|
| 190 | + trigger_error($msg, E_USER_WARNING); |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + $this->clearRegisters(); |
|
| 194 | + $this->register = $iv; |
|
| 195 | + $this->iv = $iv; |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + return $this->iv; |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + |
|
| 202 | + /** |
|
| 203 | + * Checks to see if the current mode requires an IV and that it is set |
|
| 204 | + * if it is required. Triggers E_USER_WARNING an IV is required and not set |
|
| 205 | + * |
|
| 206 | + * @return void |
|
| 207 | + */ |
|
| 208 | + public function checkIV() |
|
| 209 | + { |
|
| 210 | + if($this->requiresIV() && strlen($this->register) == 0) |
|
| 211 | + { |
|
| 212 | + $msg = strtoupper($this->mode_name)." mode requires an IV or the IV is empty"; |
|
| 213 | + trigger_error($msg, E_USER_WARNING); |
|
| 214 | + } |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + |
|
| 218 | + /** |
|
| 219 | + * Sets and returns the name of the mode being used |
|
| 220 | + * If $name parameter is set, sets the mode. If |
|
| 221 | + * $name is not set, returns the current mode in use |
|
| 222 | + * |
|
| 223 | + * @param string $name Optional, One of the predefined |
|
| 224 | + * phpCrypt mode constant names |
|
| 225 | + * @return string One of the predefined phpCrypt mode |
|
| 226 | + * constant mode names |
|
| 227 | + */ |
|
| 228 | + public function name($name = "") |
|
| 229 | + { |
|
| 230 | + if($name != "") |
|
| 231 | + $this->mode_name = $name; |
|
| 232 | + |
|
| 233 | + return $this->mode_name; |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + |
|
| 237 | + /** |
|
| 238 | + * Sets or Returns the padding type used with the mode |
|
| 239 | + * If the $type parameter is not given, this function |
|
| 240 | + * returns the the padding type only. |
|
| 241 | + * |
|
| 242 | + * @param string $type One of the predefined padding types |
|
| 243 | + * @return void |
|
| 244 | + */ |
|
| 245 | + public function padding($type = "") |
|
| 246 | + { |
|
| 247 | + if($type != "") |
|
| 248 | + $this->padding = $type; |
|
| 249 | + |
|
| 250 | + return $this->padding; |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + |
|
| 254 | + /** |
|
| 255 | + * Returns or Sets the cipher object being used |
|
| 256 | + * If the $cipher parameter is set with a cipher object, |
|
| 257 | + * the cipher current cipher will be set to this cipher. |
|
| 258 | + * If the $cipher parameter is not set, returns the |
|
| 259 | + * current cipher object being used. |
|
| 260 | + * |
|
| 261 | + * @param object $cipher Optional, An object of type Cipher |
|
| 262 | + * @return object An object of type Cipher |
|
| 263 | + */ |
|
| 264 | + public function cipher($cipher = null) |
|
| 265 | + { |
|
| 266 | + if(is_object($cipher)) |
|
| 267 | + $this->cipher = $cipher; |
|
| 268 | + |
|
| 269 | + return $this->cipher; |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + |
|
| 273 | + |
|
| 274 | + |
|
| 275 | + /********************************************************************** |
|
| 276 | 276 | * PROTECTED METHODS |
| 277 | 277 | * |
| 278 | 278 | **********************************************************************/ |
| 279 | 279 | |
| 280 | - /** |
|
| 281 | - * Pads str so that final block is $block_bits in size, if the final block |
|
| 282 | - * is $block_bits, then an additional block is added that is $block_bits in size |
|
| 283 | - * The padding should be set by phpCrypt::setPadding() |
|
| 284 | - * |
|
| 285 | - * @param string $str the string to be padded |
|
| 286 | - * @return boolean Returns true |
|
| 287 | - */ |
|
| 288 | - protected function pad(&$str) |
|
| 289 | - { |
|
| 290 | - $len = strlen($str); |
|
| 291 | - $bytes = $this->cipher->blockSize(); // returns bytes |
|
| 292 | - |
|
| 293 | - // now determine the next multiple of blockSize(), then find |
|
| 294 | - // the difference between that and the length of $str, |
|
| 295 | - // this is how many padding bytes we will need |
|
| 296 | - $num = ceil($len / $bytes) * $bytes; |
|
| 297 | - $num = $num - $len; |
|
| 298 | - |
|
| 299 | - Padding::pad($str, $num, $this->padding); |
|
| 300 | - return true; |
|
| 301 | - } |
|
| 302 | - |
|
| 303 | - |
|
| 304 | - /** |
|
| 305 | - * Strip out the padded blocks created from Pad(). |
|
| 306 | - * Padding type should be set by phpCrypt::setPadding() |
|
| 307 | - * |
|
| 308 | - * @param string $str the string to strip padding from |
|
| 309 | - * @return boolean Returns True |
|
| 310 | - */ |
|
| 311 | - protected function strip(&$str) |
|
| 312 | - { |
|
| 313 | - Padding::strip($str, $this->padding); |
|
| 314 | - return true; |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - |
|
| 318 | - /********************************************************************** |
|
| 280 | + /** |
|
| 281 | + * Pads str so that final block is $block_bits in size, if the final block |
|
| 282 | + * is $block_bits, then an additional block is added that is $block_bits in size |
|
| 283 | + * The padding should be set by phpCrypt::setPadding() |
|
| 284 | + * |
|
| 285 | + * @param string $str the string to be padded |
|
| 286 | + * @return boolean Returns true |
|
| 287 | + */ |
|
| 288 | + protected function pad(&$str) |
|
| 289 | + { |
|
| 290 | + $len = strlen($str); |
|
| 291 | + $bytes = $this->cipher->blockSize(); // returns bytes |
|
| 292 | + |
|
| 293 | + // now determine the next multiple of blockSize(), then find |
|
| 294 | + // the difference between that and the length of $str, |
|
| 295 | + // this is how many padding bytes we will need |
|
| 296 | + $num = ceil($len / $bytes) * $bytes; |
|
| 297 | + $num = $num - $len; |
|
| 298 | + |
|
| 299 | + Padding::pad($str, $num, $this->padding); |
|
| 300 | + return true; |
|
| 301 | + } |
|
| 302 | + |
|
| 303 | + |
|
| 304 | + /** |
|
| 305 | + * Strip out the padded blocks created from Pad(). |
|
| 306 | + * Padding type should be set by phpCrypt::setPadding() |
|
| 307 | + * |
|
| 308 | + * @param string $str the string to strip padding from |
|
| 309 | + * @return boolean Returns True |
|
| 310 | + */ |
|
| 311 | + protected function strip(&$str) |
|
| 312 | + { |
|
| 313 | + Padding::strip($str, $this->padding); |
|
| 314 | + return true; |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + |
|
| 318 | + /********************************************************************** |
|
| 319 | 319 | * PRIVATE METHODS |
| 320 | 320 | * |
| 321 | 321 | **********************************************************************/ |
| 322 | 322 | |
| 323 | - /** |
|
| 324 | - * Clears the registers used for some modes |
|
| 325 | - * |
|
| 326 | - * @return void |
|
| 327 | - */ |
|
| 328 | - private function clearRegisters() |
|
| 329 | - { |
|
| 330 | - $this->register = ""; |
|
| 331 | - $this->enc_register = ""; |
|
| 332 | - } |
|
| 323 | + /** |
|
| 324 | + * Clears the registers used for some modes |
|
| 325 | + * |
|
| 326 | + * @return void |
|
| 327 | + */ |
|
| 328 | + private function clearRegisters() |
|
| 329 | + { |
|
| 330 | + $this->register = ""; |
|
| 331 | + $this->enc_register = ""; |
|
| 332 | + } |
|
| 333 | 333 | } |
| 334 | 334 | ?> |
@@ -43,209 +43,209 @@ |
||
| 43 | 43 | */ |
| 44 | 44 | class Cipher_3DES extends Cipher_DES |
| 45 | 45 | { |
| 46 | - /** @type integer BYTES_BLOCK The size of the block, in bytes */ |
|
| 47 | - const BYTES_BLOCK = 8; // 64 bits |
|
| 46 | + /** @type integer BYTES_BLOCK The size of the block, in bytes */ |
|
| 47 | + const BYTES_BLOCK = 8; // 64 bits |
|
| 48 | 48 | |
| 49 | - /** @type integer BYTES_KEY The size of the key. The key can be |
|
| 49 | + /** @type integer BYTES_KEY The size of the key. The key can be |
|
| 50 | 50 | 8, 16, or 24 bytes but gets expanded to 24 bytes before used */ |
| 51 | - const BYTES_KEY = 24; |
|
| 52 | - |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * Constructor |
|
| 56 | - * |
|
| 57 | - * @param string $key The key used for Encryption/Decryption |
|
| 58 | - * @return void |
|
| 59 | - */ |
|
| 60 | - public function __construct($key) |
|
| 61 | - { |
|
| 62 | - $key_len = strlen($key); |
|
| 63 | - |
|
| 64 | - if($key_len == 8 || $key_len == 16) |
|
| 65 | - $key = self::expandKey($key, $key_len); |
|
| 66 | - else if($key_len < self::BYTES_KEY) |
|
| 67 | - { |
|
| 68 | - $msg = PHP_Crypt::CIPHER_3DES." requires an 8, 16, or 24 byte key. "; |
|
| 69 | - $msg .= "$key_len bytes received."; |
|
| 70 | - trigger_error($msg, E_USER_WARNING); |
|
| 71 | - } |
|
| 72 | - // else if the key is longer than 24 bytes, phpCrypt will shorten it |
|
| 73 | - |
|
| 74 | - // set the 3DES key, note that we call __construct1() not __construct() |
|
| 75 | - // this is a second contructor we created for classes that extend the |
|
| 76 | - // DES class |
|
| 77 | - parent::__construct1(PHP_Crypt::CIPHER_3DES, $key, self::BYTES_KEY); |
|
| 78 | - |
|
| 79 | - // 3DES requires that data is 64 bits |
|
| 80 | - $this->blockSize(self::BYTES_BLOCK); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Destructor |
|
| 86 | - * |
|
| 87 | - * @return void |
|
| 88 | - */ |
|
| 89 | - public function __destruct() |
|
| 90 | - { |
|
| 91 | - parent::__destruct(); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * Encrypt plain text data using TripleDES |
|
| 97 | - * For encryption 3DES takes a 192 bit key, splits |
|
| 98 | - * it into 3 64 bit parts, and then does the following |
|
| 99 | - * DES ENCRYPT(key1) -> DES DECRYPT(key2) -> DES ENCRYPT(key3) |
|
| 100 | - * |
|
| 101 | - * @param string $data A plain text string |
|
| 102 | - * @return boolean Returns true |
|
| 103 | - */ |
|
| 104 | - public function encrypt(&$text) |
|
| 105 | - { |
|
| 106 | - $blocksz = $this->blockSize(); |
|
| 107 | - |
|
| 108 | - for($i = 0; $i < 3; ++$i) |
|
| 109 | - { |
|
| 110 | - $key = substr($this->key(), ($i * 8), $blocksz); |
|
| 111 | - $this->keyPermutation($key); |
|
| 112 | - |
|
| 113 | - if($i % 2) // round 1 |
|
| 114 | - $this->operation(parent::DECRYPT); |
|
| 115 | - else // rounds 0 and 2 |
|
| 116 | - $this->operation(parent::ENCRYPT); |
|
| 117 | - |
|
| 118 | - $this->des($text); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - return true; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Decrypt a TripleDES encrypted string |
|
| 127 | - * For decryption 3DES takes a 192 bit key, splits |
|
| 128 | - * it into 3 64 bit parts, and then does the following |
|
| 129 | - * DES DECRYPT(key1) -> DES ENCRYPT(key2) -> DES DECRYPT(key3) |
|
| 130 | - * |
|
| 131 | - * @param string $encrypted A DES encrypted string |
|
| 132 | - * @return boolean Returns true |
|
| 133 | - */ |
|
| 134 | - public function decrypt(&$text) |
|
| 135 | - { |
|
| 136 | - $blocksz = $this->blockSize(); |
|
| 137 | - |
|
| 138 | - for($i = 2; $i >= 0; --$i) |
|
| 139 | - { |
|
| 140 | - $key = substr($this->key(), ($i * 8), $blocksz); |
|
| 141 | - $this->keyPermutation($key); |
|
| 142 | - |
|
| 143 | - if($i % 2) // round 1 |
|
| 144 | - $this->operation(parent::ENCRYPT); |
|
| 145 | - else // round 0 and 2 |
|
| 146 | - $this->operation(parent::DECRYPT); |
|
| 147 | - |
|
| 148 | - $this->des($text); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - return true; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * We overload the DES::keyPermutation() function so it takes |
|
| 157 | - * a 64 bit key as a parameter. This is because 3DES uses a 192 bit |
|
| 158 | - * key which is split into 3 64 bit parts, each of which must be |
|
| 159 | - * passed through the keyPermutation() function |
|
| 160 | - * Key permutation, based on tables $_pc1 and $_pc2 |
|
| 161 | - * Create 16 subkeys, each of which is 48-bits long. |
|
| 162 | - * NOTE: The $key parameter is required, however because PHP expects |
|
| 163 | - * an overloaded method to contain the same number of parameters |
|
| 164 | - * (when E_STRICT is set) as the parent class, we make it optional |
|
| 165 | - * to shut the 'error' up |
|
| 166 | - * |
|
| 167 | - * @param string $key A 64 bit key |
|
| 168 | - * @return void |
|
| 169 | - */ |
|
| 170 | - private function keyPermutation($key = "") |
|
| 171 | - { |
|
| 172 | - $this->sub_keys = array(); |
|
| 173 | - $pc1m = array(); |
|
| 174 | - $pcr = array(); |
|
| 175 | - $c = array(); |
|
| 176 | - $d = array(); |
|
| 177 | - |
|
| 178 | - // convert the key to binary |
|
| 179 | - $binkey = parent::str2Bin($key); |
|
| 180 | - |
|
| 181 | - // reduce the key down to 56bits based on table $_pc1 |
|
| 182 | - for($i = 0; $i < 56; ++$i) |
|
| 183 | - { |
|
| 184 | - $pos = parent::$_pc1[$i] - 1; |
|
| 51 | + const BYTES_KEY = 24; |
|
| 52 | + |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * Constructor |
|
| 56 | + * |
|
| 57 | + * @param string $key The key used for Encryption/Decryption |
|
| 58 | + * @return void |
|
| 59 | + */ |
|
| 60 | + public function __construct($key) |
|
| 61 | + { |
|
| 62 | + $key_len = strlen($key); |
|
| 63 | + |
|
| 64 | + if($key_len == 8 || $key_len == 16) |
|
| 65 | + $key = self::expandKey($key, $key_len); |
|
| 66 | + else if($key_len < self::BYTES_KEY) |
|
| 67 | + { |
|
| 68 | + $msg = PHP_Crypt::CIPHER_3DES." requires an 8, 16, or 24 byte key. "; |
|
| 69 | + $msg .= "$key_len bytes received."; |
|
| 70 | + trigger_error($msg, E_USER_WARNING); |
|
| 71 | + } |
|
| 72 | + // else if the key is longer than 24 bytes, phpCrypt will shorten it |
|
| 73 | + |
|
| 74 | + // set the 3DES key, note that we call __construct1() not __construct() |
|
| 75 | + // this is a second contructor we created for classes that extend the |
|
| 76 | + // DES class |
|
| 77 | + parent::__construct1(PHP_Crypt::CIPHER_3DES, $key, self::BYTES_KEY); |
|
| 78 | + |
|
| 79 | + // 3DES requires that data is 64 bits |
|
| 80 | + $this->blockSize(self::BYTES_BLOCK); |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Destructor |
|
| 86 | + * |
|
| 87 | + * @return void |
|
| 88 | + */ |
|
| 89 | + public function __destruct() |
|
| 90 | + { |
|
| 91 | + parent::__destruct(); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * Encrypt plain text data using TripleDES |
|
| 97 | + * For encryption 3DES takes a 192 bit key, splits |
|
| 98 | + * it into 3 64 bit parts, and then does the following |
|
| 99 | + * DES ENCRYPT(key1) -> DES DECRYPT(key2) -> DES ENCRYPT(key3) |
|
| 100 | + * |
|
| 101 | + * @param string $data A plain text string |
|
| 102 | + * @return boolean Returns true |
|
| 103 | + */ |
|
| 104 | + public function encrypt(&$text) |
|
| 105 | + { |
|
| 106 | + $blocksz = $this->blockSize(); |
|
| 107 | + |
|
| 108 | + for($i = 0; $i < 3; ++$i) |
|
| 109 | + { |
|
| 110 | + $key = substr($this->key(), ($i * 8), $blocksz); |
|
| 111 | + $this->keyPermutation($key); |
|
| 112 | + |
|
| 113 | + if($i % 2) // round 1 |
|
| 114 | + $this->operation(parent::DECRYPT); |
|
| 115 | + else // rounds 0 and 2 |
|
| 116 | + $this->operation(parent::ENCRYPT); |
|
| 117 | + |
|
| 118 | + $this->des($text); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + return true; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Decrypt a TripleDES encrypted string |
|
| 127 | + * For decryption 3DES takes a 192 bit key, splits |
|
| 128 | + * it into 3 64 bit parts, and then does the following |
|
| 129 | + * DES DECRYPT(key1) -> DES ENCRYPT(key2) -> DES DECRYPT(key3) |
|
| 130 | + * |
|
| 131 | + * @param string $encrypted A DES encrypted string |
|
| 132 | + * @return boolean Returns true |
|
| 133 | + */ |
|
| 134 | + public function decrypt(&$text) |
|
| 135 | + { |
|
| 136 | + $blocksz = $this->blockSize(); |
|
| 137 | + |
|
| 138 | + for($i = 2; $i >= 0; --$i) |
|
| 139 | + { |
|
| 140 | + $key = substr($this->key(), ($i * 8), $blocksz); |
|
| 141 | + $this->keyPermutation($key); |
|
| 142 | + |
|
| 143 | + if($i % 2) // round 1 |
|
| 144 | + $this->operation(parent::ENCRYPT); |
|
| 145 | + else // round 0 and 2 |
|
| 146 | + $this->operation(parent::DECRYPT); |
|
| 147 | + |
|
| 148 | + $this->des($text); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + return true; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * We overload the DES::keyPermutation() function so it takes |
|
| 157 | + * a 64 bit key as a parameter. This is because 3DES uses a 192 bit |
|
| 158 | + * key which is split into 3 64 bit parts, each of which must be |
|
| 159 | + * passed through the keyPermutation() function |
|
| 160 | + * Key permutation, based on tables $_pc1 and $_pc2 |
|
| 161 | + * Create 16 subkeys, each of which is 48-bits long. |
|
| 162 | + * NOTE: The $key parameter is required, however because PHP expects |
|
| 163 | + * an overloaded method to contain the same number of parameters |
|
| 164 | + * (when E_STRICT is set) as the parent class, we make it optional |
|
| 165 | + * to shut the 'error' up |
|
| 166 | + * |
|
| 167 | + * @param string $key A 64 bit key |
|
| 168 | + * @return void |
|
| 169 | + */ |
|
| 170 | + private function keyPermutation($key = "") |
|
| 171 | + { |
|
| 172 | + $this->sub_keys = array(); |
|
| 173 | + $pc1m = array(); |
|
| 174 | + $pcr = array(); |
|
| 175 | + $c = array(); |
|
| 176 | + $d = array(); |
|
| 177 | + |
|
| 178 | + // convert the key to binary |
|
| 179 | + $binkey = parent::str2Bin($key); |
|
| 180 | + |
|
| 181 | + // reduce the key down to 56bits based on table $_pc1 |
|
| 182 | + for($i = 0; $i < 56; ++$i) |
|
| 183 | + { |
|
| 184 | + $pos = parent::$_pc1[$i] - 1; |
|
| 185 | 185 | $pc1m[$i] = $binkey[$pos]; |
| 186 | - } |
|
| 187 | - |
|
| 188 | - // split $pc1m in half (C0 and D0) |
|
| 189 | - $c[0] = array_slice($pc1m, 0, 28); |
|
| 190 | - $d[0] = array_slice($pc1m, 28, 28); |
|
| 191 | - |
|
| 192 | - // now that $c[0] and $d[0] are defined, create 16 blocks for Cn and Dn |
|
| 193 | - // where 1 <= n <= 16 |
|
| 194 | - for($i = 1; $i <= 16; ++$i) |
|
| 195 | - { |
|
| 196 | - // now set the next Cn and Dn as the previous Cn and Dn |
|
| 197 | - $c[$i] = $c[$i-1]; |
|
| 198 | - $d[$i] = $d[$i-1]; |
|
| 199 | - |
|
| 200 | - for($j = 0; $j < parent::$_key_sched[$i-1]; ++$j) |
|
| 201 | - { |
|
| 202 | - // do a left shift, move each bit one place to the left, |
|
| 203 | - // except for the first bit, which is cycled to the end |
|
| 204 | - // of the block. |
|
| 205 | - $c[$i][] = array_shift($c[$i]); |
|
| 206 | - $d[$i][] = array_shift($d[$i]); |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - // We now form the sub_keys (Kn), for 1<=n<=16, by applying the |
|
| 210 | - // following permutation table to each of the concatenated |
|
| 211 | - // pairs CnDn. Each pair has 56 bits, but PC-2 only uses 48 |
|
| 212 | - // of these. |
|
| 213 | - $CnDn = array_merge($c[$i], $d[$i]); |
|
| 214 | - $this->sub_keys[$i-1] = ""; |
|
| 215 | - for($j = 0; $j < 48; ++$j) |
|
| 216 | - $this->sub_keys[$i-1] .= $CnDn[parent::$_pc2[$j] - 1]; |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - // the sub_keys are created, we are done with the key permutation |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - |
|
| 223 | - /** |
|
| 224 | - * Triple DES accepts an 8, 16, or 24 bytes key. If the key is |
|
| 225 | - * 8 or 16 bytes, it is expanded to 24 bytes |
|
| 226 | - * An 8 byte key is expanded by repeating it 3 times to make a 24 |
|
| 227 | - * byte key |
|
| 228 | - * A 16 byte key add the first 8 bytes to the end of the string |
|
| 229 | - * to make a 24 byte key |
|
| 230 | - * |
|
| 231 | - * @param string $key The 8 or 16 byte key to expand |
|
| 232 | - * @return string If the key given is 8 or 16 bytes it returns the |
|
| 233 | - * expanded 24 byte key, else it returns the original key unexpanded |
|
| 234 | - */ |
|
| 235 | - private static function expandKey($key, $len) |
|
| 236 | - { |
|
| 237 | - // if we were given an 8 byte key, repeat it |
|
| 238 | - // 3 times to produce a 24 byte key |
|
| 239 | - if($len == 8) |
|
| 240 | - $key = str_repeat($key, 3); |
|
| 241 | - |
|
| 242 | - // if we were given a 16 byte key, add the first |
|
| 243 | - // 8 bytes to the end of the key to produce 24 bytes |
|
| 244 | - if($len == 16) |
|
| 245 | - $key .= substr($key, 0, 8); |
|
| 246 | - |
|
| 247 | - // return the key |
|
| 248 | - return $key; |
|
| 249 | - } |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + // split $pc1m in half (C0 and D0) |
|
| 189 | + $c[0] = array_slice($pc1m, 0, 28); |
|
| 190 | + $d[0] = array_slice($pc1m, 28, 28); |
|
| 191 | + |
|
| 192 | + // now that $c[0] and $d[0] are defined, create 16 blocks for Cn and Dn |
|
| 193 | + // where 1 <= n <= 16 |
|
| 194 | + for($i = 1; $i <= 16; ++$i) |
|
| 195 | + { |
|
| 196 | + // now set the next Cn and Dn as the previous Cn and Dn |
|
| 197 | + $c[$i] = $c[$i-1]; |
|
| 198 | + $d[$i] = $d[$i-1]; |
|
| 199 | + |
|
| 200 | + for($j = 0; $j < parent::$_key_sched[$i-1]; ++$j) |
|
| 201 | + { |
|
| 202 | + // do a left shift, move each bit one place to the left, |
|
| 203 | + // except for the first bit, which is cycled to the end |
|
| 204 | + // of the block. |
|
| 205 | + $c[$i][] = array_shift($c[$i]); |
|
| 206 | + $d[$i][] = array_shift($d[$i]); |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + // We now form the sub_keys (Kn), for 1<=n<=16, by applying the |
|
| 210 | + // following permutation table to each of the concatenated |
|
| 211 | + // pairs CnDn. Each pair has 56 bits, but PC-2 only uses 48 |
|
| 212 | + // of these. |
|
| 213 | + $CnDn = array_merge($c[$i], $d[$i]); |
|
| 214 | + $this->sub_keys[$i-1] = ""; |
|
| 215 | + for($j = 0; $j < 48; ++$j) |
|
| 216 | + $this->sub_keys[$i-1] .= $CnDn[parent::$_pc2[$j] - 1]; |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + // the sub_keys are created, we are done with the key permutation |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + |
|
| 223 | + /** |
|
| 224 | + * Triple DES accepts an 8, 16, or 24 bytes key. If the key is |
|
| 225 | + * 8 or 16 bytes, it is expanded to 24 bytes |
|
| 226 | + * An 8 byte key is expanded by repeating it 3 times to make a 24 |
|
| 227 | + * byte key |
|
| 228 | + * A 16 byte key add the first 8 bytes to the end of the string |
|
| 229 | + * to make a 24 byte key |
|
| 230 | + * |
|
| 231 | + * @param string $key The 8 or 16 byte key to expand |
|
| 232 | + * @return string If the key given is 8 or 16 bytes it returns the |
|
| 233 | + * expanded 24 byte key, else it returns the original key unexpanded |
|
| 234 | + */ |
|
| 235 | + private static function expandKey($key, $len) |
|
| 236 | + { |
|
| 237 | + // if we were given an 8 byte key, repeat it |
|
| 238 | + // 3 times to produce a 24 byte key |
|
| 239 | + if($len == 8) |
|
| 240 | + $key = str_repeat($key, 3); |
|
| 241 | + |
|
| 242 | + // if we were given a 16 byte key, add the first |
|
| 243 | + // 8 bytes to the end of the key to produce 24 bytes |
|
| 244 | + if($len == 16) |
|
| 245 | + $key .= substr($key, 0, 8); |
|
| 246 | + |
|
| 247 | + // return the key |
|
| 248 | + return $key; |
|
| 249 | + } |
|
| 250 | 250 | } |
| 251 | 251 | ?> |
@@ -38,35 +38,35 @@ |
||
| 38 | 38 | */ |
| 39 | 39 | class Cipher_AES_256 extends Cipher_Rijndael_128 |
| 40 | 40 | { |
| 41 | - /** @type integer BYTES_BLOCK The size of the block, in bytes */ |
|
| 42 | - const BYTES_BLOCK = 16; // 128 bits; |
|
| 41 | + /** @type integer BYTES_BLOCK The size of the block, in bytes */ |
|
| 42 | + const BYTES_BLOCK = 16; // 128 bits; |
|
| 43 | 43 | |
| 44 | - /** @type integer BITS_KEY The size of the key, in bytes */ |
|
| 45 | - const BYTES_KEY = 32; // 256 bits; |
|
| 44 | + /** @type integer BITS_KEY The size of the key, in bytes */ |
|
| 45 | + const BYTES_KEY = 32; // 256 bits; |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Constructor |
|
| 49 | - * Sets the key used for encryption. |
|
| 50 | - * |
|
| 51 | - * @param string $key string containing the user supplied encryption key |
|
| 52 | - * @return void |
|
| 53 | - */ |
|
| 54 | - public function __construct($key) |
|
| 55 | - { |
|
| 56 | - // Setup AES by calling the second constructor in Rijndael_128 |
|
| 57 | - // The block size is set here too, since all AES implementations use 128 bit blocks |
|
| 58 | - parent::__construct1(PHP_Crypt::CIPHER_AES_256, $key, self::BYTES_KEY); |
|
| 59 | - } |
|
| 47 | + /** |
|
| 48 | + * Constructor |
|
| 49 | + * Sets the key used for encryption. |
|
| 50 | + * |
|
| 51 | + * @param string $key string containing the user supplied encryption key |
|
| 52 | + * @return void |
|
| 53 | + */ |
|
| 54 | + public function __construct($key) |
|
| 55 | + { |
|
| 56 | + // Setup AES by calling the second constructor in Rijndael_128 |
|
| 57 | + // The block size is set here too, since all AES implementations use 128 bit blocks |
|
| 58 | + parent::__construct1(PHP_Crypt::CIPHER_AES_256, $key, self::BYTES_KEY); |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * Destructor |
|
| 64 | - * |
|
| 65 | - * @return void |
|
| 66 | - */ |
|
| 67 | - public function __destruct() |
|
| 68 | - { |
|
| 69 | - parent::__destruct(); |
|
| 70 | - } |
|
| 62 | + /** |
|
| 63 | + * Destructor |
|
| 64 | + * |
|
| 65 | + * @return void |
|
| 66 | + */ |
|
| 67 | + public function __destruct() |
|
| 68 | + { |
|
| 69 | + parent::__destruct(); |
|
| 70 | + } |
|
| 71 | 71 | } |
| 72 | 72 | ?> |
@@ -45,101 +45,101 @@ |
||
| 45 | 45 | */ |
| 46 | 46 | class Cipher_Simple_XOR extends Cipher |
| 47 | 47 | { |
| 48 | - /** @type integer BYTES_BLOCK The size of the block, in bytes */ |
|
| 49 | - const BYTES_BLOCK = 1; // 8 bits |
|
| 50 | - |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Constructor |
|
| 54 | - * Sets the key used for encryption. Also sets the requied block size |
|
| 55 | - * |
|
| 56 | - * @param string $key string containing the user supplied encryption key |
|
| 57 | - * @return void |
|
| 58 | - */ |
|
| 59 | - public function __construct($key) |
|
| 60 | - { |
|
| 61 | - // SimpleXOR does not have a key size requirement |
|
| 62 | - parent::__construct(PHP_Crypt::CIPHER_SIMPLEXOR, $key); |
|
| 63 | - |
|
| 64 | - // required block size in bits |
|
| 65 | - $this->blockSize(self::BYTES_BLOCK); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Destructor |
|
| 71 | - * |
|
| 72 | - * @return void |
|
| 73 | - */ |
|
| 74 | - public function __destruct() |
|
| 75 | - { |
|
| 76 | - parent::__destruct(); |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * Encrypts data using an XOR encryption scheme |
|
| 82 | - * |
|
| 83 | - * @param string $text A string to encrypt |
|
| 84 | - * @return boolean Always returns true |
|
| 85 | - */ |
|
| 86 | - public function encrypt(&$text) |
|
| 87 | - { |
|
| 88 | - $this->operation(parent::ENCRYPT); |
|
| 89 | - return $this->simpleXOR($text); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Decrypts data encrypted with SimpleXOR::Encrypt() |
|
| 95 | - * |
|
| 96 | - * @param string $text An encrypted string to decrypt |
|
| 97 | - * @return boolean Always returns true |
|
| 98 | - */ |
|
| 99 | - public function decrypt(&$text) |
|
| 100 | - { |
|
| 101 | - $this->operation(parent::DECRYPT); |
|
| 102 | - return $this->simpleXOR($text); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * Because XOR Encryption uses the same algorithm to encrypt and decrypt, |
|
| 108 | - * this function contains the code to do both. The SimpleXOR::Encrypt() |
|
| 109 | - * and SimpleXOR::Decrypt() function above just call this function |
|
| 110 | - * |
|
| 111 | - * @param string $input |
|
| 112 | - * @return boolean Always returns true |
|
| 113 | - */ |
|
| 114 | - private function simpleXOR(&$text) |
|
| 115 | - { |
|
| 116 | - $pos = 0; |
|
| 117 | - $max = strlen($text); |
|
| 118 | - $key = $this->key(); |
|
| 119 | - |
|
| 120 | - for($i = 0; $i < $max; ++$i) |
|
| 121 | - { |
|
| 122 | - // if the current position in the key reaches the end of the key, |
|
| 123 | - // start over at position 0 of the key |
|
| 124 | - if($pos >= $this->keySize()) |
|
| 125 | - $pos = 0; |
|
| 126 | - |
|
| 127 | - $text[$i] = $text[$i] ^ $key[$pos]; |
|
| 128 | - ++$pos; |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - return true; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * Indicates that this is stream cipher |
|
| 137 | - * |
|
| 138 | - * @return integer Returns Cipher::STREAM |
|
| 139 | - */ |
|
| 140 | - public function type() |
|
| 141 | - { |
|
| 142 | - return parent::STREAM; |
|
| 143 | - } |
|
| 48 | + /** @type integer BYTES_BLOCK The size of the block, in bytes */ |
|
| 49 | + const BYTES_BLOCK = 1; // 8 bits |
|
| 50 | + |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Constructor |
|
| 54 | + * Sets the key used for encryption. Also sets the requied block size |
|
| 55 | + * |
|
| 56 | + * @param string $key string containing the user supplied encryption key |
|
| 57 | + * @return void |
|
| 58 | + */ |
|
| 59 | + public function __construct($key) |
|
| 60 | + { |
|
| 61 | + // SimpleXOR does not have a key size requirement |
|
| 62 | + parent::__construct(PHP_Crypt::CIPHER_SIMPLEXOR, $key); |
|
| 63 | + |
|
| 64 | + // required block size in bits |
|
| 65 | + $this->blockSize(self::BYTES_BLOCK); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Destructor |
|
| 71 | + * |
|
| 72 | + * @return void |
|
| 73 | + */ |
|
| 74 | + public function __destruct() |
|
| 75 | + { |
|
| 76 | + parent::__destruct(); |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * Encrypts data using an XOR encryption scheme |
|
| 82 | + * |
|
| 83 | + * @param string $text A string to encrypt |
|
| 84 | + * @return boolean Always returns true |
|
| 85 | + */ |
|
| 86 | + public function encrypt(&$text) |
|
| 87 | + { |
|
| 88 | + $this->operation(parent::ENCRYPT); |
|
| 89 | + return $this->simpleXOR($text); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Decrypts data encrypted with SimpleXOR::Encrypt() |
|
| 95 | + * |
|
| 96 | + * @param string $text An encrypted string to decrypt |
|
| 97 | + * @return boolean Always returns true |
|
| 98 | + */ |
|
| 99 | + public function decrypt(&$text) |
|
| 100 | + { |
|
| 101 | + $this->operation(parent::DECRYPT); |
|
| 102 | + return $this->simpleXOR($text); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * Because XOR Encryption uses the same algorithm to encrypt and decrypt, |
|
| 108 | + * this function contains the code to do both. The SimpleXOR::Encrypt() |
|
| 109 | + * and SimpleXOR::Decrypt() function above just call this function |
|
| 110 | + * |
|
| 111 | + * @param string $input |
|
| 112 | + * @return boolean Always returns true |
|
| 113 | + */ |
|
| 114 | + private function simpleXOR(&$text) |
|
| 115 | + { |
|
| 116 | + $pos = 0; |
|
| 117 | + $max = strlen($text); |
|
| 118 | + $key = $this->key(); |
|
| 119 | + |
|
| 120 | + for($i = 0; $i < $max; ++$i) |
|
| 121 | + { |
|
| 122 | + // if the current position in the key reaches the end of the key, |
|
| 123 | + // start over at position 0 of the key |
|
| 124 | + if($pos >= $this->keySize()) |
|
| 125 | + $pos = 0; |
|
| 126 | + |
|
| 127 | + $text[$i] = $text[$i] ^ $key[$pos]; |
|
| 128 | + ++$pos; |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + return true; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * Indicates that this is stream cipher |
|
| 137 | + * |
|
| 138 | + * @return integer Returns Cipher::STREAM |
|
| 139 | + */ |
|
| 140 | + public function type() |
|
| 141 | + { |
|
| 142 | + return parent::STREAM; |
|
| 143 | + } |
|
| 144 | 144 | } |
| 145 | 145 | ?> |
@@ -46,219 +46,219 @@ |
||
| 46 | 46 | */ |
| 47 | 47 | class Cipher_Enigma extends Cipher |
| 48 | 48 | { |
| 49 | - const MASK = 0377; |
|
| 50 | - |
|
| 51 | - /** @type integer ROTORSZ The size of $t1,$t2,$t3,$deck */ |
|
| 52 | - const ROTORSZ = 256; |
|
| 53 | - |
|
| 54 | - /** @type array $t1 An array of chars, ROTORSZ size */ |
|
| 55 | - private $t1 = array(); |
|
| 56 | - |
|
| 57 | - /** @type array $t2 An array of chars, ROTORSZ size */ |
|
| 58 | - private $t2 = array(); |
|
| 59 | - |
|
| 60 | - /** @type array $t3 An array of chars, ROTORSZ size */ |
|
| 61 | - private $t3 = array(); |
|
| 62 | - |
|
| 63 | - /** @type array $deck An array of chars, ROTORSZ size */ |
|
| 64 | - private $deck = array(); |
|
| 65 | - |
|
| 66 | - private $n1 = 0; |
|
| 67 | - private $n2 = 0; |
|
| 68 | - private $nr1 = 0; |
|
| 69 | - private $nr2 = 0; |
|
| 70 | - |
|
| 71 | - /** @type string $xkey Stores the modified key */ |
|
| 72 | - private $xkey = ""; |
|
| 73 | - |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * Constructor |
|
| 77 | - * |
|
| 78 | - * @param string $key The key used for Encryption/Decryption |
|
| 79 | - * @return void |
|
| 80 | - */ |
|
| 81 | - public function __construct($key) |
|
| 82 | - { |
|
| 83 | - parent::__construct(PHP_Crypt::CIPHER_ENIGMA, $key, strlen($key)); |
|
| 84 | - |
|
| 85 | - $this->createKey(); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Destructor |
|
| 91 | - * |
|
| 92 | - * @return void |
|
| 93 | - */ |
|
| 94 | - public function __destruct() |
|
| 95 | - { |
|
| 96 | - parent::__destruct(); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * Encrypt plain text data using Enigma |
|
| 102 | - * |
|
| 103 | - * @param string $str A plain text string of any length |
|
| 104 | - * @return boolean Returns true |
|
| 105 | - */ |
|
| 106 | - public function encrypt(&$str) |
|
| 107 | - { |
|
| 108 | - $this->operation(parent::ENCRYPT); |
|
| 109 | - return $this->enigma($str); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * Decrypt an Enigma encrypted string |
|
| 115 | - * |
|
| 116 | - * @param string $str A Enigma encrypted string |
|
| 117 | - * @return boolean Returns true |
|
| 118 | - */ |
|
| 119 | - public function decrypt(&$str) |
|
| 120 | - { |
|
| 121 | - $this->operation(parent::DECRYPT); |
|
| 122 | - return $this->enigma($str); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * Peforms the actual Enigma encryption/decryption. Since |
|
| 128 | - * the algorithm is the same for both, we do it all in |
|
| 129 | - * this one function |
|
| 130 | - * |
|
| 131 | - * @param string $str A string to encrypt or decrypt |
|
| 132 | - * @return boolean Returns true |
|
| 133 | - */ |
|
| 134 | - private function enigma(&$str) |
|
| 135 | - { |
|
| 136 | - // zero out the counters |
|
| 137 | - $this->n1 = 0; |
|
| 138 | - $this->n2 = 0; |
|
| 139 | - $this->nr1 = 0; |
|
| 140 | - $this->nr2 = 0; |
|
| 141 | - |
|
| 142 | - $max = strlen($str); |
|
| 143 | - for($j = 0; $j < $max; ++$j) |
|
| 144 | - { |
|
| 145 | - $i = ord($str[$j]); |
|
| 146 | - $this->nr1 = $this->n1; |
|
| 147 | - |
|
| 148 | - $pos1 = ($i + $this->nr1) & self::MASK; |
|
| 149 | - $pos3 = ($this->t1[$pos1] + $this->nr2) & self::MASK; |
|
| 150 | - $pos2 = ($this->t3[$pos3] - $this->nr2) & self::MASK; |
|
| 151 | - $i = $this->t2[$pos2] - $this->nr1; |
|
| 152 | - |
|
| 153 | - $str[$j] = chr($i); |
|
| 154 | - $this->n1++; |
|
| 155 | - |
|
| 156 | - if($this->n1 == self::ROTORSZ) |
|
| 157 | - { |
|
| 158 | - $this->n1 = 0; |
|
| 159 | - $this->n2++; |
|
| 160 | - |
|
| 161 | - if($this->n2 == self::ROTORSZ) |
|
| 162 | - $this->n2 = 0; |
|
| 163 | - |
|
| 164 | - $this->nr2 = $this->n2; |
|
| 165 | - } |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - return true; |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * The code for this function was translated to PHP from mcrypt's enigma.c, |
|
| 174 | - * I was not able to find sufficient documentation to create my own |
|
| 175 | - * version of this function. |
|
| 176 | - * Enigma requires a 13 byte key, this function will |
|
| 177 | - * lengthen the key to get it to 13 bytes long if it's short. It also |
|
| 178 | - * Sets $deck, and $t1, $t2, $t3 which I think are the Enigma rotors. |
|
| 179 | - * |
|
| 180 | - * @return void |
|
| 181 | - */ |
|
| 182 | - private function createKey() |
|
| 183 | - { |
|
| 184 | - $this->deck = array(); |
|
| 185 | - $this->t1 = array(); |
|
| 186 | - $this->t2 = array_fill(0, self::ROTORSZ, 0); |
|
| 187 | - $this->t3 = $this->t2; |
|
| 188 | - $this->xkey = $this->key(); |
|
| 189 | - $klen = $this->keySize(); |
|
| 190 | - |
|
| 191 | - // get the key to exactly 13 bytes if it's less than 13 |
|
| 192 | - if($klen < 13) |
|
| 193 | - $this->xkey = str_pad($this->xkey, 13, chr(0), STR_PAD_RIGHT); |
|
| 194 | - |
|
| 195 | - $seed = 123; |
|
| 196 | - for($i = 0; $i < 13; ++$i) |
|
| 197 | - $seed = parent::sInt32($seed) * ord($this->xkey[$i]) + $i; |
|
| 198 | - |
|
| 199 | - // sets $t1 and $deck |
|
| 200 | - for($i = 0; $i < self::ROTORSZ; ++$i) |
|
| 201 | - { |
|
| 202 | - $this->t1[] = $i; |
|
| 203 | - $this->deck[] = $i; |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - // sets $t3 |
|
| 207 | - for($i = 0; $i < self::ROTORSZ; ++$i) |
|
| 208 | - { |
|
| 209 | - // make sure the return values are 32 bit |
|
| 210 | - $seed = 5 * parent::sInt32($seed) + ord($this->xkey[$i % 13]); |
|
| 211 | - $seed = parent::sInt32($seed); |
|
| 212 | - |
|
| 213 | - // force the returned value to be an unsigned int |
|
| 214 | - //$random = parent::uint32($seed % 65521); |
|
| 215 | - $random = parent::uInt($seed % 65521); |
|
| 216 | - |
|
| 217 | - $k = self::ROTORSZ - 1 - $i; |
|
| 218 | - $ic = ($random & self::MASK) % ($k + 1); |
|
| 219 | - |
|
| 220 | - // make sure the returned value is an unsigned int |
|
| 221 | - $random = parent::uInt($random >> 8); |
|
| 222 | - |
|
| 223 | - $temp = $this->t1[$k]; |
|
| 224 | - $this->t1[$k] = $this->t1[$ic]; |
|
| 225 | - $this->t1[$ic] = $temp; |
|
| 226 | - if($this->t3[$k] != 0) |
|
| 227 | - continue; |
|
| 228 | - |
|
| 229 | - $ic = ($random & self::MASK) % $k; |
|
| 230 | - while($this->t3[$ic] != 0) |
|
| 231 | - $ic = ($ic + 1) % $k; |
|
| 232 | - |
|
| 233 | - $this->t3[$k] = $ic; |
|
| 234 | - $this->t3[$ic] = $k; |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - // sets $t2 |
|
| 238 | - for($i = 0; $i < self::ROTORSZ; ++$i) |
|
| 239 | - { |
|
| 240 | - $pos = $this->t1[$i] & self::MASK; |
|
| 241 | - $this->t2[$pos] = $i; |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - // now convert $t1, $t2, $t3, $deck values to signed chars, |
|
| 245 | - // PHP's chr() function returns an unsigned char, so we have |
|
| 246 | - // to use use our own |
|
| 247 | - $this->t1 = array_map("parent::sChar", $this->t1); |
|
| 248 | - $this->t2 = array_map("parent::sChar", $this->t2); |
|
| 249 | - $this->t3 = array_map("parent::sChar", $this->t3); |
|
| 250 | - $this->deck = array_map("parent::sChar", $this->deck); |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - |
|
| 254 | - /** |
|
| 255 | - * Indicates this is a stream cipher |
|
| 256 | - * |
|
| 257 | - * @return integer Returns Cipher::STREAM |
|
| 258 | - */ |
|
| 259 | - public function type() |
|
| 260 | - { |
|
| 261 | - return parent::STREAM; |
|
| 262 | - } |
|
| 49 | + const MASK = 0377; |
|
| 50 | + |
|
| 51 | + /** @type integer ROTORSZ The size of $t1,$t2,$t3,$deck */ |
|
| 52 | + const ROTORSZ = 256; |
|
| 53 | + |
|
| 54 | + /** @type array $t1 An array of chars, ROTORSZ size */ |
|
| 55 | + private $t1 = array(); |
|
| 56 | + |
|
| 57 | + /** @type array $t2 An array of chars, ROTORSZ size */ |
|
| 58 | + private $t2 = array(); |
|
| 59 | + |
|
| 60 | + /** @type array $t3 An array of chars, ROTORSZ size */ |
|
| 61 | + private $t3 = array(); |
|
| 62 | + |
|
| 63 | + /** @type array $deck An array of chars, ROTORSZ size */ |
|
| 64 | + private $deck = array(); |
|
| 65 | + |
|
| 66 | + private $n1 = 0; |
|
| 67 | + private $n2 = 0; |
|
| 68 | + private $nr1 = 0; |
|
| 69 | + private $nr2 = 0; |
|
| 70 | + |
|
| 71 | + /** @type string $xkey Stores the modified key */ |
|
| 72 | + private $xkey = ""; |
|
| 73 | + |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * Constructor |
|
| 77 | + * |
|
| 78 | + * @param string $key The key used for Encryption/Decryption |
|
| 79 | + * @return void |
|
| 80 | + */ |
|
| 81 | + public function __construct($key) |
|
| 82 | + { |
|
| 83 | + parent::__construct(PHP_Crypt::CIPHER_ENIGMA, $key, strlen($key)); |
|
| 84 | + |
|
| 85 | + $this->createKey(); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Destructor |
|
| 91 | + * |
|
| 92 | + * @return void |
|
| 93 | + */ |
|
| 94 | + public function __destruct() |
|
| 95 | + { |
|
| 96 | + parent::__destruct(); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * Encrypt plain text data using Enigma |
|
| 102 | + * |
|
| 103 | + * @param string $str A plain text string of any length |
|
| 104 | + * @return boolean Returns true |
|
| 105 | + */ |
|
| 106 | + public function encrypt(&$str) |
|
| 107 | + { |
|
| 108 | + $this->operation(parent::ENCRYPT); |
|
| 109 | + return $this->enigma($str); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * Decrypt an Enigma encrypted string |
|
| 115 | + * |
|
| 116 | + * @param string $str A Enigma encrypted string |
|
| 117 | + * @return boolean Returns true |
|
| 118 | + */ |
|
| 119 | + public function decrypt(&$str) |
|
| 120 | + { |
|
| 121 | + $this->operation(parent::DECRYPT); |
|
| 122 | + return $this->enigma($str); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * Peforms the actual Enigma encryption/decryption. Since |
|
| 128 | + * the algorithm is the same for both, we do it all in |
|
| 129 | + * this one function |
|
| 130 | + * |
|
| 131 | + * @param string $str A string to encrypt or decrypt |
|
| 132 | + * @return boolean Returns true |
|
| 133 | + */ |
|
| 134 | + private function enigma(&$str) |
|
| 135 | + { |
|
| 136 | + // zero out the counters |
|
| 137 | + $this->n1 = 0; |
|
| 138 | + $this->n2 = 0; |
|
| 139 | + $this->nr1 = 0; |
|
| 140 | + $this->nr2 = 0; |
|
| 141 | + |
|
| 142 | + $max = strlen($str); |
|
| 143 | + for($j = 0; $j < $max; ++$j) |
|
| 144 | + { |
|
| 145 | + $i = ord($str[$j]); |
|
| 146 | + $this->nr1 = $this->n1; |
|
| 147 | + |
|
| 148 | + $pos1 = ($i + $this->nr1) & self::MASK; |
|
| 149 | + $pos3 = ($this->t1[$pos1] + $this->nr2) & self::MASK; |
|
| 150 | + $pos2 = ($this->t3[$pos3] - $this->nr2) & self::MASK; |
|
| 151 | + $i = $this->t2[$pos2] - $this->nr1; |
|
| 152 | + |
|
| 153 | + $str[$j] = chr($i); |
|
| 154 | + $this->n1++; |
|
| 155 | + |
|
| 156 | + if($this->n1 == self::ROTORSZ) |
|
| 157 | + { |
|
| 158 | + $this->n1 = 0; |
|
| 159 | + $this->n2++; |
|
| 160 | + |
|
| 161 | + if($this->n2 == self::ROTORSZ) |
|
| 162 | + $this->n2 = 0; |
|
| 163 | + |
|
| 164 | + $this->nr2 = $this->n2; |
|
| 165 | + } |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + return true; |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * The code for this function was translated to PHP from mcrypt's enigma.c, |
|
| 174 | + * I was not able to find sufficient documentation to create my own |
|
| 175 | + * version of this function. |
|
| 176 | + * Enigma requires a 13 byte key, this function will |
|
| 177 | + * lengthen the key to get it to 13 bytes long if it's short. It also |
|
| 178 | + * Sets $deck, and $t1, $t2, $t3 which I think are the Enigma rotors. |
|
| 179 | + * |
|
| 180 | + * @return void |
|
| 181 | + */ |
|
| 182 | + private function createKey() |
|
| 183 | + { |
|
| 184 | + $this->deck = array(); |
|
| 185 | + $this->t1 = array(); |
|
| 186 | + $this->t2 = array_fill(0, self::ROTORSZ, 0); |
|
| 187 | + $this->t3 = $this->t2; |
|
| 188 | + $this->xkey = $this->key(); |
|
| 189 | + $klen = $this->keySize(); |
|
| 190 | + |
|
| 191 | + // get the key to exactly 13 bytes if it's less than 13 |
|
| 192 | + if($klen < 13) |
|
| 193 | + $this->xkey = str_pad($this->xkey, 13, chr(0), STR_PAD_RIGHT); |
|
| 194 | + |
|
| 195 | + $seed = 123; |
|
| 196 | + for($i = 0; $i < 13; ++$i) |
|
| 197 | + $seed = parent::sInt32($seed) * ord($this->xkey[$i]) + $i; |
|
| 198 | + |
|
| 199 | + // sets $t1 and $deck |
|
| 200 | + for($i = 0; $i < self::ROTORSZ; ++$i) |
|
| 201 | + { |
|
| 202 | + $this->t1[] = $i; |
|
| 203 | + $this->deck[] = $i; |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + // sets $t3 |
|
| 207 | + for($i = 0; $i < self::ROTORSZ; ++$i) |
|
| 208 | + { |
|
| 209 | + // make sure the return values are 32 bit |
|
| 210 | + $seed = 5 * parent::sInt32($seed) + ord($this->xkey[$i % 13]); |
|
| 211 | + $seed = parent::sInt32($seed); |
|
| 212 | + |
|
| 213 | + // force the returned value to be an unsigned int |
|
| 214 | + //$random = parent::uint32($seed % 65521); |
|
| 215 | + $random = parent::uInt($seed % 65521); |
|
| 216 | + |
|
| 217 | + $k = self::ROTORSZ - 1 - $i; |
|
| 218 | + $ic = ($random & self::MASK) % ($k + 1); |
|
| 219 | + |
|
| 220 | + // make sure the returned value is an unsigned int |
|
| 221 | + $random = parent::uInt($random >> 8); |
|
| 222 | + |
|
| 223 | + $temp = $this->t1[$k]; |
|
| 224 | + $this->t1[$k] = $this->t1[$ic]; |
|
| 225 | + $this->t1[$ic] = $temp; |
|
| 226 | + if($this->t3[$k] != 0) |
|
| 227 | + continue; |
|
| 228 | + |
|
| 229 | + $ic = ($random & self::MASK) % $k; |
|
| 230 | + while($this->t3[$ic] != 0) |
|
| 231 | + $ic = ($ic + 1) % $k; |
|
| 232 | + |
|
| 233 | + $this->t3[$k] = $ic; |
|
| 234 | + $this->t3[$ic] = $k; |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + // sets $t2 |
|
| 238 | + for($i = 0; $i < self::ROTORSZ; ++$i) |
|
| 239 | + { |
|
| 240 | + $pos = $this->t1[$i] & self::MASK; |
|
| 241 | + $this->t2[$pos] = $i; |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + // now convert $t1, $t2, $t3, $deck values to signed chars, |
|
| 245 | + // PHP's chr() function returns an unsigned char, so we have |
|
| 246 | + // to use use our own |
|
| 247 | + $this->t1 = array_map("parent::sChar", $this->t1); |
|
| 248 | + $this->t2 = array_map("parent::sChar", $this->t2); |
|
| 249 | + $this->t3 = array_map("parent::sChar", $this->t3); |
|
| 250 | + $this->deck = array_map("parent::sChar", $this->deck); |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + |
|
| 254 | + /** |
|
| 255 | + * Indicates this is a stream cipher |
|
| 256 | + * |
|
| 257 | + * @return integer Returns Cipher::STREAM |
|
| 258 | + */ |
|
| 259 | + public function type() |
|
| 260 | + { |
|
| 261 | + return parent::STREAM; |
|
| 262 | + } |
|
| 263 | 263 | } |
| 264 | 264 | ?> |
@@ -38,39 +38,39 @@ |
||
| 38 | 38 | */ |
| 39 | 39 | class Cipher_Rijndael_256 extends Cipher_Rijndael |
| 40 | 40 | { |
| 41 | - /** @type integer BYTES_BLOCK The size of the block, in bytes */ |
|
| 42 | - const BYTES_BLOCK = 32; // 256 bits |
|
| 41 | + /** @type integer BYTES_BLOCK The size of the block, in bytes */ |
|
| 42 | + const BYTES_BLOCK = 32; // 256 bits |
|
| 43 | 43 | |
| 44 | - //const BITS_KEY = 0; |
|
| 44 | + //const BITS_KEY = 0; |
|
| 45 | 45 | |
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Constructor |
|
| 49 | - * Sets the key used for encryption. Also sets the requied block size |
|
| 50 | - * |
|
| 51 | - * @param string $key string containing the user supplied encryption key |
|
| 52 | - * @return void |
|
| 53 | - */ |
|
| 54 | - public function __construct($key) |
|
| 55 | - { |
|
| 56 | - // Set up the key |
|
| 57 | - parent::__construct(PHP_Crypt::CIPHER_RIJNDAEL_256, $key); |
|
| 47 | + /** |
|
| 48 | + * Constructor |
|
| 49 | + * Sets the key used for encryption. Also sets the requied block size |
|
| 50 | + * |
|
| 51 | + * @param string $key string containing the user supplied encryption key |
|
| 52 | + * @return void |
|
| 53 | + */ |
|
| 54 | + public function __construct($key) |
|
| 55 | + { |
|
| 56 | + // Set up the key |
|
| 57 | + parent::__construct(PHP_Crypt::CIPHER_RIJNDAEL_256, $key); |
|
| 58 | 58 | |
| 59 | - // required block size in bits |
|
| 60 | - $this->blockSize(self::BYTES_BLOCK); |
|
| 59 | + // required block size in bits |
|
| 60 | + $this->blockSize(self::BYTES_BLOCK); |
|
| 61 | 61 | |
| 62 | - // expand the key now that we know the key size, and the bit size |
|
| 63 | - $this->expandKey(); |
|
| 64 | - } |
|
| 62 | + // expand the key now that we know the key size, and the bit size |
|
| 63 | + $this->expandKey(); |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * Destructor |
|
| 69 | - * |
|
| 70 | - * @return void |
|
| 71 | - */ |
|
| 72 | - public function __destruct() |
|
| 73 | - { |
|
| 74 | - parent::__destruct(); |
|
| 75 | - } |
|
| 67 | + /** |
|
| 68 | + * Destructor |
|
| 69 | + * |
|
| 70 | + * @return void |
|
| 71 | + */ |
|
| 72 | + public function __destruct() |
|
| 73 | + { |
|
| 74 | + parent::__destruct(); |
|
| 75 | + } |
|
| 76 | 76 | } |