ReadOnlyStream.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. namespace Drupal\Core\StreamWrapper;
  3. /**
  4. * Defines a read-only Drupal stream wrapper base class.
  5. *
  6. * This class provides a minimal-read only stream wrapper implementation.
  7. * Specifically, it only implements the writing classes and read classes where
  8. * we need to restrict 'write-capable' arguments.
  9. *
  10. * Drupal\Core\StreamWrapper\ReadOnlyStream implementations need to implement
  11. * all the read-related classes.
  12. */
  13. abstract class ReadOnlyStream implements StreamWrapperInterface {
  14. /**
  15. * Stream context resource.
  16. *
  17. * @var resource
  18. */
  19. public $context;
  20. /**
  21. * A generic resource handle.
  22. *
  23. * @var resource
  24. */
  25. public $handle = NULL;
  26. /**
  27. * Instance URI (stream).
  28. *
  29. * A stream is referenced as "scheme://target".
  30. *
  31. * @var string
  32. */
  33. protected $uri;
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function setUri($uri) {
  38. $this->uri = $uri;
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function getUri() {
  44. return $this->uri;
  45. }
  46. /**
  47. * Support for fopen(), file_get_contents(), etc.
  48. *
  49. * Any write modes will be rejected, as this is a read-only stream wrapper.
  50. *
  51. * @param string $uri
  52. * A string containing the URI to the file to open.
  53. * @param int $mode
  54. * The file mode, only strict readonly modes are supported.
  55. * @param int $options
  56. * A bit mask of STREAM_USE_PATH and STREAM_REPORT_ERRORS.
  57. * @param string $opened_path
  58. * A string containing the path actually opened.
  59. *
  60. * @return bool
  61. * TRUE if $mode denotes a readonly mode and the file was opened
  62. * successfully, FALSE otherwise.
  63. *
  64. * @see http://php.net/manual/streamwrapper.stream-open.php
  65. */
  66. public function stream_open($uri, $mode, $options, &$opened_path) {
  67. if (!in_array($mode, ['r', 'rb', 'rt'])) {
  68. if ($options & STREAM_REPORT_ERRORS) {
  69. trigger_error('stream_open() write modes not supported for read-only stream wrappers', E_USER_WARNING);
  70. }
  71. return FALSE;
  72. }
  73. $this->uri = $uri;
  74. $path = $this->getLocalPath();
  75. $this->handle = ($options & STREAM_REPORT_ERRORS) ? fopen($path, $mode) : @fopen($path, $mode);
  76. if ($this->handle !== FALSE && ($options & STREAM_USE_PATH)) {
  77. $opened_path = $path;
  78. }
  79. return (bool) $this->handle;
  80. }
  81. /**
  82. * Support for flock().
  83. *
  84. * An exclusive lock attempt will be rejected, as this is a read-only stream
  85. * wrapper.
  86. *
  87. * @param int $operation
  88. * One of the following:
  89. * - LOCK_SH to acquire a shared lock (reader).
  90. * - LOCK_EX to acquire an exclusive lock (writer).
  91. * - LOCK_UN to release a lock (shared or exclusive).
  92. * - LOCK_NB if you don't want flock() to block while locking (not
  93. * supported on Windows).
  94. *
  95. * @return bool
  96. * Return FALSE for an exclusive lock (writer), as this is a read-only
  97. * stream wrapper. Return the result of flock() for other valid operations.
  98. * Defaults to TRUE if an invalid operation is passed.
  99. *
  100. * @see http://php.net/manual/streamwrapper.stream-lock.php
  101. */
  102. public function stream_lock($operation) {
  103. if (in_array($operation, [LOCK_EX, LOCK_EX | LOCK_NB])) {
  104. trigger_error('stream_lock() exclusive lock operations not supported for read-only stream wrappers', E_USER_WARNING);
  105. return FALSE;
  106. }
  107. if (in_array($operation, [LOCK_SH, LOCK_UN, LOCK_SH | LOCK_NB])) {
  108. return flock($this->handle, $operation);
  109. }
  110. return TRUE;
  111. }
  112. /**
  113. * Support for fwrite(), file_put_contents() etc.
  114. *
  115. * Data will not be written as this is a read-only stream wrapper.
  116. *
  117. * @param string $data
  118. * The string to be written.
  119. *
  120. * @return bool
  121. * FALSE as data will not be written.
  122. *
  123. * @see http://php.net/manual/streamwrapper.stream-write.php
  124. */
  125. public function stream_write($data) {
  126. trigger_error('stream_write() not supported for read-only stream wrappers', E_USER_WARNING);
  127. return FALSE;
  128. }
  129. /**
  130. * Support for fflush().
  131. *
  132. * Nothing will be output to the file, as this is a read-only stream wrapper.
  133. * However as stream_flush is called during stream_close we should not trigger
  134. * an error.
  135. *
  136. * @return bool
  137. * FALSE, as no data will be stored.
  138. *
  139. * @see http://php.net/manual/streamwrapper.stream-flush.php
  140. */
  141. public function stream_flush() {
  142. return FALSE;
  143. }
  144. /**
  145. * {@inheritdoc}
  146. *
  147. * Does not change meta data as this is a read-only stream wrapper.
  148. */
  149. public function stream_metadata($uri, $option, $value) {
  150. trigger_error('stream_metadata() not supported for read-only stream wrappers', E_USER_WARNING);
  151. return FALSE;
  152. }
  153. /**
  154. * {@inheritdoc}
  155. */
  156. public function stream_truncate($new_size) {
  157. trigger_error('stream_truncate() not supported for read-only stream wrappers', E_USER_WARNING);
  158. return FALSE;
  159. }
  160. /**
  161. * Support for unlink().
  162. *
  163. * The file will not be deleted from the stream as this is a read-only stream
  164. * wrapper.
  165. *
  166. * @param string $uri
  167. * A string containing the uri to the resource to delete.
  168. *
  169. * @return bool
  170. * TRUE so that file_delete() will remove db reference to file. File is not
  171. * actually deleted.
  172. *
  173. * @see http://php.net/manual/streamwrapper.unlink.php
  174. */
  175. public function unlink($uri) {
  176. trigger_error('unlink() not supported for read-only stream wrappers', E_USER_WARNING);
  177. return TRUE;
  178. }
  179. /**
  180. * Support for rename().
  181. *
  182. * This file will not be renamed as this is a read-only stream wrapper.
  183. *
  184. * @param string $from_uri
  185. * The uri to the file to rename.
  186. * @param string $to_uri
  187. * The new uri for file.
  188. *
  189. * @return bool
  190. * FALSE as file will never be renamed.
  191. *
  192. * @see http://php.net/manual/streamwrapper.rename.php
  193. */
  194. public function rename($from_uri, $to_uri) {
  195. trigger_error('rename() not supported for read-only stream wrappers', E_USER_WARNING);
  196. return FALSE;
  197. }
  198. /**
  199. * Support for mkdir().
  200. *
  201. * Directory will never be created as this is a read-only stream wrapper.
  202. *
  203. * @param string $uri
  204. * A string containing the URI to the directory to create.
  205. * @param int $mode
  206. * Permission flags - see mkdir().
  207. * @param int $options
  208. * A bit mask of STREAM_REPORT_ERRORS and STREAM_MKDIR_RECURSIVE.
  209. *
  210. * @return bool
  211. * FALSE as directory will never be created.
  212. *
  213. * @see http://php.net/manual/streamwrapper.mkdir.php
  214. */
  215. public function mkdir($uri, $mode, $options) {
  216. trigger_error('mkdir() not supported for read-only stream wrappers', E_USER_WARNING);
  217. return FALSE;
  218. }
  219. /**
  220. * Support for rmdir().
  221. *
  222. * Directory will never be deleted as this is a read-only stream wrapper.
  223. *
  224. * @param string $uri
  225. * A string containing the URI to the directory to delete.
  226. * @param int $options
  227. * A bit mask of STREAM_REPORT_ERRORS.
  228. *
  229. * @return bool
  230. * FALSE as directory will never be deleted.
  231. *
  232. * @see http://php.net/manual/streamwrapper.rmdir.php
  233. */
  234. public function rmdir($uri, $options) {
  235. trigger_error('rmdir() not supported for read-only stream wrappers', E_USER_WARNING);
  236. return FALSE;
  237. }
  238. }