PageRenderTime 21ms CodeModel.GetById 12ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/joomla/filesystem/streams/string.php

https://gitlab.com/vitaliylukin91/lavka
PHP | 296 lines | 107 code | 29 blank | 160 comment | 5 complexity | d0f3033ae3399160a14c3572219acee9 MD5 | raw file
  1<?php
  2/**
  3 * @package     Joomla.Platform
  4 * @subpackage  FileSystem
  5 *
  6 * @copyright   Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
  7 * @license     GNU General Public License version 2 or later; see LICENSE
  8 */
  9
 10defined('JPATH_PLATFORM') or die;
 11
 12jimport('joomla.filesystem.support.stringcontroller');
 13
 14/**
 15 * String Stream Wrapper
 16 *
 17 * This class allows you to use a PHP string in the same way that
 18 * you would normally use a regular stream wrapper
 19 *
 20 * @since  11.1
 21 */
 22class JStreamString
 23{
 24	/**
 25	 * The current string
 26	 *
 27	 * @var   string
 28	 * @since  12.1
 29	 */
 30	protected $currentString;
 31
 32	/**
 33	 *
 34	 * The path
 35	 *
 36	 * @var   string
 37	 * @since  12.1
 38	 */
 39	protected $path;
 40
 41	/**
 42	 *
 43	 * The mode
 44	 *
 45	 * @var   string
 46	 * @since  12.1
 47	 */
 48	protected $mode;
 49
 50	/**
 51	 *
 52	 * Enter description here ...
 53	 * @var   string
 54	 *
 55	 * @since  12.1
 56	 */
 57	protected $options;
 58
 59	/**
 60	 *
 61	 * Enter description here ...
 62	 * @var   string
 63	 *
 64	 * @since  12.1
 65	 */
 66	protected $openedPath;
 67
 68	/**
 69	 * Current position
 70	 *
 71	 * @var   integer
 72	 * @since  12.1
 73	 */
 74	protected $pos;
 75
 76	/**
 77	 * Length of the string
 78	 *
 79	 * @var   string
 80	 *
 81	 * @since  12.1
 82	 */
 83	protected $len;
 84
 85	/**
 86	 * Statistics for a file
 87	 *
 88	 * @var    array
 89	 * @since  12.1
 90	 *
 91	 * @see    http://us.php.net/manual/en/function.stat.php
 92	 */
 93	protected $stat;
 94
 95	/**
 96	 * Method to open a file or URL.
 97	 *
 98	 * @param   string   $path          The stream path.
 99	 * @param   string   $mode          Not used.
100	 * @param   integer  $options       Not used.
101	 * @param   string   &$opened_path  Not used.
102	 *
103	 * @return  boolean
104	 *
105	 * @since   11.1
106	 */
107	public function stream_open($path, $mode, $options, &$opened_path)
108	{
109		$this->currentString = &JStringController::getRef(str_replace('string://', '', $path));
110
111		if ($this->currentString)
112		{
113			$this->len = strlen($this->currentString);
114			$this->pos = 0;
115			$this->stat = $this->url_stat($path, 0);
116
117			return true;
118		}
119		else
120		{
121			return false;
122		}
123	}
124
125	/**
126	 * Method to retrieve information from a file resource
127	 *
128	 * @return  array
129	 *
130	 * @see     http://www.php.net/manual/en/streamwrapper.stream-stat.php
131	 * @since   11.1
132	 */
133	public function stream_stat()
134	{
135		return $this->stat;
136	}
137
138	/**
139	 * Method to retrieve information about a file.
140	 *
141	 * @param   string   $path   File path or URL to stat
142	 * @param   integer  $flags  Additional flags set by the streams API
143	 *
144	 * @return  array
145	 *
146	 * @see     http://php.net/manual/en/streamwrapper.url-stat.php
147	 * @since   11.1
148	 */
149	public function url_stat($path, $flags = 0)
150	{
151		$now = time();
152		$string = &JStringController::getRef(str_replace('string://', '', $path));
153		$stat = array(
154			'dev' => 0,
155			'ino' => 0,
156			'mode' => 0,
157			'nlink' => 1,
158			'uid' => 0,
159			'gid' => 0,
160			'rdev' => 0,
161			'size' => strlen($string),
162			'atime' => $now,
163			'mtime' => $now,
164			'ctime' => $now,
165			'blksize' => '512',
166			'blocks' => ceil(strlen($string) / 512));
167
168		return $stat;
169	}
170
171	/**
172	 * Method to read a given number of bytes starting at the current position
173	 * and moving to the end of the string defined by the current position plus the
174	 * given number.
175	 *
176	 * @param   integer  $count  Bytes of data from the current position should be returned.
177	 *
178	 * @return  void
179	 *
180	 * @since   11.1
181	 *
182	 * @see     http://www.php.net/manual/en/streamwrapper.stream-read.php
183	 */
184	public function stream_read($count)
185	{
186		$result = substr($this->currentString, $this->pos, $count);
187		$this->pos += $count;
188
189		return $result;
190	}
191
192	/**
193	 * Stream write, always returning false.
194	 *
195	 * @param   string  $data  The data to write.
196	 *
197	 * @return  boolean
198	 *
199	 * @since   11.1
200	 * @note    Updating the string is not supported.
201	 */
202	public function stream_write($data)
203	{
204		// We don't support updating the string.
205		return false;
206	}
207
208	/**
209	 * Method to get the current position
210	 *
211	 * @return  integer  The position
212	 *
213	 * @since   11.1
214	 */
215	public function stream_tell()
216	{
217		return $this->pos;
218	}
219
220	/**
221	 * End of field check
222	 *
223	 * @return  boolean  True if at end of field.
224	 *
225	 * @since   11.1
226	 */
227	public function stream_eof()
228	{
229		if ($this->pos > $this->len)
230		{
231			return true;
232		}
233
234		return false;
235	}
236
237	/**
238	 * Stream offset
239	 *
240	 * @param   integer  $offset  The starting offset.
241	 * @param   integer  $whence  SEEK_SET, SEEK_CUR, SEEK_END
242	 *
243	 * @return  boolean  True on success.
244	 *
245	 * @since   11.1
246	 */
247	public function stream_seek($offset, $whence)
248	{
249		// $whence: SEEK_SET, SEEK_CUR, SEEK_END
250		if ($offset > $this->len)
251		{
252			// We can't seek beyond our len.
253			return false;
254		}
255
256		switch ($whence)
257		{
258			case SEEK_SET:
259				$this->pos = $offset;
260				break;
261
262			case SEEK_CUR:
263				if (($this->pos + $offset) < $this->len)
264				{
265					$this->pos += $offset;
266				}
267				else
268				{
269					return false;
270				}
271				break;
272
273			case SEEK_END:
274				$this->pos = $this->len - $offset;
275				break;
276		}
277
278		return true;
279	}
280
281	/**
282	 * Stream flush, always returns true.
283	 *
284	 * @return  boolean
285	 *
286	 * @since   11.1
287	 * @note    Data storage is not supported
288	 */
289	public function stream_flush()
290	{
291		// We don't store data.
292		return true;
293	}
294}
295
296stream_wrapper_register('string', 'JStreamString') or die('JStreamString Wrapper Registration Failed');