<?php

/*
The following program finds all files that were modified within a week (you 
can change this period) in a given directory (you can change the directory) 
and its subdirectories. Files and directories with no public read permissions
will not be processed. Also, files whose extension begins with a period 
such as ".htaccess" and ".htpasswd" are not processed.

Files are not sorted with respect to the modification date.

This version is better than the previous ones and easier to understand.

Dealing with directories is similar to dealing with file. You have to open them, 
process their contents with a loop and using a read function, and close them. 
*/


// The URL of the main website
$main_url = "http://www.cs.fredonia.edu/abu-jeib/";
  
// Dir. and subdirectories you want to process for new files
$dir_to_process_path = "/home/abu-jeib/www/teaching/Fall_06/207/";

// The path to the main web directory
$main_web_dir_path = "/home/abu-jeib/www/";

// List in this array the directory or the directories you don't
// want to process.
$do_not_process = array("/home/abu-jeib/www/teaching/Fall_06/207/exams");

// This is the period you want its files. In this case
// it's one week. It's in seconds. If you want more or less
// days (than a week) change the number 7 below to the 
// number you want.
$period = 60 * 60 * 24 * 7;

echo "<html><head><title>What's new?</title></head><body>\n";
echo "<h1 align=\"center\">What's new in one week?</h1>\n";
echo "<ol>\n";

function process_dir($dir_path)
{
  global $dir_to_process_path;
  global $do_not_process;
  global $sorted;
  global $period;
  global $main_web_dir_path;
  global $main_url;
  $current_date = time();

  // The main URL for your website
  @$dir = opendir($dir_path);
  while (@$file = readdir($dir))
  {
	$file_path = $dir_path.$file;
	// Avoid files such as .htaccess and .htpasswd and avoid going 
	// backward to directories behind the one you want to process.
	if ($file[0] != ".")
	{
		// Check if you reached a directory.
		if (is_dir($file_path))
		{
                        if (!in_array($file_path,$do_not_process))
                                process_dir($file_path."/");
		}
		else
		{
			// Get the last modification date of the file
			@$file_date = filemtime($file_path);
			$date_diff = time() - $file_date;
			$wanted = FALSE;
			if ($date_diff <= $period)
			{
				$wanted = TRUE;
			}
			// Check if the file modification date is within the period you want
			// and the file is readable and it is not a link.
			if ($wanted  && is_readable($file_path) && !is_link($file_path))
			{
				// Get the path starting from the main Web directory.
				// The path will be the 2nd element of $ar.
				$ar = split("$main_web_dir_path",$file_path,2);
				$new_path = $ar[1];

				// Get the path starting from the directory you want
				// to process. The path will be the 2nd element of 
				// $path_to_write.
				$path_to_write=split("$dir_to_process_path",$file_path,2);				

				// Get the URL of the file.
				$file_url = $main_url.$new_path;
				
                                echo "<li><a href='$file_url'>$path_to_write[1]<a> 
				(<font color='red'>Last Modified:</font>",
                                date('M d, y H:i:s',filemtime($file_path)),")<BR>\n";

			} 

		}
        }
  }
  @closedir($dir);
}

process_dir($dir_to_process_path);


echo "</ol>\n";
echo "</body></html>";

?>

