<?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_ftp extends file_accessor {
private $_CONN;
private $conf = Array( 'host' => '127.0.0.1',
'port' => '21',
'timeout' => 30,
'user' => 'anonymous',
'pass' => 'hide@address.com',
'ssl' => FALSE,
'passif'=> FALSE
);
public function __construct($RRef){
parent::__construct($RRef);
unset($RRef);
$conf = configs::get('file_repo', $this->RRef, 'conf');
$this->basepath = configs::get('file_repo', $this->RRef, Array('conf', 'basepath'));
if ( !isset($conf['basepath']) ){
$this->basepath = '/';
} else {
$this->basepath = $conf['basepath'];
unset($conf['basepath']);
}
foreach($conf as $opt => &$val){ $this->conf[$opt] = $val; }
}
public function connect(){
if ( isset($this->isconn) ){ return $this->isconn; }
if ( $this->conf['ssl'] === TRUE ){
$this->_CONN = @ftp_ssl_connect($this->conf['host'], $this->conf['port'], $this->conf['timeout']);
} else {
$this->_CONN = @ftp_connect($this->conf['host'], $this->conf['port'], $this->conf['timeout']);
}
if ( FALSE === $this->_CONN ){
$this->isconn = FALSE;
return errors::getnew('Connection to FTP File Repo '.$this->RRef.' could not be established', CORE_LOG_ERROR, 'FTP');
} elseif ( !@ftp_login($this->_CONN, $this->conf['user'], $this->conf['pass']) ){
$this->isconn = FALSE;
return errors::getnew('Connection to FTP File Repo '.$this->RRef.' failed, authentification failure', CORE_LOG_ERROR, 'FTP');
} elseif ( FALSE === @ftp_pasv($this->_CONN, $this->conf['passif']) ){
$this->isconn = FALSE;
ftp_close($this->_CONN);
return errors::getnew('Connection to FTP File Repo '.$this->RRef.' failed, could not switch PASV mode', CORE_LOG_ERROR, 'FTP');
} elseif ( FALSE === @ftp_chdir($this->_CONN, $this->basepath) ){
$this->isconn = FALSE;
ftp_close($this->_CONN);
return errors::getnew('Connection to FTP File Repo '.$this->RRef.' failed, could not chdir to remote basepath ('.$this->basepath.')', CORE_LOG_ERROR, 'FTP');
}
$this->isconn = TRUE;
$this->curpath = $this->basepath;
errors::raise('Connected to FTP File Repo "'.$this->RRef.'" ('.$this->conf['host'].':'.$this->conf['port'].$this->basepath.')', CORE_LOG_DEBUG, 'FTP');
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);
ftp_close($this->_CONN);
unset($this->_CONN);
errors::raise('Now Disconnected from FTP File Repo "'.$this->RRef.'" ('.$this->conf['host'].':'.$this->conf['port'].$this->basepath.')', CORE_LOG_DEBUG, 'FTP');
return TRUE;
}
public function exists($path){
$rpath = $this->realpath($path);
$url = 'ftp://'.$this->conf['user'].':'.$this->conf['pass'].'@'.$this->conf['host'].':'.$this->conf['port'].$rpath;
if ( $this->isfile($path) ){ return TRUE; }
if ( is_dir($url) ){ return TRUE; }
return FALSE;
}
public function isfile($path){
return (FALSE !== $this->getmtime($path) AND $this->isdir($path) === FALSE );
/*
$rpath = $this->realpath($path);
$url = 'ftp://'.$this->conf['user'].':'.$this->conf['pass'].'@'.$this->conf['host'].':'.$this->conf['port'].$rpath;
return is_file($url);
*/
}
public function isdir($path){
$rpath = $this->realpath($path);
if ( @ftp_chdir($this->_CONN, $rpath) ){
ftp_chdir($this->_CONN, $this->curpath);
return TRUE;
}
return FALSE;
}
public function chdir($path){
$rpath = $this->realpath($path);
if ( ftp_chdir($this->_CONN, $rpath) ){ $this->curpath = $rpath;return TRUE; }
return FALSE;
}
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]['list'] = ftp_nlist($this->_CONN, $rpath);
$this->_HDL[$ref]['k'] = 0;
if ( FALSE === $this->_HDL[$ref]['list'] ){ unset($this->_HDL[$ref]);return FALSE; }
}
if ( isset($this->_HDL[$ref]['list'][$this->_HDL[$ref]['k']]) ){
$o = $this->_HDL[$ref]['list'][$this->_HDL[$ref]['k']];
unset($this->_HDL[$ref]['list'][$this->_HDL[$ref]['k']]);
$this->_HDL[$ref]['k']++;
return basename($o);
} else {
unset($this->_HDL[$ref]);
return FALSE;
}
}
public function readfile($path){
$rpath = $this->realpath($path);
$url = 'ftp://'.$this->conf['user'].':'.$this->conf['pass'].'@'.$this->conf['host'].':'.$this->conf['port'].$rpath;
return file_get_contents($url);
}
public function getmtime($path){
$rpath = $this->realpath($path);
$buff = ftp_mdtm($this->_CONN, $rpath);
if ($buff == "-1" ){ return FALSE; }
return $buff;
}
public function getctime($path){
return FALSE;
$rpath = $this->realpath($path);
$url = 'ftp://'.$this->conf['user'].':'.$this->conf['pass'].'@'.$this->conf['host'].':'.$this->conf['port'].$rpath;
$buff = filectime($url);
if ($buff == "-1" ){ return FALSE; }
return $buff;
}
public function getsize($path){
$rpath = $this->realpath($path);
$buff = ftp_size($this->_CONN, $rpath);
if ($buff != -1) { return sprintf ("%u", $buff); }
return FALSE;
}
public function remove($path, $recur=FALSE){
if ( $this->isdir($path) ){
if ( $recur ){
while( FALSE !== ($child = $this->readdir($path)) ){
$npath = $path.'/'.$child;
if ( ! $this->remove($npath, $recur) ){ return FALSE; }
}
}
if ( FALSE === ftp_rmdir($this->_CONN, $this->realpath($path) ) ){ return FALSE; }
return TRUE;
}
$rpath = $this->realpath($path);
return ftp_delete($this->_CONN, $rpath);
}
public function createdir($path, $recur=FALSE){
$rpath = $this->realpath($path);
if ( FALSE === $recur ){ return ftp_mkdir($this->_CONN, $rpath); }
//check existance of each path level, create if not present
}
public function createfile($path, $data=""){
$rpath = $this->realpath($path);
//check if exists
$url = 'ftp://'.$this->conf['user'].':'.$this->conf['pass'].'@'.$this->conf['host'].':'.$this->conf['port'].$rpath;
return file_put_contents($url, $data);
}
public function file_open($path, $mode){
$rpath = $this->realpath($path);
$url = 'ftp://'.$this->conf['user'].':'.$this->conf['pass'].'@'.$this->conf['host'].':'.$this->conf['port'].$rpath;
$FH = CORE::hash(NULL, 10).md5($rpath);
$this->_HDL[$FH] = fopen($url, $mode);
if ( FALSE === $this->_HDL[$FH] ){
unset($this->_HDL[$FH]);
return FALSE;
}
return $FH;
}
}