PHP Classes

File: clsDirectoryBrowser

Recommend this page to a friend!
  Classes of chris  >  DirectoryBrowser  >  clsDirectoryBrowser  >  Download  
File: clsDirectoryBrowser
Role: ???
Content type: text/plain
Description: You'll need to get DataProducer class too for it to work
Class: DirectoryBrowser
A class + template system for exploring a file sys
Author: By
Last change:
Date: 20 years ago
Size: 9,086 bytes
 

Contents

Class file image Download
<?
include_once("clsDataProducer.php");
/*
************************************************************************
*  Sloppycode.net All rights reserved.
*
* This is a standard copyright header for all source code appearing
* at sloppycode.net. This application/class/script may be redistributed,
* as long as the above copyright remains intact. 
* Comments/Suggestions to sloppycode@sloppycode.net
************************************************************************
*/


/**
 * Simple structure for holding file/directory details
 */
class FileDataStore
{
	var $name;
	var $link;
	var $type;
}

/*
 * DirectoryBrowser - directory browsing class to display files and directories.
 * Requires the DataProducer class for its templates.
 * @author C.Small <chris@sloppycode.net>
 * @version 1.1 PEAR style comments addded
 * @version 1.0
 * @public
 */
 
class DirectoryBrowser
{
	/*
	 * How the files/directories are display. Either 'byname' or 'bytype'. Default is byname
	 * @public
	 * @see DirectoryBrowser#_sortFiles()
	 */
	var $sorttype = "byname";
	/*
	 * The home domain and directory that DirectoryBrowser is running in. Include http://
	 * @access public
	 */
	var $rootdomain;
	/*
	 * The icon/graphic for a directory. In the example it is directory.gif
	 * @access public
	 */
	var $image_dir;
	/*
	 * The icon/graphic for a file. In the example it is file.gif
	 * @access public
	 */
	var $image_file;
	/*
	 * Current directory that is being browsed
	 * @access public
	 */
	var $currentdir = "";
	/*
	 * The querystring variable name for the current directory
	 * @access public
	 */
	var $querystring_dirname = "dir";
	/*
	 * The querystring variable name for the current sort type
	 * @access public
	 */
	var $querystring_sorttypename = "sort";
	
	/*
	 * Files and subdirectories in the current directory
	 * @access private
	 */
	var $files = array();
	/*
	 * The html template file
	 * @access private
	 */
	var $contents;
	/*
	 * Name of the script, including preceeding path if any
	 * @access public

	 */
	 var $scriptname;
    /*
    * Displays the contents of the templatefile, with all tags replaced with data. Tags are:
    * &lt;sortoption_link/&gt; - Displays a link for the sorting option (by name or type)
    * &lt;rootdomain/&gt; - The rootdomain name.
    * &lt;parent_link/&gt; - Dispalys a link to the parent folder.
    * &lt;parent_dirname/&gt; - Displays the parent directory name.
    * &lt;files&gt; - The files to display
    *  &nbsp;&nbsp;&lt;file_image/&gt; - The image for the file, either a directory or file icon.
    *  &nbsp;&nbsp;&lt;file_link/&gt; - A link to the file, or directory.
    * &lt;/files&gt;
    * &lt;self_dirname/&gt; - The directory name of the current directory.
    * @access public
    * @param string $name description
    * @return void
    */
	function display($templatefile)
	{
		global $HTTP_GET_VARS;
		
		if (!empty($HTTP_GET_VARS[$this->querystring_dirname]))
		{
			$this->currentdir = $HTTP_GET_VARS[$this->querystring_dirname];
		}
		if (!empty($HTTP_GET_VARS[$this->querystring_sorttypename]))
		{
			$this->sorttype = $HTTP_GET_VARS[$this->querystring_sorttypename];
		}

		if (empty($templatefile))
		{
			echo "DirectoryBrowser class error: no templatefile specified";
			return;
		}
		$this->_checkCurrentDir();
		$this->_getFiles();
		
		$dp = new DataProducer();
		$contents = $dp->openTemplate($templatefile);
		
		/* ---- Single tags ---- */
		
		$tags['rootdomain'] = $this->rootdomain;
		$tags['parent_link'] = "?".$this->querystring_dirname."=".$this->_getParentFolder($this->currentdir)."&".$this->querystring_sorttypename."=".$this->sorttype;
		$tags['parent_dirname'] = $this->_getParentFolder($this->currentdir);
		if ($tags['parent_dirname'] == "")
		{
			$tags['parent_dirname'] = "/";
		}
		
		$tags['self_scriptname'] = $this->scriptname;
		$tags['self_link'] = "?".$this->querystring_dirname."=".$this->currentdir."&".$this->querystring_sorttypename."=".$this->sorttype;
		$tags['self_dirname'] = $this->currentdir;
		if ($tags['self_dirname'] == "")
		{
			$tags['self_dirname'] = "/";
		}
		if ($this->sorttype == "byname")
		{
			$tags['sortoption_link'] = "?".$this->querystring_dirname."=".$this->currentdir."&".$this->querystring_sorttypename."=bydir";
			$tags['sortoption'] = "by directory/file";
		} else{
			$tags['sortoption_link'] = "?".$this->querystring_dirname."=".$this->currentdir."&".$this->querystring_sorttypename."=byname";
			$tags['sortoption'] = "by name";
		}
		
		$contents = $dp->doSingleDataProducer($tags,$contents);
		
		/* ---- Multiple tags ---- */
		$contents = $dp->doDataProducer("files",$this->_sortFiles(),$contents);
		echo $contents;
	}

