Advertisement
Guest User

Untitled

a guest
Jan 17th, 2011
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.54 KB | None | 0 0
  1. <?php
  2.  
  3. //// Initialize the sql library...
  4. //
  5. $sql = new sql;
  6. $sql->appname = 'www.furaffinity.net';
  7. $sql->database = 'furaffinity';
  8.  
  9.  
  10. $sql->server = '192.168.1.1';
  11. $sql->user = 'furaffinity';
  12. $sql->password = 'kayWrolv~quoles6';
  13.  
  14. $sql->logfile = '/srv/www/logs/mysql-error.log';
  15.  
  16. $sql->connect();
  17.  
  18. return $sql;
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26. class sql
  27. {
  28. var $database = '';
  29.  
  30. var $linkid;
  31. var $query_result;
  32. var $record;
  33.  
  34. var $logfile;
  35.  
  36. var $errdesc;
  37. var $errno;
  38. var $show_error;
  39.  
  40. var $server;
  41. var $user;
  42. var $password;
  43.  
  44.  
  45. //// Store all executed queries internally for debugging purposes
  46. //
  47. var $query_count;
  48. var $query_history;
  49.  
  50. var $_transaction_started;
  51.  
  52. function sql()
  53. {
  54. $this->database = '';
  55. $this->logfile = '';
  56.  
  57. $this->linkid = 0;
  58. $this->query_result = 0;
  59. $this->record = array();
  60.  
  61. $this->errdesc = '';
  62. $this->errno = 0;
  63. $this->show_error = 1;
  64.  
  65. $this->server = '';
  66. $this->user = '';
  67. $this->password = '';
  68.  
  69. $this->query_count = 0;
  70. $this->query_history = array();
  71.  
  72. $this->_transaction_started = FALSE;
  73. }
  74.  
  75. function connect()
  76. {
  77. if($this->linkid == 0)
  78. {
  79. $this->linkid = @mysql_connect($this->server, $this->user, $this->password);
  80.  
  81. $this->linkid
  82. or
  83. $this->print_error("Link-ID == false, connect failed");
  84.  
  85. ($this->database and $this->linkid)
  86. and
  87. $this->select_db($this->database);
  88.  
  89. return $this->linkid;
  90. }
  91. }
  92.  
  93.  
  94.  
  95. function disconnect()
  96. {
  97. mysql_close($this->linkid);
  98. }
  99.  
  100.  
  101.  
  102. function sql_error()
  103. {
  104. $this->error = mysql_error();
  105.  
  106. return $this->error;
  107. }
  108.  
  109.  
  110.  
  111. function geterrno()
  112. {
  113. $this->errno=mysql_errno();
  114.  
  115. return $this->errno;
  116. }
  117.  
  118.  
  119.  
  120. function fetch_row($query_result=-1)
  121. {
  122. $query_result != -1
  123. and
  124. $this->query_result = $query_result;
  125.  
  126.  
  127. $this->record = mysql_fetch_row($this->query_result);
  128.  
  129. return $this->record;
  130. }
  131.  
  132.  
  133.  
  134. function select_db($database = '')
  135. {
  136. $database
  137. and
  138. $this->database = $database;
  139.  
  140. @mysql_select_db($this->database, $this->linkid)
  141. or
  142. $this->print_error('cannot use database '.$this->database);
  143. }
  144.  
  145.  
  146.  
  147. function query($query_string, $query_tag = '', $query_file = '', $ignore_errors = FALSE)
  148. {
  149. //// Free the previuos query result, if it happends to still exist
  150. //
  151. $this->query_result
  152. and
  153. @mysql_free_result($this->query_result);
  154.  
  155. //// Add a comment to the query string, so the query could be
  156. // easily traceable via mysql "show processlist"
  157. //
  158. $comment = '';
  159.  
  160. $query_file
  161. and
  162. $comment .= '['.basename($query_file).'] ';
  163.  
  164. $query_tag
  165. and
  166. $comment .= $query_tag;
  167.  
  168. $comment
  169. and
  170. $comment = '/* '.$comment.' */ ';
  171.  
  172. $query_string = $comment.$query_string;
  173.  
  174.  
  175.  
  176. $query_start = microtime();
  177. $this->query_result = mysql_query($query_string, $this->linkid);
  178. $query_end = microtime();
  179.  
  180. $used_time = $this->_microtime_used($query_start, $query_end);
  181.  
  182. if($ignore_errors == FALSE)
  183. {
  184. $this->query_result
  185. or
  186. $this->print_error("Invalid SQL: ".$query_string);
  187. }
  188.  
  189.  
  190. $this->query_history[$this->query_count]['sql'] = $query_string;
  191. $this->query_history[$this->query_count]['tag'] = $query_tag;
  192. $this->query_history[$this->query_count]['file'] = $query_file;
  193. $this->query_history[$this->query_count]['time'] = $used_time;
  194.  
  195. $this->query_count++;
  196.  
  197. return $this->query_result;
  198. }
  199.  
  200.  
  201.  
  202. function fetch_array($query_result=-1)
  203. {
  204. $query_result != -1
  205. and
  206. $this->query_result=$query_result;
  207.  
  208. $this->record = mysql_fetch_array($this->query_result, MYSQL_ASSOC);
  209.  
  210. return $this->record;
  211. }
  212.  
  213.  
  214.  
  215. function free_result($query_result=-1)
  216. {
  217. $query_result != -1
  218. and
  219. $this->query_result = $query_result;
  220.  
  221. return @mysql_free_result($this->query_result);
  222. }
  223.  
  224.  
  225.  
  226. function insertid($field, $table, $condition, $condval)
  227. {
  228. for($i=0; $i!=$val; $i++)
  229. {
  230. $val = $sql->query_first("SELECT MAX($field)as maxid FROM $table WHERE $condition='$condval'");
  231.  
  232. if($val['maxid'])
  233. {
  234. return $val['maxid'];
  235. }
  236. }
  237. }
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245. function count($columnname, $from, $condition='')
  246. {
  247. $condition = ($condition) ? ' WHERE '.$condition : '';
  248.  
  249. $var = $this->query_first('SELECT count('.$columnname.') AS thiscount FROM '.$from.' '.$condition);
  250. $var = $var['thiscount'];
  251.  
  252. return $var;
  253. }
  254.  
  255.  
  256.  
  257. function get_maxid($columnname, $table, $condition)
  258. {
  259. $condition = ($condition) ? ' WHERE '.$condition : '';
  260.  
  261. $var = $this->query_first('SELECT max('.$columnname.') AS thismax FROM '.$table.' '.$condition);
  262. $var = $var['thismax']+1;
  263.  
  264. return $var;
  265. }
  266.  
  267.  
  268.  
  269. function num_rows($query_result=-1)
  270. {
  271. $query_result!=-1
  272. and
  273. $this->query_result=$query_result;
  274.  
  275. return mysql_num_rows($this->query_result);
  276. }
  277.  
  278.  
  279. function affected_rows()
  280. {
  281. return mysql_affected_rows($this->linkid);
  282. }
  283.  
  284. function insert_id()
  285. {
  286. return mysql_insert_id($this->linkid);
  287. }
  288.  
  289. function count_queries()
  290. {
  291. return $this->query_count;
  292. }
  293.  
  294.  
  295.  
  296. function print_error($msg)
  297. {
  298. global $adminmail, $basepath, $_USER, $header, $footer, $_BASE;
  299.  
  300. $this->errdesc = mysql_error();
  301. $this->errno = mysql_errno();
  302.  
  303. $this->errdesc == 'Got error 127 from table handler'
  304. and
  305. DoTableScan();
  306.  
  307. $this->errno == 1203
  308. and
  309. $error->sysSendError('013');
  310.  
  311.  
  312. if($this->errno == 1040)
  313. {
  314. echo "<font size='2'><b>System Error</b></font><br/>Database responded: Too many connections!";
  315. exit();
  316. }
  317.  
  318. $message = "Database error in $this->appname: $msg\n";
  319. $message .= "mysql error: $this->errdesc\n";
  320. $message .= "mysql error number: $this->errno\n";
  321. $message .= "User: ".$_USER['username']."\n";
  322. $message .= "Date: ".date("d.m.Y @ H:i")."\n";
  323. $message .= "Script: ".getenv("REQUEST_URI")."\n";
  324. $message .= "Referer: ".getenv("HTTP_REFERER")."\n";
  325.  
  326.  
  327. $this->_log($message);
  328.  
  329. if($_USER['accesslevel'] == "1")
  330. {
  331. $adminerror = "<br/><br/>
  332. <div align='center'>
  333. <table cellpadding='3' cellspacing='1' border='0' class='maintable'>
  334. <tr>
  335. <td class='cat'>Access to query information is granted!</td>
  336. </tr>
  337. <tr>
  338. <td class='alt1'>".nl2br($message)."</td>
  339. </tr>
  340. </table>
  341. </div>";
  342. }
  343.  
  344. eval("dooutput(\"".tpl("db_error","htm",1)."\");");
  345. die("");
  346. }
  347.  
  348.  
  349.  
  350.  
  351.  
  352.  
  353.  
  354. function _microtime_used(&$before, &$after)
  355. {
  356. return (substr($after, 11) - substr($before, 11)) + (substr($after, 0, 9) - substr($before, 0, 9));
  357. }
  358.  
  359.  
  360. function generate_report($admin=0)
  361. {
  362. $cnt = count($this->query_history);
  363.  
  364. $total_time = 0;
  365.  
  366. $report = '<pre>'.
  367. BR.' SQL runtime report'.
  368. BR.' <hr>'.
  369. BR.' '.str_pad('Tag', 65, ' ').str_pad('Run time', 10, ' ').'Query'.
  370. BR.' <hr>';
  371.  
  372. for ($i=0; $i<$cnt; $i++)
  373. {
  374. $query = $this->query_history[$i];
  375.  
  376. $tag = '';
  377.  
  378. $query['file']
  379. and
  380. $tag .= '['.basename($query['file']).'] ';
  381.  
  382. $query['tag']
  383. and
  384. $tag .= $query['tag'];
  385.  
  386. $report .= BR.
  387. ' '.
  388. str_pad($tag , 65, ' ').
  389. str_pad(round($query['time'], 5), 10, ' ').
  390. substr($query['sql'], strpos($query['sql'], '*/')+3);
  391.  
  392. $total_time += $query['time'];
  393. }
  394.  
  395. $report .= BR.
  396. ' <hr>'.
  397. ' Total time taken for SQL: '.(round($total_time, 5)).' sec'.
  398. ' <hr>'.
  399. '</pre>';
  400.  
  401. return $report;
  402. }
  403.  
  404.  
  405. function run_time()
  406. {
  407. $total_time = 0;
  408. $cnt = count($this->query_history);
  409.  
  410. for($i=0; $i<$cnt; $i++)
  411. $total_time += $this->query_history[$i]['time'];
  412.  
  413. return $total_time;
  414. }
  415.  
  416.  
  417.  
  418.  
  419.  
  420.  
  421.  
  422.  
  423.  
  424.  
  425.  
  426. function transaction_start()
  427. {
  428. if($this->_transaction_started == TRUE)
  429. $this->transaction_end();
  430.  
  431. $query = 'SET AUTOCOMMIT=0';
  432. $this->query($query, 'start_transaction', __FILE__);
  433.  
  434. $query = 'BEGIN';
  435. $this->query($query, 'start_transaction', __FILE__);
  436.  
  437. $this->_transaction_started = TRUE;
  438. }
  439.  
  440. function transaction_end()
  441. {
  442. $query = 'COMMIT';
  443. $this->query($query, 'end_transaction', __FILE__);
  444.  
  445. $this->_transaction_started = FALSE;
  446. }
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454. //
  455. //
  456. //
  457. // Functions to use in the future.
  458. // Obsolete the functions declared above.
  459. //
  460. //
  461. //
  462.  
  463. //
  464. // Internal
  465. //
  466.  
  467. function _log($message)
  468. {
  469. global $ip;
  470.  
  471. file_put_contents($this->logfile, $message.' IP ADDRESS: '.$ip."\n\n\n", FILE_APPEND);
  472. }
  473.  
  474. //
  475. // String manipulation
  476. //
  477. function real_escape_string($text)
  478. {
  479. return mysql_real_escape_string($text, $this->linkid);
  480. }
  481.  
  482. function qstr($text)
  483. {
  484. return "'".$this->real_escape_string($text)."'";
  485. }
  486.  
  487.  
  488.  
  489.  
  490. //
  491. // Query shortcuts
  492. //
  493. function get_all($query, $desc=FALSE, $file=FALSE)
  494. {
  495. $result = array();
  496.  
  497. $rst = $this->query($query, $desc, $file);
  498. if($rst)
  499. {
  500. while($row = $this->fetch_array($rst))
  501. $result[] = $row;
  502.  
  503. $this->free_result($rst);
  504. }
  505.  
  506. return $result;
  507. }
  508.  
  509. function get_row($query, $desc=FALSE, $file=FALSE)
  510. {
  511. $result = $this->query($query, $desc, $file);
  512.  
  513. if($result == FALSE)
  514. return FALSE;
  515.  
  516. return $this->fetch_array($result);
  517. }
  518.  
  519. function query_first($query_string, $query_tag = '', $query_file = '')
  520. {
  521. $this->query($query_string, $query_tag, $query_file);
  522.  
  523. $returnarray=$this->fetch_array($this->query_result);
  524.  
  525. $this->free_result($this->query_result);
  526.  
  527. return $returnarray;
  528. }
  529.  
  530. function get_one($query_string, $query_tag = '', $query_file = '')
  531. {
  532. $this->query($query_string, $query_tag, $query_file);
  533.  
  534. $tmp = $this->fetch_row($this->query_result);
  535. $this->free_result($this->query_result);
  536.  
  537. return $tmp[0];
  538. }
  539. }
  540.  
  541. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement