PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/php/classes/class_DbConnect.php

http://easyapns.googlecode.com/
PHP | 467 lines | 205 code | 60 blank | 202 comment | 24 complexity | 8892b700329bd5ca05c49d7b1099ff9f MD5 | raw file
  1. <?PHP
  2. #################################################################################
  3. ## Developed by Manifest Interactive, LLC ##
  4. ## http://www.manifestinteractive.com ##
  5. ## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ##
  6. ## ##
  7. ## THIS SOFTWARE IS PROVIDED BY MANIFEST INTERACTIVE 'AS IS' AND ANY ##
  8. ## EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE ##
  9. ## IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR ##
  10. ## PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MANIFEST INTERACTIVE BE ##
  11. ## LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR ##
  12. ## CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF ##
  13. ## SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR ##
  14. ## BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, ##
  15. ## WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE ##
  16. ## OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, ##
  17. ## EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ##
  18. ## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ##
  19. ## Authors of file: Peter Schmalfeldt & John Kramlich ##
  20. #################################################################################
  21. /**
  22. * @category Apple Push Notification Service using PHP & MySQL
  23. * @package EasyAPNs
  24. * @author Peter Schmalfeldt <manifestinteractive@gmail.com>
  25. * @author John Kramlich <me@johnkramlich.com>
  26. * @license http://www.apache.org/licenses/LICENSE-2.0
  27. * @link http://code.google.com/p/easyapns/
  28. */
  29. /**
  30. * Begin Document
  31. */
  32. class DbConnect
  33. {
  34. /**
  35. * Connection to MySQL.
  36. *
  37. * @var string
  38. */
  39. var $link;
  40. /**
  41. * Holds the most recent connection.
  42. *
  43. * @var string
  44. */
  45. var $recent_link = null;
  46. /**
  47. * Holds the contents of the most recent SQL query.
  48. *
  49. * @var string
  50. */
  51. var $sql = '';
  52. /**
  53. * Holds the number of queries executed.
  54. *
  55. * @var integer
  56. */
  57. var $query_count = 0;
  58. /**
  59. * The text of the most recent database error message.
  60. *
  61. * @var string
  62. */
  63. var $error = '';
  64. /**
  65. * The error number of the most recent database error message.
  66. *
  67. * @var integer
  68. */
  69. var $errno = '';
  70. /**
  71. * Do we currently have a lock in place?
  72. *
  73. * @var boolean
  74. */
  75. var $is_locked = false;
  76. /**
  77. * Show errors? If set to true, the error message/sql is displayed.
  78. *
  79. * @var boolean
  80. */
  81. var $show_errors = false;
  82. /**
  83. * Log errors? If set to true, the error message/sql is logged.
  84. *
  85. * @var boolean
  86. */
  87. public $log_errors = false;
  88. /**
  89. * The Database.
  90. *
  91. * @var string
  92. */
  93. public $DB_DATABASE;
  94. /**
  95. * The variable used to contain a singleton instance of the database connection.
  96. *
  97. * @var string
  98. */
  99. static $instance;
  100. /**
  101. * The number of rows affected by the most recent query.
  102. *
  103. * @var string
  104. */
  105. public $affected_rows;
  106. public $insert_id;
  107. /**
  108. * Constructor. Initializes a database connection and selects our database.
  109. */
  110. function __construct()
  111. {
  112. $this->DB_HOST = 'localhost';
  113. $this->DB_USERNAME = 'MYUSERNAME'; // !!! CHANGE ME
  114. $this->DB_PASSWORD = 'MYPASSWORD'; // !!! CHANGE ME
  115. $this->DB_DATABASE = 'MYDATABASE'; // !!! CHANGE ME
  116. }
  117. /**
  118. * Singleton pattern to retrieve database connection.
  119. *
  120. * @return mixed MySQL database connection
  121. */
  122. function _get($property)
  123. {
  124. if(self::$instance == NULL)
  125. {
  126. self::$instance = $this->connect();
  127. }
  128. return self::$instance->$property;
  129. }
  130. /**
  131. * Singleton pattern to retrieve database connection.
  132. *
  133. * @return mixed MySQL database connection
  134. */
  135. function Connection()
  136. {
  137. if(self::$instance == NULL)
  138. {
  139. self::$instance = $this->connect();
  140. }
  141. return self::$instance;
  142. }
  143. /**
  144. * Connect to the Database.
  145. *
  146. */
  147. function connect()
  148. {
  149. self::$instance = new mysqli($this->DB_HOST, $this->DB_USERNAME, $this->DB_PASSWORD, $this->DB_DATABASE);
  150. if (mysqli_connect_errno()) {
  151. $this->raise_error(printf("Connect failed: %s\n", mysqli_connect_error()));
  152. }
  153. return self::$instance;
  154. }
  155. /**
  156. * Executes a sql query. If optional $only_first is set to true, it will
  157. * return the first row of the result as an array.
  158. *
  159. * @param string Query to run
  160. * @param bool Return only the first row, as an array?
  161. * @return mixed
  162. */
  163. function query($sql, $only_first = false)
  164. {
  165. if(self::$instance == NULL)
  166. {
  167. self::$instance = $this->connect();
  168. }
  169. $this->recent_link =& self::$instance;
  170. $this->sql =& $sql;
  171. if(!$result = self::$instance->query($sql))
  172. {
  173. $this->raise_error(printf("Connect failed: %s\n", self::$instance->error));
  174. }
  175. $this->affected_rows = self::$instance->affected_rows;
  176. $this->insert_id = self::$instance->insert_id;
  177. $this->query_count++;
  178. if ($only_first)
  179. {
  180. $return = $result->fetch_array(MYSQLI_ASSOC);
  181. $this->free_result($result);
  182. return $return;
  183. }
  184. return $result;
  185. }
  186. /**
  187. * Fetches a row from a query result and returns the values from that row as an array.
  188. *
  189. * @param string The query result we are dealing with.
  190. * @return array
  191. */
  192. function fetch_array($result)
  193. {
  194. return @mysql_fetch_assoc($result);
  195. }
  196. /**
  197. * Returns the number of rows in a result set.
  198. *
  199. * @param string The query result we are dealing with.
  200. * @return integer
  201. */
  202. function num_rows($result)
  203. {
  204. return self::$instance->num_rows;
  205. }
  206. /**
  207. * Retuns the number of rows affected by the most recent query
  208. *
  209. * @return integer
  210. */
  211. function affected_rows()
  212. {
  213. return self::$instance->affected_rows;
  214. }
  215. /**
  216. * Returns the number of queries executed.
  217. *
  218. * @param none
  219. * @return integer
  220. */
  221. function num_queries()
  222. {
  223. return $this->query_count;
  224. }
  225. /**
  226. * Lock database tables
  227. *
  228. * @param array Array of table => lock type
  229. * @return void
  230. */
  231. function lock($tables)
  232. {
  233. if (is_array($tables) AND count($tables))
  234. {
  235. $sql = '';
  236. foreach ($tables AS $name => $type)
  237. {
  238. $sql .= (!empty($sql) ? ', ' : '') . "$name $type";
  239. }
  240. $this->query("LOCK TABLES $sql");
  241. $this->is_locked = true;
  242. }
  243. }
  244. /**
  245. * Unlock tables
  246. */
  247. function unlock()
  248. {
  249. if ($this->is_locked)
  250. {
  251. $this->query("UNLOCK TABLES");
  252. }
  253. }
  254. /**
  255. * Returns the ID of the most recently inserted item in an auto_increment field
  256. *
  257. * @return integer
  258. */
  259. function insert_id()
  260. {
  261. return self::$instance->insert_id;
  262. }
  263. /**
  264. * Escapes a value to make it safe for using in queries.
  265. *
  266. * @param string Value to be escaped
  267. * @param bool Do we need to escape this string for a LIKE statement?
  268. * @return string
  269. */
  270. function prepare($value, $do_like = false)
  271. {
  272. if(self::$instance == NULL)
  273. {
  274. self::$instance = $this->connect();
  275. }
  276. if ($do_like)
  277. {
  278. $value = str_replace(array('%', '_'), array('\%', '\_'), $value);
  279. }
  280. return self::$instance->real_escape_string($value);
  281. }
  282. /**
  283. * Frees memory associated with a query result.
  284. *
  285. * @param string The query result we are dealing with.
  286. * @return boolean
  287. */
  288. function free_result($result)
  289. {
  290. return @mysql_free_result($result);
  291. }
  292. /**
  293. * Turns database error reporting on
  294. */
  295. function show_errors()
  296. {
  297. $this->show_errors = true;
  298. }
  299. /**
  300. * Turns database error reporting off
  301. */
  302. function hide_errors()
  303. {
  304. $this->show_errors = false;
  305. }
  306. /**
  307. * Closes our connection to MySQL.
  308. *
  309. * @param none
  310. * @return boolean
  311. */
  312. function close()
  313. {
  314. $this->sql = '';
  315. return self::$instance->close();
  316. }
  317. /**
  318. * Returns the MySQL error message.
  319. *
  320. * @param none
  321. * @return string
  322. */
  323. function error()
  324. {
  325. $this->error = (is_null($this->recent_link)) ? '' : self::$instance->error;
  326. return $this->error;
  327. }
  328. /**
  329. * Returns the MySQL error number.
  330. *
  331. * @param none
  332. * @return string
  333. */
  334. function errno()
  335. {
  336. $this->errno = (is_null($this->recent_link)) ? 0 : self::$instance->errno ;
  337. return $this->errno;
  338. }
  339. /**
  340. * Gets the url/path of where we are when a MySQL error occurs.
  341. *
  342. * @access private
  343. * @param none
  344. * @return string
  345. */
  346. function _get_error_path()
  347. {
  348. if ($_SERVER['REQUEST_URI'])
  349. {
  350. $errorpath = $_SERVER['REQUEST_URI'];
  351. }
  352. else
  353. {
  354. if ($_SERVER['PATH_INFO'])
  355. {
  356. $errorpath = $_SERVER['PATH_INFO'];
  357. }
  358. else
  359. {
  360. $errorpath = $_SERVER['PHP_SELF'];
  361. }
  362. if ($_SERVER['QUERY_STRING'])
  363. {
  364. $errorpath .= '?' . $_SERVER['QUERY_STRING'];
  365. }
  366. }
  367. if (($pos = strpos($errorpath, '?')) !== false)
  368. {
  369. $errorpath = urldecode(substr($errorpath, 0, $pos)) . substr($errorpath, $pos);
  370. }
  371. else
  372. {
  373. $errorpath = urldecode($errorpath);
  374. }
  375. return $_SERVER['HTTP_HOST'] . $errorpath;
  376. }
  377. /**
  378. * If there is a database error, the script will be stopped and an error message displayed.
  379. *
  380. * @param string The error message. If empty, one will be built with $this->sql.
  381. * @return string
  382. */
  383. function raise_error($error_message = '')
  384. {
  385. if ($this->recent_link)
  386. {
  387. $this->error = $this->error($this->recent_link);
  388. $this->errno = $this->errno($this->recent_link);
  389. }
  390. if ($error_message == '')
  391. {
  392. $this->sql = "Error in SQL query:\n\n" . rtrim($this->sql) . ';';
  393. $error_message =& $this->sql;
  394. }
  395. else
  396. {
  397. $error_message = $error_message . ($this->sql != '' ? "\n\nSQL:" . rtrim($this->sql) . ';' : '');
  398. }
  399. $message = "<textarea rows=\"10\" cols=\"80\">MySQL Error:\n\n\n$error_message\n\nError: {$this->error}\nError #: {$this->errno}\nFilename: " . $this->_get_error_path() . "\n</textarea>";
  400. if (!$this->show_errors)
  401. {
  402. $message = "<!--\n\n$message\n\n-->";
  403. }
  404. else die("There seems to have been a slight problem with our database, please try again later.<br /><br />\n$message");
  405. }
  406. }
  407. ?>