PageRenderTime 38ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/forum/includes/db/mysql.php

https://bitbucket.org/itoxable/chiron-gaming
PHP | 492 lines | 319 code | 76 blank | 97 comment | 63 complexity | 11dc82461b5b3c97bc30684e40af5e28 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0
  1. <?php
  2. /**
  3. *
  4. * @package dbal
  5. * @version $Id$
  6. * @copyright (c) 2005 phpBB Group
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. /**
  11. * @ignore
  12. */
  13. if (!defined('IN_PHPBB'))
  14. {
  15. exit;
  16. }
  17. include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
  18. /**
  19. * MySQL4 Database Abstraction Layer
  20. * Compatible with:
  21. * MySQL 3.23+
  22. * MySQL 4.0+
  23. * MySQL 4.1+
  24. * MySQL 5.0+
  25. * @package dbal
  26. */
  27. class dbal_mysql extends dbal
  28. {
  29. var $multi_insert = true;
  30. /**
  31. * Connect to server
  32. * @access public
  33. */
  34. function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
  35. {
  36. $this->persistency = $persistency;
  37. $this->user = $sqluser;
  38. $this->server = $sqlserver . (($port) ? ':' . $port : '');
  39. $this->dbname = $database;
  40. $this->sql_layer = 'mysql4';
  41. $this->db_connect_id = ($this->persistency) ? @mysql_pconnect($this->server, $this->user, $sqlpassword) : @mysql_connect($this->server, $this->user, $sqlpassword, $new_link);
  42. if ($this->db_connect_id && $this->dbname != '')
  43. {
  44. if (@mysql_select_db($this->dbname, $this->db_connect_id))
  45. {
  46. // Determine what version we are using and if it natively supports UNICODE
  47. if (version_compare($this->sql_server_info(true), '4.1.0', '>='))
  48. {
  49. @mysql_query("SET NAMES 'utf8'", $this->db_connect_id);
  50. // enforce strict mode on databases that support it
  51. if (version_compare($this->sql_server_info(true), '5.0.2', '>='))
  52. {
  53. $result = @mysql_query('SELECT @@session.sql_mode AS sql_mode', $this->db_connect_id);
  54. $row = @mysql_fetch_assoc($result);
  55. @mysql_free_result($result);
  56. $modes = array_map('trim', explode(',', $row['sql_mode']));
  57. // TRADITIONAL includes STRICT_ALL_TABLES and STRICT_TRANS_TABLES
  58. if (!in_array('TRADITIONAL', $modes))
  59. {
  60. if (!in_array('STRICT_ALL_TABLES', $modes))
  61. {
  62. $modes[] = 'STRICT_ALL_TABLES';
  63. }
  64. if (!in_array('STRICT_TRANS_TABLES', $modes))
  65. {
  66. $modes[] = 'STRICT_TRANS_TABLES';
  67. }
  68. }
  69. $mode = implode(',', $modes);
  70. @mysql_query("SET SESSION sql_mode='{$mode}'", $this->db_connect_id);
  71. }
  72. }
  73. else if (version_compare($this->sql_server_info(true), '4.0.0', '<'))
  74. {
  75. $this->sql_layer = 'mysql';
  76. }
  77. return $this->db_connect_id;
  78. }
  79. }
  80. return $this->sql_error('');
  81. }
  82. /**
  83. * Version information about used database
  84. * @param bool $raw if true, only return the fetched sql_server_version
  85. * @param bool $use_cache If true, it is safe to retrieve the value from the cache
  86. * @return string sql server version
  87. */
  88. function sql_server_info($raw = false, $use_cache = true)
  89. {
  90. global $cache;
  91. if (!$use_cache || empty($cache) || ($this->sql_server_version = $cache->get('mysql_version')) === false)
  92. {
  93. $result = @mysql_query('SELECT VERSION() AS version', $this->db_connect_id);
  94. $row = @mysql_fetch_assoc($result);
  95. @mysql_free_result($result);
  96. $this->sql_server_version = $row['version'];
  97. if (!empty($cache) && $use_cache)
  98. {
  99. $cache->put('mysql_version', $this->sql_server_version);
  100. }
  101. }
  102. return ($raw) ? $this->sql_server_version : 'MySQL ' . $this->sql_server_version;
  103. }
  104. /**
  105. * SQL Transaction
  106. * @access private
  107. */
  108. function _sql_transaction($status = 'begin')
  109. {
  110. switch ($status)
  111. {
  112. case 'begin':
  113. return @mysql_query('BEGIN', $this->db_connect_id);
  114. break;
  115. case 'commit':
  116. return @mysql_query('COMMIT', $this->db_connect_id);
  117. break;
  118. case 'rollback':
  119. return @mysql_query('ROLLBACK', $this->db_connect_id);
  120. break;
  121. }
  122. return true;
  123. }
  124. /**
  125. * Base query method
  126. *
  127. * @param string $query Contains the SQL query which shall be executed
  128. * @param int $cache_ttl Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
  129. * @return mixed When casted to bool the returned value returns true on success and false on failure
  130. *
  131. * @access public
  132. */
  133. function sql_query($query = '', $cache_ttl = 0)
  134. {
  135. if ($query != '')
  136. {
  137. global $cache;
  138. // EXPLAIN only in extra debug mode
  139. if (defined('DEBUG_EXTRA'))
  140. {
  141. $this->sql_report('start', $query);
  142. }
  143. $this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
  144. $this->sql_add_num_queries($this->query_result);
  145. if ($this->query_result === false)
  146. {
  147. if (($this->query_result = @mysql_query($query, $this->db_connect_id)) === false)
  148. {
  149. $this->sql_error($query);
  150. }
  151. if (defined('DEBUG_EXTRA'))
  152. {
  153. $this->sql_report('stop', $query);
  154. }
  155. if ($cache_ttl && method_exists($cache, 'sql_save'))
  156. {
  157. $this->open_queries[(int) $this->query_result] = $this->query_result;
  158. $cache->sql_save($query, $this->query_result, $cache_ttl);
  159. }
  160. else if (strpos($query, 'SELECT') === 0 && $this->query_result)
  161. {
  162. $this->open_queries[(int) $this->query_result] = $this->query_result;
  163. }
  164. }
  165. else if (defined('DEBUG_EXTRA'))
  166. {
  167. $this->sql_report('fromcache', $query);
  168. }
  169. }
  170. else
  171. {
  172. return false;
  173. }
  174. return $this->query_result;
  175. }
  176. /**
  177. * Build LIMIT query
  178. */
  179. function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
  180. {
  181. $this->query_result = false;
  182. // if $total is set to 0 we do not want to limit the number of rows
  183. if ($total == 0)
  184. {
  185. // Having a value of -1 was always a bug
  186. $total = '18446744073709551615';
  187. }
  188. $query .= "\n LIMIT " . ((!empty($offset)) ? $offset . ', ' . $total : $total);
  189. return $this->sql_query($query, $cache_ttl);
  190. }
  191. /**
  192. * Return number of affected rows
  193. */
  194. function sql_affectedrows()
  195. {
  196. return ($this->db_connect_id) ? @mysql_affected_rows($this->db_connect_id) : false;
  197. }
  198. /**
  199. * Fetch current row
  200. */
  201. function sql_fetchrow($query_id = false)
  202. {
  203. global $cache;
  204. if ($query_id === false)
  205. {
  206. $query_id = $this->query_result;
  207. }
  208. if (isset($cache->sql_rowset[$query_id]))
  209. {
  210. return $cache->sql_fetchrow($query_id);
  211. }
  212. return ($query_id !== false) ? @mysql_fetch_assoc($query_id) : false;
  213. }
  214. /**
  215. * Seek to given row number
  216. * rownum is zero-based
  217. */
  218. function sql_rowseek($rownum, &$query_id)
  219. {
  220. global $cache;
  221. if ($query_id === false)
  222. {
  223. $query_id = $this->query_result;
  224. }
  225. if (isset($cache->sql_rowset[$query_id]))
  226. {
  227. return $cache->sql_rowseek($rownum, $query_id);
  228. }
  229. return ($query_id !== false) ? @mysql_data_seek($query_id, $rownum) : false;
  230. }
  231. /**
  232. * Get last inserted id after insert statement
  233. */
  234. function sql_nextid()
  235. {
  236. return ($this->db_connect_id) ? @mysql_insert_id($this->db_connect_id) : false;
  237. }
  238. /**
  239. * Free sql result
  240. */
  241. function sql_freeresult($query_id = false)
  242. {
  243. global $cache;
  244. if ($query_id === false)
  245. {
  246. $query_id = $this->query_result;
  247. }
  248. if (isset($cache->sql_rowset[$query_id]))
  249. {
  250. return $cache->sql_freeresult($query_id);
  251. }
  252. if (isset($this->open_queries[(int) $query_id]))
  253. {
  254. unset($this->open_queries[(int) $query_id]);
  255. return @mysql_free_result($query_id);
  256. }
  257. return false;
  258. }
  259. /**
  260. * Escape string used in sql query
  261. */
  262. function sql_escape($msg)
  263. {
  264. if (!$this->db_connect_id)
  265. {
  266. return @mysql_real_escape_string($msg);
  267. }
  268. return @mysql_real_escape_string($msg, $this->db_connect_id);
  269. }
  270. /**
  271. * Build LIKE expression
  272. * @access private
  273. */
  274. function _sql_like_expression($expression)
  275. {
  276. return $expression;
  277. }
  278. /**
  279. * Build db-specific query data
  280. * @access private
  281. */
  282. function _sql_custom_build($stage, $data)
  283. {
  284. switch ($stage)
  285. {
  286. case 'FROM':
  287. $data = '(' . $data . ')';
  288. break;
  289. }
  290. return $data;
  291. }
  292. /**
  293. * return sql error array
  294. * @access private
  295. */
  296. function _sql_error()
  297. {
  298. if (!$this->db_connect_id)
  299. {
  300. return array(
  301. 'message' => @mysql_error(),
  302. 'code' => @mysql_errno()
  303. );
  304. }
  305. return array(
  306. 'message' => @mysql_error($this->db_connect_id),
  307. 'code' => @mysql_errno($this->db_connect_id)
  308. );
  309. }
  310. /**
  311. * Close sql connection
  312. * @access private
  313. */
  314. function _sql_close()
  315. {
  316. return @mysql_close($this->db_connect_id);
  317. }
  318. /**
  319. * Build db-specific report
  320. * @access private
  321. */
  322. function _sql_report($mode, $query = '')
  323. {
  324. static $test_prof;
  325. // current detection method, might just switch to see the existance of INFORMATION_SCHEMA.PROFILING
  326. if ($test_prof === null)
  327. {
  328. $test_prof = false;
  329. if (version_compare($this->sql_server_info(true), '5.0.37', '>=') && version_compare($this->sql_server_info(true), '5.1', '<'))
  330. {
  331. $test_prof = true;
  332. }
  333. }
  334. switch ($mode)
  335. {
  336. case 'start':
  337. $explain_query = $query;
  338. if (preg_match('/UPDATE ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
  339. {
  340. $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
  341. }
  342. else if (preg_match('/DELETE FROM ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
  343. {
  344. $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
  345. }
  346. if (preg_match('/^SELECT/', $explain_query))
  347. {
  348. $html_table = false;
  349. // begin profiling
  350. if ($test_prof)
  351. {
  352. @mysql_query('SET profiling = 1;', $this->db_connect_id);
  353. }
  354. if ($result = @mysql_query("EXPLAIN $explain_query", $this->db_connect_id))
  355. {
  356. while ($row = @mysql_fetch_assoc($result))
  357. {
  358. $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
  359. }
  360. }
  361. @mysql_free_result($result);
  362. if ($html_table)
  363. {
  364. $this->html_hold .= '</table>';
  365. }
  366. if ($test_prof)
  367. {
  368. $html_table = false;
  369. // get the last profile
  370. if ($result = @mysql_query('SHOW PROFILE ALL;', $this->db_connect_id))
  371. {
  372. $this->html_hold .= '<br />';
  373. while ($row = @mysql_fetch_assoc($result))
  374. {
  375. // make <unknown> HTML safe
  376. if (!empty($row['Source_function']))
  377. {
  378. $row['Source_function'] = str_replace(array('<', '>'), array('&lt;', '&gt;'), $row['Source_function']);
  379. }
  380. // remove unsupported features
  381. foreach ($row as $key => $val)
  382. {
  383. if ($val === null)
  384. {
  385. unset($row[$key]);
  386. }
  387. }
  388. $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
  389. }
  390. }
  391. @mysql_free_result($result);
  392. if ($html_table)
  393. {
  394. $this->html_hold .= '</table>';
  395. }
  396. @mysql_query('SET profiling = 0;', $this->db_connect_id);
  397. }
  398. }
  399. break;
  400. case 'fromcache':
  401. $endtime = explode(' ', microtime());
  402. $endtime = $endtime[0] + $endtime[1];
  403. $result = @mysql_query($query, $this->db_connect_id);
  404. while ($void = @mysql_fetch_assoc($result))
  405. {
  406. // Take the time spent on parsing rows into account
  407. }
  408. @mysql_free_result($result);
  409. $splittime = explode(' ', microtime());
  410. $splittime = $splittime[0] + $splittime[1];
  411. $this->sql_report('record_fromcache', $query, $endtime, $splittime);
  412. break;
  413. }
  414. }
  415. }
  416. ?>