Solução Gráfica Design SGD
[ class tree: SGD ] [ index: SGD ] [ all elements ]

Source for file RSSerror.php

Documentation is available at RSSerror.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. //
  4. // +----------------------------------------------------------------------+
  5. // | PHP version 4                                                        |
  6. // +----------------------------------------------------------------------+
  7. // | Copyright (c) 1997-2005 The PHP Group                                |
  8. // +----------------------------------------------------------------------+
  9. // | This source file is subject to version 2.0 of the PHP license,       |
  10. // | that is bundled with this package in the file LICENSE, and is        |
  11. // | available through the world-wide-web at                              |
  12. // | http://www.php.net/license/2_02.txt.                                 |
  13. // | If you did not receive a copy of the PHP license and are unable to   |
  14. // | obtain it through the world-wide-web, please send a note to          |
  15. // | license@php.net so we can mail you a copy immediately.               |
  16. // +----------------------------------------------------------------------+
  17. // | Authors: Antonio Jozzolino <info@sgd.com.br>                         |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: RSSerror.php,v 1.13 2006/11/24 17:18:58 Antonio Jozzolino Exp $
  21. //
  22. //This verifies if the class already was declared.
  23. if(!class_exists('RSSerror')){
  24.     ini_set('display_errors'0);
  25.     /**
  26.      * This class is necessary to make xml file.
  27.      * @link http://www.phpclasses.org/browse/package/1820.html
  28.      */
  29.     require_once("phpclasses/class_easyrss/class.easyrss.php");    
  30.     /**
  31.      * Generates a rss feed from the php error log file.
  32.      *
  33.      * <p>Features:</p>
  34.      * <ul>
  35.      * <li>XML 2.0 compatible</li>
  36.      * <li>Shows the error line and some lines of source code.</li>
  37.      * <li>Protected with password.</li>
  38.      * <li>Generates link for edition of php file in developer machine (Best result with Firefox 2.0 e sage extension).</li>
  39.      * </ul>
  40.      * <b>USASE</b>:
  41.      * 
  42.      * Create a file with a name secret or deceiving (ex: listimages.php ) and put this code below.
  43.      *
  44.      * <code>
  45.      *
  46.      *  include_once('RSSerror.php');
  47.      * 
  48.      *  $args  = array( "root"        => "/home/user/public_html/",
  49.      *                  "local"       => "file:///C:/Documents%20and%20Settings/User/My%20Documents/My%20site/",
  50.      *                  "log"         => "/home/user/var/error_log",
  51.      *                  "url"         => "http://www.user.com.br/",
  52.      *                  "lines"       => 5,
  53.      *                  "password"    => "123456"
  54.      *                );
  55.      *
  56.      * $param = array( "encoding"    => "UTF-8",
  57.      *                 "language"    => "pt-BR",
  58.      *                 "title"       => "Error Report for www.user.com.br",
  59.      *                 "description" => "Please corrects some errors occurred in the site www.user.com.br",
  60.      *                 "stylesheet"  => "http://www.user.com.br/rsserror.css",
  61.      *                 "link"        => "http://www.user.com.br/"
  62.      *               );
  63.      * $rss = new RRSerror($args, $param);
  64.      *
  65.      * </code>
  66.      * 
  67.      * in your feeds agregador, call the file of that sorts things out:
  68.      * http://www.domain.com/listimages.php?p=123456
  69.      *  
  70.      * @example    RSSerrorExample.php How to use this class.
  71.      * @author     Antonio Jozzolino <info@sgd.com.br>
  72.      * @version    $Revision: 1.13 $
  73.      * @since      2006/11/21 17:34:32
  74.      * @access     public
  75.      * @see        http://www.sgd.com.br
  76.      * @todo       Create a way to turn application secure, specifically to encrypt the xml code generated.
  77.      * @todo       To do xml code more compatible with RSS readers like Google Reader.
  78.      * @link       http://www.sgd.com.br/images/class_RSSerror.png Google Reader screenshot
  79.      * @link       http://www.phpclasses.org/browse/package/3543.html phpclasses.org package
  80.      * @link       http://www.sgd.com.br/content/view/26/88888890/ Monitorando erros em sites PHP via RSS
  81.      */
  82.     class RRSerror extends easyRSS{    
  83.         /**
  84.          * Necessary parameters for the class RSSerror
  85.          *
  86.          * @access public
  87.          * @var string key["root"] Path of home directory of your site in UNIX format.
  88.          * @var string key["local"] Path of home LOCAL directory of your site in UNIX or Windows format .
  89.          * @var string key["log"] Complete path of your log file in UNIX format.
  90.          * @var string key["url"] URL of your site.
  91.          * @var string key["lines"] Number of lines before and after the line that to present error.
  92.          * @var string key["password"] Your password.
  93.          */
  94.         var $args  = array("root"        => "/home/www/",
  95.                            "local"       => "file:///C:/Documents%20and%20Settings/Antonio%20Jozzolino/My%20Documents/My%20site/",
  96.                            "log"         => "/home/var/logs/.error_log",
  97.                            "url"         => "http://www.sgd.com.br/",
  98.                             "lines"       => 5,
  99.                             "password"    => "xxxxxx"
  100.                            );
  101.         /**
  102.          * Necessary parameters for the class easyRSS
  103.          *
  104.          * @access public
  105.          * @var string key["encoding"] Default is UTF-8.
  106.          * @var string key["language"] Default is pt-BR.
  107.          * @var string key["title"] Title of your feed.
  108.          * @var string key["description"] Description of your feed.
  109.          * @var string key["stylesheet"] URL of your css file. Let it empty if you don't have one.
  110.          * @var string key["link"] Link to your site.
  111.          */
  112.         var $param = array("encoding"    => "UTF-8",
  113.                             "language"    => "pt-BR",
  114.                            "title"       => "Error Report for www.sgd.com.br",
  115.                            "description" => "Please corrects some errors occurred in the site www.sgd.com.br",
  116.                            "stylesheet"  => "http://www.sgd.com.br/includes/class_RSSerror/rsserror.css",
  117.                            "link"        => "http://www.sgd.com.br/"
  118.                           );        
  119.         /**
  120.          * This var save old value off $args["root"]
  121.          * 
  122.          * @access public
  123.          * @var string 
  124.          */
  125.         var $root;
  126.         /**
  127.          * Constructor function.
  128.          *
  129.          * This function opens the log file, it extracts the information passing
  130.          * them for an array, setting up the ítens of the file rss. In the end,
  131.          * it calls the function easy RSS and it prints the xml file .
  132.          *
  133.          * @param   array [$args] Necessary parameters for the class RSSerror
  134.          * @param   array [$param] Necessary parameters for the class easyRSS
  135.          * @return  string xml/rss file format
  136.          * @since   2006/11/23 13:39:13
  137.          */
  138.         function RRSerror($args$param)
  139.         {    
  140.             //var assignment
  141.             $this->args  = $args;
  142.             $this->param = $param;
  143.             
  144.             //save old value of $this->args["root"] var
  145.             $this->root = $this->args["root"];
  146.             
  147.             //make a path var
  148.             $log split("/"$this->args["root"]);
  149.             $this->args["root""";
  150.             for ($i=1$i<count($log)$i++){
  151.                 $this->args["root".= "\/".$log[$i]
  152.             }
  153.  
  154.             //Ok, I know, it's horrible. I promise fix it soon.
  155.             if(!isset($_GET["p"])) exit;
  156.             if($_GET["p"!= $this->args["password"exit;
  157.             
  158.         
  159.             //It opens the log archive
  160.             $file_log file($this->args['log']);
  161.             
  162.             //It verify if log file is empty
  163.             if($file_log){    
  164.                 //This makes an array from the log file
  165.                 $file_log array_reverse($file_log);        
  166.                 $size (count($file_log>= 10)10 count($file_log);  
  167.                 for($i=0$i<$size$i++){
  168.                    if (preg_match("/".$this->args['root']."([^\s)]*)/"$file_log[$i])){
  169.                        $error[$i]["title"]       $this->title($file_log[$i]);
  170.                        $error[$i]["description"$this->description($file_log[$i]);   
  171.                        $error[$i]["pubDate"]     $this->data($file_log[$i]);
  172.                        $error[$i]["link"]        $this->links($file_log[$i]);
  173.                    }
  174.                 }
  175.             }
  176.             
  177.             
  178.             if(!isset($error)){
  179.                    $error[0]["title"]       "All clear";
  180.                    $error[0]["description""No error detected yet.";   
  181.                    $error[0]["pubDate"]     time();
  182.                    $error[0]["link"]        $this->args["url"];        
  183.             }        
  184.             //RSS setup parameters Attribution    
  185.             $atrbs array"encoding"    => $this->param["encoding"],
  186.                              "language"    => $this->param["language"],
  187.                             "title"       => $this->param["title"],
  188.                             "description" => $this->param["description"],
  189.                             "stylesheet"  => $this->param["stylesheet"],
  190.                             "link"        => $this->param["link"],
  191.                             "items"       => $error
  192.                           );
  193.             header("Content-type: application/xml");
  194.             echo parent::rss($atrbs);
  195.         }        
  196.         /**
  197.          * This function returns PHP error type.
  198.          *
  199.          * @param   string [$line] Line of log file
  200.          * @return  string PHP error type
  201.          * @since   2006/11/23 12:36:36
  202.          */
  203.         function title($line)
  204.         {
  205.             preg_match("/(PHP[^:]*):/"$line$result);
  206.             return utf8_encode($result[1]);
  207.         }
  208.         /**
  209.          * This function returns part of the source code with the line that presented error.
  210.          *
  211.          * @param   string [$line] Line of log file
  212.          * @return  string HTML formated source code
  213.          * @since   2006/11/23 12:36:36
  214.          */
  215.         function description($line)
  216.         {
  217.             preg_match("/\][^P]*P[^:]*: (.*)/"$line$result);
  218.             preg_match("/on line ([0-9]*)/"$result[1]$line_n);
  219.             $output  "<p><strong>".utf8_encode($result[1])."</strong></p>\n";
  220.             if(preg_match("/".$this->args['root']."([^\s)]*)/"$line$file)){
  221.                 $source  file($this->root.$file[1]);
  222.                 array_unshift($source'');
  223.                 if($source){
  224.                     $begin   (($line_n[1$this->args['lines']<= 0)($line_n[1$this->args['lines']);
  225.                     $end     (($line_n[1$this->args['lines']>= count($source))count($source($line_n[1$this->args['lines']);
  226.                     $output  .='<pre>'."\n";
  227.                     for($i=$begin$i<=$end$i++){
  228.                         if($i == $line_n[1]){
  229.                             $output .= '<table border="0" cellpadding="0" cellspacing="0" width="100%">'
  230.                                     .'<tr><td bgcolor="#DFDFDF"><strong>['.$i
  231.                                     .']&nbsp;</strong></td><td bgcolor="#DFDFDF"><strong>'
  232.                                     .utf8_encode(htmlentities($source[$i]))
  233.                                     ."</strong></td></tr></table>";
  234.                         }else{
  235.                             $output .= '['.$i.']&nbsp;'.utf8_encode(htmlentities($source[$i]));
  236.                         }        
  237.                     }
  238.                     $output .= "</pre>";
  239.                 }else{
  240.                     $output "Sorry! I can't show source code now.";    
  241.                 }
  242.             }else{
  243.                  $output "Sorry! I can't show source code now.";   
  244.             }            
  245.             return $output;
  246.         }
  247.         /**
  248.          * This function catches the date in the log file and it returns converted in the format of the UNIX.
  249.          *
  250.          * @param   string [$line] Line of log file
  251.          * @return  integer UNIX timestamp date format
  252.          * @since   2006/11/23 12:36:36
  253.          */
  254.         function data($line)
  255.         {
  256.             preg_match("/\[([^]]*)\]/"$line$result);
  257.             $datetime split(" "$result[1]);
  258.             $hour split(":"$datetime[1]);
  259.             $time (strtotime($datetime[0]($hour[0]*3600($hour[1]*60$hour[2]);
  260.             return $time;
  261.         }
  262.         /**
  263.          * This function returns the physical path or the url of the file that it presented errors.
  264.          *
  265.          * @param   string [$line] Line of log file
  266.          * @param   bolean [$default] Key to choose the return value
  267.          * @return  string It returns url file or file fisical path if false
  268.          * @since   2006/11/23 11:30:08
  269.          */
  270.         function links($line)
  271.         {
  272.             if(preg_match("/".$this->args['root']."([^\s)]*)/"$line$result)){
  273.                 $links $this->args['local'].$result[1];
  274.             }else{
  275.                 $links "";
  276.             }
  277.             return  utf8_encode($links);
  278.         }
  279.     }
  280. }    
  281. ?>

Documentation generated on Wed, 06 Aug 2008 12:59:17 -0300 by phpDocumentor 1.4.1