PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_virtuemart/classes/Log/file.php

https://bitbucket.org/dgough/annamaria-daneswood-25102012
PHP | 360 lines | 143 code | 48 blank | 169 comment | 33 complexity | c8de178f2020f514aa92ea78e6057464 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );
  3. /**
  4. *
  5. * @version $Id: file.php 1359 2008-04-07 20:02:17Z soeren_nb $
  6. * @package VirtueMart
  7. * @subpackage Log
  8. * @copyright Copyright (C) 2004-2008 soeren - All rights reserved.
  9. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
  10. * VirtueMart is free software. This version may have been modified pursuant
  11. * to the GNU General Public License, and as distributed it includes or
  12. * is derivative of works licensed under the GNU General Public License or
  13. * other free or open source software licenses.
  14. * See /administrator/components/com_virtuemart/COPYRIGHT.php for copyright notices and details.
  15. *
  16. * http://virtuemart.net
  17. */
  18. /**
  19. * $Header$
  20. *
  21. * @version $ Revision: 1.44 $
  22. * @package Log
  23. */
  24. /**
  25. * The vmLog_file class is a concrete implementation of the Log abstract
  26. * class that logs messages to a text file.
  27. *
  28. * @author Jon Parise <jon@php.net>
  29. * @author Roman Neuhauser <neuhauser@bellavista.cz>
  30. * @since Log 1.0
  31. * @package Log
  32. *
  33. * @example file.php Using the file handler.
  34. */
  35. class vmLog_file extends vmLog
  36. {
  37. /**
  38. * String containing the name of the log file.
  39. * @var string
  40. * @access private
  41. */
  42. var $_filename = 'php.log';
  43. /**
  44. * Handle to the log file.
  45. * @var resource
  46. * @access private
  47. */
  48. var $_fp = false;
  49. /**
  50. * Should new log entries be append to an existing log file, or should the
  51. * a new log file overwrite an existing one?
  52. * @var boolean
  53. * @access private
  54. */
  55. var $_append = true;
  56. /**
  57. * Should advisory file locking (i.e., flock()) be used?
  58. * @var boolean
  59. * @access private
  60. */
  61. var $_locking = false;
  62. /**
  63. * Integer (in octal) containing the log file's permissions mode.
  64. * @var integer
  65. * @access private
  66. */
  67. var $_mode = 0644;
  68. /**
  69. * Integer (in octal) specifying the file permission mode that will be
  70. * used when creating directories that do not already exist.
  71. * @var integer
  72. * @access private
  73. */
  74. var $_dirmode = 0755;
  75. /**
  76. * String containing the format of a log line.
  77. * @var string
  78. * @access private
  79. */
  80. var $_lineFormat = '%1$s %2$s [%3$s] %4$s';
  81. /**
  82. * String containing the timestamp format. It will be passed directly to
  83. * strftime(). Note that the timestamp string will generated using the
  84. * current locale.
  85. * @var string
  86. * @access private
  87. */
  88. var $_timeFormat = '%b %d %H:%M:%S';
  89. /**
  90. * Hash that maps canonical format keys to position arguments for the
  91. * "line format" string.
  92. * @var array
  93. * @access private
  94. */
  95. /*@MWM1:Add additional format keys {remoteip}, {username}, {vmsessionid}*/
  96. var $_formatMap = array('%{timestamp}' => '%1$s',
  97. '%{ident}' => '%2$s',
  98. '%{priority}' => '%3$s',
  99. '%{message}' => '%4$s',
  100. '%{remoteip}' => '%5$s',
  101. '%{username}' => '%6$s',
  102. '%{vmsessionid}' => '%7$s',
  103. '%\{' => '%%{');
  104. /**
  105. * String containing the end-on-line character sequence.
  106. * @var string
  107. * @access private
  108. */
  109. var $_eol = "\n";
  110. /**
  111. * Constructs a new vmLog_file object.
  112. *
  113. * @param string $name Ignored.
  114. * @param string $ident The identity string.
  115. * @param array $conf The configuration array.
  116. * @param int $level Log messages up to and including this level.
  117. * @access public
  118. */
  119. function vmLog_file($name, $ident = '', $conf = array(),
  120. $level = PEAR_LOG_DEBUG)
  121. {
  122. $this->_id = md5(microtime());
  123. $this->_filename = $name;
  124. $this->_ident = $ident;
  125. $this->_mask = vmLog::UPTO($level);
  126. if (isset($conf['append'])) {
  127. $this->_append = $conf['append'];
  128. }
  129. if (isset($conf['locking'])) {
  130. $this->_locking = $conf['locking'];
  131. }
  132. if (!empty($conf['mode'])) {
  133. if (is_string($conf['mode'])) {
  134. $this->_mode = octdec($conf['mode']);
  135. } else {
  136. $this->_mode = $conf['mode'];
  137. }
  138. }
  139. if (!empty($conf['dirmode'])) {
  140. if (is_string($conf['dirmode'])) {
  141. $this->_dirmode = octdec($conf['dirmode']);
  142. } else {
  143. $this->_dirmode = $conf['dirmode'];
  144. }
  145. }
  146. if (!empty($conf['lineFormat'])) {
  147. $this->_lineFormat = str_replace(array_keys($this->_formatMap),
  148. array_values($this->_formatMap),
  149. $conf['lineFormat']);
  150. }
  151. if (!empty($conf['timeFormat'])) {
  152. $this->_timeFormat = $conf['timeFormat'];
  153. }
  154. if (!empty($conf['eol'])) {
  155. $this->_eol = $conf['eol'];
  156. } else {
  157. $this->_eol = (strstr(PHP_OS, 'WIN')) ? "\r\n" : "\n";
  158. }
  159. register_shutdown_function(array(&$this, '_vmLog_file'));
  160. }
  161. /**
  162. * Destructor
  163. */
  164. function _vmLog_file()
  165. {
  166. if ($this->_opened) {
  167. $this->close();
  168. }
  169. }
  170. /**
  171. * Creates the given directory path. If the parent directories don't
  172. * already exist, they will be created, too.
  173. *
  174. * This implementation is inspired by Python's os.makedirs function.
  175. *
  176. * @param string $path The full directory path to create.
  177. * @param integer $mode The permissions mode with which the
  178. * directories will be created.
  179. *
  180. * @return True if the full path is successfully created or already
  181. * exists.
  182. *
  183. * @access private
  184. */
  185. function _mkpath($path, $mode = 0700)
  186. {
  187. /* Separate the last pathname component from the rest of the path. */
  188. $head = dirname($path);
  189. $tail = basename($path);
  190. /* Make sure we've split the path into two complete components. */
  191. if (empty($tail)) {
  192. $head = dirname($path);
  193. $tail = basename($path);
  194. }
  195. /* Recurse up the path if our current segment does not exist. */
  196. if (!empty($head) && !empty($tail) && !is_dir($head)) {
  197. $this->_mkpath($head, $mode);
  198. }
  199. /* Create this segment of the path. */
  200. return @mkdir($head, $mode);
  201. }
  202. /**
  203. * Opens the log file for output. If the specified log file does not
  204. * already exist, it will be created. By default, new log entries are
  205. * appended to the end of the log file.
  206. *
  207. * This is implicitly called by log(), if necessary.
  208. *
  209. * @access public
  210. */
  211. function open()
  212. {
  213. if (!$this->_opened) {
  214. /* If the log file's directory doesn't exist, create it. */
  215. if (!is_dir(dirname($this->_filename))) {
  216. $this->_mkpath($this->_filename, $this->_dirmode);
  217. }
  218. /* Determine whether the log file needs to be created. */
  219. $creating = !file_exists($this->_filename);
  220. /* Obtain a handle to the log file. */
  221. $this->_fp = fopen($this->_filename, ($this->_append) ? 'a' : 'w');
  222. /* We consider the file "opened" if we have a valid file pointer. */
  223. $this->_opened = ($this->_fp !== false);
  224. /* Attempt to set the file's permissions if we just created it. */
  225. if ($creating && $this->_opened) {
  226. chmod($this->_filename, $this->_mode);
  227. }
  228. }
  229. return $this->_opened;
  230. }
  231. /**
  232. * Closes the log file if it is open.
  233. *
  234. * @access public
  235. */
  236. function close()
  237. {
  238. /* If the log file is open, close it. */
  239. if ($this->_opened && fclose($this->_fp)) {
  240. $this->_opened = false;
  241. }
  242. return ($this->_opened === false);
  243. }
  244. /**
  245. * Flushes all pending data to the file handle.
  246. *
  247. * @access public
  248. * @since Log 1.8.2
  249. */
  250. function flush()
  251. {
  252. return is_resource($this->_fp) ? fflush($this->_fp) : false;
  253. }
  254. /**
  255. * Logs $message to the output window. The message is also passed along
  256. * to any Log_observer instances that are observing this Log.
  257. *
  258. * @param mixed $message String or object containing the message to log.
  259. * @param string $priority The priority of the message. Valid
  260. * values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT,
  261. * PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING,
  262. * PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
  263. * @return boolean True on success or false on failure.
  264. * @access public
  265. */
  266. function log($message, $priority = null)
  267. {
  268. /* If a priority hasn't been specified, use the default value. */
  269. if ($priority === null) {
  270. $priority = $this->_priority;
  271. }
  272. /* Abort early if the priority is above the maximum logging level. */
  273. if (!$this->_isMasked($priority)) {
  274. return false;
  275. }
  276. /* If the log file isn't already open, open it now. */
  277. if (!$this->_opened && !$this->open()) {
  278. return false;
  279. }
  280. /* Extract the string representation of the message. */
  281. $message = $this->_extractMessage($message);
  282. /* #B@MWM1: Add additional info that can be logged */
  283. if (!empty($_SERVER['REMOTE_ADDR']))
  284. $remoteip = $_SERVER['REMOTE_ADDR'];
  285. else
  286. $remoteip = 'unknown IP';
  287. if (!empty($_SESSION['auth']["username"]))
  288. $username = $_SESSION['auth']["username"];
  289. else
  290. $username = 'unknown user';
  291. $vmsessionid = session_id();
  292. /*#E@MWM1*/
  293. /* Build the string containing the complete log line. */
  294. /* @MWM1 Logging changes...*/
  295. $line = sprintf($this->_lineFormat, strftime($this->_timeFormat),
  296. $this->_ident, $this->priorityToShortStringPEAR($priority),
  297. $message, $remoteip, $username, $vmsessionid) . $this->_eol;
  298. /* If locking is enabled, acquire an exclusive lock on the file. */
  299. if ($this->_locking) {
  300. flock($this->_fp, LOCK_EX);
  301. }
  302. /* Write the log line to the log file. */
  303. $success = (fwrite($this->_fp, $line) !== false);
  304. /* Unlock the file now that we're finished writing to it. */
  305. if ($this->_locking) {
  306. flock($this->_fp, LOCK_UN);
  307. }
  308. /* Notify observers about this log message. */
  309. $this->_announce(array('priority' => $priority, 'message' => $message));
  310. return $success;
  311. }
  312. }