| Conditions | 40 |
| Paths | 2913 |
| Total Lines | 367 |
| Code Lines | 190 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
| 1 | <?php |
||
| 231 | $extension[] = '.ps'; |
||
| 232 | $mime_type[] = 'application/mac-binhex40'; |
||
| 233 | $extension[] = '.hqx'; |
||
| 234 | $mime_type[] = 'application/x-gzip'; |
||
| 235 | $extension[] = 'tar.gz'; |
||
| 236 | $mime_type[] = 'application/x-shockwave-flash'; |
||
| 237 | $extension[] = '.swf'; |
||
| 238 | $mime_type[] = 'application/x-stuffit'; |
||
| 239 | $extension[] = '.sit'; |
||
| 240 | $mime_type[] = 'application/x-tar'; |
||
| 241 | $extension[] = '.tar'; |
||
| 242 | $mime_type[] = 'application/zip'; |
||
| 243 | $extension[] = '.zip'; |
||
| 244 | $mime_type[] = 'application/x-tar'; |
||
| 245 | $extension[] = '.tar'; |
||
| 246 | $mime_type[] = 'text/html'; |
||
| 247 | $extension[] = '.html'; |
||
| 248 | $mime_type[] = 'text/plain'; |
||
| 249 | $extension[] = '.txt'; |
||
| 250 | $mime_type[] = 'text/rtf'; |
||
| 251 | $extension[] = '.rtf'; |
||
| 252 | $mime_type[] = 'img/gif'; |
||
| 253 | $extension[] = '.gif'; |
||
| 254 | $mime_type[] = 'img/jpeg'; |
||
| 255 | $extension[] = '.jpg'; |
||
| 256 | $mime_type[] = 'img/png'; |
||
| 257 | $extension[] = '.png'; |
||
| 258 | $mime_type[] = 'audio/midi'; |
||
| 259 | $extension[] = '.mid'; |
||
| 260 | $mime_type[] = 'audio/mpeg'; |
||
| 261 | $extension[] = '.mp3'; |
||
| 262 | $mime_type[] = 'audio/x-aiff'; |
||
| 263 | $extension[] = '.aif'; |
||
| 264 | $mime_type[] = 'audio/x-pn-realaudio'; |
||
| 265 | $extension[] = '.rm'; |
||
| 266 | $mime_type[] = 'audio/x-pn-realaudio-plugin'; |
||
| 267 | $extension[] = '.rpm'; |
||
| 268 | $mime_type[] = 'audio/x-wav'; |
||
| 269 | $extension[] = '.wav'; |
||
| 270 | $mime_type[] = 'video/mpeg'; |
||
| 271 | $extension[] = '.mpg'; |
||
| 272 | $mime_type[] = 'video/mpeg4-generic'; |
||
| 273 | $extension[] = '.mp4'; |
||
| 274 | $mime_type[] = 'video/quicktime'; |
||
| 275 | $extension[] = '.mov'; |
||
| 276 | $mime_type[] = 'video/x-msvideo'; |
||
| 277 | $extension[] = '.avi'; |
||
| 278 | |||
| 279 | $mime_type[] = 'video/x-ms-wmv'; |
||
| 280 | $extension[] = '.wmv'; |
||
| 281 | $mime_type[] = 'video/x-flv'; |
||
| 282 | $extension[] = '.flv'; |
||
| 283 | $mime_type[] = 'image/svg+xml'; |
||
| 284 | $extension[] = '.svg'; |
||
| 285 | $mime_type[] = 'image/svg+xml'; |
||
| 286 | $extension[] = '.svgz'; |
||
| 287 | $mime_type[] = 'video/ogg'; |
||
| 288 | $extension[] = '.ogv'; |
||
| 289 | $mime_type[] = 'audio/ogg'; |
||
| 290 | $extension[] = '.oga'; |
||
| 291 | $mime_type[] = 'application/ogg'; |
||
| 292 | $extension[] = '.ogg'; |
||
| 293 | $mime_type[] = 'application/ogg'; |
||
| 294 | $extension[] = '.ogx'; |
||
| 295 | $mime_type[] = 'application/x-freemind'; |
||
| 296 | $extension[] = '.mm'; |
||
| 297 | |||
| 298 | $mime_type[] = 'application/vnd.ms-word.document.macroEnabled.12'; |
||
| 299 | $extension[] = '.docm'; |
||
| 300 | $mime_type[] = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'; |
||
| 301 | $extension[] = '.docx'; |
||
| 302 | $mime_type[] = 'application/vnd.ms-word.template.macroEnabled.12'; |
||
| 303 | $extension[] = '.dotm'; |
||
| 304 | $mime_type[] = 'application/vnd.openxmlformats-officedocument.wordprocessingml.template'; |
||
| 305 | $extension[] = '.dotx'; |
||
| 306 | $mime_type[] = 'application/vnd.ms-powerpoint.template.macroEnabled.12'; |
||
| 307 | $extension[] = '.potm'; |
||
| 308 | $mime_type[] = 'application/vnd.openxmlformats-officedocument.presentationml.template'; |
||
| 309 | $extension[] = '.potx'; |
||
| 310 | $mime_type[] = 'application/vnd.ms-powerpoint.addin.macroEnabled.12'; |
||
| 311 | $extension[] = '.ppam'; |
||
| 312 | $mime_type[] = 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12'; |
||
| 313 | $extension[] = '.ppsm'; |
||
| 314 | $mime_type[] = 'application/vnd.openxmlformats-officedocument.presentationml.slideshow'; |
||
| 315 | $extension[] = '.ppsx'; |
||
| 316 | $mime_type[] = 'application/vnd.ms-powerpoint.presentation.macroEnabled.12'; |
||
| 317 | $extension[] = '.pptm'; |
||
| 318 | $mime_type[] = 'application/vnd.openxmlformats-officedocument.presentationml.presentation'; |
||
| 319 | $extension[] = '.pptx'; |
||
| 320 | $mime_type[] = 'application/vnd.ms-excel.addin.macroEnabled.12'; |
||
| 321 | $extension[] = '.xlam'; |
||
| 322 | $mime_type[] = 'application/vnd.ms-excel.sheet.binary.macroEnabled.12'; |
||
| 323 | $extension[] = '.xlsb'; |
||
| 324 | $mime_type[] = 'application/vnd.ms-excel.sheet.macroEnabled.12'; |
||
| 325 | $extension[] = '.xlsm'; |
||
| 326 | $mime_type[] = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; |
||
| 327 | $extension[] = '.xlsx'; |
||
| 328 | $mime_type[] = 'application/vnd.ms-excel.template.macroEnabled.12'; |
||
| 329 | $extension[] = '.xltm'; |
||
| 330 | $mime_type[] = 'application/vnd.openxmlformats-officedocument.spreadsheetml.template'; |
||
| 331 | $extension[] = '.xltx'; |
||
| 332 | |||
| 333 | // Test on PC (files with no extension get application/octet-stream) |
||
| 334 | //$mime_type[] = 'application/octet-stream'; $extension[] = '.ext'; |
||
| 335 | // Check whether the MIME type sent by the browser is within the table |
||
| 336 | foreach ($mime_type as $key => &$type) { |
||
| 337 | if ($type == $file_type) { |
||
| 338 | $file_name .= $extension[$key]; |
||
| 339 | break; |
||
| 340 | } |
||
| 341 | } |
||
| 342 | |||
| 343 | unset($mime_type, $extension, $type, $key); // Delete to eschew possible collisions |
||
| 344 | } |
||
| 345 | |||
| 346 | return $file_name; |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * This function is a callback function that is used while extracting a zipfile |
||
| 351 | * http://www.phpconcept.net/pclzip/man/en/index.php?options-pclzip_cb_pre_extract. |
||
| 352 | * |
||
| 353 | * @param array $p_event |
||
| 354 | * @param array $p_header |
||
| 355 | * |
||
| 356 | * @return int (If the function returns 1, then the extraction is resumed, if 0 the path was skipped) |
||
| 357 | */ |
||
| 358 | function clean_up_files_in_zip($p_event, &$p_header) |
||
| 359 | { |
||
| 360 | $originalStoredFileName = $p_header['stored_filename']; |
||
| 361 | $baseName = basename($originalStoredFileName); |
||
| 362 | // Skip files |
||
| 363 | $skipFiles = [ |
||
| 364 | '__MACOSX', |
||
| 365 | '.Thumbs.db', |
||
| 366 | 'Thumbs.db', |
||
| 367 | ]; |
||
| 368 | |||
| 369 | if (in_array($baseName, $skipFiles)) { |
||
| 370 | return 0; |
||
| 371 | } |
||
| 372 | $modifiedStoredFileName = clean_up_path($originalStoredFileName); |
||
| 373 | $p_header['filename'] = str_replace($originalStoredFileName, $modifiedStoredFileName, $p_header['filename']); |
||
| 374 | |||
| 375 | return 1; |
||
| 376 | } |
||
| 377 | |||
| 378 | function cleanZipFilesNoRename($p_event, &$p_header) |
||
| 379 | { |
||
| 380 | $originalStoredFileName = $p_header['stored_filename']; |
||
| 381 | $baseName = basename($originalStoredFileName); |
||
| 382 | // Skip files |
||
| 383 | $skipFiles = [ |
||
| 384 | '__MACOSX', |
||
| 385 | '.Thumbs.db', |
||
| 386 | 'Thumbs.db', |
||
| 387 | ]; |
||
| 388 | |||
| 389 | if (in_array($baseName, $skipFiles)) { |
||
| 390 | return 0; |
||
| 391 | } |
||
| 392 | $modifiedStoredFileName = clean_up_path($originalStoredFileName, false); |
||
| 393 | $p_header['filename'] = str_replace($originalStoredFileName, $modifiedStoredFileName, $p_header['filename']); |
||
| 394 | |||
| 395 | return 1; |
||
| 396 | } |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Allow .htaccess file. |
||
| 400 | * |
||
| 401 | * @param $p_event |
||
| 402 | * @param $p_header |
||
| 403 | * |
||
| 404 | * @return int |
||
| 405 | */ |
||
| 406 | function cleanZipFilesAllowHtaccess($p_event, &$p_header) |
||
| 407 | { |
||
| 408 | $originalStoredFileName = $p_header['stored_filename']; |
||
| 409 | $baseName = basename($originalStoredFileName); |
||
| 410 | |||
| 411 | $allowFiles = ['.htaccess']; |
||
| 412 | if (in_array($baseName, $allowFiles)) { |
||
| 413 | return 1; |
||
| 414 | } |
||
| 415 | |||
| 416 | // Skip files |
||
| 417 | $skipFiles = [ |
||
| 418 | '__MACOSX', |
||
| 419 | '.Thumbs.db', |
||
| 420 | 'Thumbs.db', |
||
| 421 | ]; |
||
| 422 | |||
| 423 | if (in_array($baseName, $skipFiles)) { |
||
| 424 | return 0; |
||
| 425 | } |
||
| 426 | $modifiedStoredFileName = clean_up_path($originalStoredFileName); |
||
| 427 | $p_header['filename'] = str_replace($originalStoredFileName, $modifiedStoredFileName, $p_header['filename']); |
||
| 428 | |||
| 429 | return 1; |
||
| 430 | } |
||
| 431 | |||
| 432 | /** |
||
| 433 | * This function cleans up a given path |
||
| 434 | * by eliminating dangerous file names and cleaning them. |
||
| 435 | * |
||
| 436 | * @param string $path |
||
| 437 | * @param bool $replaceName |
||
| 438 | * |
||
| 439 | * @return string |
||
| 440 | * |
||
| 441 | * @see disable_dangerous_file() |
||
| 442 | * @see api_replace_dangerous_char() |
||
| 443 | */ |
||
| 444 | function clean_up_path($path, $replaceName = true) |
||
| 445 | { |
||
| 446 | // Split the path in folders and files |
||
| 447 | $path_array = explode('/', $path); |
||
| 448 | // Clean up every folder and filename in the path |
||
| 449 | foreach ($path_array as $key => &$val) { |
||
| 450 | // We don't want to lose the dots in ././folder/file (cfr. zipfile) |
||
| 451 | if ('.' != $val) { |
||
| 452 | if ($replaceName) { |
||
| 453 | $val = api_replace_dangerous_char($val); |
||
| 454 | } |
||
| 455 | $val = disable_dangerous_file($val); |
||
| 456 | } |
||
| 457 | } |
||
| 458 | // Join the "cleaned" path (modified in-place as passed by reference) |
||
| 459 | $path = implode('/', $path_array); |
||
| 460 | filter_extension($path); |
||
| 461 | |||
| 462 | return $path; |
||
| 463 | } |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Checks if the file is dangerous, based on extension and/or mimetype. |
||
| 467 | * The list of extensions accepted/rejected can be found from |
||
| 468 | * api_get_setting('upload_extensions_exclude') and api_get_setting('upload_extensions_include'). |
||
| 469 | * |
||
| 470 | * @param string $filename passed by reference. The filename will be modified |
||
| 471 | * if filter rules say so! (you can include path but the filename should look like 'abc.html') |
||
| 472 | * |
||
| 473 | * @return int 0 to skip file, 1 to keep file |
||
| 474 | */ |
||
| 475 | function filter_extension(&$filename) |
||
| 476 | { |
||
| 477 | if ('/' == substr($filename, -1)) { |
||
| 478 | return 1; // Authorize directories |
||
| 479 | } |
||
| 480 | $blacklist = api_get_setting('upload_extensions_list_type'); |
||
| 481 | if ('whitelist' != $blacklist) { // if = blacklist |
||
| 482 | $extensions = explode(';', strtolower(api_get_setting('upload_extensions_blacklist'))); |
||
| 483 | |||
| 484 | $skip = api_get_setting('upload_extensions_skip'); |
||
| 485 | $ext = strrchr($filename, '.'); |
||
| 486 | $ext = substr($ext, 1); |
||
| 487 | if (empty($ext)) { |
||
| 488 | return 1; // We're in blacklist mode, so accept empty extensions |
||
| 489 | } |
||
| 490 | if (in_array(strtolower($ext), $extensions)) { |
||
| 491 | if ('true' == $skip) { |
||
| 492 | return 0; |
||
| 493 | } else { |
||
| 494 | $new_ext = api_get_setting('upload_extensions_replace_by'); |
||
| 495 | $filename = str_replace('.'.$ext, '.'.$new_ext, $filename); |
||
| 496 | |||
| 497 | return 1; |
||
| 498 | } |
||
| 499 | } else { |
||
| 500 | return 1; |
||
| 501 | } |
||
| 502 | } else { |
||
| 503 | $extensions = explode(';', strtolower(api_get_setting('upload_extensions_whitelist'))); |
||
| 504 | $skip = api_get_setting('upload_extensions_skip'); |
||
| 505 | $ext = strrchr($filename, '.'); |
||
| 506 | $ext = substr($ext, 1); |
||
| 507 | if (empty($ext)) { |
||
| 508 | return 1; // Accept empty extensions |
||
| 509 | } |
||
| 510 | if (!in_array(strtolower($ext), $extensions)) { |
||
| 511 | if ('true' == $skip) { |
||
| 512 | return 0; |
||
| 513 | } else { |
||
| 514 | $new_ext = api_get_setting('upload_extensions_replace_by'); |
||
| 515 | $filename = str_replace('.'.$ext, '.'.$new_ext, $filename); |
||
| 516 | |||
| 517 | return 1; |
||
| 518 | } |
||
| 519 | } else { |
||
| 520 | return 1; |
||
| 521 | } |
||
| 522 | } |
||
| 523 | } |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Creates a new directory trying to find a directory name |
||
| 527 | * that doesn't already exist. |
||
| 528 | * |
||
| 529 | * @author Hugues Peeters <[email protected]> |
||
| 530 | * @author Bert Vanderkimpen |
||
| 531 | * |
||
| 532 | * @param array $_course current course information |
||
| 533 | * @param int $user_id current user id |
||
| 534 | * @param int $session_id |
||
| 535 | * @param int $to_group_id group.id |
||
| 536 | * @param int $to_user_id |
||
| 537 | * @param string $base_work_dir /var/www/chamilo/courses/ABC/document |
||
| 538 | * @param string $desired_dir_name complete path of the desired name |
||
| 539 | * Example: /folder1/folder2 |
||
| 540 | * @param string $title "folder2" |
||
| 541 | * @param int $visibility (0 for invisible, 1 for visible, 2 for deleted) |
||
| 542 | * @param bool $generateNewNameIfExists |
||
| 543 | * @param bool $sendNotification depends in conf setting "send_notification_when_document_added" |
||
| 544 | * @param array $parentInfo |
||
| 545 | * |
||
| 546 | * @return CDocument|false |
||
| 547 | */ |
||
| 548 | function create_unexisting_directory( |
||
| 549 | $_course, |
||
| 550 | $user_id, |
||
| 551 | $session_id, |
||
| 552 | $to_group_id, |
||
| 553 | $to_user_id, |
||
| 554 | $base_work_dir, |
||
| 555 | $desired_dir_name, |
||
| 556 | $title = '', |
||
| 557 | $visibility = '', |
||
| 558 | $generateNewNameIfExists = false, |
||
| 559 | $sendNotification = true, |
||
| 560 | $parentInfo = null |
||
| 561 | ) { |
||
| 562 | $course_id = $_course['real_id']; |
||
| 563 | $session_id = (int) $session_id; |
||
| 564 | |||
| 565 | $parentId = 0; |
||
| 566 | if (!empty($parentInfo)) { |
||
| 567 | if (is_array($parentInfo) && isset($parentInfo['iid'])) { |
||
| 568 | $parentId = $parentInfo['iid']; |
||
| 569 | } |
||
| 570 | if ($parentInfo instanceof CDocument) { |
||
| 571 | $parentId = $parentInfo->getIid(); |
||
| 572 | } |
||
| 573 | } |
||
| 574 | |||
| 575 | $document = DocumentManager::addDocument( |
||
| 576 | $_course, |
||
| 577 | $desired_dir_name, |
||
| 578 | 'folder', |
||
| 579 | 0, |
||
| 580 | $title, |
||
| 581 | null, |
||
| 582 | 0, |
||
| 583 | true, |
||
| 584 | $to_group_id, |
||
| 585 | $session_id, |
||
| 586 | $user_id, |
||
| 587 | $sendNotification, |
||
| 588 | '', |
||
| 589 | $parentId |
||
| 590 | ); |
||
| 591 | |||
| 592 | if ($document) { |
||
| 593 | return $document; |
||
| 594 | } |
||
| 595 | |||
| 596 | $folderExists = DocumentManager::folderExists( |
||
| 597 | $desired_dir_name, |
||
| 598 | $_course, |
||
| 690 |