PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/Quản lý website tin tức PHP/discuznews/source/class/discuz/_25000003_discuz_database_b5b11f.bak.php

https://gitlab.com/phamngsinh/baitaplon_sinhvien
PHP | 458 lines | 410 code | 42 blank | 6 comment | 31 complexity | 7039289fe8fc8b2eb36e809181541f12 MD5 | raw file
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: discuz_database.php 31468 2012-08-31 02:27:23Z zhangguosheng $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. class discuz_database {
  12. public static $db;
  13. public static $driver;
  14. public static function init($driver, $config) {
  15. self::$driver = $driver;
  16. self::$db = new $driver;
  17. self::$db->set_config($config);
  18. self::$db->connect();
  19. }
  20. public static function object() {
  21. return self::$db;
  22. }
  23. public static function table($table) {
  24. return self::$db->table_name($table);
  25. }
  26. public static function delete($table, $condition, $limit = 0, $unbuffered = true) {
  27. if (empty($condition)) {
  28. return false;
  29. } elseif (is_array($condition)) {
  30. if (count($condition) == 2 && isset($condition['where']) && isset($condition['arg'])) {
  31. $where = self::format($condition['where'], $condition['arg']);
  32. } else {
  33. $where = self::implode_field_value($condition, ' AND ');
  34. }
  35. } else {
  36. $where = $condition;
  37. }
  38. $limit = dintval($limit);
  39. $sql = "DELETE FROM " . self::table($table) . " WHERE $where " . ($limit > 0 ? "LIMIT $limit" : '');
  40. return self::query($sql, ($unbuffered ? 'UNBUFFERED' : ''));
  41. }
  42. public static function insert($table, $data, $return_insert_id = false, $replace = false, $silent = false) {
  43. $sql = self::implode($data);
  44. $cmd = $replace ? 'REPLACE INTO' : 'INSERT INTO';
  45. $table = self::table($table);
  46. $silent = $silent ? 'SILENT' : '';
  47. return self::query("$cmd $table SET $sql", null, $silent, !$return_insert_id);
  48. }
  49. public static function update($table, $data, $condition, $unbuffered = false, $low_priority = false) {
  50. $sql = self::implode($data);
  51. if(empty($sql)) {
  52. return false;
  53. }
  54. $cmd = "UPDATE " . ($low_priority ? 'LOW_PRIORITY' : '');
  55. $table = self::table($table);
  56. $where = '';
  57. if (empty($condition)) {
  58. $where = '1';
  59. } elseif (is_array($condition)) {
  60. $where = self::implode($condition, ' AND ');
  61. } else {
  62. $where = $condition;
  63. }
  64. $res = self::query("$cmd $table SET $sql WHERE $where", $unbuffered ? 'UNBUFFERED' : '');
  65. return $res;
  66. }
  67. public static function insert_id() {
  68. return self::$db->insert_id();
  69. }
  70. public static function fetch($resourceid, $type = MYSQL_ASSOC) {
  71. return self::$db->fetch_array($resourceid, $type);
  72. }
  73. public static function fetch_first($sql, $arg = array(), $silent = false) {
  74. $res = self::query($sql, $arg, $silent, false);
  75. $ret = self::$db->fetch_array($res);
  76. self::$db->free_result($res);
  77. return $ret ? $ret : array();
  78. }
  79. public static function fetch_all($sql, $arg = array(), $keyfield = '', $silent=false) {
  80. $data = array();
  81. $query = self::query($sql, $arg, $silent, false);
  82. while ($row = self::$db->fetch_array($query)) {
  83. if ($keyfield && isset($row[$keyfield])) {
  84. $data[$row[$keyfield]] = $row;
  85. } else {
  86. $data[] = $row;
  87. }
  88. }
  89. self::$db->free_result($query);
  90. return $data;
  91. }
  92. public static function result($resourceid, $row = 0) {
  93. return self::$db->result($resourceid, $row);
  94. }
  95. public static function result_first($sql, $arg = array(), $silent = false) {
  96. $res = self::query($sql, $arg, $silent, false);
  97. $ret = self::$db->result($res, 0);
  98. self::$db->free_result($res);
  99. return $ret;
  100. }
  101. public static function query($sql, $arg = array(), $silent = false, $unbuffered = false) {
  102. if (!empty($arg)) {
  103. if (is_array($arg)) {
  104. $sql = self::format($sql, $arg);
  105. } elseif ($arg === 'SILENT') {
  106. $silent = true;
  107. } elseif ($arg === 'UNBUFFERED') {
  108. $unbuffered = true;
  109. }
  110. }
  111. self::checkquery($sql);
  112. $ret = self::$db->query($sql, $silent, $unbuffered);
  113. if (!$unbuffered && $ret) {
  114. $cmd = trim(strtoupper(substr($sql, 0, strpos($sql, ' '))));
  115. if ($cmd === 'SELECT') {
  116. } elseif ($cmd === 'UPDATE' || $cmd === 'DELETE') {
  117. $ret = self::$db->affected_rows();
  118. } elseif ($cmd === 'INSERT') {
  119. $ret = self::$db->insert_id();
  120. }
  121. }
  122. return $ret;
  123. }
  124. public static function num_rows($resourceid) {
  125. return self::$db->num_rows($resourceid);
  126. }
  127. public static function affected_rows() {
  128. return self::$db->affected_rows();
  129. }
  130. public static function free_result($query) {
  131. return self::$db->free_result($query);
  132. }
  133. public static function error() {
  134. return self::$db->error();
  135. }
  136. public static function errno() {
  137. return self::$db->errno();
  138. }
  139. public static function checkquery($sql) {
  140. return discuz_database_safecheck::checkquery($sql);
  141. }
  142. public static function quote($str, $noarray = false) {
  143. if (is_string($str))
  144. return '\'' . addcslashes($str, "\n\r\\'\"\032") . '\'';
  145. if (is_int($str) or is_float($str))
  146. return '\'' . $str . '\'';
  147. if (is_array($str)) {
  148. if($noarray === false) {
  149. foreach ($str as &$v) {
  150. $v = self::quote($v, true);
  151. }
  152. return $str;
  153. } else {
  154. return '\'\'';
  155. }
  156. }
  157. if (is_bool($str))
  158. return $str ? '1' : '0';
  159. return '\'\'';
  160. }
  161. public static function quote_field($field) {
  162. if (is_array($field)) {
  163. foreach ($field as $k => $v) {
  164. $field[$k] = self::quote_field($v);
  165. }
  166. } else {
  167. if (strpos($field, '`') !== false)
  168. $field = str_replace('`', '', $field);
  169. $field = '`' . $field . '`';
  170. }
  171. return $field;
  172. }
  173. public static function limit($start, $limit = 0) {
  174. $limit = intval($limit > 0 ? $limit : 0);
  175. $start = intval($start > 0 ? $start : 0);
  176. if ($start > 0 && $limit > 0) {
  177. return " LIMIT $start, $limit";
  178. } elseif ($limit) {
  179. return " LIMIT $limit";
  180. } elseif ($start) {
  181. return " LIMIT $start";
  182. } else {
  183. return '';
  184. }
  185. }
  186. public static function order($field, $order = 'ASC') {
  187. if(empty($field)) {
  188. return '';
  189. }
  190. $order = strtoupper($order) == 'ASC' || empty($order) ? 'ASC' : 'DESC';
  191. return self::quote_field($field) . ' ' . $order;
  192. }
  193. public static function field($field, $val, $glue = '=') {
  194. $field = self::quote_field($field);
  195. if (is_array($val)) {
  196. $glue = $glue == 'notin' ? 'notin' : 'in';
  197. } elseif ($glue == 'in') {
  198. $glue = '=';
  199. }
  200. switch ($glue) {
  201. case '=':
  202. return $field . $glue . self::quote($val);
  203. break;
  204. case '-':
  205. case '+':
  206. return $field . '=' . $field . $glue . self::quote((string) $val);
  207. break;
  208. case '|':
  209. case '&':
  210. case '^':
  211. return $field . '=' . $field . $glue . self::quote($val);
  212. break;
  213. case '>':
  214. case '<':
  215. case '<>':
  216. case '<=':
  217. case '>=':
  218. return $field . $glue . self::quote($val);
  219. break;
  220. case 'like':
  221. return $field . ' LIKE(' . self::quote($val) . ')';
  222. break;
  223. case 'in':
  224. case 'notin':
  225. $val = $val ? implode(',', self::quote($val)) : '\'\'';
  226. return $field . ($glue == 'notin' ? ' NOT' : '') . ' IN(' . $val . ')';
  227. break;
  228. default:
  229. throw new DbException('Not allow this glue between field and value: "' . $glue . '"');
  230. }
  231. }
  232. public static function implode($array, $glue = ',') {
  233. $sql = $comma = '';
  234. $glue = ' ' . trim($glue) . ' ';
  235. foreach ($array as $k => $v) {
  236. $sql .= $comma . self::quote_field($k) . '=' . self::quote($v);
  237. $comma = $glue;
  238. }
  239. return $sql;
  240. }
  241. public static function implode_field_value($array, $glue = ',') {
  242. return self::implode($array, $glue);
  243. }
  244. public static function format($sql, $arg) {
  245. $count = substr_count($sql, '%');
  246. if (!$count) {
  247. return $sql;
  248. } elseif ($count > count($arg)) {
  249. throw new DbException('SQL string format error! This SQL need "' . $count . '" vars to replace into.', 0, $sql);
  250. }
  251. $len = strlen($sql);
  252. $i = $find = 0;
  253. $ret = '';
  254. while ($i <= $len && $find < $count) {
  255. if ($sql{$i} == '%') {
  256. $next = $sql{$i + 1};
  257. if ($next == 't') {
  258. $ret .= self::table($arg[$find]);
  259. } elseif ($next == 's') {
  260. $ret .= self::quote(is_array($arg[$find]) ? serialize($arg[$find]) : (string) $arg[$find]);
  261. } elseif ($next == 'f') {
  262. $ret .= sprintf('%F', $arg[$find]);
  263. } elseif ($next == 'd') {
  264. $ret .= dintval($arg[$find]);
  265. } elseif ($next == 'i') {
  266. $ret .= $arg[$find];
  267. } elseif ($next == 'n') {
  268. if (!empty($arg[$find])) {
  269. $ret .= is_array($arg[$find]) ? implode(',', self::quote($arg[$find])) : self::quote($arg[$find]);
  270. } else {
  271. $ret .= '0';
  272. }
  273. } else {
  274. $ret .= self::quote($arg[$find]);
  275. }
  276. $i++;
  277. $find++;
  278. } else {
  279. $ret .= $sql{$i};
  280. }
  281. $i++;
  282. }
  283. if ($i < $len) {
  284. $ret .= substr($sql, $i);
  285. }
  286. return $ret;
  287. }
  288. }
  289. class discuz_database_safecheck {
  290. protected static $checkcmd = array('SELECT', 'UPDATE', 'INSERT', 'REPLACE', 'DELETE');
  291. protected static $config;
  292. public static function checkquery($sql) {
  293. if (self::$config === null) {
  294. self::$config = getglobal('config/security/querysafe');
  295. }
  296. if (self::$config['status']) {
  297. $cmd = trim(strtoupper(substr($sql, 0, strpos($sql, ' '))));
  298. if (in_array($cmd, self::$checkcmd)) {
  299. $test = self::_do_query_safe($sql);
  300. if ($test < 1) {
  301. throw new DbException('It is not safe to do this query', 0, $sql);
  302. }
  303. }
  304. }
  305. return true;
  306. }
  307. private static function _do_query_safe($sql) {
  308. $sql = str_replace(array('\\\\', '\\\'', '\\"', '\'\''), '', $sql);
  309. $mark = $clean = '';
  310. if (strpos($sql, '/') === false && strpos($sql, '#') === false && strpos($sql, '-- ') === false) {
  311. $clean = preg_replace("/'(.+?)'/s", '', $sql);
  312. } else {
  313. $len = strlen($sql);
  314. $mark = $clean = '';
  315. for ($i = 0; $i < $len; $i++) {
  316. $str = $sql[$i];
  317. switch ($str) {
  318. case '\'':
  319. if (!$mark) {
  320. $mark = '\'';
  321. $clean .= $str;
  322. } elseif ($mark == '\'') {
  323. $mark = '';
  324. }
  325. break;
  326. case '/':
  327. if (empty($mark) && $sql[$i + 1] == '*') {
  328. $mark = '/*';
  329. $clean .= $mark;
  330. $i++;
  331. } elseif ($mark == '/*' && $sql[$i - 1] == '*') {
  332. $mark = '';
  333. $clean .= '*';
  334. }
  335. break;
  336. case '#':
  337. if (empty($mark)) {
  338. $mark = $str;
  339. $clean .= $str;
  340. }
  341. break;
  342. case "\n":
  343. if ($mark == '#' || $mark == '--') {
  344. $mark = '';
  345. }
  346. break;
  347. case '-':
  348. if (empty($mark) && substr($sql, $i, 3) == '-- ') {
  349. $mark = '-- ';
  350. $clean .= $mark;
  351. }
  352. break;
  353. default:
  354. break;
  355. }
  356. $clean .= $mark ? '' : $str;
  357. }
  358. }
  359. $clean = preg_replace("/[^a-z0-9_\-\(\)#\*\/\"]+/is", "", strtolower($clean));
  360. if (self::$config['afullnote']) {
  361. $clean = str_replace('/**/', '', $clean);
  362. }
  363. if (is_array(self::$config['dfunction'])) {
  364. foreach (self::$config['dfunction'] as $fun) {
  365. if (strpos($clean, $fun . '(') !== false)
  366. return '-1';
  367. }
  368. }
  369. if (is_array(self::$config['daction'])) {
  370. foreach (self::$config['daction'] as $action) {
  371. if (strpos($clean, $action) !== false)
  372. return '-3';
  373. }
  374. }
  375. if (self::$config['dlikehex'] && strpos($clean, 'like0x')) {
  376. return '-2';
  377. }
  378. if (is_array(self::$config['dnote'])) {
  379. foreach (self::$config['dnote'] as $note) {
  380. if (strpos($clean, $note) !== false)
  381. return '-4';
  382. }
  383. }
  384. return 1;
  385. }
  386. public static function setconfigstatus($data) {
  387. self::$config['status'] = $data ? 1 : 0;
  388. }
  389. }
  390. ?>