PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/site/libraries/joomla/error/log.php

https://bitbucket.org/manchas/pperezm
PHP | 231 lines | 114 code | 29 blank | 88 comment | 19 complexity | 4feca05839565692214f88ae226fe6d3 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * @version $Id: log.php 10707 2008-08-21 09:52:47Z eddieajau $
  4. * @package Joomla.Framework
  5. * @subpackage Error
  6. * @copyright Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
  7. * @license GNU/GPL, see LICENSE.php
  8. * Joomla! is free software. This version may have been modified pursuant
  9. * to the GNU General Public License, and as distributed it includes or
  10. * is derivative of works licensed under the GNU General Public License or
  11. * other free or open source software licenses.
  12. * See COPYRIGHT.php for copyright notices and details.
  13. */
  14. // Check to ensure this file is within the rest of the framework
  15. defined('JPATH_BASE') or die();
  16. /**
  17. * Joomla! Logging class
  18. *
  19. * This class is designed to build log files based on the
  20. * W3C specification at: http://www.w3.org/TR/WD-logfile.html
  21. *
  22. * @package Joomla.Framework
  23. * @subpackage Error
  24. * @since 1.5
  25. */
  26. class JLog extends JObject
  27. {
  28. /**
  29. * Log File Pointer
  30. * @var resource
  31. */
  32. var $_file;
  33. /**
  34. * Log File Path
  35. * @var string
  36. */
  37. var $_path;
  38. /**
  39. * Log Format
  40. * @var string
  41. */
  42. var $_format = "{DATE}\t{TIME}\t{LEVEL}\t{C-IP}\t{STATUS}\t{COMMENT}";
  43. /**
  44. * Constructor
  45. *
  46. * @access protected
  47. * @param string $path Log file path
  48. * @param array $options Log file options
  49. * @since 1.5
  50. */
  51. function __construct($path, $options)
  52. {
  53. // Set default values
  54. $this->_path = $path;
  55. $this->setOptions($options);
  56. }
  57. /**
  58. * Returns a reference to the global log object, only creating it
  59. * if it doesn't already exist.
  60. *
  61. * This method must be invoked as:
  62. * <pre> $log = & JLog::getInstance();</pre>
  63. *
  64. * @access public
  65. * @static
  66. * @return object The JLog object.
  67. * @since 1.5
  68. */
  69. function & getInstance($file = 'error.php', $options = null, $path = null)
  70. {
  71. static $instances;
  72. // Set default path if not set
  73. if (!$path)
  74. {
  75. $config =& JFactory::getConfig();
  76. $path = $config->getValue('config.log_path');
  77. }
  78. jimport('joomla.filesystem.path');
  79. $path = JPath :: clean($path . DS . $file);
  80. $sig = md5($path);
  81. if (!isset ($instances)) {
  82. $instances = array ();
  83. }
  84. if (empty ($instances[$sig])) {
  85. $instances[$sig] = new JLog($path, $options);
  86. }
  87. return $instances[$sig];
  88. }
  89. /**
  90. * Set log file options
  91. *
  92. * @access public
  93. * @param array $options Associative array of options to set
  94. * @return boolean True if successful
  95. * @since 1.5
  96. */
  97. function setOptions($options) {
  98. if (isset ($options['format'])) {
  99. $this->_format = $options['format'];
  100. }
  101. return true;
  102. }
  103. function addEntry($entry)
  104. {
  105. // Set some default field values if not already set.
  106. $date =& JFactory::getDate();
  107. if (!isset ($entry['date'])) {
  108. $entry['date'] = $date->toFormat("%Y-%m-%d");
  109. }
  110. if (!isset ($entry['time'])) {
  111. $entry['time'] = $date->toFormat("%H:%M:%S");
  112. }
  113. if (!isset ($entry['c-ip'])) {
  114. $entry['c-ip'] = $_SERVER['REMOTE_ADDR'];
  115. }
  116. // Ensure that the log entry keys are all uppercase
  117. $entry = array_change_key_case($entry, CASE_UPPER);
  118. // Find all fields in the format string
  119. $fields = array ();
  120. $regex = "/{(.*?)}/i";
  121. preg_match_all($regex, $this->_format, $fields);
  122. // Fill in the field data
  123. $line = $this->_format;
  124. for ($i = 0; $i < count($fields[0]); $i++)
  125. {
  126. $line = str_replace($fields[0][$i], (isset ($entry[$fields[1][$i]])) ? $entry[$fields[1][$i]] : "-", $line);
  127. }
  128. // Write the log entry line
  129. if ($this->_openLog())
  130. {
  131. if (!fputs($this->_file, "\n" . $line)) {
  132. return false;
  133. }
  134. } else {
  135. return false;
  136. }
  137. return true;
  138. }
  139. /**
  140. * Open the log file pointer and create the file if it doesn't exist
  141. *
  142. * @access public
  143. * @return boolean True on success
  144. * @since 1.5
  145. */
  146. function _openLog()
  147. {
  148. // Only open if not already opened...
  149. if (is_resource($this->_file)) {
  150. return true;
  151. }
  152. $now =& JFactory::getDate();
  153. $date = $now->toMySQL();
  154. if (!file_exists($this->_path))
  155. {
  156. jimport("joomla.filesystem.folder");
  157. if (!JFolder :: create(dirname($this->_path))) {
  158. return false;
  159. }
  160. $header[] = "#<?php die('Direct Access To Log Files Not Permitted'); ?>";
  161. $header[] = "#Version: 1.0";
  162. $header[] = "#Date: " . $date;
  163. // Prepare the fields string
  164. $fields = str_replace("{", "", $this->_format);
  165. $fields = str_replace("}", "", $fields);
  166. $fields = strtolower($fields);
  167. $header[] = "#Fields: " . $fields;
  168. // Prepare the software string
  169. $version = new JVersion();
  170. $header[] = "#Software: " . $version->getLongVersion();
  171. $head = implode("\n", $header);
  172. } else {
  173. $head = false;
  174. }
  175. if (!$this->_file = fopen($this->_path, "a")) {
  176. return false;
  177. }
  178. if ($head)
  179. {
  180. if (!fputs($this->_file, $head)) {
  181. return false;
  182. }
  183. }
  184. // If we opened the file lets make sure we close it
  185. register_shutdown_function(array(&$this,'_closeLog'));
  186. return true;
  187. }
  188. /**
  189. * Close the log file pointer
  190. *
  191. * @access public
  192. * @return boolean True on success
  193. * @since 1.5
  194. */
  195. function _closeLog()
  196. {
  197. if (is_resource($this->_file)) {
  198. fclose($this->_file);
  199. }
  200. return true;
  201. }
  202. }