Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
database.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage Log
5  *
6  * @copyright Copyright (C) 2005 - 2013 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  * @package Joomla.Platform
20  * @subpackage Log
21  * @since 11.1
22  */
24 {
25  /**
26  * @var string The name of the database driver to use for connecting to the database.
27  * @since 11.1
28  */
29  protected $driver = 'mysqli';
30 
31  /**
32  * @var string The host name (or IP) of the server with which to connect for the logger.
33  * @since 11.1
34  */
35  protected $host = '127.0.0.1';
36 
37  /**
38  * @var string The database server user to connect as for the logger.
39  * @since 11.1
40  */
41  protected $user = 'root';
42 
43  /**
44  * @var string The password to use for connecting to the database server.
45  * @since 11.1
46  */
47  protected $password = '';
48 
49  /**
50  * @var string The name of the database table to use for the logger.
51  * @since 11.1
52  */
53  protected $database = 'logging';
54 
55  /**
56  * @var string The database table to use for logging entries.
57  * @since 11.1
58  */
59  protected $table = 'jos_';
60 
61  /**
62  * @var JDatabaseDriver The database driver object for the logger.
63  * @since 11.1
64  */
65  protected $db;
66 
67  /**
68  * Constructor.
69  *
70  * @param array &$options Log object options.
71  *
72  * @since 11.1
73  */
74  public function __construct(array &$options)
75  {
76  // Call the parent constructor.
77  parent::__construct($options);
78 
79  // If both the database object and driver options are empty we want to use the system database connection.
80  if (empty($this->options['db_driver']))
81  {
82  $this->db = JFactory::getDbo();
83  $this->driver = null;
84  $this->host = null;
85  $this->user = null;
86  $this->password = null;
87  $this->database = null;
88  $this->prefix = null;
89  }
90  else
91  {
92  $this->db = null;
93  $this->driver = (empty($this->options['db_driver'])) ? 'mysqli' : $this->options['db_driver'];
94  $this->host = (empty($this->options['db_host'])) ? '127.0.0.1' : $this->options['db_host'];
95  $this->user = (empty($this->options['db_user'])) ? 'root' : $this->options['db_user'];
96  $this->password = (empty($this->options['db_pass'])) ? '' : $this->options['db_pass'];
97  $this->database = (empty($this->options['db_database'])) ? 'logging' : $this->options['db_database'];
98  $this->prefix = (empty($this->options['db_prefix'])) ? 'jos_' : $this->options['db_prefix'];
99  }
100 
101  // The table name is independent of how we arrived at the connection object.
102  $this->table = (empty($this->options['db_table'])) ? '#__log_entries' : $this->options['db_table'];
103  }
104 
105  /**
106  * Method to add an entry to the log.
107  *
108  * @param JLogEntry $entry The log entry object to add to the log.
109  *
110  * @return void
111  *
112  * @since 11.1
113  */
114  public function addEntry(JLogEntry $entry)
115  {
116  // Connect to the database if not connected.
117  if (empty($this->db))
118  {
119  $this->connect();
120  }
121 
122  // Convert the date.
123  $entry->date = $entry->date->toSql(false, $this->db);
124 
125  $this->db->insertObject($this->table, $entry);
126  }
127 
128  /**
129  * Method to connect to the database server based on object properties.
130  *
131  * @return void
132  *
133  * @since 11.1
134  * @throws RuntimeException
135  */
136  protected function connect()
137  {
138  // Build the configuration object to use for JDatabaseDriver.
139  $options = array(
140  'driver' => $this->driver,
141  'host' => $this->host,
142  'user' => $this->user,
143  'password' => $this->password,
144  'database' => $this->database,
145  'prefix' => $this->prefix);
146 
147  $db = JDatabaseDriver::getInstance($options);
148 
149  // Assign the database connector to the class.
150  $this->db = $db;
151  }
152 }