/**
  *	self::status()
  *
  *	Logs data to a CSV file. Optional unique serial identifier.
  *	If a serial is passed then EVERYTHING will be logged to the specified serial file in addition to whatever (if anything) is logged to main status file.
  *	Always logs to main status file based on logging settings whether serial is passed or not.
  *	NOTE: When full logging is on AND a serial is passed, it will be written to a _sum_ text file instead of the main log file.
  *
  *	@see self::get_status().
  *
  *	@param	string			$type		Valid types: error, warning, details, message
  *	@param	string			$text		Text message to log.
  *	@param	string			$serial		Optional. Optional unique identifier for this plugin's message. Status messages are unique per plugin so this adds an additional unique layer for retrieval.
  *										If self::$_status_serial has been set by set_status_serial() then it will override if $serial is blank.
  *	@return	null
  */
 public static function status($type, $message, $serials = '', $js_mode = false, $echoNotWrite = false)
 {
     if (!class_exists('backupbuddy_core')) {
         require_once pb_backupbuddy::plugin_path() . '/classes/core.php';
     }
     if (self::$_status_serial != '' && $serials == '') {
         $serials = self::$_status_serial;
     }
     if (defined('BACKUPBUDDY_WP_CLI') && true === BACKUPBUDDY_WP_CLI) {
         if (class_exists('WP_CLI')) {
             WP_CLI::line($type . ' - ' . $message);
         }
     }
     // Make sure we have a unique log serial for all logs for security.
     if (!isset(self::$options['log_serial']) || self::$options['log_serial'] == '') {
         self::$options['log_serial'] = self::random_string(15);
         self::save();
     }
     if (!is_array($serials)) {
         $serials = array($serials);
     }
     // Calculate log directory.
     $log_directory = backupbuddy_core::getLogDirectory();
     // Also handles when within importbuddy.
     // Prepare directory for log files. Return if unable to do so.
     if (true === self::$_skiplog) {
         // bool true so skip.
         return;
     } elseif (false !== self::$_skiplog) {
         // something other than bool false so check directory before proceeding.
         if (true !== self::anti_directory_browsing($log_directory, $die_on_fail = false, $deny_all = false, $suppress_alert = true)) {
             // Unable to secure directory. Fail.
             self::$_skiplog = true;
             return;
         } else {
             self::$_skiplog = false;
         }
     }
     foreach ($serials as $serial) {
         // ImportBuddy always write to main status log.
         if (defined('PB_IMPORTBUDDY') && PB_IMPORTBUDDY === true) {
             // IMPORTBUDDY
             $write_serial = false;
             $write_main = true;
             $main_file = $log_directory . 'status-' . self::$options['log_serial'] . '.txt';
         } else {
             // STANDALONE.
             // Determine whether writing to main extraneous log file.
             $write_main = false;
             if (self::$options['log_level'] == 0) {
                 // No logging.
                 $write_main = false;
             } elseif (self::$options['log_level'] == 1) {
                 // Errors only.
                 if ($type == 'error') {
                     $write_main = true;
                     self::log('[' . $serial . '] ' . $message, 'error');
                 }
             } else {
                 // Everything else.
                 $write_main = true;
                 self::log('[' . $serial . '] ' . $message, $type);
             }
             // Determine which normal status log files to write.
             if ($serial != '') {
                 $write_serial = true;
                 $write_main = true;
                 $main_file = $log_directory . 'status-' . $serial . '_sum_' . self::$options['log_serial'] . '.txt';
             } else {
                 $write_serial = false;
                 $write_main = false;
             }
         }
         // Function for writing actual log CSV data. Used later.
         if (!function_exists('write_status_line')) {
             function write_status_line($file, $content_array, $echoNotWrite)
             {
                 $writeData = json_encode($content_array) . PHP_EOL;
                 if (true === $echoNotWrite) {
                     // echo data instead of writing to file. used by ajax when checking status log and needing to prepend before log.
                     echo $writeData;
                 } else {
                     //$delimiter = '|~|';
                     if (false !== ($file_handle = @fopen($file, 'a'))) {
                         // Append mode.
                         //fputcsv ( $file_handle , $content_array );
                         //@fwrite( $file_handle, trim( implode( $delimiter, $content_array ) ) . PHP_EOL );
                         @fwrite($file_handle, $writeData);
                         @fclose($file_handle);
                     } else {
                         //pb_backupbuddy::alert( 'Unable to open file handler for status file `' . $file . '`. Unable to write status log.' );
                     }
                 }
             }
         }
         $content_array = array('event' => $type, 'time' => pb_backupbuddy::$format->localize_time(time()), 'u' => substr((string) microtime(), 2, 2), 'run' => sprintf("%01.2f", round(microtime(true) - self::$start_time, 2)), 'mem' => sprintf("%01.2f", round(memory_get_peak_usage() / 1048576, 2)), 'data' => str_replace(chr(9), '   ', $message));
         /********** MAIN LOG FILE or SUM FILE **********/
         if ($write_main === true) {
             // WRITE TO MAIN LOG FILE or SUM FILE.
             write_status_line($main_file, $content_array, $echoNotWrite);
         }
         /********** SERIAL LOG FILE **********/
         if ($write_serial === true) {
             $serial_file = $log_directory . 'status-' . $serial . '_' . self::$options['log_serial'] . '.txt';
             write_status_line($serial_file, $content_array, $echoNotWrite);
         }
         // Output importbuddy status log to screen.
         global $pb_backupbuddy_js_status;
         if ((defined('PB_IMPORTBUDDY') || isset($pb_backupbuddy_js_status) && $pb_backupbuddy_js_status === true) && 'true' != pb_backupbuddy::_GET('deploy')) {
             // If importbuddy, js mode, and not a deployment.
             echo '<script>pb_status_append( ' . json_encode($content_array) . ' );</script>' . "\n";
             pb_backupbuddy::flush();
         }
     }
     // end foreach $serials.
 }
