Merging r51397ff from Trunk: moving VFS API classes into a namespaced PSR4 autoloadable structure:

- PSR4 autoloader exists beside our old autloader to support old as well as new structure until everything is ported over
- moved ported API stuff from phpgwapi to new api directory (idea is phpgwapi become a compatibility layer for old code, while we only port selected stuff to new api directory)
- namespaces use prefix "EGroupware", then (first letter capitalised) app-name or "Api", sub-system names like "Vfs" or for apps "Ui", "Bo, "So" and at least class name starting with a capital letter and without understores eg. "StreamWrapper" plus just ".php"
- examples:
  + egw_vfs in phpgwapi/inc/class.egw_vfs.inc.php --> EGroupware\Api\Vfs in api/src/Vfs.php
  + sqlfs_stream_wrapper in phpgwapi/inc/class.sqlfs_stream_wrapper.inc.php --> EGroupware\Api\Vfs\Sqlfs\StreamWrapper in api/src/Vfs/Sqlfs/StreamWrapper.php
  + sqlfs_utils in phpgwapi/inc/class.sqlfs_utils.inc.php --> EGroupware\Api\Vfs\Sqlfs\Utils in api/src/Vfs/Sqlfs/Utils.php
- api directory is no a new svn module but exists (like home) as sub-directory under base egroupware module
This commit is contained in:
Ralf Becker 2015-04-07 09:19:30 +00:00
commit 107eda829b
9 changed files with 7529 additions and 0 deletions

2127
api/src/Vfs.php Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,93 @@
<?php
/**
* EGroupware API: VFS directory for use with SabreDAV
*
* @link http://www.egroupware.org
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @package api
* @subpackage vfs
* @author Ralf Becker <rb@stylitede>
* @copyright (c) 2015 by Ralf Becker <rb@stylite.de>
* @version $Id$
*/
namespace EGroupware\Api\Vfs\Dav;
use Sabre\DAV;
use EGroupware\Api\Vfs;
/**
* VFS directory for use with SabreDAV
*/
class Directory extends DAV\FS\Directory
{
/**
* VFS path without prefix / vfs schema
*
* @var string
*/
protected $vfs_path;
/**
* Constructor
*
* @param string $path vfs path without prefix
*/
function __construct($path)
{
//error_log(__METHOD__."('$path')");
$this->vfs_path = rtrim($path, '/');
parent::__construct(Vfs::PREFIX.$path);
}
/**
* Returns the name of the node
*
* We override this method to remove url-decoding required by EGroupware VFS
*
* @return string
*/
function getName()
{
return Vfs::decodePath(parent::getName());
}
/**
* Returns a specific child node, referenced by its name
*
* This method must throw DAV\Exception\NotFound if the node does not
* exist.
*
* @param string $name
* @throws DAV\Exception\NotFound
* @return DAV\INode
*/
function getChild($name)
{
//error_log(__METHOD__."('$name') this->path=$this->path, this->vfs_path=$this->vfs_path");
$path = $this->vfs_path . '/' . $name;
$vfs_path = $this->vfs_path . '/' . Vfs::encodePathComponent($name);
if (!Vfs::file_exists($vfs_path)) throw new DAV\Exception\NotFound('File with name ' . $path . ' could not be located');
if (Vfs::is_dir($vfs_path))
{
return new Directory($vfs_path);
}
else
{
return new File($vfs_path);
}
}
/**
* Returns available diskspace information
*
* @return array [ available-space, free-space ]
*/
function getQuotaInfo()
{
return [ false, false ];
}
}

87
api/src/Vfs/Dav/File.php Normal file
View File

@ -0,0 +1,87 @@
<?php
/**
* EGroupware API: VFS file for use with SabreDAV
*
* @link http://www.egroupware.org
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @package api
* @subpackage vfs
* @author Ralf Becker <rb@stylitede>
* @copyright (c) 2015 by Ralf Becker <rb@stylite.de>
* @version $Id$
*/
namespace EGroupware\Api\Vfs\Dav;
use Sabre\DAV;
use EGroupware\Api\Vfs;
/**
* VFS file for use with SabreDAV
*/
class File extends DAV\FS\File
{
/**
* VFS path without prefix / vfs schema
*
* @var string
*/
protected $vfs_path;
/**
* Constructor
*
* @param string $path vfs path without prefix
*/
function __construct($path)
{
//error_log(__METHOD__."('$path')");
$this->vfs_path = $path;
parent::__construct(Vfs::PREFIX.$path);
}
/**
* Returns the name of the node
*
* We override this method to remove url-decoding required by EGroupware VFS
*
* @return string
*/
function getName()
{
return Vfs::decodePath(parent::getName());
}
/**
* Returns the ETag for a file
*
* An ETag is a unique identifier representing the current version of the file. If the file changes, the ETag MUST change.
* The ETag is an arbitrary string, but MUST be surrounded by double-quotes.
*
* Return null if the ETag can not effectively be determined
*
* @return mixed
*/
function getETag()
{
if (($stat = Vfs::url_stat($this->vfs_path, STREAM_URL_STAT_QUIET)))
{
return '"'.$stat['ino'].':'.$stat['mtime'].':'.$stat['size'].'"';
}
return null;
}
/**
* Returns the mime-type for a file
*
* If null is returned, we'll assume application/octet-stream
*
* @return mixed
*/
function getContentType()
{
return Vfs::mime_content_type($this->vfs_path);
}
}

View File

