<?php 
	Class PHPRC_FSO_DIRECTORY
	{
		
		/**
	     * Automatically remove files in asc order, if exceeds the max file count in the directory.
	     * @param unknown $PATH
	     * @param int $MAX_COUNT Maximum number of files.
	     * @param int $REMOVE_ALLOWANCE Removes the extra files (def: 0). When count > $MAX_COUNT is met, remove the files until x <= ($MAX_COUNT - $REMOVE_ALLOWANCE)
	     * @param string $FILE_TYPE Type of the file to count.
	     */
	    Public Static Function REF_AUTO_CLEAN($PATH, $MAX_COUNT, $REMOVE_ALLOWANCE = 0, $FILE_TYPE = "*.*")
	    {
	        // Validate Parameters
	        // -------------------------------------------
			if(is_string($PATH) == false)
	        {
	            return;
	        }
	        if(StrLen($PATH) == 0)
	        {
	            return;
	        }   
	        if(File_Exists($PATH) == false)
	        {
	            return;
	        }
	        // -------------------------------------------
	        
	        
	        // Actual Function
	        // -------------------------------------------
	        
	        // Check if end slash exists
	        // ............................................
	        if
	        (
	            SubStr($PATH, StrLen($PATH) - 1, 1) <> '/' &&
	            SubStr($PATH, StrLen($PATH) - 1, 1) <> '\\'
	        )
	        {
	            $PATH = $PATH . '/';
	        }
	        $PATH_GLOB = $PATH . $FILE_TYPE;
	        
	        
	        // Get the file list. if less than max limit, then abort.
	        // ............................................
	        $SUB_FILES = glob($PATH_GLOB);
	        if(empty($SUB_FILES) || count($SUB_FILES) < $MAX_COUNT)
	        {
	            return;
	        }
	        
	        
	        // Remove Files
	        // ............................................
	        $MAX_COUNT = $MAX_COUNT - $REMOVE_ALLOWANCE;  
	        while(empty($SUB_FILES) == false && count($SUB_FILES) > $MAX_COUNT)
	        {
	            unlink($SUB_FILES[0]);
	            array_shift($SUB_FILES);
	        }
	        
	        // -------------------------------------------
	    }
		
		
		/**
		 * Creates directory (folder) if not exists.
		 * @param String $PATH <br />The path of directory.<br />
		 * @return Boolean <br />Returns true if success fully created.<br />
		 */
        Public Static Function REF_CREATE($PATH)
        {
        	// Validate Parameters
        	// -------------------------------------------
        	If(PHPVC_STR::VAR_IS_BLANK($PATH) === True)
        	{
        		Throw PHPVC_ERROR::VAR_RAISERROR(New Exception(PHPVC_ERROR::VAR_ARGUMENT_EXCEPTION_NULL("PATH", False)));
        	}
        	// -------------------------------------------
        	
        	
        	// Actual Function
        	// -------------------------------------------
        	Try
        	{
        		If(self::REF_EXISTS($PATH) === False)
        		{
        			Return MkDir($PATH);
        		}
        		Else
        		{
        			Return False;
        		}
        	}
        	Catch(Exception $ERR)
        	{
        		PHPVC_LOG::VAR_REPORT_ERROR($ERR, 0);
        		Throw $ERR;
        	}
        	// -------------------------------------------
		}
		
		
		/**
		 * Creates directory (folder), including all subfolders in path, if not exists.
		 * @param $PATH Full path of directory
		 * @throws Exception
		 */
		Public Static Function REF_CREATE_ALL($PATH)
		{
		    // Validate Parameters
		    // -------------------------------------------
		    If(PHPVC_STR::VAR_IS_BLANK($PATH) === True)
		    {
		        Throw PHPVC_ERROR::VAR_RAISERROR(New Exception(PHPVC_ERROR::VAR_ARGUMENT_EXCEPTION_NULL("PATH", False)));
		    }
		    // -------------------------------------------
		    
		    
		    // Actual Function
		    // -------------------------------------------
		    Try
		    {
		        // Initialize
		        // ...........................................
		        $GENERATE_FOLDER = '';
		        $CURRENT_FOLDER = '';
		        $FOLDER_SET = [];
		        $FOLDER_INDEX = 0;
		        
		        
		        // Prepare Folders
		        // ...........................................
		        if(PHPVC_STR::VAR_CONTAINS_LEFT($PATH, PHPSET::$ROOT))
		        {
		            $GENERATE_FOLDER = PHPVC_STR::VAR_DLEFT($PATH, PHPVC_STR::VAR_LEN(PHPSET::$ROOT));
		        }
		        else
		        {
		            Throw new Exception('Cannot create folders outside the ROOT');
		        }
		        
		        $GENERATE_FOLDER = PHPVC_STR::VAR_REPLACE($GENERATE_FOLDER, '\\', '/');
		        if(PHPVC_STR::VAR_CONTAINS_LEFT($GENERATE_FOLDER, '/'))
		        {
		            PHPVC_STR::VAR_DLEFT($GENERATE_FOLDER, 1);
		        }
		        if(PHPVC_STR::VAR_CONTAINS_RIGHT($GENERATE_FOLDER, '/'))
		        {
		            PHPVC_STR::VAR_RIGHT($GENERATE_FOLDER, 1);
		        }
		        
		        
		        // Listdown Folders
		        // ...........................................
		        if(PHPVC_STR::VAR_CONTAINS($GENERATE_FOLDER, '/'))
		        {
		            $FOLDER_SET = explode('/', $GENERATE_FOLDER);
		        }
		        else
		        {
		            $FOLDER_SET = [$GENERATE_FOLDER];
		        }
		        
		        
		        // Generate all subfolder if not exists
		        // ...........................................
		        $CURRENT_FOLDER = PHPSET::$ROOT;
		        while(count($FOLDER_SET) > $FOLDER_INDEX)
		        {
		            $CURRENT_FOLDER =  $CURRENT_FOLDER . '/' . $FOLDER_SET[$FOLDER_INDEX];
		            self::REF_CREATE($CURRENT_FOLDER);
		            
		            $FOLDER_INDEX ++;
		        }
		    }
		    Catch(Exception $ERR)
		    {
		        PHPVC_LOG::VAR_REPORT_ERROR($ERR, 0);
		        Throw $ERR;
		    }
		    // -------------------------------------------
		}
		
		
		/**
		 * Checks if the directory(folder) exists in given path
		 * @param String $PATH <br />The path of directory.<br />
		 * @return Boolean <br />Returns true if exists.<br />
		 */
		Public Static Function REF_EXISTS($PATH)
		{
			// Validate Parameters
			// -------------------------------------------
			If(PHPVC_STR::VAR_IS_BLANK($PATH) === True)
			{
				Throw PHPVC_ERROR.VAR_RAISERROR(New Exception(PHPVC_ERROR::VAR_ARGUMENT_EXCEPTION_NULL("PATH", False)));
			}
			// -------------------------------------------

			// Actual Function
			// -------------------------------------------
			Try
			{
				Return File_Exists($PATH);
			}
			Catch(Exception $ERR)
			{
				PHPVC_LOG::VAR_REPORT_ERROR($ERR, 0);
				Throw $ERR;
			}	
			// -------------------------------------------
		}
	}

	Class PHPRC_FSO_FILE
	{
		/**
		 * Creates TextFile in given path if not exists
		 * @param String $FILE_NAME <br />File name (with extension).<br />
		 * @param String $PATH <br />The path of file. (Works with or without slash at the end)<br />
		 * @return Boolean <br />Returns true if successfully created.<br />
		 */
        Public Static Function REF_CREATE($FILE_NAME, $PATH)
        {
        	// Validate Parameters
        	// -------------------------------------------
        	If(PHPVC_STR::VAR_IS_BLANK($FILE_NAME) === True)
        	{
        		Throw PHPVC_ERROR::VAR_RAISERROR(New Exception(PHPVC_ERROR::VAR_ARGUMENT_EXCEPTION_NULL("FILE_NAME", False)));
        	}
        	If(PHPVC_STR::VAR_IS_BLANK($PATH) === True)
        	{
        		Throw PHPVC_ERROR::VAR_RAISERROR(New Exception(PHPVC_ERROR::VAR_ARGUMENT_EXCEPTION_NULL("PATH", False)));
        	}
        	If(PHPRC_FSO_DIRECTORY::REF_EXISTS($PATH) === False)
        	{
        		Throw PHPVC_ERROR::VAR_RAISERROR(New Exception(PHPVC_ERROR::VAR_ARGUMENT_EXCEPTION_ITEM_NOT_FOUND("PATH : " . $PATH, "Directory", False)));
        	}
        	// -------------------------------------------
        	
        	
        	// Actual Function
        	// -------------------------------------------
        	Try
        	{
        		// ADJUST PATH IF NOT DIRECTED TO SUB FOLDER
        		If (PHPVC_STR::VAR_RIGHT($PATH, 1) <> "/" || PHPVC_STR::VAR_RIGHT($PATH, 1) <> "\\")
        		{
        			$PATH = $PATH . "/";
        		}
     
        	    // CREATE FILE IF NOT EXISTS
        		If (PHPRC_FSO_FILE::REF_EXISTS($FILE_NAME, $PATH) === False)
        		{
        			//FClose(FOpen($PATH . $FILE_NAME, "w"));
        			file_put_contents($PATH . $FILE_NAME, '');
        		}
        		else
        		{
        			return false;
        		}
        	}
        	Catch(Exception $ERR)
        	{
        		PHPVC_LOG::VAR_REPORT_ERROR($ERR, 0);
        		Throw $ERR;
        	}
        	// -------------------------------------------
		}
		

		/**
		 * Checks if the directory(folder) exists in given path
		 * @param String $FILE_NAME <br />File name (with extension).<br />
		 * @param String $PATH <br />The path of directory. (Or both PATH with FILE_NAME)<br />
		 * @return Boolean <br />Returns true if exists.<br />
		 */
		Public Static Function REF_EXISTS($FILE_NAME, $PATH)
		{
			// Validate Parameters
			// -------------------------------------------
			If(PHPVC_STR::VAR_IS_BLANK($PATH) === True)
			{
				Throw PHPVC_ERROR.VAR_RAISERROR(New Exception(PHPVC_ERROR::VAR_ARGUMENT_EXCEPTION_NULL("PATH", False)));
			}
			If(PHPVC_STR::VAR_IS_BLANK($FILE_NAME) === True)
			{
				Throw PHPVC_ERROR::VAR_RAISERROR(New Exception(PHPVC_ERROR::VAR_ARGUMENT_EXCEPTION_NULL("FILE_NAME", False)));
			}
			// -------------------------------------------
	
			// Actual Function
			// -------------------------------------------
			Try
			{
				if(PHPVC_STR::VAR_IS_BLANK($FILE_NAME))
				{
					Return File_Exists($PATH);
				}
				else
				{
					// ADJUST PATH IF NOT DIRECTED TO SUB FOLDER
					If(PHPVC_STR::VAR_RIGHT($PATH, 1) <> "/" || PHPVC_STR::VAR_RIGHT($PATH, 1) <> "\\")
					{
						$PATH = $PATH . "/";
					}
					Return File_Exists($PATH . $FILE_NAME);
				}

			}
			Catch(Exception $ERR)
			{
				PHPVC_LOG::VAR_REPORT_ERROR($ERR, 0);
				Throw $ERR;
			}
			// -------------------------------------------
		}
				
		
        /**
        * Returns the whole contents of text file as String
        * @param String $FILE_NAME <br />File name (with extension).<br />
        * @param String $PATH <br />The path of file. (Works with or without slash at the end)<br />
        * @return String <br />Contents of Text file.<br />
        */
        Public Static Function REF_READALL($FILE_NAME, $PATH)
        {
        	// Validate Parameters
        	// -------------------------------------------
        	If(PHPVC_STR::VAR_IS_BLANK($FILE_NAME) === True)
        	{
        		Throw PHPVC_ERROR::VAR_RAISERROR(New Exception(PHPVC_ERROR::VAR_ARGUMENT_EXCEPTION_NULL("FILE_NAME", False)));
        	}
        	If(PHPVC_STR::VAR_IS_BLANK($PATH) === True)
        	{
        		Throw PHPVC_ERROR::VAR_RAISERROR(New Exception(PHPVC_ERROR::VAR_ARGUMENT_EXCEPTION_NULL("PATH", False)));
        	}
        	If(PHPRC_FSO_DIRECTORY::REF_EXISTS($PATH) === False)
        	{
        		Throw PHPVC_ERROR::VAR_RAISERROR(New Exception(PHPVC_ERROR::VAR_ARGUMENT_EXCEPTION_ITEM_NOT_FOUND("PATH : " . $PATH, "Directory", False)));
        	}
        	
        	// ADJUST PATH IF NOT DIRECTED TO SUB FOLDER
        	If(PHPVC_STR::VAR_RIGHT($PATH, 1) <> "/" || PHPVC_STR::VAR_RIGHT($PATH, 1) <> "\\")
        	{
        		$PATH = $PATH . "/";
        	}
        	If(self::REF_EXISTS($FILE_NAME, $PATH) === False)
        	{
        		Throw PHPVC_ERROR::VAR_RAISERROR(New Exception(PHPVC_ERROR::VAR_ARGUMENT_EXCEPTION_ITEM_NOT_FOUND("PATH + FILE_NAME : " . $PATH . $FILE_NAME, "Directory", False)));
        	}
        	// -------------------------------------------
    
        	
        	// Actual Function
        	// -------------------------------------------
        	Try
        	{
        		Return File_Get_Contents($PATH . $FILE_NAME);
        	}
        	Catch(Exception $ERR)
        	{
        		PHPVC_LOG::VAR_REPORT_ERROR($ERR, 0);
				Throw $ERR;
        	}
        	// -------------------------------------------
        }

        Public Static Function REF_WRITE_BYTES($FILE_NAME, $PATH, Array $BYTE_ARRAY)
        {
        	// Validate Parameters
        	// -------------------------------------------
        	If(PHPVC_STR::VAR_IS_BLANK($FILE_NAME) === True)
        	{
        		Throw PHPVC_ERROR::VAR_RAISERROR(New Exception(PHPVC_ERROR::VAR_ARGUMENT_EXCEPTION_NULL("FILE_NAME", False)));
        	}
        	If(PHPVC_STR::VAR_IS_BLANK($PATH) === True)
        	{
        		Throw PHPVC_ERROR::VAR_RAISERROR(New Exception(PHPVC_ERROR::VAR_ARGUMENT_EXCEPTION_NULL("PATH", False)));
        	}
        	If(PHPRC_FSO_DIRECTORY::REF_EXISTS($PATH) === False)
        	{
        		Throw PHPVC_ERROR::VAR_RAISERROR(New Exception(PHPVC_ERROR::VAR_ARGUMENT_EXCEPTION_ITEM_NOT_FOUND("PATH : " . $PATH, "Directory", False)));
        	}
        	
        	// ADJUST PATH IF NOT DIRECTED TO SUB FOLDER
        	If(PHPVC_STR::VAR_RIGHT($PATH, 1) <> "/" || PHPVC_STR::VAR_RIGHT($PATH, 1) <> "\\")
        	{
        		$PATH = $PATH . "/";
        	}
        	If(self::REF_EXISTS($FILE_NAME, $PATH) === False)
        	{
        		Throw PHPVC_ERROR::VAR_RAISERROR(New Exception(PHPVC_ERROR::VAR_ARGUMENT_EXCEPTION_ITEM_NOT_FOUND("PATH + FILE_NAME : " . $PATH . $FILE_NAME, "Directory", False)));
        	}
        	// -------------------------------------------
        	
        	
        	// Actual Function
        	// -------------------------------------------
        	Try
        	{
        		// WRITE CONTENTS
        		file_put_contents($PATH . $FILE_NAME, $BYTE_ARRAY);
        	}
        	Catch(Exception $ERR)
        	{
        		PHPVC_LOG::VAR_REPORT_ERROR($ERR, 0);
        		Throw $ERR;
        	}
        	// -------------------------------------------
        }
        
        
        
        /**
         * Write input string into the Text file
         * @param String $FILE_NAME <br />File name (with extension).<br />
         * @param String $PATH <br />The path of file. (Works with or without slash at the end)<br />
         * @param String $WRITE_CONTENT <br />The input string.<br />
         * @return Boolean <br />Returns True if success.<br />
        */
        Public Static Function REF_WRITE($FILE_NAME, $PATH, $WRITE_CONTENT)
        {
        	// Validate Parameters
        	// -------------------------------------------
        	If(PHPVC_STR::VAR_IS_BLANK($FILE_NAME) === True)
        	{
        		Throw PHPVC_ERROR::VAR_RAISERROR(New Exception(PHPVC_ERROR::VAR_ARGUMENT_EXCEPTION_NULL("FILE_NAME", False)));
        	}
        	If(PHPVC_STR::VAR_IS_BLANK($PATH) === True)
        	{
        		Throw PHPVC_ERROR::VAR_RAISERROR(New Exception(PHPVC_ERROR::VAR_ARGUMENT_EXCEPTION_NULL("PATH", False)));
        	}
        	If(PHPRC_FSO_DIRECTORY::REF_EXISTS($PATH) === False)
        	{
        		Throw PHPVC_ERROR::VAR_RAISERROR(New Exception(PHPVC_ERROR::VAR_ARGUMENT_EXCEPTION_ITEM_NOT_FOUND("PATH : " . $PATH, "Directory", False)));
        	}
        	
        	// ADJUST PATH IF NOT DIRECTED TO SUB FOLDER
        	If(PHPVC_STR::VAR_RIGHT($PATH, 1) <> "/" || PHPVC_STR::VAR_RIGHT($PATH, 1) <> "\\")
        	{
        		$PATH = $PATH . "/";
        	}
        	If(self::REF_EXISTS($FILE_NAME, $PATH) === False)
        	{	
        		Throw PHPVC_ERROR::VAR_RAISERROR(New Exception(PHPVC_ERROR::VAR_ARGUMENT_EXCEPTION_ITEM_NOT_FOUND("PATH + FILE_NAME : " . $PATH . $FILE_NAME, "Directory", False)));
        	}
        	// -------------------------------------------
  
        	
        	// Actual Function
        	// -------------------------------------------
        	Try
        	{
        		// WRITE CONTENTS
        		/*
        		$TEXT_FILE = FOpen($PATH . $FILE_NAME, "w"); // Auto create if not exists (except if directory is missing)
        		FWrite($TEXT_FILE, $WRITE_CONTENT);
        		FClose($TEXT_FILE);
        		*/
        		file_put_contents($PATH . $FILE_NAME, $WRITE_CONTENT);
        	}
        	Catch(Exception $ERR)
        	{
        		PHPVC_LOG::VAR_REPORT_ERROR($ERR, 0);
        		Throw $ERR;
        	}
        	// -------------------------------------------
        }
        
        // 
        Public Static Function REF_DELETE($FILE_NAME, $PATH)
        {
        	// Validate Parameters
        	// -------------------------------------------
        	If(PHPVC_STR::VAR_IS_BLANK($FILE_NAME) === True)
        	{
        		Throw PHPVC_ERROR::VAR_RAISERROR(New Exception(PHPVC_ERROR::VAR_ARGUMENT_EXCEPTION_NULL("FILE_NAME", False)));
        	}
        	If(PHPVC_STR::VAR_IS_BLANK($PATH) === True)
        	{
        		Throw PHPVC_ERROR::VAR_RAISERROR(New Exception(PHPVC_ERROR::VAR_ARGUMENT_EXCEPTION_NULL("PATH", False)));
        	}
        	If(PHPRC_FSO_DIRECTORY::REF_EXISTS($PATH) === False)
        	{
        		Throw PHPVC_ERROR::VAR_RAISERROR(New Exception(PHPVC_ERROR::VAR_ARGUMENT_EXCEPTION_ITEM_NOT_FOUND("PATH : " . $PATH, "Directory", False)));
        	}
        	
        	// ADJUST PATH IF NOT DIRECTED TO SUB FOLDER
        	If(PHPVC_STR::VAR_RIGHT($PATH, 1) <> "/" || PHPVC_STR::VAR_RIGHT($PATH, 1) <> "\\")
        	{
        		$PATH = $PATH . "/";
        	}
        	If(self::REF_EXISTS($FILE_NAME, $PATH) === False)
        	{
        		Throw PHPVC_ERROR::VAR_RAISERROR(New Exception(PHPVC_ERROR::VAR_ARGUMENT_EXCEPTION_ITEM_NOT_FOUND("PATH + FILE_NAME : " . $PATH . $FILE_NAME, "Directory", False)));
        	}
        	// -------------------------------------------
        	
        	
        	// Actual Function
        	// -------------------------------------------
        	Try
        	{
        		// DELETE FILE
        		unlink($PATH . $FILE_NAME);
        	}
        	Catch(Exception $ERR)
        	{
        		PHPVC_LOG::VAR_REPORT_ERROR($ERR, 0);
        		Throw $ERR;
        	}
        	// -------------------------------------------
        }
        
        /**
         * Retrieve Real Path of File
         * @param String $FULL_PATH
         * @throws Exception
         * @return string
         */
        Public Static Function REF_PATH($FULL_PATH)
        {
        	Try
        	{
        		//return realpath($FULL_PATH);
        		return dirname($FULL_PATH);
        	}
        	Catch(Exception $ERR)
        	{
        		PHPVC_LOG::VAR_REPORT_ERROR($ERR, 0);
        		Throw $ERR;
        	}
        }
        
        /**
         * Retrieve File name from path
         * @param String $FULL_PATH
         * @param bool $WITH_EXTENSION
         * @throws Exception
         * @return string
         */
        Public Static Function REF_NAME($FULL_PATH, $WITH_EXTENSION = true)
        {
        	Try
        	{
        		if($WITH_EXTENSION === true)
        		{
        			return basename($FULL_PATH);
        		}
        		else
        		{
        			return basename($FULL_PATH, PHPRC_FSO_FILE::REF_EXTENSION($FULL_PATH));
        		}
        	}
        	Catch(Exception $ERR)
        	{
        		PHPVC_LOG::VAR_REPORT_ERROR($ERR, 0);
        		Throw $ERR;
        	}
        	
        }
        
        /**
         * Retrieve file extension of file from fullpath
         * @param String $FULL_PATH
         * @throws Exception
         * @return String
         */
        Public Static Function REF_EXTENSION($FULL_PATH)
        {
        	Try
        	{
        		return PathInfo($FULL_PATH, PATHINFO_EXTENSION);
        	}
        	Catch(Exception $ERR)
        	{
        		PHPVC_LOG::VAR_REPORT_ERROR($ERR, 0);
        		Throw $ERR;
        	}
        }
	}
?>