Total Complexity | 115 |
Total Lines | 665 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like MediaRequestHandler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MediaRequestHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class MediaRequestHandler extends RecordsRequestHandler |
||
9 | { |
||
10 | // RecordRequestHandler configuration |
||
11 | public static $recordClass = Media::class; |
||
12 | public static $accountLevelRead = false; |
||
13 | public static $accountLevelBrowse = 'Staff'; |
||
14 | public static $accountLevelWrite = 'Staff'; |
||
15 | public static $accountLevelAPI = false; |
||
16 | public static $browseLimit = 100; |
||
17 | public static $browseOrder = ['ID' => 'DESC']; |
||
18 | |||
19 | // configurables |
||
20 | public static $defaultPage = 'browse'; |
||
21 | public static $defaultThumbnailWidth = 100; |
||
22 | public static $defaultThumbnailHeight = 100; |
||
23 | public static $uploadFileFieldName = 'mediaFile'; |
||
24 | public static $responseMode = 'html'; |
||
25 | |||
26 | public static $searchConditions = [ |
||
27 | 'Caption' => [ |
||
28 | 'qualifiers' => ['any','caption'] |
||
29 | ,'points' => 2 |
||
30 | ,'sql' => 'Caption LIKE "%%%s%%"', |
||
31 | ] |
||
32 | ,'CaptionLike' => [ |
||
33 | 'qualifiers' => ['caption-like'] |
||
34 | ,'points' => 2 |
||
35 | ,'sql' => 'Caption LIKE "%s"', |
||
36 | ] |
||
37 | ,'CaptionNot' => [ |
||
38 | 'qualifiers' => ['caption-not'] |
||
39 | ,'points' => 2 |
||
40 | ,'sql' => 'Caption NOT LIKE "%%%s%%"', |
||
41 | ] |
||
42 | ,'CaptionNotLike' => [ |
||
43 | 'qualifiers' => ['caption-not-like'] |
||
44 | ,'points' => 2 |
||
45 | ,'sql' => 'Caption NOT LIKE "%s"', |
||
46 | ], |
||
47 | ]; |
||
48 | |||
49 | public static function handleRequest() |
||
125 | } |
||
126 | } |
||
127 | } |
||
128 | } |
||
129 | |||
130 | |||
131 | public static function handleUploadRequest($options = [], $authenticationRequired = true) |
||
132 | { |
||
133 | // require authentication |
||
134 | if ($authenticationRequired) { |
||
135 | static::checkUploadAccess(); |
||
136 | } |
||
137 | |||
138 | if ($_SERVER['REQUEST_METHOD'] == 'POST') { |
||
139 | // init options |
||
140 | $options = array_merge([ |
||
141 | 'fieldName' => static::$uploadFileFieldName, |
||
142 | ], $options); |
||
143 | |||
144 | |||
145 | // check upload |
||
146 | if (empty($_FILES[$options['fieldName']])) { |
||
147 | return static::throwError('You did not select a file to upload'); |
||
148 | } |
||
149 | |||
150 | // handle upload errors |
||
151 | if ($_FILES[$options['fieldName']]['error'] != UPLOAD_ERR_OK) { |
||
152 | switch ($_FILES[$options['fieldName']]['error']) { |
||
153 | case UPLOAD_ERR_NO_FILE: |
||
154 | return static::throwError('You did not select a file to upload'); |
||
155 | |||
156 | case UPLOAD_ERR_INI_SIZE: |
||
157 | case UPLOAD_ERR_FORM_SIZE: |
||
158 | return static::throwError('Your file exceeds the maximum upload size. Please try again with a smaller file.'); |
||
159 | |||
160 | case UPLOAD_ERR_PARTIAL: |
||
161 | return static::throwError('Your file was only partially uploaded, please try again.'); |
||
162 | |||
163 | default: |
||
164 | return static::throwError('There was an unknown problem while processing your upload, please try again.'); |
||
165 | } |
||
166 | } |
||
167 | |||
168 | // init caption |
||
169 | if (!isset($options['Caption'])) { |
||
170 | if (!empty($_REQUEST['Caption'])) { |
||
171 | $options['Caption'] = $_REQUEST['Caption']; |
||
172 | } else { |
||
173 | $options['Caption'] = preg_replace('/\.[^.]+$/', '', $_FILES[$options['fieldName']]['name']); |
||
174 | } |
||
175 | } |
||
176 | |||
177 | // create media |
||
178 | try { |
||
179 | $Media = Media::createFromUpload($_FILES[$options['fieldName']]['tmp_name'], $options); |
||
180 | } catch (Exception $e) { |
||
181 | return static::throwInvalidRequestError('The file you uploaded is not of a supported media format'); |
||
182 | } |
||
183 | } elseif ($_SERVER['REQUEST_METHOD'] == 'PUT') { |
||
184 | $put = fopen("php://input", "r"); // open input stream |
||
185 | |||
186 | $tmp = tempnam("/tmp", "emr"); // use PHP to make a temporary file |
||
187 | $fp = fopen($tmp, "w"); // open write stream to temp file |
||
188 | |||
189 | // write |
||
190 | while ($data = fread($put, 1024)) { |
||
191 | fwrite($fp, $data); |
||
192 | } |
||
193 | |||
194 | // close handles |
||
195 | fclose($fp); |
||
196 | fclose($put); |
||
197 | |||
198 | // create media |
||
199 | try { |
||
200 | $Media = Media::createFromFile($tmp, $options); |
||
201 | } catch (Exception $e) { |
||
202 | return static::throwInvalidRequestError('The file you uploaded is not of a supported media format'); |
||
203 | } |
||
204 | } else { |
||
205 | return static::respond('upload'); |
||
206 | } |
||
207 | |||
208 | // assign tag |
||
209 | /*if (!empty($_REQUEST['Tag']) && ($Tag = Tag::getByHandle($_REQUEST['Tag']))) { |
||
210 | $Tag->assignItem('Media', $Media->ID); |
||
211 | }*/ |
||
212 | |||
213 | // assign context |
||
214 | if (!empty($_REQUEST['ContextClass']) && !empty($_REQUEST['ContextID'])) { |
||
215 | if (!is_subclass_of($_REQUEST['ContextClass'], ActiveRecord::class) |
||
216 | || !in_array($_REQUEST['ContextClass']::getStaticRootClass(), Media::$fields['ContextClass']['values']) |
||
217 | || !is_numeric($_REQUEST['ContextID'])) { |
||
218 | return static::throwError('Context is invalid'); |
||
219 | } elseif (!$Media->Context = $_REQUEST['ContextClass']::getByID($_REQUEST['ContextID'])) { |
||
220 | return static::throwError('Context class not found'); |
||
221 | } |
||
222 | |||
223 | $Media->save(); |
||
224 | } |
||
225 | |||
226 | return static::respond('uploadComplete', [ |
||
227 | 'success' => (boolean)$Media |
||
228 | ,'data' => $Media |
||
229 | ,'TagID' => isset($Tag) ? $Tag->ID : null, |
||
230 | ]); |
||
231 | } |
||
232 | |||
233 | |||
234 | public static function handleMediaRequest($mediaID) |
||
235 | { |
||
236 | if (empty($mediaID) || !is_numeric($mediaID)) { |
||
237 | static::throwError('Missing or invalid media_id'); |
||
238 | } |
||
239 | |||
240 | // get media |
||
241 | try { |
||
242 | $Media = Media::getById($mediaID); |
||
243 | } catch (Exception $e) { |
||
244 | return static::throwUnauthorizedError('You are not authorized to download this media'); |
||
245 | } |
||
246 | |||
247 | if (!$Media) { |
||
248 | static::throwNotFoundError('Media ID #%u was not found', $media_id); |
||
249 | } |
||
250 | |||
251 | if (!static::checkReadAccess($Media)) { |
||
252 | return static::throwUnauthorizedError(); |
||
253 | } |
||
254 | |||
255 | if (static::$responseMode == 'json' || $_SERVER['HTTP_ACCEPT'] == 'application/json') { |
||
256 | JSON::translateAndRespond([ |
||
257 | 'success' => true |
||
258 | ,'data' => $Media, |
||
259 | ]); |
||
260 | } else { |
||
261 | |||
262 | // determine variant |
||
263 | if ($variant = static::shiftPath()) { |
||
264 | if (!$Media->isVariantAvailable($variant)) { |
||
265 | return static::throwNotFoundError('Requested variant is not available'); |
||
266 | } |
||
267 | } else { |
||
268 | $variant = 'original'; |
||
269 | } |
||
270 | |||
271 | // send caching headers |
||
272 | $expires = 60*60*24*365; |
||
273 | header("Cache-Control: public, max-age=$expires"); |
||
274 | header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time()+$expires)); |
||
275 | header('Pragma: public'); |
||
276 | |||
277 | // media are immutable for a given URL, so no need to actually check anything if the browser wants to revalidate its cache |
||
278 | if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) { |
||
279 | header('HTTP/1.0 304 Not Modified'); |
||
280 | exit(); |
||
281 | } |
||
282 | |||
283 | // initialize response |
||
284 | set_time_limit(0); |
||
285 | $filePath = $Media->getFilesystemPath($variant); |
||
286 | $fp = fopen($filePath, 'rb'); |
||
287 | $size = filesize($filePath); |
||
288 | $length = $filesize; |
||
289 | $start = 0; |
||
290 | $end = $size - 1; |
||
291 | |||
292 | header('Content-Type: '.$Media->getMIMEType($variant)); |
||
293 | header('ETag: media-'.$Media->ID.'-'.$variant); |
||
294 | header('Accept-Ranges: bytes'); |
||
295 | |||
296 | // interpret range requests |
||
297 | if (!empty($_SERVER['HTTP_RANGE'])) { |
||
298 | $chunkStart = $start; |
||
299 | $chunkEnd = $end; |
||
300 | |||
301 | list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2); |
||
302 | |||
303 | if (strpos($range, ',') !== false) { |
||
304 | header('HTTP/1.1 416 Requested Range Not Satisfiable'); |
||
305 | header("Content-Range: bytes $start-$end/$size"); |
||
306 | exit(); |
||
307 | } |
||
308 | |||
309 | if ($range == '-') { |
||
310 | $chunkStart = $size - substr($range, 1); |
||
311 | } else { |
||
312 | $range = explode('-', $range); |
||
313 | $chunkStart = $range[0]; |
||
314 | $chunkEnd = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $size; |
||
315 | } |
||
316 | |||
317 | $chunkEnd = ($chunkEnd > $end) ? $end : $chunkEnd; |
||
318 | if ($chunkStart > $chunkEnd || $chunkStart > $size - 1 || $chunkEnd >= $size) { |
||
319 | header('HTTP/1.1 416 Requested Range Not Satisfiable'); |
||
320 | header("Content-Range: bytes $start-$end/$size"); |
||
321 | exit(); |
||
322 | } |
||
323 | |||
324 | $start = $chunkStart; |
||
325 | $end = $chunkEnd; |
||
326 | $length = $end - $start + 1; |
||
327 | |||
328 | fseek($fp, $start); |
||
329 | header('HTTP/1.1 206 Partial Content'); |
||
330 | } |
||
331 | |||
332 | // finish response |
||
333 | header("Content-Range: bytes $start-$end/$size"); |
||
334 | header("Content-Length: $length"); |
||
335 | |||
336 | $buffer = 1024 * 8; |
||
337 | while (!feof($fp) && ($p = ftell($fp)) <= $end) { |
||
338 | if ($p + $buffer > $end) { |
||
339 | $buffer = $end - $p + 1; |
||
340 | } |
||
341 | |||
342 | echo fread($fp, $buffer); |
||
343 | flush(); |
||
344 | } |
||
345 | |||
346 | fclose($fp); |
||
347 | |||
348 | Site::finishRequest(); |
||
349 | } |
||
350 | } |
||
351 | |||
352 | public static function handleInfoRequest($mediaID) |
||
374 | } |
||
375 | |||
376 | public static function handleDownloadRequest($media_id, $filename = false) |
||
377 | { |
||
378 | if (empty($media_id) || !is_numeric($media_id)) { |
||
379 | static::throwError('Missing or invalid media_id'); |
||
380 | } |
||
381 | |||
382 | // get media |
||
383 | try { |
||
384 | $Media = Media::getById($media_id); |
||
385 | } catch (Exception $e) { |
||
386 | return static::throwUnauthorizedError('You are not authorized to download this media'); |
||
387 | } |
||
388 | |||
389 | |||
390 | if (!$Media) { |
||
391 | static::throwNotFoundError('Media ID #%u was not found', $media_id); |
||
392 | } |
||
393 | |||
394 | if (!static::checkReadAccess($Media)) { |
||
395 | return static::throwUnauthorizedError(); |
||
396 | } |
||
397 | |||
398 | // determine filename |
||
399 | if (empty($filename)) { |
||
400 | $filename = $Media->Caption ? $Media->Caption : sprintf('%s_%u', $Media->ContextClass, $Media->ContextID); |
||
401 | } |
||
402 | |||
403 | if (strpos($filename, '.') === false) { |
||
404 | // add extension |
||
405 | $filename .= '.'.$Media->Extension; |
||
406 | } |
||
407 | |||
408 | header('Content-Type: '.$Media->MIMEType); |
||
409 | header('Content-Disposition: attachment; filename="'.str_replace('"', '', $filename).'"'); |
||
410 | header('Content-Length: '.filesize($Media->FilesystemPath)); |
||
411 | |||
412 | readfile($Media->FilesystemPath); |
||
413 | exit(); |
||
414 | } |
||
415 | |||
416 | public static function handleCaptionRequest($media_id) |
||
449 | ]); |
||
450 | } |
||
451 | |||
452 | public static function handleDeleteRequest(ActiveRecord $Record) |
||
453 | { |
||
454 | // require authentication |
||
455 | $GLOBALS['Session']->requireAccountLevel('Staff'); |
||
456 | |||
457 | if ($mediaID = static::peekPath()) { |
||
458 | $mediaIDs = [$mediaID]; |
||
459 | } elseif (!empty($_REQUEST['mediaID'])) { |
||
460 | $mediaIDs = [$_REQUEST['mediaID']]; |
||
461 | } elseif (is_array($_REQUEST['media'])) { |
||
462 | $mediaIDs = $_REQUEST['media']; |
||
463 | } |
||
464 | |||
465 | $deleted = []; |
||
466 | foreach ($mediaIDs as $mediaID) { |
||
467 | if (!is_numeric($mediaID)) { |
||
468 | static::throwError('Invalid mediaID'); |
||
469 | } |
||
470 | |||
471 | // get media |
||
472 | $Media = Media::getByID($mediaID); |
||
473 | |||
474 | if (!$Media) { |
||
475 | static::throwNotFoundError('Media ID #%u was not found', $mediaID); |
||
476 | } |
||
477 | |||
478 | if ($Media->destroy()) { |
||
479 | $deleted[] = $Media; |
||
480 | } |
||
481 | } |
||
482 | |||
483 | return static::respond('mediaDeleted', [ |
||
484 | 'success' => true |
||
485 | ,'data' => $deleted, |
||
486 | ]); |
||
487 | } |
||
488 | |||
489 | |||
490 | |||
491 | |||
492 | |||
493 | |||
494 | public static function handleThumbnailRequest(Media $Media = null) |
||
495 | { |
||
496 | // send caching headers |
||
497 | $expires = 60*60*24*365; |
||
498 | header("Cache-Control: public, max-age=$expires"); |
||
499 | header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time()+$expires)); |
||
500 | header('Pragma: public'); |
||
501 | |||
502 | |||
503 | // thumbnails are immutable for a given URL, so no need to actually check anything if the browser wants to revalidate its cache |
||
504 | if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) { |
||
505 | header('HTTP/1.0 304 Not Modified'); |
||
506 | exit(); |
||
507 | } |
||
508 | |||
509 | |||
510 | // get media |
||
511 | if (!$Media) { |
||
512 | if (!$mediaID = static::shiftPath()) { |
||
513 | return static::throwError('Invalid request'); |
||
514 | } elseif (!$Media = Media::getByID($mediaID)) { |
||
515 | return static::throwNotFoundError('Media not found'); |
||
516 | } |
||
517 | } |
||
518 | |||
519 | |||
520 | // get format |
||
521 | if (preg_match('/^(\d+)x(\d+)(x([0-9A-F]{6})?)?$/i', static::peekPath(), $matches)) { |
||
522 | static::shiftPath(); |
||
523 | $maxWidth = $matches[1]; |
||
524 | $maxHeight = $matches[2]; |
||
525 | $fillColor = !empty($matches[4]) ? $matches[4] : false; |
||
526 | } else { |
||
527 | $maxWidth = static::$defaultThumbnailWidth; |
||
528 | $maxHeight = static::$defaultThumbnailHeight; |
||
529 | $fillColor = false; |
||
530 | } |
||
531 | |||
532 | if (static::peekPath() == 'cropped') { |
||
533 | static::shiftPath(); |
||
534 | $cropped = true; |
||
535 | } else { |
||
536 | $cropped = false; |
||
537 | } |
||
538 | |||
539 | |||
540 | // fetch thumbnail |
||
541 | try { |
||
542 | // get thumbnail |
||
543 | $thumbPath = $Media->getThumbnail($maxWidth, $maxHeight, $fillColor, $cropped); |
||
544 | } catch (Exception $e) { |
||
545 | /*\Emergence\Logger::general_warning('Caught exception while creating thumbnail for media, returning server error', array( |
||
546 | 'exceptionClass' => get_class($e) |
||
547 | ,'exceptionMessage' => $e->getMessage() |
||
548 | ,'exceptionCode' => $e->getCode() |
||
549 | ,'recordData' => $Media->getData() |
||
550 | ,'thumbFormat' => array( |
||
551 | 'maxWidth' => $maxWidth |
||
552 | ,'maxHeight' => $maxHeight |
||
553 | ,'fillColor' => $fillColor |
||
554 | ,'cropped' => $cropped |
||
555 | ) |
||
556 | ));*/ |
||
557 | |||
558 | return static::throwServerError('Thumbnail unavailable'); |
||
559 | } |
||
560 | |||
561 | |||
562 | // dump it out |
||
563 | header("ETag: media-$Media->ID-$maxWidth-$maxHeight-$fillColor-$cropped"); |
||
564 | header("Content-Type: $Media->ThumbnailMIMEType"); |
||
565 | header('Content-Length: '.filesize($thumbPath)); |
||
566 | readfile($thumbPath); |
||
567 | exit(); |
||
568 | } |
||
569 | |||
570 | |||
571 | |||
572 | public static function handleManageRequest() |
||
573 | { |
||
574 | // access control |
||
575 | $GLOBALS['Session']->requireAccountLevel('Staff'); |
||
576 | |||
577 | return static::respond('manage'); |
||
578 | } |
||
579 | |||
580 | |||
581 | |||
582 | public static function handleBrowseRequest($options = [], $conditions = [], $responseID = null, $responseData = []) |
||
605 | } |
||
606 | |||
607 | |||
608 | |||
609 | # public static function handleMediaRequest() |
||
610 | # { |
||
611 | # if(static::peekPath() == 'delete') |
||
612 | # { |
||
613 | # return static::handleMediaDeleteRequest(); |
||
614 | # } |
||
615 | # |
||
616 | # |
||
617 | # // get media |
||
618 | # $media = JSON::translateRecords(Media::getAll(), true); |
||
619 | # |
||
620 | # // get tag media assignments |
||
621 | # $media_tags = Tag::getAllItems('media'); |
||
622 | # |
||
623 | # // inject album assignments to photo records |
||
624 | # foreach($media_tags AS $media_id => $tags) |
||
625 | # { |
||
626 | # foreach($tags AS $tag) |
||
627 | # { |
||
628 | # $media[$media_id]['tags'][] = $tag['tag_id']; |
||
629 | # } |
||
630 | # } |
||
631 | # |
||
632 | # return static::respond('media', array( |
||
633 | # 'success' => true |
||
634 | # ,'data' => array_values($media) |
||
635 | # )); |
||
636 | # } |
||
637 | |||
638 | |||
639 | public static function handleMediaDeleteRequest() |
||
673 | ]); |
||
674 | } |
||
676 |
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.