@ -0,0 +1,790 @@
<?php
/**
* EGroupware API: VFS - stream wrapper to access the regular filesystem (setting a given user, group and mode)
*
* @link http://www.egroupware.org
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @package api
* @subpackage vfs
* @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
* @copyright (c) 2008-15 by Ralf Becker <RalfBecker-AT-outdoor-training.de>
* @version $Id$
*/
namespace EGroupware\Api\Vfs\Filesystem;
use EGroupware\Api\Vfs;
/**
* eGroupWare API: VFS - stream wrapper to access the regular filesystem (setting a given user, group and mode)
*
* This stream wrapper allows to mount parts of the regular filesystem, under specified permissions.
* You can eg. mount an directory in the docroot to allow the Admin group to upload files there.
*
* This stream wrapper uses query parameters to pass certain options to it:
* - user: uid or user-name owning the path, default root
* - group: gid or group-name owning the path, default root
* - mode: mode bit for the path, default 0005 (read and execute for nobody)
* - exec: false (default) = do NOT allow to upload or modify scripts, true = allow it (if docroot is mounted, this allows to run scripts!)
* scripts are considered every file having a script-extension (eg. .php, .pl, .py), defined with SCRIPT_EXTENSION_PREG constant
* - url: download url, if NOT a regular webdav.php download should be used, eg. because directory already
* lies within the docroot or is mapped via an alias
*
* Example mount command for an uploads directory in the docroot (needs to be writable by webserver!):
*
* filemanager/cli.php mount --user root_admin --password secret --domain default \
* 'filesystem://egal/var/www/html/uploads?group=Admins&mode=075&url=http://domain.com/uploads' /uploads
*
* (admin / secret is username / password of setup user, "root_" prefix differenciate from regular EGw-user!)
*
* To correctly support characters with special meaning in url's (#?%), we urlencode them with Vfs::encodePathComponent
* and urldecode all path again, before passing them to php's filesystem functions.
*
* @link http://www.php.net/manual/en/function.stream-wrapper-register.php
*/
class StreamWrapper implements Vfs\StreamWrapperIface
{
/**
* Scheme / protocol used for this stream-wrapper
*/
const SCHEME = 'filesystem';
/**
* Mime type of directories, the old vfs used 'Directory', while eg. WebDAV uses 'httpd/unix-directory'
*/
const DIR_MIME_TYPE = Vfs::DIR_MIME_TYPE ;
/**
* mode-bits, which have to be set for files
*/
const MODE_FILE = 0100000;
/**
* mode-bits, which have to be set for directories
*/
const MODE_DIR = 040000;
/**
* optional context param when opening the stream, null if no context passed
*
* @var mixed
*/
var $context;
/**
* stream / ressouce this class is opened for by stream_open
*
* @var ressource
*/
private $opened_stream;
/**
* URL of the opened stream, used to build the complete URL of files in the dir
*
* @var string
*/
private $opened_stream_url;
/**
* directory-ressouce this class is opened for by dir_open
*
* @var ressource
*/
private $opened_dir;
/**
* URL of the opened dir, used to build the complete URL of files in the dir
*
* @var string
*/
private $opened_dir_url;
/**
* How much should be logged to the apache error-log
*
* 0 = Nothing
* 1 = only errors
* 2 = all function calls and errors (contains passwords too!)
*/
const LOG_LEVEL = 1;
/**
* Regular expression identifying scripts, to NOT allow updating them if exec mount option is NOT set
*/
const SCRIPT_EXTENSIONS_PREG = '/\.(php[0-9]*|pl|py)$/';
/**
* This method is called immediately after your stream object is created.
*
* @param string $url URL that was passed to fopen() and that this object is expected to retrieve
* @param string $mode mode used to open the file, as detailed for fopen()
* @param int $options additional flags set by the streams API (or'ed together):
* - STREAM_USE_PATH If path is relative, search for the resource using the include_path.
* - STREAM_REPORT_ERRORS If this flag is set, you are responsible for raising errors using trigger_error() during opening of the stream.
* If this flag is not set, you should not raise any errors.
* @param string $opened_path full path of the file/resource, if the open was successfull and STREAM_USE_PATH was set
* @return boolean true if the ressource was opened successful, otherwise false
*/
function stream_open ( $url, $mode, $options, &$opened_path )
{
unset($opened_path); // not used, but required by interface
$this->opened_stream = $this->opened_stream_url = null;
$read_only = str_replace('b','',$mode) == 'r';
// check access rights, based on the eGW mount perms
if (!($stat = self::url_stat($url,0)) || $mode[0] == 'x') // file not found or file should NOT exist
{
$dir = Vfs::dirname($url);
if ($mode[0] == 'r' || // does $mode require the file to exist (r,r+)
$mode[0] == 'x' || // or file should not exist, but does
!Vfs::check_access($dir,Vfs::WRITABLE,$dir_stat=self::url_stat($dir,0))) // or we are not allowed to create it
{
if (self::LOG_LEVEL) error_log(__METHOD__."($url,$mode,$options) file does not exist or can not be created!");
if (!($options & STREAM_URL_STAT_QUIET))
{
trigger_error(__METHOD__."($url,$mode,$options) file does not exist or can not be created!",E_USER_WARNING);
}
return false;
}
}
elseif (!$read_only && !Vfs::check_access($url,Vfs::WRITABLE,$stat)) // we are not allowed to edit it
{
if (self::LOG_LEVEL) error_log(__METHOD__."($url,$mode,$options) file can not be edited!");
if (!($options & STREAM_URL_STAT_QUIET))
{
trigger_error(__METHOD__."($url,$mode,$options) file can not be edited!",E_USER_WARNING);
}
return false;
}
if (!$read_only && self::deny_script($url))
{
if (self::LOG_LEVEL) error_log(__METHOD__."($url,$mode,$options) permission denied, file is a script!");
if (!($options & STREAM_URL_STAT_QUIET))
{
trigger_error(__METHOD__."($url,$mode,$options) permission denied, file is a script!",E_USER_WARNING);
}
return false;
}
// open the "real" file
if (!($this->opened_stream = fopen($path=Vfs::decodePath(Vfs::parse_url($url,PHP_URL_PATH)),$mode,$options)))
{
if (self::LOG_LEVEL) error_log(__METHOD__."($url,$mode,$options) fopen('$path','$mode',$options) returned false!");
return false;
}
$this->opened_stream_url = $url;
return true;
}
/**
* This method is called when the stream is closed, using fclose().
*
* You must release any resources that were locked or allocated by the stream.
*/
function stream_close ( )
{
$ret = fclose($this->opened_stream);
$this->opened_stream = null;
return $ret;
}
/**
* This method is called in response to fread() and fgets() calls on the stream.
*
* You must return up-to count bytes of data from the current read/write position as a string.
* If there are less than count bytes available, return as many as are available.
* If no more data is available, return either FALSE or an empty string.
* You must also update the read/write position of the stream by the number of bytes that were successfully read.
*
* @param int $count
* @return string/false up to count bytes read or false on EOF
*/
function stream_read ( $count )
{
return fread($this->opened_stream,$count);
}
/**
* This method is called in response to fwrite() calls on the stream.
*
* You should store data into the underlying storage used by your stream.
* If there is not enough room, try to store as many bytes as possible.
* You should return the number of bytes that were successfully stored in the stream, or 0 if none could be stored.
* You must also update the read/write position of the stream by the number of bytes that were successfully written.
*
* @param string $data
* @return integer
*/
function stream_write ( $data )
{
return fwrite($this->opened_stream,$data);
}
/**
* This method is called in response to feof() calls on the stream.
*
* Important: PHP 5.0 introduced a bug that wasn't fixed until 5.1: the return value has to be the oposite!
*
* if(version_compare(PHP_VERSION,'5.0','>=') && version_compare(PHP_VERSION,'5.1','<'))
* {
* $eof = !$eof;
* }
*
* @return boolean true if the read/write position is at the end of the stream and no more data availible, false otherwise
*/
function stream_eof ( )
{
return feof($this->opened_stream);
}
/**
* This method is called in response to ftell() calls on the stream.
*
* @return integer current read/write position of the stream
*/
function stream_tell ( )
{
return ftell($this->opened_stream);
}
/**
* This method is called in response to fseek() calls on the stream.
*
* You should update the read/write position of the stream according to offset and whence.
* See fseek() for more information about these parameters.
*
* @param integer $offset
* @param integer $whence SEEK_SET - 0 - Set position equal to offset bytes
* SEEK_CUR - 1 - Set position to current location plus offset.
* SEEK_END - 2 - Set position to end-of-file plus offset. (To move to a position before the end-of-file, you need to pass a negative value in offset.)
* @return boolean TRUE if the position was updated, FALSE otherwise.
*/
function stream_seek ( $offset, $whence )
{
return !fseek($this->opened_stream,$offset,$whence); // fseek returns 0 on success and -1 on failure
}
/**
* This method is called in response to fflush() calls on the stream.
*
* If you have cached data in your stream but not yet stored it into the underlying storage, you should do so now.
*
* @return booelan TRUE if the cached data was successfully stored (or if there was no data to store), or FALSE if the data could not be stored.
*/
function stream_flush ( )
{
return fflush($this->opened_stream);
}
/**
* This method is called in response to fstat() calls on the stream.
*
* If you plan to use your wrapper in a require_once you need to define stream_stat().
* If you plan to allow any other tests like is_file()/is_dir(), you have to define url_stat().
* stream_stat() must define the size of the file, or it will never be included.
* url_stat() must define mode, or is_file()/is_dir()/is_executable(), and any of those functions affected by clearstatcache() simply won't work.
* It's not documented, but directories must be a mode like 040777 (octal), and files a mode like 0100666.
* If you wish the file to be executable, use 7s instead of 6s.
* The last 3 digits are exactly the same thing as what you pass to chmod.
* 040000 defines a directory, and 0100000 defines a file.
*
* @return array containing the same values as appropriate for the stream.
*/
function stream_stat ( )
{
return self::url_stat($this->opened_stream_url,0);
}
/**
* This method is called in response to unlink() calls on URL paths associated with the wrapper.
*
* It should attempt to delete the item specified by path.
* In order for the appropriate error message to be returned, do not define this method if your wrapper does not support unlinking!
*
* @param string $url
* @return boolean TRUE on success or FALSE on failure
*/
static function unlink ( $url )
{
$path = Vfs::decodePath(Vfs::parse_url($url,PHP_URL_PATH));
// check access rights (file need to exist and directory need to be writable
if (!file_exists($path) || is_dir($path) || !Vfs::check_access(Vfs::dirname($url),Vfs::WRITABLE))
{
if (self::LOG_LEVEL) error_log(__METHOD__."($url) permission denied!");
return false; // no permission or file does not exist
}
return unlink($path);
}
/**
* This method is called in response to rename() calls on URL paths associated with the wrapper.
*
* It should attempt to rename the item specified by path_from to the specification given by path_to.
* In order for the appropriate error message to be returned, do not define this method if your wrapper does not support renaming.
*
* The regular filesystem stream-wrapper returns an error, if $url_from and $url_to are not either both files or both dirs!
*
* @param string $url_from
* @param string $url_to
* @return boolean TRUE on success or FALSE on failure
*/
static function rename ( $url_from, $url_to )
{
$from = Vfs::parse_url($url_from);
$to = Vfs::parse_url($url_to);
// check access rights
if (!($from_stat = self::url_stat($url_from,0)) || !Vfs::check_access(Vfs::dirname($url_from),Vfs::WRITABLE))
{
if (self::LOG_LEVEL) error_log(__METHOD__."($url_from,$url_to): $from[path] permission denied!");
return false; // no permission or file does not exist
}
$to_dir = Vfs::dirname($url_to);
if (!Vfs::check_access($to_dir,Vfs::WRITABLE,$to_dir_stat = self::url_stat($to_dir,0)))
{
if (self::LOG_LEVEL) error_log(__METHOD__."($url_from,$url_to): $to_dir permission denied!");
return false; // no permission or parent-dir does not exist
}
if (self::deny_script($url_to))
{
if (self::LOG_LEVEL) error_log(__METHOD__."($url_from,$url_to) permission denied, file is a script!");
return false;
}
// the filesystem stream-wrapper does NOT allow to rename files to directories, as this makes problems
// for our vfs too, we abort here with an error, like the filesystem one does
if (($to_stat = self::url_stat($to['path'],0)) &&
($to_stat['mime'] === self::DIR_MIME_TYPE) !== ($from_stat['mime'] === self::DIR_MIME_TYPE))
{
$is_dir = $to_stat['mime'] === self::DIR_MIME_TYPE ? 'a' : 'no';
if (self::LOG_LEVEL) error_log(__METHOD__."($url_to,$url_from) $to[path] is $is_dir directory!");
return false; // no permission or file does not exist
}
// if destination file already exists, delete it
if ($to_stat && !self::unlink($url_to))
{
if (self::LOG_LEVEL) error_log(__METHOD__."($url_to,$url_from) can't unlink existing $url_to!");
return false;
}
return rename(Vfs::decodePath($from['path']),Vfs::decodePath($to['path']));
}
/**
* This method is called in response to mkdir() calls on URL paths associated with the wrapper.
*
* It should attempt to create the directory specified by path.
* In order for the appropriate error message to be returned, do not define this method if your wrapper does not support creating directories.
*
* @param string $url
* @param int $mode not used, as we dont allow to change mode
* @param int $options Posible values include STREAM_REPORT_ERRORS and STREAM_MKDIR_RECURSIVE
* @return boolean TRUE on success or FALSE on failure
*/
static function mkdir ( $url, $mode, $options )
{
unset($mode); // not used, but required by interface
$path = Vfs::decodePath(Vfs::parse_url($url,PHP_URL_PATH));
$recursive = (bool)($options & STREAM_MKDIR_RECURSIVE);
// find the real parent (might be more then one level if $recursive!)
do {
$parent = dirname($parent ? $parent : $path);
$parent_url = Vfs::dirname($parent_url ? $parent_url : $url);
}
while ($recursive && $parent != '/' && !file_exists($parent));
//echo __METHOD__."($url,$mode,$options) path=$path, recursive=$recursive, parent=$parent, Vfs::check_access(parent_url=$parent_url,Vfs::WRITABLE)=".(int)Vfs::check_access($parent_url,Vfs::WRITABLE)."\n";
// check access rights (in real filesystem AND by mount perms)
if (file_exists($path) || !file_exists($parent) || !is_writable($parent) || !Vfs::check_access($parent_url,Vfs::WRITABLE))
{
if (self::LOG_LEVEL) error_log(__METHOD__."($url) permission denied!");
return false;
}
return mkdir($path, 0700, $recursive); // setting mode 0700 allows (only) apache to write into the dir
}
/**
* This method is called in response to rmdir() calls on URL paths associated with the wrapper.
*
* It should attempt to remove the directory specified by path.
* In order for the appropriate error message to be returned, do not define this method if your wrapper does not support removing directories.
*
* @param string $url
* @param int $options Possible values include STREAM_REPORT_ERRORS.
* @return boolean TRUE on success or FALSE on failure.
*/
static function rmdir ( $url, $options )
{
unset($options); // not used, but required by interface
$path = Vfs::decodePath(Vfs::parse_url($url,PHP_URL_PATH));
$parent = dirname($path);
// check access rights (in real filesystem AND by mount perms)
if (!file_exists($path) || !is_writable($parent) || !Vfs::check_access(Vfs::dirname($url),Vfs::WRITABLE))
{
if (self::LOG_LEVEL) error_log(__METHOD__."($url) permission denied!");
return false;
}
return rmdir($path);
}
/**
* This is not (yet) a stream-wrapper function, but it's necessary and can be used static
*
* @param string $url
* @param int $time =null modification time (unix timestamp), default null = current time
* @param int $atime =null access time (unix timestamp), default null = current time, not implemented in the vfs!
* @return boolean true on success, false otherwise
*/
static function touch($url,$time=null,$atime=null)
{
$path = Vfs::decodePath(Vfs::parse_url($url,PHP_URL_PATH));
$parent = dirname($path);
// check access rights (in real filesystem AND by mount perms)
if (!file_exists($path) || !is_writable($parent) || !Vfs::check_access(Vfs::dirname($url),Vfs::WRITABLE))
{
if (self::LOG_LEVEL) error_log(__METHOD__."($url) permission denied!");
return false;
}
return touch($path,$time,$atime);
}
/**
* This is not (yet) a stream-wrapper function, but it's necessary and can be used static
*
* Not supported, as it would require root rights!
*
* @param string $path
* @param string $mode mode string see Vfs::mode2int
* @return boolean true on success, false otherwise
*/
static function chmod($path,$mode)
{
unset($path, $mode); // not used, but required by interface
return false;
}
/**
* This is not (yet) a stream-wrapper function, but it's necessary and can be used static
*
* Not supported, as it would require root rights!
*
* @param string $path
* @param int $owner numeric user id
* @return boolean true on success, false otherwise
*/
static function chown($path,$owner)
{
unset($path, $owner); // not used, but required by interface
return false;
}
/**
* This is not (yet) a stream-wrapper function, but it's necessary and can be used static
*
* Not supported, as it would require root rights!
*
* @param string $path
* @param int $group numeric group id
* @return boolean true on success, false otherwise
*/
static function chgrp($path,$group)
{
unset($path, $group); // not used, but required by interface
return false;
}
/**
* This method is called immediately when your stream object is created for examining directory contents with opendir().
*
* @param string $url URL that was passed to opendir() and that this object is expected to explore.
* @param int $options
* @return booelan
*/
function dir_opendir ( $url, $options )
{
if (self::LOG_LEVEL > 1) error_log(__METHOD__."($url,$options)");
$this->opened_dir = null;
$path = Vfs::decodePath(Vfs::parse_url($this->opened_dir_url = $url,PHP_URL_PATH));
// ToDo: check access rights
if (!($this->opened_dir = opendir($path)))
{
if (self::LOG_LEVEL > 0) error_log(__METHOD__."($url,$options) opendir('$path') failed!");
return false;
}
return true;
}
/**
* This method is called in response to stat() calls on the URL paths associated with the wrapper.
*
* It should return as many elements in common with the system function as possible.
* Unknown or unavailable values should be set to a rational value (usually 0).
*
* If you plan to use your wrapper in a require_once you need to define stream_stat().
* If you plan to allow any other tests like is_file()/is_dir(), you have to define url_stat().
* stream_stat() must define the size of the file, or it will never be included.
* url_stat() must define mode, or is_file()/is_dir()/is_executable(), and any of those functions affected by clearstatcache() simply won't work.
* It's not documented, but directories must be a mode like 040777 (octal), and files a mode like 0100666.
* If you wish the file to be executable, use 7s instead of 6s.
* The last 3 digits are exactly the same thing as what you pass to chmod.
* 040000 defines a directory, and 0100000 defines a file.
*
* @param string $url
* @param int $flags holds additional flags set by the streams API. It can hold one or more of the following values OR'd together:
* - STREAM_URL_STAT_LINK For resources with the ability to link to other resource (such as an HTTP Location: forward,
* or a filesystem symlink). This flag specified that only information about the link itself should be returned,
* not the resource pointed to by the link.
* This flag is set in response to calls to lstat(), is_link(), or filetype().
* - STREAM_URL_STAT_QUIET If this flag is set, your wrapper should not raise any errors. If this flag is not set,
* you are responsible for reporting errors using the trigger_error() function during stating of the path.
* stat triggers it's own warning anyway, so it makes no sense to trigger one by our stream-wrapper!
* @return array
*/
static function url_stat ( $url, $flags )
{
$parts = Vfs::parse_url($url);
$path = Vfs::decodePath($parts['path']);
$stat = @stat($path); // suppressed the stat failed warnings
if ($stat)
{
// set owner, group and mode from mount options
$uid = $gid = $mode = null;
if (!self::parse_query($parts['query'],$uid,$gid,$mode))
{
return false;
}
$stat['uid'] = $stat[4] = $uid;
$stat['gid'] = $stat[5] = $gid;
$stat['mode'] = $stat[2] = $stat['mode'] & self::MODE_DIR ? self::MODE_DIR | $mode : self::MODE_FILE | ($mode & ~0111);
// write rights also depend on the write rights of the webserver
if (!is_writable($path))
{
$stat['mode'] = $stat[2] = $stat['mode'] & ~0222;
}
}
if (self::LOG_LEVEL > 1) error_log(__METHOD__."($url,$flags) path=$path, mount_mode=".sprintf('0%o',$mode).", mode=".sprintf('0%o',$stat['mode']).'='.Vfs::int2mode($stat['mode']));
return $stat;
}
/**
* This method is called in response to readdir().
*
* It should return a string representing the next filename in the location opened by dir_opendir().
*
* Unless other filesystem, we only return files readable by the user, if the dir is not writable for him.
* This is done to hide files and dirs not accessible by the user (eg. other peoples home-dirs in /home).
*
* @return string
*/
function dir_readdir ( )
{
do {
$file = readdir($this->opened_dir);
$ignore = !($file === false || // stop if no more dirs or
($file != '.' && $file != '..' )); // file not . or ..
if (self::LOG_LEVEL > 1 && $ignore) error_log(__METHOD__.'() ignoring '.array2string($file));
}
while ($ignore);
// encode special chars messing up url's
if ($file !== false) $file = Vfs::encodePathComponent($file);
if (self::LOG_LEVEL > 1) error_log(__METHOD__.'() returning '.array2string($file));
return $file;
}
/**
* This method is called in response to rewinddir().
*
* It should reset the output generated by dir_readdir(). i.e.:
* The next call to dir_readdir() should return the first entry in the location returned by dir_opendir().
*
* @return boolean
*/
function dir_rewinddir ( )
{
return rewinddir($this->opened_dir);
}
/**
* This method is called in response to closedir().
*
* You should release any resources which were locked or allocated during the opening and use of the directory stream.
*
* @return boolean
*/
function dir_closedir ( )
{
closedir($this->opened_dir);
$this->opened_dir = $this->extra_dirs = null;
return true;
}
/**
* parse a query containing mount parameters: user, uid, group, gid or mode
*
* @param string|array $query query string or array returned by parse_url (key 'query' holds the value)
* @param int &$uid default if not set is 0=root
* @param int &$gid default if not set is 0=root
* @param int &$mode default if not set is 05 r-x for others
* @return boolean true on successfull parse, false on error
*/
static function parse_query($query,&$uid,&$gid,&$mode)
{
$params = null;
parse_str(is_array($query) ? $query['query'] : $query,$params);
// setting the default perms root.root r-x for other
$uid = $gid = 0;
$mode = 05;
foreach($params as $name => $value)
{
switch($name)
{
case 'user':
if (!is_numeric($value))
{
if ($name === 'root')
{
$value = 0;
}
elseif (($value = $GLOBALS['egw']->accounts->name2id($value,'account_lid','u')) === false)
{
error_log(__METHOD__."('$query') unknown user-name '$value'!");
return false; // wrong user-name
}
}
// fall-through
case 'uid':
if (!is_numeric($value) || $value < 0 || !is_int($value) && !$GLOBALS['egw']->accounts->id2name($value))
{
error_log(__METHOD__."('$query') wrong numeric user-id '$value'!");
return false;
}
$uid = (int)$value;
break;
case 'group':
if (!is_numeric($value))
{
if ($name === 'root')
{
$value = 0;
}
elseif (($value = $GLOBALS['egw']->accounts->name2id($value,'account_lid','g')) === false)
{
error_log(__METHOD__."('$query') unknown group-name '$value'!");
return false; // wrong group-name
}
$value = -$value; // vfs uses positiv gid's!
}
// fall-through
case 'gid':
if (!is_numeric($value) || $value < 0 || !is_int($value) && !$GLOBALS['egw']->accounts->id2name(-$value))
{
error_log(__METHOD__."('$query') wrong numeric group-id '$value'!");
return false;
}
$gid = (int)$value;
break;
case 'mode':
$mode = Vfs::mode2int($value);
break;
case 'url':
// ignored, only used for download_url method
break;
default:
error_log(__METHOD__."('$query') unknown option '$name'!");
break;
}
}
//echo __METHOD__.'('.print_r($query,true).") uid=$uid, gid=$gid, mode=".sprintf('0%o',$mode)."\n";
return true;
}
/**
* Check if url is a script (self::$script_extentions) and exec mount option is NOT set
*
* @param string $url
* @return boolean true if $url is a script AND exec is NOT set, false otherwise
*/
static function deny_script($url)
{
$parts = Vfs::parse_url($url);
$get = null;
parse_str($parts['query'],$get);
$deny = !$get['exec'] && preg_match(self::SCRIPT_EXTENSIONS_PREG,$parts['path']);
if (self::LOG_LEVEL > 1 || self::LOG_LEVEL > 0 && $deny)
{
error_log(__METHOD__."($url) returning ".array2string($deny));
}
return $deny;
}
/**
* URL to download a file
*
* We use our webdav handler as download url instead of an own download method.
* The webdav hander (filemanager/webdav.php) recognices eGW's session cookie and of cause understands regular GET requests.
*
* @param string $_url
* @param boolean $force_download =false add header('Content-disposition: filename="' . basename($path) . '"'), currently not supported!
* @todo get $force_download working through webdav
* @return string|false string with full download url or false to use default webdav.php url
*/
static function download_url($_url,$force_download=false)
{
unset($force_download); // not used, but required by interface
list($url,$query) = explode('?',$_url,2);
$get = null;
parse_str($query,$get);
if (empty($get['url'])) return false; // no download url given for this mount-point
if (!($mount_url = Vfs::mount_url($_url))) return false; // no mount url found, should not happen
list($mount_url) = explode('?',$mount_url);
$relpath = substr($url,strlen($mount_url));
$download_url = Vfs::concat($get['url'],$relpath);
if ($download_url[0] == '/')
{
$download_url = ($_SERVER['HTTPS'] ? 'https://' : 'http://').
$_SERVER['HTTP_HOST'].$download_url;
}
//die(__METHOD__."('$url') --> relpath = $relpath --> $download_url");
return $download_url;
}
/**
* Register our stream-wrapper
*/
public static function register()
{
stream_register_wrapper(self::SCHEME, __CLASS__);
}
}
StreamWrapper::register();

