10 defined(
'JPATH_PLATFORM') or die;
12 jimport('joomla.filesystem.folder');
37 protected $format =
'{DATETIME} {PRIORITY} {CATEGORY} {MESSAGE}';
43 protected $fields = array();
58 public function __construct(array &$options)
61 parent::__construct($options);
64 if (empty($this->options[
'text_file']))
66 $this->options[
'text_file'] =
'error.php';
70 if (empty($this->options[
'text_file_path']))
76 if (empty($this->options[
'text_file_no_php']))
78 $this->options[
'text_file_no_php'] =
false;
82 $this->path = $this->options[
'text_file_path'] .
'/' . $this->options[
'text_file'];
85 if (!empty($this->options[
'text_entry_format']))
87 $this->format = (string) $this->options[
'text_entry_format'];
99 public function __destruct()
101 if (is_resource($this->file))
120 if (!is_resource($this->file))
126 if (!isset($entry->clientIP))
130 if (isset($_SERVER[
'REMOTE_ADDR']))
132 $entry->clientIP = $_SERVER[
'REMOTE_ADDR'];
134 elseif (isset($_SERVER[
'HTTP_X_FORWARDED_FOR']))
136 $entry->clientIP = $_SERVER[
'HTTP_X_FORWARDED_FOR'];
138 elseif (isset($_SERVER[
'HTTP_CLIENT_IP']))
140 $entry->clientIP = $_SERVER[
'HTTP_CLIENT_IP'];
145 if ((strlen($entry->date) != 10) || !isset($entry->time))
149 $entry->datetime = $entry->date->toISO8601();
150 $entry->time = $entry->date->format(
'H:i:s',
false);
151 $entry->date = $entry->date->format(
'Y-m-d',
false);
155 $tmp = array_change_key_case(get_object_vars($entry), CASE_UPPER);
158 $tmp[
'PRIORITY'] = $this->priorities[$entry->priority];
161 $line = $this->format;
163 foreach ($this->fields as $field)
165 $line = str_replace(
'{' . $field .
'}', (isset($tmp[$field])) ? $tmp[$field] :
'-', $line);
169 if (!fwrite($this->file, $line .
"\n"))
171 throw new RuntimeException(
'Cannot write to log file.');
182 protected function generateFileHeader()
189 if (empty($this->options[
'text_file_no_php']))
193 $head[] =
'#<?php die(\'Forbidden.\'); ?>';
195 $head[] =
'#Date: ' . gmdate(
'Y-m-d H:i:s') .
' UTC';
200 $head[] =
'#Fields: ' . strtolower(str_replace(
'}',
'', str_replace(
'{',
'', $this->format)));
203 return implode(
"\n", $head);
216 protected function initFile()
219 if (!is_file($this->path))
226 $head = $this->generateFileHeader();
234 if (!$this->file = fopen($this->path,
'a'))
236 throw new RuntimeException(
'Cannot open file for writing log');
240 if (!fwrite($this->file, $head))
242 throw new RuntimeException(
'Cannot fput file for log');
254 protected function parseFields()
256 $this->fields = array();
260 preg_match_all(
"/{(.*?)}/i", $this->format, $matches);
263 foreach ($matches[1] as $match)
265 $this->fields[] = strtoupper($match);