<?php
/*
URIManager.php, manager for the ADMS_URINamespace
Copyright (C) 2004-2005 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('Config.php');
require_once('Constants.php');
require_once('String.php');
require_once('ADMS/IndexFile.php');
define('ADMS_DOCROOT', AUKYLA_DIR.'/data/apps/ADMS');
/**
* @internal
*
* Global manager for the ADMS_URINamespace.
*/
class ADMS_URIManager
{
/**
* Constructor.
*/
public function __construct($root = '')
{
$this->root = ADMS_DOCROOT."/$root";
$this->indexFiles = array();
$this->haveLock = false;
}
/**
* Destructor.
*/
public function __destruct()
{
foreach($this->indexFiles as $indexFile)
{
$indexFile->save();
}
$this->releaseLock();
}
/**
* Gets a lock for write actions.
*/
public static function getLock()
{
global $ADMS_URIManager;
if($ADMS_URIManager->haveLock == true)
{
return true;
}
$time = 0;
while(($fp = @fopen(AUKYLA_DIR.'/run/ADMS_lock', 'x')) === false)
{
usleep(1000);
if(++$time == 500)
{
return false;
}
}
fclose($fp);
$ADMS_URIManager->haveLock = true;
return true;
}
/**
* Releases a lock.
*/
private function releaseLock()
{
if($this->haveLock == true)
{
@unlink(AUKYLA_DIR.'/run/ADMS_lock');
}
}
/**
* Converts the @p path to a real path.
*/
public static function realPath($path)
{
global $ADMS_URIManager;
$path = String::normalizeSlashes(String::substringAfter($path, 'ADMS:/'));
return "{$ADMS_URIManager->root}/$path";
}
/**
* Opens the IndexFile in @p directory.
*/
public static function openIndexFile($directory)
{
global $ADMS_URIManager;
$realDirectory = self::realPath($directory);
foreach($ADMS_URIManager->indexFiles as $indexFile)
{
if($indexFile->filename() == "$realDirectory/index.xml")
{
return $indexFile;
}
}
$indexFile = new ADMS_IndexFile;
if($indexFile->open("$realDirectory/index.xml") === false)
{
return false;
}
$ADMS_URIManager->indexFiles[] = $indexFile;
return $indexFile;
}
/**
* Deletes the IndexFile in @p directory.
*/
public static function deleteIndexFile($directory)
{
global $ADMS_URIManager;
$realDirectory = self::realPath($directory);
$i = 0;
foreach($ADMS_URIManager->indexFiles as $indexFile)
{
if($indexFile->filename() == "$realDirectory/index.xml")
{
unset($ADMS_URIManager->indexFiles[$i]);
}
$i++;
}
if(unlink("$realDirectory/index.xml") == false)
{
return false;
}
return true;
}
/**
* Creates an IndexFile in @p directory.
*/
public static function createIndexFile($directory)
{
global $ADMS_URIManager;
$realDirectory = self::realPath($directory);
foreach($ADMS_URIManager->indexFiles as $indexFile)
{
if($indexFile->filename() == "$realDirectory/index.xml")
{
return $indexFile;
}
}
$indexFile = ($ADMS_URIManager->indexFiles[] = new ADMS_IndexFile);
$indexFile->setFilename("$realDirectory/index.xml");
return $indexFile;
}
/**
* Creates a new entry for @p path.
*/
public static function createNewEntry($path)
{
$dirName = dirname($path);
if(($indexFile = self::openIndexFile($dirName)) === false)
{
return false;
}
$entrynum = $indexFile->addEntry(basename($path));
$parentIndexFile = self::openIndexFile(dirname($dirName));
$parentEntrynum = $parentIndexFile->searchEntry(basename($dirName));
$ipAccessArray = $parentIndexFile->ipAccessArray($parentEntrynum);
$userAccessArray = $parentIndexFile->userAccessArray($parentEntrynum);
$groupAccessArray = $parentIndexFile->groupAccessArray($parentEntrynum);
$otherAccess = $parentIndexFile->otherAccess($parentEntrynum);
$ownerPermissions = PERMISSION_READ | PERMISSION_APPEND | PERMISSION_MODIFY | PERMISSION_DELETE;
if(String::startsWith(Login::username(), 'anonymous') == false)
{
$userMatch = false;
foreach($userAccessArray as $user => $permissions)
{
if($user == Login::username())
{
$userAccessArray[$user] |= $ownerPermissions;
$userMatch = true;
}
}
if($userMatch == false)
{
foreach($groupAccessArray as $group => $permissions)
{
if(in_array($group, Login::memberGroups()))
{
$ownerPermissions |= $permissions;
}
}
$ownerPermissions |= $otherAccess;
$userAccessArray[Login::username()] = $ownerPermissions;
}
}
else
{
$otherAccess |= $ownerPermissions;
}
$indexFile->setIpAccessArray($entrynum, $ipAccessArray);
$indexFile->setUserAccessArray($entrynum, $userAccessArray);
$indexFile->setGroupAccessArray($entrynum, $groupAccessArray);
$indexFile->setOtherAccess($entrynum, $otherAccess);
return true;
}
/**
* Checks whether the entry given by @p path exists.
*/
public static function entryExists($path)
{
$dirName = dirname($path);
$indexFile = self::openIndexFile($dirName);
if($indexFile == false)
{
return false;
}
return ($indexFile->searchEntry(basename($path)) !== false);
}
/**
* Removes the entry for @p path.
*/
public static function removeEntry($path)
{
$dirName = dirname($path);
$indexFile = self::openIndexFile($dirName);
if($indexFile === false)
{
return false;
}
if(($entrynum = $indexFile->searchEntry(basename($path))) === false)
{
return false;
}
$indexFile->removeEntry($entrynum);
return true;
}
private $root;
private $indexFiles;
private $haveLock;
}
// create a global ADMS_URIManager as soon as the namespace is included
global $ADMS_URIManager;
$ADMS_URIManager = new ADMS_URIManager();
?>