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

/administrator/components/com_virtuemart/classes/Log/sql.php

https://bitbucket.org/dgough/annamaria-daneswood-25102012
PHP | 272 lines | 91 code | 33 blank | 148 comment | 18 complexity | cace99ea1a2856b24eea413c634a59fe MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );
  3. /**
  4. *
  5. * @version $Id: sql.php 1336 2008-03-31 17:06:23Z soeren_nb $
  6. * @package VirtueMart
  7. * @subpackage Log
  8. * @copyright Copyright (C) 2004-2008 soeren - All rights reserved.
  9. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
  10. * VirtueMart is free software. This version may have been modified pursuant
  11. * to the GNU General Public License, and as distributed it includes or
  12. * is derivative of works licensed under the GNU General Public License or
  13. * other free or open source software licenses.
  14. * See /administrator/components/com_virtuemart/COPYRIGHT.php for copyright notices and details.
  15. *
  16. * http://virtuemart.net
  17. */
  18. /**
  19. * $Header$
  20. * $Horde: horde/lib/Log/sql.php,v 1.12 2000/08/16 20:27:34 chuck Exp $
  21. *
  22. * @version $ Revision: 1.36 $
  23. * @package Log
  24. */
  25. /** PEAR's DB package */
  26. require_once 'DB.php';
  27. /**
  28. * The vmLog_sql class is a concrete implementation of the Log::
  29. * abstract class which sends messages to an SQL server. Each entry
  30. * occupies a separate row in the database.
  31. *
  32. * This implementation uses PHP's PEAR database abstraction layer.
  33. *
  34. * CREATE TABLE log_table (
  35. * id INT NOT NULL,
  36. * logtime TIMESTAMP NOT NULL,
  37. * ident CHAR(16) NOT NULL,
  38. * priority INT NOT NULL,
  39. * message VARCHAR(200),
  40. * PRIMARY KEY (id)
  41. * );
  42. *
  43. * @author Jon Parise <jon@php.net>
  44. * @since Horde 1.3
  45. * @since Log 1.0
  46. * @package Log
  47. *
  48. * @example sql.php Using the SQL handler.
  49. */
  50. class vmLog_sql extends vmLog
  51. {
  52. /**
  53. * Variable containing the DSN information.
  54. * @var mixed
  55. * @access private
  56. */
  57. var $_dsn = '';
  58. /**
  59. * Array containing our set of DB configuration options.
  60. * @var array
  61. * @access private
  62. */
  63. var $_options = array('persistent' => true);
  64. /**
  65. * Object holding the database handle.
  66. * @var object
  67. * @access private
  68. */
  69. var $_db = null;
  70. /**
  71. * Resource holding the prepared statement handle.
  72. * @var resource
  73. * @access private
  74. */
  75. var $_statement = null;
  76. /**
  77. * Flag indicating that we're using an existing database connection.
  78. * @var boolean
  79. * @access private
  80. */
  81. var $_existingConnection = false;
  82. /**
  83. * String holding the database table to use.
  84. * @var string
  85. * @access private
  86. */
  87. var $_table = 'log_table';
  88. /**
  89. * String holding the name of the ID sequence.
  90. * @var string
  91. * @access private
  92. */
  93. var $_sequence = 'log_id';
  94. /**
  95. * Maximum length of the $ident string. This corresponds to the size of
  96. * the 'ident' column in the SQL table.
  97. * @var integer
  98. * @access private
  99. */
  100. var $_identLimit = 16;
  101. /**
  102. * Constructs a new sql logging object.
  103. *
  104. * @param string $name The target SQL table.
  105. * @param string $ident The identification field.
  106. * @param array $conf The connection configuration array.
  107. * @param int $level Log messages up to and including this level.
  108. * @access public
  109. */
  110. function vmLog_sql($name, $ident = '', $conf = array(),
  111. $level = PEAR_LOG_DEBUG)
  112. {
  113. $this->_id = md5(microtime());
  114. $this->_table = $name;
  115. $this->_mask = vmLog::UPTO($level);
  116. /* If an options array was provided, use it. */
  117. if (isset($conf['options']) && is_array($conf['options']))
  118. {
  119. $this->_options = $conf['options'];
  120. }
  121. /* If a specific sequence name was provided, use it. */
  122. if (!empty($conf['sequence'])) {
  123. $this->_sequence = $conf['sequence'];
  124. }
  125. /* If a specific sequence name was provided, use it. */
  126. if (isset($conf['identLimit'])) {
  127. $this->_identLimit = $conf['identLimit'];
  128. }
  129. /* Now that the ident limit is confirmed, set the ident string. */
  130. $this->setIdent($ident);
  131. /* If an existing database connection was provided, use it. */
  132. if (isset($conf['db'])) {
  133. $this->_db = &$conf['db'];
  134. $this->_existingConnection = true;
  135. $this->_opened = true;
  136. } else {
  137. $this->_dsn = $conf['dsn'];
  138. }
  139. }
  140. /**
  141. * Opens a connection to the database, if it has not already
  142. * been opened. This is implicitly called by log(), if necessary.
  143. *
  144. * @return boolean True on success, false on failure.
  145. * @access public
  146. */
  147. function open()
  148. {
  149. if (!$this->_opened) {
  150. /* Use the DSN and options to create a database connection. */
  151. $this->_db = &DB::connect($this->_dsn, $this->_options);
  152. if (DB::isError($this->_db)) {
  153. return false;
  154. }
  155. /* Create a prepared statement for repeated use in log(). */
  156. $this->_statement =
  157. $this->_db->prepare('INSERT INTO ' . $this->_table .
  158. ' (id, logtime, ident, priority, message)' .
  159. ' VALUES(?, CURRENT_TIMESTAMP, ?, ?, ?)');
  160. if (DB::isError($this->_statement)) {
  161. return false;
  162. }
  163. /* We now consider out connection open. */
  164. $this->_opened = true;
  165. }
  166. return $this->_opened;
  167. }
  168. /**
  169. * Closes the connection to the database if it is still open and we were
  170. * the ones that opened it. It is the caller's responsible to close an
  171. * existing connection that was passed to us via $conf['db'].
  172. *
  173. * @return boolean True on success, false on failure.
  174. * @access public
  175. */
  176. function close()
  177. {
  178. if ($this->_opened && !$this->_existingConnection) {
  179. $this->_opened = false;
  180. $this->_db->freePrepared($this->_statement);
  181. return $this->_db->disconnect();
  182. }
  183. return ($this->_opened === false);
  184. }
  185. /**
  186. * Sets this Log instance's identification string. Note that this
  187. * SQL-specific implementation will limit the length of the $ident string
  188. * to sixteen (16) characters.
  189. *
  190. * @param string $ident The new identification string.
  191. *
  192. * @access public
  193. * @since Log 1.8.5
  194. */
  195. function setIdent($ident)
  196. {
  197. $this->_ident = substr($ident, 0, $this->_identLimit);
  198. }
  199. /**
  200. * Inserts $message to the currently open database. Calls open(),
  201. * if necessary. Also passes the message along to any Log_observer
  202. * instances that are observing this Log.
  203. *
  204. * @param mixed $message String or object containing the message to log.
  205. * @param string $priority The priority of the message. Valid
  206. * values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT,
  207. * PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING,
  208. * PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
  209. * @return boolean True on success or false on failure.
  210. * @access public
  211. */
  212. function log($message, $priority = null)
  213. {
  214. /* If a priority hasn't been specified, use the default value. */
  215. if ($priority === null) {
  216. $priority = $this->_priority;
  217. }
  218. /* Abort early if the priority is above the maximum logging level. */
  219. if (!$this->_isMasked($priority)) {
  220. return false;
  221. }
  222. /* If the connection isn't open and can't be opened, return failure. */
  223. if (!$this->_opened && !$this->open()) {
  224. return false;
  225. }
  226. /* Extract the string representation of the message. */
  227. $message = $this->_extractMessage($message);
  228. /* Build our set of values for this log entry. */
  229. $id = $this->_db->nextId($this->_sequence);
  230. $values = array($id, $this->_ident, $priority, $message);
  231. /* Execute the SQL query for this log entry insertion. */
  232. $result =& $this->_db->execute($this->_statement, $values);
  233. if (DB::isError($result)) {
  234. return false;
  235. }
  236. $this->_announce(array('priority' => $priority, 'message' => $message));
  237. return true;
  238. }
  239. }