View File

@ -0,0 +1,336 @@
<?php
/**
* eGroupWare API: VFS - stream wrapper for linked files
*
* @link http://www.egroupware.org
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @package api
* @subpackage vfs
* @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
* @copyright (c) 2008-15 by Ralf Becker <RalfBecker-AT-outdoor-training.de>
* @version $Id: class.sqlfs_stream_wrapper.inc.php 24997 2008-03-02 21:44:15Z ralfbecker $
*/
namespace EGroupware\Api\Vfs\Links;
use EGroupware\Api\Vfs;
// explicitly import old phpgwapi classes used:
use egw_link;
use addressbook_vcal;
/**
* Define parent for Vfs\Links\StreamWrapper, if not already defined
*
* Allows to base Vfs\Links\StreamWrapper on an other wrapper
*/
if (!class_exists('EGroupware\\Api\\Vfs\\Links\\LinksParent', false))
{
class LinksParent extends Vfs\Sqlfs\StreamWrapper {}
}
/**
* EGroupware API: stream wrapper for linked files
*
* The files stored by the sqlfs_stream_wrapper in a /apps/$app/$id directory
*
* The links stream wrapper extends the sqlfs one, to implement an own ACL based on the access
* of the entry the files are linked to.
*
* Applications can define a 'file_access' method in the link registry with the following signature:
*
* boolean function file_access(string $id,int $check,string $rel_path)
*
* If the do not implement such a function the title function is used to test if the user has
* at least read access to an entry, and if true full (write) access to the files is granted.
*
* Entry directories are always reported existing and empty, if not existing in sqlfs.
*
* The stream wrapper interface is according to the docu on php.net
*
* @link http://www.php.net/manual/en/function.stream-wrapper-register.php
*/
class StreamWrapper extends LinksParent
{
/**
* Scheme / protocoll used for this stream-wrapper
*/
const SCHEME = 'links';
/**
* Prefix to predend to get an url from a path
*/
const PREFIX = 'links://default';
/**
* Base url to store links
*/
const BASEURL = 'links://default/apps';
/**
* Enable some debug output to the error_log
*/
const DEBUG = false;
/**
* Implements ACL based on the access of the user to the entry the files are linked to.
*
* @param string $url url to check
* @param int $check mode to check: one or more or'ed together of: 4 = read, 2 = write, 1 = executable
* @return boolean
*/
static function check_extended_acl($url,$check)
{
if (Vfs::$is_root)
{
return true;
}
$path = Vfs::parse_url($url,PHP_URL_PATH);
list(,$apps,$app,$id,$rel_path) = explode('/',$path,5);
if ($apps != 'apps')
{
$access = false; // no access to anything, but /apps
$what = '!= apps';
}
elseif (!$app)
{
$access = !($check & Vfs::WRITABLE); // always grant read access to /apps
$what = '!$app';
}
elseif(!isset($GLOBALS['egw_info']['user']['apps'][$app]))
{
$access = false; // user has no access to the $app application
$what = 'no app-rights';
}
elseif (!$id)
{
$access = true; // grant read&write access to /apps/$app
$what = 'app dir';
}
// allow applications to implement their own access control to the file storage
// otherwise use the title method to check if user has (at least read access) to the entry
// which gives him then read AND write access to the file store of the entry
else
{
// vfs & stream-wrapper use posix rights, egw_link::file_access uses EGW_ACL_{EDIT|READ}!
$required = $check & Vfs::WRITABLE ? EGW_ACL_EDIT : EGW_ACL_READ;
$access = egw_link::file_access($app,$id,$required,$rel_path,Vfs::$user);
$what = "from egw_link::file_access('$app',$id,$required,'$rel_path,".Vfs::$user.")";
}
if (self::DEBUG) error_log(__METHOD__."($url,$check) user=".Vfs::$user." ($what) ".($access?"access granted ($app:$id:$rel_path)":'no access!!!'));
return $access;
}
/**
* This method is called in response to stat() calls on the URL paths associated with the wrapper.
*
* Reimplemented from sqlfs, as we have to pass the value of check_extends_acl(), due to the lack of late static binding.
* And to return vcard for url /apps/addressbook/$id/.entry
*
* @param string $url
* @param int $flags holds additional flags set by the streams API. It can hold one or more of the following values OR'd together:
* - STREAM_URL_STAT_LINK For resources with the ability to link to other resource (such as an HTTP Location: forward,
* or a filesystem symlink). This flag specified that only information about the link itself should be returned,
* not the resource pointed to by the link.
* This flag is set in response to calls to lstat(), is_link(), or filetype().
* - STREAM_URL_STAT_QUIET If this flag is set, your wrapper should not raise any errors. If this flag is not set,
* you are responsible for reporting errors using the trigger_error() function during stating of the path.
* stat triggers it's own warning anyway, so it makes no sense to trigger one by our stream-wrapper!
* @return array
*/
static function url_stat ( $url, $flags )
{
$eacl_check=self::check_extended_acl($url,Vfs::READABLE);
// return vCard as /.entry
if ( $eacl_check && substr($url,-7) == '/.entry' &&
(list($app) = array_slice(explode('/',$url),-3,1)) && $app === 'addressbook')
{
$ret = array(
'ino' => md5($url),
'name' => '.entry',
'mode' => self::MODE_FILE|Vfs::READABLE, // required by the stream wrapper
'size' => 1024, // fmail does NOT attach files with size 0!
'uid' => 0,
'gid' => 0,
'mtime' => time(),
'ctime' => time(),
'nlink' => 1,
// eGW addition to return some extra values
'mime' => $app == 'addressbook' ? 'text/vcard' : 'text/calendar',
);
}
// if entry directory does not exist --> return fake directory
elseif (!($ret = parent::url_stat($url,$flags,$eacl_check)) && $eacl_check)
{
list(,/*$apps*/,/*$app*/,$id,$rel_path) = explode('/', Vfs::parse_url($url, PHP_URL_PATH), 5);
if ($id && !isset($rel_path))
{
$ret = array(
'ino' => md5($url),
'name' => $id,
'mode' => self::MODE_DIR, // required by the stream wrapper
'size' => 0,
'uid' => 0,
'gid' => 0,
'mtime' => time(),
'ctime' => time(),
'nlink' => 2,
// eGW addition to return some extra values
'mime' => Vfs::DIR_MIME_TYPE,
);
}
}
if (self::DEBUG) error_log(__METHOD__."('$url', $flags) calling parent::url_stat(,,".array2string($eacl_check).') returning '.array2string($ret));
return $ret;
}
/**
* Set or delete extended acl for a given path and owner (or delete them if is_null($rights)
*
* Reimplemented, to NOT call the sqlfs functions, as we dont allow to modify the ACL (defined by the apps)
*
* @param string $path string with path
* @param int $rights =null rights to set, or null to delete the entry
* @param int/boolean $owner =null owner for whom to set the rights, null for the current user, or false to delete all rights for $path
* @param int $fs_id =null fs_id to use, to not query it again (eg. because it's already deleted)
* @return boolean true if acl is set/deleted, false on error
*/
static function eacl($path,$rights=null,$owner=null,$fs_id=null)
{
unset($path, $rights, $owner, $fs_id); // not used, but required by function signature
return false;
}
/**
* Get all ext. ACL set for a path
*
* Reimplemented, to NOT call the sqlfs functions, as we dont allow to modify the ACL (defined by the apps)
*
* @param string $path
* @return array/boolean array with array('path'=>$path,'owner'=>$owner,'rights'=>$rights) or false if $path not found
*/
function get_eacl($path)
{
unset($path); // not used, but required by function signature
return false;
}
/**
* mkdir for links
*
* Reimplemented as we have no static late binding to allow the extended sqlfs to call our eacl and to set no default rights for entry dirs
*
* This method is called in response to mkdir() calls on URL paths associated with the wrapper.
*
* It should attempt to create the directory specified by path.
* In order for the appropriate error message to be returned, do not define this method if your wrapper does not support creating directories.
*
* @param string $path
* @param int $mode not used(!), we inherit 005 for /apps/$app and set 000 for /apps/$app/$id
* @param int $options Posible values include STREAM_REPORT_ERRORS and STREAM_MKDIR_RECURSIVE, we allways use recursive!
* @return boolean TRUE on success or FALSE on failure
*/
static function mkdir($path,$mode,$options)
{
unset($mode); // not used, but required by function signature
if($path[0] != '/')
{
if (strpos($path,'?') !== false) $query = Vfs::parse_url($path,PHP_URL_QUERY);
$path = Vfs::parse_url($path,PHP_URL_PATH).($query ? '?'.$query : '');
}
list(,$apps,$app,$id) = explode('/',$path);
$ret = false;
if ($apps == 'apps' && $app && !$id || self::check_extended_acl($path,Vfs::WRITABLE)) // app directory itself is allways ok
{
$current_is_root = Vfs::$is_root; Vfs::$is_root = true;
$current_user = Vfs::$user; Vfs::$user = 0;
$ret = parent::mkdir($path,0,$options|STREAM_MKDIR_RECURSIVE);
if ($id) parent::chmod($path,0); // no other rights
Vfs::$user = $current_user;
Vfs::$is_root = $current_is_root;
}
//error_log(__METHOD__."($path,$mode,$options) apps=$apps, app=$app, id=$id: returning $ret");
return $ret;
}
/**
* This method is called immediately after your stream object is created.
*
* Reimplemented from sqlfs to ensure self::url_stat is called, to fill sqlfs stat cache with our eacl!
* And to return vcard for url /apps/addressbook/$id/.entry
*
* @param string $url URL that was passed to fopen() and that this object is expected to retrieve
* @param string $mode mode used to open the file, as detailed for fopen()
* @param int $options additional flags set by the streams API (or'ed together):
* - STREAM_USE_PATH If path is relative, search for the resource using the include_path.
* - STREAM_REPORT_ERRORS If this flag is set, you are responsible for raising errors using trigger_error() during opening of the stream.
* If this flag is not set, you should not raise any errors.
* @param string $opened_path full path of the file/resource, if the open was successfull and STREAM_USE_PATH was set
* @return boolean true if the ressource was opened successful, otherwise false
*/
function stream_open ( $url, $mode, $options, &$opened_path )
{
// the following call is necessary to fill sqlfs_stream_wrapper::$stat_cache, WITH the extendes ACL!
$stat = self::url_stat($url,0);
//error_log(__METHOD__."('$url', '$mode', $options) stat=".array2string($stat));
// return vCard as /.entry
if ($stat && $mode[0] == 'r' && substr($url,-7) === '/.entry' &&
(list($app) = array_slice(explode('/',$url),-3,1)) && $app === 'addressbook')
{
list($id) = array_slice(explode('/',$url),-2,1);
$ab_vcard = new addressbook_vcal('addressbook','text/vcard');
if (!($vcard =& $ab_vcard->getVCard($id)))
{
error_log(__METHOD__."('$url', '$mode', $options) addressbook_vcal::getVCard($id) returned false!");
return false;
}
//error_log(__METHOD__."('$url', '$mode', $options) addressbook_vcal::getVCard($id) returned ".$GLOBALS[$name]);
$this->opened_stream = fopen('php://temp', 'wb');
fwrite($this->opened_stream, $vcard);
fseek($this->opened_stream, 0, SEEK_SET);
return true;
}
// create not existing entry directories on the fly
if ($mode[0] != 'r' && !parent::url_stat($dir = Vfs::dirname($url),0) && self::check_extended_acl($dir,Vfs::WRITABLE))
{
self::mkdir($dir,0,STREAM_MKDIR_RECURSIVE);
}
return parent::stream_open($url,$mode,$options,$opened_path);
}
/**
* This method is called immediately when your stream object is created for examining directory contents with opendir().
*
* Reimplemented to give no error, if entry directory does not exist.
*
* @param string $url URL that was passed to opendir() and that this object is expected to explore.
* @param $options
* @return booelan
*/
function dir_opendir ( $url, $options )
{
if (!parent::url_stat($url, STREAM_URL_STAT_QUIET) && self::url_stat($url, STREAM_URL_STAT_QUIET))
{
$this->opened_dir = array();
return true;
}
return parent::dir_opendir($url, $options);
}
/**
* Register this stream-wrapper
*/
public static function register()
{
stream_register_wrapper(self::SCHEME, __CLASS__);
}
}
StreamWrapper::register();

