Source for file formattedtext.php

Documentation is available at formattedtext.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Platform
  4.  * @subpackage  Log
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE
  8.  */
  9.  
  10. defined('JPATH_PLATFORM'or die;
  11.  
  12. jimport('joomla.filesystem.folder');
  13.  
  14. /**
  15.  * Joomla! Formatted Text File Log class
  16.  *
  17.  * This class is designed to use as a base for building formatted text files for output. By
  18.  * default it emulates the Syslog style format output. This is a disk based output format.
  19.  *
  20.  * @package     Joomla.Platform
  21.  * @subpackage  Log
  22.  * @since       11.1
  23.  */
  24. {
  25.     /**
  26.      * @var    resource  The file pointer for the log file.
  27.      * @since  11.1
  28.      */
  29.     protected $file;
  30.  
  31.     /**
  32.      * @var    string  The format for which each entry follows in the log file.  All fields must be named
  33.      *  in all caps and be within curly brackets eg. {FOOBAR}.
  34.      * @since  11.1
  35.      */
  36.     protected $format = '{DATETIME}    {PRIORITY}    {CATEGORY}    {MESSAGE}';
  37.  
  38.     /**
  39.      * @var    array  The parsed fields from the format string.
  40.      * @since  11.1
  41.      */
  42.     protected $fields = array();
  43.  
  44.     /**
  45.      * @var    string  The full filesystem path for the log file.
  46.      * @since  11.1
  47.      */
  48.     protected $path;
  49.  
  50.     /**
  51.      * Constructor.
  52.      *
  53.      * @param   array  &$options  Log object options.
  54.      *
  55.      * @since   11.1
  56.      */
  57.     public function __construct(array &$options)
  58.     {
  59.         // Call the parent constructor.
  60.         parent::__construct($options);
  61.  
  62.         // The name of the text file defaults to 'error.php' if not explicitly given.
  63.         if (empty($this->options['text_file']))
  64.         {
  65.             $this->options['text_file''error.php';
  66.         }
  67.  
  68.         // The name of the text file path defaults to that which is set in configuration if not explicitly given.
  69.         if (empty($this->options['text_file_path']))
  70.         {
  71.             $this->options['text_file_path'JFactory::getConfig()->get('log_path');
  72.         }
  73.  
  74.         // False to treat the log file as a php file.
  75.         if (empty($this->options['text_file_no_php']))
  76.         {
  77.             $this->options['text_file_no_php'false;
  78.         }
  79.  
  80.         // Build the full path to the log file.
  81.         $this->path $this->options['text_file_path''/' $this->options['text_file'];
  82.  
  83.         // Use the default entry format unless explicitly set otherwise.
  84.         if (!empty($this->options['text_entry_format']))
  85.         {
  86.             $this->format = (string) $this->options['text_entry_format'];
  87.         }
  88.  
  89.         // Build the fields array based on the format string.
  90.         $this->parseFields();
  91.     }
  92.  
  93.     /**
  94.      * Destructor.
  95.      *
  96.      * @since   11.1
  97.      */
  98.     public function __destruct()
  99.     {
  100.         if (is_resource($this->file))
  101.         {
  102.             fclose($this->file);
  103.         }
  104.     }
  105.  
  106.     /**
  107.      * Method to add an entry to the log.
  108.      *
  109.      * @param   JLogEntry  $entry  The log entry object to add to the log.
  110.      *
  111.      * @return  boolean  True on success.
  112.      *
  113.      * @since   11.1
  114.      * @throws  RuntimeException
  115.      */
  116.     public function addEntry(JLogEntry $entry)
  117.     {
  118.         // Initialise the file if not already done.
  119.         if (!is_resource($this->file))
  120.         {
  121.             $this->initFile();
  122.         }
  123.  
  124.         // Set some default field values if not already set.
  125.         if (!isset($entry->clientIP))
  126.         {
  127.  
  128.             // Check for proxies as well.
  129.             if (isset($_SERVER['REMOTE_ADDR']))
  130.             {
  131.                 $entry->clientIP $_SERVER['REMOTE_ADDR'];
  132.             }
  133.             elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
  134.             {
  135.                 $entry->clientIP $_SERVER['HTTP_X_FORWARDED_FOR'];
  136.             }
  137.             elseif (isset($_SERVER['HTTP_CLIENT_IP']))
  138.             {
  139.                 $entry->clientIP $_SERVER['HTTP_CLIENT_IP'];
  140.             }
  141.         }
  142.  
  143.         // If the time field is missing or the date field isn't only the date we need to rework it.
  144.         if ((strlen($entry->date!= 10|| !isset($entry->time))
  145.         {
  146.  
  147.             // Get the date and time strings in GMT.
  148.             $entry->datetime $entry->date->toISO8601();
  149.             $entry->time = $entry->date->format('H:i:s'false);
  150.             $entry->date = $entry->date->format('Y-m-d'false);
  151.         }
  152.  
  153.         // Get a list of all the entry keys and make sure they are upper case.
  154.         $tmp array_change_key_case(get_object_vars($entry)CASE_UPPER);
  155.  
  156.         // Decode the entry priority into an English string.
  157.         $tmp['PRIORITY'$this->priorities[$entry->priority];
  158.  
  159.         // Fill in field data for the line.
  160.         $line $this->format;
  161.  
  162.         foreach ($this->fields as $field)
  163.         {
  164.             $line str_replace('{' $field '}'(isset($tmp[$field])) $tmp[$field'-'$line);
  165.         }
  166.  
  167.         // Write the new entry to the file.
  168.         if (!fwrite($this->file$line "\n"))
  169.         {
  170.             throw new RuntimeException('Cannot write to log file.');
  171.         }
  172.     }
  173.  
  174.     /**
  175.      * Method to generate the log file header.
  176.      *
  177.      * @return  string  The log file header
  178.      *
  179.      * @since   11.1
  180.      */
  181.     protected function generateFileHeader()
  182.     {
  183.         $head array();
  184.  
  185.         // Build the log file header.
  186.  
  187.         // If the no php flag is not set add the php die statement.
  188.         if (empty($this->options['text_file_no_php']))
  189.         {
  190.             // Blank line to prevent information disclose: https://bugs.php.net/bug.php?id=60677
  191.             $head['#';
  192.             $head['#<?php die(\'Forbidden.\'); ?>';
  193.         }
  194.         $head['#Date: ' gmdate('Y-m-d H:i:s'' UTC';
  195.         $head['#Software: ' JPlatform::getLongVersion();
  196.         $head['';
  197.  
  198.         // Prepare the fields string
  199.         $head['#Fields: ' strtolower(str_replace('}'''str_replace('{'''$this->format)));
  200.         $head['';
  201.  
  202.         return implode("\n"$head);
  203.     }
  204.  
  205.     /**
  206.      * Method to initialise the log file.  This will create the folder path to the file if it doesn't already
  207.      * exist and also get a new file header if the file doesn't already exist.  If the file already exists it
  208.      * will simply open it for writing.
  209.      *
  210.      * @return  void 
  211.      *
  212.      * @since   11.1
  213.      * @throws  RuntimeException
  214.      */
  215.     protected function initFile()
  216.     {
  217.         // If the file doesn't already exist we need to create it and generate the file header.
  218.         if (!is_file($this->path))
  219.         {
  220.  
  221.             // Make sure the folder exists in which to create the log file.
  222.             JFolder::create(dirname($this->path));
  223.  
  224.             // Build the log file header.
  225.             $head $this->generateFileHeader();
  226.         }
  227.         else
  228.         {
  229.             $head false;
  230.         }
  231.  
  232.         // Open the file for writing (append mode).
  233.         if (!$this->file = fopen($this->path'a'))
  234.         {
  235.             throw new RuntimeException('Cannot open file for writing log');
  236.         }
  237.         if ($head)
  238.         {
  239.             if (!fwrite($this->file$head))
  240.             {
  241.                 throw new RuntimeException('Cannot fput file for log');
  242.             }
  243.         }
  244.     }
  245.  
  246.     /**
  247.      * Method to parse the format string into an array of fields.
  248.      *
  249.      * @return  void 
  250.      *
  251.      * @since   11.1
  252.      */
  253.     protected function parseFields()
  254.     {
  255.         $this->fields array();
  256.         $matches array();
  257.  
  258.         // Get all of the available fields in the format string.
  259.         preg_match_all("/{(.*?)}/i"$this->format$matches);
  260.  
  261.         // Build the parsed fields list based on the found fields.
  262.         foreach ($matches[1as $match)
  263.         {
  264.             $this->fields[strtoupper($match);
  265.         }
  266.     }
  267. }

Documentation generated on Tue, 19 Nov 2013 15:03:44 +0100 by phpDocumentor 1.4.3