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