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;
177 $this->title = $a_val;
187 $this->use_upload_directory = $a_val;
202 if (!is_array($pars)) {
226 $next_class = $this->
ctrl->getNextClass($this);
227 $cmd = $this->
ctrl->getCmd(
"listFiles");
228 if (str_starts_with((
string) $cmd,
"extCommand_")) {
229 $ret = $this->
extCommand(substr((
string) $cmd, 11, strlen((
string) $cmd) - 11));
231 $ret = $this->$cmd();
241 bool $a_single =
true,
242 bool $a_allow_dir =
false
244 $i = count($this->commands);
246 $this->commands[$i][
"object"] = $a_obj;
247 $this->commands[$i][
"method"] = $a_func;
248 $this->commands[$i][
"name"] = $a_name;
249 $this->commands[$i][
"single"] = $a_single;
250 $this->commands[$i][
"allow_dir"] = $a_allow_dir;
255 $this->commands = [];
258 public function labelFile(
string $a_file,
string $a_label): void
260 $this->file_labels[$a_file][] = $a_label;
265 $this->label_enable = $a_act;
266 $this->label_header = $a_label_header;
279 $cur_subdir = $this->wrapper->query()->has(self::PARAMETER_CDIR)
280 ? $this->wrapper->query()->retrieve(self::PARAMETER_CDIR, $this->
refinery->to()->string())
283 $new_subdir = $this->wrapper->query()->has(self::PARAMETER_NEWDIR)
284 ? $this->wrapper->query()->retrieve(self::PARAMETER_NEWDIR, $this->
refinery->to()->string())
290 if ($new_subdir ===
"..") {
291 $cur_subdir = substr($cur_subdir, 0, strrpos($cur_subdir,
"/"));
292 } elseif (!empty($new_subdir)) {
293 $cur_subdir = empty($cur_subdir) ? $new_subdir : $cur_subdir .
"/" . $new_subdir;
296 $cur_subdir = str_replace(
"..",
"", $cur_subdir);
297 $cur_dir = (empty($cur_subdir))
298 ? $this->main_absolute_dir
299 : $this->main_absolute_dir .
"/" . $cur_subdir;
302 "dir" => realpath($cur_dir),
303 "subdir" => $cur_subdir
310 protected function getFileList(
string $a_dir, ?
string $a_subdir =
null): array
314 $entries = (is_dir($a_dir))
316 : [[
"type" =>
"dir",
"entry" =>
".."]];
319 foreach ($entries as
$e) {
320 if (
$e[
"entry"] ==
".") {
323 if (
$e[
"entry"] ==
".." && empty($a_subdir)) {
326 $cfile = (empty($a_subdir))
328 : $a_subdir .
"/" .
$e[
"entry"];
331 self::POST_PARAM_FILE => $cfile,
332 "entry" =>
$e[
"entry"],
333 "type" =>
$e[
"type"],
334 "size" =>
$e[
"size"] ?? 0,
335 "hash" => md5((
string)
$e[
"entry"])
347 $sel_files = $hashes = [];
348 if ($this->wrapper->post()->has(self::POST_PARAM_FILE)) {
349 $hashes = $this->wrapper->post()->retrieve(
350 self::POST_PARAM_FILE,
352 $this->refinery->to()->string()
355 } elseif ($this->wrapper->query()->has(self::PARAMETER_FHSH)) {
356 $hashes = [$this->wrapper->query()->retrieve(
357 self::PARAMETER_FHSH,
363 if (count($hashes) > 0) {
364 $dir = $this->parseCurrentDirectory();
365 $all_files = $this->getFileList($dir[
"dir"], $dir[
"subdir"]);
366 foreach ($hashes as $hash) {
367 foreach ($all_files as $file) {
368 if ($file[
"hash"] == $hash) {
369 $sel_files[] = $this->getPostDirPath()
370 ? $file[self::POST_PARAM_FILE]
383 $selected = $this->getIncomingFiles();
385 if ($selected === []) {
386 $this->tpl->setOnScreenMessage(
'failure', $this->
lng->txt(
"no_checkbox"),
true);
387 $this->
ctrl->redirect($this,
"listFiles");
391 if (count($selected) > 1 && ($this->commands[$a_nr][
"single"] ??
false)) {
392 $this->tpl->setOnScreenMessage(
'failure', $this->
lng->txt(
"cont_select_max_one_item"),
true);
393 $this->
ctrl->redirect($this,
"listFiles");
396 $cur_subdir = $this->sanitizeCurrentDirectory();
400 foreach ($selected as $file) {
402 $file = (empty($cur_subdir))
404 : $cur_subdir .
"/" . $file;
407 if (is_dir($this->main_absolute_dir .
"/" . $file) &&
408 !$this->commands[$a_nr][
"allow_dir"]) {
409 $this->tpl->setOnScreenMessage(
'failure', $this->
lng->txt(
"select_a_file"),
true);
410 $this->
ctrl->redirect($this,
"listFiles");
416 if ($this->commands[$a_nr][
"single"] ??
false) {
417 $files = array_shift($files);
420 $obj = $this->commands[$a_nr][
"object"];
421 $method = $this->commands[$a_nr][
"method"];
423 return (
string) $obj->$method($files);
428 $this->directory_creation = $a_val;
433 return $this->directory_creation;
441 $this->file_creation = $a_val;
446 return $this->file_creation;
452 $ilToolbar =
$DIC[
'ilToolbar'];
454 $ilCtrl =
$DIC[
'ilCtrl'];
456 $dir = $this->parseCurrentDirectory();
458 $this->
ctrl->setParameter($this, self::PARAMETER_CDIR, $dir[
"subdir"]);
461 $ilToolbar->setFormAction($ilCtrl->getFormAction($this),
true);
463 if ($this->getAllowDirectories() && $this->getAllowDirectoryCreation()) {
464 $ti =
new ilTextInputGUI($this->
lng->txt(
"cont_new_dir"), self::POST_PARAM_NEW_DIR);
465 $ti->setMaxLength(80);
467 $ilToolbar->addInputItem($ti,
true);
468 $ilToolbar->addFormButton(
$lng->txt(
"create"),
"createDirectory");
470 $ilToolbar->addSeparator();
472 if ($this->getAllowFileCreation()) {
475 $ilToolbar->addInputItem($fi,
true);
476 $ilToolbar->addFormButton(
$lng->txt(
"upload"),
"uploadFile");
479 $ilToolbar->addSeparator();
481 $options[
""] =
$lng->txt(
"cont_select_from_upload_dir");
482 foreach ($files as $file) {
483 $file = htmlspecialchars($file, ENT_QUOTES,
"utf-8");
484 $options[$file] = $file;
486 $si =
new ilSelectInputGUI($this->
lng->txt(
"cont_uploaded_file"), self::POST_PARAM_UPLOADED_FILE);
487 $si->setOptions($options);
488 $ilToolbar->addInputItem($si,
true);
489 $ilToolbar->addFormButton(
$lng->txt(
"copy"),
"uploadFile");
492 $fs_table = $this->getTable($dir[
"dir"], $dir[
"subdir"]);
495 $fs_table->setTitle($this->
getTitle());
498 $this->wrapper->query()->has(self::PARAM_RESETOFFSET)
499 && $this->wrapper->query()->retrieve(
500 self::PARAM_RESETOFFSET,
501 $this->refinery->kindlyTo()->int()
503 $fs_table->resetOffset();
505 $this->tpl->setContent($fs_table->getHTML());
519 $this->getPostDirPath(),
526 $cur_subdir = $this->sanitizeCurrentDirectory();
527 $file = $this->main_absolute_dir .
"/" . $a_file;
529 $this->
ctrl->setParameter($this, self::PARAM_OLD_NAME, basename($a_file));
530 $this->
ctrl->saveParameter($this, self::PARAMETER_CDIR);
535 $ti->setMaxLength(200);
537 $ti->setValue(basename($a_file));
541 $form->addCommandButton(
"renameFile", $this->
lng->txt(
"rename"));
542 $form->addCommandButton(
"cancelRename", $this->
lng->txt(
"cancel"));
543 $form->setFormAction($this->
ctrl->getFormAction($this,
"renameFile"));
546 $form->setTitle($this->
lng->txt(
"cont_rename_dir"));
548 $form->setTitle($this->
lng->txt(
"rename_file"));
551 $this->tpl->setContent($form->getHTML());
556 $new_name = $this->wrapper->post()->has(self::POST_PARAM_NEW_NAME)
557 ? $this->wrapper->post()->retrieve(self::POST_PARAM_NEW_NAME, $this->
refinery->to()->string())
561 $new_name = str_replace(
"/",
"", $new_name);
562 if ($new_name ===
"") {
563 throw new LogicException($this->
lng->txt(
"enter_new_name"));
566 $pi = pathinfo($new_name);
567 $suffix = $pi[
"extension"] ??
"";
568 if ($suffix !=
"" && !$this->isValidSuffix($suffix)) {
569 $this->tpl->setOnScreenMessage(
'failure', $this->
lng->txt(
"file_no_valid_file_type") .
" ($suffix)",
true);
570 $this->
ctrl->redirect($this,
"listFiles");
573 $cur_subdir = $this->sanitizeCurrentDirectory();
574 $dir = (empty($cur_subdir))
575 ? $this->main_absolute_dir .
"/"
576 : $this->main_absolute_dir .
"/" . $cur_subdir .
"/";
578 $old_name = $this->wrapper->query()->has(self::PARAM_OLD_NAME)
579 ? $this->wrapper->query()->retrieve(self::PARAM_OLD_NAME, $this->
refinery->to()->string())
584 $realpath = realpath($dir . $old_name);
585 if (!str_starts_with($realpath, realpath($dir))) {
589 if (is_dir($dir . $old_name)) {
590 rename($dir . $old_name, $dir . $new_name);
595 $this->tpl->setOnScreenMessage(
'failure',
$e->getMessage(),
true);
596 $this->
ctrl->redirect($this,
"listFiles");
601 if (is_dir($dir . $new_name)) {
602 $this->tpl->setOnScreenMessage(
604 $this->
lng->txt(
"cont_dir_renamed"),
607 $this->setPerformedCommand(
"rename_dir", [self::PARAM_OLD_NAME => $old_name,
608 self::POST_PARAM_NEW_NAME => $new_name
611 $this->tpl->setOnScreenMessage(
613 $this->
lng->txt(
"cont_file_renamed"),
616 $this->setPerformedCommand(
"rename_file", [self::PARAM_OLD_NAME => $old_name,
617 self::POST_PARAM_NEW_NAME => $new_name
620 $this->
ctrl->redirect($this,
"listFiles");
625 $this->
ctrl->redirect($this,
"listFiles");
634 $cur_subdir = $this->sanitizeCurrentDirectory();
635 $cur_dir = (empty($cur_subdir))
636 ? $this->main_absolute_dir
637 : $this->main_absolute_dir .
"/" . $cur_subdir;
639 $new_dir = $this->wrapper->post()->has(self::POST_PARAM_NEW_DIR)
640 ? $this->wrapper->post()->retrieve(self::POST_PARAM_NEW_DIR, $this->
refinery->to()->string())
644 $new_dir = str_replace(
"/",
"", $new_dir);
646 if (!empty($new_dir)) {
648 if (is_dir($cur_dir .
"/" . $new_dir)) {
649 $this->tpl->setOnScreenMessage(
'success',
$lng->txt(
"cont_dir_created"),
true);
650 $this->setPerformedCommand(
"create_dir", [
"name" => $new_dir]);
653 $this->tpl->setOnScreenMessage(
'failure',
$lng->txt(
"cont_enter_a_dir_name"),
true);
655 $this->
ctrl->saveParameter($this, self::PARAMETER_CDIR);
656 $this->
ctrl->redirect($this,
'listFiles');
665 $cur_subdir = $this->sanitizeCurrentDirectory();
666 $cur_dir = (empty($cur_subdir))
667 ? $this->main_absolute_dir
668 : $this->main_absolute_dir .
"/" . $cur_subdir;
672 $pi = pathinfo((
string) $_FILES[
"new_file"][
"name"]);
673 $suffix = $pi[
"extension"] ??
"";
674 if (!$this->isValidSuffix($suffix)) {
675 $this->tpl->setOnScreenMessage(
'failure', $this->
lng->txt(
"file_no_valid_file_type") .
" ($suffix)",
true);
676 $this->
ctrl->redirect($this,
"listFiles");
679 $uploaded_file = $this->wrapper->post()->has(self::POST_PARAM_UPLOADED_FILE)
680 ? $this->wrapper->post()->retrieve(self::POST_PARAM_UPLOADED_FILE, $this->
refinery->to()->string())
682 if (is_file($_FILES[
"new_file"][
"tmp_name"])) {
684 $tgt_file = $cur_dir .
"/" . $name;
688 $upload =
$DIC->upload();
691 if (!$upload->hasBeenProcessed()) {
696 if (!$upload->hasUploads()) {
698 $DIC->language()->txt(
"upload_error_file_not_found")
701 $upload_result = $upload->getResults()[$_FILES[
"new_file"][
"tmp_name"]];
703 $processing_status = $upload_result->getStatus();
704 if ($processing_status->getCode() === ProcessingStatus::REJECTED) {
705 $this->tpl->setOnScreenMessage(
707 $processing_status->getMessage(),
710 $this->
ctrl->redirect($this,
"listFiles");
714 $upload->moveOneFileTo(
716 LegacyPathHelper::createRelativePath($cur_dir .
"/"),
717 LegacyPathHelper::deriveLocationFrom($cur_dir),
722 } elseif ($uploaded_file) {
730 } elseif (trim((
string) $_FILES[
"new_file"][
"name"]) ===
"") {
731 $this->tpl->setOnScreenMessage(
'failure',
$lng->txt(
"cont_enter_a_file"),
true);
734 if ($tgt_file && is_file($tgt_file)) {
736 if (MimeType::getMimeType($tgt_file) ===
"application/zip") {
737 $this->
ctrl->setParameter($this, self::PARAM_UPFILE, basename($tgt_file));
738 $url = $this->
ctrl->getLinkTarget($this, self::CMD_UNZIP_FILE);
739 $this->
ctrl->setParameter($this, self::PARAM_UPFILE,
"");
741 $unzip_link = $this->ui_factory->link()->standard(
742 $this->
lng->txt(
"unzip"),
746 $unzip =
" " . $this->ui_renderer->render($unzip_link);
749 $this->tpl->setOnScreenMessage(
'success',
$lng->txt(
"cont_file_created") . $unzip,
true);
751 $this->setPerformedCommand(
753 [
"name" => substr($tgt_file, strlen($this->main_absolute_dir) + 1)]
757 $this->
ctrl->saveParameter($this, self::PARAMETER_CDIR);
761 $this->
ctrl->redirect($this,
'listFiles');
767 $ilCtrl =
$DIC[
'ilCtrl'];
772 $cgui->setFormAction($ilCtrl->getFormAction($this));
773 $cgui->setHeaderText(
$lng->txt(
"info_delete_sure"));
774 $cgui->setCancel(
$lng->txt(
"cancel"),
"listFiles");
775 $cgui->setConfirm(
$lng->txt(
"delete"),
"deleteFile");
777 foreach ($a_files as $i) {
778 $cgui->addItem(
"file[]", $i, $i);
786 if (!$this->wrapper->post()->has(self::POST_PARAM_FILE)) {
787 throw new LogicException($this->
lng->txt(
"no_checkbox"));
792 $postfiles = $this->wrapper->post()->retrieve(
793 self::POST_PARAM_FILE,
795 $this->refinery->to()->string()
798 foreach ($postfiles as $post_file) {
800 throw new LogicException($this->
lng->txt(
"no_checkbox"));
804 $cur_subdir = $this->sanitizeCurrentDirectory();
805 $cur_dir = (empty($cur_subdir))
806 ? $this->main_absolute_dir
807 : $this->main_absolute_dir .
"/" . $cur_subdir;
808 $pi = pathinfo((
string) $post_file);
811 if (is_file($file)) {
821 $this->
ctrl->saveParameter($this, self::PARAMETER_CDIR);
823 $this->tpl->setOnScreenMessage(
'success', $this->
lng->txt(
"cont_dir_deleted"),
true);
824 $this->setPerformedCommand(
829 $this->tpl->setOnScreenMessage(
'success', $this->
lng->txt(
"cont_file_deleted"),
true);
830 $this->setPerformedCommand(
835 $this->
ctrl->redirect($this,
'listFiles');
841 $upname = $this->wrapper->query()->has(self::PARAM_UPFILE)
842 ? $this->wrapper->query()->retrieve(self::PARAM_UPFILE, $this->
refinery->to()->string())
844 if (is_null($a_file) && $upname !==
null) {
845 $a_file = basename((
string) $upname);
848 $cur_subdir = $this->sanitizeCurrentDirectory();
849 $cur_dir = (empty($cur_subdir))
850 ? $this->main_absolute_dir
851 : $this->main_absolute_dir .
"/" . $cur_subdir;
852 $a_file = $this->main_absolute_dir .
"/" . $a_file;
854 if (is_file($a_file)) {
856 $cur_files_r = iterator_to_array(
new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($cur_dir)));
862 !$this->getAllowDirectories(),
867 $new_files_r = iterator_to_array(
new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($cur_dir)));
869 $diff = array_diff($new_files, $cur_files);
870 $diff_r = array_diff($new_files_r, $cur_files_r);
873 foreach (array_keys($diff_r) as
$f) {
875 if (!is_dir(
$f) && !$this->isValidSuffix(strtolower($pi[
"extension"] ??
''))) {
876 $this->tpl->setOnScreenMessage(
'failure', $this->
lng->txt(
"file_some_invalid_file_types_removed") .
" (" . $pi[
"extension"] .
")",
true);
882 if ($this->getAllowDirectories()) {
885 foreach ($diff as $new_item) {
886 if (is_dir($cur_dir .
"/" . $new_item)) {
891 if (isset($new_files[
"path"])) {
892 foreach ($new_files[
"path"] as $idx =>
$path) {
893 $path = substr((
string)
$path, strlen($this->main_absolute_dir) + 1);
894 $diff[] =
$path . $new_files[self::POST_PARAM_FILE][$idx];
899 $this->setPerformedCommand(
909 $this->
ctrl->saveParameter($this, self::PARAMETER_CDIR);
910 $this->tpl->setOnScreenMessage(
'success', $this->
lng->txt(
"cont_file_unzipped"),
true);
911 $this->
ctrl->redirect($this,
"listFiles");
916 $file = $this->main_absolute_dir .
"/" . $a_file;
918 if (is_file($file) && !(is_dir($file))) {
922 $this->
ctrl->saveParameter($this, self::PARAMETER_CDIR);
923 $this->
ctrl->redirect($this,
"listFiles");
931 return $this->commands;
939 "method" =>
"downloadFile",
940 "name" => $this->
lng->txt(
"download"),
946 "method" =>
"confirmDeleteFile",
947 "name" => $this->
lng->txt(
"delete"),
953 "method" => self::CMD_UNZIP_FILE,
954 "name" => $this->
lng->txt(
"unzip"),
961 "method" =>
"renameFileForm",
962 "name" => $this->
lng->txt(
"rename"),
972 $cur_subdir = $this->wrapper->query()->has(self::PARAMETER_CDIR)
973 ? $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...