27use ILIAS\ResourceStorage\Preloader\SecureString;
79 $this->
ctrl = $DIC->ctrl();
80 $this->
lng = $DIC->language();
81 $this->tpl =
$DIC->ui()->mainTemplate();
82 $this->wrapper =
$DIC->http()->wrapper();
84 $this->main_absolute_dir = realpath($main_absolute_directory);
85 $this->ui_factory =
$DIC->ui()->factory();
86 $this->ui_renderer =
$DIC->ui()->renderer();
87 $this->unzip =
$DIC->legacyArchives();
91 $this->
ctrl->saveParameter($this, self::PARAMETER_CDIR);
92 $this->
lng->loadLanguageModule(
"content");
103 $this->allowed_suffixes = $a_suffixes;
119 $this->forbidden_suffixes = $a_suffixes;
132 if (empty($a_suffix)) {
147 $this->allow_directories = $a_val;
157 $this->post_dir_path = $a_val;
167 $this->table_id = $a_val;
182 $this->title = $a_val;
192 $this->use_upload_directory = $a_val;
207 if (!is_array($pars)) {
231 $next_class = $this->
ctrl->getNextClass($this);
232 $cmd = $this->
ctrl->getCmd(
"listFiles");
233 if (str_starts_with((
string) $cmd,
"extCommand_")) {
234 $ret = $this->
extCommand(substr((
string) $cmd, 11, strlen((
string) $cmd) - 11));
236 $ret = $this->$cmd();
246 bool $a_single =
true,
247 bool $a_allow_dir =
false
249 $i = count($this->commands);
251 $this->commands[$i][
"object"] = $a_obj;
252 $this->commands[$i][
"method"] = $a_func;
253 $this->commands[$i][
"name"] = $a_name;
254 $this->commands[$i][
"single"] = $a_single;
255 $this->commands[$i][
"allow_dir"] = $a_allow_dir;
260 $this->commands = [];
263 public function labelFile(
string $a_file,
string $a_label): void
265 $this->file_labels[$a_file][] = $a_label;
270 $this->label_enable = $a_act;
271 $this->label_header = $a_label_header;
284 $cur_subdir = $this->wrapper->query()->has(self::PARAMETER_CDIR)
285 ? $this->wrapper->query()->retrieve(self::PARAMETER_CDIR, $this->
refinery->to()->string())
288 $new_subdir = $this->wrapper->query()->has(self::PARAMETER_NEWDIR)
289 ? $this->wrapper->query()->retrieve(self::PARAMETER_NEWDIR, $this->
refinery->to()->string())
295 if ($new_subdir ===
"..") {
296 $cur_subdir = substr($cur_subdir, 0, strrpos($cur_subdir,
"/"));
297 } elseif (!empty($new_subdir)) {
298 $cur_subdir = empty($cur_subdir) ? $new_subdir : $cur_subdir .
"/" . $new_subdir;
301 $cur_subdir = str_replace(
"..",
"", $cur_subdir);
302 $cur_dir = (empty($cur_subdir))
303 ? $this->main_absolute_dir
304 : $this->main_absolute_dir .
"/" . $cur_subdir;
306 $resolved_dir = realpath($cur_dir);
307 if ($resolved_dir ===
false) {
313 "dir" => $this->main_absolute_dir,
319 "dir" => $resolved_dir,
320 "subdir" => $cur_subdir
327 protected function getFileList(
string $a_dir, ?
string $a_subdir =
null): array
331 $entries = (is_dir($a_dir))
333 : [[
"type" =>
"dir",
"entry" =>
".."]];
336 foreach ($entries as
$e) {
337 if (
$e[
"entry"] ==
".") {
340 if (
$e[
"entry"] ==
".." && empty($a_subdir)) {
343 $cfile = (empty($a_subdir))
345 : $a_subdir .
"/" .
$e[
"entry"];
348 self::POST_PARAM_FILE => $cfile,
349 "entry" =>
$e[
"entry"],
350 "type" =>
$e[
"type"],
351 "size" =>
$e[
"size"] ?? 0,
352 "hash" => md5((
string)
$e[
"entry"])
364 $sel_files = $hashes = [];
365 if ($this->wrapper->post()->has(self::POST_PARAM_FILE)) {
366 $hashes = $this->wrapper->post()->retrieve(
367 self::POST_PARAM_FILE,
369 $this->refinery->to()->string()
372 } elseif ($this->wrapper->query()->has(self::PARAMETER_FHSH)) {
373 $hashes = [$this->wrapper->query()->retrieve(
374 self::PARAMETER_FHSH,
380 if (count($hashes) > 0) {
381 $dir = $this->parseCurrentDirectory();
382 $all_files = $this->getFileList($dir[
"dir"], $dir[
"subdir"]);
383 foreach ($hashes as $hash) {
384 foreach ($all_files as $file) {
385 if ($file[
"hash"] == $hash) {
386 $sel_files[] = $this->getPostDirPath()
387 ? $file[self::POST_PARAM_FILE]
400 $selected = $this->getIncomingFiles();
402 if ($selected === []) {
403 $this->tpl->setOnScreenMessage(
'failure', $this->
lng->txt(
"no_checkbox"),
true);
404 $this->
ctrl->redirect($this,
"listFiles");
408 if (count($selected) > 1 && ($this->commands[$a_nr][
"single"] ??
false)) {
409 $this->tpl->setOnScreenMessage(
'failure', $this->
lng->txt(
"cont_select_max_one_item"),
true);
410 $this->
ctrl->redirect($this,
"listFiles");
413 $cur_subdir = $this->sanitizeCurrentDirectory();
417 foreach ($selected as $file) {
419 $file = (empty($cur_subdir))
421 : $cur_subdir .
"/" . $file;
424 if (is_dir($this->main_absolute_dir .
"/" . $file) &&
425 !$this->commands[$a_nr][
"allow_dir"]) {
426 $this->tpl->setOnScreenMessage(
'failure', $this->
lng->txt(
"select_a_file"),
true);
427 $this->
ctrl->redirect($this,
"listFiles");
433 if ($this->commands[$a_nr][
"single"] ??
false) {
434 $files = array_shift($files);
437 $obj = $this->commands[$a_nr][
"object"];
438 $method = $this->commands[$a_nr][
"method"];
440 return (
string) $obj->$method($files);
445 $this->directory_creation = $a_val;
450 return $this->directory_creation;
458 $this->file_creation = $a_val;
463 return $this->file_creation;
469 $ilToolbar =
$DIC[
'ilToolbar'];
471 $ilCtrl =
$DIC[
'ilCtrl'];
473 $dir = $this->parseCurrentDirectory();
475 $this->
ctrl->setParameter($this, self::PARAMETER_CDIR, $dir[
"subdir"]);
478 $ilToolbar->setFormAction($ilCtrl->getFormAction($this),
true);
480 if ($this->getAllowDirectories() && $this->getAllowDirectoryCreation()) {
481 $ti =
new ilTextInputGUI($this->
lng->txt(
"cont_new_dir"), self::POST_PARAM_NEW_DIR);
482 $ti->setMaxLength(80);
484 $ilToolbar->addInputItem($ti,
true);
485 $ilToolbar->addFormButton(
$lng->txt(
"create"),
"createDirectory");
487 $ilToolbar->addSeparator();
489 if ($this->getAllowFileCreation()) {
492 $ilToolbar->addInputItem($fi,
true);
493 $ilToolbar->addFormButton(
$lng->txt(
"upload"),
"uploadFile");
496 $ilToolbar->addSeparator();
498 $options[
""] =
$lng->txt(
"cont_select_from_upload_dir");
499 foreach ($files as $file) {
500 $file = htmlspecialchars($file, ENT_QUOTES,
"utf-8");
503 $si =
new ilSelectInputGUI($this->
lng->txt(
"cont_uploaded_file"), self::POST_PARAM_UPLOADED_FILE);
505 $ilToolbar->addInputItem($si,
true);
506 $ilToolbar->addFormButton(
$lng->txt(
"copy"),
"uploadFile");
509 $fs_table = $this->getTable($dir[
"dir"], $dir[
"subdir"]);
512 $fs_table->setTitle($this->
getTitle());
515 $this->wrapper->query()->has(self::PARAM_RESETOFFSET)
516 && $this->wrapper->query()->retrieve(
517 self::PARAM_RESETOFFSET,
518 $this->refinery->kindlyTo()->int()
520 $fs_table->resetOffset();
522 $this->tpl->setContent($fs_table->getHTML());
536 $this->getPostDirPath(),
543 $cur_subdir = $this->sanitizeCurrentDirectory();
544 $file = $this->main_absolute_dir .
"/" . $a_file;
546 $this->
ctrl->setParameter($this, self::PARAM_OLD_NAME, basename($a_file));
547 $this->
ctrl->saveParameter($this, self::PARAMETER_CDIR);
552 $ti->setMaxLength(200);
554 $ti->setValue(basename($a_file));
558 $form->addCommandButton(
"renameFile", $this->
lng->txt(
"rename"));
559 $form->addCommandButton(
"cancelRename", $this->
lng->txt(
"cancel"));
560 $form->setFormAction($this->
ctrl->getFormAction($this,
"renameFile"));
563 $form->setTitle($this->
lng->txt(
"cont_rename_dir"));
565 $form->setTitle($this->
lng->txt(
"rename_file"));
568 $this->tpl->setContent($form->getHTML());
573 $new_name = $this->wrapper->post()->has(self::POST_PARAM_NEW_NAME)
574 ? $this->wrapper->post()->retrieve(self::POST_PARAM_NEW_NAME, $this->
refinery->to()->string())
578 $new_name = str_replace(
"/",
"", $new_name);
579 if ($new_name ===
"") {
580 throw new LogicException($this->
lng->txt(
"enter_new_name"));
583 $pi = pathinfo($new_name);
584 $suffix = $pi[
"extension"] ??
"";
585 if ($suffix !=
"" && !$this->isValidSuffix($suffix)) {
586 $this->tpl->setOnScreenMessage(
'failure', $this->
lng->txt(
"file_no_valid_file_type") .
" ($suffix)",
true);
587 $this->
ctrl->redirect($this,
"listFiles");
590 $cur_subdir = $this->sanitizeCurrentDirectory();
591 $dir = (empty($cur_subdir))
592 ? $this->main_absolute_dir .
"/"
593 : $this->main_absolute_dir .
"/" . $cur_subdir .
"/";
595 $old_name = $this->wrapper->query()->has(self::PARAM_OLD_NAME)
596 ? $this->wrapper->query()->retrieve(self::PARAM_OLD_NAME, $this->
refinery->to()->string())
601 $realpath = realpath($dir . $old_name);
602 if (!str_starts_with($realpath, realpath($dir))) {
606 if (is_dir($dir . $old_name)) {
607 rename($dir . $old_name, $dir . $new_name);
612 $this->tpl->setOnScreenMessage(
'failure',
$e->getMessage(),
true);
613 $this->
ctrl->redirect($this,
"listFiles");
618 if (is_dir($dir . $new_name)) {
619 $this->tpl->setOnScreenMessage(
621 $this->
lng->txt(
"cont_dir_renamed"),
624 $this->setPerformedCommand(
"rename_dir", [self::PARAM_OLD_NAME => $old_name,
625 self::POST_PARAM_NEW_NAME => $new_name
628 $this->tpl->setOnScreenMessage(
630 $this->
lng->txt(
"cont_file_renamed"),
633 $this->setPerformedCommand(
"rename_file", [self::PARAM_OLD_NAME => $old_name,
634 self::POST_PARAM_NEW_NAME => $new_name
637 $this->
ctrl->redirect($this,
"listFiles");
642 $this->
ctrl->redirect($this,
"listFiles");
651 $cur_subdir = $this->sanitizeCurrentDirectory();
652 $cur_dir = (empty($cur_subdir))
653 ? $this->main_absolute_dir
654 : $this->main_absolute_dir .
"/" . $cur_subdir;
656 $new_dir = $this->wrapper->post()->has(self::POST_PARAM_NEW_DIR)
657 ? $this->wrapper->post()->retrieve(self::POST_PARAM_NEW_DIR, $this->
refinery->to()->string())
661 $new_dir = str_replace(
"/",
"", $new_dir);
663 if (!empty($new_dir)) {
665 if (is_dir($cur_dir .
"/" . $new_dir)) {
666 $this->tpl->setOnScreenMessage(
'success',
$lng->txt(
"cont_dir_created"),
true);
667 $this->setPerformedCommand(
"create_dir", [
"name" => $new_dir]);
670 $this->tpl->setOnScreenMessage(
'failure',
$lng->txt(
"cont_enter_a_dir_name"),
true);
672 $this->
ctrl->saveParameter($this, self::PARAMETER_CDIR);
673 $this->
ctrl->redirect($this,
'listFiles');
682 $cur_subdir = $this->sanitizeCurrentDirectory();
683 $cur_dir = (empty($cur_subdir))
684 ? $this->main_absolute_dir
685 : $this->main_absolute_dir .
"/" . $cur_subdir;
689 $pi = pathinfo((
string) $_FILES[
"new_file"][
"name"]);
690 $suffix = $pi[
"extension"] ??
"";
691 if (!$this->isValidSuffix($suffix)) {
692 $this->tpl->setOnScreenMessage(
'failure', $this->
lng->txt(
"file_no_valid_file_type") .
" ($suffix)",
true);
693 $this->
ctrl->redirect($this,
"listFiles");
696 $uploaded_file = $this->wrapper->post()->has(self::POST_PARAM_UPLOADED_FILE)
697 ? $this->wrapper->post()->retrieve(self::POST_PARAM_UPLOADED_FILE, $this->
refinery->to()->string())
699 if (is_file($_FILES[
"new_file"][
"tmp_name"])) {
701 $tgt_file = $cur_dir .
"/" . $name;
705 $upload =
$DIC->upload();
708 if (!$upload->hasBeenProcessed()) {
713 if (!$upload->hasUploads()) {
715 $DIC->language()->txt(
"upload_error_file_not_found")
718 $upload_result = $upload->getResults()[$_FILES[
"new_file"][
"tmp_name"]];
720 $processing_status = $upload_result->getStatus();
721 if ($processing_status->getCode() === ProcessingStatus::REJECTED) {
722 $this->tpl->setOnScreenMessage(
724 $processing_status->getMessage(),
727 $this->
ctrl->redirect($this,
"listFiles");
731 $upload->moveOneFileTo(
733 LegacyPathHelper::createRelativePath($cur_dir .
"/"),
734 LegacyPathHelper::deriveLocationFrom($cur_dir),
739 } elseif ($uploaded_file) {
747 } elseif (trim((
string) $_FILES[
"new_file"][
"name"]) ===
"") {
748 $this->tpl->setOnScreenMessage(
'failure',
$lng->txt(
"cont_enter_a_file"),
true);
751 if ($tgt_file && is_file($tgt_file)) {
753 if (MimeType::getMimeType($tgt_file) ===
"application/zip") {
754 $this->
ctrl->setParameter($this, self::PARAM_UPFILE, basename($tgt_file));
755 $url = $this->
ctrl->getLinkTarget($this, self::CMD_UNZIP_FILE);
756 $this->
ctrl->setParameter($this, self::PARAM_UPFILE,
"");
758 $unzip_link = $this->ui_factory->link()->standard(
759 $this->
lng->txt(
"unzip"),
763 $unzip =
" " . $this->ui_renderer->render($unzip_link);
766 $this->tpl->setOnScreenMessage(
'success',
$lng->txt(
"cont_file_created") . $unzip,
true);
768 $this->setPerformedCommand(
770 [
"name" => substr($tgt_file, strlen($this->main_absolute_dir) + 1)]
774 $this->
ctrl->saveParameter($this, self::PARAMETER_CDIR);
778 $this->
ctrl->redirect($this,
'listFiles');
784 $ilCtrl =
$DIC[
'ilCtrl'];
789 $cgui->setFormAction($ilCtrl->getFormAction($this));
790 $cgui->setHeaderText(
$lng->txt(
"info_delete_sure"));
791 $cgui->setCancel(
$lng->txt(
"cancel"),
"listFiles");
792 $cgui->setConfirm(
$lng->txt(
"delete"),
"deleteFile");
794 foreach ($a_files as $i) {
795 $cgui->addItem(
"file[]", $i, $i);
803 if (!$this->wrapper->post()->has(self::POST_PARAM_FILE)) {
804 throw new LogicException($this->
lng->txt(
"no_checkbox"));
809 $postfiles = $this->wrapper->post()->retrieve(
810 self::POST_PARAM_FILE,
812 $this->refinery->to()->string()
815 foreach ($postfiles as $post_file) {
817 throw new LogicException($this->
lng->txt(
"no_checkbox"));
821 $cur_subdir = $this->sanitizeCurrentDirectory();
822 $cur_dir = (empty($cur_subdir))
823 ? $this->main_absolute_dir
824 : $this->main_absolute_dir .
"/" . $cur_subdir;
825 $pi = pathinfo((
string) $post_file);
828 if (is_file($file)) {
838 $this->
ctrl->saveParameter($this, self::PARAMETER_CDIR);
840 $this->tpl->setOnScreenMessage(
'success', $this->
lng->txt(
"cont_dir_deleted"),
true);
841 $this->setPerformedCommand(
846 $this->tpl->setOnScreenMessage(
'success', $this->
lng->txt(
"cont_file_deleted"),
true);
847 $this->setPerformedCommand(
852 $this->
ctrl->redirect($this,
'listFiles');
858 $upname = $this->wrapper->query()->has(self::PARAM_UPFILE)
859 ? $this->wrapper->query()->retrieve(self::PARAM_UPFILE, $this->
refinery->to()->string())
861 if (is_null($a_file) && $upname !==
null) {
862 $a_file = basename((
string) $upname);
865 $cur_subdir = $this->sanitizeCurrentDirectory();
866 $cur_dir = (empty($cur_subdir))
867 ? $this->main_absolute_dir
868 : $this->main_absolute_dir .
"/" . $cur_subdir;
869 $a_file = $this->main_absolute_dir .
"/" . $a_file;
871 if (is_file($a_file)) {
873 $cur_files_r = iterator_to_array(
new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($cur_dir)));
879 !$this->getAllowDirectories(),
884 $new_files_r = iterator_to_array(
new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($cur_dir)));
886 $diff = array_diff($new_files, $cur_files);
887 $diff_r = array_diff($new_files_r, $cur_files_r);
890 foreach (array_keys($diff_r) as
$f) {
892 if (!is_dir(
$f) && !$this->isValidSuffix(strtolower($pi[
"extension"] ??
''))) {
893 $this->tpl->setOnScreenMessage(
'failure', $this->
lng->txt(
"file_some_invalid_file_types_removed") .
" (" . $pi[
"extension"] .
")",
true);
899 if ($this->getAllowDirectories()) {
902 foreach ($diff as $new_item) {
903 if (is_dir($cur_dir .
"/" . $new_item)) {
908 if (isset($new_files[
"path"])) {
909 foreach ($new_files[
"path"] as $idx =>
$path) {
910 $path = substr((
string)
$path, strlen($this->main_absolute_dir) + 1);
911 $diff[] =
$path . $new_files[self::POST_PARAM_FILE][$idx];
916 $this->setPerformedCommand(
926 $this->
ctrl->saveParameter($this, self::PARAMETER_CDIR);
927 $this->tpl->setOnScreenMessage(
'success', $this->
lng->txt(
"cont_file_unzipped"),
true);
928 $this->
ctrl->redirect($this,
"listFiles");
933 $file = $this->main_absolute_dir .
"/" . $a_file;
935 if (is_file($file) && !(is_dir($file))) {
939 $this->
ctrl->saveParameter($this, self::PARAMETER_CDIR);
940 $this->
ctrl->redirect($this,
"listFiles");
948 return $this->commands;
956 "method" =>
"downloadFile",
957 "name" => $this->
lng->txt(
"download"),
963 "method" =>
"confirmDeleteFile",
964 "name" => $this->
lng->txt(
"delete"),
970 "method" => self::CMD_UNZIP_FILE,
971 "name" => $this->
lng->txt(
"unzip"),
978 "method" =>
"renameFileForm",
979 "name" => $this->
lng->txt(
"rename"),
989 $cur_subdir = $this->wrapper->query()->has(self::PARAMETER_CDIR)
990 ? $this->wrapper->query()->retrieve(self::PARAMETER_CDIR, $this->
refinery->to()->string())
Builds a Color from either hex- or rgb values.
The legacy path helper provides convenient functions for the integration of the filesystem service wi...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Base class for ILIAS Exception handling.
static deliverFileLegacy(string $a_file, ?string $a_filename=null, ?string $a_mime=null, ?bool $isInline=false, ?bool $removeAfterDelivery=false, ?bool $a_exit_after=true)
File System Explorer GUI class.
bool $use_upload_directory
getAllowDirectoryCreation()
sanitizeCurrentDirectory()
const POST_PARAM_UPLOADED_FILE
setPostDirPath(bool $a_val)
setAllowedSuffixes(array $a_suffixes)
activateLabels(bool $a_act, string $a_label_header)
confirmDeleteFile(array $a_files)
listFiles(?ilTable2GUI $a_table_gui=null)
setForbiddenSuffixes(array $a_suffixes)
__construct(string $main_absolute_directory)
ilGlobalTemplateInterface $tpl
array $forbidden_suffixes
addCommand(object $a_obj, string $a_func, string $a_name, bool $a_single=true, bool $a_allow_dir=false)
labelFile(string $a_file, string $a_label)
getTable(string $a_dir, string $a_subdir)
const POST_PARAM_NEW_NAME
setPerformedCommand($command, array $pars=[])
setAllowFileCreation(bool $a_val)
Set allowed file creation.
setAllowDirectoryCreation(bool $a_val)
unzipFile(?string $a_file=null)
setTableId(string $a_val)
getLastPerformedCommand()
downloadFile(string $a_file)
const SESSION_LAST_COMMAND
getFileList(string $a_dir, ?string $a_subdir=null)
setUseUploadDirectory(bool $a_val)
setAllowDirectories(bool $a_val)
isValidSuffix(string $a_suffix)
ILIAS Refinery Factory $refinery
renameFileForm(string $a_file)
string $main_absolute_dir
static getDir(string $a_dir, bool $a_rec=false, ?string $a_sub_dir="")
get directory
static recursive_dirscan(string $dir, array &$arr)
Recursively scans a given directory and writes path and filename into referenced array.
static makeDir(string $a_dir)
creates a new directory and inherits all filesystem permissions of the parent directory You may pass ...
static rename(string $a_source, string $a_target)
static delDir(string $a_dir, bool $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
static renameExecutables(string $a_dir)
static get(string $a_var)
static set(string $a_var, $a_val)
Set a value.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This class represents a text property in a property form.
static _copyUploadFile(string $a_file, string $a_target, bool $a_raise_errors=true)
static _checkUploadFile(string $a_file)
static _getUploadDirectory()
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")
setContent(string $a_html)
Sets content for standard template.
This is how the factory for UI elements looks.
An entity that renders components to a string output.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...