[ Index ]

MailPress 7.2

[ 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          $REQUEST_URI = '';
  43          $REQUEST_URI = filter_input( INPUT_SERVER, 'REQUEST_URI' );
  44  
  45          $HTTP_USER_AGENT = '';
  46          $HTTP_USER_AGENT = filter_input( INPUT_SERVER, 'HTTP_USER_AGENT' );
  47  
  48          $this->data = "\n";
  49  
  50          if ( $force ) 
  51          {
  52              $this->log ( " >>> Start | {$this->name} | log forced$plugin_version" );
  53          }
  54          elseif ( !empty( $REQUEST_URI ) )
  55          {
  56              $this->log ( " >>> Start | {$this->name} | level : {$this->level}$plugin_version | $REQUEST_URI" );
  57              //$this->log ( " >>>       | {$this->name} | useragent : $HTTP_USER_AGENT" );
  58          }
  59          else
  60          {
  61              $this->log ( " >>> Start | {$this->name} | level : {$this->level}$plugin_version" );
  62          }
  63  
  64  // purge log
  65          $this->dopurge();
  66  
  67          ob_start();
  68      }
  69  
  70  	function restart()
  71      {
  72          $this->stop();
  73          $this->data = "";
  74          ob_start();
  75      }
  76  
  77  	function log( $x, $level=0 )
  78      {
  79          if ( stripos( $x, 'simplepie' ) == true ) return;
  80          if ( strpos( $x, ' WP_Http' ) == true )   return;
  81  
  82          if ( self::noMP_Log    == $this->level ) return;
  83          if ( $level <= $this->level ) $this->data .= wp_date( 'Y-m-d H:i:s' ) . " -- " . $x . "\n";
  84      }
  85  
  86  	function logError( $error_level, $error_message, $error_file, $error_line )
  87      { 
  88          if ( strpos( $error_message, 'Please use the instanceof operator' ) == true ) return;
  89          $this->log ( "PHP [" . $this->errors[$error_level] . "] $error_level : $error_message in $error_file at line $error_line ", $error_level );
  90      }
  91  
  92  	function stop()
  93      {
  94              if ( self::noMP_Log == $this->level ) return;
  95              if ( 0   != $this->level ) restore_error_handler();
  96  
  97              $log = ( ob_get_length() ) ? ob_get_contents() : '';
  98          if ( ob_get_length() ) ob_end_clean();
  99          if ( !empty( $log ) ) $this->log( $log );
 100  
 101          $this->fh = fopen( $this->file , 'a+' );
 102          fputs( $this->fh, $this->data ); 
 103          fclose( $this->fh ); 
 104      }
 105  
 106  	function end( $y = true )
 107      {
 108              if ( self::noMP_Log == $this->level ) return;
 109              if ( 0   != $this->level ) restore_error_handler();
 110  
 111              $log = ( ob_get_length() ) ? ob_get_contents() : '';
 112          if ( ob_get_length() ) ob_end_clean();
 113          if ( !empty( $log ) ) $this->log( $log );
 114  
 115          $y = ( $y ) ? "TRUE" : "FALSE";
 116          $this->log ( " <<< End   | {$this->name} | level : $this->level | status : $y " );
 117  
 118          $this->fh = fopen( $this->file , 'a+' );
 119          fputs( $this->fh, $this->data ); 
 120          fclose( $this->fh ); 
 121  
 122  // mem'ries ...
 123          $xs = array(     'this->data', 'this->errors', 'this->name', 'this->path', 'this->plug', 'this->ftmplt', 'this->level', 'this->levels', 'this->lastpurge', 'this->lognbr' );
 124          foreach ( $xs as $x ) if ( isset( $$x ) ) unset( $$x );
 125      }
 126  
 127  	function dopurge()
 128      {
 129          $now = wp_date( 'Ymd' );
 130          $this->lastpurge= $this->log_options['lastpurge']      ?? $now;
 131          $this->lognbr     = ( int ) $this->log_options['lognbr'] ?? 1;
 132  
 133          if ( $now == $this->lastpurge ) return;
 134  
 135          $this->log_options['lastpurge'] = $now;
 136          $this->log_options['lognbr']    = $this->lognbr;
 137  
 138          self::update_option( $this->option_name, $this->log_options );
 139  
 140          $xs = array();
 141          $l = opendir( $this->path );
 142          if ( $l ) 
 143          {
 144              while ( ( $file = readdir( $l ) ) !== false ) if ( preg_match( '#' . $this->ftmplt . '[0-9]#', $file ) ) $xs[] = $file;
 145              @closedir( $l );
 146          }
 147  
 148          sort( $xs );
 149          $y = count( $xs ) - $this->lognbr + 1;
 150  
 151          while ( $y > 0 )
 152          {
 153              @unlink( $file = $this->path . '/' . array_shift( $xs ) );
 154              $this->log ( " **** Purged log file **** " . $file );
 155              $y--;
 156          }
 157      }
 158  
 159  	function debug_backtrace()
 160      {
 161          $calls = debug_backtrace();
 162          array_shift( $calls );        // we know who we are !
 163  
 164          $debug_backtrace = array();
 165  
 166          foreach( $calls as $call ) 
 167          {
 168              $x = array( 'class' => null, 'function' => null );
 169              foreach( $x as $k => $v )
 170              {
 171                  $x[$k] = $call[$k] ?? null;
 172              }
 173              $debug_backtrace[] = implode( '/', $x );
 174          }
 175          return $debug_backtrace;
 176      }
 177  
 178  	public static function get_defined_constants( $s = true )
 179      {
 180          $x = (!$s) ? array() :     array(    123456789    => __( 'No logging', 'MailPress' ) , 
 181                                      0        => __( 'simple log', 'MailPress' )
 182                              );
 183  
 184          $f = array();
 185          $e = get_defined_constants(true);
 186          $e = $e['Core'];
 187          foreach( $e as $k => $v)
 188          {
 189              if ('E_' == substr($k, 0, 2)) $f[$v] = $k;
 190          }
 191          ksort( $f );
 192          $z = $x + $f;
 193  
 194          return $z;
 195      }
 196  
 197  	public static function get_option( $name )
 198      {
 199          $options = get_option( MailPress::option_name_logs );
 200                  if ( !isset( $options[$name] ) )
 201                  {
 202                      self::set_option( $name );
 203                      return MailPress::$default_option_logs;
 204                  }
 205                  $option = $options[$name];
 206                  $option['level'] = (int) $option['level'];
 207  
 208                  $errors = self::get_defined_constants();
 209                  if ( !isset( $errors[$option['level']] ) ) $option['level'] = (int) MailPress::$default_option_logs['level'];
 210                  return $option;
 211      }
 212          
 213          public static function update_option( $name, $option )
 214      {
 215          $logs = get_option( MailPress::option_name_logs );
 216          $option['level'] = (int) $option['level'];
 217          $logs[$name] = $option;
 218          update_option ( MailPress::option_name_logs, $logs );
 219      }
 220          
 221          public static function set_option( $name )
 222      {
 223          $logs = get_option( MailPress::option_name_logs );
 224          if ( !isset( $logs[$name] ) )
 225          {
 226              $logs[$name] = MailPress::$default_option_logs;
 227              update_option( MailPress::option_name_logs, $logs );
 228          }
 229      }
 230  }


Generated: Tue May 19 15:55:14 2020 Cross-referenced by PHPXref 0.7.1