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