ILIAS  Release_4_1_x_branch Revision 61804
 All Data Structures Namespaces Files Functions Variables Groups Pages
sqlite.php
Go to the documentation of this file.
1 <?php
31 class Log_sqlite extends Log
32 {
38  var $_options = array('mode' => 0666,
39  'persistent' => false);
40 
46  var $_db = null;
47 
53  var $_existingConnection = false;
54 
60  var $_table = 'log_table';
61 
62 
74  function Log_sqlite($name, $ident = '', &$conf, $level = PEAR_LOG_DEBUG)
75  {
76  $this->_id = md5(microtime());
77  $this->_table = $name;
78  $this->_ident = $ident;
79  $this->_mask = Log::UPTO($level);
80 
81  if (is_array($conf)) {
82  foreach ($conf as $k => $opt) {
83  $this->_options[$k] = $opt;
84  }
85  } else {
86  // If an existing database connection was provided, use it.
87  $this->_db =& $conf;
88  $this->_existingConnection = true;
89  }
90  }
91 
99  function open()
100  {
101  if (is_resource($this->_db)) {
102  $this->_opened = true;
103  return $this->_createTable();
104  } else {
105  /* Set the connection function based on the 'persistent' option. */
106  if (empty($this->_options['persistent'])) {
107  $connectFunction = 'sqlite_open';
108  } else {
109  $connectFunction = 'sqlite_popen';
110  }
111 
112  /* Attempt to connect to the database. */
113  if ($this->_db = $connectFunction($this->_options['filename'],
114  (int)$this->_options['mode'],
115  $error)) {
116  $this->_opened = true;
117  return $this->_createTable();
118  }
119  }
120 
121  return $this->_opened;
122  }
123 
132  function close()
133  {
134  /* We never close existing connections. */
135  if ($this->_existingConnection) {
136  return false;
137  }
138 
139  if ($this->_opened) {
140  $this->_opened = false;
141  sqlite_close($this->_db);
142  }
143 
144  return ($this->_opened === false);
145  }
146 
160  function log($message, $priority = null)
161  {
162  /* If a priority hasn't been specified, use the default value. */
163  if ($priority === null) {
164  $priority = $this->_priority;
165  }
166 
167  /* Abort early if the priority is above the maximum logging level. */
168  if (!$this->_isMasked($priority)) {
169  return false;
170  }
171 
172  /* If the connection isn't open and can't be opened, return failure. */
173  if (!$this->_opened && !$this->open()) {
174  return false;
175  }
176 
177  // Extract the string representation of the message.
178  $message = $this->_extractMessage($message);
179 
180  // Build the SQL query for this log entry insertion.
181  $q = sprintf('INSERT INTO [%s] (logtime, ident, priority, message) ' .
182  "VALUES ('%s', '%s', %d, '%s')",
183  $this->_table,
184  strftime('%Y-%m-%d %H:%M:%S', time()),
185  sqlite_escape_string($this->_ident),
186  $priority,
187  sqlite_escape_string($message));
188  if (!($res = @sqlite_unbuffered_query($this->_db, $q))) {
189  return false;
190  }
191  $this->_announce(array('priority' => $priority, 'message' => $message));
192 
193  return true;
194  }
195 
202  function _createTable()
203  {
204  $q = "SELECT name FROM sqlite_master WHERE name='" . $this->_table .
205  "' AND type='table'";
206 
207  $res = sqlite_query($this->_db, $q);
208 
209  if (sqlite_num_rows($res) == 0) {
210  $q = 'CREATE TABLE [' . $this->_table . '] (' .
211  'id INTEGER PRIMARY KEY NOT NULL, ' .
212  'logtime NOT NULL, ' .
213  'ident CHAR(16) NOT NULL, ' .
214  'priority INT NOT NULL, ' .
215  'message)';
216 
217  if (!($res = sqlite_unbuffered_query($this->_db, $q))) {
218  return false;
219  }
220  }
221 
222  return true;
223  }
224 
225 }