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