discuz_database.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Discuz! Team
  4. * This is NOT a freeware, use is subject to license terms
  5. * https://license.discuz.vip
  6. */
  7. if(!defined('IN_DISCUZ')) {
  8. exit('Access Denied');
  9. }
  10. class discuz_database {
  11. public static $db;
  12. public static $driver;
  13. public static function init($driver, $config) {
  14. self::$driver = $driver;
  15. self::$db = new $driver;
  16. self::$db->set_config($config);
  17. self::$db->connect();
  18. }
  19. public static function object() {
  20. return self::$db;
  21. }
  22. public static function table($table) {
  23. return self::$db->table_name($table);
  24. }
  25. public static function delete($table, $condition, $limit = 0, $unbuffered = true) {
  26. $arg = null;
  27. if(empty($condition)) {
  28. return false;
  29. } elseif(is_array($condition)) {
  30. if(count($condition) == 2 && isset($condition['where']) && isset($condition['arg'])) {
  31. if(!self::is_pdo()) {
  32. $where = self::format($condition['where'], $condition['arg']);
  33. } else {
  34. $where = self::format_prepared($condition['where'], $condition['arg']);
  35. }
  36. } else {
  37. if(!self::is_pdo()) {
  38. $where = self::implode_field_value($condition, ' AND ');
  39. } else {
  40. $where = self::implode_field_value_prepared($condition, $arg, ' AND ');
  41. }
  42. }
  43. } else {
  44. $where = $condition;
  45. }
  46. $limit = dintval($limit);
  47. $sql = 'DELETE FROM '.self::table($table)." WHERE $where ".($limit > 0 ? "LIMIT $limit" : '');
  48. return self::query($sql, $arg, false, $unbuffered);
  49. }
  50. public static function insert($table, $data, $return_insert_id = false, $replace = false, $silent = false) {
  51. if(!self::is_pdo()) {
  52. $sql = 'SET '.self::implode($data);
  53. $arg = null;
  54. } else {
  55. $arg = [];
  56. $sql = self::implode_prepared_insert($data, $arg);
  57. }
  58. $cmd = $replace ? 'REPLACE INTO' : 'INSERT INTO';
  59. $table = self::table($table);
  60. $silent = $silent ? 'SILENT' : '';
  61. return self::query("$cmd $table $sql", $arg, $silent, !$return_insert_id);
  62. }
  63. public static function update($table, $data, $condition = '', $unbuffered = false, $low_priority = false) {
  64. if(!self::is_pdo()) {
  65. $sql = self::implode($data);
  66. $arg = null;
  67. } else {
  68. $arg = [];
  69. $sql = self::implode_prepared($data, $arg);
  70. }
  71. if(empty($sql)) {
  72. return false;
  73. }
  74. $cmd = 'UPDATE '.($low_priority ? 'LOW_PRIORITY' : '');
  75. $table = self::table($table);
  76. $where = '';
  77. if(empty($condition)) {
  78. $where = '1';
  79. } elseif(is_array($condition)) {
  80. if(!self::is_pdo()) {
  81. $where = self::implode($condition, ' AND ');
  82. } else {
  83. $where = self::implode_prepared($condition, $arg, ' AND ');
  84. }
  85. } else {
  86. $where = $condition;
  87. }
  88. $res = self::query("$cmd $table SET $sql WHERE $where", $arg, false, $unbuffered);
  89. return $res;
  90. }
  91. public static function insert_id() {
  92. return self::$db->insert_id();
  93. }
  94. public static function fetch($resourceid, $type = null) {
  95. if(!isset($type)) {
  96. $type = constant('MYSQLI_ASSOC');
  97. }
  98. return self::$db->fetch_array($resourceid, $type);
  99. }
  100. public static function fetch_first($sql, $arg = [], $silent = false) {
  101. $res = self::query($sql, $arg, $silent, false);
  102. if($res === 0) {
  103. return [];
  104. }
  105. $ret = self::$db->fetch_array($res);
  106. self::$db->free_result($res);
  107. return $ret ? $ret : [];
  108. }
  109. public static function fetch_all($sql, $arg = [], $keyfield = '', $silent = false) {
  110. $data = [];
  111. $query = self::query($sql, $arg, $silent, false);
  112. while($row = self::$db->fetch_array($query)) {
  113. if($keyfield && isset($row[$keyfield])) {
  114. $data[$row[$keyfield]] = $row;
  115. } else {
  116. $data[] = $row;
  117. }
  118. }
  119. self::$db->free_result($query);
  120. return $data;
  121. }
  122. public static function result($resourceid, $row = 0) {
  123. return self::$db->result($resourceid, $row);
  124. }
  125. public static function result_first($sql, $arg = [], $silent = false) {
  126. $res = self::query($sql, $arg, $silent, false);
  127. $ret = self::$db->result($res, 0);
  128. self::$db->free_result($res);
  129. return $ret;
  130. }
  131. public static function query($sql, $arg = [], $silent = false, $unbuffered = false) {
  132. if(!empty($arg)) {
  133. if(is_array($arg)) {
  134. if(!self::is_pdo()) {
  135. $sql = self::format($sql, $arg);
  136. } else {
  137. $sql = self::format_prepared($sql, $arg);
  138. }
  139. } elseif($arg === 'SILENT') {
  140. $silent = true;
  141. $arg = [];
  142. } elseif($arg === 'UNBUFFERED') {
  143. $unbuffered = true;
  144. $arg = [];
  145. }
  146. }
  147. self::checkquery($sql);
  148. $ret = self::$db->query(!self::is_pdo() ? $sql : [$sql, $arg], $silent, $unbuffered);
  149. if(!$unbuffered && $ret) {
  150. $cmd = trim(strtoupper(substr($sql, 0, strpos($sql, ' '))));
  151. if($cmd === 'SELECT') {
  152. } elseif($cmd === 'UPDATE' || $cmd === 'DELETE') {
  153. $ret = self::$db->affected_rows();
  154. } elseif($cmd === 'INSERT') {
  155. $ret = self::$db->insert_id();
  156. }
  157. }
  158. return $ret;
  159. }
  160. public static function num_rows($resourceid) {
  161. return self::$db->num_rows($resourceid);
  162. }
  163. public static function affected_rows() {
  164. return self::$db->affected_rows();
  165. }
  166. public static function free_result($query) {
  167. return self::$db->free_result($query);
  168. }
  169. public static function error() {
  170. return self::$db->error();
  171. }
  172. public static function errno() {
  173. return self::$db->errno();
  174. }
  175. public static function checkquery($sql) {
  176. return discuz_database_safecheck::checkquery($sql);
  177. }
  178. public static function quote($str, $noarray = false) {
  179. if(is_string($str))
  180. return '\''.self::$db->escape_string($str).'\'';
  181. if(is_int($str) or is_float($str))
  182. return '\''.$str.'\'';
  183. if(is_array($str)) {
  184. if($noarray === false) {
  185. foreach($str as &$v) {
  186. $v = self::quote($v, true);
  187. }
  188. return $str;
  189. } else {
  190. return '\'\'';
  191. }
  192. }
  193. if(is_bool($str))
  194. return $str ? '1' : '0';
  195. return '\'\'';
  196. }
  197. public static function quote_field($field) {
  198. if(is_array($field)) {
  199. foreach($field as $k => $v) {
  200. $field[$k] = self::quote_field($v);
  201. }
  202. } else {
  203. if(str_contains($field, '`'))
  204. $field = str_replace('`', '', $field);
  205. $field = '`'.$field.'`';
  206. }
  207. return $field;
  208. }
  209. public static function limit($start, $limit = 0) {
  210. $limit = intval($limit > 0 ? $limit : 0);
  211. $start = intval($start > 0 ? $start : 0);
  212. if($start > 0 && $limit > 0) {
  213. return " LIMIT $start, $limit";
  214. } elseif($limit) {
  215. return " LIMIT $limit";
  216. } elseif($start) {
  217. return " LIMIT $start";
  218. } else {
  219. return '';
  220. }
  221. }
  222. public static function order($field, $order = 'ASC') {
  223. if(empty($field)) {
  224. return '';
  225. }
  226. $order = strtoupper($order) == 'ASC' || empty($order) ? 'ASC' : 'DESC';
  227. return self::quote_field($field).' '.$order;
  228. }
  229. public static function field($field, $val, $glue = '=') {
  230. $field = self::quote_field($field);
  231. if(is_array($val)) {
  232. $glue = $glue == 'notin' ? 'notin' : 'in';
  233. } elseif($glue == 'in') {
  234. $glue = '=';
  235. }
  236. switch($glue) {
  237. case '>=':
  238. case '=':
  239. return $field.$glue.self::quote($val);
  240. break;
  241. case '-':
  242. case '+':
  243. return $field.'='.$field.$glue.self::quote((string)$val);
  244. break;
  245. case '|':
  246. case '&':
  247. case '^':
  248. case '&~':
  249. return $field.'='.$field.$glue.self::quote($val);
  250. break;
  251. case '>':
  252. case '<':
  253. case '<>':
  254. case '<=':
  255. return $field.$glue.self::quote($val);
  256. break;
  257. case 'like':
  258. return $field.' LIKE('.self::quote($val).')';
  259. break;
  260. case 'in':
  261. case 'notin':
  262. $val = $val ? implode(',', self::quote($val)) : '\'\'';
  263. return $field.($glue == 'notin' ? ' NOT' : '').' IN('.$val.')';
  264. break;
  265. default:
  266. throw new DbException('Not allow this glue between field and value: "'.$glue.'"');
  267. }
  268. }
  269. public static function is_pdo() {
  270. return getglobal('db_driver') == 'pdo';
  271. }
  272. public static function implode($array, $glue = ',') {
  273. $sql = $comma = '';
  274. $glue = ' '.trim($glue).' ';
  275. foreach($array as $k => $v) {
  276. $sql .= $comma.self::quote_field($k).'='.self::quote($v);
  277. $comma = $glue;
  278. }
  279. return $sql;
  280. }
  281. public static function implode_prepared_insert($array, &$arg, $glue = ',') {
  282. $sql1 = $sql2 = $comma1 = $comma2 = '';
  283. $glue = ' '.trim($glue).' ';
  284. foreach($array as $k => $v) {
  285. $sql1 .= $comma1.self::quote_field($k);
  286. $sql2 .= $comma2.'?';
  287. $arg[] = !is_object($v) ? $v : '';
  288. $comma1 = $glue;
  289. $comma2 = $glue;
  290. }
  291. return '('.$sql1.') VALUES ('.$sql2.')';
  292. }
  293. public static function implode_prepared($array, &$arg, $glue = ',') {
  294. $sql = $comma = '';
  295. $glue = ' '.trim($glue).' ';
  296. foreach($array as $k => $v) {
  297. $sql .= $comma.self::quote_field($k).'=?';
  298. $arg[] = !is_object($v) ? $v : '';
  299. $comma = $glue;
  300. }
  301. return $sql;
  302. }
  303. public static function implode_field_value($array, $glue = ',') {
  304. return self::implode($array, $glue);
  305. }
  306. public static function implode_field_value_prepared($array, &$arg, $glue = ',') {
  307. return self::implode_prepared($array, $arg, $glue);
  308. }
  309. public static function format($sql, $arg) {
  310. $count = substr_count($sql, '%');
  311. if(!$count) {
  312. return $sql;
  313. } elseif($count > count($arg)) {
  314. throw new DbException('SQL string format error! This SQL need "'.$count.'" vars to replace into.', 0, $sql);
  315. }
  316. $len = strlen($sql);
  317. $i = $find = 0;
  318. $ret = '';
  319. while($i <= $len && $find < $count) {
  320. if($sql[$i] == '%') {
  321. $next = $sql[$i + 1];
  322. if($next == 't') {
  323. $ret .= self::table($arg[$find]);
  324. } elseif($next == 's') {
  325. $ret .= self::quote(is_array($arg[$find]) ? serialize($arg[$find]) : (string)$arg[$find]);
  326. } elseif($next == 'f') {
  327. $ret .= sprintf('%F', $arg[$find]);
  328. } elseif($next == 'd') {
  329. $ret .= dintval($arg[$find]);
  330. } elseif($next == 'i') {
  331. $ret .= $arg[$find];
  332. } elseif($next == 'n') {
  333. if(!empty($arg[$find])) {
  334. $ret .= is_array($arg[$find]) ? implode(',', self::quote($arg[$find])) : self::quote($arg[$find]);
  335. } else {
  336. $ret .= '0';
  337. }
  338. } else {
  339. $ret .= self::quote($arg[$find]);
  340. }
  341. $i++;
  342. $find++;
  343. } else {
  344. $ret .= $sql[$i];
  345. }
  346. $i++;
  347. }
  348. if($i < $len) {
  349. $ret .= substr($sql, $i);
  350. }
  351. return $ret;
  352. }
  353. public static function format_prepared($sql, &$arg) {
  354. $params = [];
  355. $count = substr_count($sql, '%');
  356. if(!$count) {
  357. return $sql;
  358. } elseif($count > count($arg)) {
  359. throw new DbException('SQL string format error! This SQL need "'.$count.'" vars to replace into.', 0, $sql);
  360. }
  361. $len = strlen($sql);
  362. $i = $find = 0;
  363. $ret = '';
  364. while($i <= $len && $find < $count) {
  365. if($sql[$i] == '%') {
  366. $next = $sql[$i + 1];
  367. $v = $arg[$find];
  368. if($next == 't') {
  369. $ret .= self::table($v);
  370. } elseif($next == 's') {
  371. $v = is_array($v) ? serialize($v) : (string)$v;
  372. $ret .= '?';
  373. $params[] = $v;
  374. } elseif($next == 'f') {
  375. $ret .= sprintf('%F', $v);
  376. } elseif($next == 'd') {
  377. $ret .= dintval($v);
  378. } elseif($next == 'i') {
  379. $ret .= $v;
  380. } elseif($next == 'n') {
  381. if(!empty($v)) {
  382. if(is_array($v)) {
  383. $_ret = [];
  384. foreach($v as $_v) {
  385. $_ret[] = '?';
  386. $params[] = $_v;
  387. }
  388. $ret .= implode(',', $_ret);
  389. } else {
  390. $ret .= '?';
  391. $params[] = $v;
  392. }
  393. } else {
  394. $ret .= '0';
  395. }
  396. } else {
  397. $ret .= '?';
  398. $params[] = $v;
  399. }
  400. $i++;
  401. $find++;
  402. } else {
  403. $ret .= $sql[$i];
  404. }
  405. $i++;
  406. }
  407. if($i < $len) {
  408. $ret .= substr($sql, $i);
  409. }
  410. $arg = $params;
  411. return $ret;
  412. }
  413. public static function begin_transaction() {
  414. return self::$db->begin_transaction();
  415. }
  416. public static function commit() {
  417. return self::$db->commit();
  418. }
  419. public static function rollback() {
  420. return self::$db->rollback();
  421. }
  422. }
  423. class discuz_database_safecheck {
  424. protected static $checkcmd = ['SEL' => 1, 'UPD' => 1, 'INS' => 1, 'REP' => 1, 'DEL' => 1];
  425. protected static $config;
  426. public static function checkquery($sql) {
  427. if(is_array($sql)) {
  428. $sql = $sql[0];
  429. }
  430. if(self::$config === null) {
  431. self::$config = getglobal('config/security/querysafe');
  432. }
  433. if(self::$config['status']) {
  434. $check = 1;
  435. $cmd = strtoupper(substr(trim($sql), 0, 3));
  436. if(isset(self::$checkcmd[$cmd])) {
  437. $check = self::_do_query_safe($sql);
  438. } elseif(str_starts_with($cmd, '/*')) {
  439. $check = -1;
  440. }
  441. if($check < 1) {
  442. throw new DbException('It is not safe to do this query', 0, $sql);
  443. }
  444. }
  445. return true;
  446. }
  447. private static function _do_query_safe($sql) {
  448. $sql = str_replace(['\\\\', '\\\'', '\\"', '\'\''], '', $sql);
  449. $mark = $clean = '';
  450. if(!str_contains($sql, '/') && !str_contains($sql, '#') && !str_contains($sql, '-- ') && !str_contains($sql, '@') && !str_contains($sql, '`') && !str_contains($sql, '"')) {
  451. $clean = preg_replace("/'(.+?)'/s", '', $sql);
  452. } else {
  453. $len = strlen($sql);
  454. $mark = $clean = '';
  455. for($i = 0; $i < $len; $i++) {
  456. $str = $sql[$i];
  457. switch($str) {
  458. case '`':
  459. if(!$mark) {
  460. $mark = '`';
  461. $clean .= $str;
  462. } elseif($mark == '`') {
  463. $mark = '';
  464. }
  465. break;
  466. case '\'':
  467. if(!$mark) {
  468. $mark = '\'';
  469. $clean .= $str;
  470. } elseif($mark == '\'') {
  471. $mark = '';
  472. }
  473. break;
  474. case '/':
  475. if(empty($mark) && $sql[$i + 1] == '*') {
  476. $mark = '/*';
  477. $clean .= $mark;
  478. $i++;
  479. } elseif($mark == '/*' && $sql[$i - 1] == '*') {
  480. $mark = '';
  481. $clean .= '*';
  482. }
  483. break;
  484. case '#':
  485. if(empty($mark)) {
  486. $mark = $str;
  487. $clean .= $str;
  488. }
  489. break;
  490. case "\n":
  491. if($mark == '#' || $mark == '--') {
  492. $mark = '';
  493. }
  494. break;
  495. case '-':
  496. if(empty($mark) && substr($sql, $i, 3) == '-- ') {
  497. $mark = '-- ';
  498. $clean .= $mark;
  499. }
  500. break;
  501. default:
  502. break;
  503. }
  504. $clean .= $mark ? '' : $str;
  505. }
  506. }
  507. if(str_contains($clean, '@')) {
  508. return '-3';
  509. }
  510. $clean = preg_replace("/[^a-z0-9_\-\(\)#\*\/\"]+/is", '', strtolower($clean));
  511. if(self::$config['afullnote']) {
  512. $clean = str_replace('/**/', '', $clean);
  513. }
  514. if(is_array(self::$config['dfunction'])) {
  515. foreach(self::$config['dfunction'] as $fun) {
  516. if(str_contains($clean, $fun.'('))
  517. return '-1';
  518. }
  519. }
  520. if(is_array(self::$config['daction'])) {
  521. foreach(self::$config['daction'] as $action) {
  522. if(str_contains($clean, $action))
  523. return '-3';
  524. }
  525. }
  526. if(self::$config['dlikehex'] && strpos($clean, 'like0x')) {
  527. return '-2';
  528. }
  529. if(is_array(self::$config['dnote'])) {
  530. foreach(self::$config['dnote'] as $note) {
  531. if(str_contains($clean, $note))
  532. return '-4';
  533. }
  534. }
  535. return 1;
  536. }
  537. public static function setconfigstatus($data) {
  538. self::$config['status'] = $data ? 1 : 0;
  539. }
  540. }
  541. class discuz_database_prepared {
  542. }