Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * $Header: /repository/pear/Log/Log/sql.php,v 1.40 2006/01/03 04:12:45 jon Exp $
4
 * $Horde: horde/lib/Log/sql.php,v 1.12 2000/08/16 20:27:34 chuck Exp $
5
 *
6
 * @version $Revision: 1.40 $
7
 * @package Log
8
 */
9
 
10
/*
11
 * We require the PEAR DB class.  This is generally defined in the DB.php file,
12
 * but it's possible that the caller may have provided the DB class, or a
13
 * compatible wrapper (such as the one shipped with MDB2), so we first check
14
 * for an existing 'DB' class before including 'DB.php'.
15
 */
16
if (!class_exists('DB')) {
17
    require_once 'DB.php';
18
}
19
 
20
/**
21
 * The Log_sql class is a concrete implementation of the Log::
22
 * abstract class which sends messages to an SQL server.  Each entry
23
 * occupies a separate row in the database.
24
 *
25
 * This implementation uses PHP's PEAR database abstraction layer.
26
 *
27
 * CREATE TABLE log_table (
28
 *  id          INT NOT NULL,
29
 *  logtime     TIMESTAMP NOT NULL,
30
 *  ident       CHAR(16) NOT NULL,
31
 *  priority    INT NOT NULL,
32
 *  message     VARCHAR(200),
33
 *  PRIMARY KEY (id)
34
 * );
35
 *
36
 * @author  Jon Parise <jon@php.net>
37
 * @since   Horde 1.3
38
 * @since   Log 1.0
39
 * @package Log
40
 *
41
 * @example sql.php     Using the SQL handler.
42
 */
