Source for file Var.php
Documentation is available at Var.php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2002 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Stephan Schmidt <schst@php-tools.net> |
// | partly based on an exmple by Wez Furlong |
// +----------------------------------------------------------------------+
// $Id: Var.php 287139 2009-08-12 06:42:04Z cweiske $
* This depends on the mode, that is used for opening the stream
define("STREAM_VAR_READABLE", 1 );
* This depends on the mode, that is used for opening the stream
define("STREAM_VAR_WRITEABLE", 2 );
* Stream wrapper to access a variable
* Stream wrappers allow you to access any datasource using PHP's file manipulation functions
* like fopen(), fclose(), fseek(), ftell(),.. as well as directory functions like
* opendir() readdir() and closedir().
* This wrapper allows you to access any variable using these functions.
* You have to specify a scope (GLOBALS, _GET, _POST, etc.) as the host and
* the variable name as the path. If you want to access a string, that is
* stored in an array, you can use this array like you would use a directory.
* require_once '../Var.php';
* stream_wrapper_register( "var", "Stream_Var" );
* $fp = fopen('var://GLOBALS/myArray/index','r');
* $data = fread($fp,100);
* This wrapper also has support for dir functions, so it's possible to read any array, like
* you would read a directory. The following code will list all keys in an array.
* You could use fopen() to read the values for the keys.
* require_once '../Var.php';
* stream_wrapper_register( "var", "Stream_Var" );
* $dh = opendir('var://_SERVER');
* while ($entry = readdir($dh)) {
* This wrapper allows you to replace files and directories with structures in
* memory in any application, that relies on filesystem functions. But keep in mind
* that variables are not persistent during several request, unless you write to
* But this can be used to replace temporary files with variables.
* @author Stephan Schmidt <schst@php-tools.net>
* @link http://pear.php.net/package/Stream_Var
* pointer to the variable
* flag to indicate whether stream is open
* mode of the opened file
* @param string $path Path to the variable
* (e.g. var://GLOBALS/myArray/anyIndex)
* @param string $mode Mode to open the stream,
* like 'r', 'w,',... ({@see fopen()})
* @param array $options Options (not implemented yet)
* @param string &$opened_path This will be set to the actual opened path
* @return boolean $success
function stream_open($path, $mode, $options, &$opened_path)
$varpath = substr($url["path"], 1 );
$status = $this->_setPointer ($scope, $varpath, false );
$status = $this->_setPointer ($scope, $varpath, true );
$status = $this->_setPointer ($scope, $varpath, false );
$status = $this->_setPointer ($scope, $varpath, true );
if (strlen($mode) > 1 && $mode{1 } == '+') {
* Check for end of stream.
* @return boolean True if at end of stream
return ($this->_pos >= strlen($this->_pointer));
* Return the current position.
* @return integer Current position in stream
* @param integer $count Amount of bytes to read
* @return string $data Data that has been read
$data = substr($this->_pointer, $this->_pos, $count);
$this->_pos = $this->_pos + strlen($data);
* @param mixed $data Data to write
* @return integer Number of bytes that were written
$this->_pointer = substr($this->_pointer, 0 , $this->_pos)
. substr($this->_pointer, $this->_pos+ $datalen);
$this->_pos = $this->_pos + $datalen;
* Move the position in the stream.
* @param integer $offset Offset
* @param integer $whence Point from which the offset
* @return boolean True if the position could be reached
if ($offset < strlen($this->_pointer) && $offset >= 0 ) {
if (strlen($this->_pointer) + $offset >= 0 ) {
$this->_pos = strlen($this->_pointer) + $offset;
* Write all data to storage.
* @return boolean Always true
* Return information about the stream.
* @return array $stat Information about the stream
* (currently only the length is included)
'size' => strlen($this->_pointer)
* This method is called in response to stat() calls on the URL paths
* As taken from the PHP Manual:
* "This method is called in response to stat() calls on the URL paths
* associated with the wrapper and 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)."
* With regards to the implementation that is Stream_Var we can actually fake
* some of the data. For instance, the uid and gid can be that of the corrent
* posix_getuid and posix_getgid()
* The following outlines the information that we essentially fake:
* - 'dev': is unknown and set to 0
* - 'ino': is unknown and set to 0
* - 'mode': set to 33216 (chmod 700 means user has read,
* write and execute on the file)
* - 'nlink': is unknown and set to 0
* - 'uid': if the method posix_getuid exist, this is called,
* otherwise 0 is returned
* - 'gid': if the method posix_getgid exist, this is called,
* otherwise 0 is returned
* - 'rdev' unknown and set to 0
* - 'size': is set to the strlen of the pointer.
* - 'atime': set to current value returned by time()
* - 'mtime': set to current value returned by time()
* - 'ctime': set to current value returned by time()
* - 'blksize': is unknown and set to 0
* - 'blocks': is unknown and set to 0
* @param string $path The path to stat.
* @param integer $flags Holds additional flags set by the streams API.
* It can hold one or more of the following values
* - STREAM_URL_STAT_LINK - currently this is
* - STREAM_URL_STAT_QUIET - makes call to
* @see http://au.php.net/stream_wrapper_register
* @author Alex Hayes <ahayes@wcg.net.au>
// chmod 700 means user has read, write and execute on the file
'size' => $flags & STREAM_URL_STAT_QUIET
* @param string $path Path to the array (i.e. the directory)
* @param array $options Not implemented, yet.
* @return boolean $success
if (isset ($url['path'])) {
$varpath = substr($url['path'], 1 );
if (!$status = $this->_setPointer ($scope, $varpath)) {
* @return boolean $success
* @return boolean $success
* Read one entry from 'directory'
* @return mixed $entry Entry that has been read, or
* false if there are no entries left
if (current($this->_pointer) == count($this->_pointer) - 1 ) {
list ($key) = each($this->_pointer);
* Set the internal pointer
* Basically this method only sets the object property _pointer
* as a reference to a variable
* @param string $scope Scope of the variable: GLOBAL, GET,
* POST, COOKIE, SESSION, SERVER, ENV
* @param string $path Path to the variable. Array indices
* are seperated by a slash
* @param boolean $create Create the variable, if it does not exist
* @return boolean true if the pointer was set, false if not found
function _setPointer ($scope, $path, $create = false )
$this->_pointer = &$_GET;
$this->_pointer = &$_POST;
$this->_pointer = &$_SERVER;
$this->_pointer = &$_SESSION;
$this->_pointer = &$_COOKIE;
$this->_pointer = &$_ENV;
$this->_pointer = &$GLOBALS;
if (!isset ($this->_pointer[$part])) {
$this->_pointer[$part] = '';
$this->_pointer = &$this->_pointer[$part];
Documentation generated on Mon, 11 Mar 2019 15:32:46 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|