Location: PHPKode > projects > Mummi (Multi-user MOD_MP3 Interface) > mummi.0.6/lib/filesys.inc
<?
class FILESYS{

	// Define class variables.
	// a file pointer var.
	var $fs_file_id;
	var $fs_filename;
	var $fs_filesize;

	// Define class varibles inside the class function.
	function FILESYS(){
		// These items are variable, and may change.
		$this->fs_file_id = false;
		$this->fs_filename = false;
		$this->fs_filesize = false;
	}//eo constructor function.

	function open_file($filepath){
		if(!is_file($filepath)or(!$fp = fopen($filepath, "rb+"))){
			clearstatcache();
			return false;
		}else{
			$this->fs_file_id  = $fp;
			$this->fs_filename = $filepath;
			$this->fs_filesize = filesize($this->fs_filename);
			//echo "this filesize = $this->fs_filesize<br>\n";
			clearstatcache();
			return true;
		}
	}//eo open_file function


	// get the entire contents of a file into a string.
	function get_scm_data(){
		rewind($this->fs_file_id);
		return fread($this->fs_file_id, $this->fs_filesize);
	}//eo get_file_data function.

	function set_scm_data($scm_contents){
		rewind($this->fs_file_id);
		return fwrite($this->fs_file_id, $scm_contents);
	}

	// I dislike having to decide if I need fgetc(), or fread().
	// So here is a function that decides for me.
	// Anything strange happens and the func returns false.
	function read_file($size_int=1){
		switch($size_int){
			case 0:
				return false;
			case 1:
				fgetc($this->fs_file_id);
			break;
			case ($size_int > 1):
				fread($this->fs_file_id,$size_int);
			break;
		}//eo switch
	}//eo read_file function.



	// Closing the file handle.
	function close_file($file_id){
		if(!fclose($this->fs_file_id)) return false;
		else return true;
	}


	// ---------------------------------------------------------


	// The main difference of the above and below functions...
	// is the above are true class funcs, the bellow are not confined to this class.


	//
	function get_attribs($path){
		if(is_file($path) || is_dir($path)){
			if(is_writeable($temp)) $a .= "w";
			if(is_readable($temp)) $a .= "r";
			if(is_executable($temp)) $a .= "e";
			clearstatcache();
			return $a;
		}else{
			clearstatcache();
			return false;
		}
	}


	// build a path from two parts.
	function create_path($path,$relative=""){
		//echo "relative path = $relative<br>\n";
		if(!$path)return false;
		if(empty($relative))return $path;

/*	this needs to be fixed.... I dont' like the way the dots are handled. this needs to be
	reg-pattern needs to be updated, and tested.*/

		if(ereg("\..", $relative))return "$path";



		$fp = "$path" . "$relative";
		if(is_file($fp) xor is_dir($fp) xor is_link($fp)){
			clearstatcache();
			return $fp;
		}
	}


	// Make a full path safe for the inocent eyes of our guest, relative.
	function make_relative($path, $fullpath){
		if(!$path or !$fullpath)return false;
		return str_replace("$path", "", "$fullpath");
	}



	// returns the path of the parent folder.
	// simply returns the path minus the stuff to the right of the right-most slash
	function up_dir($path){
		return str_replace("/".end(explode("/", $path)), "", "$path");
	}

	// returns the end of the path, either a filename, or the last folder in the path.
	function node_name($path){
		return end(explode("/", $path));
	}



	// update 9-23 to work with non-arrays better.
	function ckext($filename, $allow_ext){
		if(empty($filename))return false;
		if(empty($allow_ext))return true;
		if(is_array($allow_ext)){
			while(list(,$val)=each($allow_ext)){
				if(eregi("\.$val$", $filename)) return true;
				else return false;
			}
		}
		elseif(eregi("\.$allow_ext$", $filename)) return true;
		else return false;
	}//eo chext function.







	// This can scan a folder, and return the contents.
	// This function supports recursion.
	// ex: rdir("/tmp",r,"m3u")
	// would recursivly scan /tmp for any file ending in .m3u
	// This returns a special array, one for files, the other for folders.
	// In recursive mode, a multi-dmimesional array is returned containing a tree structure.

	function rdir($dir,$r="",$file_ext=""){

		if(!$dir) return false;

		if(!is_dir($dir)){
			clearstatcache();
			return false;
		}else clearstatcache();

		$dir_array=array();
		$file_array=array();
		$a=0;
		$b=0;
		$d=dir("$dir");
		while($e=$d->read()){
			$f=filetype("$dir/" . "$e");
			clearstatcache();
			if($f=="dir"){
				if(($e=="..")or($e=="."))continue;
				switch($r){
					// recursive scan.
					case r:
					$dir_array["$e"]=$this->rdir("$dir/"."$e",r,$file_ext);
					$a++;
					break;
					// flat scan (default).
					case f:
					default:
					$dir_array["$e"]="$dir/"."$e";$a++;
				}//eo switch
			}elseif($f=="file"){
				if(!$this->ckext($e, $file_ext))continue;
				$file_array[$e]="$dir/"."$e";
				$b++;
			}
		}//eo while
		$d->close();
		return array(0=>$dir_array,1=>$file_array);
	}//eo function


	//A function that can walk an array produced by rdir()
	// This function simply applies another function based on a condition met in the paramater.
	function walk_rdir_array($rdir_array,$func){
		while(list($key,$val)=each($rdir_array)){
			if(is_array($val)) $dir_array[$key] = $this->walk_rdir_array($val,$func);
			else{
				if($func($val,$key)) $file_array[$key] = $val;
				else continue;
			}
		}//eo while
		return array(0=>$dir_array,1=>$file_array);
	}//eo function



	// Create an md5 signature of the file data.
	function file_data_md5($path2file){
		$filesize = filesize("$path2file");
		clearstatcache();
		$fp = fopen("$path2file", "r");
		$file_data = fread($fp, $filesize);
		fclose($fp);
		return md5($file_data);
	}
	// Create an crc32 check-sum of the file data.
	function file_data_crc($path2file){
		$filesize = filesize("$path2file");
		clearstatcache();
		$fp = fopen("$path2file", "r");
		$file_data = fread($fp, $filesize);
		fclose($fp);
		return crc32($file_data);
	}


	// a function to randomize an array.
	function randoray($array){
		srand((double)microtime()*10000000);
		$n = count($array);
		$i=1;
		while($i<=$n){
			$r = array_rand($array);
			$randarray[$i] = $array[$r];
			unset($array[$r]);
			$i++;
		}//eo while
		return $randarray;
	}//eofunction

	function sorty($array, $op="a"){
		switch($op){
			case "d":		/* d = decending order. */
			@arsort($array);
			break;
			case "a":		/* a = ascending order. */
			@asort($array);
			break;
			case "r":		/* r = random order. */
			@randoray($array);
			break;
		}
		return $array;
	}



}//eo class.



?>
Return current item: Mummi (Multi-user MOD_MP3 Interface)