All pastes #674680 Raw Edit

Something

public text v1 · immutable
#674680 ·published 2007-08-29 13:29 UTC
rendered paste body
<?

class searchFilesByPatterns 
{	
	var $insertMode = 1;
	var $arrMatchedFiles = array();
	var $arrErrFilesAndDirectory = array();
	var $arrPatern = array();	
	var $strDirectory;
	var $strCurrentDirectory;
	var $arrFileExt = array();
	var $boolTraverse = 1;
	var $strPathDivider  ;
	function searchFilesByPatterns($strDirectory, $arrPatern=array('simpletest','Log4Php','W3C'), $arrFileExt = array('php','inc','class'), $insertMode = 1, $boolTraverse = 1)
	{
		/*
		*	@Description:	Class constructor, Initializes basic requirements.
		*
		*	@Method:		searchFilesByPatterns
		*	@Parameters:	4
		*	@Param-1:		strDirectory 				- String 	- The directory to be searched
		*	@Param-2:		arrPatern 					- Array 	- An array list of file Patterns (Strings to be compared with the content of the file)
		*	@Param-3:		arrFileExt 					- Array 	- An arrray list of file Extension (File extention of the files to be checked)
		*	@Param-4:		insertMode 					- Boolean 	- 1(default) = inserts fullpath of the file which does not contain the pattern; 0 = inserts fullpath of the file which contain the pattern;
		* 	@Param-5:		boolTraverse 				- Boolean 	- 1(default) = crawls deeper directories; 0 = only searches selected directories without going deeper
		*	@return			arrMatchedFiles				- Array		- Successfully inserted full path of the files
		*					arrErrFilesAndDirectory 	- Array		- Unuccessfully inserted full path of the files and directories
		*/	
		
		$this->insertMode=$insertMode;
		$this->strDirectory = $strDirectory;
		$this->arrFileExt = $arrFileExt;
		$this->boolTraverse = $boolTraverse;
		$this->arrPatern = $arrPatern;
		$this->crawlDirectory($this->strDirectory);		
		
		
	}
	
	function findMatches($file)
	{
		
		/*
		*	@Description:	Compares the content of the file through the patterns.
		*
		*	@Method:		findMatches
		*	@Parameters:	1
		*	@Param-1:		file	- String 	- The full path of the file to be checked
		*	@return					- Boolean	- If Successfully read or not
		*					
		*/	
		
		if($fp = @fopen($file, "r"))
		{
			$data = fread($fp, filesize($file));		
			fclose($fp); 
		
			$pattern="'" . implode("|",$this->arrPatern) . "'";
			if(preg_match($pattern,$data))
			{
				if($this->insertMode)
					return false;
				else
					return true;
			}
			else
			{
				if($this->insertMode)
					return true;
				else
					return false;
			}
		}
		else
		{
			$this->arrErrFilesAndDirectory[] = $file;
			return false;
		}	
	}
	
	function crawlDirectory($strDirectory)
	{
	
		/*
		*	@Description:	Browse the file content of a directory path
		*
		*	@Method:		crawlDirectory
		*	@Parameters:	1
		*	@Param-1:		strDirectory - String - The directory path to be searched
		*	@retuns:		Nothing
		*
		*/
		
		
		//checks if the arrBlackListedPattern and arrFileType is supplied with proper array values
		if(is_array($this->arrPatern) && is_array($this->arrFileExt))
		{
			//this opens the directory up
			if($dirHandle = opendir($strDirectory))
			{	
				$this->strPathDivider = substr(PHP_OS, 0, 3) == "WIN" ? "\\" : "/";
				//browse the files in the directory 
				while (false !== ($strFile = readdir($dirHandle)))
				{ 
					if($strFile <> '.' AND $strFile <> '..')
					{			 
						$strFullFilePath = $strDirectory.$this->strPathDivider.$strFile;
						 
						if($this->boolTraverse AND is_dir($strFullFilePath))
						{						
							$this->crawlDirectory($strFullFilePath);
						}
						else
						{					 
							$arrFileParts = explode(".", $strFile);
							$arrFileParts = array_reverse($arrFileParts);
							$strFileExt = $arrFileParts[0];
							
							if(in_array($strFileExt, $this->arrFileExt))
							{
								if($this->findMatches($strFullFilePath))
								{
									$this->arrMatchedFiles[] = $strFullFilePath;
								}
							}						
			
						}					
					}	       
				}
				closedir($dirHandle); 
			}
			else
			{
				$this->arrErrFilesAndDirectory[] = $strDirectory;
			}
		}
		else
		{
			//display should supply proper array values to the parameter $arrPatern and $arrFileExt 
		}
	}
	
	
	
}


?>