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