Location: PHPKode > projects > XMail Log Archiver > xmlogarch.d/reports.d/zzzz-zip.php
<?php
/*
	 License
    XMail Log Archiver
	 http://xmlogarch.sourceforge.net
    Copyright (C) 2006 Bryn Mosher

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License Version 2 as
	 published by the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not contact , write to 
	 hide@address.com
	 - or -
	 Free Software Foundation, Inc.
	 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#
# (ie myreport.php  is named myreport )
# name of php file  without the '.php' extension
$modTitle = "Zipped Log Files";

#
# this is used to detect loaded modules
# key must be the same as name
#val is array of required modules
$reqRepMod['zip'] = array();

#
# this is the function to perform the report
# must be called ($modulenName)Do() and recieves no parameters
function zipDo () {
	global $archRoot, $zipFile, $zipLogs, $highScoreCount;
	if ( $zipLogs == TRUE ) {
		$zipCmd = $GLOBALS["zipCommand"] . " " . $GLOBALS["zipCommandOpts"];
		logIt( "Zipping files with command '" . $zipCmd . "'", me );
		logIt( "Zipping using archive root '" . $archRoot . "'." . PHP_EOL, _me );
		logIt( "Zipping using archive '" . slashDir( $archRoot ) . $zipFile . "'." . PHP_EOL, _me );
		if ( $logZipped = zipEm( $archRoot ) ) {
			$totalZipped = ( $logZipped['added'] + $logZipped['updated'] );
			logIt( "Total " . $totalZipped . " log files of " . ( $totalZipped + $logZipped['ignored'] ) . " from '" . $archRoot . "' zipped in '" . $zipFile . "'.", _me );
			if ( $totalZipped > 0 ) {
				logIt( "Average " . round( array_sum( $logZipped['percentages'] ) / count( $logZipped['percentages'] ), 2 ) . "% compression zipping to '" . $zipFile . "'.", _me );
				memUse( "Generating Zip High Scores" );
				logIt( "ZIPPED LOGS HIGH SCORES:", _me, NULL, FALSE );
				$themTotals = array_combine( $logZipped['zipped'], $logZipped['percentages'] );
				asort( $themTotals );
				$topX = array_slice( array_reverse( $themTotals ), 0, $highScoreCount );
				$logOut = str_repeat( " ", 4 ) . "Best Compression of Zipped Files: ";
				logIt( $logOut, _me, NULL, FALSE, TRUE );
				memUse( "Generated Best Compression High Scores" );
				$countOut = 0;
				foreach ( $topX as $value => $score ) {
					$countOut++;
					$scoreOut = str_repeat( " ", 5 ) . str_pad( "#" . $countOut, 5, " ", STR_PAD_LEFT ) . " " . str_pad( "(" . trim( $score ) . ")", 8, ".", STR_PAD_RIGHT ) . str_pad( " " . trim( $value ), 40, ".", STR_PAD_LEFT );
					$topWhole .= $scoreOut . PHP_EOL;
					logIt( $scoreOut, _me, NULL, FALSE, TRUE );
				}
			}
		} else {
			mex( 21, $archroot );
		}
	} else {
		logIt( "\$zipLogs is not set to true. No reporting performed.", _me );
	}

}

function zipEm ( $logRoot ) { # add mem reporting
	global $zipLogs;
	$retVal = FALSE;
	$zipFile = slashDir( $logRoot ) . $GLOBALS["zipFile"];
	$oldDir = getcwd();
	chdir( $logRoot );
	$ignored = 0;
	$added = 0;
	$updated = 0;
	$deflated = array();
	$zipped = array();
	$dots = 0;
	if ( $zipLogs ) {
		if ( file_exists($GLOBALS["zipCommand"] ) ) {
			$zipCmd = $GLOBALS["zipCommand"] . " " . $GLOBALS["zipCommandOpts"];
			$archFiles = getLogsToArch( $logRoot );
			pIt( "Zipping log files (A=added file, U=updated file, i=ignored)... ", NULL, $GLOBALS["showDots"] );
			foreach ( $archFiles as $file ) {
				$dots++;
				$dotAll = NULL;
				$ret = "i";
				$cmd = $zipCmd . " " . $zipFile . " " . $file . " 2>&1";
				exec( $cmd, $execOut, $errLvl );
				switch ( $errLvl ) {
					# 0   normal; no errors or warnings detected. 
					case 0:
						$up = preg_match( "/updating\\:\\s*(\\S*)\\s*/i", $execOut[0] );
						$add = preg_match( "/adding\\:\\s*(\\S*)\\s*/i", $execOut[0] );
						$updated = $up ? $updated + 1 : $updated;
						$added = $add ? $added + 1 : $added;
						$ret = $up ? "U" : "A";
						$dotAll = TRUE;
						preg_match( "/\\: (\\S*) \\(deflated (\\S*)\\%/", $execOut[0], $zipExit );
						$zipped[] = $zipExit[1];
						$deflated[] = $zipExit[2];
						break;
					case 2: # 2   unexpected end of zip file. 
					case 3: # 3   a generic error in the zipfile format was detected. Processing may have completed successfully anyway; some broken zipfiles created by other archivers have simple work-arounds. 
					case 4: # 4   zip was unable to allocate memory for one or more buffers during program initialization. 
					case 5: # 5   a severe error in the zipfile format was detected. Processing probably failed immediately. 
					case 7: # 7   invalid comment format
 					case 8: # 8   zip -T failed or out of memory
 					case 9: # 9   the user aborted zip prematurely with control-C (or similar) 
					case 10: # 10  zip encountered an error while using a temp file 
					case 11: # 11  read or seek error
					case 13: # 13  missing or empty zip file 
					case 14: # 14  error writing to a file 
					case 15: # 15  zip was unable to create a file to write to 
					case 16: # 16  bad command line parameters 
						mex( 21, slashDir( $logRoot ) . $zipFile . $execOut[0] );
					case 12: # 12  zip has nothing to do 
					case 18: # 18  zip could not open a specified file to read
						$ignored++;
						break;
				}
				dot( $dots, $ret, $dotAll );
				unset( $execOut );
			}
			$retVal = array(
				"added" => $added,
				"updated" => $updated,
				"ignored" => $ignored,
				"percentages" => $deflated,
				"zipped" => $zipped,
			);
			pIt( " Done." . PHP_EOL, NULL, $GLOBALS["showDots"] );
		} else {
			mex( 20, $GLOBALS["zipCommand"] );
		}
	}
	chdir( $oldDir );
	return $retVal;
}

?>
Return current item: XMail Log Archiver