[ Index ]

MailPress 7.1

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]     [ Statistics ]    

title

Body

[close]

/mp-includes/class/ -> MP_Log.class.php (source)

   1  <?php
   2  class MP_Log
   3  {
   4      const noMP_Log    = 123456789;
   5  
   6  	function __construct( $name, $args = array() )
   7      {
   8          $this->errors = self::get_defined_constants( false );
   9  
  10          $defaults = array(     'path'        => MP_UPL_ABSPATH,
  11                          'force'        => false,
  12                          'option_name'    => 'general',
  13          );
  14          $r = wp_parse_args( $args, $defaults );
  15          extract( $r );
  16  
  17          $this->name     = $name;
  18          $this->path     = $path . 'log';
  19          $this->option_name= $option_name;
  20  
  21          $this->ftmplt    = 'MP_Log' . '_' . get_current_blog_id() . '_' . $this->name . '_';
  22  
  23          $this->file     = $this->path . '/' . $this->ftmplt . gmdate( 'Ymd', current_time( 'timestamp' ) ) . '.txt';
  24  
  25          $this->log_options = self::get_option( $this->option_name );
  26  
  27          $this->level = (int) ( $force ) ? 0 : $this->log_options['level'];
  28          foreach( $this->errors as $k => $v ) { $this->levels[$k] = ( $force ) ? 0 : $k; }
  29  
  30          if ( !is_dir( $this->path ) ) $this->level = self::noMP_Log;
  31          if ( self::noMP_Log == $this->level ) return;
  32  
  33          if ( 0  != $this->level ) set_error_handler( array( $this, 'logError' ), $this->level );
  34  
  35          $this->start( $force );
  36      }
  37  
  38  	function start( $force = false )
  39      {
  40          $plugin_version = ' **** ( ' . MP_Version . ' )';
  41  
  42          $page = '';
  43          $page = filter_input( INPUT_SERVER, 'REQUEST_URI' );
  44  
  45          $this->data = "\n";
  46  
  47          if ( $force ) 
  48              $this->log ( " **** Start logging **** {$this->name} *** log forced$plugin_version" );
  49          elseif ( !empty( $page ) )
  50              $this->log ( " **** Start logging **** {$this->name} *** level : {$this->level}$plugin_version **** $page" );
  51          else
  52              $this->log ( " **** Start logging **** {$this->name} *** level : {$this->level}$plugin_version" );
  53  
  54  // purge log
  55          $this->dopurge();
  56  
  57          ob_start();
  58      }
  59  
  60  	function restart()
  61      {
  62          $this->stop();
  63          $this->data = "";
  64          ob_start();
  65      }
  66  
  67  	function log( $x, $level=0 )
  68      {
  69          if ( stripos( $x, 'simplepie' ) == true ) return;
  70          if ( strpos( $x, ' WP_Http' ) == true )   return;
  71  
  72          if ( self::noMP_Log    == $this->level ) return;
  73          if ( $level <= $this->level ) $this->data .= date_i18n( 'Y-m-d H:i:s u' ) . " -- " . $x . "\n";
  74      }
  75  
  76  	function logError( $error_level, $error_message, $error_file, $error_line )
  77      { 
  78          if ( strpos( $error_message, 'Please use the instanceof operator' ) == true ) return;
  79          $this->log ( "PHP [" . $this->errors[$error_level] . "] $error_level : $error_message in $error_file at line $error_line ", $error_level );
  80      }
  81  
  82  	function stop()
  83      {
  84              if ( self::noMP_Log == $this->level ) return;
  85              if ( 0   != $this->level ) restore_error_handler();
  86  
  87              $log = ( ob_get_length() ) ? ob_get_contents() : '';
  88          if ( ob_get_length() ) ob_end_clean();
  89          if ( !empty( $log ) ) $this->log( $log );
  90  
  91          $this->fh = fopen( $this->file , 'a+' );
  92          fputs( $this->fh, $this->data ); 
  93          fclose( $this->fh ); 
  94      }
  95  
  96  	function end( $y = true )
  97      {
  98              if ( self::noMP_Log == $this->level ) return;
  99              if ( 0   != $this->level ) restore_error_handler();
 100  
 101              $log = ( ob_get_length() ) ? ob_get_contents() : '';
 102          if ( ob_get_length() ) ob_end_clean();
 103          if ( !empty( $log ) ) $this->log( $log );
 104  
 105          $y = ( $y ) ? "TRUE" : "FALSE";
 106  
 107          $this->log ( " **** End   logging **** {$this->name} *** level : $this->level **** status : $y " );
 108  
 109          $this->fh = fopen( $this->file , 'a+' );
 110          fputs( $this->fh, $this->data ); 
 111          fclose( $this->fh ); 
 112  
 113  // mem'ries ...
 114          $xs = array(     'this->data', 'this->errors', 'this->name', 'this->path', 'this->plug', 'this->ftmplt', 'this->level', 'this->levels', 'this->lastpurge', 'this->lognbr' );
 115          foreach ( $xs as $x ) if ( isset( $$x ) ) unset( $$x );
 116      }
 117  
 118  	function dopurge()
 119      {
 120          $now = date_i18n( 'Ymd', current_time( 'timestamp' ) );
 121          $this->lastpurge= ( isset( $this->log_options['lastpurge'] ) ) ? $this->log_options['lastpurge']         : $now;
 122          $this->lognbr     = ( isset( $this->log_options['lognbr'] ) )    ? ( int ) $this->log_options['lognbr']     : 1;
 123  
 124          if ( $now == $this->lastpurge ) return;
 125  
 126          $this->log_options['lastpurge'] = $now;
 127          $this->log_options['lognbr']    = $this->lognbr;
 128  
 129          self::update_option( $this->option_name, $this->log_options );
 130  
 131          $xs = array();
 132          $l = opendir( $this->path );
 133          if ( $l ) 
 134          {
 135              while ( ( $file = readdir( $l ) ) !== false ) if ( preg_match( '#' . $this->ftmplt . '[0-9]#', $file ) ) $xs[] = $file;
 136              @closedir( $l );
 137          }
 138  
 139          sort( $xs );
 140          $y = count( $xs ) - $this->lognbr + 1;
 141  
 142          while ( $y > 0 )
 143          {
 144              @unlink( $file = $this->path . '/' . array_shift( $xs ) );
 145              $this->log ( " **** Purged log file **** " . $file );
 146              $y--;
 147          }
 148      }
 149  
 150  	public static function get_defined_constants( $s = true )
 151      {
 152          $x = (!$s) ? array() :     array(    123456789    => __( 'No logging', 'MailPress' ) , 
 153                                      0        => __( 'simple log', 'MailPress' )
 154                              );
 155  
 156          $f = array();
 157          $e = get_defined_constants(true);
 158          $e = $e['Core'];
 159          foreach( $e as $k => $v)
 160          {
 161              if ('E_' == substr($k, 0, 2)) $f[$v] = $k;
 162          }
 163          ksort( $f );
 164          $z = $x + $f;
 165  
 166          return $z;
 167      }
 168  
 169  	public static function get_option( $name )
 170      {
 171          $options = get_option( MailPress::option_name_logs );
 172                  if ( !isset( $options[$name] ) )
 173                  {
 174                      self::set_option( $name );
 175                      return MailPress::$default_option_logs;
 176                  }
 177                  $option = $options[$name];
 178                  $option['level'] = (int) $option['level'];
 179  
 180                  $errors = self::get_defined_constants();
 181                  if ( !isset( $errors[$option['level']] ) ) $option['level'] = (int) MailPress::$default_option_logs['level'];
 182                  return $option;
 183      }
 184          
 185          public static function update_option( $name, $option )
 186      {
 187          $logs = get_option( MailPress::option_name_logs );
 188          $option['level'] = (int) $option['level'];
 189          $logs[$name] = $option;
 190          update_option ( MailPress::option_name_logs, $logs );
 191      }
 192          
 193          public static function set_option( $name )
 194      {
 195          $logs = get_option( MailPress::option_name_logs );
 196          if ( !isset( $logs[$name] ) )
 197          {
 198              $logs[$name] = MailPress::$default_option_logs;
 199              update_option( MailPress::option_name_logs, $logs );
 200          }
 201      }
 202  }


Generated: Mon Mar 11 18:33:33 2019 Cross-referenced by PHPXref 0.7.1