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