Total Complexity | 109 |
Total Lines | 432 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like FileManager 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 FileManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | class FileManager |
||
36 | { |
||
37 | |||
38 | protected $path = null; |
||
39 | protected $length = null; |
||
40 | protected $basedir = null; |
||
41 | protected $basename = null; |
||
42 | protected $options; |
||
43 | protected $post; |
||
44 | protected $get; |
||
45 | |||
46 | public function __construct($options) |
||
47 | { |
||
48 | $path = FileManagerUtility::getPath(); |
||
49 | |||
50 | $this->options = array_merge( |
||
51 | [ |
||
52 | 'directory' => '../Demos/Files', |
||
53 | 'baseURL' => '', |
||
54 | 'assetBasePath' => '../Assets', |
||
55 | 'id3Path' => $path . '/Assets/getid3/getid3.php', |
||
56 | 'mimeTypesPath' => $path . '/MimeTypes.ini', |
||
57 | 'dateFormat' => 'j M Y - H:i', |
||
58 | 'maxUploadSize' => 1024 * 1024 * 3, |
||
59 | 'upload' => false, |
||
60 | 'destroy' => false, |
||
61 | 'safe' => true, |
||
62 | 'filter' => null, |
||
63 | |||
64 | // Xinha: Allow to specify the "Resize Large Images" tolerance level. |
||
65 | 'suggestedMaxImageDimension' => ['width' => 800, 'height' => 600], |
||
66 | ], |
||
67 | $options |
||
68 | ); |
||
69 | |||
70 | $this->basedir = realpath($this->options['directory']); |
||
71 | $this->basename = pathinfo($this->basedir, PATHINFO_BASENAME) . '/'; |
||
72 | $this->path = realpath($this->options['directory'] . '/../'); |
||
73 | $this->length = strlen($this->path); |
||
74 | |||
75 | header('Expires: Fri, 01 Jan 1990 00:00:00 GMT'); |
||
76 | header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate'); |
||
77 | |||
78 | $this->get = $_GET; |
||
79 | $this->post = $_POST; |
||
80 | } |
||
81 | |||
82 | public function fireEvent($event) |
||
83 | { |
||
84 | $event = $event ? 'on' . ucfirst($event) : null; |
||
85 | if (!$event || !method_exists($this, $event)) { |
||
86 | $event = 'onView'; |
||
87 | } |
||
88 | |||
89 | $this->{$event}(); |
||
90 | } |
||
91 | |||
92 | protected function onView() |
||
93 | { |
||
94 | $dir = $this->getDir(!empty($this->post['directory']) ? $this->post['directory'] : null); |
||
95 | $files = ($files = glob($dir . '/*')) ? $files : []; |
||
96 | |||
97 | if ($dir != $this->basedir) { |
||
98 | array_unshift($files, $dir . '/..'); |
||
99 | } |
||
100 | natcasesort($files); |
||
101 | foreach ($files as $file) { |
||
102 | $mime = $this->getMimeType($file); |
||
103 | if ($this->options['filter'] && 'text/directory' != $mime && !FileManagerUtility::startsWith($mime, $this->options['filter'])) { |
||
104 | continue; |
||
105 | } |
||
106 | |||
107 | $out[is_dir($file) ? 0 : 1][] = [ |
||
108 | 'name' => pathinfo($file, PATHINFO_BASENAME), |
||
109 | 'date' => date($this->options['dateFormat'], filemtime($file)), |
||
110 | 'mime' => $this->getMimeType($file), |
||
111 | 'icon' => $this->getIcon($this->normalize($file)), |
||
112 | 'size' => filesize($file) |
||
113 | ]; |
||
114 | } |
||
115 | |||
116 | echo json_encode( |
||
117 | [ |
||
118 | 'path' => $this->getPath($dir), |
||
119 | 'dir' => [ |
||
120 | 'name' => pathinfo($dir, PATHINFO_BASENAME), |
||
121 | 'date' => date($this->options['dateFormat'], filemtime($dir)), |
||
122 | 'mime' => 'text/directory', |
||
123 | 'icon' => 'dir' |
||
124 | ], |
||
125 | 'files' => array_merge(!empty($out[0]) ? $out[0] : [], !empty($out[1]) ? $out[1] : []) |
||
126 | ] |
||
127 | ); |
||
128 | } |
||
129 | |||
130 | protected function onDetail() |
||
131 | { |
||
132 | if (empty($this->post['directory']) || empty($this->post['file'])) { |
||
133 | return; |
||
134 | } |
||
135 | |||
136 | $file = realpath($this->path . '/' . $this->post['directory'] . '/' . $this->post['file']); |
||
137 | if (!$this->checkFile($file)) { |
||
138 | return; |
||
139 | } |
||
140 | |||
141 | require_once($this->options['id3Path']); |
||
142 | |||
143 | // Xinha: The URL is weird in the standard distribution of filemanager, it seems to expect |
||
144 | // that the files directory (where you are selecting/uploading) is always within the filemanager |
||
145 | // directory itself somewhere. |
||
146 | // |
||
147 | // Also the 'baseURL' seems to be wanted as the parent of the 'basedir' ("directory" option) |
||
148 | // Xinha is supplying both the same (eg url = /foo/test and dir = /home/bar/public_html/foo/test ) |
||
149 | // so we will rip off the first part of directory, below. |
||
150 | $url = $this->options['baseURL'] . '/' . preg_replace('/^[^\/]*\//', '', $this->post['directory'] . '/' . $this->post['file']); |
||
151 | |||
152 | $mime = $this->getMimeType($file); |
||
153 | $content = null; |
||
154 | |||
155 | // Xinha: We want to get some more information about what has been selected in a way |
||
156 | // we can use it. Effectively what gets put in here will be passed into the |
||
157 | // 'onDetails' event handler of your FileManager object (if any). |
||
158 | $extra_return_detail = [ |
||
159 | 'url' => $url, |
||
160 | 'mime' => $mime |
||
161 | ]; |
||
162 | |||
163 | if (FileManagerUtility::startsWith($mime, 'image/')) { |
||
164 | $size = getimagesize($file); |
||
165 | $content = '<img src="' . $url . '" class="preview" alt="" /> |
||
166 | <h2>${more}</h2> |
||
167 | <dl> |
||
168 | <dt>${width}</dt><dd>' . $size[0] . 'px</dd> |
||
169 | <dt>${height}</dt><dd>' . $size[1] . 'px</dd> |
||
170 | </dl>'; |
||
171 | |||
172 | // Xinha: Return some information about the image which can be access |
||
173 | // from the onDetails event handler in FileManager |
||
174 | $extra_return_detail['width'] = $size[0]; |
||
175 | $extra_return_detail['height'] = $size[1]; |
||
176 | } elseif (FileManagerUtility::startsWith($mime, 'text/') || 'application/x-javascript' == $mime) { |
||
177 | $filecontent = file_get_contents($file, null, null, 0, 300); |
||
178 | if (!FileManagerUtility::isBinary($filecontent)) { |
||
179 | $content = '<div class="textpreview">' . nl2br(str_replace(['$', "\t"], ['$', ' '], htmlentities($filecontent))) . '</div>'; |
||
180 | } |
||
181 | } elseif ('application/zip' == $mime) { |
||
182 | $out = [[], []]; |
||
183 | $getid3 = new getID3(); |
||
184 | $getid3->Analyze($file); |
||
185 | foreach ($getid3->info['zip']['files'] as $name => $size) { |
||
186 | $icon = is_array($size) ? 'dir' : $this->getIcon($name); |
||
187 | $out[('dir' == $icon) ? 0 : 1][$name] = '<li><a><img src="' . $this->options['assetBasePath'] . '/Icons/' . $icon . '.png" alt="" /> ' . $name . '</a></li>'; |
||
188 | } |
||
189 | natcasesort($out[0]); |
||
190 | natcasesort($out[1]); |
||
191 | $content = '<ul>' . implode(array_merge($out[0], $out[1])) . '</ul>'; |
||
192 | } elseif (FileManagerUtility::startsWith($mime, 'audio/')) { |
||
193 | $getid3 = new getID3(); |
||
194 | $getid3->Analyze($file); |
||
195 | |||
196 | $content = '<div class="object"> |
||
197 | <object type="application/x-shockwave-flash" data="' . $this->options['assetBasePath'] . '/dewplayer.swf?mp3=' . rawurlencode($url) . '&volume=30" width="200" height="20"> |
||
198 | <param name="movie" value="' . $this->options['assetBasePath'] . '/dewplayer.swf?mp3=' . rawurlencode($url) . '&volume=30" /> |
||
199 | </object> |
||
200 | </div> |
||
201 | <h2>${more}</h2> |
||
202 | <dl> |
||
203 | <dt>${title}</dt><dd>' . $getid3->info['comments']['title'][0] . '</dd> |
||
204 | <dt>${artist}</dt><dd>' . $getid3->info['comments']['artist'][0] . '</dd> |
||
205 | <dt>${album}</dt><dd>' . $getid3->info['comments']['album'][0] . '</dd> |
||
206 | <dt>${length}</dt><dd>' . $getid3->info['playtime_string'] . '</dd> |
||
207 | <dt>${bitrate}</dt><dd>' . round($getid3->info['bitrate'] / 1000) . 'kbps</dd> |
||
208 | </dl>'; |
||
209 | } |
||
210 | |||
211 | echo json_encode( |
||
212 | array_merge( |
||
213 | [ |
||
214 | 'content' => $content ? $content : '<div class="margin"> |
||
215 | ${nopreview}<br/><button value="' . $url . '">${download}</button> |
||
216 | </div>' |
||
217 | ], |
||
218 | $extra_return_detail |
||
219 | ) |
||
220 | ); |
||
221 | } |
||
222 | |||
223 | protected function onDestroy() |
||
224 | { |
||
225 | if (!$this->options['destroy'] || empty($this->post['directory']) || empty($this->post['file'])) { |
||
226 | return; |
||
227 | } |
||
228 | |||
229 | $file = realpath($this->path . '/' . $this->post['directory'] . '/' . $this->post['file']); |
||
230 | if (!$this->checkFile($file)) { |
||
231 | return; |
||
232 | } |
||
233 | |||
234 | $this->unlink($file); |
||
235 | |||
236 | echo json_encode( |
||
237 | [ |
||
238 | 'content' => 'destroyed' |
||
239 | ] |
||
240 | ); |
||
241 | } |
||
242 | |||
243 | protected function onCreate() |
||
244 | { |
||
245 | if ($this->options['upload']) { |
||
246 | if (empty($this->post['directory']) || empty($this->post['file'])) { |
||
247 | return; |
||
248 | } |
||
249 | |||
250 | $file = $this->getName($this->post['file'], $this->getDir($this->post['directory'])); |
||
251 | if (!$file) { |
||
252 | return; |
||
253 | } |
||
254 | |||
255 | mkdir($file); |
||
256 | } |
||
257 | |||
258 | $this->onView(); |
||
259 | } |
||
260 | |||
261 | protected function onUpload() |
||
262 | { |
||
263 | try { |
||
264 | if (!$this->options['upload']) { |
||
265 | throw new FileManagerException('disabled'); |
||
266 | } |
||
267 | if (empty($this->get['directory']) || (function_exists('UploadIsAuthenticated') && !UploadIsAuthenticated($this->get))) { |
||
268 | throw new FileManagerException('authenticated'); |
||
269 | } |
||
270 | |||
271 | $dir = $this->getDir($this->get['directory']); |
||
272 | $name = pathinfo((Upload::exists('Filedata')) ? $this->getName($_FILES['Filedata']['name'], $dir) : null, PATHINFO_FILENAME); |
||
273 | $file = Upload::move( |
||
274 | 'Filedata', |
||
275 | $dir . '/', |
||
276 | [ |
||
277 | 'name' => $name, |
||
278 | 'extension' => $this->options['safe'] && $name && in_array(strtolower(pathinfo($_FILES['Filedata']['name'], PATHINFO_EXTENSION)), ['exe', 'dll', 'php', 'php3', 'php4', 'php5', 'phps']) ? 'txt' : null, |
||
279 | 'size' => $this->options['maxUploadSize'], |
||
280 | 'mimes' => $this->getAllowedMimeTypes() |
||
281 | ] |
||
282 | ); |
||
283 | |||
284 | if (FileManagerUtility::startsWith(Upload::mime($file), 'image/') && !empty($this->get['resize'])) { |
||
285 | $img = new Image($file); |
||
286 | $size = $img->getSize(); |
||
287 | if ($size['width'] > $this->options['suggestedMaxImageDimension']['width']) { |
||
288 | $img->resize($this->options['suggestedMaxImageDimension']['width'])->save(); |
||
289 | } elseif ($size['height'] > $this->options['suggestedMaxImageDimension']['height']) { |
||
290 | $img->resize(null, $this->options['suggestedMaxImageDimension']['height'])->save(); |
||
291 | } |
||
292 | } |
||
293 | |||
294 | echo json_encode( |
||
295 | [ |
||
296 | 'status' => 1, |
||
297 | 'name' => pathinfo($file, PATHINFO_BASENAME) |
||
298 | ] |
||
299 | ); |
||
300 | } catch (UploadException $e) { |
||
301 | echo json_encode( |
||
302 | [ |
||
303 | 'status' => 0, |
||
304 | 'error' => class_exists('ValidatorException') ? strip_tags($e->getMessage()) : '${upload.' . $e->getMessage() . '}' // This is for Styx :) |
||
305 | ] |
||
306 | ); |
||
307 | } catch (FileManagerException $e) { |
||
308 | echo json_encode( |
||
309 | [ |
||
310 | 'status' => 0, |
||
311 | 'error' => '${upload.' . $e->getMessage() . '}' |
||
312 | ] |
||
313 | ); |
||
314 | } |
||
315 | } |
||
316 | |||
317 | /* This method is used by both move and rename */ |
||
318 | protected function onMove() |
||
357 | ] |
||
358 | ); |
||
359 | } |
||
360 | |||
361 | protected function unlink($file) |
||
362 | { |
||
363 | $file = realpath($file); |
||
364 | if ($this->basedir == $file || strlen($this->basedir) >= strlen($file)) { |
||
365 | return; |
||
366 | } |
||
367 | |||
368 | if (is_dir($file)) { |
||
369 | $files = glob($file . '/*'); |
||
370 | if (is_array($files)) { |
||
371 | foreach ($files as $f) { |
||
372 | $this->unlink($f); |
||
373 | } |
||
374 | } |
||
375 | |||
376 | rmdir($file); |
||
377 | } else { |
||
378 | try { |
||
379 | if ($this->checkFile($file)) { |
||
380 | unlink($file); |
||
381 | } |
||
382 | } catch (Exception $e) { |
||
383 | } |
||
384 | } |
||
385 | } |
||
386 | |||
387 | protected function getName($file, $dir) |
||
388 | { |
||
389 | $files = []; |
||
390 | foreach ((array)glob($dir . '/*') as $f) { |
||
391 | $files[] = pathinfo($f, PATHINFO_FILENAME); |
||
392 | } |
||
393 | |||
394 | $pathinfo = pathinfo($file); |
||
395 | $file = $dir . '/' . FileManagerUtility::pagetitle($pathinfo['filename'], $files) . (!empty($pathinfo['extension']) ? '.' . $pathinfo['extension'] : null); |
||
396 | |||
397 | return !$file || !FileManagerUtility::startsWith($file, $this->basedir) || file_exists($file) ? null : $file; |
||
398 | } |
||
399 | |||
400 | protected function getIcon($file) |
||
401 | { |
||
402 | if (FileManagerUtility::endsWith($file, '/..')) { |
||
403 | return 'dir_up'; |
||
404 | } elseif (is_dir($file)) { |
||
405 | return 'dir'; |
||
406 | } |
||
407 | |||
408 | $ext = pathinfo($file, PATHINFO_EXTENSION); |
||
409 | return ($ext && file_exists(realpath($this->options['assetBasePath'] . '/Icons/' . $ext . '.png'))) ? $ext : 'default'; |
||
410 | } |
||
411 | |||
412 | protected function getMimeType($file) |
||
413 | { |
||
414 | return is_dir($file) ? 'text/directory' : Upload::mime($file); |
||
415 | } |
||
416 | |||
417 | protected function getDir($dir) |
||
421 | } |
||
422 | |||
423 | protected function getPath($file) |
||
424 | { |
||
425 | $file = $this->normalize(substr($file, $this->length)); |
||
426 | return substr($file, FileManagerUtility::startsWith($file, '/') ? 1 : 0); |
||
427 | } |
||
428 | |||
429 | protected function checkFile($file) |
||
430 | { |
||
431 | $mimes = $this->getAllowedMimeTypes(); |
||
432 | $hasFilter = $this->options['filter'] && count($mimes); |
||
433 | if ($hasFilter) { |
||
434 | array_push($mimes, 'text/directory'); |
||
435 | } |
||
436 | return !(!$file || !FileManagerUtility::startsWith($file, $this->basedir) || !file_exists($file) || ($hasFilter && !in_array($this->getMimeType($file), $mimes))); |
||
437 | } |
||
438 | |||
439 | protected function normalize($file) |
||
440 | { |
||
441 | return preg_replace('/\\\|\/{2,}/', '/', $file); |
||
442 | } |
||
443 | |||
444 | protected function getAllowedMimeTypes() |
||
467 | } |
||
468 | |||
469 | } |
||
470 | |||
471 | class FileManagerException extends Exception |
||
472 | { |
||
473 | } |
||
474 | |||
475 | /* Stripped-down version of some Styx PHP Framework-Functionality bundled with this FileBrowser. Styx is located at: http://styx.og5.net */ |
||
476 | |||
477 | class FileManagerUtility |
||
478 | { |
||
479 | |||
480 | public static function endsWith($string, $look) |
||
544 |