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 * Support for fwrite(), file_put_contents() etc.
|
Chris@0
|
74 *
|
Chris@0
|
75 * Data will not be written as this is a read-only stream wrapper.
|
Chris@0
|
76 *
|
Chris@0
|
77 * @param string $data
|
Chris@0
|
78 * The string to be written.
|
Chris@0
|
79 *
|
Chris@0
|
80 * @return bool
|
Chris@0
|
81 * FALSE as data will not be written.
|
Chris@0
|
82 *
|
Chris@0
|
83 * @see http://php.net/manual/streamwrapper.stream-write.php
|
Chris@0
|
84 */
|
Chris@0
|
85 public function stream_write($data) {
|
Chris@0
|
86 trigger_error('stream_write() not supported for read-only stream wrappers', E_USER_WARNING);
|
Chris@0
|
87 return FALSE;
|
Chris@0
|
88 }
|
Chris@0
|
89
|
Chris@0
|
90 /**
|
Chris@0
|
91 * Support for fflush().
|
Chris@0
|
92 *
|
Chris@0
|
93 * Nothing will be output to the file, as this is a read-only stream wrapper.
|
Chris@0
|
94 * However as stream_flush is called during stream_close we should not trigger
|
Chris@0
|
95 * an error.
|
Chris@0
|
96 *
|
Chris@0
|
97 * @return bool
|
Chris@0
|
98 * FALSE, as no data will be stored.
|
Chris@0
|
99 *
|
Chris@0
|
100 * @see http://php.net/manual/streamwrapper.stream-flush.php
|
Chris@0
|
101 */
|
Chris@0
|
102 public function stream_flush() {
|
Chris@0
|
103 return FALSE;
|
Chris@0
|
104 }
|
Chris@0
|
105
|
Chris@0
|
106 /**
|
Chris@0
|
107 * {@inheritdoc}
|
Chris@0
|
108 *
|
Chris@0
|
109 * Does not change meta data as this is a read-only stream wrapper.
|
Chris@0
|
110 */
|
Chris@0
|
111 public function stream_metadata($uri, $option, $value) {
|
Chris@0
|
112 trigger_error('stream_metadata() not supported for read-only stream wrappers', E_USER_WARNING);
|
Chris@0
|
113 return FALSE;
|
Chris@0
|
114 }
|
Chris@0
|
115
|
Chris@0
|
116 /**
|
Chris@0
|
117 * {@inheritdoc}
|
Chris@0
|
118 */
|
Chris@0
|
119 public function stream_truncate($new_size) {
|
Chris@0
|
120 trigger_error('stream_truncate() not supported for read-only stream wrappers', E_USER_WARNING);
|
Chris@0
|
121 return FALSE;
|
Chris@0
|
122 }
|
Chris@0
|
123
|
Chris@0
|
124 /**
|
Chris@0
|
125 * Support for unlink().
|
Chris@0
|
126 *
|
Chris@0
|
127 * The file will not be deleted from the stream as this is a read-only stream
|
Chris@0
|
128 * wrapper.
|
Chris@0
|
129 *
|
Chris@0
|
130 * @param string $uri
|
Chris@0
|
131 * A string containing the uri to the resource to delete.
|
Chris@0
|
132 *
|
Chris@0
|
133 * @return bool
|
Chris@0
|
134 * TRUE so that file_delete() will remove db reference to file. File is not
|
Chris@0
|
135 * actually deleted.
|
Chris@0
|
136 *
|
Chris@0
|
137 * @see http://php.net/manual/streamwrapper.unlink.php
|
Chris@0
|
138 */
|
Chris@0
|
139 public function unlink($uri) {
|
Chris@0
|
140 trigger_error('unlink() not supported for read-only stream wrappers', E_USER_WARNING);
|
Chris@0
|
141 return TRUE;
|
Chris@0
|
142 }
|
Chris@0
|
143
|
Chris@0
|
144 /**
|
Chris@0
|
145 * Support for rename().
|
Chris@0
|
146 *
|
Chris@0
|
147 * The file will not be renamed as this is a read-only stream wrapper.
|
Chris@0
|
148 *
|
Chris@0
|
149 * @param string $from_uri
|
Chris@0
|
150 * The uri to the file to rename.
|
Chris@0
|
151 * @param string $to_uri
|
Chris@0
|
152 * The new uri for file.
|
Chris@0
|
153 *
|
Chris@0
|
154 * @return bool
|
Chris@0
|
155 * FALSE as file will never be renamed.
|
Chris@0
|
156 *
|
Chris@0
|
157 * @see http://php.net/manual/streamwrapper.rename.php
|
Chris@0
|
158 */
|
Chris@0
|
159 public function rename($from_uri, $to_uri) {
|
Chris@0
|
160 trigger_error('rename() not supported for read-only stream wrappers', E_USER_WARNING);
|
Chris@0
|
161 return FALSE;
|
Chris@0
|
162 }
|
Chris@0
|
163
|
Chris@0
|
164 /**
|
Chris@0
|
165 * Support for mkdir().
|
Chris@0
|
166 *
|
Chris@0
|
167 * Directory will never be created as this is a read-only stream wrapper.
|
Chris@0
|
168 *
|
Chris@0
|
169 * @param string $uri
|
Chris@0
|
170 * A string containing the URI to the directory to create.
|
Chris@0
|
171 * @param int $mode
|
Chris@0
|
172 * Permission flags - see mkdir().
|
Chris@0
|
173 * @param int $options
|
Chris@0
|
174 * A bit mask of STREAM_REPORT_ERRORS and STREAM_MKDIR_RECURSIVE.
|
Chris@0
|
175 *
|
Chris@0
|
176 * @return bool
|
Chris@0
|
177 * FALSE as directory will never be created.
|
Chris@0
|
178 *
|
Chris@0
|
179 * @see http://php.net/manual/streamwrapper.mkdir.php
|
Chris@0
|
180 */
|
Chris@0
|
181 public function mkdir($uri, $mode, $options) {
|
Chris@0
|
182 trigger_error('mkdir() not supported for read-only stream wrappers', E_USER_WARNING);
|
Chris@0
|
183 return FALSE;
|
Chris@0
|
184 }
|
Chris@0
|
185
|
Chris@0
|
186 /**
|
Chris@0
|
187 * Support for rmdir().
|
Chris@0
|
188 *
|
Chris@0
|
189 * Directory will never be deleted as this is a read-only stream wrapper.
|
Chris@0
|
190 *
|
Chris@0
|
191 * @param string $uri
|
Chris@0
|
192 * A string containing the URI to the directory to delete.
|
Chris@0
|
193 * @param int $options
|
Chris@0
|
194 * A bit mask of STREAM_REPORT_ERRORS.
|
Chris@0
|
195 *
|
Chris@0
|
196 * @return bool
|
Chris@0
|
197 * FALSE as directory will never be deleted.
|
Chris@0
|
198 *
|
Chris@0
|
199 * @see http://php.net/manual/streamwrapper.rmdir.php
|
Chris@0
|
200 */
|
Chris@0
|
201 public function rmdir($uri, $options) {
|
Chris@0
|
202 trigger_error('rmdir() not supported for read-only stream wrappers', E_USER_WARNING);
|
Chris@0
|
203 return FALSE;
|
Chris@0
|
204 }
|
Chris@0
|
205
|
Chris@0
|
206 }
|