Location: PHPKode > projects > XMail Log Archiver > xmlogarch.d/reports.d/zzzy-move.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 = "Archived Log Files";

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

#
# this is the function to perform the report
# must be called ($modulenName)Do() and recieves no parameters
function moveDo () {
	global $moveLogs, $logRoot, $archRoot;
	if ( $moveLogs == TRUE ) {
		logIt( "Moving using archive root '" . $archRoot . "'." . PHP_EOL, _me );
		$procLogs = getLogsToArch ( $logRoot );
		$logMoveCount = moveLogs( $procLogs );
		logIt( "Total " . $logMoveCount . " log files moved to '" . $archRoot . "'.", _me );
//		highMoveScores($logMoveCount);
	}
}

function moveLogs ( $procLogs, $highs = TRUE ) {
	global $dotsPer, $logRoot, $begun;
	$retVal = FALSE;
	$dots = 0;
	memUse( "Starting log movement" );
	pIt( "Moving stale logs to archive directory... ", NULL, $GLOBALS["showDots"] );
	$logCount = 0;
	foreach ( $procLogs as $proc ) {
		$dots++;
		$grep = "/(\\S*)\\-[0-9]{12}/";
		if ( preg_match( $grep, $proc, $iAm ) ) {
			$scores['type'][$proc] = $iAm[1];
			$scores['highScoreMethods']['type'] = "count";
		}
		$info = stat( slashDir( $logRoot ) . $proc );
		$scores['size'][$proc] = $info[7];
		$scores['highScoreMethods']['size'] = "big";
		$scores['age'][$proc] = $info[9];
		$scores['highScoreMethods']['age'] = "age";
		$ret = moveALog( $proc );
		if ( $ret == TRUE ) {
			$ret = "+";
			$logCount++;
		}
		dot( $dots, $ret );
	}
	$titles = array(
		"report" => "Archived Log Files",
		"type" => "Log Types Found",
		"size" => "Log Size",
		"age" => "Log File Age (days:hours:minutes:seconds)",
		);
	if ( $highs ) {
		highScores ( $scores, $titles );
	}
	pIt( " Done." . PHP_EOL, NULL, $GLOBALS["showDots"] );
	$retVal = $logCount;
	return $retVal;
}

function moveALog ( $log, $dest = FALSE ) {
	global $logRoot, $archRoot, $logMoveCount, $mvCommand, $mvCommandOpts;
	if ( !file_exists( $mvCommand ) ) {
		mex( 25, $mvCommand );
	}
	$retVal = FALSE;
	$errLvl = FALSE;
	if ( !$dest ) {
		$dest = $archRoot;
	}
	if ( !is_dir( $dest ) ) {
		mkdir( $dest, 770 );
	}
	$archLog = slashDir( $dest ) . $log;
	if ( !(substr( $log, 0, 1 ) == DIRECTORY_SEPARATOR) ) {
		if ( file_exists( slashDir( $logRoot ) . $log ) ) {
			$log = slashDir( $logRoot ) . $log;
		} else {
			$log = slashDir( getcwd() ) . $log;
		}
	}
	# move logs to the archive first - Xmail will start making new ones
	if ( file_exists( $log ) ) {
		$cmd = $mvCommand . " " . $mvCommandOpts . " " . $log . " " . $dest . " 2>&1"; # must redirect stderr to stdout to capture
		if ( file_exists( $archLog ) ) {
			$orig = stat( $log );
			$oldness = stat( $archLog );
			if ( ( $orig[9] == $oldness[9] ) | ( $orig[9] > $oldness[9] ) ) { # files are the same mod date
				exec( $cmd, $execOut, $errLvl );
				unlink( $log );
			} else {
				$retVal = "A";
				$errLvl = 999;
			}
		} else {
			exec( $cmd, $execOut, $errLvl );
		}
		if ( $errLvl == 0 ) {
			$retVal = TRUE;
		} else {
			logIt( "Moving '" . $log . "' to '" . $archRoot . "' yielded " . $errLvl . ".", $log );
			$retVal = "E";
		}
	} else {
		$retVal = "D";
	}
	return $retVal;
}

?>
Return current item: XMail Log Archiver