<?php
/*
ADMS_URINamespace.php, file system layer, doing all file operations
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('Login.php');
require_once('String.php');
require_once('URI.php');
require_once('ADMS/URIManager.php');
/**
* This class implements the ADMS namespace.
*/
class ADMS_URINamespace extends LocalURINamespace
{
public function stream_open($path, $mode, $options = 0, &$opened_path = '')
{
if($mode != 'r' && ADMS_URIManager::getLock() == false)
{
return false;
}
$realPath = ADMS_URIManager::realPath($path);
if($options & STREAM_USE_PATH)
{
$openedPath = $path;
}
if(ADMS_URIManager::entryExists($path) == false)
{
ADMS_URIManager::createNewEntry($path);
}
if(($this->fp = fopen($realPath, $mode)) === false)
{
ADMS_URIManager::removeEntry($path);
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_write($data)
{
return fwrite($this->fp, $data);
}
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_flush()
{
return fflush($this->fp);
}
public function stream_stat()
{
return fstat($this->fp);
}
public function unlink($path)
{
if(ADMS_URIManager::getLock() == false)
{
return false;
}
$realPath = ADMS_URIManager::realPath($path);
if(ADMS_URIManager::removeEntry($path) == false)
{
return false;
}
unlink($realPath);
return true;
}
public function rename($pathFrom, $pathTo)
{
if(ADMS_URIManager::getLock() == false)
{
return false;
}
$fromIndexFile = ADMS_URIManager::openIndexFile(dirname($pathFrom));
$toIndexFile = ADMS_URIManager::openIndexFile(dirname($pathTo));
if((ADMS_URIManager::entryExists($pathTo) == false &&
ADMS_URIManager::createNewEntry($pathTo) == false))
{
return false;
}
$entrynumFrom = $fromIndexFile->searchEntry(basename($pathFrom));
$entrynumTo = $toIndexFile->searchEntry(basename($pathTo));
$metaDataArray = $fromIndexFile->metaArray($entrynumFrom);
$userAccessArray = $fromIndexFile->userAccessArray($entrynumFrom);
$groupAccessArray = $fromIndexFile->groupAccessArray($entrynumFrom);
$otherAccess = $fromIndexFile->otherAccess($entrynumFrom);
$toIndexFile->setMetaArray($entrynumTo, $metaDataArray);
$toIndexFile->setUserAccessArray($entrynumTo, $userAccessArray);
$toIndexFile->setGroupAccessArray($entrynumTo, $groupAccessArray);
$toIndexFile->setOtherAccess($entrynumTo, $otherAccess);
if(ADMS_URIManager::removeEntry($pathFrom) == false)
{
ADMS_URIManager::removeEntry($pathTo);
return false;
}
$realPathFrom = ADMS_URIManager::realPath($pathFrom);
$realPathTo = ADMS_URIManager::realPath($pathTo);
if(rename($realPathFrom, $realPathTo) == false)
{
ADMS_URIManager::removeEntry($pathTo);
ADMS_URIManager::createNewEntry($pathFrom);
$fromIndexFile->setMetaArray($pathFrom, $metaDataArray);
$fromIndexFile->setUserAccessArray($pathFrom, $userAccessArray);
$fromIndexFile->setGroupAccessArray($pathFrom, $groupAccessArray);
$fromIndexFile->setOtherAccess($pathFrom, $otherAccess);
return false;
}
return true;
}
public function mkdir($path, $mode, $options)
{
if(ADMS_URIManager::getLock() == false)
{
return false;
}
$realPath = ADMS_URIManager::realPath($path);
if(ADMS_URIManager::entryExists(dirname($path)) == false)
{
return false;
}
ADMS_URIManager::createNewEntry($path);
if(mkdir($realPath, 0700) == false)
{
return false;
}
ADMS_URIManager::createIndexFile($path);
return true;
}
public function rmdir($path, $options = 0)
{
if(ADMS_URIManager::getLock() == false)
{
return false;
}
$realPath = ADMS_URIManager::realPath($path);
if(ADMS_URIManager::deleteIndexFile($path) == false ||
ADMS_URIManager::removeEntry($path) == false ||
rmdir($realPath) == false)
{
return false;
}
return true;
}
public function dir_opendir($path, $options)
{
if(($this->dirIndexFile = ADMS_URIManager::openIndexFile($path)) == false)
{
return false;
}
$this->directory = $path;
$this->dirPosition = 0;
return true;
}
public function url_stat($path, $flags)
{
$realPath = ADMS_URIManager::realPath($path);
if(ADMS_URIManager::entryExists($path) == false)
{
return false;
}
if($flags & STREAM_URL_STAT_LINK)
{
if($flags & STREAM_URL_STAT_QUIET)
{
return @lstat($realPath);
}
else
{
return lstat($realPath);
}
}
else
{
if($flags & STREAM_URL_STAT_QUIET)
{
return @stat($realPath);
}
else
{
return stat($realPath);
}
}
}
public function dir_readdir()
{
$entries = $this->dirIndexFile->entries();
$position = $this->dirPosition++;
return (isset($entries[$position]) ? "{$this->directory}/{$entries[$position]->id}" : false);
}
public function dir_rewinddir()
{
$this->dirPosition = 0;
return true;
}
public function dir_closedir()
{
return true;
}
public function uniquePath($path, $suggestion)
{
$indexFile = ADMS_URIManager::openIndexFile($path);
if($indexFile === false)
{
return false;
}
return (String::endsWith($path, '/') ? "$path".$indexFile->freeID() :
"$path/".$indexFile->freeID());
}
public function permissions($path)
{
// let root do everything
if(Login::username() == 'root')
{
return PERMISSION_READ | PERMISSION_APPEND | PERMISSION_MODIFY |
PERMISSION_DELETE | PERMISSION_ADMINISTRATE;
}
// open index file
$indexFile = ADMS_URIManager::openIndexFile(dirname($path));
if($indexFile === false ||
($entrynum = $indexFile->searchEntry(basename($path))) == -1)
{
return PERMISSION_NONE;
}
// check IP range permissions
$defaultAccess = PERMISSION_NONE;
$ipArray = $indexFile->ipAccessArray($entrynum);
foreach($ipArray as $ipRange => $access)
{
if(String::isIpInRange($ipRange, $_SERVER['REMOTE_ADDR']))
{
$defaultAccess |= $access;
}
}
// check user permission
if(($access = $indexFile->userAccess($entrynum, Login::username())) !== false)
{
return $defaultAccess | $access;
}
// check group permissions
$groupMatch = false;
$access = PERMISSION_NONE;
foreach(Login::memberGroups() as $group)
{
if(($groupAccess = $indexFile->groupAccess($entrynum, $group)) !== false)
{
$groupMatch = true;
$access |= $groupAccess;
}
}
if($groupMatch == true)
{
return $defaultAccess | $access;
}
// check other permission
if(($access = $indexFile->otherAccess($entrynum)) !== false)
{
return $defaultAccess | $access;
}
else
{
return $defaultAccess;
}
}
public function permissionsArray($path)
{
$indexFile = ADMS_URIManager::openIndexFile(dirname($path));
$entrynum = $indexFile->searchEntry(basename($path));
$array = array();
$ipArray = $indexFile->ipAccessArray($entrynum);
if($ipArray != false)
{
$array['ip'] = $ipArray;
}
$userArray = $indexFile->userAccessArray($entrynum);
if($userArray != false)
{
$array['user'] = $userArray;
}
$groupArray = $indexFile->groupAccessArray($entrynum);
if($groupArray != false)
{
$array['group'] = $groupArray;
}
$array['other'] = $indexFile->otherAccess($entrynum);
return $array;
}
public function setPermissionsArray($path, $permissions)
{
if(ADMS_URIManager::getLock() == false)
{
return false;
}
$indexFile = ADMS_URIManager::openIndexFile(dirname($path));
$entrynum = $indexFile->searchEntry(basename($path));
if($entrynum !== false &&
is_array($permissions['user']) && is_array($permissions['group']))
{
if(is_array($permissions['ip']))
{
$indexFile->setIpAccessArray($entrynum, $permissions['ip']);
}
$indexFile->setUserAccessArray($entrynum, $permissions['user']);
$indexFile->setGroupAccessArray($entrynum, $permissions['group']);
$indexFile->setOtherAccess($entrynum, $permissions['other']);
return true;
}
return false;
}
public function metaData($path, $key)
{
$indexFile = ADMS_URIManager::openIndexFile(dirname($path));
$entrynum = $indexFile->searchEntry(basename($path));
return $indexFile->meta($entrynum, $key);
}
public function setMetaData($path, $key, $value)
{
if(ADMS_URIManager::getLock() == false)
{
return false;
}
$indexFile = ADMS_URIManager::openIndexFile(dirname($path));
$entrynum = $indexFile->searchEntry(basename($path));
return $indexFile->setMeta($entrynum, $key, $value);
}
private $fp;
private $directory;
private $dirPosition;
private $dirIndexFile;
}
?>