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