Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like fileController 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 fileController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class fileController extends file |
||
8 | { |
||
9 | /** |
||
10 | * Initialization |
||
11 | * @return void |
||
12 | */ |
||
13 | function init() |
||
16 | |||
17 | /** |
||
18 | * Upload attachments in the editor |
||
19 | * |
||
20 | * Determine the upload target srl from editor_sequence and uploadTargetSrl variables. |
||
21 | * Create and return the UploadTargetSrl if not exists so that UI can use the value |
||
22 | * for sync. |
||
23 | * |
||
24 | * @return void |
||
25 | */ |
||
26 | function procFileUpload() |
||
27 | { |
||
28 | Context::setRequestMethod('JSON'); |
||
29 | $file_info = $_FILES['Filedata']; |
||
30 | |||
31 | // An error appears if not a normally uploaded file |
||
32 | if(!is_uploaded_file($file_info['tmp_name'])) exit(); |
||
33 | |||
34 | // Basic variables setting |
||
35 | $oFileModel = getModel('file'); |
||
|
|||
36 | $editor_sequence = Context::get('editor_sequence'); |
||
37 | $upload_target_srl = intval(Context::get('uploadTargetSrl')); |
||
38 | if(!$upload_target_srl) $upload_target_srl = intval(Context::get('upload_target_srl')); |
||
39 | $module_srl = $this->module_srl; |
||
40 | // Exit a session if there is neither upload permission nor information |
||
41 | if(!$_SESSION['upload_info'][$editor_sequence]->enabled) exit(); |
||
42 | // Extract from session information if upload_target_srl is not specified |
||
43 | if(!$upload_target_srl) $upload_target_srl = $_SESSION['upload_info'][$editor_sequence]->upload_target_srl; |
||
44 | // Create if upload_target_srl is not defined in the session information |
||
45 | if(!$upload_target_srl) $_SESSION['upload_info'][$editor_sequence]->upload_target_srl = $upload_target_srl = getNextSequence(); |
||
46 | |||
47 | $output = $this->insertFile($file_info, $module_srl, $upload_target_srl); |
||
48 | Context::setResponseMethod('JSON'); |
||
49 | if($output->error != '0') $this->stop($output->message); |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Iframe upload attachments |
||
54 | * |
||
55 | * @return Object |
||
56 | */ |
||
57 | function procFileIframeUpload() |
||
58 | { |
||
59 | // Basic variables setting |
||
60 | $editor_sequence = Context::get('editor_sequence'); |
||
61 | $callback = Context::get('callback'); |
||
62 | $module_srl = $this->module_srl; |
||
63 | $upload_target_srl = intval(Context::get('uploadTargetSrl')); |
||
64 | if(!$upload_target_srl) $upload_target_srl = intval(Context::get('upload_target_srl')); |
||
65 | |||
66 | // Exit a session if there is neither upload permission nor information |
||
67 | if(!$_SESSION['upload_info'][$editor_sequence]->enabled) exit(); |
||
68 | // Extract from session information if upload_target_srl is not specified |
||
69 | if(!$upload_target_srl) $upload_target_srl = $_SESSION['upload_info'][$editor_sequence]->upload_target_srl; |
||
70 | // Create if upload_target_srl is not defined in the session information |
||
71 | if(!$upload_target_srl) $_SESSION['upload_info'][$editor_sequence]->upload_target_srl = $upload_target_srl = getNextSequence(); |
||
72 | // Delete and then attempt to re-upload if file_srl is requested |
||
73 | $file_srl = Context::get('file_srl'); |
||
74 | if($file_srl) $this->deleteFile($file_srl); |
||
75 | |||
76 | $file_info = Context::get('Filedata'); |
||
77 | // An error appears if not a normally uploaded file |
||
78 | if(is_uploaded_file($file_info['tmp_name'])) { |
||
79 | $output = $this->insertFile($file_info, $module_srl, $upload_target_srl); |
||
80 | Context::set('uploaded_fileinfo',$output); |
||
81 | } |
||
82 | |||
83 | Context::set('layout','none'); |
||
84 | |||
85 | $this->setTemplatePath($this->module_path.'tpl'); |
||
86 | $this->setTemplateFile('iframe'); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Image resize |
||
91 | * |
||
92 | * @return Object |
||
93 | */ |
||
94 | function procFileImageResize() |
||
95 | { |
||
96 | $file_srl = Context::get('file_srl'); |
||
97 | $width = Context::get('width'); |
||
98 | $height = Context::get('height'); |
||
99 | |||
100 | if(!$file_srl || !$width) |
||
101 | { |
||
102 | return new Object(-1,'msg_invalid_request'); |
||
103 | } |
||
104 | |||
105 | $oFileModel = getModel('file'); |
||
106 | $fileInfo = $oFileModel->getFile($file_srl); |
||
107 | if(!$fileInfo || $fileInfo->direct_download != 'Y') |
||
108 | { |
||
109 | return new Object(-1,'msg_invalid_request'); |
||
110 | } |
||
111 | |||
112 | $source_src = $fileInfo->uploaded_filename; |
||
113 | $output_src = $source_src . '.resized' . strrchr($source_src,'.'); |
||
114 | |||
115 | if(!$height) $height = $width-1; |
||
116 | |||
117 | if(FileHandler::createImageFile($source_src,$output_src,$width,$height,'','ratio')) |
||
118 | { |
||
119 | $output = new stdClass(); |
||
120 | $output->info = getimagesize($output_src); |
||
121 | $output->src = $output_src; |
||
122 | } |
||
123 | else |
||
124 | { |
||
125 | return new Object(-1,'msg_invalid_request'); |
||
126 | } |
||
127 | |||
128 | $this->add('resized_info',$output); |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Download Attachment |
||
133 | * |
||
134 | * <pre> |
||
135 | * Receive a request directly |
||
136 | * file_srl: File sequence |
||
137 | * sid : value in DB for comparison, No download if not matched |
||
138 | * |
||
139 | * This method call trigger 'file.downloadFile'. |
||
140 | * before, after. |
||
141 | * Trigger object contains: |
||
142 | * - download_url |
||
143 | * - file_srl |
||
144 | * - upload_target_srl |
||
145 | * - upload_target_type |
||
146 | * - sid |
||
147 | * - module_srl |
||
148 | * - member_srl |
||
149 | * - download_count |
||
150 | * - direct_download |
||
151 | * - source_filename |
||
152 | * - uploaded_filename |
||
153 | * - file_size |
||
154 | * - comment |
||
155 | * - isvalid |
||
156 | * - regdate |
||
157 | * - ipaddress |
||
158 | * </pre> |
||
159 | * |
||
160 | * return void |
||
161 | */ |
||
162 | function procFileDownload() |
||
286 | |||
287 | public function procFileOutput() |
||
288 | { |
||
289 | $oFileModel = getModel('file'); |
||
364 | |||
365 | /** |
||
366 | * Delete an attachment from the editor |
||
367 | * |
||
368 | * @return Object |
||
369 | */ |
||
370 | function procFileDelete() |
||
408 | |||
409 | /** |
||
410 | * get file list |
||
411 | * |
||
412 | * @return Object |
||
413 | */ |
||
414 | function procFileGetList() |
||
445 | /** |
||
446 | * A trigger to return numbers of attachments in the upload_target_srl (document_srl) |
||
447 | * |
||
448 | * @param object $obj Trigger object |
||
449 | * @return Object |
||
450 | */ |
||
451 | View Code Duplication | function triggerCheckAttached(&$obj) |
|
461 | |||
462 | /** |
||
463 | * A trigger to link the attachment with the upload_target_srl (document_srl) |
||
464 | * |
||
465 | * @param object $obj Trigger object |
||
466 | * @return Object |
||
467 | */ |
||
468 | function triggerAttachFiles(&$obj) |
||
478 | |||
479 | /** |
||
480 | * A trigger to delete the attachment in the upload_target_srl (document_srl) |
||
481 | * |
||
482 | * @param object $obj Trigger object |
||
483 | * @return Object |
||
484 | */ |
||
485 | function triggerDeleteAttached(&$obj) |
||
493 | |||
494 | /** |
||
495 | * A trigger to return numbers of attachments in the upload_target_srl (comment_srl) |
||
496 | * |
||
497 | * @param object $obj Trigger object |
||
498 | * @return Object |
||
499 | */ |
||
500 | View Code Duplication | function triggerCommentCheckAttached(&$obj) |
|
510 | |||
511 | /** |
||
512 | * A trigger to link the attachment with the upload_target_srl (comment_srl) |
||
513 | * |
||
514 | * @param object $obj Trigger object |
||
515 | * @return Object |
||
516 | */ |
||
517 | function triggerCommentAttachFiles(&$obj) |
||
528 | |||
529 | /** |
||
530 | * A trigger to delete the attachment in the upload_target_srl (comment_srl) |
||
531 | * |
||
532 | * @param object $obj Trigger object |
||
533 | * @return Object |
||
534 | */ |
||
535 | function triggerCommentDeleteAttached(&$obj) |
||
545 | |||
546 | /** |
||
547 | * A trigger to delete all the attachements when deleting the module |
||
548 | * |
||
549 | * @param object $obj Trigger object |
||
550 | * @return Object |
||
551 | */ |
||
552 | function triggerDeleteModuleFiles(&$obj) |
||
560 | |||
561 | /** |
||
562 | * Upload enabled |
||
563 | * |
||
564 | * @param int $editor_sequence |
||
565 | * @param int $upload_target_srl |
||
566 | * @return void |
||
567 | */ |
||
568 | function setUploadInfo($editor_sequence, $upload_target_srl=0) |
||
577 | |||
578 | /** |
||
579 | * Set the attachements of the upload_target_srl to be valid |
||
580 | * By changing its state to valid when a document is inserted, it prevents from being considered as a unnecessary file |
||
581 | * |
||
582 | * @param int $upload_target_srl |
||
583 | * @return Object |
||
584 | */ |
||
585 | function setFilesValid($upload_target_srl) |
||
591 | |||
592 | /** |
||
593 | * Add an attachement |
||
594 | * |
||
595 | * <pre> |
||
596 | * This method call trigger 'file.insertFile'. |
||
597 | * |
||
598 | * Before trigger object contains: |
||
599 | * - module_srl |
||
600 | * - upload_target_srl |
||
601 | * |
||
602 | * After trigger object contains: |
||
603 | * - file_srl |
||
604 | * - upload_target_srl |
||
605 | * - module_srl |
||
606 | * - direct_download |
||
607 | * - source_filename |
||
608 | * - uploaded_filename |
||
609 | * - donwload_count |
||
610 | * - file_size |
||
611 | * - comment |
||
612 | * - member_srl |
||
613 | * - sid |
||
614 | * </pre> |
||
615 | * |
||
616 | * @param object $file_info PHP file information array |
||
617 | * @param int $module_srl Sequence of module to upload file |
||
618 | * @param int $upload_target_srl Sequence of target to upload file |
||
619 | * @param int $download_count Initial download count |
||
620 | * @param bool $manual_insert If set true, pass validation check |
||
621 | * @return Object |
||
622 | */ |
||
623 | function insertFile($file_info, $module_srl, $upload_target_srl, $download_count = 0, $manual_insert = false) |
||
624 | { |
||
625 | // Call a trigger (before) |
||
626 | $trigger_obj = new stdClass; |
||
627 | $trigger_obj->module_srl = $module_srl; |
||
628 | $trigger_obj->upload_target_srl = $upload_target_srl; |
||
629 | $output = ModuleHandler::triggerCall('file.insertFile', 'before', $trigger_obj); |
||
630 | if(!$output->toBool()) return $output; |
||
631 | |||
632 | // A workaround for Firefox upload bug |
||
633 | if(preg_match('/^=\?UTF-8\?B\?(.+)\?=$/i', $file_info['name'], $match)) |
||
634 | { |
||
635 | $file_info['name'] = base64_decode(strtr($match[1], ':', '/')); |
||
636 | } |
||
637 | |||
638 | if(!$manual_insert) |
||
639 | { |
||
640 | // Get the file configurations |
||
641 | $logged_info = Context::get('logged_info'); |
||
642 | if($logged_info->is_admin != 'Y') |
||
643 | { |
||
644 | $oFileModel = getModel('file'); |
||
645 | $config = $oFileModel->getFileConfig($module_srl); |
||
646 | |||
647 | // check file type |
||
648 | if(isset($config->allowed_filetypes) && $config->allowed_filetypes !== '*.*') |
||
649 | { |
||
650 | $filetypes = explode(';', $config->allowed_filetypes); |
||
651 | $ext = array(); |
||
652 | foreach($filetypes as $item) { |
||
653 | $item = explode('.', $item); |
||
654 | $ext[] = strtolower($item[1]); |
||
655 | } |
||
656 | $uploaded_ext = explode('.', $file_info['name']); |
||
657 | $uploaded_ext = strtolower(array_pop($uploaded_ext)); |
||
658 | |||
659 | if(!in_array($uploaded_ext, $ext)) |
||
660 | { |
||
661 | return $this->stop('msg_not_allowed_filetype'); |
||
662 | } |
||
663 | } |
||
664 | |||
665 | $allowed_filesize = $config->allowed_filesize * 1024 * 1024; |
||
666 | $allowed_attach_size = $config->allowed_attach_size * 1024 * 1024; |
||
667 | // An error appears if file size exceeds a limit |
||
668 | if($allowed_filesize < filesize($file_info['tmp_name'])) return new Object(-1, 'msg_exceeds_limit_size'); |
||
669 | // Get total file size of all attachements (from DB) |
||
670 | $size_args = new stdClass; |
||
671 | $size_args->upload_target_srl = $upload_target_srl; |
||
672 | $output = executeQuery('file.getAttachedFileSize', $size_args); |
||
673 | $attached_size = (int)$output->data->attached_size + filesize($file_info['tmp_name']); |
||
674 | if($attached_size > $allowed_attach_size) return new Object(-1, 'msg_exceeds_limit_size'); |
||
675 | } |
||
676 | } |
||
677 | |||
678 | // https://github.com/xpressengine/xe-core/issues/1713 |
||
679 | $file_info['name'] = preg_replace('/\.(php|phtm|phar|html?|cgi|pl|exe|jsp|asp|inc)/i', '$0-x',$file_info['name']); |
||
680 | $file_info['name'] = removeHackTag($file_info['name']); |
||
681 | $file_info['name'] = str_replace(array('<','>'),array('%3C','%3E'),$file_info['name']); |
||
682 | |||
683 | // Get random number generator |
||
684 | $random = new Password(); |
||
685 | |||
686 | // Set upload path by checking if the attachement is an image or other kinds of file |
||
687 | if(preg_match("/\.(jpe?g|gif|png|wm[va]|mpe?g|avi|swf|flv|mp[1-4]|as[fx]|wav|midi?|moo?v|qt|r[am]{1,2}|m4v)$/i", $file_info['name'])) |
||
688 | { |
||
689 | $path = sprintf("./files/attach/images/%s/%s", $module_srl,getNumberingPath($upload_target_srl,3)); |
||
690 | |||
691 | // special character to '_' |
||
692 | // change to random file name. because window php bug. window php is not recognize unicode character file name - by cherryfilter |
||
693 | $ext = substr(strrchr($file_info['name'],'.'),1); |
||
694 | //$_filename = preg_replace('/[#$&*?+%"\']/', '_', $file_info['name']); |
||
695 | $_filename = $random->createSecureSalt(32, 'hex').'.'.$ext; |
||
696 | $filename = $path.$_filename; |
||
697 | $idx = 1; |
||
698 | View Code Duplication | while(file_exists($filename)) |
|
699 | { |
||
700 | $filename = $path.preg_replace('/\.([a-z0-9]+)$/i','_'.$idx.'.$1',$_filename); |
||
701 | $idx++; |
||
702 | } |
||
703 | $direct_download = 'Y'; |
||
704 | } |
||
705 | else |
||
706 | { |
||
707 | $path = sprintf("./files/attach/binaries/%s/%s", $module_srl, getNumberingPath($upload_target_srl,3)); |
||
708 | $filename = $path.$random->createSecureSalt(32, 'hex'); |
||
709 | $direct_download = 'N'; |
||
710 | } |
||
711 | // Create a directory |
||
712 | if(!FileHandler::makeDir($path)) return new Object(-1,'msg_not_permitted_create'); |
||
713 | |||
714 | // Check uploaded file |
||
715 | if(!checkUploadedFile($file_info['tmp_name'])) return new Object(-1,'msg_file_upload_error'); |
||
716 | |||
717 | // Get random number generator |
||
718 | $random = new Password(); |
||
719 | |||
720 | // Move the file |
||
721 | if($manual_insert) |
||
722 | { |
||
723 | @copy($file_info['tmp_name'], $filename); |
||
724 | if(!file_exists($filename)) |
||
725 | { |
||
726 | $filename = $path.$random->createSecureSalt(32, 'hex').'.'.$ext; |
||
727 | @copy($file_info['tmp_name'], $filename); |
||
728 | } |
||
729 | } |
||
730 | else |
||
731 | { |
||
732 | if(!@move_uploaded_file($file_info['tmp_name'], $filename)) |
||
733 | { |
||
734 | $filename = $path.$random->createSecureSalt(32, 'hex').'.'.$ext; |
||
735 | if(!@move_uploaded_file($file_info['tmp_name'], $filename)) return new Object(-1,'msg_file_upload_error'); |
||
736 | } |
||
737 | } |
||
738 | // Get member information |
||
739 | $oMemberModel = getModel('member'); |
||
740 | $member_srl = $oMemberModel->getLoggedMemberSrl(); |
||
741 | // List file information |
||
742 | $args = new stdClass; |
||
743 | $args->file_srl = getNextSequence(); |
||
744 | $args->upload_target_srl = $upload_target_srl; |
||
745 | $args->module_srl = $module_srl; |
||
746 | $args->direct_download = $direct_download; |
||
747 | $args->source_filename = $file_info['name']; |
||
748 | $args->uploaded_filename = $filename; |
||
749 | $args->download_count = $download_count; |
||
750 | $args->file_size = @filesize($filename); |
||
751 | $args->comment = NULL; |
||
752 | $args->member_srl = $member_srl; |
||
753 | $args->sid = $random->createSecureSalt(32, 'hex'); |
||
754 | |||
755 | $output = executeQuery('file.insertFile', $args); |
||
756 | if(!$output->toBool()) return $output; |
||
757 | // Call a trigger (after) |
||
758 | $trigger_output = ModuleHandler::triggerCall('file.insertFile', 'after', $args); |
||
759 | if(!$trigger_output->toBool()) return $trigger_output; |
||
760 | |||
761 | $_SESSION['__XE_UPLOADING_FILES_INFO__'][$args->file_srl] = true; |
||
762 | |||
763 | $output->add('file_srl', $args->file_srl); |
||
764 | $output->add('file_size', $args->file_size); |
||
765 | $output->add('sid', $args->sid); |
||
766 | $output->add('direct_download', $args->direct_download); |
||
767 | $output->add('source_filename', $args->source_filename); |
||
768 | $output->add('upload_target_srl', $upload_target_srl); |
||
769 | $output->add('uploaded_filename', $args->uploaded_filename); |
||
770 | return $output; |
||
771 | } |
||
772 | |||
773 | /** |
||
774 | * Delete the attachment |
||
775 | * |
||
776 | * <pre> |
||
777 | * This method call trigger 'file.deleteFile'. |
||
778 | * Before, after trigger object contains: |
||
779 | * - download_url |
||
780 | * - file_srl |
||
781 | * - upload_target_srl |
||
782 | * - upload_target_type |
||
783 | * - sid |
||
784 | * - module_srl |
||
785 | * - member_srl |
||
786 | * - download_count |
||
787 | * - direct_download |
||
788 | * - source_filename |
||
789 | * - uploaded_filename |
||
790 | * - file_size |
||
791 | * - comment |
||
792 | * - isvalid |
||
793 | * - regdate |
||
794 | * - ipaddress |
||
795 | * </pre> |
||
796 | * |
||
797 | * @param int $file_srl Sequence of file to delete |
||
798 | * @return Object |
||
799 | */ |
||
800 | function deleteFile($file_srl) |
||
858 | |||
859 | /** |
||
860 | * Delete all attachments of a particular document |
||
861 | * |
||
862 | * @param int $upload_target_srl Upload target srl to delete files |
||
863 | * @return Object |
||
864 | */ |
||
865 | function deleteFiles($upload_target_srl) |
||
900 | |||
901 | /** |
||
902 | * Move an attachement to the other document |
||
903 | * |
||
904 | * @param int $source_srl Sequence of target to move |
||
905 | * @param int $target_module_srl New squence of module |
||
906 | * @param int $target_srl New sequence of target |
||
907 | * @return void |
||
908 | */ |
||
909 | function moveFile($source_srl, $target_module_srl, $target_srl) |
||
951 | |||
952 | public function procFileSetCoverImage() |
||
953 | { |
||
954 | $vars = Context::getRequestVars(); |
||
955 | $logged_info = Context::get('logged_info'); |
||
956 | |||
957 | if(!$vars->editor_sequence) return new Object(-1, 'msg_invalid_request'); |
||
958 | |||
959 | $upload_target_srl = $_SESSION['upload_info'][$vars->editor_sequence]->upload_target_srl; |
||
960 | |||
961 | $oFileModel = getModel('file'); |
||
962 | $file_info = $oFileModel->getFile($vars->file_srl); |
||
963 | |||
964 | if(!$file_info) return new Object(-1, 'msg_not_founded'); |
||
965 | |||
966 | if(!$this->manager && !$file_info->member_srl === $logged_info->member_srl) return new Object(-1, 'msg_not_permitted'); |
||
967 | |||
968 | $args = new stdClass(); |
||
969 | $args->file_srl = $vars->file_srl; |
||
970 | $args->upload_target_srl = $upload_target_srl; |
||
971 | |||
972 | $oDB = &DB::getInstance(); |
||
973 | $oDB->begin(); |
||
974 | |||
975 | $args->cover_image = 'N'; |
||
976 | $output = executeQuery('file.updateClearCoverImage', $args); |
||
977 | if(!$output->toBool()) |
||
978 | { |
||
979 | $oDB->rollback(); |
||
980 | return $output; |
||
981 | } |
||
982 | |||
983 | $args->cover_image = 'Y'; |
||
984 | $output = executeQuery('file.updateCoverImage', $args); |
||
985 | if(!$output->toBool()) |
||
986 | { |
||
987 | $oDB->rollback(); |
||
988 | return $output; |
||
989 | } |
||
990 | |||
991 | $oDB->commit(); |
||
992 | |||
993 | // 썸네일 삭제 |
||
994 | $thumbnail_path = sprintf('files/thumbnails/%s', getNumberingPath($upload_target_srl, 3)); |
||
995 | Filehandler::removeFilesInDir($thumbnail_path); |
||
996 | } |
||
997 | |||
998 | /** |
||
999 | * Find the attachment where a key is upload_target_srl and then return java script code |
||
1000 | * |
||
1001 | * @deprecated |
||
1002 | * @param int $editor_sequence |
||
1003 | * @param int $upload_target_srl |
||
1004 | * @return void |
||
1005 | */ |
||
1006 | function printUploadedFileList($editor_sequence, $upload_target_srl) |
||
1010 | |||
1011 | View Code Duplication | function triggerCopyModule(&$obj) |
|
1025 | } |
||
1026 | /* End of file file.controller.php */ |
||
1027 | /* Location: ./modules/file/file.controller.php */ |
||
1029 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.