Files
Studio-42-elFinder/php/elFinderVolumeFTP.class.php
T

811 lines
18 KiB
PHP

<?php
/**
* Simple elFinder driver for FTP
*
* @author Dmitry (dio) Levashov
* @author Cem (discofever)
**/
class elFinderVolumeFTP extends elFinderVolumeDriver {
/**
* Driver id
* Must be started from letter and contains [a-z0-9]
* Used as part of volume id
*
* @var string
**/
protected $driverId = 'f';
/**
* FTP Connection Instance
*
* @var ftp
**/
protected $ftp_conn = null;
/**
* Directory for tmp files
* If not set driver will try to use tmbDir as tmpDir
*
* @var string
**/
protected $tmpPath = '';
/**
* Files info cache
*
* @var array
**/
protected $cache = array();
/**
* Last FTP error message
*
* @var string
**/
protected $ftpError = '';
/**
* undocumented class variable
*
* @var string
**/
protected $separator = '';
/**
* Constructor
* Extend options with required fields
*
* @return void
* @author Dmitry (dio) Levashov
* @author Cem (DiscoFever)
**/
public function __construct() {
$opts = array(
'host' => 'localhost',
'user' => '',
'pass' => '',
'port' => 21,
'mode' => 'passive',
'path' => '/',
'timeout' => 10,
);
$this->options = array_merge($this->options, $opts);
}
/*********************************************************************/
/* INIT AND CONFIGURE */
/*********************************************************************/
/**
* Prepare FTP connection
* Connect to remote server and check if credentials are correct, if so, store the connection id in $ftp_conn
*
* @return bool
* @author Dmitry (dio) Levashov
* @author Cem (DiscoFever)
**/
protected function init() {
if (!$this->options['host']
|| !$this->options['user']
|| !$this->options['pass']
|| !$this->options['port']
|| !$this->options['path']) {
return false;
}
$this->separator = $this->options['separator'] ? $this->options['separator'] : DIRECTORY_SEPARATOR;
// make path absolute and normalize
$this->options['path'] = $this->_normpath($this->options['path']);
$this->aroot = $this->options['path'];
if ($this->aroot == $this->separator && empty($this->options['alias'])) {
$this->options['alias'] = 'FTP home folder';
}
// echo filemtime($this->ftpurl($this->aroot));
// $stat = stat($this->ftpurl($this->aroot));
//
// echo sprintf("0%o", 0777 & $stat['mode']);
return is_dir($this->ftpurl($this->aroot));
}
/*********************************************************************/
/* INIT AND CONFIGURE */
/*********************************************************************/
/**
* Configure after successfull mount.
*
* @return void
* @author Dmitry (dio) Levashov
**/
protected function configure() {
parent::configure();
}
/*********************************************************************/
/* FS API */
/*********************************************************************/
/**
* undocumented function
*
* @return void
* @author Dmitry Levashov
**/
protected function ftpurl($path) {
return 'ftp://'.$this->options['user'].':'.$this->options['pass'].'@'.$this->options['host'].':'.$this->options['port'].$path;
}
/*********************** paths/urls *************************/
/**
* Return parent directory path
*
* @param string $path file path
* @return string
* @author Dmitry (dio) Levashov
**/
protected function _dirname($path) {
return dirname($path);
}
/**
* Return file name
*
* @param string $path file path
* @return string
* @author Dmitry (dio) Levashov
**/
protected function _basename($path) {
return basename($path);
}
/**
* Join dir name and file name and retur full path
*
* @param string $dir
* @param string $name
* @return string
* @author Dmitry (dio) Levashov
**/
protected function _joinPath($dir, $name) {
return $dir.DIRECTORY_SEPARATOR.$name;
}
/**
* Return normalized path, this works the same as os.path.normpath() in Python
*
* @param string $path path
* @return string
* @author Troex Nevelin
**/
protected function _normpath($path) {
if (empty($path)) {
$path = '.';
}
$path = preg_replace('|^\.'.preg_quote($this->separator).'?|', '/', $path);
$path = preg_replace('/^([^\/])/', "/$1", $path);
if (strpos($path, '/') === 0) {
$initial_slashes = true;
} else {
$initial_slashes = false;
}
if (($initial_slashes)
&& (strpos($path, '//') === 0)
&& (strpos($path, '///') === false)) {
$initial_slashes = 2;
}
$initial_slashes = (int) $initial_slashes;
$comps = explode('/', $path);
$new_comps = array();
foreach ($comps as $comp) {
if (in_array($comp, array('', '.'))) {
continue;
}
if (($comp != '..')
|| (!$initial_slashes && !$new_comps)
|| ($new_comps && (end($new_comps) == '..'))) {
array_push($new_comps, $comp);
} elseif ($new_comps) {
array_pop($new_comps);
}
}
$comps = $new_comps;
$path = implode('/', $comps);
if ($initial_slashes) {
$path = str_repeat('/', $initial_slashes) . $path;
}
return $path ? $path : '.';
}
/**
* Return file path related to root dir
*
* @param string $path file path
* @return string
* @author Dmitry (dio) Levashov
**/
protected function _relpath($path) {
return $path == $this->root ? '' : substr($path, strlen($this->root)+1);
}
/**
* Convert path related to root dir into real path
*
* @param string $path file path
* @return string
* @author Dmitry (dio) Levashov
**/
protected function _abspath($path) {
return $path == $this->separator ? $this->root : $this->root.$this->separator.$path;
}
/**
* Return fake path started from root dir
*
* @param string $path file path
* @return string
* @author Dmitry (dio) Levashov
**/
protected function _path($path) {
return $this->rootName.($path == $this->root ? '' : $this->separator.$this->_relpath($path));
}
/**
* Return true if $path is children of $parent
*
* @param string $path path to check
* @param string $parent parent path
* @return bool
* @author Dmitry (dio) Levashov
**/
protected function _inpath($path, $parent) {
return $path == $parent || strpos($path, $parent.$this->separator) === 0;
}
/*********************** check type *************************/
/**
* Return true if file exists
*
* @param string $path file path
* @return bool
* @author Dmitry (dio) Levashov
**/
protected function _fileExists($path) {
return file_exists($this->ftpurl($path));
}
/**
* Return true if path is a directory
*
* @param string file path
* @return bool
* @author Dmitry (dio) Levashov
**/
protected function _isDir($path) {
return is_dir($this->ftpurl($path));
}
/**
* Return true if path is a file
*
* @param string file path
* @return bool
* @author Dmitry (dio) Levashov
**/
protected function _isFile($path) {
return is_file($this->ftpurl($path));
}
/**
* Return true if path is a symlink
*
* @param string file path
* @return bool
* @author Dmitry (dio) Levashov
**/
protected function _isLink($path) {
return false;
die('Not yet implemented. (_isLink)');
return is_link($path);
}
/***************** file attributes ********************/
/**
* Return true if path is readable
*
* @param string file path
* @return bool
* @author Dmitry (dio) Levashov
**/
protected function _isReadable($path) {
return is_readable($this->ftpurl($path));
}
/**
* Return true if path is writable
*
* @param string file path
* @return bool
* @author Dmitry (dio) Levashov
**/
protected function _isWritable($path) {
return is_writable($this->ftpurl($path));
}
/**
* Return true if path is locked
*
* @param string file path
* @return bool
* @author Dmitry (dio) Levashov
**/
protected function _isLocked($path) {
return false;
}
/**
* Return true if path is hidden
*
* @param string file path
* @return bool
* @author Dmitry (dio) Levashov
**/
protected function _isHidden($path) {
return false;
}
/***************** file stat ********************/
/**
* Return file size
*
* @param string $path file path
* @return int
* @author Dmitry (dio) Levashov
**/
protected function _filesize($path) {
return filesize($this->ftpurl($path));
}
/**
* Return file modification time
*
* @param string $path file path
* @return int
* @author Dmitry (dio) Levashov
**/
protected function _filemtime($path) {
return filemtime($this->ftpurl($path));
}
/**
* Return true if path is dir and has at least one childs directory
*
* @param string $path dir path
* @return bool
* @author Dmitry (dio) Levashov
**/
protected function _subdirs($path) {
$url = $this->ftpurl($path);
if (is_readable($url) && ($ls = @scandir($url)) && is_array($ls)) {
foreach ($ls as $name) {
if ($name != '.' && $name != '..' && is_dir($path.$this->separator.$name)) {
return true;
}
}
}
return false;
}
/**
* Return object width and height
* Ususaly used for images, but can be realize for video etc...
*
* @param string $path file path
* @param string $mime file mime type
* @return string
* @author Dmitry (dio) Levashov
**/
protected function _dimensions($path, $mime) {
die('Not yet implemented. (_dimensions)');
return false;
}
/**
* Return symlink stat (required only size and mtime)
*
* @param string $path link path
* @return array
* @author Dmitry (dio) Levashov
**/
protected function _lstat($path) {
return false;
}
/******************** file/dir content *********************/
/**
* Return symlink target file
*
* @param string $path link path
* @return string
* @author Dmitry (dio) Levashov
**/
protected function _readlink($path) {
return false;
die('Not yet implemented. (_readlink)');
if (!($target = @readlink($path))) {
return false;
}
if (substr($target, 0, 1) != DIRECTORY_SEPARATOR) {
$target = dirname($path).DIRECTORY_SEPARATOR.$target;
}
$atarget = realpath($target);
if (!$atarget) {
return false;
}
$root = $this->root;
$aroot = $this->aroot;
if ($this->_inpath($atarget, $this->aroot)) {
return $this->_normpath($this->root.DIRECTORY_SEPARATOR.substr($atarget, strlen($this->aroot)+1));
}
return false;
}
/**
* Return files list in directory.
*
* @param string $path dir path
* @return array
* @author Dmitry (dio) Levashov
* @author Cem (DiscoFever)
**/
protected function _scandir($path) {
$files = array();
foreach (scandir($path) as $name) {
// echo $name.'<br>';
if ($name != '.' && $name != '..') {
$files[] = $path.$this->separator.$name;
}
}
return $files;
}
/**
* Open file and return file pointer
*
* @param string $path file path
* @param bool $write open file for writing
* @return resource|false
* @author Dmitry (dio) Levashov
**/
protected function _fopen($path, $mode='rb') {
die('Not yet implemented. (_fopen)');
return @fopen($path, $mode);
}
/**
* Close opened file
*
* @param resource $fp file pointer
* @return bool
* @author Dmitry (dio) Levashov
**/
protected function _fclose($fp, $path='') {
die('Not yet implemented.');
return @fclose($fp);
}
/******************** file/dir manipulations *************************/
/**
* Create dir
*
* @param string $path parent dir path
* @param string $name new directory name
* @return bool
* @author Dmitry (dio) Levashov
**/
protected function _mkdir($path, $name) {
$path = $path.DIRECTORY_SEPARATOR.$name;
if ($path == '' || (!($this->ftp_conn)))
{
return false;
}
$result = @ftp_mkdir($this->ftp_conn, $path);
if ($result === false)
{
$this->setError('Unable to create remote directory.');
return false;
}
/* TODO : implement for ftp */
/*
if (@mkdir($path)) {
@chmod($path, $this->options['dirMode']);
return true;
}
*/
return true;
}
/**
* Create file
*
* @param string $path parent dir path
* @param string $name new file name
* @return bool
* @author Dmitry (dio) Levashov
**/
protected function _mkfile($path, $name) {
die('Not yet implemented. (_mkfile)');
$path = $path.DIRECTORY_SEPARATOR.$name;
if (($fp = @fopen($path, 'w'))) {
@fclose($fp);
@chmod($path, $this->options['fileMode']);
return true;
}
return false;
}
/**
* Create symlink
*
* @param string $target link target
* @param string $path symlink path
* @return bool
* @author Dmitry (dio) Levashov
**/
protected function _symlink($target, $path, $name='') {
die('Not yet implemented. (_symlink)');
if (!$name) {
$name = basename($path);
}
return @symlink('.'.DIRECTORY_SEPARATOR.$this->_relpath($target), $path.DIRECTORY_SEPARATOR.$name);
}
/**
* Copy file into another file
*
* @param string $source source file path
* @param string $targetDir target directory path
* @param string $name new file name
* @return bool
* @author Dmitry (dio) Levashov
**/
protected function _copy($source, $targetDir, $name='') {
die('Not yet implemented. (_copy)');
$target = $targetDir.DIRECTORY_SEPARATOR.($name ? $name : basename($source));
return copy($source, $target);
}
/**
* Move file into another parent dir
*
* @param string $source source file path
* @param string $target target dir path
* @param string $name file name
* @return bool
* @author Dmitry (dio) Levashov
**/
protected function _move($source, $targetDir, $name='') {
die('Not yet implemented. (_move)');
$target = $targetDir.DIRECTORY_SEPARATOR.($name ? $name : basename($source));
return @rename($source, $target);
}
/**
* Remove file
*
* @param string $path file path
* @return bool
* @author Dmitry (dio) Levashov
**/
protected function _unlink($path) {
if ($path == '' || (!($this->ftp_conn)))
{
return false;
}
$result = @ftp_delete($this->ftp_conn, $path);
if ($result === false)
{
return $this->setError('Unable to delete remote file');
return false;
}
return true;
}
/**
* Remove dir
*
* @param string $path dir path
* @return bool
* @author Dmitry (dio) Levashov
**/
protected function _rmdir($path) {
die('Not yet implemented. (_rmdir)');
return @rmdir($path);
}
/**
* Create new file and write into it from file pointer.
* Return new file path or false on error.
*
* @param resource $fp file pointer
* @param string $dir target dir path
* @param string $name file name
* @return bool|string
* @author Dmitry (dio) Levashov
**/
protected function _save($fp, $dir, $name, $mime, $w, $h) {
die('Not yet implemented. (_save)');
$path = $dir.DIRECTORY_SEPARATOR.$name;
if (!($target = @fopen($path, 'wb'))) {
return false;
}
while (!feof($fp)) {
fwrite($target, fread($fp, 8192));
}
fclose($target);
@chmod($path, $this->options['fileMode']);
clearstatcache();
return $path;
}
/**
* Get file contents
*
* @param string $path file path
* @return string|false
* @author Dmitry (dio) Levashov
**/
protected function _getContents($path) {
ie('Not yet implemented. (_getContents)');
return file_get_contents($path);
}
/**
* Write a string to a file
*
* @param string $path file path
* @param string $content new file content
* @return bool
* @author Dmitry (dio) Levashov
**/
protected function _filePutContents($path, $content) {
die('Not yet implemented. (_filePutContents)');
if (@file_put_contents($path, $content, LOCK_EX) !== false) {
clearstatcache();
return true;
}
return false;
}
/**
* Detect available archivers
*
* @return void
**/
protected function _checkArchivers() {
// die('Not yet implemented. (_checkArchivers)');
return array();
}
/**
* Unpack archive
*
* @param string $path archive path
* @param array $arc archiver command and arguments (same as in $this->archivers)
* @return true
* @return void
* @author Dmitry (dio) Levashov
* @author Alexey Sukhotin
**/
protected function _unpack($path, $arc) {
die('Not yet implemented. (_unpack)');
return false;
}
/**
* Recursive symlinks search
*
* @param string $path file/dir path
* @return bool
* @author Dmitry (dio) Levashov
**/
protected function _findSymlinks($path) {
die('Not yet implemented. (_findSymlinks)');
if (is_link($path)) {
return true;
}
if (is_dir($path)) {
foreach (scandir($path) as $name) {
if ($name != '.' && $name != '..') {
$p = $path.DIRECTORY_SEPARATOR.$name;
if (is_link($p)) {
return true;
}
if (is_dir($p) && $this->_findSymlinks($p)) {
return true;
} elseif (is_file($p)) {
$this->archiveSize += filesize($p);
}
}
}
} else {
$this->archiveSize += filesize($path);
}
return false;
}
/**
* Extract files from archive
*
* @param string $path archive path
* @param array $arc archiver command and arguments (same as in $this->archivers)
* @return true
* @author Dmitry (dio) Levashov,
* @author Alexey Sukhotin
**/
protected function _extract($path, $arc) {
die('Not yet implemented. (_extract)');
}
/**
* Create archive and return its path
*
* @param string $dir target dir
* @param array $files files names list
* @param string $name archive name
* @param array $arc archiver options
* @return string|bool
* @author Dmitry (dio) Levashov,
* @author Alexey Sukhotin
**/
protected function _archive($dir, $files, $name, $arc) {
die('Not yet implemented. (_archive)');
return false;
}
} // END class
?>