<?php
/*
	cron job setup for different hosting companies
  /usr/local/bin/php /home/blah/public_html/filecheck.php /dev/null 2>&1
  /ramdisk/bin/php5 -q  -- host monster 

  Shell command that finds all files below a certain directory that modified within the last 62 minutes
  Replace the file path (absolute or relative to this script's location) as necessary
  use prune to remove paths you don't want to check
  exec('find /home/username/public_html/ -name error_log -prune -o -path \'/home/username/public_html/administrator/components/com_sef\' -prune -o -path \'/home/username/public_html/components/com_sef\' -prune -o -cmin -62 -print', $last_changed);
*/

error_reporting(0);
$manual = $_REQUEST['run']; 			//needs to be 1 to run manually
$mtime = intval($_REQUEST['time']); 			 //time last files were checked
$clearlog = $_REQUEST['clog'];	 //needs to be 1 to reset log file
$dir =  dirname(__FILE__);
$timecheck = "-59";
//check if it's a windows box

//this is for the cronjob to run

if(trim($manual) == "") { 
		// E-mail settings
	$sendto = "E-mail receiver <alerts@scurit.com>";
	$sendfrom = "File change script <postmaster@localhost>";
	$sendsubject = "[SCURIT FILE MONITOR] - [pshga.com] file change notice";
	$email_output = 'Files modified in the last hour:';
	$email_output .= "\n";
	$email_output .= "\n";    
	$send_eol = "\r\n"; 
	$send_headers = 'From: ' . $sendfrom . $send_eol;
	$send_headers .= 'Reply-To: ' . $sendfrom . $send_eol;
	$send_headers .= 'Return-Path: ' . $sendfrom . $send_eol;    
	$dir = dirname(__FILE__);
	
	//process the file check
	if(!function_exists('exec')) {
	    //echo "exec is disabled";
	    $format = 'Y-m-d H:i:s';
	    $current = date($format); 
	    $oldest = date ( $format, strtotime ( '-59 minute' . $date ) );
	   	    
			//Put the date you want to compare with in the format of:  YYYY-mm-dd hh:mm:ss
			$comparedatestr=$oldest;
			$comparedate=strtotime($comparedatestr);
			//run the function here to start the search. 
			$info = directory_tree($dir,$comparedate); 
			echo $info;
	    if($info !="") { 
	    	$last_changed = 1;
	    	 // Results of files last modified
	    	$last_changed_files = $info;
	    	$email_output .= $last_changed_files;
	    } 
	} else { 
			$strexec = "find " . $dir ."  -name error_log -prune -o -cmin " .$timecheck ." -print";
			exec($strexec, $last_changed);
			if ( count ( $last_changed ) > 1 ) { 
	 			$last_changed_files = implode ( "\n", $last_changed);
	    	$email_output .= $last_changed_files;
	    }
	}
	// Only e-mail the results if anything has changed
	echo count ( $last_changed );
	if ( count ( $last_changed ) > 1 ) {
	    // Send! - write to a log file
	    if(!mail($sendto, $sendsubject, $email_output, $send_headers)) { 
	    	echo "Sendmail failed!";
	    }
	    $myFile = "changedfiles.log";
			$fh = fopen($myFile, 'a');
			fwrite($fh, $email_output); 
			fclose($fh);
	    	
	}
		
} else {
	//run manual check
	if(!function_exists('exec')) {
	    //echo "exec is disabled";
	    $format = 'Y-m-d H:i:s';
	    $current = date($format); 
	    $oldest = date ( $format, strtotime ( '-59 minute' . $date ) );
	   	    
			//Put the date you want to compare with in the format of:  YYYY-mm-dd hh:mm:ss
			$comparedatestr=$oldest;
			$comparedate=strtotime($comparedatestr);
			//run the function here to start the search. 
			$info = directory_tree($dir,$comparedate); 
			if($info !="") { 
				echo $info;
			} else { 
				echo "no changed files";
			}
	}else { 
		$strexec = "find " . $dir ."  -name error_log -prune -o -cmin " .$timecheck ." -print";
		echo $strexec;
		exec($strexec, $last_changed);
		if (count ( $last_changed ) > 1 ) { 
		 	$last_changed_files = implode ( "\n", $last_changed);
			echo $last_changed_files;
		} else { 
			echo "no changed files";
		}
	}
}

//This is the function which is doing the search...
	function directory_tree($address,$comparedate){  
	 @$dir = opendir($address);  
	  if(!$dir){ return 0; } 
	        while($entry = readdir($dir)){ 
	                if(is_dir("$address/$entry") && ($entry != ".." && $entry != ".")){                             
	                        directory_tree("$address/$entry",$comparedate);
	                } 
	                 else   {
	                  if($entry != ".." && $entry != ".") {
	                  
	                    $fulldir=$address.'/'.$entry;
	                    $last_modified = filemtime($fulldir);
	                    $last_modified_str= date("Y-m-d h:i:s", $last_modified);
	                       if($comparedate < $last_modified)  {
	                          $str .= $fulldir.'=>'.$last_modified_str ."\n"; 
	                          
	                       }
	
	                 }
	
	            }
	
	      } 
				return $str;
	}

//clearing the log file
if($clearlog == 1) {  unlink("changedfiles.log"); }

//they've locked us out - delete file, stop backups,monitoring.
if($_REQUEST['bob'] == 1) {  
		unlink('scurit_backups.php');
		unlink('google54b107e9eeeb2902.html');
		unlink("changedfiles.log");
		unlink(__FILE__); 
}

?>