1 <?php
2 /**
3 * @package Joomla.Platform
4 * @subpackage Log
5 *
6 * @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 /**
13 * Joomla! MySQL Database Log class
14 *
15 * This class is designed to output logs to a specific MySQL database table. Fields in this
16 * table are based on the Syslog style of log output. This is designed to allow quick and
17 * easy searching.
18 *
19 * @since 11.1
20 */
21 class JLogLoggerDatabase extends JLogLogger
22 {
23 /**
24 * The name of the database driver to use for connecting to the database.
25 *
26 * @var string
27 * @since 11.1
28 */
29 protected $driver = 'mysqli';
30
31 /**
32 * The host name (or IP) of the server with which to connect for the logger.
33 *
34 * @var string
35 * @since 11.1
36 */
37 protected $host = '127.0.0.1';
38
39 /**
40 * The database server user to connect as for the logger.
41 *
42 * @var string
43 * @since 11.1
44 */
45 protected $user = 'root';
46
47 /**
48 * The password to use for connecting to the database server.
49 *
50 * @var string
51 * @since 11.1
52 */
53 protected $password = '';
54
55 /**
56 * The name of the database table to use for the logger.
57 *
58 * @var string
59 * @since 11.1
60 */
61 protected $database = 'logging';
62
63 /**
64 * The database table to use for logging entries.
65 *
66 * @var string
67 * @since 11.1
68 */
69 protected $table = 'jos_';
70
71 /**
72 * The database driver object for the logger.
73 *
74 * @var JDatabaseDriver
75 * @since 11.1
76 */
77 protected $db;
78
79 /**
80 * Constructor.
81 *
82 * @param array &$options Log object options.
83 *
84 * @since 11.1
85 */
86 public function __construct(array &$options)
87 {
88 // Call the parent constructor.
89 parent::__construct($options);
90
91 // If both the database object and driver options are empty we want to use the system database connection.
92 if (empty($this->options['db_driver']))
93 {
94 $this->db = JFactory::getDbo();
95 $this->driver = null;
96 $this->host = null;
97 $this->user = null;
98 $this->password = null;
99 $this->database = null;
100 $this->prefix = null;
101 }
102 else
103 {
104 $this->db = null;
105 $this->driver = (empty($this->options['db_driver'])) ? 'mysqli' : $this->options['db_driver'];
106 $this->host = (empty($this->options['db_host'])) ? '127.0.0.1' : $this->options['db_host'];
107 $this->user = (empty($this->options['db_user'])) ? 'root' : $this->options['db_user'];
108 $this->password = (empty($this->options['db_pass'])) ? '' : $this->options['db_pass'];
109 $this->database = (empty($this->options['db_database'])) ? 'logging' : $this->options['db_database'];
110 $this->prefix = (empty($this->options['db_prefix'])) ? 'jos_' : $this->options['db_prefix'];
111 }
112
113 // The table name is independent of how we arrived at the connection object.
114 $this->table = (empty($this->options['db_table'])) ? '#__log_entries' : $this->options['db_table'];
115 }
116
117 /**
118 * Method to add an entry to the log.
119 *
120 * @param JLogEntry $entry The log entry object to add to the log.
121 *
122 * @return void
123 *
124 * @since 11.1
125 * @throws RuntimeException
126 */
127 public function addEntry(JLogEntry $entry)
128 {
129 // Connect to the database if not connected.
130 if (empty($this->db))
131 {
132 $this->connect();
133 }
134
135 // Convert the date.
136 $entry->date = $entry->date->toSql(false, $this->db);
137
138 $this->db->insertObject($this->table, $entry);
139 }
140
141 /**
142 * Method to connect to the database server based on object properties.
143 *
144 * @return void
145 *
146 * @since 11.1
147 * @throws RuntimeException
148 */
149 protected function connect()
150 {
151 // Build the configuration object to use for JDatabaseDriver.
152 $options = array(
153 'driver' => $this->driver,
154 'host' => $this->host,
155 'user' => $this->user,
156 'password' => $this->password,
157 'database' => $this->database,
158 'prefix' => $this->prefix,
159 );
160
161 $this->db = JDatabaseDriver::getInstance($options);
162 }
163 }
164