PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/firestats/lib/ezsql/mysql/ez_sql_mysql.php

https://github.com/alx/blogsfera
PHP | 330 lines | 212 code | 54 blank | 64 comment | 28 complexity | a8c915205a655b282070e0b817b2a445 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0
  1. <?php
  2. require_once(FS_ABS_PATH.'/php/utils.php');
  3. /**********************************************************************
  4. * Author: Justin Vincent (justin@visunet.ie)
  5. * Web...: http://php.justinvincent.com
  6. * Name..: ezSQL_mysql
  7. * Desc..: mySQL component (part of ezSQL databse abstraction library)
  8. *
  9. */
  10. /**********************************************************************
  11. * ezSQL Database specific class - mySQL
  12. */
  13. if ( ! function_exists ('mysql_connect') ) die('<b>Fatal Error:</b> ezSQL_mysql requires mySQL Lib to be compiled and or linked in to the PHP engine');
  14. if ( ! class_exists ('fs_ezSQLcore') ) die('<b>Fatal Error:</b> ezSQL_mysql requires ezSQLcore (ez_sql_core.php) to be included/loaded before it can be used');
  15. class fs_ezSQL_mysql extends fs_ezSQLcore
  16. {
  17. var $ezsql_mysql_str;
  18. var $dbuser = false;
  19. var $dbpassword = false;
  20. var $dbname = false;
  21. var $dbhost = false;
  22. /**********************************************************************
  23. * Constructor - allow the user to perform a qucik connect at the
  24. * same time as initialising the ezSQL_mysql class
  25. */
  26. function fs_ezSQL_mysql($dbuser='', $dbpassword='', $dbname='', $dbhost='localhost')
  27. {
  28. $this->dbuser = $dbuser;
  29. $this->dbpassword = $dbpassword;
  30. $this->dbname = $dbname;
  31. $this->dbhost = $dbhost;
  32. $this->ezsql_mysql_str = array
  33. (
  34. 1 => fs_r('User name and password are required to connect to database'),
  35. 2 => fs_r('Error establishing mySQL database connection'),
  36. 3 => fs_r('Database name is required to connect to database'),
  37. 4 => fs_r('mySQL database connection is not active'),
  38. 5 => fs_r('Unexpected error while querying database')
  39. );
  40. }
  41. function ensureConnected()
  42. {
  43. if (!$this->is_connected())
  44. {
  45. $this->connect();
  46. }
  47. }
  48. function connect()
  49. {
  50. $ret = $this->connectImpl($this->dbuser, $this->dbpassword, $this->dbhost);
  51. if ($ret)
  52. {
  53. $this->select($this->dbname);
  54. }
  55. return $ret;
  56. }
  57. function is_connected()
  58. {
  59. return isset($this->dbh) && $this->dbh;
  60. }
  61. function connectImpl($dbuser='', $dbpassword='', $dbhost='localhost', $add_debug_to_error = true)
  62. {
  63. $ezsql_mysql_str = $this->ezsql_mysql_str;
  64. $return_val = false;
  65. // Must have a user and a password
  66. if ( ! $dbuser )
  67. {
  68. $error = $ezsql_mysql_str[1];
  69. $error .= ($add_debug_to_error ? (' in '.__FILE__.' on line '.__LINE__) : '');
  70. $this->register_error($error) ;
  71. $this->show_errors ? trigger_error($ezsql_mysql_str[1],E_USER_WARNING) : null;
  72. }
  73. else
  74. {
  75. // Try to establish the server database handle
  76. ob_start(); // capture sql error
  77. $this->dbh = mysql_connect($dbhost,$dbuser,$dbpassword,true);
  78. $output = ob_get_clean();
  79. if ($this->dbh === false)
  80. {
  81. $mysql_error = mysql_error();
  82. $error = $ezsql_mysql_str[2].":".($mysql_error != '' ? $mysql_error : $output);
  83. $error .= ($add_debug_to_error ? (' in '.__FILE__.' on line '.__LINE__) : '');
  84. $this->register_error($error);
  85. $this->show_errors ? trigger_error($error,E_USER_WARNING) : null;
  86. }
  87. else
  88. {
  89. $this->dbuser = $dbuser;
  90. $this->dbpassword = $dbpassword;
  91. $this->dbhost = $dbhost;
  92. $return_val = true;
  93. }
  94. }
  95. return $return_val;
  96. }
  97. function disconnect()
  98. {
  99. if ($this->is_connected())
  100. {
  101. ob_start(); // capture sql error
  102. $res = mysql_close($this->dbh);
  103. unset($this->dbh);
  104. $output = ob_get_clean();
  105. if (!$res)
  106. {
  107. $this->register_error(sprintf(fs_r("Can't disconnect: %s"),$output));
  108. $this->show_errors ? trigger_error($error,E_USER_WARNING) : null;
  109. return false;
  110. }
  111. else
  112. {
  113. return true;
  114. }
  115. }
  116. else
  117. {
  118. $this->register_error(sprintf(fs_r("Can't disconnect: %s"),fs_r('Not connected')));
  119. $this->show_errors ? trigger_error($error,E_USER_WARNING) : null;
  120. return false;
  121. }
  122. }
  123. /**********************************************************************
  124. * Try to select a mySQL database
  125. */
  126. function select($dbname='')
  127. {
  128. $ezsql_mysql_str = $this->ezsql_mysql_str;
  129. $return_val = false;
  130. // Must have a database name
  131. if ( ! $dbname )
  132. {
  133. $this->register_error($ezsql_mysql_str[3].' in '.__FILE__.' on line '.__LINE__);
  134. $this->show_errors ? trigger_error($ezsql_mysql_str[3],E_USER_WARNING) : null;
  135. }
  136. // Must have an active database connection
  137. else if ( ! $this->dbh )
  138. {
  139. $this->register_error($ezsql_mysql_str[4].' in '.__FILE__.' on line '.__LINE__);
  140. $this->show_errors ? trigger_error($ezsql_mysql_str[4],E_USER_WARNING) : null;
  141. }
  142. // Try to connect to the database
  143. else if ( !@mysql_query("USE `$dbname`",$this->dbh) )
  144. {
  145. // Try to get error supplied by mysql if not use our own
  146. if ( !$str = @mysql_error($this->dbh))
  147. $str = $ezsql_mysql_str[5];
  148. $this->register_error($str.' in '.__FILE__.' on line '.__LINE__);
  149. $this->show_errors ? trigger_error($str,E_USER_WARNING) : null;
  150. }
  151. else
  152. {
  153. $this->dbname = $dbname;
  154. $return_val = true;
  155. }
  156. return $return_val;
  157. }
  158. /**********************************************************************
  159. * Format a mySQL string correctly for safe mySQL insert
  160. * (no mater if magic quotes are on or not)
  161. */
  162. function escape($value)
  163. {
  164. /*
  165. // If there is no existing database connection just do a dummy escape.
  166. if ( ! isset($this->dbh) || ! $this->dbh )
  167. {
  168. return "'$value'";
  169. }
  170. */
  171. // Stripslashes
  172. if (get_magic_quotes_gpc())
  173. {
  174. $value = stripslashes($value);
  175. }
  176. // Quote if not a number or a numeric string
  177. if (!is_numeric($value))
  178. {
  179. if(version_compare(phpversion(),"4.3.0")=="-1")
  180. {
  181. $value = mysql_escape_string($value);
  182. }
  183. else
  184. {
  185. $value = "'" . mysql_real_escape_string($value, $this->dbh) . "'";
  186. }
  187. }
  188. return $value;
  189. }
  190. /**********************************************************************
  191. * Return mySQL specific system date syntax
  192. * i.e. Oracle: SYSDATE Mysql: NOW()
  193. */
  194. function sysdate()
  195. {
  196. return 'NOW()';
  197. }
  198. /**********************************************************************
  199. * Perform mySQL query and try to detirmin result value
  200. */
  201. function query($query)
  202. {
  203. // Initialise return
  204. $return_val = 0;
  205. // Flush cached values..
  206. $this->flush();
  207. // For reg expressions
  208. $query = trim($query);
  209. // Log how the function was called
  210. $this->func_call = "\$db->query(\"$query\")";
  211. // Keep track of the last query for debug..
  212. $this->last_query = $query;
  213. // Count how many queries there have been
  214. $this->num_queries++;
  215. // Use core file cache function
  216. if ( $cache = $this->get_cache($query) )
  217. {
  218. return $cache;
  219. }
  220. // If there is no existing database connection then try to connect
  221. $this->ensureConnected();
  222. // Perform the query via std mysql_query function..
  223. $this->result = @mysql_query($query,$this->dbh);
  224. // If there is an error then take note of it..
  225. if ( $str = @mysql_error($this->dbh) )
  226. {
  227. $is_insert = true;
  228. $this->register_error($str);
  229. $this->show_errors ? trigger_error($str,E_USER_WARNING) : null;
  230. return false;
  231. }
  232. // Query was an insert, delete, update, replace
  233. $is_insert = false;
  234. if ( preg_match("/^(insert|delete|update|replace|load|start)\s+/i",$query)||strtolower($query) == 'commit')
  235. {
  236. $this->rows_affected = @mysql_affected_rows();
  237. // Take note of the insert_id
  238. if ( preg_match("/^(insert|replace)\s+/i",$query) )
  239. {
  240. $this->insert_id = @mysql_insert_id($this->dbh);
  241. }
  242. // Return number fo rows affected
  243. $return_val = $this->rows_affected;
  244. }
  245. // Query was a select
  246. else
  247. {
  248. // Take note of column info
  249. $i=0;
  250. while ($i < @mysql_num_fields($this->result))
  251. {
  252. $this->col_info[$i] = @mysql_fetch_field($this->result);
  253. $i++;
  254. }
  255. // Store Query Results
  256. $num_rows=0;
  257. while ( $row = @mysql_fetch_object($this->result) )
  258. {
  259. // Store relults as an objects within main array
  260. $this->last_result[$num_rows] = $row;
  261. $num_rows++;
  262. }
  263. @mysql_free_result($this->result);
  264. // Log number of rows the query returned
  265. $this->num_rows = $num_rows;
  266. // Return number of rows selected
  267. $return_val = $this->num_rows;
  268. }
  269. // disk caching of queries
  270. $this->store_cache($query,$is_insert);
  271. // If debug ALL queries
  272. $this->trace || $this->debug_all ? $this->debug() : null ;
  273. return $return_val;
  274. }
  275. }
  276. ?>