Location: PHPKode > projects > Simple Way to Usenet > swun/libs/core/files/file_local.class.php
<?php
/**
 *  
 * @author  Benjamin Gillissen <hide@address.com>
 * 
 *	**************************************************************

	Copyright (C) 2009  Benjamin Gillissen
	
	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details at:
	http://www.gnu.org/copyleft/gpl.html

 *	**************************************************************
 */

class file_local extends file_accessor {
		
	public function __construct($RRef){
		parent::__construct($RRef);
		unset($RRef);
		$this->basepath = configs::get('file_repo', $this->RRef, 'conf');
	}
	
	public function isremote(){ return FALSE; }
	
	public function connect(){ 
		if ( isset($this->isconn) ){ return $this->isconn; }
		if ( ! file_exists($this->basepath) ){
			$tocreate = $this->path_to_path($this->basepath);
			foreach($tocreate as &$path ){
				if ( ! is_dir($path) ){
					if ( FALSE === mkdir($path) ){ break; }
				}
			}
			if ( ! file_exists($this->basepath) ){
				$this->isconn = FALSE;
				return errors::getnew('Basepath for Local File Repo '.$this->RRef.' does not exists and could not be created', CORE_LOG_WARNING, 'FLOCAL');
			}
		} elseif ( !is_readable($this->basepath) ){
			$this->isconn = FALSE;
			return errors::getnew('Basepath for Local File Repo '.$this->RRef.' is not readable.', CORE_LOG_ERROR, 'FLOCAL');
		} elseif ( !is_dir($this->basepath) ){
			$this->isconn = FALSE;
			return errors::getnew('Basepath for Local File Repo '.$this->RRef.' is not a directory.', CORE_LOG_ERROR, 'FLOCAL');
		}
		errors::raise('Connected to Local File Repo "'.$this->RRef.'" ('.$this->basepath.').', CORE_LOG_DEBUG, 'FLOCAL');
		$this->isconn = TRUE;
		$this->curpath = $this->basepath;
		return TRUE;
	}
	
	public function disconnect(){
		unset($this->isconn, $this->curpath);
		if ( @count($this->_HDL) != 0 ){
			errors::raise("Closing ".count($this->_HDL)." Ressources left open.", CORE_LOG_DEBUG, 'FLOCAL');
			foreach($this->_HDL as &$ref ){ @fclose($ref);@closedir($ref); }
			
		}
		unset($this->_HDL);
		errors::raise('Now Disconnected from Local File Repo "'.$this->RRef.'" ('.$this->basepath.').', CORE_LOG_DEBUG, 'FLOCAL');
		return TRUE;
	}
		
	public function chdir($path){
		$rpath = $this->realpath($path);
		if ( is_dir($rpath) ){ $this->curpath = $rpath;return TRUE; }
		return FALSE;
	}
	
	public function exists($path){
		$rpath = $this->realpath($path);
		if ( is_file($rpath) ){ return TRUE; }
		if ( is_dir($rpath) ){ return TRUE; }		
		return FALSE;
	}
	
	public function isfile($path){
		$rpath = $this->realpath($path);
		if ( !file_exists($rpath) ){ return FALSE; }
		return is_file($rpath);
		
	}
	
	public function isdir($path){ 
		$rpath = $this->realpath($path);
		if ( !file_exists($rpath) ){ return FALSE; }
		return is_dir($rpath);
	}
	
	public function readdir($path){ 
		if ( ! $this->isdir($path) ){ return FALSE; }
		$ref = __FUNCTION__.md5($path);
		if ( !isset($this->_HDL[$ref]) ){
			$rpath = $this->realpath($path);
			$this->_HDL[$ref] = @opendir($rpath);
			if ( FALSE === $this->_HDL[$ref] ){ unset($this->_HDL[$ref]);return FALSE; }
		}
		$o = readdir($this->_HDL[$ref]);
		if ( $o == '.' OR $o == '..' ){ return $this->readdir($path); }
		if ( $o === FALSE ){ closedir($this->_HDL[$ref]);unset($this->_HDL[$ref]); }
		return $o;
	}
	
	public function readfile($path){ 
		if ( !$this->isfile($path) ){ return FALSE; }	
		$rpath = $this->realpath($path);
		return file_get_contents($rpath);	
	}
	
	public function getmtime($path){
		if ( !$this->exists($path) ){ return FALSE; }
		$rpath = $this->realpath($path);
		return filemtime($rpath);
	}
	
	public function getctime($path){ 
		if ( !$this->exists($path) ){ return FALSE; }	
		$rpath = $this->realpath($path);
		return filectime($rpath);
	}
	
	public function getsize($path){
		$rpath = $this->realpath($path);
		/*
		$a = fopen($rpath, 'r');
		if ( FALSE === $a ){ return FALSE; }
		fseek($a, 0, SEEK_END);
		$filesize = ftell($a);
		fclose($a);
		return $filesize; 
		//echo "$path => ".sprintf("%u", filesize($rpath))." == ".filesize($rpath)."<br/>\n";
		*/
		return sprintf("%u", filesize($rpath));
	}
	
	public function remove($path, $recur=FALSE){
		if ( $this->isdir($path) ){
			if ( $recur ){
				while( FALSE !== ($file = $this->readdir($path)) ){
					$npath = $path.'/'.$file;
					$nrecur = $this->is_dir($npath);
					if ( ! $this->remove($npath, $nrecur) ){
						return FALSE;
					}
				}
			}
			//rmdir();
			//rewalk through path childs, if none remove folder.
		} elseif ( $this->isfile($path) ){
			if ( $recur ){ errors::raise('Using recursive flag when removing a file got no effect', CORE_LOG_NOTICE, 'FLOCAL'); }
			$rpath = $this->realpath($path);
			return unlink($rpath);
		} else {
			return TRUE;
		}
	}

	public function createdir($path, $recur=FALSE){
		$rpath = $this->realpath($path);
		if ( FALSE === $recur ){ return mkdir($rpath); }
		return mkdir($rpath);
		//check existance of each path level, create if not present
	}
	
	public function createfile($path, $data=""){ 
		$rpath = $this->realpath($path);
		return file_put_contents($rpath, $data);	
	}
	
	//IO
	
	public function file_open($path, $mode){
		$rpath = $this->realpath($path);
		$FH = CORE::hash(NULL, 10).md5($rpath);
		$this->_HDL[$FH] = fopen($rpath, $mode);
		if ( FALSE === $this->_HDL[$FH] ){
			unset($this->_HDL[$FH]);
			return FALSE;
		}
		return $FH;
	}
	
	

}
return TRUE;
Return current item: Simple Way to Usenet