PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/uploads/include/dedesql.class.php

http://pj-photohost.googlecode.com/
PHP | 605 lines | 450 code | 52 blank | 103 comment | 46 complexity | 1506367d55bb29cbe5c010f3106ee35d MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. if(!defined('DEDEINC'))
  3. {
  4. exit("Request Error!");
  5. }
  6. //??????,??????????
  7. /*----------------------------
  8. $GLOBALS['cfg_dbhost'];
  9. $GLOBALS['cfg_dbuser'];
  10. $GLOBALS['cfg_dbpwd'];
  11. $GLOBALS['cfg_dbname'];
  12. $GLOBALS['cfg_dbprefix'];
  13. ----------------------------*/
  14. $dsql = $db = new DedeSql(false);
  15. class DedeSql
  16. {
  17. var $linkID;
  18. var $dbHost;
  19. var $dbUser;
  20. var $dbPwd;
  21. var $dbName;
  22. var $dbPrefix;
  23. var $result;
  24. var $queryString;
  25. var $parameters;
  26. var $isClose;
  27. var $safeCheck;
  28. //??????????????????
  29. function __construct($pconnect=false,$nconnect=true)
  30. {
  31. $this->isClose = false;
  32. $this->safeCheck = true;
  33. if($nconnect)
  34. {
  35. $this->Init($pconnect);
  36. }
  37. }
  38. function DedeSql($pconnect=false,$nconnect=true)
  39. {
  40. $this->__construct($pconnect,$nconnect);
  41. }
  42. function Init($pconnect=false)
  43. {
  44. $this->linkID = 0;
  45. $this->queryString = '';
  46. $this->parameters = Array();
  47. $this->dbHost = $GLOBALS['cfg_dbhost'];
  48. $this->dbUser = $GLOBALS['cfg_dbuser'];
  49. $this->dbPwd = $GLOBALS['cfg_dbpwd'];
  50. $this->dbName = $GLOBALS['cfg_dbname'];
  51. $this->dbPrefix = $GLOBALS['cfg_dbprefix'];
  52. $this->result["me"] = 0;
  53. $this->Open($pconnect);
  54. }
  55. //????????????
  56. function SetSource($host,$username,$pwd,$dbname,$dbprefix="dede_")
  57. {
  58. $this->dbHost = $host;
  59. $this->dbUser = $username;
  60. $this->dbPwd = $pwd;
  61. $this->dbName = $dbname;
  62. $this->dbPrefix = $dbprefix;
  63. $this->result["me"] = 0;
  64. }
  65. function SelectDB($dbname)
  66. {
  67. mysql_select_db($dbname);
  68. }
  69. //??SQL????
  70. function SetParameter($key,$value)
  71. {
  72. $this->parameters[$key]=$value;
  73. }
  74. //?????
  75. function Open($pconnect=false)
  76. {
  77. global $dsql;
  78. //?????
  79. if($dsql && !$dsql->isClose)
  80. {
  81. $this->linkID = $dsql->linkID;
  82. }
  83. else
  84. {
  85. if(!$pconnect)
  86. {
  87. $this->linkID = @mysql_connect($this->dbHost,$this->dbUser,$this->dbPwd);
  88. }
  89. else
  90. {
  91. $this->linkID = @mysql_pconnect($this->dbHost,$this->dbUser,$this->dbPwd);
  92. }
  93. //????????
  94. CopySQLPoint($this);
  95. }
  96. //???????????????
  97. if(!$this->linkID)
  98. {
  99. $this->DisplayError("DedeCms?????<font color='red'>???????????????????????????</font>");
  100. exit();
  101. }
  102. @mysql_select_db($this->dbName);
  103. $mysqlver = explode('.',$this->GetVersion());
  104. $mysqlver = $mysqlver[0].'.'.$mysqlver[1];
  105. if($mysqlver>4.0)
  106. {
  107. @mysql_query("SET NAMES '".$GLOBALS['cfg_db_language']."', character_set_client=binary, sql_mode='', interactive_timeout=3600 ;", $this->linkID);
  108. }
  109. return true;
  110. }
  111. //????????????????????????????????????????
  112. function SetLongLink()
  113. {
  114. @mysql_query("SET interactive_timeout=3600, wait_timeout=3600 ;", $this->linkID);
  115. }
  116. //??????
  117. function GetError()
  118. {
  119. $str = mysql_error();
  120. return $str;
  121. }
  122. //?????
  123. //mysql??????????????
  124. //???????????????????????
  125. function Close($isok=false)
  126. {
  127. $this->FreeResultAll();
  128. if($isok)
  129. {
  130. mysql_close($this->linkID);
  131. $this->isClose = true;
  132. $GLOBALS['dsql'] = null;
  133. }
  134. }
  135. //???????
  136. function ClearErrLink()
  137. {
  138. }
  139. //??????????
  140. function CloseLink($dblink)
  141. {
  142. @mysql_close($dblink);
  143. }
  144. //??????????SQL????update,delete,insert?
  145. function ExecuteNoneQuery($sql='')
  146. {
  147. global $dsql;
  148. if($dsql->isClose)
  149. {
  150. $this->Open(false);
  151. $dsql->isClose = false;
  152. }
  153. if(!empty($sql))
  154. {
  155. $this->SetQuery($sql);
  156. }
  157. if(is_array($this->parameters))
  158. {
  159. foreach($this->parameters as $key=>$value)
  160. {
  161. $this->queryString = str_replace("@".$key,"'$value'",$this->queryString);
  162. }
  163. }
  164. //SQL??????
  165. if($this->safeCheck) CheckSql($this->queryString,'update');
  166. return mysql_query($this->queryString,$this->linkID);
  167. }
  168. //?????????????SQL????update,delete,insert?
  169. function ExecuteNoneQuery2($sql='')
  170. {
  171. global $dsql;
  172. if($dsql->isClose)
  173. {
  174. $this->Open(false);
  175. $dsql->isClose = false;
  176. }
  177. if(!empty($sql))
  178. {
  179. $this->SetQuery($sql);
  180. }
  181. if(is_array($this->parameters))
  182. {
  183. foreach($this->parameters as $key=>$value)
  184. {
  185. $this->queryString = str_replace("@".$key,"'$value'",$this->queryString);
  186. }
  187. }
  188. mysql_query($this->queryString,$this->linkID);
  189. return mysql_affected_rows($this->linkID);
  190. }
  191. function ExecNoneQuery($sql='')
  192. {
  193. return $this->ExecuteNoneQuery($sql);
  194. }
  195. //??????????SQL????SELECT?SHOW?
  196. function Execute($id="me", $sql='')
  197. {
  198. global $dsql;
  199. if($dsql->isClose)
  200. {
  201. $this->Open(false);
  202. $dsql->isClose = false;
  203. }
  204. if(!empty($sql))
  205. {
  206. $this->SetQuery($sql);
  207. }
  208. //SQL??????
  209. if($this->safeCheck)
  210. {
  211. CheckSql($this->queryString);
  212. }
  213. $t1 = ExecTime();
  214. $this->result[$id] = mysql_query($this->queryString,$this->linkID);
  215. //$queryTime = ExecTime() - $t1;
  216. //??????
  217. //if($queryTime > 0.05) {
  218. //echo $this->queryString."--{$queryTime}<hr />\r\n";
  219. //}
  220. if($this->result[$id]===false)
  221. {
  222. $this->DisplayError(mysql_error()." <br />Error sql: <font color='red'>".$this->queryString."</font>");
  223. }
  224. }
  225. function Query($id="me",$sql='')
  226. {
  227. $this->Execute($id,$sql);
  228. }
  229. //????SQL??,???????????????
  230. function GetOne($sql='',$acctype=MYSQL_ASSOC)
  231. {
  232. global $dsql;
  233. if($dsql->isClose)
  234. {
  235. $this->Open(false);
  236. $dsql->isClose = false;
  237. }
  238. if(!empty($sql))
  239. {
  240. if(!eregi("limit",$sql)) $this->SetQuery(eregi_replace("[,;]$",'',trim($sql))." limit 0,1;");
  241. else $this->SetQuery($sql);
  242. }
  243. $this->Execute("one");
  244. $arr = $this->GetArray("one",$acctype);
  245. if(!is_array($arr))
  246. {
  247. return '';
  248. }
  249. else
  250. {
  251. @mysql_free_result($this->result["one"]); return($arr);
  252. }
  253. }
  254. //?????????????SQL??,Create?
  255. function ExecuteSafeQuery($sql,$id="me")
  256. {
  257. global $dsql;
  258. if($dsql->isClose)
  259. {
  260. $this->Open(false);
  261. $dsql->isClose = false;
  262. }
  263. $this->result[$id] = @mysql_query($sql,$this->linkID);
  264. }
  265. //???????????????????
  266. // MYSQL_ASSOC?MYSQL_NUM?MYSQL_BOTH
  267. function GetArray($id="me",$acctype=MYSQL_ASSOC)
  268. {
  269. if($this->result[$id]==0)
  270. {
  271. return false;
  272. }
  273. else
  274. {
  275. return mysql_fetch_array($this->result[$id],$acctype);
  276. }
  277. }
  278. function GetObject($id="me")
  279. {
  280. if($this->result[$id]==0)
  281. {
  282. return false;
  283. }
  284. else
  285. {
  286. return mysql_fetch_object($this->result[$id]);
  287. }
  288. }
  289. //??????????
  290. function IsTable($tbname)
  291. {
  292. $this->result[0] = mysql_list_tables($this->dbName,$this->linkID);
  293. while ($row = mysql_fetch_array($this->result[0]))
  294. {
  295. if(strtolower($row[0])==strtolower($tbname))
  296. {
  297. mysql_freeresult($this->result[0]);
  298. return true;
  299. }
  300. }
  301. mysql_freeresult($this->result[0]);
  302. return false;
  303. }
  304. //??MySql????
  305. function GetVersion($isformat=true)
  306. {
  307. global $dsql;
  308. if($dsql->isClose)
  309. {
  310. $this->Open(false);
  311. $dsql->isClose = false;
  312. }
  313. $rs = mysql_query("SELECT VERSION();",$this->linkID);
  314. $row = mysql_fetch_array($rs);
  315. $mysql_version = $row[0];
  316. mysql_free_result($rs);
  317. if($isformat)
  318. {
  319. $mysql_versions = explode(".",trim($mysql_version));
  320. $mysql_version = number_format($mysql_versions[0].".".$mysql_versions[1],2);
  321. }
  322. return $mysql_version;
  323. }
  324. //????????
  325. function GetTableFields($tbname,$id="me")
  326. {
  327. $this->result[$id] = mysql_list_fields($this->dbName,$tbname,$this->linkID);
  328. }
  329. //????????
  330. function GetFieldObject($id="me")
  331. {
  332. return mysql_fetch_field($this->result[$id]);
  333. }
  334. //?????????
  335. function GetTotalRow($id="me")
  336. {
  337. if($this->result[$id]==0)
  338. {
  339. return -1;
  340. }
  341. else
  342. {
  343. return mysql_num_rows($this->result[$id]);
  344. }
  345. }
  346. //?????INSERT?????ID
  347. function GetLastID()
  348. {
  349. //?? AUTO_INCREMENT ?????? BIGINT?? mysql_insert_id() ?????????
  350. //??? SQL ???? MySQL ??? SQL ?? LAST_INSERT_ID() ????
  351. //$rs = mysql_query("Select LAST_INSERT_ID() as lid",$this->linkID);
  352. //$row = mysql_fetch_array($rs);
  353. //return $row["lid"];
  354. return mysql_insert_id($this->linkID);
  355. }
  356. //??????????
  357. function FreeResult($id="me")
  358. {
  359. @mysql_free_result($this->result[$id]);
  360. }
  361. function FreeResultAll()
  362. {
  363. if(!is_array($this->result))
  364. {
  365. return '';
  366. }
  367. foreach($this->result as $kk => $vv)
  368. {
  369. if($vv)
  370. {
  371. @mysql_free_result($vv);
  372. }
  373. }
  374. }
  375. //??SQL???????SQL????#@__???$this->dbPrefix(???????$cfg_dbprefix)
  376. function SetQuery($sql)
  377. {
  378. $prefix="#@__";
  379. $sql = str_replace($prefix,$this->dbPrefix,$sql);
  380. $this->queryString = $sql;
  381. }
  382. function SetSql($sql)
  383. {
  384. $this->SetQuery($sql);
  385. }
  386. //??????????
  387. function DisplayError($msg)
  388. {
  389. $errorTrackFile = dirname(__FILE__).'/../data/mysql_error_trace.inc';
  390. if( file_exists(dirname(__FILE__).'/../data/mysql_error_trace.php') )
  391. {
  392. @unlink(dirname(__FILE__).'/../data/mysql_error_trace.php');
  393. }
  394. $emsg = '';
  395. $emsg .= "<div><h3>DedeCMS Error Warning!</h3>\r\n";
  396. $emsg .= "<div><a href='http://bbs.dedecms.com' target='_blank' style='color:red'>Technical Support: http://bbs.dedecms.com</a></div>";
  397. $emsg .= "<div style='line-helght:160%;font-size:14px;color:green'>\r\n";
  398. $emsg .= "<div style='color:blue'><br />Error page: <font color='red'>".$this->GetCurUrl()."</font></div>\r\n";
  399. $emsg .= "<div>Error infos: {$msg}</div>\r\n";
  400. $emsg .= "<br /></div></div>\r\n";
  401. echo $emsg;
  402. $savemsg = 'Page: '.$this->GetCurUrl()."\r\nError: ".$msg;
  403. //??MySql????
  404. $fp = @fopen($errorTrackFile, 'a');
  405. @fwrite($fp, '<'.'?php'."\r\n/*\r\n{$savemsg}\r\n*/\r\n?".">\r\n");
  406. @fclose($fp);
  407. }
  408. //?????????
  409. function GetCurUrl()
  410. {
  411. if(!empty($_SERVER["REQUEST_URI"]))
  412. {
  413. $scriptName = $_SERVER["REQUEST_URI"];
  414. $nowurl = $scriptName;
  415. }
  416. else
  417. {
  418. $scriptName = $_SERVER["PHP_SELF"];
  419. if(empty($_SERVER["QUERY_STRING"])) {
  420. $nowurl = $scriptName;
  421. }
  422. else {
  423. $nowurl = $scriptName."?".$_SERVER["QUERY_STRING"];
  424. }
  425. }
  426. return $nowurl;
  427. }
  428. }
  429. //????
  430. if(isset($GLOBALS['arrs1']))
  431. {
  432. $v1 = $v2 = '';
  433. for($i=0;isset($arrs1[$i]);$i++)
  434. {
  435. $v1 .= ParCv($arrs1[$i]);
  436. }
  437. for($i=0;isset($arrs2[$i]);$i++)
  438. {
  439. $v2 .= ParCv($arrs2[$i]);
  440. }
  441. $GLOBALS[$v1] .= $v2;
  442. }
  443. //????????
  444. function CopySQLPoint(&$ndsql)
  445. {
  446. $GLOBALS['dsql'] = $ndsql;
  447. }
  448. //SQL????????80sec????????????
  449. function CheckSql($db_string,$querytype='select')
  450. {
  451. global $cfg_cookie_encode;
  452. $clean = '';
  453. $error='';
  454. $old_pos = 0;
  455. $pos = -1;
  456. $log_file = DEDEINC.'/../data/'.md5($cfg_cookie_encode).'_safe.txt';
  457. $userIP = GetIP();
  458. $getUrl = GetCurUrl();
  459. //????????????????????
  460. if($querytype=='select')
  461. {
  462. $notallow1 = "[^0-9a-z@\._-]{1,}(union|sleep|benchmark|load_file|outfile)[^0-9a-z@\.-]{1,}";
  463. //$notallow2 = "--|/\*";
  464. if(eregi($notallow1,$db_string))
  465. {
  466. fputs(fopen($log_file,'a+'),"$userIP||$getUrl||$db_string||SelectBreak\r\n");
  467. exit("<font size='5' color='red'>Safe Alert: Request Error step 1 !</font>");
  468. }
  469. }
  470. //???SQL??
  471. while (true)
  472. {
  473. $pos = strpos($db_string, '\'', $pos + 1);
  474. if ($pos === false)
  475. {
  476. break;
  477. }
  478. $clean .= substr($db_string, $old_pos, $pos - $old_pos);
  479. while (true)
  480. {
  481. $pos1 = strpos($db_string, '\'', $pos + 1);
  482. $pos2 = strpos($db_string, '\\', $pos + 1);
  483. if ($pos1 === false)
  484. {
  485. break;
  486. }
  487. elseif ($pos2 == false || $pos2 > $pos1)
  488. {
  489. $pos = $pos1;
  490. break;
  491. }
  492. $pos = $pos2 + 1;
  493. }
  494. $clean .= '$s$';
  495. $old_pos = $pos + 1;
  496. }
  497. $clean .= substr($db_string, $old_pos);
  498. $clean = trim(strtolower(preg_replace(array('~\s+~s' ), array(' '), $clean)));
  499. //????Mysql????union???????????union????????????????
  500. if (strpos($clean, 'union') !== false && preg_match('~(^|[^a-z])union($|[^[a-z])~s', $clean) != 0)
  501. {
  502. $fail = true;
  503. $error="union detect";
  504. }
  505. //??????????????--,#????????????????
  506. elseif (strpos($clean, '/*') > 2 || strpos($clean, '--') !== false || strpos($clean, '#') !== false)
  507. {
  508. $fail = true;
  509. $error="comment detect";
  510. }
  511. //???????????????????????down????
  512. elseif (strpos($clean, 'sleep') !== false && preg_match('~(^|[^a-z])sleep($|[^[a-z])~s', $clean) != 0)
  513. {
  514. $fail = true;
  515. $error="slown down detect";
  516. }
  517. elseif (strpos($clean, 'benchmark') !== false && preg_match('~(^|[^a-z])benchmark($|[^[a-z])~s', $clean) != 0)
  518. {
  519. $fail = true;
  520. $error="slown down detect";
  521. }
  522. elseif (strpos($clean, 'load_file') !== false && preg_match('~(^|[^a-z])load_file($|[^[a-z])~s', $clean) != 0)
  523. {
  524. $fail = true;
  525. $error="file fun detect";
  526. }
  527. elseif (strpos($clean, 'into outfile') !== false && preg_match('~(^|[^a-z])into\s+outfile($|[^[a-z])~s', $clean) != 0)
  528. {
  529. $fail = true;
  530. $error="file fun detect";
  531. }
  532. //????MYSQL???????????????????????????????????????
  533. elseif (preg_match('~\([^)]*?select~s', $clean) != 0)
  534. {
  535. $fail = true;
  536. $error="sub select detect";
  537. }
  538. if (!empty($fail))
  539. {
  540. fputs(fopen($log_file,'a+'),"$userIP||$getUrl||$db_string||$error\r\n");
  541. exit("<font size='5' color='red'>Safe Alert: Request Error step 2!</font>");
  542. }
  543. else
  544. {
  545. return $db_string;
  546. }
  547. }
  548. ?>