LocalReadOnlyStream.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. namespace Drupal\Core\StreamWrapper;
  3. /**
  4. * Defines a read-only Drupal stream wrapper base class for local files.
  5. *
  6. * This class extends the complete stream wrapper implementation in LocalStream.
  7. * URIs such as "public://example.txt" are expanded to a normal filesystem path
  8. * such as "sites/default/files/example.txt" and then PHP filesystem functions
  9. * are invoked.
  10. *
  11. * Drupal\Core\StreamWrapper\LocalReadOnlyStream implementations need to
  12. * implement at least the getDirectoryPath() and getExternalUrl() methods.
  13. */
  14. abstract class LocalReadOnlyStream extends LocalStream {
  15. /**
  16. * {@inheritdoc}
  17. */
  18. public static function getType() {
  19. return StreamWrapperInterface::READ_VISIBLE | StreamWrapperInterface::LOCAL;
  20. }
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function stream_open($uri, $mode, $options, &$opened_path) {
  25. if (!in_array($mode, ['r', 'rb', 'rt'])) {
  26. if ($options & STREAM_REPORT_ERRORS) {
  27. trigger_error('stream_open() write modes not supported for read-only stream wrappers', E_USER_WARNING);
  28. }
  29. return FALSE;
  30. }
  31. return parent::stream_open($uri, $mode, $options, $opened_path);
  32. }
  33. /**
  34. * Support for flock().
  35. *
  36. * An exclusive lock attempt will be rejected, as this is a read-only stream
  37. * wrapper.
  38. *
  39. * @param int $operation
  40. * One of the following:
  41. * - LOCK_SH to acquire a shared lock (reader).
  42. * - LOCK_EX to acquire an exclusive lock (writer).
  43. * - LOCK_UN to release a lock (shared or exclusive).
  44. * - LOCK_NB added as a bitmask if you don't want flock() to block while
  45. * locking (not supported on Windows).
  46. *
  47. * @return bool
  48. * Return FALSE for an exclusive lock (writer), as this is a read-only
  49. * stream wrapper. Return the result of flock() for other valid operations.
  50. * Defaults to TRUE if an invalid operation is passed.
  51. *
  52. * @see http://php.net/manual/streamwrapper.stream-lock.php
  53. */
  54. public function stream_lock($operation) {
  55. // Disallow exclusive lock or non-blocking lock requests
  56. if (in_array($operation, [LOCK_EX, LOCK_EX | LOCK_NB])) {
  57. trigger_error('stream_lock() exclusive lock operations not supported for read-only stream wrappers', E_USER_WARNING);
  58. return FALSE;
  59. }
  60. if (in_array($operation, [LOCK_SH, LOCK_UN, LOCK_SH | LOCK_NB])) {
  61. return flock($this->handle, $operation);
  62. }
  63. return TRUE;
  64. }
  65. /**
  66. * Support for fwrite(), file_put_contents() etc.
  67. *
  68. * Data will not be written as this is a read-only stream wrapper.
  69. *
  70. * @param string $data
  71. * The string to be written.
  72. *
  73. * @return bool
  74. * FALSE as data will not be written.
  75. *
  76. * @see http://php.net/manual/streamwrapper.stream-write.php
  77. */
  78. public function stream_write($data) {
  79. trigger_error('stream_write() not supported for read-only stream wrappers', E_USER_WARNING);
  80. return FALSE;
  81. }
  82. /**
  83. * Support for fflush().
  84. *
  85. * Nothing will be output to the file, as this is a read-only stream wrapper.
  86. * However as stream_flush is called during stream_close we should not trigger
  87. * an error.
  88. *
  89. * @return bool
  90. * FALSE, as no data will be stored.
  91. *
  92. * @see http://php.net/manual/streamwrapper.stream-flush.php
  93. */
  94. public function stream_flush() {
  95. return FALSE;
  96. }
  97. /**
  98. * {@inheritdoc}
  99. *
  100. * Does not change meta data as this is a read-only stream wrapper.
  101. */
  102. public function stream_metadata($uri, $option, $value) {
  103. trigger_error('stream_metadata() not supported for read-only stream wrappers', E_USER_WARNING);
  104. return FALSE;
  105. }
  106. /**
  107. * {@inheritdoc}
  108. */
  109. public function stream_truncate($new_size) {
  110. trigger_error('stream_truncate() not supported for read-only stream wrappers', E_USER_WARNING);
  111. return FALSE;
  112. }
  113. /**
  114. * Support for unlink().
  115. *
  116. * The file will not be deleted from the stream as this is a read-only stream
  117. * wrapper.
  118. *
  119. * @param string $uri
  120. * A string containing the uri to the resource to delete.
  121. *
  122. * @return bool
  123. * TRUE so that file_delete() will remove db reference to file. File is not
  124. * actually deleted.
  125. *
  126. * @see http://php.net/manual/streamwrapper.unlink.php
  127. */
  128. public function unlink($uri) {
  129. trigger_error('unlink() not supported for read-only stream wrappers', E_USER_WARNING);
  130. return TRUE;
  131. }
  132. /**
  133. * Support for rename().
  134. *
  135. * The file will not be renamed as this is a read-only stream wrapper.
  136. *
  137. * @param string $from_uri
  138. * The uri to the file to rename.
  139. * @param string $to_uri
  140. * The new uri for file.
  141. *
  142. * @return bool
  143. * FALSE as file will never be renamed.
  144. *
  145. * @see http://php.net/manual/streamwrapper.rename.php
  146. */
  147. public function rename($from_uri, $to_uri) {
  148. trigger_error('rename() not supported for read-only stream wrappers', E_USER_WARNING);
  149. return FALSE;
  150. }
  151. /**
  152. * Support for mkdir().
  153. *
  154. * Directory will never be created as this is a read-only stream wrapper.
  155. *
  156. * @param string $uri
  157. * A string containing the URI to the directory to create.
  158. * @param int $mode
  159. * Permission flags - see mkdir().
  160. * @param int $options
  161. * A bit mask of STREAM_REPORT_ERRORS and STREAM_MKDIR_RECURSIVE.
  162. *
  163. * @return bool
  164. * FALSE as directory will never be created.
  165. *
  166. * @see http://php.net/manual/streamwrapper.mkdir.php
  167. */
  168. public function mkdir($uri, $mode, $options) {
  169. trigger_error('mkdir() not supported for read-only stream wrappers', E_USER_WARNING);
  170. return FALSE;
  171. }
  172. /**
  173. * Support for rmdir().
  174. *
  175. * Directory will never be deleted as this is a read-only stream wrapper.
  176. *
  177. * @param string $uri
  178. * A string containing the URI to the directory to delete.
  179. * @param int $options
  180. * A bit mask of STREAM_REPORT_ERRORS.
  181. *
  182. * @return bool
  183. * FALSE as directory will never be deleted.
  184. *
  185. * @see http://php.net/manual/streamwrapper.rmdir.php
  186. */
  187. public function rmdir($uri, $options) {
  188. trigger_error('rmdir() not supported for read-only stream wrappers', E_USER_WARNING);
  189. return FALSE;
  190. }
  191. }