Exemple #2
0
 /**
  *	self::status()
  *
  *	Logs data to a CSV file. Optional unique serial identifier.
  *	If a serial is passed then EVERYTHING will be logged to the specified serial file in addition to whatever (if anything) is logged to main status file.
  *	Always logs to main status file based on logging settings whether serial is passed or not.
  *
  *	@see self::get_status().
  *
  *	@param	string	$type		Valid types: error, warning, details, message
  *	@param	string	$text		Text message to log.
  *	@param	string	$serial		Optional. Optional unique identifier for this plugin's message. Status messages are unique per plugin so this adds an additional unique layer for retrieval.
  *								If self::$_status_serial has been set by set_status_serial() then it will override if $serial is blank.
  *	@return	null
  */
 public static function status($type, $message, $serial = '', $js_mode = false)
 {
     global $pb_backupbuddy_js_status;
     if (defined('PB_IMPORTBUDDY') || isset($pb_backupbuddy_js_status) && $pb_backupbuddy_js_status === true) {
         $status = pb_backupbuddy::$format->date(time()) . "\t" . sprintf("%01.2f", round(microtime(true) - pb_backupbuddy::$start_time, 2)) . "\t" . sprintf("%01.2f", round(memory_get_peak_usage() / 1048576, 2)) . "\t" . $type . "\t" . str_replace(chr(9), '   ', $message);
         $status = str_replace('\\', '/', $status);
         echo '<script type="text/javascript">pb_status_append("' . str_replace("\n", '\\n', str_replace('"', '&quot;', $status)) . '");</script>';
         //echo '<script type="text/javascript">pb_status_append(\'' . str_replace( '\'', '&#39;', $status ) . '\');</script>';
         /*
         USE WITH JAVASCRIPT IN PAGE:
         <script type="text/javascript">
         	function pb_status_append( status_string ) {
         		target_id = 'importbuddy_status'; // importbuddy_status or pb_backupbuddy_status
         		jQuery( '#' + target_id ).append( "\n" + status_string );
         		textareaelem = document.getElementById( target_id );
         		textareaelem.scrollTop = textareaelem.scrollHeight;
         	}
         </script>
         */
         pb_backupbuddy::flush();
         //return;
     }
     $delimiter = '|~|';
     if (self::$_status_serial != '' && $serial == '') {
         $serial = self::$_status_serial;
     }
     // Make sure we have a unique log serial for all logs for security.
     if (!isset(self::$options['log_serial']) || self::$options['log_serial'] == '') {
         self::$options['log_serial'] = self::random_string(15);
         self::save();
     }
     // Determine whether writing to main file.
     $write_main = false;
     if (self::$options['log_level'] == 0) {
         // No logging.
         $write_main = false;
     } elseif (self::$options['log_level'] == 1) {
         // Errors only.
         if ($type == 'error') {
             $write_main = true;
             self::log('[' . $serial . '] ' . $message, 'error');
         }
     } else {
         // Everything else.
         $write_main = true;
         self::log('[' . $serial . '] ' . $message, $type);
     }
     // Determine whether writing to serial file. Ignores log level.
     if ($serial != '') {
         $write_serial = true;
     } else {
         $write_serial = false;
     }
     // Return if not writing to any file.
     if ($write_main !== true && $write_serial !== true) {
         return;
     }
     // Calculate log directory.
     if (defined('PB_STANDALONE') && PB_STANDALONE === true) {
         $log_directory = ABSPATH . 'importbuddy/';
     } else {
         $log_directory = WP_CONTENT_DIR . '/uploads/pb_' . self::settings('slug') . '/';
     }
     // Prepare directory for log files. Return if unable to do so.
     if (true !== self::anti_directory_browsing($log_directory)) {
         // Unable to secure directory. Fail.
         self::alert('Unable to create / verify anti directory browsing measures for status file `' . $status_file . '`. Log not written for security.');
         return;
     }
     // Function for writing actual log CSV data. Used later.
     if (!function_exists('write_status_line')) {
         function write_status_line($file, $content_array, $delimiter)
         {
             $delimiter = '|~|';
             if (false !== ($file_handle = @fopen($file, 'a'))) {
                 // Append mode.
                 //fputcsv ( $file_handle , $content_array );
                 fwrite($file_handle, trim(implode($delimiter, $content_array)) . PHP_EOL);
                 fclose($file_handle);
             } else {
                 pb_backupbuddy::alert('Unable to open file handler for status file `' . $file . '`. Unable to write status log.');
             }
         }
     }
     $content_array = array(pb_backupbuddy::$format->localize_time(time()), sprintf("%01.2f", round(microtime(true) - self::$start_time, 2)), sprintf("%01.2f", round(memory_get_peak_usage() / 1048576, 2)), $type, str_replace(chr(9), '   ', $message));
     /********** MAIN LOG FILE **********/
     if ($write_main === true) {
         // WRITE TO MAIN LOG FILE.
         $main_file = $log_directory . 'status-' . self::$options['log_serial'] . '.txt';
         write_status_line($main_file, $content_array, $delimiter);
     }
     /********** SERIAL LOG FILE **********/
     if ($write_serial === true) {
         $serial_file = $log_directory . 'status-' . $serial . '_' . self::$options['log_serial'] . '.txt';
         write_status_line($serial_file, $content_array, $delimiter);
     }
 }
