PageRenderTime 57ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/log/loggers/database.php

http://github.com/joomla/joomla-platform
PHP | 172 lines | 73 code | 20 blank | 79 comment | 5 complexity | b3e65f546293fb283e880340b595204c MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Log
  5. *
  6. * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. jimport('joomla.log.logger');
  11. /**
  12. * Joomla! MySQL Database Log class
  13. *
  14. * This class is designed to output logs to a specific MySQL database table. Fields in this
  15. * table are based on the SysLog style of log output. This is designed to allow quick and
  16. * easy searching.
  17. *
  18. * @package Joomla.Platform
  19. * @subpackage Log
  20. * @since 11.1
  21. */
  22. class JLoggerDatabase extends JLogger
  23. {
  24. /**
  25. * @var string The name of the database driver to use for connecting to the database.
  26. * @since 11.1
  27. */
  28. protected $driver = 'mysql';
  29. /**
  30. * @var string The host name (or IP) of the server with which to connect for the logger.
  31. * @since 11.1
  32. */
  33. protected $host = '127.0.0.1';
  34. /**
  35. * @var string The database server user to connect as for the logger.
  36. * @since 11.1
  37. */
  38. protected $user = 'root';
  39. /**
  40. * @var string The password to use for connecting to the database server.
  41. * @since 11.1
  42. */
  43. protected $password = '';
  44. /**
  45. * @var string The name of the database table to use for the logger.
  46. * @since 11.1
  47. */
  48. protected $database = 'logging';
  49. /**
  50. * @var string The database table to use for logging entries.
  51. * @since 11.1
  52. */
  53. protected $table = 'jos_';
  54. /**
  55. * @var JDatabase The database connection object for the logger.
  56. * @since 11.1
  57. */
  58. protected $dbo;
  59. /**
  60. * Constructor.
  61. *
  62. * @param array &$options Log object options.
  63. *
  64. * @since 11.1
  65. * @throws LogException
  66. */
  67. public function __construct(array &$options)
  68. {
  69. // Call the parent constructor.
  70. parent::__construct($options);
  71. // If both the database object and driver options are empty we want to use the system database connection.
  72. if (empty($this->options['db_object']) && empty($this->options['db_driver']))
  73. {
  74. $this->dbo = JFactory::getDBO();
  75. $this->driver = JFactory::getConfig()->get('dbtype');
  76. $this->host = JFactory::getConfig()->get('host');
  77. $this->user = JFactory::getConfig()->get('user');
  78. $this->password = JFactory::getConfig()->get('password');
  79. $this->database = JFactory::getConfig()->get('db');
  80. $this->prefix = JFactory::getConfig()->get('dbprefix');
  81. }
  82. // We need to get the database connection settings from the configuration options.
  83. else
  84. {
  85. $this->driver = (empty($this->options['db_driver'])) ? 'mysql' : $this->options['db_driver'];
  86. $this->host = (empty($this->options['db_host'])) ? '127.0.0.1' : $this->options['db_host'];
  87. $this->user = (empty($this->options['db_user'])) ? 'root' : $this->options['db_user'];
  88. $this->password = (empty($this->options['db_pass'])) ? '' : $this->options['db_pass'];
  89. $this->database = (empty($this->options['db_database'])) ? 'logging' : $this->options['db_database'];
  90. $this->prefix = (empty($this->options['db_prefix'])) ? 'jos_' : $this->options['db_prefix'];
  91. }
  92. // The table name is independent of how we arrived at the connection object.
  93. $this->table = (empty($this->options['db_table'])) ? '#__log_entries' : $this->options['db_table'];
  94. }
  95. /**
  96. * Method to add an entry to the log.
  97. *
  98. * @param JLogEntry $entry The log entry object to add to the log.
  99. *
  100. * @return void
  101. *
  102. * @since 11.1
  103. */
  104. public function addEntry(JLogEntry $entry)
  105. {
  106. // Connect to the database if not connected.
  107. if (empty($this->dbo))
  108. {
  109. $this->connect();
  110. }
  111. // Convert the date.
  112. $entry->date = $entry->date->toSql();
  113. $this->dbo->insertObject($this->table, $entry);
  114. }
  115. /**
  116. * Method to connect to the database server based on object properties.
  117. *
  118. * @return void
  119. *
  120. * @since 11.1
  121. * @throws LogException
  122. */
  123. protected function connect()
  124. {
  125. // Build the configuration object to use for JDatabase.
  126. $options = array(
  127. 'driver' => $this->driver,
  128. 'host' => $this->host,
  129. 'user' => $this->user,
  130. 'password' => $this->password,
  131. 'database' => $this->database,
  132. 'prefix' => $this->prefix);
  133. try
  134. {
  135. $db = JDatabase::getInstance($options);
  136. if ($db instanceof Exception)
  137. {
  138. throw new LogException('Database Error: ' . (string) $db);
  139. }
  140. if ($db->getErrorNum() > 0)
  141. {
  142. throw new LogException(JText::sprintf('JLIB_UTIL_ERROR_CONNECT_DATABASE', $db->getErrorNum(), $db->getErrorMsg()));
  143. }
  144. // Assign the database connector to the class.
  145. $this->dbo = $db;
  146. }
  147. catch (JDatabaseException $e)
  148. {
  149. throw new LogException($e->getMessage());
  150. }
  151. }
  152. }