国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

669 linhas
22KB

  1. <?php if (!defined('DEDEINC')) exit("Request Error!");
  2. /**
  3. * 数据库类
  4. * 说明:系统底层数据库核心类
  5. * 调用这个类前,请先设定这些外部变量
  6. * $GLOBALS['cfg_dbhost'];
  7. * $GLOBALS['cfg_dbuser'];
  8. * $GLOBALS['cfg_dbpwd'];
  9. * $GLOBALS['cfg_dbname'];
  10. * $GLOBALS['cfg_dbprefix'];
  11. *
  12. * @version $Id: dedesqli.class.php 1 15:00 2011-1-21 tianya $
  13. * @package DedeBIZ.Libraries
  14. * @copyright Copyright (c) 2020, DedeBIZ.COM
  15. * @license https://www.dedebiz.com/license
  16. * @link https://www.dedebiz.com
  17. */
  18. @set_time_limit(0);
  19. // 在工程所有文件中均不需要单独初始化这个类,可直接用 $dsql 或 $db 进行操作
  20. // 为了防止错误,操作完后不必关闭数据库
  21. if (!function_exists("mysqli_init")) {
  22. echo "DedeBIZ提示:尚未发现开启mysqli模块,请在php.ini中启用`extension=mysqli`。";
  23. exit;
  24. }
  25. $dsql = $dsqli = $db = new DedeSqli(FALSE);
  26. /**
  27. * Dede MySQLi数据库类
  28. *
  29. * @package DedeSqli
  30. * @subpackage DedeBIZ.Libraries
  31. * @link https://www.dedebiz.com
  32. */
  33. if (!defined('MYSQL_BOTH')) {
  34. define('MYSQL_BOTH', MYSQLI_BOTH);
  35. }
  36. if (!defined('MYSQL_ASSOC')) {
  37. define('MYSQL_ASSOC', MYSQLI_ASSOC);
  38. }
  39. class DedeSqli
  40. {
  41. var $linkID;
  42. var $dbHost;
  43. var $dbUser;
  44. var $dbPwd;
  45. var $dbName;
  46. var $dbPrefix;
  47. var $result;
  48. var $queryString;
  49. var $parameters;
  50. var $isClose;
  51. var $safeCheck;
  52. var $showError = false;
  53. var $recordLog = false; // 记录日志到data/mysqli_record_log.inc便于进行调试
  54. var $isInit = false;
  55. var $pconnect = false;
  56. //用外部定义的变量初始类,并连接数据库
  57. function __construct($pconnect = FALSE, $nconnect = FALSE)
  58. {
  59. $this->isClose = FALSE;
  60. $this->safeCheck = TRUE;
  61. $this->pconnect = $pconnect;
  62. if ($nconnect) {
  63. $this->Init($pconnect);
  64. }
  65. }
  66. function DedeSql($pconnect = FALSE, $nconnect = TRUE)
  67. {
  68. $this->__construct($pconnect, $nconnect);
  69. }
  70. function Init($pconnect = FALSE)
  71. {
  72. $this->linkID = 0;
  73. //$this->queryString = '';
  74. //$this->parameters = Array();
  75. $this->dbHost = $GLOBALS['cfg_dbhost'];
  76. $this->dbUser = $GLOBALS['cfg_dbuser'];
  77. $this->dbPwd = $GLOBALS['cfg_dbpwd'];
  78. $this->dbName = $GLOBALS['cfg_dbname'];
  79. $this->dbPrefix = $GLOBALS['cfg_dbprefix'];
  80. $this->result["me"] = 0;
  81. $this->Open($pconnect);
  82. }
  83. //用指定参数初始数据库信息
  84. function SetSource($host, $username, $pwd, $dbname, $dbprefix = "dede_")
  85. {
  86. $this->dbHost = $host;
  87. $this->dbUser = $username;
  88. $this->dbPwd = $pwd;
  89. $this->dbName = $dbname;
  90. $this->dbPrefix = $dbprefix;
  91. $this->result["me"] = 0;
  92. }
  93. function SelectDB($dbname)
  94. {
  95. mysqli_select_db($this->linkID, $dbname);
  96. }
  97. //设置SQL里的参数
  98. function SetParameter($key, $value)
  99. {
  100. $this->parameters[$key] = $value;
  101. }
  102. //连接数据库
  103. function Open($pconnect = FALSE)
  104. {
  105. global $dsqli;
  106. //连接数据库
  107. if ($dsqli && !$dsqli->isClose && $dsqli->isInit) {
  108. $this->linkID = $dsqli->linkID;
  109. } else {
  110. $i = 0;
  111. @list($dbhost, $dbport) = explode(':', $this->dbHost);
  112. !$dbport && $dbport = 3306;
  113. $this->linkID = mysqli_init();
  114. mysqli_real_connect($this->linkID, $dbhost, $this->dbUser, $this->dbPwd, false, $dbport);
  115. mysqli_errno($this->linkID) != 0 && $this->DisplayError('DedeBIZ错误警告: 链接(' . $this->pconnect . ') 到MySQL发生错误');
  116. //复制一个对象副本
  117. CopySQLiPoint($this);
  118. }
  119. //处理错误,成功连接则选择数据库
  120. if (!$this->linkID) {
  121. $this->DisplayError("DedeBIZ错误警告:<font color='red'>连接数据库失败,可能数据库密码不对或数据库服务器出错!</font>");
  122. exit();
  123. }
  124. $this->isInit = TRUE;
  125. $serverinfo = mysqli_get_server_info($this->linkID);
  126. if (version_compare($serverinfo, '4.1', ">=") && $GLOBALS['cfg_db_language']) {
  127. mysqli_query($this->linkID, "SET character_set_connection=" . $GLOBALS['cfg_db_language'] . ",character_set_results=" . $GLOBALS['cfg_db_language'] . ",character_set_client=binary");
  128. }
  129. if ($serverinfo > '5.0') {
  130. mysqli_query($this->linkID, "SET sql_mode=''");
  131. }
  132. if ($this->dbName && !@mysqli_select_db($this->linkID, $this->dbName)) {
  133. $this->DisplayError('无法使用数据库');
  134. }
  135. return TRUE;
  136. }
  137. //为了防止采集等需要较长运行时间的程序超时,在运行这类程序时设置系统等待和交互时间
  138. function SetLongLink()
  139. {
  140. if ($this->linkID) {
  141. @mysqli_query($this->linkID, "SET interactive_timeout=3600, wait_timeout=3600 ;");
  142. }
  143. }
  144. //获得错误描述
  145. function GetError()
  146. {
  147. $str = mysqli_error($this->linkID);
  148. return $str;
  149. }
  150. //关闭数据库
  151. //mysql能自动管理非持久连接的连接池
  152. //实际上关闭并无意义并且容易出错,所以取消这函数
  153. function Close($isok = FALSE)
  154. {
  155. $this->FreeResultAll();
  156. if ($isok) {
  157. @mysqli_close($this->linkID);
  158. $this->isClose = TRUE;
  159. $GLOBALS['dsql'] = NULL;
  160. }
  161. }
  162. //定期清理死连接
  163. function ClearErrLink()
  164. {
  165. }
  166. //关闭指定的数据库连接
  167. function CloseLink($dblink)
  168. {
  169. @mysqli_close($dblink);
  170. }
  171. function Esc($_str)
  172. {
  173. if (version_compare(phpversion(), '4.3.0', '>=')) {
  174. return @mysqli_real_escape_string($this->linkID, $_str);
  175. } else {
  176. return @mysqli_escape_string($this->linkID, $_str);
  177. }
  178. }
  179. //执行一个不返回结果的SQL语句,如update,delete,insert等
  180. function ExecuteNoneQuery($sql = '')
  181. {
  182. global $dsqli;
  183. if (!$dsqli->isInit) {
  184. $this->Init($this->pconnect);
  185. }
  186. if ($dsqli->isClose) {
  187. $this->Open(FALSE);
  188. $dsqli->isClose = FALSE;
  189. }
  190. if (!empty($sql)) {
  191. $this->SetQuery($sql);
  192. } else {
  193. return FALSE;
  194. }
  195. if (is_array($this->parameters)) {
  196. foreach ($this->parameters as $key => $value) {
  197. $this->queryString = str_replace("@" . $key, "'$value'", $this->queryString);
  198. }
  199. }
  200. //SQL语句安全检查
  201. if ($this->safeCheck) CheckSql($this->queryString, 'update');
  202. $t1 = ExecTime();
  203. $rs = mysqli_query($this->linkID, $this->queryString);
  204. //查询性能测试
  205. if ($this->recordLog) {
  206. $queryTime = ExecTime() - $t1;
  207. $this->RecordLog($queryTime);
  208. }
  209. if (DEBUG_LEVEL === TRUE) {
  210. $queryTime = ExecTime() - $t1;
  211. echo "<div style='width:98%;margin:1rem auto;color: #155724;background-color: #d4edda;border-color: #c3e6cb;position: relative;padding: .75rem 1.25rem;border: 1px solid transparent;border-radius: .25rem;'>执行SQL:" . $this->queryString . ",执行时间:<b>{$queryTime}</b></div>\r\n";
  212. }
  213. return $rs;
  214. }
  215. //执行一个返回影响记录条数的SQL语句,如update,delete,insert等
  216. function ExecuteNoneQuery2($sql = '')
  217. {
  218. global $dsqli;
  219. if (!$dsqli->isInit) {
  220. $this->Init($this->pconnect);
  221. }
  222. if ($dsqli->isClose) {
  223. $this->Open(FALSE);
  224. $dsqli->isClose = FALSE;
  225. }
  226. if (!empty($sql)) {
  227. $this->SetQuery($sql);
  228. }
  229. if (is_array($this->parameters)) {
  230. foreach ($this->parameters as $key => $value) {
  231. $this->queryString = str_replace("@" . $key, "'$value'", $this->queryString);
  232. }
  233. }
  234. $t1 = ExecTime();
  235. mysqli_query($this->linkID, $this->queryString);
  236. //查询性能测试
  237. if ($this->recordLog) {
  238. $queryTime = ExecTime() - $t1;
  239. $this->RecordLog($queryTime);
  240. //echo $this->queryString."--{$queryTime}<hr />\r\n";
  241. }
  242. if (DEBUG_LEVEL === TRUE) {
  243. $queryTime = ExecTime() - $t1;
  244. echo "<div style='width:98%;margin:1rem auto;color: #155724;background-color: #d4edda;border-color: #c3e6cb;position: relative;padding: .75rem 1.25rem;border: 1px solid transparent;border-radius: .25rem;'>执行SQL:" . $this->queryString . ",执行时间:<b>{$queryTime}</b></div>\r\n";
  245. }
  246. return mysqli_affected_rows($this->linkID);
  247. }
  248. function ExecNoneQuery($sql = '')
  249. {
  250. return $this->ExecuteNoneQuery($sql);
  251. }
  252. function GetFetchRow($id = 'me')
  253. {
  254. return @mysqli_fetch_row($this->result[$id]);
  255. }
  256. function GetAffectedRows()
  257. {
  258. return mysqli_affected_rows($this->linkID);
  259. }
  260. //执行一个带返回结果的SQL语句,如SELECT,SHOW等
  261. function Execute($id = "me", $sql = '')
  262. {
  263. global $dsqli;
  264. if (!$dsqli->isInit) {
  265. $this->Init($this->pconnect);
  266. }
  267. if ($dsqli->isClose) {
  268. $this->Open(FALSE);
  269. $dsqli->isClose = FALSE;
  270. }
  271. if (!empty($sql)) {
  272. $this->SetQuery($sql);
  273. }
  274. //SQL语句安全检查
  275. if ($this->safeCheck) {
  276. CheckSql($this->queryString);
  277. }
  278. $t1 = ExecTime();
  279. //var_dump($this->queryString);
  280. $this->result[$id] = mysqli_query($this->linkID, $this->queryString);
  281. //var_dump(mysql_error());
  282. //查询性能测试
  283. if ($this->recordLog) {
  284. $queryTime = ExecTime() - $t1;
  285. $this->RecordLog($queryTime);
  286. //echo $this->queryString."--{$queryTime}<hr />\r\n";
  287. }
  288. if (DEBUG_LEVEL === TRUE) {
  289. $queryTime = ExecTime() - $t1;
  290. echo "<div style='width:98%;margin:1rem auto;color: #155724;background-color: #d4edda;border-color: #c3e6cb;position: relative;padding: .75rem 1.25rem;border: 1px solid transparent;border-radius: .25rem;'>执行SQL:" . $this->queryString . ",执行时间:<b>{$queryTime}</b></div>\r\n";
  291. }
  292. if ($this->result[$id] === FALSE) {
  293. $this->DisplayError(mysqli_error($this->linkID) . " <br />Error sql: <font color='red'>" . $this->queryString . "</font>");
  294. }
  295. }
  296. function Query($id = "me", $sql = '')
  297. {
  298. $this->Execute($id, $sql);
  299. }
  300. //执行一个SQL语句,返回前一条记录或仅返回一条记录
  301. function GetOne($sql = '', $acctype = MYSQLI_ASSOC)
  302. {
  303. global $dsqli;
  304. // $t1 = ExecTime();
  305. if (!$dsqli->isInit) {
  306. $this->Init($this->pconnect);
  307. }
  308. // echo ExecTime() - $t1;
  309. if ($dsqli->isClose) {
  310. $this->Open(FALSE);
  311. $dsqli->isClose = FALSE;
  312. }
  313. if (!empty($sql)) {
  314. if (!preg_match("/LIMIT/i", $sql)) $this->SetQuery(preg_replace("/[,;]$/i", '', trim($sql)) . " LIMIT 0,1;");
  315. else $this->SetQuery($sql);
  316. }
  317. $this->Execute("one");
  318. $arr = $this->GetArray("one", $acctype);
  319. if (!is_array($arr)) {
  320. return '';
  321. } else {
  322. @mysqli_free_result($this->result["one"]);
  323. return ($arr);
  324. }
  325. }
  326. //执行一个不与任何表名有关的SQL语句,Create等
  327. function ExecuteSafeQuery($sql, $id = "me")
  328. {
  329. global $dsqli;
  330. if (!$dsqli->isInit) {
  331. $this->Init($this->pconnect);
  332. }
  333. if ($dsqli->isClose) {
  334. $this->Open(FALSE);
  335. $dsqli->isClose = FALSE;
  336. }
  337. $this->result[$id] = @mysqli_query($sql, $this->linkID);
  338. }
  339. //返回当前的一条记录并把游标移向下一记录
  340. // MYSQLI_ASSOC、MYSQLI_NUM、MYSQLI_BOTH
  341. function GetArray($id = "me", $acctype = MYSQLI_ASSOC)
  342. {
  343. // var_dump($this->result);
  344. if ($this->result[$id] === 0) {
  345. return FALSE;
  346. } else {
  347. return @mysqli_fetch_array($this->result[$id], $acctype);
  348. }
  349. }
  350. function GetObject($id = "me")
  351. {
  352. if ($this->result[$id] === 0) {
  353. return FALSE;
  354. } else {
  355. return mysqli_fetch_object($this->result[$id]);
  356. }
  357. }
  358. // 检测是否存在某数据表
  359. function IsTable($tbname)
  360. {
  361. global $dsqli;
  362. if (!$dsqli->isInit) {
  363. $this->Init($this->pconnect);
  364. }
  365. $prefix = "#@__";
  366. $tbname = str_replace($prefix, $GLOBALS['cfg_dbprefix'], $tbname);
  367. if (mysqli_num_rows(@mysqli_query($this->linkID, "SHOW TABLES LIKE '" . $tbname . "'"))) {
  368. return TRUE;
  369. }
  370. return FALSE;
  371. }
  372. //获得MySql的版本号
  373. function GetVersion($isformat = TRUE)
  374. {
  375. global $dsqli;
  376. if (!$dsqli->isInit) {
  377. $this->Init($this->pconnect);
  378. }
  379. if ($dsqli->isClose) {
  380. $this->Open(FALSE);
  381. $dsqli->isClose = FALSE;
  382. }
  383. $rs = mysqli_query($this->linkID, "SELECT VERSION();");
  384. $row = mysqli_fetch_array($rs);
  385. $mysql_version = $row[0];
  386. mysqli_free_result($rs);
  387. if ($isformat) {
  388. $mysql_versions = explode(".", trim($mysql_version));
  389. $mysql_version = number_format($mysql_versions[0] . "." . $mysql_versions[1], 2);
  390. }
  391. return $mysql_version;
  392. }
  393. //获取特定表的信息
  394. function GetTableFields($tbname, $id = "me")
  395. {
  396. global $dsqli;
  397. if (!$dsqli->isInit) {
  398. $this->Init($this->pconnect);
  399. }
  400. $prefix = "#@__";
  401. $tbname = str_replace($prefix, $GLOBALS['cfg_dbprefix'], $tbname);
  402. $query = "SELECT * FROM {$tbname} LIMIT 0,1";
  403. $this->result[$id] = mysqli_query($this->linkID, $query);
  404. }
  405. //获取字段详细信息
  406. function GetFieldObject($id = "me")
  407. {
  408. return mysqli_fetch_field($this->result[$id]);
  409. }
  410. //获得查询的总记录数
  411. function GetTotalRow($id = "me")
  412. {
  413. if ($this->result[$id] === 0) {
  414. return -1;
  415. } else {
  416. return @mysqli_num_rows($this->result[$id]);
  417. }
  418. }
  419. //获取上一步INSERT操作产生的ID
  420. function GetLastID()
  421. {
  422. //如果 AUTO_INCREMENT 的列的类型是 BIGINT,则 mysqli_insert_id() 返回的值将不正确。
  423. //可以在 SQL 查询中用 MySQL 内部的 SQL 函数 LAST_INSERT_ID() 来替代。
  424. //$rs = mysqli_query($this->linkID, "Select LAST_INSERT_ID() as lid");
  425. //$row = mysqli_fetch_array($rs);
  426. //return $row["lid"];
  427. return mysqli_insert_id($this->linkID);
  428. }
  429. //释放记录集占用的资源
  430. function FreeResult($id = "me")
  431. {
  432. @mysqli_free_result($this->result[$id]);
  433. }
  434. function FreeResultAll()
  435. {
  436. if (!is_array($this->result)) {
  437. return '';
  438. }
  439. foreach ($this->result as $kk => $vv) {
  440. if ($vv) {
  441. @mysqli_free_result($vv);
  442. }
  443. }
  444. }
  445. //设置SQL语句,会自动把SQL语句里的#@__替换为$this->dbPrefix(在配置文件中为$cfg_dbprefix)
  446. function SetQuery($sql)
  447. {
  448. $prefix = "#@__";
  449. $sql = str_replace($prefix, $GLOBALS['cfg_dbprefix'], $sql);
  450. $this->queryString = $sql;
  451. }
  452. function SetSql($sql)
  453. {
  454. $this->SetQuery($sql);
  455. }
  456. function RecordLog($runtime = 0)
  457. {
  458. $RecordLogFile = dirname(__FILE__) . '/../data/mysqli_record_log.inc';
  459. $url = $this->GetCurUrl();
  460. $savemsg = <<<EOT
  461. ------------------------------------------
  462. SQL:{$this->queryString}
  463. Page:$url
  464. Runtime:$runtime
  465. EOT;
  466. $fp = @fopen($RecordLogFile, 'a');
  467. @fwrite($fp, $savemsg);
  468. @fclose($fp);
  469. }
  470. //显示数据链接错误信息
  471. function DisplayError($msg)
  472. {
  473. $errorTrackFile = dirname(__FILE__) . '/../data/mysqli_error_trace.inc';
  474. if (file_exists(dirname(__FILE__) . '/../data/mysqli_error_trace.php')) {
  475. @unlink(dirname(__FILE__) . '/../data/mysqli_error_trace.php');
  476. }
  477. if ($this->showError) {
  478. $emsg = '';
  479. $emsg .= "<div><h3>DedeBIZ Error Warning!</h3>\r\n";
  480. $emsg .= "<div><a href='https://www.dedebiz.com' target='_blank' style='color:red'>Technical Support: https://www.dedebiz.com</a></div>";
  481. $emsg .= "<div style='line-helght:160%;font-size:14px;color:green'>\r\n";
  482. $emsg .= "<div style='color:blue'><br />Error page: <font color='red'>" . $this->GetCurUrl() . "</font></div>\r\n";
  483. $emsg .= "<div>Error infos: {$msg}</div>\r\n";
  484. $emsg .= "<br /></div></div>\r\n";
  485. echo $emsg;
  486. }
  487. $savemsg = 'Page: ' . $this->GetCurUrl() . "\r\nError: " . $msg . "\r\nTime" . date('Y-m-d H:i:s');
  488. //保存MySql错误日志
  489. $fp = @fopen($errorTrackFile, 'a');
  490. @fwrite($fp, '<' . '?php exit();' . "\r\n/*\r\n{$savemsg}\r\n*/\r\n?" . ">\r\n");
  491. @fclose($fp);
  492. }
  493. //获得当前的脚本网址
  494. function GetCurUrl()
  495. {
  496. if (!empty($_SERVER["REQUEST_URI"])) {
  497. $scriptName = $_SERVER["REQUEST_URI"];
  498. $nowurl = $scriptName;
  499. } else {
  500. $scriptName = $_SERVER["PHP_SELF"];
  501. if (empty($_SERVER["QUERY_STRING"])) {
  502. $nowurl = $scriptName;
  503. } else {
  504. $nowurl = $scriptName . "?" . $_SERVER["QUERY_STRING"];
  505. }
  506. }
  507. return $nowurl;
  508. }
  509. }
  510. //复制一个对象副本
  511. function CopySQLiPoint(&$ndsql)
  512. {
  513. $GLOBALS['dsqli'] = $ndsql;
  514. }
  515. //SQL语句过滤程序,由80sec提供,这里作了适当的修改
  516. if (!function_exists('CheckSql')) {
  517. function CheckSql($db_string, $querytype = 'select')
  518. {
  519. global $cfg_cookie_encode;
  520. $clean = '';
  521. $error = '';
  522. $old_pos = 0;
  523. $pos = -1;
  524. $log_file = DEDEINC . '/../data/' . md5($cfg_cookie_encode) . '_safe.txt';
  525. $userIP = GetIP();
  526. $getUrl = GetCurUrl();
  527. //如果是普通查询语句,直接过滤一些特殊语法
  528. if ($querytype == 'select') {
  529. $notallow1 = "[^0-9a-z@\._-]{1,}(union|sleep|benchmark|load_file|outfile)[^0-9a-z@\.-]{1,}";
  530. //$notallow2 = "--|/\*";
  531. if (preg_match("/" . $notallow1 . "/i", $db_string)) {
  532. fputs(fopen($log_file, 'a+'), "$userIP||$getUrl||$db_string||SelectBreak\r\n");
  533. exit("<font size='5' color='red'>Safe Alert: Request Error step 1 !</font>");
  534. }
  535. }
  536. //完整的SQL检查
  537. while (TRUE) {
  538. $pos = strpos($db_string, '\'', $pos + 1);
  539. if ($pos === FALSE) {
  540. break;
  541. }
  542. $clean .= substr($db_string, $old_pos, $pos - $old_pos);
  543. while (TRUE) {
  544. $pos1 = strpos($db_string, '\'', $pos + 1);
  545. $pos2 = strpos($db_string, '\\', $pos + 1);
  546. if ($pos1 === FALSE) {
  547. break;
  548. } elseif ($pos2 == FALSE || $pos2 > $pos1) {
  549. $pos = $pos1;
  550. break;
  551. }
  552. $pos = $pos2 + 1;
  553. }
  554. $clean .= '$s$';
  555. $old_pos = $pos + 1;
  556. }
  557. $clean .= substr($db_string, $old_pos);
  558. $clean = trim(strtolower(preg_replace(array('~\s+~s'), array(' '), $clean)));
  559. if (
  560. strpos($clean, '@') !== FALSE or strpos($clean, 'char(') !== FALSE or strpos($clean, '"') !== FALSE
  561. or strpos($clean, '$s$$s$') !== FALSE
  562. ) {
  563. $fail = TRUE;
  564. if (preg_match("#^create table#i", $clean)) $fail = FALSE;
  565. $error = "unusual character";
  566. }
  567. //老版本的Mysql并不支持union,常用的程序里也不使用union,但是一些黑客使用它,所以检查它
  568. if (strpos($clean, 'union') !== FALSE && preg_match('~(^|[^a-z])union($|[^[a-z])~s', $clean) != 0) {
  569. $fail = TRUE;
  570. $error = "union detect";
  571. }
  572. //发布版本的程序可能比较少包括--,#这样的注释,但是黑客经常使用它们
  573. elseif (strpos($clean, '/*') > 2 || strpos($clean, '--') !== FALSE || strpos($clean, '#') !== FALSE) {
  574. $fail = TRUE;
  575. $error = "comment detect";
  576. }
  577. //这些函数不会被使用,但是黑客会用它来操作文件,down掉数据库
  578. elseif (strpos($clean, 'sleep') !== FALSE && preg_match('~(^|[^a-z])sleep($|[^[a-z])~s', $clean) != 0) {
  579. $fail = TRUE;
  580. $error = "slown down detect";
  581. } elseif (strpos($clean, 'benchmark') !== FALSE && preg_match('~(^|[^a-z])benchmark($|[^[a-z])~s', $clean) != 0) {
  582. $fail = TRUE;
  583. $error = "slown down detect";
  584. } elseif (strpos($clean, 'load_file') !== FALSE && preg_match('~(^|[^a-z])load_file($|[^[a-z])~s', $clean) != 0) {
  585. $fail = TRUE;
  586. $error = "file fun detect";
  587. } elseif (strpos($clean, 'into outfile') !== FALSE && preg_match('~(^|[^a-z])into\s+outfile($|[^[a-z])~s', $clean) != 0) {
  588. $fail = TRUE;
  589. $error = "file fun detect";
  590. }
  591. //老版本的MYSQL不支持子查询,我们的程序里可能也用得少,但是黑客可以使用它来查询数据库敏感信息
  592. elseif (preg_match('~\([^)]*?select~s', $clean) != 0) {
  593. $fail = TRUE;
  594. $error = "sub select detect";
  595. }
  596. if (!empty($fail)) {
  597. fputs(fopen($log_file, 'a+'), "$userIP||$getUrl||$db_string||$error\r\n");
  598. exit("<font size='5' color='red'>Safe Alert: Request Error step 2!</font>");
  599. } else {
  600. return $db_string;
  601. }
  602. }
  603. }