2008-06-26 13:34:04 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2008-10-01 12:06:49 +02:00
|
|
|
* eGroupWare API: VFS - stream wrapper for global variables
|
2008-06-26 13:34:04 +02:00
|
|
|
*
|
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package api
|
|
|
|
* @subpackage vfs
|
|
|
|
* @version $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2008-10-01 12:06:49 +02:00
|
|
|
* eGroupWare API: stream wrapper for global variables. It makes variables available as streams.
|
2008-06-26 13:34:04 +02:00
|
|
|
*
|
|
|
|
* Original from an expample on php.net:
|
|
|
|
* @see http://de.php.net/manual/en/function.stream-wrapper-register.php
|
|
|
|
*
|
2008-10-01 12:06:49 +02:00
|
|
|
* Use as global://varname (please note global://host/varname does NOT work, as parse_url() would return with a leading slash!)
|
|
|
|
*
|
|
|
|
* Streamwrapper is now mbstring.func_overload save.
|
2008-06-26 13:34:04 +02:00
|
|
|
*
|
|
|
|
* @todo: allow to use path to access arrays: global://varname/key1/key2 to access $string from $GLOBALS['varname'] = array('key1'=>array('key2'=>$string));
|
|
|
|
*/
|
|
|
|
class global_stream_wrapper
|
|
|
|
{
|
|
|
|
private $pos;
|
|
|
|
private $stream;
|
|
|
|
|
|
|
|
public function stream_open($path, $mode, $options, &$opened_path)
|
|
|
|
{
|
|
|
|
$this->stream = &$GLOBALS[parse_url($path,PHP_URL_HOST)];
|
|
|
|
$this->pos = 0;
|
|
|
|
if (!is_string($this->stream)) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function stream_read($count)
|
|
|
|
{
|
2008-10-01 12:06:49 +02:00
|
|
|
$ret = self::_cut_bytes($this->stream, $this->pos, $count);
|
|
|
|
//error_log(__METHOD__."($count) this->pos=$this->pos, self::_bytes(this->stream)=".self::_bytes($this->stream)." self::_bytes(ret)=".self::_bytes($ret));
|
|
|
|
$this->pos += self::_bytes($ret);
|
2008-06-26 13:34:04 +02:00
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function stream_write($data)
|
|
|
|
{
|
2008-10-01 12:06:49 +02:00
|
|
|
$l=self::_bytes($data);
|
2008-06-26 13:34:04 +02:00
|
|
|
$this->stream =
|
2008-10-01 12:06:49 +02:00
|
|
|
self::_cut_bytes($this->stream, 0, $this->pos) .
|
2008-06-26 13:34:04 +02:00
|
|
|
$data .
|
2008-10-01 12:06:49 +02:00
|
|
|
self::_cut_bytes($this->stream, $this->pos += $l);
|
2008-06-26 13:34:04 +02:00
|
|
|
return $l;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function stream_tell()
|
|
|
|
{
|
|
|
|
return $this->pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function stream_eof()
|
|
|
|
{
|
2008-10-01 12:06:49 +02:00
|
|
|
return $this->pos >= self::_bytes($this->stream);
|
2008-06-26 13:34:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function stream_seek($offset, $whence)
|
|
|
|
{
|
2008-10-01 12:06:49 +02:00
|
|
|
$l=self::_bytes($this->stream);
|
2008-06-26 13:34:04 +02:00
|
|
|
switch ($whence)
|
|
|
|
{
|
|
|
|
case SEEK_SET: $newPos = $offset; break;
|
|
|
|
case SEEK_CUR: $newPos = $this->pos + $offset; break;
|
|
|
|
case SEEK_END: $newPos = $l + $offset; break;
|
|
|
|
default: return false;
|
|
|
|
}
|
|
|
|
$ret = ($newPos >=0 && $newPos <=$l);
|
|
|
|
if ($ret) $this->pos=$newPos;
|
|
|
|
return $ret;
|
|
|
|
}
|
2008-10-01 12:06:49 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* are the string functions overloaded by their mbstring variants
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
private static $mbstring_func_overload;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* mbstring.func_overload safe strlen
|
|
|
|
*
|
|
|
|
* @param string &$data
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
private static function _bytes(&$data)
|
|
|
|
{
|
|
|
|
return self::$mbstring_func_overload ? mb_strlen($data,'ascii') : strlen($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* mbstring.func_overload safe substr
|
|
|
|
*
|
|
|
|
* @param string &$data
|
|
|
|
* @param int $offset
|
|
|
|
* @param int $len
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private static function _cut_bytes(&$data,$offset,$len=null)
|
|
|
|
{
|
|
|
|
return self::$mbstring_func_overload ? mb_substr($data,$offset,$len,'ascii') : substr($data,$offset,$len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Init the (static parts) of the stream-wrapper
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function init()
|
|
|
|
{
|
|
|
|
self::$mbstring_func_overload = @extension_loaded('mbstring') && (ini_get('mbstring.func_overload') & 2);
|
|
|
|
|
|
|
|
stream_wrapper_register('global', 'global_stream_wrapper');
|
|
|
|
}
|
2008-06-26 13:34:04 +02:00
|
|
|
}
|
2008-10-01 12:06:49 +02:00
|
|
|
global_stream_wrapper::init();
|