Location: PHPKode > projects > Aukyla Document Management System > plugins/URINamespaces/tmp_URINamespace.php
<?php
/*
     tmpURINamespace.php, namespace for storing temporary 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('Constants.php');
require_once('Login.php');
require_once('String.php');
require_once('URI.php');

/**
 * @brief tmp URI namespace.
 *
 * This class implements the tmp namespace. Basically it allows you to create
 * temporary files and directories to which you have unlimited access. The files
 * and directories in the temporary directory get deleted periodically.
 */
class tmp_URINamespace extends LocalURINamespace
{
	public function stream_open($path, $mode, $options = '', &$opened_path = '')
	{
		$realPath = $this->realPath($path);
		if($realPath === 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_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)
	{
		$realPath = $this->realPath($path);
		if($realPath === false)
		{
			return false;
		}

		return unlink($realPath);
	}

	public function rename($path_from, $path_to)
	{
		$realPathFrom = $this->realPath($path_from);
		$realPathTo = $this->realPath($path_to);
		if($realPathFrom === false || $realPathTo === false)
		{
			return false;
		}

		return rename($realPathFrom, $realPathTo);
	}

	public function mkdir($path, $mode, $options)
	{
		$realPath = $this->realPath($path);
		if($realPath === false)
		{
			return false;
		}

		if($options & STREAM_REPORT_ERRORS)
		{
			return mkdir($realPath, $mode, $options & STREAM_MKDIR_RECURSIVE ? true : false);
		}
		else
		{
			return @mkdir($realPath, $mode, $options & STREAM_MKDIR_RECURSIVE ? true : false);
		}
	}

	public function rmdir($path, $options)
	{
		$realPath = $this->realPath($path);
		if($realPath === false)
		{
			return false;
		}

		return rmdir($realPath);
	}

	public function dir_opendir($path, $options)
	{
		$realPath = $this->realPath($path);
		if($realPath === false)
		{
			return false;
		}

		if(($this->dirhandle = opendir($realPath)) === false)
		{
			return false;
		}

		return true;
	}

	public function url_stat($path, $flags)
	{
		$realPath = $this->realPath($path);
		if($realPath == 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()
	{
		return readdir($this->dirhandle);
	}

	public function dir_rewinddir()
	{
		return rewinddir($this->dirhandle);
	}

	public function dir_closedir()
	{
		return closedir($this->dirhandle);
	}

	public function uniquePath($path, $suggestion)
	{
		$realPath = $this->realPath($path);
		if($realPath === false)
		{
			return false;
		}

		do
		{
			$filename = "$suggestion-".Login::randomPassword(5);
		} while(file_exists("$realPath/$filename"));

		if(String::endsWith($path, '/'))
		{
			return "$path$filename";
		}
		else
		{
			return "$path/$filename";
		}
	}

	private function realPath($path)
	{
		$prefix = AUKYLA_DIR.'/run/tmp/'.Login::username();

		if(!file_exists($prefix))
		{
			mkdir($prefix, 0755);
		}

		$realPath = "{$prefix}/".substr($path, 4);
		$realPath = String::simplifyPath($realPath);
		if($realPath === false || !String::startsWith("$realPath/", "{$prefix}/"))
		{
			return false;
		}

		return $realPath;
	}

	private $fp;
	private $dirhandle;
}

?>
Return current item: Aukyla Document Management System