43
class Log_lt_sql extends Log
44
{
45
    /* Variable containing the user id
46
     * @var integer
47
     * @access private
48
     */
49
    var $_u_id=0;
50
 
51
    /**
52
     * Variable containing the DSN information.
53
     * @var mixed
54
     * @access private
55
     */
56
    var $_dsn = '';
57
 
58
    /**
59
     * String containing the SQL insertion statement.
60
     *
61
     * @var string
62
     * @access private
63
     */
64
    var $_sql = '';
65
 
66
    /**
67
     * Array containing our set of DB configuration options.
68
     * @var array
69
     * @access private
70
     */
71
    var $_options = array('persistent' => true);
72
 
73
    /**
74
     * Object holding the database handle.
75
     * @var object
76
     * @access private
77
     */
78
    var $_db = null;
79
 
80
    /**
81
     * Resource holding the prepared statement handle.
82
     * @var resource
83
     * @access private
84
     */
85
    var $_statement = null;
86
 
87
    /**
88
     * Flag indicating that we're using an existing database connection.
89
     * @var boolean
90
     * @access private
91
     */
92
    var $_existingConnection = false;
93
 
94
    /**
95
     * String holding the database table to use.
96
     * @var string
97
     * @access private
98
     */
99
    var $_table = 'log_table';
100
 
101
    /**
102
     * String holding the name of the ID sequence.
103
     * @var string
104
     * @access private
105
     */
106
    var $_sequence = 'log_id';
107
 
108
    /**
109
     * Maximum length of the $ident string.  This corresponds to the size of
110
     * the 'ident' column in the SQL table.
111
     * @var integer
112
     * @access private
113
     */
114
    var $_identLimit = 16;
115
 
116
 
117
    /**
118
     * Constructs a new sql logging object.
119
     *
120
     * @param string $name         The target SQL table.
121
     * @param string $ident        The identification field.
122
     * @param array $conf          The connection configuration array.
123
     * @param int $level           Log messages up to and including this level.
124
     * @access public
125
     */
126
    function Log_lt_sql($name, $ident = '', $conf = array(),
127
                     $level = PEAR_LOG_DEBUG)
128
    {
129
        $this->_id = md5(microtime());
130
        $this->_table = $name;
131
        $this->_mask = Log::UPTO($level);
132
        $this->_u_id=$conf["u_id"];
133
 
134
        /* Now that we have a table name, assign our SQL statement. */
135
        if (!empty($this->_sql)) {
136
            $this->_sql = $conf['sql'];
137
        } else {
138
            $this->_sql = 'INSERT INTO ' . $this->_table .
139
                          ' (u_id, logtime, ident, priority, message)' .
140
                          ' VALUES(?,'.time().', ?, ?, ?)';
141
        }
142
 
143
        /* If an options array was provided, use it. */
144
        if (isset($conf['options']) && is_array($conf['options'])) {
145
            $this->_options = $conf['options'];
146
        }
147
 
148
        /* If a specific sequence name was provided, use it. */
149
        if (!empty($conf['sequence'])) {
150
            $this->_sequence = $conf['sequence'];
151
        }
152
 
153
        /* If a specific sequence name was provided, use it. */
154
        if (isset($conf['identLimit'])) {
155
            $this->_identLimit = $conf['identLimit'];
156
        }
157
 
158
        /* Now that the ident limit is confirmed, set the ident string. */
159
        $this->setIdent($ident);
160
 
161
        /* If an existing database connection was provided, use it. */
162
        if (isset($conf['db'])) {
163
            $this->_db = &$conf['db'];
164
            $this->_existingConnection = true;
165
            $this->_opened = true;
166
        } else {
167
            $this->_dsn = $conf['dsn'];
168
        }
169
    }
170
 
171
    /**
172
     * Opens a connection to the database, if it has not already
173
     * been opened. This is implicitly called by log(), if necessary.
174
     *
175
     * @return boolean   True on success, false on failure.
176
     * @access public
177
     */
178
    function open()
179
    {
180
        if (!$this->_opened) {
181
            /* Use the DSN and options to create a database connection. */
182
            $this->_db = &DB::connect($this->_dsn, $this->_options);
183
            if (DB::isError($this->_db)) {
184
                return false;
185
            }
186
 
187
            /* Create a prepared statement for repeated use in log(). */
188
            if (!$this->_prepareStatement()) {
189
                return false;
190
            }
191
 
192
            /* We now consider out connection open. */
193
            $this->_opened = true;
194
        }
195
 
196
        return $this->_opened;
197
    }
198
 
199
    /**
200
     * Closes the connection to the database if it is still open and we were
201
     * the ones that opened it.  It is the caller's responsible to close an
202
     * existing connection that was passed to us via $conf['db'].
203
     *
204
     * @return boolean   True on success, false on failure.
205
     * @access public
206
     */
207
    function close()
208
    {
209
        if ($this->_opened && !$this->_existingConnection) {
210
            $this->_opened = false;
211
            $this->_db->freePrepared($this->_statement);
212
            return $this->_db->disconnect();
213
        }
214
 
215
        return ($this->_opened === false);
216
    }
217
 
218
    /**
219
     * Sets this Log instance's identification string.  Note that this
220
     * SQL-specific implementation will limit the length of the $ident string
221
     * to sixteen (16) characters.
222
     *
223
     * @param string    $ident      The new identification string.
224
     *
225
     * @access  public
226
     * @since   Log 1.8.5
227
     */
228
    function setIdent($ident)
229
    {
230
        $this->_ident = substr($ident, 0, $this->_identLimit);
231
    }
232
 
233
    /**
234
     * Inserts $message to the currently open database.  Calls open(),
235
     * if necessary.  Also passes the message along to any Log_observer
236
     * instances that are observing this Log.
237
     *
238
     * @param mixed  $message  String or object containing the message to log.
239
     * @param string $priority The priority of the message.  Valid
240
     *                  values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT,
241
     *                  PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING,
242
     *                  PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
243
     * @return boolean  True on success or false on failure.
244
     * @access public
245
     */
246
    function log($message, $priority = null)
247
    {
248
        /* If a priority hasn't been specified, use the default value. */
249
        if ($priority === null) {
250
            $priority = $this->_priority;
251
        }
252
 
253
        /* Abort early if the priority is above the maximum logging level. */
254
        if (!$this->_isMasked($priority)) {
255
            return false;
256
        }
257
 
258
        /* If the connection isn't open and can't be opened, return failure. */
259
        if (!$this->_opened && !$this->open()) {
260
            return false;
261
        }
262
 
263
        /* If we don't already have our statement object yet, create it. */
264
        if (!is_object($this->_statement) && !$this->_prepareStatement()) {
265
            return false;
266
        }
267
 
268
        /* Extract the string representation of the message. */
269
        $message = $this->_extractMessage($message);
270
 
271
        /* Build our set of values for this log entry. */
272
        //$id = $this->_db->nextId($this->_sequence);
273
 
274
        $values = array($this->_u_id, $this->_ident, $priority, $message);
275
 
276
        /* Execute the SQL query for this log entry insertion. */
277
        $result =& $this->_db->execute($this->_statement, $values);
278
        if (DB::isError($result)) {
279
            return false;
280
        }
281
 
282
        $this->_announce(array('priority' => $priority, 'message' => $message));
283
 
284
        return true;
285
    }
286
 
287
    /**
288
     * Prepare the SQL insertion statement.
289
     *
290
     * @return boolean  True if the statement was successfully created.
291
     *
292
     * @access  private
293
     * @since   Log 1.9.1
294
     */
295
    function _prepareStatement()
296
    {
297
        $this->_statement = $this->_db->prepare($this->_sql);
298
 
299
        /* Return success if we didn't generate an error. */
300
        return (DB::isError($this->_statement) === false);
301
    }
302
}
303
?>