Location: PHPKode > projects > Simple Way to Usenet > libs/core/files/file_abstract.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_abstract {
	
	private $RRef;	//file repositery reference
	private static $_FILE;
	
	public function __construct($RRef=NULL){
		if ( $RRef === NULL ){
			$this->RRef = 'DUMMY';
			if ( isset(self::$_FILE[$this->RRef]) ){ return; }
			self::$_FILE[$this->RRef] = new file_dummy('DUMMY');
			return;
		}
		$this->RRef = $RRef;
		unset($RRef);
		if ( isset(self::$_FILE[$this->RRef]) ){ return; }
		$type = configs::get('file_repo', $this->RRef, 'type');
		if ( FALSE === $type ){
			errors::raise('Missing Configuration for File Repo : '.$this->RRef.'.', CORE_LOG_ERROR, 'FREPO');
			return;	
		}
		$class = 'file_'.$type;
		self::$_FILE[$this->RRef] = new $class($this->RRef);
	}

	public function getRRef(){ return $this->RRef; }
	
	private function precall($method){
		if ( !isset(self::$_FILE[$this->RRef]) ){ return FALSE;}
		if ( $method !== 'connect' AND $method !== 'isconnected'){
			if ( !self::$_FILE[$this->RRef]->isconnected() ){
				errors::raise("Repository must be connected in order to use it !", CORE_LOG_WARNING, 'FABS'); 
				return FALSE; 
			}
		}
		if ( FALSE === method_exists(self::$_FILE[$this->RRef], $method) ){
			errors::raise("Repository Backend is missing method $method !", CORE_LOG_ERROR, 'FABS'); 
			return FALSE; 
		}
		clearstatcache();
		return TRUE;
	}
	
	//conn status
	public function connect(){ 			
		if ( !$this->precall(__FUNCTION__) ){ return FALSE; }
		return self::$_FILE[$this->RRef]->connect(); 
	}
	
	public function isconnected(){		
		if ( !$this->precall(__FUNCTION__) ){ return FALSE; }
		return self::$_FILE[$this->RRef]->isconnected(); 
	}
	
	public function disconnect(){
		if ( !$this->precall(__FUNCTION__) ){ return FALSE; }
		return self::$_FILE[$this->RRef]->disconnect(); 
	}
	
	//browsing
	public function curdir(){ 			
		if ( !$this->precall(__FUNCTION__) ){ return FALSE; }
		return self::$_FILE[$this->RRef]->curdir(); 
	}

	public function chdir($path){
		if ( !$this->precall(__FUNCTION__) ){ return FALSE; }
		return self::$_FILE[$this->RRef]->chdir($path); 
		
	 }
	
	public function readdir($path){
		if ( !$this->precall(__FUNCTION__) ){ return FALSE; }
		return self::$_FILE[$this->RRef]->readdir($path);
	}
	
	//info 
	public function exists($path){
		if ( !$this->precall(__FUNCTION__) ){ return FALSE; }
		return self::$_FILE[$this->RRef]->exists($path);
	 }
	 
	public function isfile($path){
		if ( !$this->precall(__FUNCTION__) ){ return FALSE; }
		return self::$_FILE[$this->RRef]->isfile($path);
	 }
	
	public function isdir($path){
		if ( !$this->precall(__FUNCTION__) ){ return FALSE; }
		return self::$_FILE[$this->RRef]->isdir($path);
	 }
		
	public function getmtime($path){
		if ( !$this->precall(__FUNCTION__) ){ return FALSE; }
		return self::$_FILE[$this->RRef]->getmtime($path);
	 }
	
	public function getctime($path){
		if ( !$this->precall(__FUNCTION__) ){ return FALSE; }
		return self::$_FILE[$this->RRef]->getctime($path);
	 }
	
	public function getsize($path){
		if ( !$this->precall(__FUNCTION__) ){ return FALSE; }
		return self::$_FILE[$this->RRef]->getsize($path);
	 }
	
	//management		 
	public function readfile($path){
		if ( !$this->precall(__FUNCTION__) ){ return FALSE; }
		return self::$_FILE[$this->RRef]->readfile($path);
	 }
	
	public function createdir($path, $recur=FALSE){	
		if ( !$this->precall(__FUNCTION__) ){ return FALSE; }
		if ( self::$_FILE[$this->RRef]->isdir($path) ){
			//errors::raise("createdir aborded : Folder '$path' on Repo '".$this->RRef."' already exists", CORE_LOG_NOTICE, 'FABS');
			return TRUE;
		}
		//errors::raise("CREATEDIR $this->RRef:$path", CORE_LOG_NOTICE, 'FABS');
		return self::$_FILE[$this->RRef]->createdir($path, $recur);
	}
	
	//if file already exists, abord and drop a warning
	public function createfile($path, $data=''){
		if ( !$this->precall(__FUNCTION__) ){ return FALSE; }
		clearstatcache();
		if ( self::$_FILE[$this->RRef]->isfile($path) ){
			errors::raise("createfile aborded : File '$path' on Repo '".$this->RRef."' already exists !", CORE_LOG_WARNING, 'FABS');
			return FALSE;
		}
		errors::raise("CREATEFILE $this->RRef:$path", CORE_LOG_NOTICE, 'FABS');
		return self::$_FILE[$this->RRef]->createfile($path, $data); 
	}
	
	//if file already exists, remove it
	public function writefile($path, $data=''){
		if ( !$this->precall('createfile') ){ return FALSE; }
		if ( self::$_FILE[$this->RRef]->isfile($path) ){
			if ( FALSE === self::$_FILE[$this->RRef]->remove($path) ){
				errors::raise("File '$path' on Repo '".$this->RRef."' already exists, and could not be removed !", CORE_LOG_WARNING, 'FABS');
				return FALSE;
			}
		}
		errors::raise("CREATEFILE $this->RRef:$path", CORE_LOG_NOTICE, 'FABS');
		return self::$_FILE[$this->RRef]->createfile($path, $data);
	}
	
	//remove files and folders, with recurcivity if needed
	public function remove($path, $recur=FALSE){ 
		//TODO, do recur stuff here
		if ( !$this->precall(__FUNCTION__) ){ return FALSE; }
		return self::$_FILE[$this->RRef]->remove($path, $recur);
	} 

	
	// FILE IO
	public function file_open($path, $mode='r'){ 
		if ( !$this->precall(__FUNCTION__) ){ return FALSE; }
		return self::$_FILE[$this->RRef]->file_open($path, $mode);
	}
	 
	public function file_close($FH){ 
		if ( !$this->precall(__FUNCTION__) ){ return FALSE; }
		return self::$_FILE[$this->RRef]->file_close($FH);
	}
	 
	public function file_seek($FH, $offset){ 
		if ( !$this->precall(__FUNCTION__) ){ return FALSE; }
		return self::$_FILE[$this->RRef]->file_seek($FH, $offset);
	}
	
	public function file_read($FH, $limit){ 
		if ( !$this->precall(__FUNCTION__) ){ return FALSE; }
		return self::$_FILE[$this->RRef]->file_read($FH, $limit);
	}
	
	public function file_line($FH, $line=NULL){ 
		if ( !$this->precall(__FUNCTION__) ){ return FALSE; }
		return self::$_FILE[$this->RRef]->file_line($FH, $line);
	}
	
	public function file_put($FH, $data){ 
		if ( !$this->precall(__FUNCTION__) ){ return FALSE; }
		return self::$_FILE[$this->RRef]->file_put($FH, $data);
	}
	
	public function file_tell($FH){ 
		if ( !$this->precall(__FUNCTION__) ){ return FALSE; }
		return self::$_FILE[$this->RRef]->file_tell($FH);
	}
	
	public function file_lock($FH, $lock){ 
		if ( !$this->precall(__FUNCTION__) ){ return FALSE; }
		return self::$_FILE[$this->RRef]->file_lock($FH, $lock);
	}
	
	public function file_end($FH){ 
		if ( !$this->precall(__FUNCTION__) ){ return FALSE; }
		return self::$_FILE[$this->RRef]->file_end($FH);
	}

	public function move($src_ref, $src_path, $dst_ref, $dst_path, $recur=FALSE){
		errors::raise("MOVING [$src_ref][$src_path] TO [$dst_ref][$dst_path]", CORE_LOG_NOTICE, 'FABS');
		$ft = new file_transfert($src_ref, $src_path, $dst_ref, $dst_path);
		$ft->set_option('isamove', TRUE);
		$ft->set_option('recursive', $recur);
		if ( FALSE === $ft->compute_queue() ){ return FALSE; }
		while( FALSE !== $ft->list_queue() ){
			while( TRUE ){
				$stat = $ft->process_item();
				if ( FALSE === $stat ){ break; }
				if ( TRUE === $stat ){ break; }
			}
			if ( $stat !== TRUE ){ break; }
		}
		return ( $stat === TRUE ); 
	}
	
	public function copy($src_ref, $src_path, $dst_ref, $dst_path, $recur=FALSE){
		errors::raise("COPYING [$src_ref][$src_path] TO [$dst_ref][$dst_path]", CORE_LOG_NOTICE, 'FABS');
		$ft = new file_transfert($src_ref, $src_path, $dst_ref, $dst_path);
		$ft->set_option('recursive', $recur);
		if ( FALSE === $ft->compute_queue() ){ return FALSE; }
		while( FALSE !== $ft->list_queue() ){
			while( TRUE ){
				$stat = $ft->process_item();
				if ( FALSE === $stat ){ break; }
				if ( TRUE === $stat ){ break; }
			}
			if ( $stat !== TRUE ){ break; }
		}
		return ( $stat === TRUE ); 
	}

}
Return current item: Simple Way to Usenet