ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilFileUtils.php
Go to the documentation of this file.
1<?php
32{
33
37 protected static $new_files = array();
38
39
44 public static function processZipFile($a_directory, $a_file, $structure, $ref_id = null, $containerType = null, $tree = null, $access_handler = null)
45 {
46 global $DIC;
47
48 $lng = $DIC->language();
49
50 self::$new_files = array();
51
52 $pathinfo = pathinfo($a_file);
53 $file = $pathinfo["basename"];
54
55 // see 22727
56 if ($pathinfo["extension"] == "") {
57 $file .= ".zip";
58 }
59
60 // Copy zip-file to new directory, unzip and remove it
61 // TODO: check archive for broken file
62 //copy ($a_file, $a_directory . "/" . $file);
63 ilUtil::moveUploadedFile($a_file, $file, $a_directory . "/" . $file);
64 ilUtil::unzip($a_directory . "/" . $file);
65 unlink($a_directory . "/" . $file);
66 //echo "-".$a_directory . "/" . $file."-";
67 // Stores filename and paths into $filearray to check for viruses
68 // Checks if filenames can be read, else -> throw exception and leave
69 ilFileUtils::recursive_dirscan($a_directory, $filearray);
70
71 // if there are no files unziped (->broken file!)
72 if (empty($filearray)) {
73 throw new ilFileUtilsException($lng->txt("archive_broken"), ilFileUtilsException::$BROKEN_FILE);
74 }
75
76 // virus handling
77 foreach ($filearray["file"] as $key => $value) {
78 // remove "invisible" files
79 if (substr($value, 0, 1) == "." || stristr($filearray["path"][$key], "/__MACOSX/")) {
80 unlink($filearray["path"][$key] . $value);
81 unset($filearray["path"][$key]);
82 unset($filearray["file"][$key]);
83 continue;
84 }
85
86 $vir = ilUtil::virusHandling($filearray["path"][$key], $value);
87 if (!$vir[0]) {
88 // Unlink file and throw exception
89 unlink($filearray[path][$key]);
90 throw new ilFileUtilsException($lng->txt("file_is_infected") . "<br />" . $vir[1], ilFileUtilsException::$INFECTED_FILE);
91 break;
92 } else {
93 if ($vir[1] != "") {
95 break;
96 }
97 }
98 }
99
100 // If archive is to be used "flat"
101 if (!$structure) {
102 foreach (array_count_values($filearray["file"]) as $key => $value) {
103 // Archive contains same filenames in different directories
104 if ($value != "1") {
105 $doublettes .= " '" . ilFileUtils::utf8_encode($key) . "'";
106 }
107 }
108 if (isset($doublettes)) {
109 throw new ilFileUtilsException(
110 $lng->txt("exc_upload_error") . "<br />" . $lng->txt("zip_structure_error") . $doublettes,
112 );
113 }
114 } else {
115 $mac_dir = $a_directory . "/__MACOSX";
116 if (file_exists($mac_dir)) {
117 ilUtil::delDir($mac_dir);
118 }
119 }
120
121 // Everything fine since we got here; so we can store files and folders into the system (if ref_id is given)
122 if ($ref_id != null) {
123 ilFileUtils::createObjects($a_directory, $structure, $ref_id, $containerType, $tree, $access_handler);
124 }
125 }
126
127
138 public static function recursive_dirscan($dir, &$arr)
139 {
140 global $DIC;
141
142 $lng = $DIC->language();
143
144 $dirlist = opendir($dir);
145 while (false !== ($file = readdir($dirlist))) {
146 if (!is_file($dir . "/" . $file) && !is_dir($dir . "/" . $file)) {
147 throw new ilFileUtilsException($lng->txt("filenames_not_supported"), ilFileUtilsException::$BROKEN_FILE);
148 }
149
150 if ($file != '.' && $file != '..') {
151 $newpath = $dir . '/' . $file;
152 $level = explode('/', $newpath);
153 if (is_dir($newpath)) {
154 ilFileUtils::recursive_dirscan($newpath, $arr);
155 } else {
156 $arr["path"][] = $dir . "/";
157 $arr["file"][] = end($level);
158 }
159 }
160 }
161 closedir($dirlist);
162 }
163
167 public static function createObjects($dir, $structure, $ref_id, $containerType, $tree = null, $access_handler = null)
168 {
169 $dirlist = opendir($dir);
170
171 while (false !== ($file = readdir($dirlist))) {
172 if (!is_file($dir . "/" . $file) && !is_dir($dir . "/" . $file)) {
173 throw new ilFileUtilsException($lng->txt("filenames_not_supported"), ilFileUtilsException::$BROKEN_FILE);
174 }
175 if ($file != '.' && $file != '..') {
176 $newpath = $dir . '/' . $file;
177 $level = explode('/', $newpath);
178 if (is_dir($newpath)) {
179 if ($structure) {
180 $new_ref_id = ilFileUtils::createContainer(ilFileUtils::utf8_encode($file), $ref_id, $containerType, $tree, $access_handler);
181 ilFileUtils::createObjects($newpath, $structure, $new_ref_id, $containerType, $tree, $access_handler);
182 } else {
183 ilFileUtils::createObjects($newpath, $structure, $ref_id, $containerType, $tree, $access_handler);
184 }
185 } else {
186 ilFileUtils::createFile(end($level), $dir, $ref_id, $tree, $access_handler);
187 }
188 }
189 }
190 closedir($dirlist);
191 }
192
193
197 public static function createContainer($name, $ref_id, $containerType, $tree = null, $access_handler = null)
198 {
199 switch ($containerType) {
200 case "Category":
201 include_once("./Modules/Category/classes/class.ilObjCategory.php");
202 $newObj = new ilObjCategory();
203 $newObj->setType("cat");
204 break;
205
206 case "Folder":
207 include_once("./Modules/Folder/classes/class.ilObjFolder.php");
208 $newObj = new ilObjFolder();
209 $newObj->setType("fold");
210 break;
211
212 case "WorkspaceFolder":
213 include_once("./Modules/WorkspaceFolder/classes/class.ilObjWorkspaceFolder.php");
214 $newObj = new ilObjWorkspaceFolder();
215 break;
216 }
217
218 $newObj->setTitle($name);
219 $newObj->create();
220
221 // repository
222 if (!$access_handler) {
223 $newObj->createReference();
224 $newObj->putInTree($ref_id);
225 $newObj->setPermissions($ref_id);
226
227 if ($newObj->getType() == "cat") {
228 global $DIC;
229
230 $lng = $DIC->language();
231 $newObj->addTranslation($name, "", $lng->getLangKey(), $lng->getLangKey());
232 }
233
234 self::$new_files[$ref_id][] = $newObj;
235
236 return $newObj->getRefId();
237 } // workspace
238 else {
239 $node_id = $tree->insertObject($ref_id, $newObj->getId());
240 $access_handler->setPermissions($ref_id, $node_id);
241
242 return $node_id;
243 }
244 }
245
246
258 public static function createFile($filename, $path, $ref_id, $tree = null, $access_handler = null)
259 {
260 global $DIC;
261
262 $rbacsystem = $DIC->rbac()->system();
263 $lng = $DIC->language();
264 $ilErr = $DIC["ilErr"];
265
266 if (!$access_handler) {
267 $permission = $rbacsystem->checkAccess("create", $ref_id, "file");
268 } else {
269 $permission = $access_handler->checkAccess("create", "", $ref_id, "file");
270 }
271 if ($permission) {
272
273 // create and insert file in grp_tree
274 include_once("./Modules/File/classes/class.ilObjFile.php");
275 $fileObj = new ilObjFile();
276 $fileObj->setType('file');
279
280 // better use this, mime_content_type is deprecated
281 include_once("./Services/MediaObjects/classes/class.ilObjMediaObject.php");
282 $fileObj->setFileType(ilObjMediaObject::getMimeType($path . "/" . $filename));
283 $fileObj->setFileSize(filesize($path . "/" . $filename));
284 $fileObj->create();
285
286 // repository
287 if (!$access_handler) {
288 $fileObj->createReference();
289 $fileObj->putInTree($ref_id);
290 $fileObj->setPermissions($ref_id);
291
292 self::$new_files[$ref_id][] = $fileObj;
293 } else {
294 $node_id = $tree->insertObject($ref_id, $fileObj->getId());
295 $access_handler->setPermissions($ref_id, $node_id);
296 }
297
298 // upload file to filesystem
299 $fileObj->createDirectory();
300 $fileObj->storeUnzipedFile($path . "/" . $filename, ilFileUtils::utf8_encode(ilUtil::stripSlashes($filename)));
301 } else {
302 $ilErr->raiseError($lng->txt("permission_denied"), $ilErr->MESSAGE);
303 }
304 }
305
306
310 public static function getNewObjects()
311 {
312 return self::$new_files;
313 }
314
315
325 public static function utf8_encode($string)
326 {
327
328 // From http://w3.org/International/questions/qa-forms-utf-8.html
329 return (preg_match('%^(?:
330 [\x09\x0A\x0D\x20-\x7E] # ASCII
331 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
332 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
333 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
334 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
335 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
336 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
337 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
338 )*$%xs', $string)) ? $string : utf8_encode($string);
339 }
340
341
350 public static function fastBase64Decode($filein, $fileout)
351 {
352 $fh = fopen($filein, 'rb');
353 $fh2 = fopen($fileout, 'wb');
354 stream_filter_append($fh2, 'convert.base64-decode');
355
356 while (!feof($fh)) {
357 $chunk = fgets($fh);
358 if ($chunk === false) {
359 break;
360 }
361 fwrite($fh2, $chunk);
362 }
363 fclose($fh);
364 fclose($fh2);
365
366 return true;
367 }
368
369
377 public function fastBase64Encode($filein, $fileout)
378 {
379 $fh = fopen($filein, 'rb');
380 $fh2 = fopen($fileout, 'wb');
381 stream_filter_append($fh2, 'convert.base64-encode');
382
383 while (feof($fh)) {
384 $chunk = fgets($fh, 76);
385 if ($chunk === false) {
386 break;
387 }
388 fwrite($fh2, $chunk);
389 }
390 fclose($fh);
391 fclose($fh2);
392 }
393
394
405 private function fastGZip($in, $out, $level = "9")
406 {
407 if (!file_exists($in) || !is_readable($in)) {
408 return false;
409 }
410 if ((!file_exists($out) && !is_writable(dirname($out)) || (file_exists($out) && !is_writable($out)))) {
411 return false;
412 }
413
414 $in_file = fopen($in, "rb");
415 if (!$out_file = gzopen($out, "wb" . $param)) {
416 return false;
417 }
418
419 while (!feof($in_file)) {
420 $buffer = fgets($in_file, 4096);
421 gzwrite($out_file, $buffer, 4096);
422 }
423
424 fclose($in_file);
425 gzclose($out_file);
426
427 return true;
428 }
429
430
440 public function fastGunzip($in, $out)
441 {
442 if (!file_exists($in) || !is_readable($in)) {
443 return false;
444 }
445 if ((!file_exists($out) && !is_writable(dirname($out)) || (file_exists($out) && !is_writable($out)))) {
446 return false;
447 }
448
449 $in_file = gzopen($in, "rb");
450 $out_file = fopen($out, "wb");
451
452 while (!gzeof($in_file)) {
453 $buffer = gzread($in_file, 4096);
454 fwrite($out_file, $buffer, 4096);
455 }
456
457 gzclose($in_file);
458 fclose($out_file);
459
460 return true;
461 }
462
463
469 public static function lookupContentMimeType($content)
470 {
471 $finfo = new finfo(FILEINFO_MIME);
472
473 return $finfo->buffer($content);
474 }
475
476
482 public static function lookupFileMimeType($a_file)
483 {
484 if (!file_exists($a_file) or !is_readable($a_file)) {
485 return false;
486 }
487
488 return self::lookupContentMimeType(file_get_contents($a_file));
489 }
490
491
497 public static function _lookupMimeType($a_file)
498 {
499 return self::lookupFileMimeType($a_file);
500 }
501
502
506 public static function getExplicitlyBlockedFiles()
507 {
508 global $DIC;
509 $setting = $DIC->settings();
510
511 static $fileadmin_ref_id;
512
513 if ($fileadmin_ref_id === null) {
514 $objects_by_type = ilObject2::_getObjectsByType('facs');
515 $id = (int) reset($objects_by_type)['obj_id'];
516 $references = ilObject2::_getAllReferences($id);
517 $fileadmin_ref_id = (int) reset($references);
518 }
519 if ($DIC->rbac()->system()->checkAccess('upload_blacklisted_files', $fileadmin_ref_id)) {
520 return [];
521 }
522
523 $blocked = [];
524 foreach (explode(",", $setting->get("suffix_custom_expl_black")) as $blocked_suffix) {
525 $blocked[] = trim(strtolower($blocked_suffix));
526 }
527 $blocked = array_filter($blocked, function ($item) {
528 return $item !== '';
529 });
530
531 return $blocked;
532 }
533
534
540 public static function getValidExtensions()
541 {
542 global $DIC;
543
544 $setting = $DIC->settings();
545
546 // default white list
548
549 // remove custom black list values
550 foreach (explode(",", $setting->get("suffix_repl_additional")) as $custom_black) {
551 $custom_black = trim(strtolower($custom_black));
552 if (($key = array_search($custom_black, $whitelist)) !== false) {
553 unset($whitelist[$key]);
554 }
555 }
556
557 // add custom white list values
558 foreach (explode(",", $setting->get("suffix_custom_white_list")) as $custom_white) {
559 $custom_white = trim(strtolower($custom_white));
560 if (!in_array($custom_white, $whitelist)) {
561 $whitelist[] = $custom_white;
562 }
563 }
564
565 // bugfix mantis 25498: add an empty entry to ensure that files without extensions are still valid
566 $whitelist[] = '';
567
568 return $whitelist;
569 }
570
571
577 public static function getDefaultValidExtensionWhiteList()
578 {
579 return array(
580 '3gp', // VIDEO__3_GPP
581 '7z', // application/x-7z-compressed
582 'ai', // APPLICATION__POSTSCRIPT
583 'aif', // AUDIO__AIFF
584 'aifc', // AUDIO__AIFF
585 'aiff', // AUDIO__AIFF
586 'au', // AUDIO__BASIC
587 'arw', // IMAGE__X_SONY_ARW
588 'avi', // AUDIO__BASIC
589 'backup', // scorm wbts
590 'bak', // scorm wbts
591 'bas', // SPSS script
592 'bpmn', // bpmn
593 'bpmn2', // bpmn2
594 'bmp', // IMAGE__BMP
595 'bib', // bibtex
596 'bibtex', // bibtex
597 'bz', // APPLICATION__X_BZIP
598 'bz2', // APPLICATION__X_BZIP2
599 'c', // TEXT__PLAIN
600 'c++', // TEXT__PLAIN
601 'cc', // TEXT__PLAIN
602 'cct', // scorm wbts
603 'cdf', // (Wolfram) Computable Document Format
604 'cer', // APPLICATION__X_X509_CA_CERT
605 'class', // APPLICATION__X_JAVA_CLASS
606 'cls', // SPSS script
607 'conf', // TEXT__PLAIN
608 'cpp', // TEXT__X_C
609 'crt', // APPLICATION__X_X509_CA_CERT
610 'crs', // scorm wbts
611 'crw', // IMAGE__X_CANON_CRW
612 'cr2', // IMAGE__X_CANON_CR2
613 'css', // TEXT__CSS
614 'cst', // scorm wbts
615 'csv',
616 'cur', // scorm wbts
617 'db', // scorm wbts
618 'dcr', // scorm wbts
619 'des', // scorm wbts
620 'dng', // IMAGE__X_ADOBE_DNG
621 'doc', // APPLICATION__MSWORD,
622 'docx', // APPLICATION__VND_OPENXMLFORMATS_OFFICEDOCUMENT_WORDPROCESSINGML_DOCUMENT,
623 'dot', // APPLICATION__MSWORD,
624 'dotx', // APPLICATION__VND_OPENXMLFORMATS_OFFICEDOCUMENT_WORDPROCESSINGML_TEMPLATE,
625 'dtd',
626 'dvi', // APPLICATION__X_DVI,
627 'el', // TEXT__X_SCRIPT_ELISP,
628 'eps', // APPLICATION__POSTSCRIPT,
629 'epub', // APPLICATION__EPUB,
630 'f', // TEXT__X_FORTRAN,
631 'f77', // TEXT__X_FORTRAN,
632 'f90', // TEXT__X_FORTRAN,
633 'flv', // VIDEO__X_FLV,
634 'for', // TEXT__X_FORTRAN,
635 'g3', // IMAGE__G3FAX,
636 'gif', // IMAGE__GIF,
637 'gl', // VIDEO__GL,
638 'gan',
639 'ggb', // GEOGEBRA
640 'gsd', // AUDIO__X_GSM,
641 'gsm', // AUDIO__X_GSM,
642 'gtar', // APPLICATION__X_GTAR,
643 'gz', // APPLICATION__X_GZIP,
644 'gzip', // APPLICATION__X_GZIP,
645 'h', // TEXT__X_C
646 'hpp', // TEXT__X_C
647 'htm', // TEXT__HTML,
648 'html', // TEXT__HTML,
649 'htmls', // TEXT__HTML,
650 'ibooks', // Apple IBook Format
651 'ico', // IMAGE__X_ICON,
652 'ics', // iCalendar, TEXT__CALENDAR
653 'ini', // scorm wbts
654 'ipynb', // iPython file for Jupyter Notebooks
655 'java', // TEXT__X_JAVA_SOURCE,
656 'jbf', // scorm wbts
657 'jpeg', // IMAGE__PJPEG,
658 'jpg', // IMAGE__JPEG,
659 'js', // APPLICATION__X_JAVASCRIPT,
660 'jsf', // scorm wbts
661 'jso', // scorm wbts
662 'json', // APPLICATION__JSON
663 'latex', // APPLICATION__X_LATEX,
664 'lang', // lang files
665 'less', // less
666 'log', // TEXT__PLAIN,
667 'lsp', // APPLICATION__X_LISP,
668 'ltx', // APPLICATION__X_LATEX,
669 'm1v', // VIDEO__MPEG,
670 'm2a', // AUDIO__MPEG,
671 'm2v', // VIDEO__MPEG,
672 'm3u', // AUDIO__X_MPEQURL,
673 'm4a', // AUDIO__MP4,
674 'm4v', // VIDEO__MP4,
675 'markdown', // TEXT__MARKDOWN,
676 'm', // MATLAB
677 'mat', // MATLAB
678 'md', // TEXT__MARKDOWN,
679 'mdl', // Vensim files
680 'mdown', // TEXT__MARKDOWN,
681 'mid', // AUDIO__MIDI,
682 'min', // scorm articulate?
683 'midi', // AUDIO__MIDI,
684 'mobi', // APPLICATION__X_MOBI,
685 'mod', // AUDIO__MOD,
686 'mov', // VIDEO__QUICKTIME,
687 'movie', // VIDEO__X_SGI_MOVIE,
688 'mp2', // AUDIO__X_MPEG,
689 'mp3', // AUDIO__X_MPEG3,
690 'mp4', // VIDEO__MP4,
691 'mpa', // AUDIO__MPEG,
692 'mpeg', // VIDEO__MPEG,
693 'mpg', // AUDIO__MPEG,
694 'mph', // COMSOL Multiphysics
695 'mpga', // AUDIO__MPEG,
696 'mpp', // APPLICATION__VND_MS_PROJECT,
697 'mpt', // APPLICATION__X_PROJECT,
698 'mpv', // APPLICATION__X_PROJECT,
699 'mpx', // APPLICATION__X_PROJECT,
700 'mv', // VIDEO__X_SGI_MOVIE,
701 'mw',
702 'mv4', // VIDEO__MP4,
703 'nb', // Wolfram Notebook files
704 'nbp', // Wolfram Notebook Player files
705 'nef', // IMAGE__X_NIKON_NEF,
706 'nif', // IMAGE__X_NIFF,
707 'niff', // IMAGE__X_NIFF,
708 'obj', // Wavefront .obj file
709 'obm', // SPSS script
710 'odt', // Open document text,
711 'ods', // Open document spreadsheet,
712 'odp', // Open document presentation,
713 'odg', // Open document graphics,
714 'odf', // Open document formula,
715 'oga', // AUDIO__OGG,
716 'ogg', // AUDIO__OGG,
717 'ogv', // VIDEO__OGG,
718 'old', // no real file extension, but used in mail/forum components,
719 'p', // TEXT__X_PASCAL,
720 'pas', // TEXT__PASCAL,
721 'pbm', // IMAGE__X_PORTABLE_BITMAP,
722 'pcl', // APPLICATION__VND_HP_PCL,
723 'pct', // IMAGE__X_PICT,
724 'pcx', // IMAGE__X_PCX,
725 'pdf', // APPLICATION__PDF,
726 'pgm', // IMAGE__X_PORTABLE_GRAYMAP,
727 'pic', // IMAGE__PICT,
728 'pict', // IMAGE__PICT,
729 'png', // IMAGE__PNG,
730 'por', // Portable SPSS file
731 'pov', // MODEL__X_POV,
732 'project', // scorm wbts
733 'properties', // scorm wbts
734 'ppa', // APPLICATION__VND_MS_POWERPOINT,
735 'ppm', // IMAGE__X_PORTABLE_PIXMAP,
736 'pps', // APPLICATION__VND_MS_POWERPOINT,
737 'ppsx', // APPLICATION__VND_OPENXMLFORMATS_OFFICEDOCUMENT_PRESENTATIONML_SLIDESHOW,
738 'ppt', // APPLICATION__POWERPOINT,
739 'pptx', // APPLICATION__VND_OPENXMLFORMATS_OFFICEDOCUMENT_PRESENTATIONML_PRESENTATION,
740 'ppz', // APPLICATION__MSPOWERPOINT,
741 'ps', // APPLICATION__POSTSCRIPT,
742 'psd', // scorm wbts
743 'pwz', // APPLICATION__VND_MS_POWERPOINT,
744 'qt', // VIDEO__QUICKTIME,
745 'qtc', // VIDEO__X_QTC,
746 'qti', // IMAGE__X_QUICKTIME,
747 'qtif', // IMAGE__X_QUICKTIME,
748 'r', // R script file
749 'ra', // AUDIO__X_PN_REALAUDIO,
750 'ram', // AUDIO__X_PN_REALAUDIO,
751 'rar', // RAR (application/vnd.rar)
752 'rast', // IMAGE__CMU_RASTER,
753 'rda', // R data file
754 'rev', // RAR (application/vnd.rar)
755 'rexx', // TEXT__X_SCRIPT_REXX,
756 'ris', // ris
757 'rf', // IMAGE__VND_RN_REALFLASH,
758 'rgb', // IMAGE__X_RGB,
759 'rm', // APPLICATION__VND_RN_REALMEDIA,
760 'rmd', // R Markdown file
761 'rmi', // AUDIO__MID,
762 'rmm', // AUDIO__X_PN_REALAUDIO,
763 'rmp', // AUDIO__X_PN_REALAUDIO,
764 'rt', // TEXT__RICHTEXT,
765 'rtf', // TEXT__RICHTEXT,
766 'rtx', // TEXT__RICHTEXT,
767 'rv', // VIDEO__VND_RN_REALVIDEO,
768 's', // TEXT__X_ASM,
769 's3m', // AUDIO__S3M,
770 'sav', // SPSS data file
771 'sbs', // SPSS script
772 'sec', //
773 'sdml', // TEXT__PLAIN,
774 'sgm', // TEXT__SGML,
775 'sgml', // TEXT__SGML
776 'smi', // APPLICATION__SMIL,
777 'smil', // APPLICATION__SMIL,
778 'sps', // SPSS syntax file
779 'spv', // SPSS output file
780 'srt', //
781 'stl', // Stereolithography CAD file
782 'svg', // IMAGE__SVG_XML,
783 'swa', // scorm wbts
784 'swf', // APPLICATION__X_SHOCKWAVE_FLASH,
785 'swz', // scorm wbts
786 'tar', // application/x-tar
787 'tex', // APPLICATION__X_TEX,
788 'texi', // APPLICATION__X_TEXINFO,
789 'texinfo', // APPLICATION__X_TEXINFO,
790 'text', // TEXT__PLAIN,
791 'tgz', // APPLICATION__X_COMPRESSED,
792 'tif', // IMAGE__TIFF,
793 'tiff', // IMAGE__TIFF,
794 'ttf', // scorm wbts
795 'txt', // TEXT__PLAIN,
796 'tmp',
797 'uvproj',
798 'vdf',
799 'vimeo', // VIDEO__VIMEO,
800 'viv', // VIDEO__VIMEO,
801 'vivo', // VIDEO__VIVO,
802 'vrml', // APPLICATION__X_VRML,
803 'vsdx', // viseo
804 'wav', // wav
805 'webm', // VIDEO__WEBM,
806 'wmv', // VIDEO__X_MS_WMV,
807 'wmx', // VIDEO__X_MS_WMX,
808 'wmz', // VIDEO__X_MS_WMZ,
809 'woff', // web open font format,
810 'wwd', // SPSS script
811 'xhtml', // APPLICATION__XHTML_XML,
812 'xif', // IMAGE__VND_XIFF,
813 'xls', // APPLICATION__EXCEL,
814 'xlsx', // APPLICATION__VND_OPENXMLFORMATS_OFFICEDOCUMENT_SPREADSHEETML_SHEET,
815 'xmind',
816 'xml', // self::TEXT__XML,
817 'xsl', // APPLICATION__XML,
818 'xsd', // scorm
819 'zip' // APPLICATION__ZIP
820 );
821 }
822
823
832 public static function getValidFilename($a_filename)
833 {
834 if (!self::hasValidExtension($a_filename)) {
835 $pi = pathinfo($a_filename);
836 // if extension is not in white list, remove all "." and add ".sec" extension
837 $basename = str_replace(".", "", $pi["basename"]);
838 if (trim($basename) == "") {
839 include_once("./Services/Utilities/classes/class.ilFileUtilsException.php");
840 throw new ilFileUtilsException("Invalid upload filename.");
841 }
842 $basename .= ".sec";
843 if ($pi["dirname"] != "" && ($pi["dirname"] != "." || substr($a_filename, 0, 2) == "./")) {
844 $a_filename = $pi["dirname"] . "/" . $basename;
845 } else {
846 $a_filename = $basename;
847 }
848 }
849
850 return $a_filename;
851 }
852
853
859 public static function hasValidExtension($a_filename) : bool
860 {
861 $pi = pathinfo($a_filename);
862
863 $extension = strtolower($pi["extension"]);
864 // Regular expression pattern to match PHP file extensions, see https://mantis.ilias.de/view.php?id=0028626
865 if (preg_match('/^ph(p[3457]?|t|tml|ar)$/i', $extension)) {
866 return false;
867 }
868
869 return in_array($extension, self::getValidExtensions())
870 && !in_array($extension, self::getExplicitlyBlockedFiles());
871 }
872
873
883 public static function rename($a_source, $a_target)
884 {
885 $pi = pathinfo($a_target);
886 if (!in_array(strtolower($pi["extension"]), self::getValidExtensions())) {
887 include_once("./Services/Utilities/classes/class.ilFileUtilsException.php");
888 throw new ilFileUtilsException("Invalid target file");
889 }
890
891 return rename($a_source, $a_target);
892 }
893}
if(php_sapi_name() !='cli') $in
Definition: Utf8Test.php:37
$filename
Definition: buildRTE.php:89
An exception for terminatinating execution or to throw for unit testing.
Class to report exception.
Class ilFileUtils.
fastGunzip($in, $out)
fast uncompressing the file with the zlib-extension without memory consumption
static getExplicitlyBlockedFiles()
static hasValidExtension($a_filename)
static createObjects($dir, $structure, $ref_id, $containerType, $tree=null, $access_handler=null)
static getValidFilename($a_filename)
Get valid filename.
static createContainer($name, $ref_id, $containerType, $tree=null, $access_handler=null)
static processZipFile($a_directory, $a_file, $structure, $ref_id=null, $containerType=null, $tree=null, $access_handler=null)
static getNewObjects()
static utf8_encode($string)
utf8-encodes string if it is not a valid utf8-string.
static getDefaultValidExtensionWhiteList()
Valid extensions.
static getValidExtensions()
Valid extensions.
fastBase64Encode($filein, $fileout)
decodes base encoded file row by row to prevent memory exhaust
static lookupContentMimeType($content)
static _lookupMimeType($a_file)
fastGZip($in, $out, $level="9")
fast compressing the file with the zlib-extension without memory consumption
static recursive_dirscan($dir, &$arr)
Recursively scans a given directory and writes path and filename into referenced array.
static createFile($filename, $path, $ref_id, $tree=null, $access_handler=null)
Creates and inserts file object into tree.
static fastBase64Decode($filein, $fileout)
decodes base encoded file row by row to prevent memory exhaust
static lookupFileMimeType($a_file)
static rename($a_source, $a_target)
Rename a file.
Class ilObjCategory.
Class ilObjFile.
Class ilObjFolder.
static getMimeType($a_file, $a_external=null)
get mime type for file
Class ilObjWorkspaceFolder.
static _getObjectsByType($a_obj_type="", $a_owner="")
Get objects by type.
static _getAllReferences($a_id)
get all reference ids of object
static moveUploadedFile($a_file, $a_name, $a_target, $a_raise_errors=true, $a_mode="move_uploaded")
move uploaded file
static delDir($a_dir, $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
static virusHandling($a_file, $a_orig_name="", $a_clean=true)
scan file for viruses and clean files if possible
static unzip(string $path_to_zip_file, bool $overwrite_existing=false, bool $unpack_flat=false)
static stripSlashes($a_str, $a_strip_html=true, $a_allow="")
strip slashes if magic qoutes is enabled
global $DIC
Definition: goto.php:24
if($format !==null) $name
Definition: metadata.php:230
$ilErr
Definition: raiseError.php:18
$lng
$param
Definition: xapitoken.php:29