ILIAS  Release_4_1_x_branch Revision 61804
 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->commands = array();
27  $this->file_labels = array();
28  $this->label_enable = false;
29  $this->ctrl->saveParameter($this, "cdir");
30  $lng->loadLanguageModule("content");
31  $this->setAllowDirectories(true);
32 //echo "<br>main_dir:".$this->main_dir.":";
33  }
34 
40  function setAllowDirectories($a_val)
41  {
42  $this->allow_directories = $a_val;
43  }
44 
51  {
52  return $this->allow_directories;
53  }
54 
60  function setTableId($a_val)
61  {
62  $this->table_id = $a_val;
63  }
64 
70  function getTableId()
71  {
72  return $this->table_id;
73  }
74 
80  function setTitle($a_val)
81  {
82  $this->title = $a_val;
83  }
84 
90  function getTitle()
91  {
92  return $this->title;
93  }
94 
101  protected function setPerformedCommand($command, $pars = "")
102  {
103  if (!is_array($pars))
104  {
105  $pars = array();
106  }
107  $_SESSION["fsys"]["lastcomm"] = array_merge(
108  array("cmd" => $command), $pars);
109  }
110 
116  public function getLastPerformedCommand()
117  {
118  $ret = $_SESSION["fsys"]["lastcomm"];
119  $_SESSION["fsys"]["lastcomm"] = "none";
120  return $ret;
121  }
122 
126  function &executeCommand()
127  {
128  $next_class = $this->ctrl->getNextClass($this);
129  $cmd = $this->ctrl->getCmd();
130 
131  switch($next_class)
132  {
133 
134  default:
135  if (substr($cmd, 0, 11) == "extCommand_")
136  {
137  $ret =& $this->extCommand(substr($cmd, 11, strlen($cmd) - 11));
138  }
139  else
140  {
141  $ret =& $this->$cmd();
142  }
143  break;
144  }
145 
146  return $ret;
147  }
148 
149 
153  function addCommand(&$a_obj, $a_func, $a_name)
154  {
155  $i = count($this->commands);
156 
157  $this->commands[$i]["object"] =& $a_obj;
158  $this->commands[$i]["method"] = $a_func;
159  $this->commands[$i]["name"] = $a_name;
160 
161  //$this->commands[] = $arr;
162  }
163 
164 
168  function labelFile($a_file, $a_label)
169  {
170  $this->file_labels[$a_file][] = $a_label;
171  }
172 
176  function activateLabels($a_act, $a_label_header)
177  {
178  $this->label_enable = $a_act;
179  $this->label_header = $a_label_header;
180  }
181 
185  function &extCommand($a_nr)
186  {
187  if (!isset($_POST["file"]))
188  {
189  $this->ilias->raiseError($this->lng->txt("no_checkbox"),$this->ilias->error_obj->MESSAGE);
190  }
191 
192  if (count($_POST["file"]) > 1)
193  {
194  $this->ilias->raiseError($this->lng->txt("cont_select_max_one_item"),$this->ilias->error_obj->MESSAGE);
195  }
196 
197  if ($_POST["file"][0] == ".." )
198  {
199  $this->ilias->raiseError($this->lng->txt("select_a_file"),$this->ilias->error_obj->MESSAGE);
200  }
201 
202  $cur_subdir = str_replace(".", "", ilUtil::stripSlashes($_GET["cdir"]));
203  $file = (!empty($cur_subdir))
204  ? $this->main_dir."/".$cur_subdir."/".ilUtil::stripSlashes($_POST["file"][0])
205  : $this->main_dir."/".ilUtil::stripSlashes($_POST["file"][0]);
206 
207  // check wether selected item is a directory
208  if (@is_dir($file))
209  {
210  $this->ilias->raiseError($this->lng->txt("select_a_file"),$this->ilias->error_obj->MESSAGE);
211  }
212 
213  $file = (!empty($cur_subdir))
214  ? $cur_subdir."/".ilUtil::stripSlashes($_POST["file"][0])
215  : ilUtil::stripSlashes($_POST["file"][0]);
216 
217  $obj =& $this->commands[$a_nr]["object"];
218  $method = $this->commands[$a_nr]["method"];
219 
220 
221  return $obj->$method($file);
222  }
223 
224 
228  function listFiles()
229  {
230  global $ilToolbar, $lng, $ilCtrl;
231 
232 
233  // determine directory
234  // FIXME: I have to call stripSlashes here twice, because I could not
235  // determine where the second layer of slashes is added to the
236  // URL Parameter
237  $cur_subdir = ilUtil::stripSlashes(ilUtil::stripSlashes($_GET["cdir"]));
238  $new_subdir = ilUtil::stripSlashes(ilUtil::stripSlashes($_GET["newdir"]));
239 
240  if($new_subdir == "..")
241  {
242  $cur_subdir = substr($cur_subdir, 0, strrpos($cur_subdir, "/"));
243  }
244  else
245  {
246  if (!empty($new_subdir))
247  {
248  if (!empty($cur_subdir))
249  {
250  $cur_subdir = $cur_subdir."/".$new_subdir;
251  }
252  else
253  {
254  $cur_subdir = $new_subdir;
255  }
256  }
257  }
258 
259  $cur_subdir = str_replace("..", "", $cur_subdir);
260 
261  $cur_dir = (!empty($cur_subdir))
262  ? $this->main_dir."/".$cur_subdir
263  : $this->main_dir;
264 
265  $this->ctrl->setParameter($this, "cdir", $cur_subdir);
266 
267  // toolbar for adding files/directories
268  $ilToolbar->setFormAction($ilCtrl->getFormAction($this), true);
269  include_once("./Services/Form/classes/class.ilTextInputGUI.php");
270 
271  if ($this->getAllowDirectories())
272  {
273  $ti = new ilTextInputGUI($this->lng->txt("cont_new_dir"), "new_dir");
274  $ti->setMaxLength(80);
275  $ti->setSize(10);
276  $ilToolbar->addInputItem($ti, true);
277  $ilToolbar->addFormButton($lng->txt("create"), "createDirectory");
278 
279  $ilToolbar->addSeparator();
280  }
281 
282  include_once("./Services/Form/classes/class.ilFileInputGUI.php");
283  $fi = new ilFileInputGUI($this->lng->txt("cont_new_file"), "new_file");
284  $fi->setSize(10);
285  $ilToolbar->addInputItem($fi, true);
286  $ilToolbar->addFormButton($lng->txt("upload"), "uploadFile");
287 
288  include_once 'Services/FileSystemStorage/classes/class.ilUploadFiles.php';
290  {
291  $ilToolbar->addSeparator();
293  $options[""] = $lng->txt("cont_select_from_upload_dir");
294  foreach($files as $file)
295  {
296  $file = htmlspecialchars($file, ENT_QUOTES, "utf-8");
297  $options[$file] = $file;
298  }
299  include_once("./Services/Form/classes/class.ilSelectInputGUI.php");
300  $si = new ilSelectInputGUI($this->lng->txt("cont_uploaded_file"), "uploaded_file");
301  $si->setOptions($options);
302  $ilToolbar->addInputItem($si, true);
303  $ilToolbar->addFormButton($lng->txt("copy"), "uploadFile");
304  }
305 
306  // load files templates
307  //$this->tpl->addBlockfile("ADM_CONTENT", "adm_content", "tpl.directory.html", false);
308  include_once("./Services/FileSystemStorage/classes/class.ilFileSystemTableGUI.php");
309  $fs_table = new ilFileSystemTableGUI($this, "listFiles", $cur_dir, $cur_subdir,
310  $this->label_enable, $this->file_labels, $this->label_header, $this->commands);
311  $fs_table->setId($this->getTableId());
312  if ($this->getTitle() != "")
313  {
314  $fs_table->setTitle($this->getTitle());
315  }
316  if ($_GET["resetoffset"] == 1)
317  {
318  $fs_table->resetOffset();
319  }
320  $this->tpl->setContent($fs_table->getHTML());
321  }
322 
326  function renameFileForm()
327  {
328  if (!isset($_POST["file"]))
329  {
330  $this->ilias->raiseError($this->lng->txt("no_checkbox"),$this->ilias->error_obj->MESSAGE);
331  }
332 
333  if (count($_POST["file"]) > 1)
334  {
335  $this->ilias->raiseError($this->lng->txt("cont_select_max_one_item"),$this->ilias->error_obj->MESSAGE);
336  }
337 
338  if (ilUtil::stripSlashes($_POST["file"][0]) == ".." )
339  {
340  $this->ilias->raiseError($this->lng->txt("select_a_file"),$this->ilias->error_obj->MESSAGE);
341  }
342 
343  $cur_subdir = str_replace(".", "", ilUtil::stripSlashes($_GET["cdir"]));
344  $file = (!empty($cur_subdir))
345  ? $this->main_dir."/".$cur_subdir."/".ilUtil::stripSlashes($_POST["file"][0])
346  : $this->main_dir."/".ilUtil::stripSlashes($_POST["file"][0]);
347 
348  // load files templates
349  $this->tpl->addBlockfile("ADM_CONTENT", "adm_content", "tpl.file_rename.html", false);
350 
351  $this->ctrl->setParameter($this, "old_name", ilUtil::stripSlashes($_POST["file"][0]));
352  $this->tpl->setVariable("FORMACTION", $this->ctrl->getFormAction($this, "renameFile"));
353  if (@is_dir($file))
354  {
355  $this->tpl->setVariable("TXT_HEADER", $this->lng->txt("cont_rename_dir"));
356  }
357  else
358  {
359  $this->tpl->setVariable("TXT_HEADER", $this->lng->txt("rename_file"));
360  }
361  $this->tpl->setVariable("TXT_NAME", $this->lng->txt("name"));
362  $this->tpl->setVariable("VAL_NAME", ilUtil::stripSlashes($_POST["file"][0]));
363  $this->tpl->setVariable("CMD_CANCEL", "cancelRename");
364  $this->tpl->setVariable("CMD_SUBMIT", "renameFile");
365  $this->tpl->setVariable("TXT_CANCEL", $this->lng->txt("cancel"));
366  $this->tpl->setVariable("TXT_SUBMIT", $this->lng->txt("rename"));
367 
368  $this->tpl->parseCurrentBlock();
369  }
370 
374  function renameFile()
375  {
376  global $lng;
377 
378  $new_name = str_replace("..", "", ilUtil::stripSlashes($_POST["new_name"]));
379  $new_name = str_replace("/", "", $new_name);
380  if ($new_name == "")
381  {
382  $this->ilias->raiseError($this->lng->txt("enter_new_name"),$this->ilias->error_obj->MESSAGE);
383  }
384 
385  $cur_subdir = str_replace(".", "", ilUtil::stripSlashes($_GET["cdir"]));
386  $dir = (!empty($cur_subdir))
387  ? $this->main_dir."/".$cur_subdir."/"
388  : $this->main_dir."/";
389 
390  rename($dir.ilUtil::stripSlashes($_GET["old_name"]), $dir.$new_name);
391 
392  ilUtil::renameExecutables($this->main_dir);
393  if (@is_dir($dir.$new_name))
394  {
395  ilUtil::sendSuccess($lng->txt("cont_dir_renamed"), true);
396  $this->setPerformedCommand("rename_dir", array("old_name" => $_GET["old_name"],
397  "new_name" => $new_name));
398  }
399  else
400  {
401  ilUtil::sendSuccess($lng->txt("cont_file_renamed"), true);
402  $this->setPerformedCommand("rename_file", array("old_name" => $_GET["old_name"],
403  "new_name" => $new_name));
404  }
405  $this->ctrl->redirect($this, "listFiles");
406  }
407 
411  function cancelRename()
412  {
413  $this->ctrl->redirect($this, "listFiles");
414  }
415 
419  function createDirectory()
420  {
421  global $lng;
422 
423  // determine directory
424  $cur_subdir = str_replace(".", "", ilUtil::stripSlashes($_GET["cdir"]));
425  $cur_dir = (!empty($cur_subdir))
426  ? $this->main_dir."/".$cur_subdir
427  : $this->main_dir;
428 
429  $new_dir = str_replace(".", "", ilUtil::stripSlashes($_POST["new_dir"]));
430  $new_dir = str_replace("/", "", $new_dir);
431 
432  if (!empty($new_dir))
433  {
434  ilUtil::makeDir($cur_dir."/".$new_dir);
435  if (is_dir($cur_dir."/".$new_dir))
436  {
437  ilUtil::sendSuccess($lng->txt("cont_dir_created"), true);
438  $this->setPerformedCommand("create_dir", array("name" => $new_dir));
439  }
440  }
441  else
442  {
443  ilUtil::sendFailure($lng->txt("cont_enter_a_dir_name"), true);
444  }
445  $this->ctrl->saveParameter($this, "cdir");
446  $this->ctrl->redirect($this, "listFiles");
447  }
448 
452  function uploadFile()
453  {
454  global $lng;
455 
456  // determine directory
457  $cur_subdir = str_replace(".", "", ilUtil::stripSlashes($_GET["cdir"]));
458  $cur_dir = (!empty($cur_subdir))
459  ? $this->main_dir."/".$cur_subdir
460  : $this->main_dir;
461 
462 
463  include_once 'Services/FileSystemStorage/classes/class.ilUploadFiles.php';
464 
465  if (is_file($_FILES["new_file"]["tmp_name"]))
466  {
467  move_uploaded_file($_FILES["new_file"]["tmp_name"],
468  $cur_dir."/".ilUtil::stripSlashes($_FILES["new_file"]["name"]));
469  if (is_file($cur_dir."/".ilUtil::stripSlashes($_FILES["new_file"]["name"])))
470  {
471  ilUtil::sendSuccess($lng->txt("cont_file_created"), true);
472  $this->setPerformedCommand("create_file",
473  array("name" => ilUtil::stripSlashes($_FILES["new_file"]["name"])));
474 
475  }
476  }
477  elseif ($_POST["uploaded_file"])
478  {
479  // check if the file is in the ftp directory and readable
480  if (ilUploadFiles::_checkUploadFile($_POST["uploaded_file"]))
481  {
482  // copy uploaded file to data directory
483  ilUploadFiles::_copyUploadFile($_POST["uploaded_file"],
484  $cur_dir."/".ilUtil::stripSlashes($_POST["uploaded_file"]));
485  }
486  if (is_file($cur_dir."/".ilUtil::stripSlashes($_POST["uploaded_file"])))
487  {
488  ilUtil::sendSuccess($lng->txt("cont_file_created"), true);
489  $this->setPerformedCommand("create_file",
490  array("name" => ilUtil::stripSlashes($_POST["uploaded_file"])));
491  }
492  }
493  else if (trim($_FILES["new_file"]["name"]) == "")
494  {
495  ilUtil::sendFailure($lng->txt("cont_enter_a_file"), true);
496  }
497 
498  $this->ctrl->saveParameter($this, "cdir");
499 
500  ilUtil::renameExecutables($this->main_dir);
501 
502  $this->ctrl->redirect($this, "listFiles");
503  }
504 
508  function confirmDeleteFile()
509  {
510  global $ilCtrl, $tpl, $lng;
511 
512  if (!is_array($_POST["file"]) || count($_POST["file"]) == 0)
513  {
514  ilUtil::sendFailure($lng->txt("no_checkbox"), true);
515  $ilCtrl->redirect($this, "listFile");
516  }
517  else
518  {
519  include_once("./Services/Utilities/classes/class.ilConfirmationGUI.php");
520  $cgui = new ilConfirmationGUI();
521  $cgui->setFormAction($ilCtrl->getFormAction($this));
522  $cgui->setHeaderText($lng->txt("info_delete_sure"));
523  $cgui->setCancel($lng->txt("cancel"), "listFiles");
524  $cgui->setConfirm($lng->txt("delete"), "deleteFile");
525 
526  foreach ($_POST["file"] as $i)
527  {
528  $cgui->addItem("file[]", $i, $i);
529  }
530 
531  $tpl->setContent($cgui->getHTML());
532  }
533  }
534 
538  function deleteFile()
539  {
540  global $lng;
541 
542  if (!isset($_POST["file"]))
543  {
544  $this->ilias->raiseError($this->lng->txt("no_checkbox"),$this->ilias->error_obj->MESSAGE);
545  }
546 
547  foreach ($_POST["file"] as $post_file)
548  {
549  if (ilUtil::stripSlashes($post_file) == "..")
550  {
551  $this->ilias->raiseError($this->lng->txt("no_checkbox"),$this->ilias->error_obj->MESSAGE);
552  break;
553  }
554 
555  $cur_subdir = str_replace(".", "", ilUtil::stripSlashes($_GET["cdir"]));
556  $cur_dir = (!empty($cur_subdir))
557  ? $this->main_dir."/".$cur_subdir
558  : $this->main_dir;
559  $file = $cur_dir."/".ilUtil::stripSlashes($post_file);
560 
561  if (@is_file($file))
562  {
563  unlink($file);
564  }
565 
566  if (@is_dir($file))
567  {
568  $is_dir = true;
570  }
571  }
572 
573  $this->ctrl->saveParameter($this, "cdir");
574  if ($is_dir)
575  {
576  ilUtil::sendSuccess($lng->txt("cont_dir_deleted"), true);
577  $this->setPerformedCommand("delete_dir",
578  array("name" => ilUtil::stripSlashes($post_file)));
579  }
580  else
581  {
582  ilUtil::sendSuccess($lng->txt("cont_file_deleted"), true);
583  $this->setPerformedCommand("delete_file",
584  array("name" => ilUtil::stripSlashes($post_file)));
585  }
586  $this->ctrl->redirect($this, "listFiles");
587  }
588 
592  function unzipFile()
593  {
594  global $lng;
595 
596  if (!isset($_POST["file"]))
597  {
598  $this->ilias->raiseError($this->lng->txt("no_checkbox"),$this->ilias->error_obj->MESSAGE);
599  }
600 
601  if (count($_POST["file"]) > 1)
602  {
603  $this->ilias->raiseError($this->lng->txt("cont_select_max_one_item"),$this->ilias->error_obj->MESSAGE);
604  }
605 
606  $cur_subdir = str_replace(".", "", ilUtil::stripSlashes($_GET["cdir"]));
607  $cur_dir = (!empty($cur_subdir))
608  ? $this->main_dir."/".$cur_subdir
609  : $this->main_dir;
610  $file = $cur_dir."/".ilUtil::stripSlashes($_POST["file"][0]);
611 
612  if (@is_file($file))
613  {
614  if ($this->getAllowDirectories())
615  {
616  ilUtil::unzip($file, true);
617  }
618  else
619  {
620  ilUtil::unzip($file, true, true);
621  }
622  }
623 
624  ilUtil::renameExecutables($this->main_dir);
625 
626  $this->ctrl->saveParameter($this, "cdir");
627  ilUtil::sendSuccess($lng->txt("cont_file_unzipped"), true);
628  $this->ctrl->redirect($this, "listFiles");
629  }
630 
634  function downloadFile()
635  {
636  if (!isset($_POST["file"]))
637  {
638  $this->ilias->raiseError($this->lng->txt("no_checkbox"),$this->ilias->error_obj->MESSAGE);
639  }
640 
641  if (count($_POST["file"]) > 1)
642  {
643  $this->ilias->raiseError($this->lng->txt("cont_select_max_one_item"),$this->ilias->error_obj->MESSAGE);
644  }
645 
646  $cur_subdir = str_replace(".", "", ilUtil::stripSlashes($_GET["cdir"]));
647  $cur_dir = (!empty($cur_subdir))
648  ? $this->main_dir."/".$cur_subdir
649  : $this->main_dir;
650  $file = $cur_dir."/".$_POST["file"][0];
651 
652  // validate against files of current directory
653  $valid = false;
654  foreach(ilUtil::getDir($cur_dir) as $entry)
655  {
656  if($entry["type"] == "file" &&
657  $cur_dir."/".$entry["entry"] == $file)
658  {
659  $valid = true;
660  break;
661  }
662  }
663 
664  if (@is_file($file) && !(@is_dir($file)) && $valid)
665  {
666  ilUtil::deliverFile($file, $_POST["file"][0]);
667  exit;
668  }
669  else
670  {
671  $this->ctrl->saveParameter($this, "cdir");
672  $this->ctrl->redirect($this, "listFiles");
673  }
674  }
675 
679  function getTabs(&$tabs_gui)
680  {
681  global $ilCtrl;
682 
683  $ilCtrl->setParameter($this, "resetoffset", 1);
684  $tabs_gui->addTarget("cont_list_files",
685  $this->ctrl->getLinkTarget($this, "listFiles"), "listFiles",
686  get_class($this));
687  $ilCtrl->setParameter($this, "resetoffset", "");
688  }
689 
690 }
691 ?>