<?php
/*
uploadURINamespace.php, namespace for accessing uploaded files.
Copyright (C) 2004 Arend van Beelen, Auton Rijnsburg
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.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
For any questions, comments or whatever, you may mail me at: hide@address.com
*/
require_once('Locale.php');
require_once('MIME.php');
require_once('String.php');
require_once('URI.php');
/**
* @brief upload URI namespace.
*
* This class implements the upload namespace. It provides read-only access to
* uploaded files. Directory operations are not supported.
*/
class upload_URINamespace extends LocalURINamespace
{
public function stream_open($path, $mode, $options = 0, &$opened_path = '')
{
if(($realPath = $this->realPath($path)) === false)
{
return false;
}
if($options & STREAM_USE_PATH)
{
$openedPath = $path;
}
if(($this->fp = fopen($realPath, $mode)) === false)
{
return false;
}
return true;
}
public function stream_close()
{
return fclose($this->fp);
}
public function stream_read($count)
{
return fread($this->fp, $count);
}
public function stream_eof()
{
return feof($this->fp);
}
public function stream_tell()
{
return ftell($this->fp);
}
public function stream_seek($offset, $whence)
{
return fseek($this->fp, $offset, $whence);
}
public function stream_stat()
{
return fstat($this->fp);
}
public function url_stat($path, $flags)
{
if(($realPath = $this->realPath($path)) === false)
{
return false;
}
if($flags & STREAM_URL_STAT_LINK)
{
return lstat($realPath);
}
else
{
return stat($realPath);
}
}
public function permissions($path)
{
if(($realPath = $this->realPath($path)) === false)
{
return false;
}
return (stat($realPath) === false ? false :
PERMISSION_READ);
}
public function permissionsArray($path)
{
if(($realPath = $this->realPath($path)) === false)
{
return false;
}
return (stat($realPath) === false ? false :
array('other' => PERMISSION_READ));
}
public function metaData($path, $key)
{
$path = String::substringAfter($path, 'upload://');
if(!isset($_FILES[$path]))
{
return false;
}
if($key == 'name')
{
return $_FILES[$path]['name'];
}
if($key == 'mime-type')
{
$mimetype = $_FILES[$path]['type'];
$description = MIME::description($mimetype);
if($mimetype == 'application/octet-stream' || $mimetype == 'text/plain' ||
$description == i18n('Unknown filetype'))
{
$mimetype = MIME::type($_FILES[$path]['name'], false);
if($mimetype == '')
{
copy($_FILES['userfile']['tmp_name'], 'tmp://uploadedFile');
$mimetype = MIME::type('tmp://uploadedFile', true);
}
}
return $mimetype;
}
return false;
}
public function setMetaData($path, $key, $value)
{
$path = String::substringAfter($path, 'upload://');
if(!isset($_FILES[$path]))
{
return false;
}
if($key == 'name')
{
$_FILES[$path]['name'] = $value;
return true;
}
if($key == 'mime-type')
{
$_FILES[$path]['type'] = $value;
return true;
}
return false;
}
private function realPath($path)
{
$path = String::substringAfter($path, 'upload://');
if(!isset($_FILES[$path]))
{
return false;
}
return $_FILES[$path]['tmp_name'];
}
private $fp;
}
?>