	/*
	 * Checks whether the current directory is root or not.
	 * @access private
	 * @return void
	 */
	function _checkCurrentDir()
	{
		if (isset($this->currentdir))
		{
			if ($this->currentdir[sizeof($this->currentdir)] == "/")
			{
				$this->basedir .= "/".$this->currentdir;
			} else{
				$this->basedir .= $this->currentdir;
			}
		} else{
			$this->basedir .= "/";
		}
	}

	/*
	 * Retrieves all files and folders for the current directory.
	 * @access private
	 * @return void
	 */
	function _getFiles()
	{
		$PHP_SELF = $this->scriptname;
	
		$fHnd = opendir($this->basedir);
		if ($fHnd)
		{
			while (false !== ($filename = readdir($fHnd)) )
			{
				if ($filename != "." && $filename != "..")
				{
					if (is_dir($this->basedir."/".$filename)) 
					{
						$file = new FileDataStore();
						$file->type = "dir";
						$file->name = $filename;
						
						$goto = $this->currentdir."/".$filename;
						$file->link = $PHP_SELF."?".$this->querystring_dirname."=".$goto."&".$this->querystring_sorttypename."=".$this->sorttype;
						
						$this->files[] = $file;
						unset($file);
					} else{
						$file = new FileDataStore();
						$file->type = "file";
						$file->name = $filename;
						$file->link = $this->rootdomain.$this->currentdir."/".$filename;
						
						$this->files[] = $file;
						unset($file);
					}
				}
			}
			closedir($fHnd);
		}
	}

	/*
	 * Sorts the files and directories according to the $sorttype property
	 * @access private
	 * @return void
	 * @see $sorttype property
	 */
	function _sortFiles()
	{
		if ($this->sorttype == "byname")
		{
			// Sort alphabetically
			$tags = array();
			$rows = array();
		
			if (sizeof($this->files) >0)
			{
				// Make up new array to be sorted
				for ($i=0;$i <= sizeof($this->files) -1;$i++)
				{
					if ($this->files[$i]->type == "dir")
					{
						$img = $this->image_dir;
					} else{
						$img= $this->image_file;
					}
				
					$sortedarr[] = array($this->files[$i]->name,$this->files[$i]->link,$this->files[$i]->type,$img);
				}
				
				sort($sortedarr);
				reset($sortedarr);
				for ($i=0;$i <= sizeof($sortedarr) -1;$i++)
				{
					$tags['file_name'] = $sortedarr[$i][0];
					$tags['file_link'] = $sortedarr[$i][1];
					$tags['file_type'] = $sortedarr[$i][2];
					$tags['file_image'] = $sortedarr[$i][3];
					$rows[] = $tags;
					unset($tags);
				}
			}
		} else{
			// Sort by file type (directory then file)
			
			$tags = array();
			$rows = array();
		
			if (sizeof($this->files) >0)
			{
				// Add directories
				for ($i=0;$i <= sizeof($this->files) -1;$i++)
				{
					if ($this->files[$i]->type == "dir")
					{
						$tags['file_type'] = "dir";
						$tags['file_name'] = $this->files[$i]->name;
						$tags['file_link'] = $this->files[$i]->link;
						$tags['file_image'] = $this->image_dir;
						$rows[] = $tags;
						unset($tags);
					}
				}
				
				// Add files
				
				for ($i=0;$i <= sizeof($this->files) -1;$i++)
				{
					if ($this->files[$i]->type == "file")
					{
						$tags['file_type'] = "file";
						$tags['file_name'] = $this->files[$i]->name;
						$tags['file_link'] = $this->files[$i]->link;
						$tags['file_image'] = $this->image_file;
						$rows[] = $tags;
						unset($tags);
					}
				}
			}
		}
		return $rows;
	}

	/*
	 * Gets the parent folder from provided $folder.
	 * @access private
	 * @param string $folder Child folder whose parent should be retrieved.
	 * @return string The parent folder.
	 */
	function _getParentFolder($folder)
	{
			$temp = explode("/",$folder);
			
			if (sizeof($temp) > 0)
			{
				// first element will be empty as it starts with "/"
				if (empty($temp[0])){
					array_shift($temp);
				}
				// get rid of last element to show arent
				array_pop($temp);
				
				$folder = $temp[0];
				for ($i=1;$i <= sizeof($temp) -1;$i++){
					if (!empty($temp[$i]))
					{
						$folder .= "/".$temp[$i];
					}
				}
				if (strpos($folder,"/") != 1)
				{
					$folder = "/".$folder;
				}
				if ($folder == "/") $folder = "";
			}
			return $folder;
	}
	
}
?>

For more information send a message to info at phpclasses dot org.