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

/lib/php/Log/sqlite.php

https://bitbucket.org/adarshj/convenient_website
PHP | 238 lines | 99 code | 25 blank | 114 comment | 17 complexity | 3ac714450f35d6bff88923c2474d22e8 MD5 | raw file
Possible License(s): Apache-2.0, MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-2-Clause, GPL-2.0, LGPL-3.0
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4.0 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available at through the world-wide-web at |
  11. // | http://www.php.net/license/2_02.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | license@php.net so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Bertrand Mansion <bmansion@mamasam.com> |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: sqlite.php,v 1.3 2004/01/19 08:02:40 jon Exp $
  20. /**
  21. * The Log_sqlite class is a concrete implementation of the Log::
  22. * abstract class which sends messages to an Sqlite database.
  23. * Each entry occupies a separate row in the database.
  24. *
  25. * This implementation uses PHP native Sqlite functions.
  26. *
  27. * CREATE TABLE log_table (
  28. * id INTEGER PRIMARY KEY NOT NULL,
  29. * logtime NOT NULL,
  30. * ident CHAR(16) NOT NULL,
  31. * priority INT NOT NULL,
  32. * message
  33. * );
  34. *
  35. * @author Bertrand Mansion <bmansion@mamasam.com>
  36. * @author Jon Parise <jon@php.net>
  37. * @since Log 1.8.3
  38. * @package Log
  39. *
  40. * @example sqlite.php Using the Sqlite handler.
  41. */
  42. class Log_sqlite extends Log
  43. {
  44. /**
  45. * Array containing the connection defaults
  46. * @var array
  47. * @access private
  48. */
  49. var $_options = array('mode' => 0666,
  50. 'persistent' => false);
  51. /**
  52. * Object holding the database handle.
  53. * @var object
  54. * @access private
  55. */
  56. var $_db = null;
  57. /**
  58. * Flag indicating that we're using an existing database connection.
  59. * @var boolean
  60. * @access private
  61. */
  62. var $_existingConnection = false;
  63. /**
  64. * String holding the database table to use.
  65. * @var string
  66. * @access private
  67. */
  68. var $_table = 'log_table';
  69. /**
  70. * Constructs a new sql logging object.
  71. *
  72. * @param string $name The target SQL table.
  73. * @param string $ident The identification field.
  74. * @param mixed $conf Can be an array of configuration options used
  75. * to open a new database connection
  76. * or an already opened sqlite connection.
  77. * @param int $level Log messages up to and including this level.
  78. * @access public
  79. */
  80. function Log_sqlite($name, $ident = '', &$conf, $level = PEAR_LOG_DEBUG)
  81. {
  82. $this->_id = md5(microtime());
  83. $this->_table = $name;
  84. $this->_ident = $ident;
  85. $this->_mask = Log::UPTO($level);
  86. if (is_array($conf)) {
  87. foreach ($conf as $k => $opt) {
  88. $this->_options[$k] = $opt;
  89. }
  90. } else {
  91. // If an existing database connection was provided, use it.
  92. $this->_db =& $conf;
  93. $this->_existingConnection = true;
  94. }
  95. }
  96. /**
  97. * Opens a connection to the database, if it has not already
  98. * been opened. This is implicitly called by log(), if necessary.
  99. *
  100. * @return boolean True on success, false on failure.
  101. * @access public
  102. */
  103. function open()
  104. {
  105. if (is_resource($this->_db)) {
  106. $this->_opened = true;
  107. return $this->_createTable();
  108. } else {
  109. /* Set the connection function based on the 'persistent' option. */
  110. if (empty($this->_options['persistent'])) {
  111. $connectFunction = 'sqlite_open';
  112. } else {
  113. $connectFunction = 'sqlite_popen';
  114. }
  115. /* Attempt to connect to the database. */
  116. if ($this->_db = $connectFunction($this->_options['filename'],
  117. (int)$this->_options['mode'],
  118. $error)) {
  119. $this->_opened = true;
  120. return $this->_createTable();
  121. }
  122. }
  123. return $this->_opened;
  124. }
  125. /**
  126. * Closes the connection to the database if it is still open and we were
  127. * the ones that opened it. It is the caller's responsible to close an
  128. * existing connection that was passed to us via $conf['db'].
  129. *
  130. * @return boolean True on success, false on failure.
  131. * @access public
  132. */
  133. function close()
  134. {
  135. /* We never close existing connections. */
  136. if ($this->_existingConnection) {
  137. return false;
  138. }
  139. if ($this->_opened) {
  140. $this->_opened = false;
  141. sqlite_close($this->_db);
  142. }
  143. return ($this->_opened === false);
  144. }
  145. /**
  146. * Inserts $message to the currently open database. Calls open(),
  147. * if necessary. Also passes the message along to any Log_observer
  148. * instances that are observing this Log.
  149. *
  150. * @param mixed $message String or object containing the message to log.
  151. * @param string $priority The priority of the message. Valid
  152. * values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT,
  153. * PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING,
  154. * PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
  155. * @return boolean True on success or false on failure.
  156. * @access public
  157. */
  158. function log($message, $priority = null)
  159. {
  160. /* If a priority hasn't been specified, use the default value. */
  161. if ($priority === null) {
  162. $priority = $this->_priority;
  163. }
  164. /* Abort early if the priority is above the maximum logging level. */
  165. if (!$this->_isMasked($priority)) {
  166. return false;
  167. }
  168. /* If the connection isn't open and can't be opened, return failure. */
  169. if (!$this->_opened && !$this->open()) {
  170. return false;
  171. }
  172. // Extract the string representation of the message.
  173. $message = $this->_extractMessage($message);
  174. // Build the SQL query for this log entry insertion.
  175. $q = sprintf('INSERT INTO [%s] (logtime, ident, priority, message) ' .
  176. "VALUES ('%s', '%s', %d, '%s')",
  177. $this->_table,
  178. strftime('%Y-%m-%d %H:%M:%S', time()),
  179. sqlite_escape_string($this->_ident),
  180. $priority,
  181. sqlite_escape_string($message));
  182. if (!($res = @sqlite_unbuffered_query($this->_db, $q))) {
  183. return false;
  184. }
  185. $this->_announce(array('priority' => $priority, 'message' => $message));
  186. return true;
  187. }
  188. /**
  189. * Checks whether the log table exists and creates it if necessary.
  190. *
  191. * @return boolean True on success or false on failure.
  192. * @access private
  193. */
  194. function _createTable()
  195. {
  196. $q = "SELECT name FROM sqlite_master WHERE name='" . $this->_table .
  197. "' AND type='table'";
  198. $res = sqlite_query($this->_db, $q);
  199. if (sqlite_num_rows($res) == 0) {
  200. $q = 'CREATE TABLE [' . $this->_table . '] (' .
  201. 'id INTEGER PRIMARY KEY NOT NULL, ' .
  202. 'logtime NOT NULL, ' .
  203. 'ident CHAR(16) NOT NULL, ' .
  204. 'priority INT NOT NULL, ' .
  205. 'message)';
  206. if (!($res = sqlite_unbuffered_query($this->_db, $q))) {
  207. return false;
  208. }
  209. }
  210. return true;
  211. }
  212. }
  213. ?>