ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilFileSystemGUI.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
14 {
15  var $ctrl;
16 
17  function ilFileSystemGUI($a_main_directory)
18  {
19  global $lng, $ilCtrl, $tpl, $ilias;
20 
21  $this->ctrl =& $ilCtrl;
22  $this->lng =& $lng;
23  $this->ilias =& $ilias;
24  $this->tpl =& $tpl;
25  $this->main_dir = $a_main_directory;
26  $this->post_dir_path = false;
27  $this->commands = array(
28  0 => array(
29  "object" => $this,
30  "method" => "downloadFile",
31  "name" => $lng->txt("download"),
32  "int" => true
33  ),
34  1 => array(
35  "object" => $this,
36  "method" => "confirmDeleteFile",
37  "name" => $lng->txt("delete"),
38  "allow_dir" => true,
39  "int" => true
40  ),
41  2 => array(
42  "object" => $this,
43  "method" => "unzipFile",
44  "name" => $lng->txt("unzip"),
45  "int" => true
46  ),
47  3 => array(
48  "object" => $this,
49  "method" => "renameFileForm",
50  "name" => $lng->txt("rename"),
51  "allow_dir" => true,
52  "int" => true
53  ),
54  );
55 
56  $this->file_labels = array();
57  $this->label_enable = false;
58  $this->ctrl->saveParameter($this, "cdir");
59  $lng->loadLanguageModule("content");
60  $this->setAllowDirectories(true);
61  $this->setAllowDirectoryCreation(true);
62  $this->setAllowFileCreation(true);
63 //echo "<br>main_dir:".$this->main_dir.":";
64  }
65 
71  function setAllowDirectories($a_val)
72  {
73  $this->allow_directories = $a_val;
74  }
75 
82  {
83  return $this->allow_directories;
84  }
85 
91  function setPostDirPath($a_val)
92  {
93  $this->post_dir_path = $a_val;
94  }
95 
101  function getPostDirPath()
102  {
103  return $this->post_dir_path;
104  }
105 
111  function setTableId($a_val)
112  {
113  $this->table_id = $a_val;
114  }
115 
121  function getTableId()
122  {
123  return $this->table_id;
124  }
125 
131  function setTitle($a_val)
132  {
133  $this->title = $a_val;
134  }
135 
141  function getTitle()
142  {
143  return $this->title;
144  }
145 
152  protected function setPerformedCommand($command, $pars = "")
153  {
154  if (!is_array($pars))
155  {
156  $pars = array();
157  }
158  $_SESSION["fsys"]["lastcomm"] = array_merge(
159  array("cmd" => $command), $pars);
160  }
161 
167  public function getLastPerformedCommand()
168  {
169  $ret = $_SESSION["fsys"]["lastcomm"];
170  $_SESSION["fsys"]["lastcomm"] = "none";
171  return $ret;
172  }
173 
177  function &executeCommand()
178  {
179  $next_class = $this->ctrl->getNextClass($this);
180  $cmd = $this->ctrl->getCmd();
181 
182  switch($next_class)
183  {
184 
185  default:
186  if (substr($cmd, 0, 11) == "extCommand_")
187  {
188  $ret =& $this->extCommand(substr($cmd, 11, strlen($cmd) - 11));
189  }
190  else
191  {
192  $ret =& $this->$cmd();
193  }
194  break;
195  }
196 
197  return $ret;
198  }
199 
200 
204  function addCommand(&$a_obj, $a_func, $a_name, $a_single = true,
205  $a_allow_dir = false)
206  {
207  $i = count($this->commands);
208 
209  $this->commands[$i]["object"] =& $a_obj;
210  $this->commands[$i]["method"] = $a_func;
211  $this->commands[$i]["name"] = $a_name;
212  $this->commands[$i]["single"] = $a_single;
213  $this->commands[$i]["allow_dir"] = $a_allow_dir;
214 
215  //$this->commands[] = $arr;
216  }
217 
221  function clearCommands()
222  {
223  $this->commands = array();
224  }
225 
229  function labelFile($a_file, $a_label)
230  {
231  $this->file_labels[$a_file][] = $a_label;
232  }
233 
237  function activateLabels($a_act, $a_label_header)
238  {
239  $this->label_enable = $a_act;
240  $this->label_header = $a_label_header;
241  }
242 
246  function &extCommand($a_nr)
247  {
248  // remove ".." items
249  foreach ($_POST["file"] as $k => $v)
250  {
251  if ($_POST["file"][$k] == "..")
252  {
253  unset($_POST["file"][$k]);
254  }
255  }
256 
257  // check if at least one item is select
258  if (!isset($_POST["file"]))
259  {
260  $this->ilias->raiseError($this->lng->txt("no_checkbox"),$this->ilias->error_obj->MESSAGE);
261  }
262 
263  // check if only one item is select, if command does not allow multiple selection
264  if (count($_POST["file"]) > 1 && $this->commands[$a_nr]["single"])
265  {
266  $this->ilias->raiseError($this->lng->txt("cont_select_max_one_item"),$this->ilias->error_obj->MESSAGE);
267  }
268 
269  $cur_subdir = str_replace(".", "", ilUtil::stripSlashes($_GET["cdir"]));
270 
271  // collect files and
272  $files = array();
273  foreach ($_POST["file"] as $k => $v)
274  {
275  $file = (!empty($cur_subdir))
276  ? $this->main_dir."/".$cur_subdir."/".ilUtil::stripSlashes($_POST["file"][$k])
277  : $this->main_dir."/".ilUtil::stripSlashes($_POST["file"][$k]);
278 
279  // check wether selected item is a directory
280  if (@is_dir($file) && !$this->commands[$a_nr]["allow_dir"])
281  {
282  $this->ilias->raiseError($this->lng->txt("select_a_file"),$this->ilias->error_obj->MESSAGE);
283  }
284  $file = (!empty($cur_subdir))
285  ? $cur_subdir."/".ilUtil::stripSlashes($_POST["file"][$k])
286  : ilUtil::stripSlashes($_POST["file"][$k]);
287  $files[] = $file;
288  }
289 
290  if ($this->commands[$a_nr]["single"])
291  {
292  $files = $files[0];
293  }
294 
295  $obj =& $this->commands[$a_nr]["object"];
296  $method = $this->commands[$a_nr]["method"];
297 
298  return $obj->$method($files);
299  }
300 
304  public function setAllowDirectoryCreation($a_val)
305  {
306  $this->directory_creation = $a_val;
307  }
308 
312  public function getAllowDirectoryCreation()
313  {
314  return $this->directory_creation;
315  }
316 
320  public function setAllowFileCreation($a_val)
321  {
322  $this->file_creation = $a_val;
323  }
324 
328  public function getAllowFileCreation()
329  {
330  return $this->file_creation;
331  }
332 
336  function listFiles()
337  {
338  global $ilToolbar, $lng, $ilCtrl;
339 
340 
341  // determine directory
342  // FIXME: I have to call stripSlashes here twice, because I could not
343  // determine where the second layer of slashes is added to the
344  // URL Parameter
345  $cur_subdir = ilUtil::stripSlashes(ilUtil::stripSlashes($_GET["cdir"]));
346  $new_subdir = ilUtil::stripSlashes(ilUtil::stripSlashes($_GET["newdir"]));
347 
348  if($new_subdir == "..")
349  {
350  $cur_subdir = substr($cur_subdir, 0, strrpos($cur_subdir, "/"));
351  }
352  else
353  {
354  if (!empty($new_subdir))
355  {
356  if (!empty($cur_subdir))
357  {
358  $cur_subdir = $cur_subdir."/".$new_subdir;
359  }
360  else
361  {
362  $cur_subdir = $new_subdir;
363  }
364  }
365  }
366 
367  $cur_subdir = str_replace("..", "", $cur_subdir);
368 
369  $cur_dir = (!empty($cur_subdir))
370  ? $this->main_dir."/".$cur_subdir
371  : $this->main_dir;
372 
373  $this->ctrl->setParameter($this, "cdir", $cur_subdir);
374 
375  // toolbar for adding files/directories
376  $ilToolbar->setFormAction($ilCtrl->getFormAction($this), true);
377  include_once("./Services/Form/classes/class.ilTextInputGUI.php");
378 
379  if ($this->getAllowDirectories() && $this->getAllowDirectoryCreation())
380  {
381  $ti = new ilTextInputGUI($this->lng->txt("cont_new_dir"), "new_dir");
382  $ti->setMaxLength(80);
383  $ti->setSize(10);
384  $ilToolbar->addInputItem($ti, true);
385  $ilToolbar->addFormButton($lng->txt("create"), "createDirectory");
386 
387  $ilToolbar->addSeparator();
388  }
389 
390  include_once("./Services/Form/classes/class.ilFileInputGUI.php");
391  if ($this->getAllowFileCreation())
392  {
393  $fi = new ilFileInputGUI($this->lng->txt("cont_new_file"), "new_file");
394  $fi->setSize(10);
395  $ilToolbar->addInputItem($fi, true);
396  $ilToolbar->addFormButton($lng->txt("upload"), "uploadFile");
397  }
398 
399  include_once 'Services/FileSystemStorage/classes/class.ilUploadFiles.php';
401  {
402  $ilToolbar->addSeparator();
404  $options[""] = $lng->txt("cont_select_from_upload_dir");
405  foreach($files as $file)
406  {
407  $file = htmlspecialchars($file, ENT_QUOTES, "utf-8");
408  $options[$file] = $file;
409  }
410  include_once("./Services/Form/classes/class.ilSelectInputGUI.php");
411  $si = new ilSelectInputGUI($this->lng->txt("cont_uploaded_file"), "uploaded_file");
412  $si->setOptions($options);
413  $ilToolbar->addInputItem($si, true);
414  $ilToolbar->addFormButton($lng->txt("copy"), "uploadFile");
415  }
416 
417  // load files templates
418  //$this->tpl->addBlockfile("ADM_CONTENT", "adm_content", "tpl.directory.html", false);
419  include_once("./Services/FileSystemStorage/classes/class.ilFileSystemTableGUI.php");
420  $fs_table = new ilFileSystemTableGUI($this, "listFiles", $cur_dir, $cur_subdir,
421  $this->label_enable, $this->file_labels, $this->label_header, $this->commands,
422  $this->getPostDirPath());
423  $fs_table->setId($this->getTableId());
424  if ($this->getTitle() != "")
425  {
426  $fs_table->setTitle($this->getTitle());
427  }
428  if ($_GET["resetoffset"] == 1)
429  {
430  $fs_table->resetOffset();
431  }
432  $this->tpl->setContent($fs_table->getHTML());
433  }
434 
438  function renameFileForm()
439  {
440  global $lng, $ilCtrl;
441 
442  if (!isset($_POST["file"]))
443  {
444  $this->ilias->raiseError($this->lng->txt("no_checkbox"),$this->ilias->error_obj->MESSAGE);
445  }
446 
447  if (count($_POST["file"]) > 1)
448  {
449  $this->ilias->raiseError($this->lng->txt("cont_select_max_one_item"),$this->ilias->error_obj->MESSAGE);
450  }
451 
452  if (ilUtil::stripSlashes($_POST["file"][0]) == ".." )
453  {
454  $this->ilias->raiseError($this->lng->txt("select_a_file"),$this->ilias->error_obj->MESSAGE);
455  }
456 
457  $cur_subdir = str_replace(".", "", ilUtil::stripSlashes($_GET["cdir"]));
458  $file = (!empty($cur_subdir))
459  ? $this->main_dir."/".$cur_subdir."/".ilUtil::stripSlashes($_POST["file"][0])
460  : $this->main_dir."/".ilUtil::stripSlashes($_POST["file"][0]);
461 
462  $this->ctrl->setParameter($this, "old_name", ilUtil::stripSlashes($_POST["file"][0]));
463 
464  include_once("Services/Form/classes/class.ilPropertyFormGUI.php");
465  $form = new ilPropertyFormGUI();
466 
467  // file/dir name
468  $ti = new ilTextInputGUI($this->lng->txt("name"), "new_name");
469  $ti->setMaxLength(200);
470  $ti->setSize(40);
471  $ti->setValue(ilUtil::stripSlashes($_POST["file"][0]));
472  $form->addItem($ti);
473 
474  // save and cancel commands
475  $form->addCommandButton("renameFile", $lng->txt("rename"));
476  $form->addCommandButton("cancelRename", $lng->txt("cancel"));
477  $form->setFormAction($ilCtrl->getFormAction($this, "renameFile"));
478 
479  if (@is_dir($file))
480  {
481  $form->setTitle($this->lng->txt("cont_rename_dir"));
482  }
483  else
484  {
485  $form->setTitle($this->lng->txt("rename_file"));
486  }
487 
488  $this->tpl->setContent($form->getHTML());
489  }
490 
494  function renameFile()
495  {
496  global $lng;
497 
498  $new_name = str_replace("..", "", ilUtil::stripSlashes($_POST["new_name"]));
499  $new_name = str_replace("/", "", $new_name);
500  if ($new_name == "")
501  {
502  $this->ilias->raiseError($this->lng->txt("enter_new_name"),$this->ilias->error_obj->MESSAGE);
503  }
504 
505  $cur_subdir = str_replace(".", "", ilUtil::stripSlashes($_GET["cdir"]));
506  $dir = (!empty($cur_subdir))
507  ? $this->main_dir."/".$cur_subdir."/"
508  : $this->main_dir."/";
509 
510  rename($dir.ilUtil::stripSlashes($_GET["old_name"]), $dir.$new_name);
511 
512  ilUtil::renameExecutables($this->main_dir);
513  if (@is_dir($dir.$new_name))
514  {
515  ilUtil::sendSuccess($lng->txt("cont_dir_renamed"), true);
516  $this->setPerformedCommand("rename_dir", array("old_name" => $_GET["old_name"],
517  "new_name" => $new_name));
518  }
519  else
520  {
521  ilUtil::sendSuccess($lng->txt("cont_file_renamed"), true);
522  $this->setPerformedCommand("rename_file", array("old_name" => $_GET["old_name"],
523  "new_name" => $new_name));
524  }
525  $this->ctrl->redirect($this, "listFiles");
526  }
527 
531  function cancelRename()
532  {
533  $this->ctrl->redirect($this, "listFiles");
534  }
535 
539  function createDirectory()
540  {
541  global $lng;
542 
543  // determine directory
544  $cur_subdir = str_replace(".", "", ilUtil::stripSlashes($_GET["cdir"]));
545  $cur_dir = (!empty($cur_subdir))
546  ? $this->main_dir."/".$cur_subdir
547  : $this->main_dir;
548 
549  $new_dir = str_replace(".", "", ilUtil::stripSlashes($_POST["new_dir"]));
550  $new_dir = str_replace("/", "", $new_dir);
551 
552  if (!empty($new_dir))
553  {
554  ilUtil::makeDir($cur_dir."/".$new_dir);
555  if (is_dir($cur_dir."/".$new_dir))
556  {
557  ilUtil::sendSuccess($lng->txt("cont_dir_created"), true);
558  $this->setPerformedCommand("create_dir", array("name" => $new_dir));
559  }
560  }
561  else
562  {
563  ilUtil::sendFailure($lng->txt("cont_enter_a_dir_name"), true);
564  }
565  $this->ctrl->saveParameter($this, "cdir");
566  $this->ctrl->redirect($this, "listFiles");
567  }
568 
572  function uploadFile()
573  {
574  global $lng;
575 
576  // determine directory
577  $cur_subdir = str_replace(".", "", ilUtil::stripSlashes($_GET["cdir"]));
578  $cur_dir = (!empty($cur_subdir))
579  ? $this->main_dir."/".$cur_subdir
580  : $this->main_dir;
581 
582 
583  include_once 'Services/FileSystemStorage/classes/class.ilUploadFiles.php';
584 
585  if (is_file($_FILES["new_file"]["tmp_name"]))
586  {
587  move_uploaded_file($_FILES["new_file"]["tmp_name"],
588  $cur_dir."/".ilUtil::stripSlashes($_FILES["new_file"]["name"]));
589  if (is_file($cur_dir."/".ilUtil::stripSlashes($_FILES["new_file"]["name"])))
590  {
591  ilUtil::sendSuccess($lng->txt("cont_file_created"), true);
592  $this->setPerformedCommand("create_file",
593  array("name" => ilUtil::stripSlashes($_FILES["new_file"]["name"])));
594 
595  }
596  }
597  elseif ($_POST["uploaded_file"])
598  {
599  // check if the file is in the ftp directory and readable
600  if (ilUploadFiles::_checkUploadFile($_POST["uploaded_file"]))
601  {
602  // copy uploaded file to data directory
603  ilUploadFiles::_copyUploadFile($_POST["uploaded_file"],
604  $cur_dir."/".ilUtil::stripSlashes($_POST["uploaded_file"]));
605  }
606  if (is_file($cur_dir."/".ilUtil::stripSlashes($_POST["uploaded_file"])))
607  {
608  ilUtil::sendSuccess($lng->txt("cont_file_created"), true);
609  $this->setPerformedCommand("create_file",
610  array("name" => ilUtil::stripSlashes($_POST["uploaded_file"])));
611  }
612  }
613  else if (trim($_FILES["new_file"]["name"]) == "")
614  {
615  ilUtil::sendFailure($lng->txt("cont_enter_a_file"), true);
616  }
617 
618  $this->ctrl->saveParameter($this, "cdir");
619 
620  ilUtil::renameExecutables($this->main_dir);
621 
622  $this->ctrl->redirect($this, "listFiles");
623  }
624 
628  function confirmDeleteFile()
629  {
630  global $ilCtrl, $tpl, $lng;
631 
632  if (!is_array($_POST["file"]) || count($_POST["file"]) == 0)
633  {
634  ilUtil::sendFailure($lng->txt("no_checkbox"), true);
635  $ilCtrl->redirect($this, "listFile");
636  }
637  else
638  {
639  include_once("./Services/Utilities/classes/class.ilConfirmationGUI.php");
640  $cgui = new ilConfirmationGUI();
641  $cgui->setFormAction($ilCtrl->getFormAction($this));
642  $cgui->setHeaderText($lng->txt("info_delete_sure"));
643  $cgui->setCancel($lng->txt("cancel"), "listFiles");
644  $cgui->setConfirm($lng->txt("delete"), "deleteFile");
645 
646  foreach ($_POST["file"] as $i)
647  {
648  $cgui->addItem("file[]", $i, $i);
649  }
650 
651  $tpl->setContent($cgui->getHTML());
652  }
653  }
654 
658  function deleteFile()
659  {
660  global $lng;
661 
662  if (!isset($_POST["file"]))
663  {
664  $this->ilias->raiseError($this->lng->txt("no_checkbox"),$this->ilias->error_obj->MESSAGE);
665  }
666 
667  foreach ($_POST["file"] as $post_file)
668  {
669  if (ilUtil::stripSlashes($post_file) == "..")
670  {
671  $this->ilias->raiseError($this->lng->txt("no_checkbox"),$this->ilias->error_obj->MESSAGE);
672  break;
673  }
674 
675  $cur_subdir = str_replace(".", "", ilUtil::stripSlashes($_GET["cdir"]));
676  $cur_dir = (!empty($cur_subdir))
677  ? $this->main_dir."/".$cur_subdir
678  : $this->main_dir;
679  $file = $cur_dir."/".ilUtil::stripSlashes($post_file);
680 
681  if (@is_file($file))
682  {
683  unlink($file);
684  }
685 
686  if (@is_dir($file))
687  {
688  $is_dir = true;
690  }
691  }
692 
693  $this->ctrl->saveParameter($this, "cdir");
694  if ($is_dir)
695  {
696  ilUtil::sendSuccess($lng->txt("cont_dir_deleted"), true);
697  $this->setPerformedCommand("delete_dir",
698  array("name" => ilUtil::stripSlashes($post_file)));
699  }
700  else
701  {
702  ilUtil::sendSuccess($lng->txt("cont_file_deleted"), true);
703  $this->setPerformedCommand("delete_file",
704  array("name" => ilUtil::stripSlashes($post_file)));
705  }
706  $this->ctrl->redirect($this, "listFiles");
707  }
708 
712  function unzipFile()
713  {
714  global $lng;
715 
716  if (!isset($_POST["file"]))
717  {
718  $this->ilias->raiseError($this->lng->txt("no_checkbox"),$this->ilias->error_obj->MESSAGE);
719  }
720 
721  if (count($_POST["file"]) > 1)
722  {
723  $this->ilias->raiseError($this->lng->txt("cont_select_max_one_item"),$this->ilias->error_obj->MESSAGE);
724  }
725 
726  $cur_subdir = str_replace(".", "", ilUtil::stripSlashes($_GET["cdir"]));
727  $cur_dir = (!empty($cur_subdir))
728  ? $this->main_dir."/".$cur_subdir
729  : $this->main_dir;
730  $file = $cur_dir."/".ilUtil::stripSlashes($_POST["file"][0]);
731 
732  if (@is_file($file))
733  {
734  if ($this->getAllowDirectories())
735  {
736  ilUtil::unzip($file, true);
737  }
738  else
739  {
740  ilUtil::unzip($file, true, true);
741  }
742  }
743 
744  ilUtil::renameExecutables($this->main_dir);
745 
746  $this->ctrl->saveParameter($this, "cdir");
747  ilUtil::sendSuccess($lng->txt("cont_file_unzipped"), true);
748  $this->ctrl->redirect($this, "listFiles");
749  }
750 
754  function downloadFile()
755  {
756  if (!isset($_POST["file"]))
757  {
758  $this->ilias->raiseError($this->lng->txt("no_checkbox"),$this->ilias->error_obj->MESSAGE);
759  }
760 
761  if (count($_POST["file"]) > 1)
762  {
763  $this->ilias->raiseError($this->lng->txt("cont_select_max_one_item"),$this->ilias->error_obj->MESSAGE);
764  }
765 
766  $cur_subdir = str_replace(".", "", ilUtil::stripSlashes($_GET["cdir"]));
767  $cur_dir = (!empty($cur_subdir))
768  ? $this->main_dir."/".$cur_subdir
769  : $this->main_dir;
770  $file = $cur_dir."/".$_POST["file"][0];
771 
772  // validate against files of current directory
773  $valid = false;
774  foreach(ilUtil::getDir($cur_dir) as $entry)
775  {
776  if($entry["type"] == "file" &&
777  $cur_dir."/".$entry["entry"] == $file)
778  {
779  $valid = true;
780  break;
781  }
782  }
783 
784  if (@is_file($file) && !(@is_dir($file)) && $valid)
785  {
786  ilUtil::deliverFile($file, $_POST["file"][0]);
787  exit;
788  }
789  else
790  {
791  $this->ctrl->saveParameter($this, "cdir");
792  $this->ctrl->redirect($this, "listFiles");
793  }
794  }
795 
799  function getTabs(&$tabs_gui)
800  {
801  global $ilCtrl;
802 
803  $ilCtrl->setParameter($this, "resetoffset", 1);
804  $tabs_gui->addTarget("cont_list_files",
805  $this->ctrl->getLinkTarget($this, "listFiles"), "listFiles",
806  get_class($this));
807  $ilCtrl->setParameter($this, "resetoffset", "");
808  }
809 
810 }
811 ?>