<?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_accessor {
protected $RRef;
protected $isconn;
protected $basepath;
protected $curpath;
protected $_HDL;
public function __construct($RRef){ $this->RRef = $RRef; }
public function isconnected(){ return $this->isconn; }
public function curdir(){ return $this->curpath; }
protected function realpath($path){
if ( "/" == $path ){
$path = $this->basepath;
} elseif ( "." == $path OR empty($path) ){
$path = $this->curpath;
} elseif ( ereg("^/", $path) ){
$path = ereg_replace("^/", '', $path);
$path = $this->basepath.'/'.$this->clean_path($path);
} else {
$path = $this->curpath.'/'.$this->clean_path($path);
}
return $path;
}
private function clean_path($path){
$path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path);
$parts = array_filter(explode(DIRECTORY_SEPARATOR, $path), 'strlen');
$absolutes = array();
foreach ($parts as $part) {
if ('.' == $part) continue;
if ('..' == $part) {
array_pop($absolutes);
} else {
$absolutes[] = $part;
}
}
return implode(DIRECTORY_SEPARATOR, $absolutes);
}
protected function path_to_path($path){
/* for ../cache/newzbin/search
* return [0] ../cache
* [1] ../cache/newzbin
* [2] ../cache/newzbin/search
*/
$buf = split('/', $path);
$f='';
foreach($buf as $c => &$entry ){
if ( $c != 0 ){
$f .= '/'.$entry;
} else {
$f .= $entry;
}
if ( $entry !== '..' AND $entry !== '.' AND !empty($entry) ){ $o[] = $f; }
}
if ( isset($o) ){ return $o; }
return Array();
}
protected function precheck($FH){
if ( !isset($this->_HDL[$FH]) ){
errors::raise('First Argument is not a valid File Ressource', CORE_LOG_WARNING, 'FLOCAL');
return FALSE;
}
return TRUE;
}
public function file_close($FH){
if ( ! $this->precheck($FH) ){ return FALSE; }
fclose($this->_HDL[$FH]);
unset($this->_HDL[$FH]);
return TRUE;
}
public function file_tell($FH){
if ( ! $this->precheck($FH) ){ return FALSE; }
return ftell($this->_HDL[$FH]);
}
public function file_seek($FH, $offset=0){
if ( ! $this->precheck($FH) ){ return FALSE; }
return fseek($this->_HDL[$FH], $offset);
}
public function file_read($FH, $limit=1024){
if ( ! $this->precheck($FH) ){ return FALSE; }
return fread($this->_HDL[$FH], $limit);
}
public function file_line($FH, $line=NULL){
if ( ! $this->precheck($FH) ){ return FALSE; }
if ( $line === NULL ){ return fgets($this->_HDL[$FH]); }
$this->file_seek($FH);
$c=1;
while( FALSE !== ($str = $this->file_line($FH)) ){
if ( $c == $line ){ return $str; }
$c++;
}
return FALSE;
}
public function file_put($FH, $data){
if ( ! $this->precheck($FH) ){ return FALSE; }
return fputs($this->_HDL[$FH], $data);
}
public function file_lock($FH, $lock){
if ( ! $this->precheck($FH) ){ return FALSE; }
return flock($this->_HDL[$FH], $lock);
}
public function file_end($FH){
if ( ! $this->precheck($FH) ){ return TRUE; } //yep, to prevent endless loop
return feof($this->_HDL[$FH]);
}
}