File diff suppressed because it is too large Load Diff

490
api/src/Vfs/Sqlfs/Utils.php Normal file
View File

@ -0,0 +1,490 @@
<?php
/**
* EGroupware API: sqlfs stream wrapper utilities: migration db-fs, fsck
*
* @link http://www.egroupware.org
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @package api
* @subpackage vfs
* @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
* @copyright (c) 2008-15 by Ralf Becker <RalfBecker-AT-outdoor-training.de>
* @version $Id$
*/
namespace EGroupware\Api\Vfs\Sqlfs;
use EGroupware\Api\Vfs;
// explicitly import old phpgwapi classes used:
use mime_magic;
use egw_exception_assertion_failed;
use schema_proc;
/**
* sqlfs stream wrapper utilities: migration db-fs, fsck
*/
class Utils extends StreamWrapper
{
/**
* Migrate SQLFS content from DB to filesystem
*
* @param boolean $debug true to echo a message for each copied file
*/
static function migrate_db2fs($debug=true)
{
if (!is_object(self::$pdo))
{
self::_pdo();
}
$query = 'SELECT fs_id,fs_name,fs_size,fs_content'.
$query = 'SELECT fs_id,fs_name,fs_size,fs_content'.
' FROM '.self::TABLE.' WHERE fs_content IS NOT NULL'.
' ORDER BY fs_id LIMIT 5 OFFSET :offset';
$fs_id = $fs_name = $fs_size = $fs_content = null;
$n = 0;
$stmt = self::$pdo->prepare($query);
$stmt->bindColumn(1,$fs_id);
$stmt->bindColumn(2,$fs_name);
$stmt->bindColumn(3,$fs_size);
$stmt->bindColumn(4,$fs_content,PDO::PARAM_LOB);
$stmt->bindValue(':offset', $n, PDO::PARAM_INT);
while ($stmt->execute())
{
foreach($stmt as $row)
{
// hack to work around a current php bug (http://bugs.php.net/bug.php?id=40913)
// PDOStatement::bindColumn(,,\PDO::PARAM_LOB) is not working for MySQL, content is returned as string :-(
if (is_string($fs_content))
{
$content = fopen('php://temp', 'wb');
fwrite($content, $fs_content);
fseek($content, 0, SEEK_SET);
unset($fs_content);
}
else
{
$content = $fs_content;
}
if (!is_resource($content))
{
throw new egw_exception_assertion_failed(__METHOD__."(): fs_id=$fs_id ($fs_name, $fs_size bytes) content is NO resource! ".array2string($content));
}
$filename = self::_fs_path($fs_id);
if (!file_exists($fs_dir=Vfs::dirname($filename)))
{
self::mkdir_recursive($fs_dir,0700,true);
}
if (!($dest = fopen($filename,'w')))
{
throw new egw_exception_assertion_failed(__METHOD__."(): fopen($filename,'w') failed!");
}
if (($bytes = stream_copy_to_stream($content,$dest)) != $fs_size)
{
throw new egw_exception_assertion_failed(__METHOD__."(): fs_id=$fs_id ($fs_name) $bytes bytes copied != size of $fs_size bytes!");
}
if ($debug) error_log("$fs_id: $fs_name: $bytes bytes copied to fs");
fclose($dest);
fclose($content); unset($content);
++$n;
}
if (!$n) break; // just in case nothing is found, statement will execute just fine
$stmt->bindValue(':offset', $n, PDO::PARAM_INT);
}
unset($row); // not used, as we access bound variables
unset($stmt);
if ($n) // delete all content in DB, if there was some AND no error (exception thrown!)
{
$query = 'UPDATE '.self::TABLE.' SET fs_content=NULL';
$stmt = self::$pdo->prepare($query);
$stmt->execute();
}
return $n;
}
/**
* Check and optionaly fix corruption in sqlfs
*
* @param boolean $check_only =true
* @return array with messages / found problems
*/
public static function fsck($check_only=true)
{
if (!is_object(self::$pdo))
{
self::_pdo();
}
$msgs = array();
foreach(array(
self::fsck_fix_required_nodes($check_only),
self::fsck_fix_multiple_active($check_only),
self::fsck_fix_unconnected($check_only),
self::fsck_fix_no_content($check_only),
) as $check_msgs)
{
if ($check_msgs) $msgs = array_merge($msgs, $check_msgs);
}
foreach ($GLOBALS['egw']->hooks->process(array(
'location' => 'fsck',
'check_only' => $check_only)
) as $app_msgs)
{
if ($app_msgs) $msgs = array_merge($msgs, $app_msgs);
}
return $msgs;
}
/**
* Check and optionally create required nodes: /, /home, /apps
*
* @param boolean $check_only =true
* @return array with messages / found problems
*/
private static function fsck_fix_required_nodes($check_only=true)
{
static $dirs = array(
'/' => 1,
'/home' => 2,
'/apps' => 3,
);
$stmt = $delete_stmt = null;
$msgs = array();
foreach($dirs as $path => $id)
{
if (!($stat = self::url_stat($path, STREAM_URL_STAT_LINK)))
{
if ($check_only)
{
$msgs[] = lang('Required directory "%1" not found!', $path);
}
else
{
if (!isset($stmt))
{
$stmt = self::$pdo->prepare('INSERT INTO '.self::TABLE.' (fs_id,fs_name,fs_dir,fs_mode,fs_uid,fs_gid,fs_size,fs_mime,fs_created,fs_modified,fs_creator'.
') VALUES (:fs_id,:fs_name,:fs_dir,:fs_mode,:fs_uid,:fs_gid,:fs_size,:fs_mime,:fs_created,:fs_modified,:fs_creator)');
}
try {
$ok = $stmt->execute($data = array(
'fs_id' => $id,
'fs_name' => substr($path,1),
'fs_dir' => $path == '/' ? 0 : $dirs['/'],
'fs_mode' => 05,
'fs_uid' => 0,
'fs_gid' => 0,
'fs_size' => 0,
'fs_mime' => 'httpd/unix-directory',
'fs_created' => self::_pdo_timestamp(time()),
'fs_modified' => self::_pdo_timestamp(time()),
'fs_creator' => 0,
));
}
catch (\PDOException $e)
{
$ok = false;
unset($e); // ignore exception
}
if (!$ok) // can not insert it, try deleting it first
{
if (!isset($delete_stmt))
{
$delete_stmt = self::$pdo->prepare('DELETE FROM '.self::TABLE.' WHERE fs_id=:fs_id');
}
try {
$ok = $delete_stmt->execute(array('fs_id' => $id)) && $stmt->execute($data);
}
catch (\PDOException $e)
{
unset($e); // ignore exception
}
}
$msgs[] = $ok ? lang('Required directory "%1" created.', $path) :
lang('Failed to create required directory "%1"!', $path);
}
}
// check if directory is at least world readable and executable (r-x), we allow more but not less
elseif (($stat['mode'] & 05) != 05)
{
if ($check_only)
{
$msgs[] = lang('Required directory "%1" has wrong mode %2 instead of %3!',
$path, Vfs::int2mode($stat['mode']), Vfs::int2mode(05|0x4000));
}
else
{
$stmt = self::$pdo->prepare('UPDATE '.self::TABLE.' SET fs_mode=:fs_mode WHERE fs_id=:fs_id');
if (($ok = $stmt->execute(array(
'fs_id' => $id,
'fs_mode' => 05,
))))
{
$msgs[] = lang('Mode of required directory "%1" changed to %2.', $path, Vfs::int2mode(05|0x4000));
}
else
{
$msgs[] = lang('Failed to change mode of required directory "%1" to %2!', $path, Vfs::int2mode(05|0x4000));
}
}
}
}
if (!$check_only && $msgs)
{
global $oProc;
if (!isset($oProc)) $oProc = new schema_proc();
// PostgreSQL seems to require to update the sequenz, after manually inserting id's
$oProc->UpdateSequence('egw_sqlfs', 'fs_id');
}
return $msgs;
}
/**
* Check and optionally remove files without content part in physical filesystem
*
* @param boolean $check_only =true
* @return array with messages / found problems
*/
private static function fsck_fix_no_content($check_only=true)
{
$stmt = null;
$msgs = array();
foreach(self::$pdo->query('SELECT fs_id FROM '.self::TABLE.
" WHERE fs_mime!='httpd/unix-directory' AND fs_content IS NULL AND fs_link IS NULL") as $row)
{
if (!file_exists($phy_path=self::_fs_path($row['fs_id'])))
{
$path = self::id2path($row['fs_id']);
if ($check_only)
{
$msgs[] = lang('File %1 has no content in physical filesystem %2!',
$path.' (#'.$row['fs_id'].')',$phy_path);
}
else
{
if (!isset($stmt))
{
$stmt = self::$pdo->prepare('DELETE FROM '.self::TABLE.' WHERE fs_id=:fs_id');
$stmt_props = self::$pdo->prepare('DELETE FROM '.self::PROPS_TABLE.' WHERE fs_id=:fs_id');
}
if ($stmt->execute(array('fs_id' => $row['fs_id'])) &&
$stmt_props->execute(array('fs_id' => $row['fs_id'])))
{
$msgs[] = lang('File %1 has no content in physical filesystem %2 --> file removed!',$path,$phy_path);
}
else
{
$msgs[] = lang('File %1 has no content in physical filesystem %2 --> failed to remove file!',
$path.' (#'.$row['fs_id'].')',$phy_path);
}
}
}
}
if ($check_only && $msgs)
{
$msgs[] = lang('Files without content in physical filesystem will be removed.');
}
return $msgs;
}
/**
* Name of lost+found directory for unconnected nodes
*/
const LOST_N_FOUND = '/lost+found';
const LOST_N_FOUND_MOD = 070;
const LOST_N_FOUND_GRP = 'Admins';
/**
* Check and optionally fix unconnected nodes - parent directory does not (longer) exists:
*
* SELECT fs.*
* FROM egw_sqlfs fs
* LEFT JOIN egw_sqlfs dir ON dir.fs_id=fs.fs_dir
* WHERE fs.fs_id > 1 && dir.fs_id IS NULL
*
* @param boolean $check_only =true
* @return array with messages / found problems
*/
private static function fsck_fix_unconnected($check_only=true)
{
$lostnfound = null;
$msgs = array();
foreach(self::$pdo->query('SELECT fs.* FROM '.self::TABLE.' fs'.
' LEFT JOIN '.self::TABLE.' dir ON dir.fs_id=fs.fs_dir'.
' WHERE fs.fs_id > 1 AND dir.fs_id IS NULL') as $row)
{
if ($check_only)
{
$msgs[] = lang('Found unconnected %1 %2!',
mime_magic::mime2label($row['fs_mime']),
Vfs::decodePath($row['fs_name']).' (#'.$row['fs_id'].')');
continue;
}
if (!isset($lostnfound))
{
// check if we already have /lost+found, create it if not
if (!($lostnfound = self::url_stat(self::LOST_N_FOUND, STREAM_URL_STAT_QUIET)))
{
Vfs::$is_root = true;
if (!self::mkdir(self::LOST_N_FOUND, self::LOST_N_FOUND_MOD, 0) ||
!(!($admins = $GLOBALS['egw']->accounts->name2id(self::LOST_N_FOUND_GRP)) ||
self::chgrp(self::LOST_N_FOUND, $admins) && self::chmod(self::LOST_N_FOUND,self::LOST_N_FOUND_MOD)) ||
!($lostnfound = self::url_stat(self::LOST_N_FOUND, STREAM_URL_STAT_QUIET)))
{
$msgs[] = lang("Can't create directory %1 to connect found unconnected nodes to it!",self::LOST_N_FOUND);
}
else
{
$msgs[] = lang('Successful created new directory %1 for unconnected nods.',self::LOST_N_FOUND);
}
Vfs::$is_root = false;
if (!$lostnfound) break;
}
$stmt = self::$pdo->prepare('UPDATE '.self::TABLE.' SET fs_dir=:fs_dir WHERE fs_id=:fs_id');
}
if ($stmt->execute(array(
'fs_dir' => $lostnfound['ino'],
'fs_id' => $row['fs_id'],
)))
{
$msgs[] = lang('Moved unconnected %1 %2 to %3.',
mime_magic::mime2label($row['fs_mime']),
Vfs::decodePath($row['fs_name']).' (#'.$row['fs_id'].')',
self::LOST_N_FOUND);
}
else
{
$msgs[] = lang('Failed to move unconnected %1 %2 to %3!',
mime_magic::mime2label($row['fs_mime']), Vfs::decodePath($row['fs_name']), self::LOST_N_FOUND);
}
}
if ($check_only && $msgs)
{
$msgs[] = lang('Unconnected nodes will be moved to %1.',self::LOST_N_FOUND);
}
return $msgs;
}
/**
* Check and optionally fix multiple active files and directories with identical path
*
* @param boolean $check_only =true
* @return array with messages / found problems
*/
private static function fsck_fix_multiple_active($check_only=true)
{
$stmt = $inactivate_msg_added = null;
$msgs = array();
foreach(self::$pdo->query('SELECT fs_dir,fs_name,COUNT(*) FROM '.self::TABLE.
' WHERE fs_active='.self::_pdo_boolean(true).
' GROUP BY fs_dir,'.(self::$pdo_type == 'mysql' ? 'BINARY ' : '').'fs_name'. // fs_name is casesensitive!
' HAVING COUNT(*) > 1') as $row)
{
if (!isset($stmt))
{
$stmt = self::$pdo->prepare('SELECT *,(SELECT COUNT(*) FROM '.self::TABLE.' sub WHERE sub.fs_dir=fs.fs_id) AS children'.
' FROM '.self::TABLE.' fs'.
' WHERE fs.fs_dir=:fs_dir AND fs.fs_active='.self::_pdo_boolean(true).' AND fs.fs_name'.self::$case_sensitive_equal.':fs_name'.
" ORDER BY fs.fs_mime='httpd/unix-directory' DESC,children DESC,fs.fs_modified DESC");
$inactivate_stmt = self::$pdo->prepare('UPDATE '.self::TABLE.
' SET fs_active='.self::_pdo_boolean(false).
' WHERE fs_dir=:fs_dir AND fs_active='.self::_pdo_boolean(true).
' AND fs_name'.self::$case_sensitive_equal.':fs_name AND fs_id!=:fs_id');
}
//$msgs[] = array2string($row);
$cnt = 0;
$stmt->execute(array(
'fs_dir' => $row['fs_dir'],
'fs_name' => $row['fs_name'],
));
foreach($stmt as $n => $entry)
{
if ($entry['fs_mime'] == 'httpd/unix-directory')
{
if (!$n)
{
$dir = $entry; // directory to keep
$msgs[] = lang('%1 directories %2 found!', $row[2], self::id2path($entry['fs_id']));
if ($check_only) break;
}
else
{
if ($entry['children'])
{
$msgs[] = lang('Moved %1 children from directory fs_id=%2 to %3',
$children = self::$pdo->exec('UPDATE '.self::TABLE.' SET fs_dir='.(int)$dir['fs_id'].
' WHERE fs_dir='.(int)$entry['fs_id']),
$entry['fs_id'], $dir['fs_id']);
$dir['children'] += $children;
}
self::$pdo->query('DELETE FROM '.self::TABLE.' WHERE fs_id='.(int)$entry['fs_id']);
$msgs[] = lang('Removed (now) empty directory fs_id=%1',$entry['fs_id']);
}
}
elseif (isset($dir)) // file and directory with same name exist!
{
if (!$check_only)
{
$inactivate_stmt->execute(array(
'fs_dir' => $row['fs_dir'],
'fs_name' => $row['fs_name'],
'fs_id' => $dir['fs_id'],
));
$cnt = $inactivate_stmt->rowCount();
}
else
{
$cnt = ucfirst(lang('none of %1', $row[2]-1));
}
$msgs[] = lang('%1 active file(s) with same name as directory inactivated!',$cnt);
break;
}
else // newest file --> set for all other fs_active=false
{
if (!$check_only)
{
$inactivate_stmt->execute(array(
'fs_dir' => $row['fs_dir'],
'fs_name' => $row['fs_name'],
'fs_id' => $entry['fs_id'],
));
$cnt = $inactivate_stmt->rowCount();
}
else
{
$cnt = lang('none of %1', $row[2]-1);
}
$msgs[] = lang('More then one active file %1 found, inactivating %2 older revisions!',
self::id2path($entry['fs_id']), $cnt);
break;
}
}
unset($dir);
if ($cnt && !isset($inactivate_msg_added))
{
$msgs[] = lang('To examine or reinstate inactived files, you might need to turn versioning on.');
$inactivate_msg_added = true;
}
}
return $msgs;
}
}
// fsck testcode, if this file is called via it's URL (you need to uncomment it!)
/*if (isset($_SERVER['SCRIPT_FILENAME']) && $_SERVER['SCRIPT_FILENAME'] == __FILE__)
{
$GLOBALS['egw_info'] = array(
'flags' => array(
'currentapp' => 'admin',
'nonavbar' => true,
),
);
include_once '../../header.inc.php';
$msgs = Utils::fsck(!isset($_GET['check_only']) || $_GET['check_only']);
echo '<p>'.implode("</p>\n<p>", (array)$msgs)."</p>\n";
}*/

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,250 @@
<?php
/**
* EGroupware API: VFS - stream wrapper interface
*
* @link http://www.egroupware.org
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @package api
* @subpackage vfs
* @version $Id$
*/
namespace EGroupware\Api\Vfs;
/**
* VFS - stream wrapper interface
*
* The interface is according to the docu on php.net
*
* @link http://www.php.net/manual/en/function.stream-wrapper-register.php
*/
interface StreamWrapperIface
{
/**
* optional context param when opening the stream, null if no context passed
*
* @var mixed
*/
//var $context;
/**
* This method is called immediately after your stream object is created.
*
* @param string $path URL that was passed to fopen() and that this object is expected to retrieve
* @param string $mode mode used to open the file, as detailed for fopen()
* @param int $options additional flags set by the streams API (or'ed together):
* - STREAM_USE_PATH If path is relative, search for the resource using the include_path.
* - STREAM_REPORT_ERRORS If this flag is set, you are responsible for raising errors using trigger_error() during opening of the stream.
* If this flag is not set, you should not raise any errors.
* @param string $opened_path full path of the file/resource, if the open was successfull and STREAM_USE_PATH was set
* @return boolean true if the ressource was opened successful, otherwise false
*/
function stream_open ( $path, $mode, $options, &$opened_path );
/**
* This method is called when the stream is closed, using fclose().
*
* You must release any resources that were locked or allocated by the stream.
*/
function stream_close ( );
/**
* This method is called in response to fread() and fgets() calls on the stream.
*
* You must return up-to count bytes of data from the current read/write position as a string.
* If there are less than count bytes available, return as many as are available.
* If no more data is available, return either FALSE or an empty string.
* You must also update the read/write position of the stream by the number of bytes that were successfully read.
*
* @param int $count
* @return string/false up to count bytes read or false on EOF
*/
function stream_read ( $count );
/**
* This method is called in response to fwrite() calls on the stream.
*
* You should store data into the underlying storage used by your stream.
* If there is not enough room, try to store as many bytes as possible.
* You should return the number of bytes that were successfully stored in the stream, or 0 if none could be stored.
* You must also update the read/write position of the stream by the number of bytes that were successfully written.
*
* @param string $data
* @return integer
*/
function stream_write ( $data );
/**
* This method is called in response to feof() calls on the stream.
*
* Important: PHP 5.0 introduced a bug that wasn't fixed until 5.1: the return value has to be the oposite!
*
* if(version_compare(PHP_VERSION,'5.0','>=') && version_compare(PHP_VERSION,'5.1','<'))
* {
* $eof = !$eof;
* }
*
* @return boolean true if the read/write position is at the end of the stream and no more data availible, false otherwise
*/
function stream_eof ( );
/**
* This method is called in response to ftell() calls on the stream.
*
* @return integer current read/write position of the stream
*/
function stream_tell ( );
/**
* This method is called in response to fseek() calls on the stream.
*
* You should update the read/write position of the stream according to offset and whence.
* See fseek() for more information about these parameters.
*
* @param integer $offset
* @param integer $whence SEEK_SET - Set position equal to offset bytes
* SEEK_CUR - Set position to current location plus offset.
* SEEK_END - Set position to end-of-file plus offset. (To move to a position before the end-of-file, you need to pass a negative value in offset.)
* @return boolean TRUE if the position was updated, FALSE otherwise.
*/
function stream_seek ( $offset, $whence );
/**
* This method is called in response to fflush() calls on the stream.
*
* If you have cached data in your stream but not yet stored it into the underlying storage, you should do so now.
*
* @return booelan TRUE if the cached data was successfully stored (or if there was no data to store), or FALSE if the data could not be stored.
*/
function stream_flush ( );
/**
* This method is called in response to fstat() calls on the stream.
*
* If you plan to use your wrapper in a require_once you need to define stream_stat().
* If you plan to allow any other tests like is_file()/is_dir(), you have to define url_stat().
* stream_stat() must define the size of the file, or it will never be included.
* url_stat() must define mode, or is_file()/is_dir()/is_executable(), and any of those functions affected by clearstatcache() simply won't work.
* It's not documented, but directories must be a mode like 040777 (octal), and files a mode like 0100666.
* If you wish the file to be executable, use 7s instead of 6s.
* The last 3 digits are exactly the same thing as what you pass to chmod.
* 040000 defines a directory, and 0100000 defines a file.
*
* @return array containing the same values as appropriate for the stream.
*/
function stream_stat ( );
/**
* This method is called in response to unlink() calls on URL paths associated with the wrapper.
*
* It should attempt to delete the item specified by path.
* In order for the appropriate error message to be returned, do not define this method if your wrapper does not support unlinking!
*
* @param string $path
* @return boolean TRUE on success or FALSE on failure
*/
static function unlink ( $path );
/**
* This method is called in response to rename() calls on URL paths associated with the wrapper.
*
* It should attempt to rename the item specified by path_from to the specification given by path_to.
* In order for the appropriate error message to be returned, do not define this method if your wrapper does not support renaming.
*
* The regular filesystem stream-wrapper returns an error, if $url_from and $url_to are not either both files or both dirs!
*
* @param string $path_from
* @param string $path_to
* @return boolean TRUE on success or FALSE on failure
*/
static function rename ( $path_from, $path_to );
/**
* This method is called in response to mkdir() calls on URL paths associated with the wrapper.
*
* It should attempt to create the directory specified by path.
* In order for the appropriate error message to be returned, do not define this method if your wrapper does not support creating directories.
*
* @param string $path
* @param int $mode
* @param int $options Posible values include STREAM_REPORT_ERRORS and STREAM_MKDIR_RECURSIVE
* @return boolean TRUE on success or FALSE on failure
*/
static function mkdir ( $path, $mode, $options );
/**
* This method is called in response to rmdir() calls on URL paths associated with the wrapper.
*
* It should attempt to remove the directory specified by path.
* In order for the appropriate error message to be returned, do not define this method if your wrapper does not support removing directories.
*
* @param string $path
* @param int $options Possible values include STREAM_REPORT_ERRORS.
* @return boolean TRUE on success or FALSE on failure.
*/
static function rmdir ( $path, $options );
/**
* This method is called immediately when your stream object is created for examining directory contents with opendir().
*
* @param string $path URL that was passed to opendir() and that this object is expected to explore.
* @return booelan
*/
function dir_opendir ( $path, $options );
/**
* This method is called in response to stat() calls on the URL paths associated with the wrapper.
*
* It should return as many elements in common with the system function as possible.
* Unknown or unavailable values should be set to a rational value (usually 0).
*
* If you plan to use your wrapper in a require_once you need to define stream_stat().
* If you plan to allow any other tests like is_file()/is_dir(), you have to define url_stat().
* stream_stat() must define the size of the file, or it will never be included.
* url_stat() must define mode, or is_file()/is_dir()/is_executable(), and any of those functions affected by clearstatcache() simply won't work.
* It's not documented, but directories must be a mode like 040777 (octal), and files a mode like 0100666.
* If you wish the file to be executable, use 7s instead of 6s.
* The last 3 digits are exactly the same thing as what you pass to chmod.
* 040000 defines a directory, and 0100000 defines a file.
*
* @param string $path
* @param int $flags holds additional flags set by the streams API. It can hold one or more of the following values OR'd together:
* - STREAM_URL_STAT_LINK For resources with the ability to link to other resource (such as an HTTP Location: forward,
* or a filesystem symlink). This flag specified that only information about the link itself should be returned,
* not the resource pointed to by the link.
* This flag is set in response to calls to lstat(), is_link(), or filetype().
* - STREAM_URL_STAT_QUIET If this flag is set, your wrapper should not raise any errors. If this flag is not set,
* you are responsible for reporting errors using the trigger_error() function during stating of the path.
* stat triggers it's own warning anyway, so it makes no sense to trigger one by our stream-wrapper!
* @return array
*/
static function url_stat ( $path, $flags );
/**
* This method is called in response to readdir().
*
* It should return a string representing the next filename in the location opened by dir_opendir().
*
* @return string
*/
function dir_readdir ( );
/**
* This method is called in response to rewinddir().
*
* It should reset the output generated by dir_readdir(). i.e.:
* The next call to dir_readdir() should return the first entry in the location returned by dir_opendir().
*
* @return boolean
*/
function dir_rewinddir ( );
/**
* This method is called in response to closedir().
*
* You should release any resources which were locked or allocated during the opening and use of the directory stream.
*
* @return boolean
*/
function dir_closedir ( );
}