Exemple #3
0
 /**
  *	self::status()
  *
  *	Logs data to a CSV file. Optional unique serial identifier.
  *	If a serial is passed then EVERYTHING will be logged to the specified serial file in addition to whatever (if anything) is logged to main status file.
  *	Always logs to main status file based on logging settings whether serial is passed or not.
  *
  *	@see self::get_status().
  *
  *	@param	string			$type		Valid types: error, warning, details, message
  *	@param	string			$text		Text message to log.
  *	@param	string|array	$serial		Optional. Optional unique identifier for this plugin's message. Status messages are unique per plugin so this adds an additional unique layer for retrieval.
  *										If self::$_status_serial has been set by set_status_serial() then it will override if $serial is blank.
  *	@return	null
  */
 public static function status($type, $message, $serials = '', $js_mode = false)
 {
     if (!class_exists('backupbuddy_core')) {
         require_once pb_backupbuddy::plugin_path() . '/classes/core.php';
     }
     if (self::$_status_serial != '' && $serials == '') {
         $serials = self::$_status_serial;
     }
     if (!is_array($serials)) {
         $serials = array($serials);
     }
     global $pb_backupbuddy_js_status;
     if (defined('PB_IMPORTBUDDY') || isset($pb_backupbuddy_js_status) && $pb_backupbuddy_js_status === true) {
         $status = pb_backupbuddy::$format->date(time()) . "\t" . sprintf("%01.2f", round(microtime(true) - pb_backupbuddy::$start_time, 2)) . "\t" . sprintf("%01.2f", round(memory_get_peak_usage() / 1048576, 2)) . "\t" . $type . "\t" . str_replace(chr(9), '   ', $message);
         $status = str_replace('\\', '/', $status);
         echo '<script type="text/javascript">pb_status_append("' . str_replace("\n", '\\n', str_replace('"', '&quot;', $status)) . '");</script>';
         pb_backupbuddy::flush();
     }
     if (defined('BACKUPBUDDY_WP_CLI') && true === BACKUPBUDDY_WP_CLI) {
         if (class_exists('WP_CLI')) {
             WP_CLI::line($type . ' - ' . $message);
         }
     }
     $delimiter = '|~|';
     // Make sure we have a unique log serial for all logs for security.
     if (!isset(self::$options['log_serial']) || self::$options['log_serial'] == '') {
         self::$options['log_serial'] = self::random_string(15);
         self::save();
     }
     foreach ($serials as $serial) {
         // Determine whether writing to main file.
         $write_main = false;
         if (self::$options['log_level'] == 0) {
             // No logging.
             $write_main = false;
         } elseif (self::$options['log_level'] == 1) {
             // Errors only.
             if ($type == 'error') {
                 $write_main = true;
                 self::log('[' . $serial . '] ' . $message, 'error');
             }
         } else {
             // Everything else.
             $write_main = true;
             self::log('[' . $serial . '] ' . $message, $type);
         }
         // Determine whether writing to serial file. Ignores log level.
         if ($serial != '') {
             $write_serial = true;
         } else {
             $write_serial = false;
         }
         // Return if not writing to any file.
         if ($write_main !== true && $write_serial !== true) {
             return;
         }
         // Calculate log directory.
         $log_directory = backupbuddy_core::getLogDirectory();
         // Also handles when within importbuddy.
         // Prepare directory for log files. Return if unable to do so.
         if (true === self::$_skiplog) {
             // bool true so skip.
             return;
         } elseif (false !== self::$_skiplog) {
             // something other than bool false so check directory before proceeding.
             if (true !== self::anti_directory_browsing($log_directory, $die_on_fail = false, $deny_all = false, $suppress_alert = true)) {
                 // Unable to secure directory. Fail.
                 self::$_skiplog = true;
                 return;
             } else {
                 self::$_skiplog = false;
             }
         }
         // Function for writing actual log CSV data. Used later.
         if (!function_exists('write_status_line')) {
             function write_status_line($file, $content_array, $delimiter)
             {
                 $delimiter = '|~|';
                 if (false !== ($file_handle = @fopen($file, 'a'))) {
                     // Append mode.
                     //fputcsv ( $file_handle , $content_array );
                     @fwrite($file_handle, trim(implode($delimiter, $content_array)) . PHP_EOL);
                     @fclose($file_handle);
                 } else {
                     //pb_backupbuddy::alert( 'Unable to open file handler for status file `' . $file . '`. Unable to write status log.' );
                 }
             }
         }
         $content_array = array(pb_backupbuddy::$format->localize_time(time()), sprintf("%01.2f", round(microtime(true) - self::$start_time, 2)), sprintf("%01.2f", round(memory_get_peak_usage() / 1048576, 2)), $type, str_replace(chr(9), '   ', $message));
         /********** MAIN LOG FILE **********/
         if ($write_main === true) {
             // WRITE TO MAIN LOG FILE.
             $main_file = $log_directory . 'status-' . self::$options['log_serial'] . '.txt';
             write_status_line($main_file, $content_array, $delimiter);
         }
         /********** SERIAL LOG FILE **********/
         if ($write_serial === true) {
             $serial_file = $log_directory . 'status-' . $serial . '_' . self::$options['log_serial'] . '.txt';
             write_status_line($serial_file, $content_array, $delimiter);
         }
     }
     // end foreach $serials.
 }