<?php
	defined( "_VALID_PAGE_MONITOR" ) or die (_NODIRECTACCESS);
	
	class FolderList {
		/* Variables */
		var $folderstack;
			// Folderstack.
		var $starterfolder;
			// Starting folder.
	
		/* Constructor */
		function FolderList($starter_folder){
			$this->starterfolder = $starter_folder;
			$this->folderstack = array();
		}
		
		/*
		 *	getListOfDates()
		 *	Get a complete list of dates, which are is in fact a listing of the first-level
		 *	folder inside the "ftp_folder" specified in the config variable (pm_config["ftp_folder"]).
		 */
		public function getListOfDates(){
			$relative_folder = $this->starterfolder;
			
			$listOfDates = array();
			
			if( $handle = opendir($relative_folder) ){
				while (false !== ($file = readdir($handle))) {
					if( $file != "." && $file != ".." && $file != ".DS_Store" ){
						$dateAsString = $file;
						if( ereg("([0-9]{2})_([0-9]{2})_([0-9]{2})+", $file, $regs) ){
							$y = $regs[1];
							$m = $regs[2];
							$d = $regs[3];
							if( strlen($y) < 4 )
								$y = '20' . $y;
							$dateAsString = $y . "." . $m . "." . $d;
						}
						$listOfDates[] = array( "filename" => $file, "string" => $dateAsString );
					}
				}
			} // if $handle
			
			return $listOfDates;
		}
		
		/*
		 *	getListOfProducts($date_folder)
		 *	Get a list of product for a specified date, which is a folder name in the first-level
		 *	folder of ftp_folder configuration variable.
		 *	@param $date_folder	- a string, containing a name of the "date" folder in the
		 *		"ftp_folder" hierarchy.
		 */
		public function getListOfProducts($date_folder){
			$relative_folder = $this->starterfolder . DIRECTORY_SEPARATOR . $date_folder;
			$listOfProducts = array();

			if ($handle = opendir($relative_folder)) {
				while (false !== ($file = readdir($handle))) {
					if( $file != "." && $file != ".." && $file != ".DS_Store" )
						$listOfProducts[] = $file;
				}
			}// $handle
			return $listOfProducts;
		}
		
		/*
		 *	getListOfPages($date_folder, $product)
		 *	Get a list of pages in regard to date and product. Both $date_folder and $product are
		 *	folder names in the "ftp_folder" folder hierarchy.
		 */
		public function getListOfPages($date_folder, $product){
			$relative_folder = $this->starterfolder . DIRECTORY_SEPARATOR . $date_folder . DIRECTORY_SEPARATOR . $product;
			$listOfPages = array();
			if ($handle = opendir($relative_folder)) {
				while (false !== ($file = readdir($handle))) {
					if( $file != "." && $file != ".." && $file != ".DS_Store" )
						$listOfPages[] = $file;
				}
			}// $handle
			return $listOfPages;
		}
	} // class
?>
