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 JUpload 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 JUpload, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 55 | class JUpload { |
||
| 56 | |||
| 57 | var $appletparams; |
||
|
|
|||
| 58 | var $classparams; |
||
| 59 | var $files; |
||
| 60 | |||
| 61 | public function JUpload($appletparams = array(), $classparams = array()) { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Return an array of uploaded files * The array contains: name, size, tmp_name, error, |
||
| 192 | * relativePath, md5sum, mimetype, fullName, path |
||
| 193 | */ |
||
| 194 | public function uploadedfiles() { |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Log a message on the current output, as a HTML comment. |
||
| 200 | */ |
||
| 201 | protected function logDebug($function, $msg, $htmlComment=true) { |
||
| 202 | $output = "[DEBUG] [$function] $msg"; |
||
| 203 | if ($htmlComment) { |
||
| 204 | echo("<!-- $output -->\r\n"); |
||
| 205 | } else { |
||
| 206 | echo("$output\r\n"); |
||
| 207 | } |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Log a message to the PHP log. |
||
| 212 | * Declared "protected" so it may be Extended if you require customised logging (e.g. particular log file location). |
||
| 213 | */ |
||
| 214 | protected function logPHPDebug($function, $msg) { |
||
| 215 | if ($this->classparams['debug_php'] === true) { |
||
| 216 | $output = "[DEBUG] [$function] ".$this->arrayexpand($msg); |
||
| 217 | error_log($output); |
||
| 218 | } |
||
| 219 | } |
||
| 220 | |||
| 221 | private function arrayexpand($array) { |
||
| 222 | $output = ''; |
||
| 223 | if (is_array($array)) { |
||
| 224 | foreach ($array as $key => $value) { |
||
| 225 | $output .= "\n ".$key.' => '.$this->arrayexpand($value); |
||
| 226 | } |
||
| 227 | } else { |
||
| 228 | $output .= $array; |
||
| 229 | } |
||
| 230 | return $output; |
||
| 231 | } |
||
| 232 | |||
| 233 | |||
| 234 | /** |
||
| 235 | * Convert a value ending in 'G','M' or 'K' to bytes |
||
| 236 | * |
||
| 237 | */ |
||
| 238 | private function tobytes($val) { |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Build a string, containing a javascript wrapper function |
||
| 254 | * for setting applet properties via JavaScript. This is necessary, |
||
| 255 | * because we use the "modern" method of including the applet (using |
||
| 256 | * <object> resp. <embed> tags) in order to trigger automatic JRE downloading. |
||
| 257 | * Therefore, in Netscape-like browsers, the applet is accessible via |
||
| 258 | * the document.embeds[] array while in others, it is accessible via the |
||
| 259 | * document.applets[] array. |
||
| 260 | * |
||
| 261 | * @return A string, containing the necessary wrapper function (named JUploadSetProperty) |
||
| 262 | */ |
||
| 263 | private function str_jsinit() { |
||
| 264 | $N = "\n"; |
||
| 265 | $name = $this->appletparams['name']; |
||
| 266 | $ret = '<script type="text/javascript">'.$N; |
||
| 267 | $ret .= '<!--'.$N; |
||
| 268 | $ret .= 'function '.$this->classparams['jscript_wrapper'].'(name, value) {'.$N; |
||
| 269 | $ret .= ' document.applets["'.$name.'"] == null || document.applets["'.$name.'"].setProperty(name,value);'.$N; |
||
| 270 | $ret .= ' document.embeds["'.$name.'"] == null || document.embeds["'.$name.'"].setProperty(name,value);'.$N; |
||
| 271 | $ret .= '}'.$N; |
||
| 272 | $ret .= '//-->'.$N; |
||
| 273 | $ret .= '</script>'; |
||
| 274 | return $ret; |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Build a string, containing the applet tag with all parameters. |
||
| 279 | * |
||
| 280 | * @return A string, containing the applet tag |
||
| 281 | */ |
||
| 282 | private function str_applet() { |
||
| 283 | $N = "\n"; |
||
| 284 | $params = $this->appletparams; |
||
| 285 | // return the actual applet tag |
||
| 286 | $ret = '<object classid = "clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"'.$N; |
||
| 287 | $ret .= ' codebase = "http://java.sun.com/update/1.5.0/jinstall-1_5-windows-i586.cab#Version=5,0,0,3"'.$N; |
||
| 288 | $ret .= ' width = "'.$params['width'].'"'.$N; |
||
| 289 | $ret .= ' height = "'.$params['height'].'"'.$N; |
||
| 290 | $ret .= ' name = "'.$params['name'].'">'.$N; |
||
| 291 | foreach ($params as $key => $val) { |
||
| 292 | if ($key !== 'width' && $key !== 'height') |
||
| 293 | $ret .= ' <param name = "'.$key.'" value = "'.$val.'" />'.$N; |
||
| 294 | } |
||
| 295 | $ret .= ' <comment>'.$N; |
||
| 296 | $ret .= ' <embed'.$N; |
||
| 297 | $ret .= ' type = "application/x-java-applet;version=1.5"'.$N; |
||
| 298 | foreach ($params as $key => $val) |
||
| 299 | $ret .= ' '.$key.' = "'.$val.'"'.$N; |
||
| 300 | $ret .= ' pluginspage = "http://java.sun.com/products/plugin/index.html#download">'.$N; |
||
| 301 | $ret .= ' <noembed>'.$N; |
||
| 302 | $ret .= ' Java 1.5 or higher plugin required.'.$N; |
||
| 303 | $ret .= ' </noembed>'.$N; |
||
| 304 | $ret .= ' </embed>'.$N; |
||
| 305 | $ret .= ' </comment>'.$N; |
||
| 306 | $ret .= '</object>'; |
||
| 307 | return $ret; |
||
| 308 | } |
||
| 309 | |||
| 310 | private function abort($msg = '') { |
||
| 311 | $this->cleanup(); |
||
| 312 | if ($msg != '') |
||
| 313 | die(str_replace('(.*)',$msg,$this->appletparams['stringUploadError'])."\n"); |
||
| 314 | exit; |
||
| 315 | } |
||
| 316 | |||
| 317 | private function warning($msg = '') { |
||
| 318 | $this->cleanup(); |
||
| 319 | if ($msg != '') |
||
| 320 | echo('WARNING: '.$msg."\n"); |
||
| 321 | echo $this->appletparams['stringUploadSuccess']."\n"; |
||
| 322 | exit; |
||
| 323 | } |
||
| 324 | |||
| 325 | private function cleanup() { |
||
| 326 | // remove all uploaded files of *this* request |
||
| 327 | if (isset($_FILES)) { |
||
| 328 | foreach ($_FILES as $key => $val) |
||
| 329 | @unlink($val['tmp_name']); |
||
| 330 | } |
||
| 331 | // remove accumulated file, if any. |
||
| 332 | @unlink($this->classparams['destdir'].'/'.$this->classparams['tmp_prefix'].session_id()); |
||
| 333 | @unlink($this->classparams['destdir'].'/'.$this->classparams['tmp_prefix'].'tmp'.session_id()); |
||
| 334 | // reset session var |
||
| 335 | $_SESSION['RF'][$this->classparams['var_prefix'].'size'] = 0; |
||
| 336 | return; |
||
| 337 | } |
||
| 338 | |||
| 339 | private function mkdirp($path) { |
||
| 340 | // create subdir (hierary) below destdir; |
||
| 341 | $dirs = explode('/', $path); |
||
| 342 | $path = $this->classparams['destdir']; |
||
| 343 | foreach ($dirs as $dir) { |
||
| 344 | $path .= '/'.$dir; |
||
| 345 | if (!file_exists($path)) { // @ does NOT always supress the error! |
||
| 346 | $_umask = umask(0); // override the system mask |
||
| 347 | @mkdir($path, $this->classparams['dirperm']); |
||
| 348 | umask($_umask); |
||
| 349 | } |
||
| 350 | } |
||
| 351 | if (!is_dir($path) && is_writable($path)) |
||
| 352 | $this->abort('Destination dir not accessible'); |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * This method: |
||
| 357 | * - Replaces some potentially dangerous characters by '_' (in the given name an relative path) |
||
| 358 | * - Checks if a files of the same name already exists. |
||
| 359 | * - If no: no problem. |
||
| 360 | * - If yes, and the duplicate class param is set to rename, the file is renamed. |
||
| 361 | * - If yes, and the duplicate class param is set to overwrite, the file is not renamed. The existing one will be erased. |
||
| 362 | * - If yes, and the duplicate class param is set to reject, an error is thrown. |
||
| 363 | */ |
||
| 364 | private function dstfinal(&$name, &$subdir) { |
||
| 365 | $name = preg_replace('![`$\\\\/|]!', '_', $name); |
||
| 366 | if ($this->classparams['allow_subdirs'] && ($subdir != '')) { |
||
| 367 | $subdir = trim(preg_replace('!\\\\!','/',$subdir),'/'); |
||
| 368 | $subdir = preg_replace('![`$|]!', '_', $subdir); |
||
| 369 | if (!$this->classparams['spaces_in_subdirs']) { |
||
| 370 | $subdir = str_replace(' ','_',$subdir); |
||
| 371 | } |
||
| 372 | // recursively create subdir |
||
| 373 | if (!$this->classparams['demo_mode']) |
||
| 374 | $this->mkdirp($subdir); |
||
| 375 | // append a slash |
||
| 376 | $subdir .= '/'; |
||
| 377 | } else { |
||
| 378 | $subdir = ''; |
||
| 379 | } |
||
| 380 | $ret = $this->classparams['destdir'].'/'.$subdir.$name; |
||
| 381 | if (file_exists($ret)) { |
||
| 382 | if ($this->classparams['duplicate'] === 'overwrite') { |
||
| 383 | return $ret; |
||
| 384 | } |
||
| 385 | if ($this->classparams['duplicate'] === 'reject') { |
||
| 386 | $this->abort('A file with the same name already exists'); |
||
| 387 | } |
||
| 388 | if ($this->classparams['duplicate'] === 'warning') { |
||
| 389 | $this->warning("File $name already exists - rejected"); |
||
| 390 | } |
||
| 391 | if ($this->classparams['duplicate'] === 'rename') { |
||
| 392 | $cnt = 1; |
||
| 393 | $dir = $this->classparams['destdir'].'/'.$subdir; |
||
| 394 | $ext = strrchr($name, '.'); |
||
| 395 | if ($ext) { |
||
| 396 | $nameWithoutExtension = substr($name, 0, strlen($name) - strlen($ext)); |
||
| 397 | } else { |
||
| 398 | $ext = ''; |
||
| 399 | $nameWithoutExtension = $name; |
||
| 400 | } |
||
| 401 | |||
| 402 | $rtry = $dir.$nameWithoutExtension.'_'.$cnt.$ext; |
||
| 403 | while (file_exists($rtry)) { |
||
| 404 | $cnt++; |
||
| 405 | $rtry = $dir.$nameWithoutExtension.'._'.$cnt.$ext; |
||
| 406 | } |
||
| 407 | //We store the result name in the byReference name parameter. |
||
| 408 | $name = $nameWithoutExtension.'_'.$cnt.$ext; |
||
| 409 | $ret = $rtry; |
||
| 410 | } |
||
| 411 | } |
||
| 412 | return $ret; |
||
| 413 | } |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Example function to process the files uploaded. This one simply displays the files' data. |
||
| 417 | * |
||
| 418 | */ |
||
| 419 | public function defaultAfterUploadManagement() { |
||
| 420 | $flist = '[defaultAfterUploadManagement] Nb uploaded files is: ' . sizeof($this->files); |
||
| 421 | $flist = $this->classparams['http_flist_start']; |
||
| 422 | foreach ($this->files as $f) { |
||
| 423 | //$f is an array, that contains all info about the uploaded file. |
||
| 424 | $this->logDebug('defaultAfterUploadManagement', " Reading file ${f['name']}"); |
||
| 425 | $flist .= $this->classparams['http_flist_file_before']; |
||
| 426 | $flist .= $f['name']; |
||
| 427 | $flist .= $this->classparams['http_flist_file_between']; |
||
| 428 | $flist .= $f['size']; |
||
| 429 | $flist .= $this->classparams['http_flist_file_between']; |
||
| 430 | $flist .= $f['relativePath']; |
||
| 431 | $flist .= $this->classparams['http_flist_file_between']; |
||
| 432 | $flist .= $f['fullName']; |
||
| 433 | $flist .= $this->classparams['http_flist_file_between']; |
||
| 434 | $flist .= $f['md5sum']; |
||
| 435 | $addBR = false; |
||
| 436 | foreach ($f as $key=>$value) { |
||
| 437 | //If it's a specific key, let's display it: |
||
| 438 | if ($key !== 'name' && $key !== 'size' && $key !== 'relativePath' && $key !== 'fullName' && $key !== 'md5sum') { |
||
| 439 | if ($addBR) { |
||
| 440 | $flist .= "<br>"; |
||
| 441 | } else { |
||
| 442 | // First line. We must add a new 'official' list separator. |
||
| 443 | $flist .= $this->classparams['http_flist_file_between']; |
||
| 444 | $addBR = true; |
||
| 445 | } |
||
| 446 | $flist .= "$key => $value"; |
||
| 447 | } |
||
| 448 | } |
||
| 449 | $flist .= $this->classparams['http_flist_file_after']; |
||
| 450 | } |
||
| 451 | $flist .= $this->classparams['http_flist_end']; |
||
| 452 | |||
| 453 | return $flist; |
||
| 454 | } |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Generation of the applet tag, and necessary things around (js content). Insertion of this into the content of the |
||
| 458 | * page. |
||
| 459 | * See the tag_jscript and tag_applet class parameters. |
||
| 460 | */ |
||
| 461 | private function generateAppletTag($str) { |
||
| 462 | $this->logDebug('generateAppletTag', 'Entering function'); |
||
| 463 | $str = preg_replace('/'.$this->classparams['tag_jscript'].'/', $this->str_jsinit(), $str); |
||
| 464 | return preg_replace('/'.$this->classparams['tag_applet'].'/', $this->str_applet(), $str); |
||
| 465 | } |
||
| 466 | |||
| 467 | /** |
||
| 468 | * This function is called when constructing the page, when we're not reveiving uploaded files. It 'just' construct |
||
| 469 | * the applet tag, by calling the relevant function. |
||
| 470 | * |
||
| 471 | * This *must* be public, because it is called from PHP's output buffering |
||
| 472 | */ |
||
| 473 | public function interceptBeforeUpload($str) { |
||
| 477 | |||
| 478 | /** |
||
| 479 | * This function displays the uploaded files description in the current page (see tag_flist class parameter) |
||
| 480 | * |
||
| 481 | * This *must* be public, because it is called from PHP's output buffering. |
||
| 482 | */ |
||
| 483 | public function interceptAfterUpload($str) { |
||
| 484 | $this->logDebug('interceptAfterUpload', 'Entering function'); |
||
| 485 | $this->logPHPDebug('interceptAfterUpload', $this->files); |
||
| 486 | |||
| 487 | if (count($this->files) > 0) { |
||
| 488 | if (isset($this->classparams['callbackAfterUploadManagement'])) { |
||
| 489 | $this->logDebug('interceptAfterUpload', 'Before call of ' .$this->classparams['callbackAfterUploadManagement']); |
||
| 490 | $strForFListContent = call_user_func($this->classparams['callbackAfterUploadManagement'], $this, $this->files); |
||
| 491 | } else { |
||
| 492 | $strForFListContent = $this->defaultAfterUploadManagement(); |
||
| 493 | } |
||
| 494 | $str = preg_replace('/'.$this->classparams['tag_flist'].'/', $strForFListContent, $str); |
||
| 495 | } |
||
| 496 | return $this->generateAppletTag($str); |
||
| 497 | } |
||
| 498 | |||
| 499 | /** |
||
| 500 | * This method manages the receiving of the debug log, when an error occurs. |
||
| 501 | */ |
||
| 502 | private function receive_debug_log() { |
||
| 515 | |||
| 516 | /** |
||
| 517 | * This method is the heart of the system. It manage the files sent by the applet, check the incoming parameters (md5sum) and |
||
| 518 | * reconstruct the files sent in chunk mode. |
||
| 519 | * |
||
| 520 | * The result is stored in the $files array, and can then be managed by the function given in the callbackAfterUploadManagement |
||
| 521 | * class parameter, or within the page whose URL is given in the afterUploadURL applet parameter. |
||
| 522 | * Or you can Extend the class and redeclare defaultAfterUploadManagement() to your needs. |
||
| 523 | */ |
||
| 524 | private function receive_uploaded_files() { |
||
| 708 | |||
| 709 | /** |
||
| 710 | * |
||
| 711 | * |
||
| 712 | */ |
||
| 713 | private function page_start() { |
||
| 714 | $this->logDebug('page_start', 'Entering function'); |
||
| 715 | |||
| 716 | // If the applet checks for the serverProtocol, it issues a HEAD request |
||
| 717 | // -> Simply return an empty doc. |
||
| 718 | if ($_SERVER['REQUEST_METHOD'] === 'HEAD') { |
||
| 719 | // Nothing to do |
||
| 720 | |||
| 721 | } else if ($_SERVER['REQUEST_METHOD'] === 'GET') { |
||
| 765 | } |
||
| 766 | |||
| 768 |
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.