PageRenderTime 25ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

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

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