Merge "Return early in Title::checkUserBlock() if user does not have a block."
[lhc/web/wiklou.git] / includes / ForeignResourceManager.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @ingroup Maintenance
20 */
21
22 /**
23 * Manage foreign resources registered with ResourceLoader.
24 *
25 * @since 1.32
26 */
27 class ForeignResourceManager {
28 private $defaultAlgo = 'sha384';
29 private $hasErrors = false;
30 private $registryFile;
31 private $libDir;
32 private $tmpParentDir;
33 private $infoPrinter;
34 private $errorPrinter;
35 private $verbosePrinter;
36 private $action;
37
38 /**
39 * @param string $registryFile Path to YAML file
40 * @param string $libDir Path to a modules directory
41 * @param callable|null $infoPrinter Callback for printing info about the run.
42 * @param callable|null $errorPrinter Callback for printing errors from the run.
43 * @param callable|null $verbosePrinter Callback for printing extra verbose
44 * progress information from the run.
45 */
46 public function __construct(
47 $registryFile,
48 $libDir,
49 callable $infoPrinter = null,
50 callable $errorPrinter = null,
51 callable $verbosePrinter = null
52 ) {
53 $this->registryFile = $registryFile;
54 $this->libDir = $libDir;
55 $this->infoPrinter = $infoPrinter ?? function () {
56 };
57 $this->errorPrinter = $errorPrinter ?? $this->infoPrinter;
58 $this->verbosePrinter = $verbosePrinter ?? function () {
59 };
60
61 // Use a temporary directory under the destination directory instead
62 // of wfTempDir() because PHP's rename() does not work across file
63 // systems, as the user's /tmp and $IP may be on different filesystems.
64 $this->tmpParentDir = "{$this->libDir}/.tmp";
65 }
66
67 /**
68 * @return bool
69 * @throws Exception
70 */
71 public function run( $action, $module ) {
72 if ( !in_array( $action, [ 'update', 'verify', 'make-sri' ] ) ) {
73 throw new Exception( 'Invalid action parameter.' );
74 }
75 $this->action = $action;
76
77 $registry = $this->parseBasicYaml( file_get_contents( $this->registryFile ) );
78 if ( $module === 'all' ) {
79 $modules = $registry;
80 } elseif ( isset( $registry[ $module ] ) ) {
81 $modules = [ $module => $registry[ $module ] ];
82 } else {
83 throw new Exception( 'Unknown module name.' );
84 }
85
86 foreach ( $modules as $moduleName => $info ) {
87 $this->verbose( "\n### {$moduleName}\n\n" );
88 $destDir = "{$this->libDir}/$moduleName";
89
90 if ( $this->action === 'update' ) {
91 $this->output( "... updating '{$moduleName}'\n" );
92 $this->verbose( "... emptying directory for $moduleName\n" );
93 wfRecursiveRemoveDir( $destDir );
94 } elseif ( $this->action === 'verify' ) {
95 $this->output( "... verifying '{$moduleName}'\n" );
96 } else {
97 $this->output( "... checking '{$moduleName}'\n" );
98 }
99
100 $this->verbose( "... preparing {$this->tmpParentDir}\n" );
101 wfRecursiveRemoveDir( $this->tmpParentDir );
102 if ( !wfMkdirParents( $this->tmpParentDir ) ) {
103 throw new Exception( "Unable to create {$this->tmpParentDir}" );
104 }
105
106 if ( !isset( $info['type'] ) ) {
107 throw new Exception( "Module '$moduleName' must have a 'type' key." );
108 }
109 switch ( $info['type'] ) {
110 case 'tar':
111 $this->handleTypeTar( $moduleName, $destDir, $info );
112 break;
113 case 'file':
114 $this->handleTypeFile( $moduleName, $destDir, $info );
115 break;
116 case 'multi-file':
117 $this->handleTypeMultiFile( $moduleName, $destDir, $info );
118 break;
119 default:
120 throw new Exception( "Unknown type '{$info['type']}' for '$moduleName'" );
121 }
122 }
123
124 $this->cleanUp();
125 $this->output( "\nDone!\n" );
126 if ( $this->hasErrors ) {
127 // The verify mode should check all modules/files and fail after, not during.
128 return false;
129 }
130
131 return true;
132 }
133
134 private function fetch( $src, $integrity ) {
135 $req = MWHttpRequest::factory( $src, [ 'method' => 'GET', 'followRedirects' => false ] );
136 if ( !$req->execute()->isOK() ) {
137 throw new Exception( "Failed to download resource at {$src}" );
138 }
139 if ( $req->getStatus() !== 200 ) {
140 throw new Exception( "Unexpected HTTP {$req->getStatus()} response from {$src}" );
141 }
142 $data = $req->getContent();
143 $algo = $integrity === null ? $this->defaultAlgo : explode( '-', $integrity )[0];
144 $actualIntegrity = $algo . '-' . base64_encode( hash( $algo, $data, true ) );
145 if ( $integrity === $actualIntegrity ) {
146 $this->verbose( "... passed integrity check for {$src}\n" );
147 } else {
148 if ( $this->action === 'make-sri' ) {
149 $this->output( "Integrity for {$src}\n\tintegrity: ${actualIntegrity}\n" );
150 } else {
151 throw new Exception( "Integrity check failed for {$src}\n" .
152 "\tExpected: {$integrity}\n" .
153 "\tActual: {$actualIntegrity}"
154 );
155 }
156 }
157 return $data;
158 }
159
160 private function handleTypeFile( $moduleName, $destDir, array $info ) {
161 if ( !isset( $info['src'] ) ) {
162 throw new Exception( "Module '$moduleName' must have a 'src' key." );
163 }
164 $data = $this->fetch( $info['src'], $info['integrity'] ?? null );
165 $dest = $info['dest'] ?? basename( $info['src'] );
166 $path = "$destDir/$dest";
167 if ( $this->action === 'verify' && sha1_file( $path ) !== sha1( $data ) ) {
168 throw new Exception( "File for '$moduleName' is different." );
169 }
170 if ( $this->action === 'update' ) {
171 wfMkdirParents( $destDir );
172 file_put_contents( "$destDir/$dest", $data );
173 }
174 }
175
176 private function handleTypeMultiFile( $moduleName, $destDir, array $info ) {
177 if ( !isset( $info['files'] ) ) {
178 throw new Exception( "Module '$moduleName' must have a 'files' key." );
179 }
180 foreach ( $info['files'] as $dest => $file ) {
181 if ( !isset( $file['src'] ) ) {
182 throw new Exception( "Module '$moduleName' file '$dest' must have a 'src' key." );
183 }
184 $data = $this->fetch( $file['src'], $file['integrity'] ?? null );
185 $path = "$destDir/$dest";
186 if ( $this->action === 'verify' && sha1_file( $path ) !== sha1( $data ) ) {
187 throw new Exception( "File '$dest' for '$moduleName' is different." );
188 } elseif ( $this->action === 'update' ) {
189 wfMkdirParents( $destDir );
190 file_put_contents( "$destDir/$dest", $data );
191 }
192 }
193 }
194
195 private function handleTypeTar( $moduleName, $destDir, array $info ) {
196 $info += [ 'src' => null, 'integrity' => null, 'dest' => null ];
197 if ( $info['src'] === null ) {
198 throw new Exception( "Module '$moduleName' must have a 'src' key." );
199 }
200 // Download the resource to a temporary file and open it
201 $data = $this->fetch( $info['src'], $info['integrity' ] );
202 $tmpFile = "{$this->tmpParentDir}/$moduleName.tar";
203 $this->verbose( "... writing '$moduleName' src to $tmpFile\n" );
204 file_put_contents( $tmpFile, $data );
205 $p = new PharData( $tmpFile );
206 $tmpDir = "{$this->tmpParentDir}/$moduleName";
207 $p->extractTo( $tmpDir );
208 unset( $data, $p );
209
210 if ( $info['dest'] === null ) {
211 // Default: Replace the entire directory
212 $toCopy = [ $tmpDir => $destDir ];
213 } else {
214 // Expand and normalise the 'dest' entries
215 $toCopy = [];
216 foreach ( $info['dest'] as $fromSubPath => $toSubPath ) {
217 // Use glob() to expand wildcards and check existence
218 $fromPaths = glob( "{$tmpDir}/{$fromSubPath}", GLOB_BRACE );
219 if ( !$fromPaths ) {
220 throw new Exception( "Path '$fromSubPath' of '$moduleName' not found." );
221 }
222 foreach ( $fromPaths as $fromPath ) {
223 $toCopy[$fromPath] = $toSubPath === null
224 ? "$destDir/" . basename( $fromPath )
225 : "$destDir/$toSubPath/" . basename( $fromPath );
226 }
227 }
228 }
229 foreach ( $toCopy as $from => $to ) {
230 if ( $this->action === 'verify' ) {
231 $this->verbose( "... verifying $to\n" );
232 if ( is_dir( $from ) ) {
233 $rii = new RecursiveIteratorIterator( new RecursiveDirectoryIterator(
234 $from,
235 RecursiveDirectoryIterator::SKIP_DOTS
236 ) );
237 foreach ( $rii as $file ) {
238 $remote = $file->getPathname();
239 $local = strtr( $remote, [ $from => $to ] );
240 if ( sha1_file( $remote ) !== sha1_file( $local ) ) {
241 $this->error( "File '$local' is different." );
242 $this->hasErrors = true;
243 }
244 }
245 } elseif ( sha1_file( $from ) !== sha1_file( $to ) ) {
246 $this->error( "File '$to' is different." );
247 $this->hasErrors = true;
248 }
249 } elseif ( $this->action === 'update' ) {
250 $this->verbose( "... moving $from to $to\n" );
251 wfMkdirParents( dirname( $to ) );
252 if ( !rename( $from, $to ) ) {
253 throw new Exception( "Could not move $from to $to." );
254 }
255 }
256 }
257 }
258
259 private function verbose( $text ) {
260 ( $this->verbosePrinter )( $text );
261 }
262
263 private function output( $text ) {
264 ( $this->infoPrinter )( $text );
265 }
266
267 private function error( $text ) {
268 ( $this->errorPrinter )( $text );
269 }
270
271 private function cleanUp() {
272 wfRecursiveRemoveDir( $this->tmpParentDir );
273 }
274
275 /**
276 * Basic YAML parser.
277 *
278 * Supports only string or object values, and 2 spaces indentation.
279 *
280 * @todo Just ship symfony/yaml.
281 * @param string $input
282 * @return array
283 */
284 private function parseBasicYaml( $input ) {
285 $lines = explode( "\n", $input );
286 $root = [];
287 $stack = [ &$root ];
288 $prev = 0;
289 foreach ( $lines as $i => $text ) {
290 $line = $i + 1;
291 $trimmed = ltrim( $text, ' ' );
292 if ( $trimmed === '' || $trimmed[0] === '#' ) {
293 continue;
294 }
295 $indent = strlen( $text ) - strlen( $trimmed );
296 if ( $indent % 2 !== 0 ) {
297 throw new Exception( __METHOD__ . ": Odd indentation on line $line." );
298 }
299 $depth = $indent === 0 ? 0 : ( $indent / 2 );
300 if ( $depth < $prev ) {
301 // Close previous branches we can't re-enter
302 array_splice( $stack, $depth + 1 );
303 }
304 if ( !array_key_exists( $depth, $stack ) ) {
305 throw new Exception( __METHOD__ . ": Too much indentation on line $line." );
306 }
307 if ( strpos( $trimmed, ':' ) === false ) {
308 throw new Exception( __METHOD__ . ": Missing colon on line $line." );
309 }
310 $dest =& $stack[ $depth ];
311 if ( $dest === null ) {
312 // Promote from null to object
313 $dest = [];
314 }
315 list( $key, $val ) = explode( ':', $trimmed, 2 );
316 $val = ltrim( $val, ' ' );
317 if ( $val !== '' ) {
318 // Add string
319 $dest[ $key ] = $val;
320 } else {
321 // Add null (may become an object later)
322 $val = null;
323 $stack[] = &$val;
324 $dest[ $key ] = &$val;
325 }
326 $prev = $depth;
327 unset( $dest, $val );
328 }
329 return $root;
330 }
331 }