VPN licensing server
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

1010 líneas
38KB

  1. <?php
  2. /*
  3. Copyright (C) 2008 Sergey Tsalkov (stsalkov@gmail.com)
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. class DB {
  16. // initial connection
  17. public static $dbName = '';
  18. public static $user = '';
  19. public static $password = '';
  20. public static $host = 'localhost';
  21. public static $port = 3306; //hhvm complains if this is null
  22. public static $socket = null;
  23. public static $encoding = 'latin1';
  24. // configure workings
  25. public static $param_char = '%';
  26. public static $named_param_seperator = '_';
  27. public static $success_handler = false;
  28. public static $error_handler = true;
  29. public static $throw_exception_on_error = false;
  30. public static $nonsql_error_handler = null;
  31. public static $pre_sql_handler = false;
  32. public static $throw_exception_on_nonsql_error = false;
  33. public static $nested_transactions = false;
  34. public static $usenull = true;
  35. public static $ssl = array('key' => '', 'cert' => '', 'ca_cert' => '', 'ca_path' => '', 'cipher' => '');
  36. public static $connect_options = array(MYSQLI_OPT_CONNECT_TIMEOUT => 30);
  37. // internal
  38. protected static $mdb = null;
  39. public static $variables_to_sync = array('param_char', 'named_param_seperator', 'success_handler', 'error_handler', 'throw_exception_on_error',
  40. 'nonsql_error_handler', 'pre_sql_handler', 'throw_exception_on_nonsql_error', 'nested_transactions', 'usenull', 'ssl', 'connect_options');
  41. public static function getMDB() {
  42. $mdb = DB::$mdb;
  43. if ($mdb === null) {
  44. $mdb = DB::$mdb = new MeekroDB();
  45. }
  46. // Sync everytime because settings might have changed. It's fast.
  47. $mdb->sync_config();
  48. return $mdb;
  49. }
  50. // yes, this is ugly. __callStatic() only works in 5.3+
  51. public static function get() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'get'), $args); }
  52. public static function disconnect() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'disconnect'), $args); }
  53. public static function query() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'query'), $args); }
  54. public static function queryFirstRow() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryFirstRow'), $args); }
  55. public static function queryOneRow() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryOneRow'), $args); }
  56. public static function queryAllLists() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryAllLists'), $args); }
  57. public static function queryFullColumns() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryFullColumns'), $args); }
  58. public static function queryFirstList() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryFirstList'), $args); }
  59. public static function queryOneList() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryOneList'), $args); }
  60. public static function queryFirstColumn() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryFirstColumn'), $args); }
  61. public static function queryOneColumn() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryOneColumn'), $args); }
  62. public static function queryFirstField() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryFirstField'), $args); }
  63. public static function queryOneField() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryOneField'), $args); }
  64. public static function queryRaw() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryRaw'), $args); }
  65. public static function queryRawUnbuf() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryRawUnbuf'), $args); }
  66. public static function insert() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'insert'), $args); }
  67. public static function insertIgnore() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'insertIgnore'), $args); }
  68. public static function insertUpdate() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'insertUpdate'), $args); }
  69. public static function replace() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'replace'), $args); }
  70. public static function update() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'update'), $args); }
  71. public static function delete() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'delete'), $args); }
  72. public static function insertId() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'insertId'), $args); }
  73. public static function count() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'count'), $args); }
  74. public static function affectedRows() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'affectedRows'), $args); }
  75. public static function useDB() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'useDB'), $args); }
  76. public static function startTransaction() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'startTransaction'), $args); }
  77. public static function commit() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'commit'), $args); }
  78. public static function rollback() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'rollback'), $args); }
  79. public static function tableList() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'tableList'), $args); }
  80. public static function columnList() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'columnList'), $args); }
  81. public static function sqlEval() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'sqlEval'), $args); }
  82. public static function nonSQLError() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'nonSQLError'), $args); }
  83. public static function serverVersion() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'serverVersion'), $args); }
  84. public static function transactionDepth() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'transactionDepth'), $args); }
  85. public static function debugMode($handler = true) {
  86. DB::$success_handler = $handler;
  87. }
  88. }
  89. class MeekroDB {
  90. // initial connection
  91. public $dbName = '';
  92. public $user = '';
  93. public $password = '';
  94. public $host = 'localhost';
  95. public $port = 3306;
  96. public $socket = null;
  97. public $encoding = 'latin1';
  98. // configure workings
  99. public $param_char = '%';
  100. public $named_param_seperator = '_';
  101. public $success_handler = false;
  102. public $error_handler = true;
  103. public $throw_exception_on_error = false;
  104. public $nonsql_error_handler = null;
  105. public $pre_sql_handler = false;
  106. public $throw_exception_on_nonsql_error = false;
  107. public $nested_transactions = false;
  108. public $usenull = true;
  109. public $ssl = array('key' => '', 'cert' => '', 'ca_cert' => '', 'ca_path' => '', 'cipher' => '');
  110. public $connect_options = array(MYSQLI_OPT_CONNECT_TIMEOUT => 30);
  111. // internal
  112. public $internal_mysql = null;
  113. public $server_info = null;
  114. public $insert_id = 0;
  115. public $num_rows = 0;
  116. public $affected_rows = 0;
  117. public $current_db = null;
  118. public $nested_transactions_count = 0;
  119. public function __construct($host=null, $user=null, $password=null, $dbName=null, $port=null, $encoding=null, $socket=null) {
  120. if ($host === null) $host = DB::$host;
  121. if ($user === null) $user = DB::$user;
  122. if ($password === null) $password = DB::$password;
  123. if ($dbName === null) $dbName = DB::$dbName;
  124. if ($port === null) $port = DB::$port;
  125. if ($socket === null) $socket = DB::$socket;
  126. if ($encoding === null) $encoding = DB::$encoding;
  127. $this->host = $host;
  128. $this->user = $user;
  129. $this->password = $password;
  130. $this->dbName = $dbName;
  131. $this->port = $port;
  132. $this->socket = $socket;
  133. $this->encoding = $encoding;
  134. $this->sync_config();
  135. }
  136. // suck in config settings from static class
  137. public function sync_config() {
  138. foreach (DB::$variables_to_sync as $variable) {
  139. if ($this->$variable !== DB::$$variable) {
  140. $this->$variable = DB::$$variable;
  141. }
  142. }
  143. }
  144. public function get() {
  145. $mysql = $this->internal_mysql;
  146. if (!($mysql instanceof MySQLi)) {
  147. if (! $this->port) $this->port = ini_get('mysqli.default_port');
  148. $this->current_db = $this->dbName;
  149. $mysql = new mysqli();
  150. $connect_flags = 0;
  151. if ($this->ssl['key']) {
  152. $mysql->ssl_set($this->ssl['key'], $this->ssl['cert'], $this->ssl['ca_cert'], $this->ssl['ca_path'], $this->ssl['cipher']);
  153. $connect_flags |= MYSQLI_CLIENT_SSL;
  154. }
  155. foreach ($this->connect_options as $key => $value) {
  156. $mysql->options($key, $value);
  157. }
  158. // suppress warnings, since we will check connect_error anyway
  159. @$mysql->real_connect($this->host, $this->user, $this->password, $this->dbName, $this->port, $this->socket, $connect_flags);
  160. if ($mysql->connect_error) {
  161. return $this->nonSQLError('Unable to connect to MySQL server! Error: ' . $mysql->connect_error);
  162. }
  163. $mysql->set_charset($this->encoding);
  164. $this->internal_mysql = $mysql;
  165. $this->server_info = $mysql->server_info;
  166. }
  167. return $mysql;
  168. }
  169. public function disconnect() {
  170. $mysqli = $this->internal_mysql;
  171. if ($mysqli instanceof MySQLi) {
  172. if ($thread_id = $mysqli->thread_id) $mysqli->kill($thread_id);
  173. $mysqli->close();
  174. }
  175. $this->internal_mysql = null;
  176. }
  177. public function nonSQLError($message) {
  178. if ($this->throw_exception_on_nonsql_error) {
  179. $e = new MeekroDBException($message);
  180. throw $e;
  181. }
  182. $error_handler = is_callable($this->nonsql_error_handler) ? $this->nonsql_error_handler : 'meekrodb_error_handler';
  183. call_user_func($error_handler, array(
  184. 'type' => 'nonsql',
  185. 'error' => $message
  186. ));
  187. }
  188. public function debugMode($handler = true) {
  189. $this->success_handler = $handler;
  190. }
  191. public function serverVersion() { $this->get(); return $this->server_info; }
  192. public function transactionDepth() { return $this->nested_transactions_count; }
  193. public function insertId() { return $this->insert_id; }
  194. public function affectedRows() { return $this->affected_rows; }
  195. public function count() { $args = func_get_args(); return call_user_func_array(array($this, 'numRows'), $args); }
  196. public function numRows() { return $this->num_rows; }
  197. public function useDB() { $args = func_get_args(); return call_user_func_array(array($this, 'setDB'), $args); }
  198. public function setDB($dbName) {
  199. $db = $this->get();
  200. if (! $db->select_db($dbName)) return $this->nonSQLError("Unable to set database to $dbName");
  201. $this->current_db = $dbName;
  202. }
  203. public function startTransaction() {
  204. if ($this->nested_transactions && $this->serverVersion() < '5.5') {
  205. return $this->nonSQLError("Nested transactions are only available on MySQL 5.5 and greater. You are using MySQL " . $this->serverVersion());
  206. }
  207. if (!$this->nested_transactions || $this->nested_transactions_count == 0) {
  208. $this->query('START TRANSACTION');
  209. $this->nested_transactions_count = 1;
  210. } else {
  211. $this->query("SAVEPOINT LEVEL{$this->nested_transactions_count}");
  212. $this->nested_transactions_count++;
  213. }
  214. return $this->nested_transactions_count;
  215. }
  216. public function commit($all=false) {
  217. if ($this->nested_transactions && $this->serverVersion() < '5.5') {
  218. return $this->nonSQLError("Nested transactions are only available on MySQL 5.5 and greater. You are using MySQL " . $this->serverVersion());
  219. }
  220. if ($this->nested_transactions && $this->nested_transactions_count > 0)
  221. $this->nested_transactions_count--;
  222. if (!$this->nested_transactions || $all || $this->nested_transactions_count == 0) {
  223. $this->nested_transactions_count = 0;
  224. $this->query('COMMIT');
  225. } else {
  226. $this->query("RELEASE SAVEPOINT LEVEL{$this->nested_transactions_count}");
  227. }
  228. return $this->nested_transactions_count;
  229. }
  230. public function rollback($all=false) {
  231. if ($this->nested_transactions && $this->serverVersion() < '5.5') {
  232. return $this->nonSQLError("Nested transactions are only available on MySQL 5.5 and greater. You are using MySQL " . $this->serverVersion());
  233. }
  234. if ($this->nested_transactions && $this->nested_transactions_count > 0)
  235. $this->nested_transactions_count--;
  236. if (!$this->nested_transactions || $all || $this->nested_transactions_count == 0) {
  237. $this->nested_transactions_count = 0;
  238. $this->query('ROLLBACK');
  239. } else {
  240. $this->query("ROLLBACK TO SAVEPOINT LEVEL{$this->nested_transactions_count}");
  241. }
  242. return $this->nested_transactions_count;
  243. }
  244. protected function formatTableName($table) {
  245. $table = trim($table, '`');
  246. if (strpos($table, '.')) return implode('.', array_map(array($this, 'formatTableName'), explode('.', $table)));
  247. else return '`' . str_replace('`', '``', $table) . '`';
  248. }
  249. public function update() {
  250. $args = func_get_args();
  251. $table = array_shift($args);
  252. $params = array_shift($args);
  253. $update_part = $this->parseQueryParams(
  254. str_replace('%', $this->param_char, "UPDATE %b SET %hc"),
  255. $table, $params
  256. );
  257. $where_part = call_user_func_array(array($this, 'parseQueryParams'), $args);
  258. $query = $update_part . ' WHERE ' . $where_part;
  259. return $this->query($query);
  260. }
  261. public function insertOrReplace($which, $table, $datas, $options=array()) {
  262. $datas = unserialize(serialize($datas)); // break references within array
  263. $keys = $values = array();
  264. if (isset($datas[0]) && is_array($datas[0])) {
  265. $var = '%ll?';
  266. foreach ($datas as $datum) {
  267. ksort($datum);
  268. if (! $keys) $keys = array_keys($datum);
  269. $values[] = array_values($datum);
  270. }
  271. } else {
  272. $var = '%l?';
  273. $keys = array_keys($datas);
  274. $values = array_values($datas);
  275. }
  276. if ($which != 'INSERT' && $which != 'INSERT IGNORE' && $which != 'REPLACE') {
  277. return $this->nonSQLError('insertOrReplace() must be called with one of: INSERT, INSERT IGNORE, REPLACE');
  278. }
  279. if (isset($options['update']) && is_array($options['update']) && $options['update'] && $which == 'INSERT') {
  280. if (array_values($options['update']) !== $options['update']) {
  281. return $this->query(
  282. str_replace('%', $this->param_char, "INSERT INTO %b %lb VALUES $var ON DUPLICATE KEY UPDATE %hc"),
  283. $table, $keys, $values, $options['update']);
  284. } else {
  285. $update_str = array_shift($options['update']);
  286. $query_param = array(
  287. str_replace('%', $this->param_char, "INSERT INTO %b %lb VALUES $var ON DUPLICATE KEY UPDATE ") . $update_str,
  288. $table, $keys, $values);
  289. $query_param = array_merge($query_param, $options['update']);
  290. return call_user_func_array(array($this, 'query'), $query_param);
  291. }
  292. }
  293. return $this->query(
  294. str_replace('%', $this->param_char, "%l INTO %b %lb VALUES $var"),
  295. $which, $table, $keys, $values);
  296. }
  297. public function insert($table, $data) { return $this->insertOrReplace('INSERT', $table, $data); }
  298. public function insertIgnore($table, $data) { return $this->insertOrReplace('INSERT IGNORE', $table, $data); }
  299. public function replace($table, $data) { return $this->insertOrReplace('REPLACE', $table, $data); }
  300. public function insertUpdate() {
  301. $args = func_get_args();
  302. $table = array_shift($args);
  303. $data = array_shift($args);
  304. if (! isset($args[0])) { // update will have all the data of the insert
  305. if (isset($data[0]) && is_array($data[0])) { //multiple insert rows specified -- failing!
  306. return $this->nonSQLError("Badly formatted insertUpdate() query -- you didn't specify the update component!");
  307. }
  308. $args[0] = $data;
  309. }
  310. if (is_array($args[0])) $update = $args[0];
  311. else $update = $args;
  312. return $this->insertOrReplace('INSERT', $table, $data, array('update' => $update));
  313. }
  314. public function delete() {
  315. $args = func_get_args();
  316. $table = $this->formatTableName(array_shift($args));
  317. $where = call_user_func_array(array($this, 'parseQueryParams'), $args);
  318. $query = "DELETE FROM {$table} WHERE {$where}";
  319. return $this->query($query);
  320. }
  321. public function sqleval() {
  322. $args = func_get_args();
  323. $text = call_user_func_array(array($this, 'parseQueryParams'), $args);
  324. return new MeekroDBEval($text);
  325. }
  326. public function columnList($table) {
  327. return $this->queryOneColumn('Field', "SHOW COLUMNS FROM %b", $table);
  328. }
  329. public function tableList($db = null) {
  330. if ($db) {
  331. $olddb = $this->current_db;
  332. $this->useDB($db);
  333. }
  334. $result = $this->queryFirstColumn('SHOW TABLES');
  335. if (isset($olddb)) $this->useDB($olddb);
  336. return $result;
  337. }
  338. protected function preparseQueryParams() {
  339. $args = func_get_args();
  340. $sql = trim(strval(array_shift($args)));
  341. $args_all = $args;
  342. if (count($args_all) == 0) return array($sql);
  343. $param_char_length = strlen($this->param_char);
  344. $named_seperator_length = strlen($this->named_param_seperator);
  345. $types = array(
  346. $this->param_char . 'll', // list of literals
  347. $this->param_char . 'ls', // list of strings
  348. $this->param_char . 'l', // literal
  349. $this->param_char . 'li', // list of integers
  350. $this->param_char . 'ld', // list of decimals
  351. $this->param_char . 'lb', // list of backticks
  352. $this->param_char . 'lt', // list of timestamps
  353. $this->param_char . 's', // string
  354. $this->param_char . 'i', // integer
  355. $this->param_char . 'd', // double / decimal
  356. $this->param_char . 'b', // backtick
  357. $this->param_char . 't', // timestamp
  358. $this->param_char . '?', // infer type
  359. $this->param_char . 'l?', // list of inferred types
  360. $this->param_char . 'll?', // list of lists of inferred types
  361. $this->param_char . 'hc', // hash `key`='value' pairs separated by commas
  362. $this->param_char . 'ha', // hash `key`='value' pairs separated by and
  363. $this->param_char . 'ho', // hash `key`='value' pairs separated by or
  364. $this->param_char . 'ss', // search string (like string, surrounded with %'s)
  365. $this->param_char . 'ssb', // search string (like, begins with)
  366. $this->param_char . 'sse', // search string (like, ends with)
  367. );
  368. // generate list of all MeekroDB variables in our query, and their position
  369. // in the form "offset => variable", sorted by offsets
  370. $posList = array();
  371. foreach ($types as $type) {
  372. $lastPos = 0;
  373. while (($pos = strpos($sql, $type, $lastPos)) !== false) {
  374. $lastPos = $pos + 1;
  375. if (isset($posList[$pos]) && strlen($posList[$pos]) > strlen($type)) continue;
  376. $posList[$pos] = $type;
  377. }
  378. }
  379. ksort($posList);
  380. // for each MeekroDB variable, substitute it with array(type: i, value: 53) or whatever
  381. $chunkyQuery = array(); // preparsed query
  382. $pos_adj = 0; // how much we've added or removed from the original sql string
  383. foreach ($posList as $pos => $type) {
  384. $type = substr($type, $param_char_length); // variable, without % in front of it
  385. $length_type = strlen($type) + $param_char_length; // length of variable w/o %
  386. $new_pos = $pos + $pos_adj; // position of start of variable
  387. $new_pos_back = $new_pos + $length_type; // position of end of variable
  388. $arg_number_length = 0; // length of any named or numbered parameter addition
  389. // handle numbered parameters
  390. if ($arg_number_length = strspn($sql, '0123456789', $new_pos_back)) {
  391. $arg_number = substr($sql, $new_pos_back, $arg_number_length);
  392. if (! array_key_exists($arg_number, $args_all)) return $this->nonSQLError("Non existent argument reference (arg $arg_number): $sql");
  393. $arg = $args_all[$arg_number];
  394. // handle named parameters
  395. } else if (substr($sql, $new_pos_back, $named_seperator_length) == $this->named_param_seperator) {
  396. $arg_number_length = strspn($sql, 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_',
  397. $new_pos_back + $named_seperator_length) + $named_seperator_length;
  398. $arg_number = substr($sql, $new_pos_back + $named_seperator_length, $arg_number_length - $named_seperator_length);
  399. if (count($args_all) != 1 || !is_array($args_all[0])) return $this->nonSQLError("If you use named parameters, the second argument must be an array of parameters");
  400. if (! array_key_exists($arg_number, $args_all[0])) return $this->nonSQLError("Non existent argument reference (arg $arg_number): $sql");
  401. $arg = $args_all[0][$arg_number];
  402. } else {
  403. $arg_number = 0;
  404. $arg = array_shift($args);
  405. }
  406. if ($new_pos > 0) $chunkyQuery[] = substr($sql, 0, $new_pos);
  407. if (is_object($arg) && ($arg instanceof WhereClause)) {
  408. list($clause_sql, $clause_args) = $arg->textAndArgs();
  409. array_unshift($clause_args, $clause_sql);
  410. $preparsed_sql = call_user_func_array(array($this, 'preparseQueryParams'), $clause_args);
  411. $chunkyQuery = array_merge($chunkyQuery, $preparsed_sql);
  412. } else {
  413. $chunkyQuery[] = array('type' => $type, 'value' => $arg);
  414. }
  415. $sql = substr($sql, $new_pos_back + $arg_number_length);
  416. $pos_adj -= $new_pos_back + $arg_number_length;
  417. }
  418. if (strlen($sql) > 0) $chunkyQuery[] = $sql;
  419. return $chunkyQuery;
  420. }
  421. public function escape($str) { return "'" . $this->get()->real_escape_string(strval($str)) . "'"; }
  422. public function sanitize($value, $type='basic', $hashjoin=', ') {
  423. if ($type == 'basic') {
  424. if (is_object($value)) {
  425. if ($value instanceof MeekroDBEval) return $value->text;
  426. else if ($value instanceof DateTime) return $this->escape($value->format('Y-m-d H:i:s'));
  427. else return $this->escape($value); // use __toString() value for objects, when possible
  428. }
  429. if (is_null($value)) return $this->usenull ? 'NULL' : "''";
  430. else if (is_bool($value)) return ($value ? 1 : 0);
  431. else if (is_int($value)) return $value;
  432. else if (is_float($value)) return $value;
  433. else if (is_array($value)) return "''";
  434. else return $this->escape($value);
  435. } else if ($type == 'list') {
  436. if (is_array($value)) {
  437. $value = array_values($value);
  438. return '(' . implode(', ', array_map(array($this, 'sanitize'), $value)) . ')';
  439. } else {
  440. return $this->nonSQLError("Expected array parameter, got something different!");
  441. }
  442. } else if ($type == 'doublelist') {
  443. if (is_array($value) && array_values($value) === $value && is_array($value[0])) {
  444. $cleanvalues = array();
  445. foreach ($value as $subvalue) {
  446. $cleanvalues[] = $this->sanitize($subvalue, 'list');
  447. }
  448. return implode(', ', $cleanvalues);
  449. } else {
  450. return $this->nonSQLError("Expected double array parameter, got something different!");
  451. }
  452. } else if ($type == 'hash') {
  453. if (is_array($value)) {
  454. $pairs = array();
  455. foreach ($value as $k => $v) {
  456. $pairs[] = $this->formatTableName($k) . '=' . $this->sanitize($v);
  457. }
  458. return implode($hashjoin, $pairs);
  459. } else {
  460. return $this->nonSQLError("Expected hash (associative array) parameter, got something different!");
  461. }
  462. } else {
  463. return $this->nonSQLError("Invalid type passed to sanitize()!");
  464. }
  465. }
  466. protected function parseTS($ts) {
  467. if (is_string($ts)) return date('Y-m-d H:i:s', strtotime($ts));
  468. else if (is_object($ts) && ($ts instanceof DateTime)) return $ts->format('Y-m-d H:i:s');
  469. }
  470. protected function intval($var) {
  471. if (PHP_INT_SIZE == 8) return intval($var);
  472. return floor(doubleval($var));
  473. }
  474. public function parseQueryParams() {
  475. $args = func_get_args();
  476. $chunkyQuery = call_user_func_array(array($this, 'preparseQueryParams'), $args);
  477. $query = '';
  478. $array_types = array('ls', 'li', 'ld', 'lb', 'll', 'lt', 'l?', 'll?', 'hc', 'ha', 'ho');
  479. foreach ($chunkyQuery as $chunk) {
  480. if (is_string($chunk)) {
  481. $query .= $chunk;
  482. continue;
  483. }
  484. $type = $chunk['type'];
  485. $arg = $chunk['value'];
  486. $result = '';
  487. $is_array_type = in_array($type, $array_types, true);
  488. if ($is_array_type && !is_array($arg)) return $this->nonSQLError("Badly formatted SQL query: Expected array, got scalar instead!");
  489. else if (!$is_array_type && is_array($arg)) $arg = '';
  490. if ($type == 's') $result = $this->escape($arg);
  491. else if ($type == 'i') $result = $this->intval($arg);
  492. else if ($type == 'd') $result = doubleval($arg);
  493. else if ($type == 'b') $result = $this->formatTableName($arg);
  494. else if ($type == 'l') $result = $arg;
  495. else if ($type == 'ss') $result = $this->escape("%" . str_replace(array('%', '_'), array('\%', '\_'), $arg) . "%");
  496. else if ($type == 'ssb') $result = $this->escape(str_replace(array('%', '_'), array('\%', '\_'), $arg) . "%");
  497. else if ($type == 'sse') $result = $this->escape("%" . str_replace(array('%', '_'), array('\%', '\_'), $arg));
  498. else if ($type == 't') $result = $this->escape($this->parseTS($arg));
  499. else if ($type == 'ls') $result = array_map(array($this, 'escape'), $arg);
  500. else if ($type == 'li') $result = array_map(array($this, 'intval'), $arg);
  501. else if ($type == 'ld') $result = array_map('doubleval', $arg);
  502. else if ($type == 'lb') $result = array_map(array($this, 'formatTableName'), $arg);
  503. else if ($type == 'll') $result = $arg;
  504. else if ($type == 'lt') $result = array_map(array($this, 'escape'), array_map(array($this, 'parseTS'), $arg));
  505. else if ($type == '?') $result = $this->sanitize($arg);
  506. else if ($type == 'l?') $result = $this->sanitize($arg, 'list');
  507. else if ($type == 'll?') $result = $this->sanitize($arg, 'doublelist');
  508. else if ($type == 'hc') $result = $this->sanitize($arg, 'hash');
  509. else if ($type == 'ha') $result = $this->sanitize($arg, 'hash', ' AND ');
  510. else if ($type == 'ho') $result = $this->sanitize($arg, 'hash', ' OR ');
  511. else return $this->nonSQLError("Badly formatted SQL query: Invalid MeekroDB param $type");
  512. if (is_array($result)) $result = '(' . implode(',', $result) . ')';
  513. $query .= $result;
  514. }
  515. return $query;
  516. }
  517. protected function prependCall($function, $args, $prepend) { array_unshift($args, $prepend); return call_user_func_array($function, $args); }
  518. public function query() { $args = func_get_args(); return $this->prependCall(array($this, 'queryHelper'), $args, 'assoc'); }
  519. public function queryAllLists() { $args = func_get_args(); return $this->prependCall(array($this, 'queryHelper'), $args, 'list'); }
  520. public function queryFullColumns() { $args = func_get_args(); return $this->prependCall(array($this, 'queryHelper'), $args, 'full'); }
  521. public function queryRaw() { $args = func_get_args(); return $this->prependCall(array($this, 'queryHelper'), $args, 'raw_buf'); }
  522. public function queryRawUnbuf() { $args = func_get_args(); return $this->prependCall(array($this, 'queryHelper'), $args, 'raw_unbuf'); }
  523. protected function queryHelper() {
  524. $args = func_get_args();
  525. $type = array_shift($args);
  526. $db = $this->get();
  527. $is_buffered = true;
  528. $row_type = 'assoc'; // assoc, list, raw
  529. $full_names = false;
  530. switch ($type) {
  531. case 'assoc':
  532. break;
  533. case 'list':
  534. $row_type = 'list';
  535. break;
  536. case 'full':
  537. $row_type = 'list';
  538. $full_names = true;
  539. break;
  540. case 'raw_buf':
  541. $row_type = 'raw';
  542. break;
  543. case 'raw_unbuf':
  544. $is_buffered = false;
  545. $row_type = 'raw';
  546. break;
  547. default:
  548. return $this->nonSQLError('Error -- invalid argument to queryHelper!');
  549. }
  550. $sql = call_user_func_array(array($this, 'parseQueryParams'), $args);
  551. if ($this->pre_sql_handler !== false && is_callable($this->pre_sql_handler)) {
  552. $sql = call_user_func($this->pre_sql_handler, $sql);
  553. }
  554. if ($this->success_handler) $starttime = microtime(true);
  555. $result = $db->query($sql, $is_buffered ? MYSQLI_STORE_RESULT : MYSQLI_USE_RESULT);
  556. if ($this->success_handler) $runtime = microtime(true) - $starttime;
  557. else $runtime = 0;
  558. // ----- BEGIN ERROR HANDLING
  559. if (!$sql || $db->error) {
  560. if ($this->error_handler) {
  561. $error_handler = is_callable($this->error_handler) ? $this->error_handler : 'meekrodb_error_handler';
  562. call_user_func($error_handler, array(
  563. 'type' => 'sql',
  564. 'query' => $sql,
  565. 'error' => $db->error,
  566. 'code' => $db->errno
  567. ));
  568. }
  569. if ($this->throw_exception_on_error) {
  570. $e = new MeekroDBException($db->error, $sql, $db->errno);
  571. throw $e;
  572. }
  573. } else if ($this->success_handler) {
  574. $runtime = sprintf('%f', $runtime * 1000);
  575. $success_handler = is_callable($this->success_handler) ? $this->success_handler : 'meekrodb_debugmode_handler';
  576. call_user_func($success_handler, array(
  577. 'query' => $sql,
  578. 'runtime' => $runtime,
  579. 'affected' => $db->affected_rows
  580. ));
  581. }
  582. // ----- END ERROR HANDLING
  583. $this->insert_id = $db->insert_id;
  584. $this->affected_rows = $db->affected_rows;
  585. // mysqli_result->num_rows won't initially show correct results for unbuffered data
  586. if ($is_buffered && ($result instanceof MySQLi_Result)) $this->num_rows = $result->num_rows;
  587. else $this->num_rows = null;
  588. if ($row_type == 'raw' || !($result instanceof MySQLi_Result)) return $result;
  589. $return = array();
  590. if ($full_names) {
  591. $infos = array();
  592. foreach ($result->fetch_fields() as $info) {
  593. if (strlen($info->table)) $infos[] = $info->table . '.' . $info->name;
  594. else $infos[] = $info->name;
  595. }
  596. }
  597. while ($row = ($row_type == 'assoc' ? $result->fetch_assoc() : $result->fetch_row())) {
  598. if ($full_names) $row = array_combine($infos, $row);
  599. $return[] = $row;
  600. }
  601. // free results
  602. $result->free();
  603. while ($db->more_results()) {
  604. $db->next_result();
  605. if ($result = $db->use_result()) $result->free();
  606. }
  607. return $return;
  608. }
  609. public function queryOneRow() { $args = func_get_args(); return call_user_func_array(array($this, 'queryFirstRow'), $args); }
  610. public function queryFirstRow() {
  611. $args = func_get_args();
  612. $result = call_user_func_array(array($this, 'query'), $args);
  613. if (!$result || !is_array($result)) return null;
  614. return reset($result);
  615. }
  616. public function queryOneList() { $args = func_get_args(); return call_user_func_array(array($this, 'queryFirstList'), $args); }
  617. public function queryFirstList() {
  618. $args = func_get_args();
  619. $result = call_user_func_array(array($this, 'queryAllLists'), $args);
  620. if (!$result || !is_array($result)) return null;
  621. return reset($result);
  622. }
  623. public function queryFirstColumn() {
  624. $args = func_get_args();
  625. $results = call_user_func_array(array($this, 'queryAllLists'), $args);
  626. $ret = array();
  627. if (!count($results) || !count($results[0])) return $ret;
  628. foreach ($results as $row) {
  629. $ret[] = $row[0];
  630. }
  631. return $ret;
  632. }
  633. public function queryOneColumn() {
  634. $args = func_get_args();
  635. $column = array_shift($args);
  636. $results = call_user_func_array(array($this, 'query'), $args);
  637. $ret = array();
  638. if (!count($results) || !count($results[0])) return $ret;
  639. if ($column === null) {
  640. $keys = array_keys($results[0]);
  641. $column = $keys[0];
  642. }
  643. foreach ($results as $row) {
  644. $ret[] = $row[$column];
  645. }
  646. return $ret;
  647. }
  648. public function queryFirstField() {
  649. $args = func_get_args();
  650. $row = call_user_func_array(array($this, 'queryFirstList'), $args);
  651. if ($row == null) return null;
  652. return $row[0];
  653. }
  654. public function queryOneField() {
  655. $args = func_get_args();
  656. $column = array_shift($args);
  657. $row = call_user_func_array(array($this, 'queryOneRow'), $args);
  658. if ($row == null) {
  659. return null;
  660. } else if ($column === null) {
  661. $keys = array_keys($row);
  662. $column = $keys[0];
  663. }
  664. return $row[$column];
  665. }
  666. }
  667. class WhereClause {
  668. public $type = 'and'; //AND or OR
  669. public $negate = false;
  670. public $clauses = array();
  671. function __construct($type) {
  672. $type = strtolower($type);
  673. if ($type !== 'or' && $type !== 'and') return DB::nonSQLError('you must use either WhereClause(and) or WhereClause(or)');
  674. $this->type = $type;
  675. }
  676. function add() {
  677. $args = func_get_args();
  678. $sql = array_shift($args);
  679. if ($sql instanceof WhereClause) {
  680. $this->clauses[] = $sql;
  681. } else {
  682. $this->clauses[] = array('sql' => $sql, 'args' => $args);
  683. }
  684. }
  685. function negateLast() {
  686. $i = count($this->clauses) - 1;
  687. if (!isset($this->clauses[$i])) return;
  688. if ($this->clauses[$i] instanceof WhereClause) {
  689. $this->clauses[$i]->negate();
  690. } else {
  691. $this->clauses[$i]['sql'] = 'NOT (' . $this->clauses[$i]['sql'] . ')';
  692. }
  693. }
  694. function negate() {
  695. $this->negate = ! $this->negate;
  696. }
  697. function addClause($type) {
  698. $r = new WhereClause($type);
  699. $this->add($r);
  700. return $r;
  701. }
  702. function count() {
  703. return count($this->clauses);
  704. }
  705. function textAndArgs() {
  706. $sql = array();
  707. $args = array();
  708. if (count($this->clauses) == 0) return array('(1)', $args);
  709. foreach ($this->clauses as $clause) {
  710. if ($clause instanceof WhereClause) {
  711. list($clause_sql, $clause_args) = $clause->textAndArgs();
  712. } else {
  713. $clause_sql = $clause['sql'];
  714. $clause_args = $clause['args'];
  715. }
  716. $sql[] = "($clause_sql)";
  717. $args = array_merge($args, $clause_args);
  718. }
  719. if ($this->type == 'and') $sql = sprintf('(%s)', implode(' AND ', $sql));
  720. else $sql = sprintf('(%s)', implode(' OR ', $sql));
  721. if ($this->negate) $sql = '(NOT ' . $sql . ')';
  722. return array($sql, $args);
  723. }
  724. // backwards compatability
  725. // we now return full WhereClause object here and evaluate it in preparseQueryParams
  726. function text() { return $this; }
  727. }
  728. class DBTransaction {
  729. private $committed = false;
  730. function __construct() {
  731. DB::startTransaction();
  732. }
  733. function __destruct() {
  734. if (! $this->committed) DB::rollback();
  735. }
  736. function commit() {
  737. DB::commit();
  738. $this->committed = true;
  739. }
  740. }
  741. class MeekroDBException extends Exception {
  742. protected $query = '';
  743. function __construct($message='', $query='', $code = 0) {
  744. parent::__construct($message);
  745. $this->query = $query;
  746. $this->code = $code;
  747. }
  748. public function getQuery() { return $this->query; }
  749. }
  750. class DBHelper {
  751. /*
  752. verticalSlice
  753. 1. For an array of assoc rays, return an array of values for a particular key
  754. 2. if $keyfield is given, same as above but use that hash key as the key in new array
  755. */
  756. public static function verticalSlice($array, $field, $keyfield = null) {
  757. $array = (array) $array;
  758. $R = array();
  759. foreach ($array as $obj) {
  760. if (! array_key_exists($field, $obj)) die("verticalSlice: array doesn't have requested field\n");
  761. if ($keyfield) {
  762. if (! array_key_exists($keyfield, $obj)) die("verticalSlice: array doesn't have requested field\n");
  763. $R[$obj[$keyfield]] = $obj[$field];
  764. } else {
  765. $R[] = $obj[$field];
  766. }
  767. }
  768. return $R;
  769. }
  770. /*
  771. reIndex
  772. For an array of assoc rays, return a new array of assoc rays using a certain field for keys
  773. */
  774. public static function reIndex() {
  775. $fields = func_get_args();
  776. $array = array_shift($fields);
  777. $array = (array) $array;
  778. $R = array();
  779. foreach ($array as $obj) {
  780. $target =& $R;
  781. foreach ($fields as $field) {
  782. if (! array_key_exists($field, $obj)) die("reIndex: array doesn't have requested field\n");
  783. $nextkey = $obj[$field];
  784. $target =& $target[$nextkey];
  785. }
  786. $target = $obj;
  787. }
  788. return $R;
  789. }
  790. }
  791. function meekrodb_error_handler($params) {
  792. if (isset($params['query'])) $out[] = "QUERY: " . $params['query'];
  793. if (isset($params['error'])) $out[] = "ERROR: " . $params['error'];
  794. $out[] = "";
  795. if (php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) {
  796. echo implode("\n", $out);
  797. } else {
  798. echo implode("<br>\n", $out);
  799. }
  800. die;
  801. }
  802. function meekrodb_debugmode_handler($params) {
  803. echo "QUERY: " . $params['query'] . " [" . $params['runtime'] . " ms]";
  804. if (php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) {
  805. echo "\n";
  806. } else {
  807. echo "<br>\n";
  808. }
  809. }
  810. class MeekroDBEval {
  811. public $text = '';
  812. function __construct($text) {
  813. $this->text = $text;
  814. }
  815. }
  816. ?>