ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilFileSystemGUI.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
32 {
33  var $ctrl;
34 
35  function ilFileSystemGUI($a_main_directory)
36  {
37  global $lng, $ilCtrl, $tpl, $ilias;
38 
39  $this->ctrl =& $ilCtrl;
40  $this->lng =& $lng;
41  $this->ilias =& $ilias;
42  $this->tpl =& $tpl;
43  $this->main_dir = $a_main_directory;
44  $this->commands = array();
45  $this->file_labels = array();
46  $this->label_enable = false;
47  $this->ctrl->saveParameter($this, "cdir");
48 //echo "<br>main_dir:".$this->main_dir.":";
49  }
50 
54  function &executeCommand()
55  {
56  $next_class = $this->ctrl->getNextClass($this);
57  $cmd = $this->ctrl->getCmd();
58 
59  switch($next_class)
60  {
61 
62  default:
63  if (substr($cmd, 0, 11) == "extCommand_")
64  {
65  $ret =& $this->extCommand(substr($cmd, 11, strlen($cmd) - 11));
66  }
67  else
68  {
69  $ret =& $this->$cmd();
70  }
71  break;
72  }
73 
74  return $ret;
75  }
76 
77 
81  function addCommand(&$a_obj, $a_func, $a_name)
82  {
83  $i = count($this->commands);
84 
85  $this->commands[$i]["object"] =& $a_obj;
86  $this->commands[$i]["method"] = $a_func;
87  $this->commands[$i]["name"] = $a_name;
88 
89  //$this->commands[] = $arr;
90  }
91 
92 
96  function labelFile($a_file, $a_label)
97  {
98  $this->file_labels[$a_file] = $a_label;
99  }
100 
104  function activateLabels($a_act, $a_label_header)
105  {
106  $this->label_enable = $a_act;
107  $this->label_header = $a_label_header;
108  }
109 
113  function &extCommand($a_nr)
114  {
115  if (!isset($_POST["file"]))
116  {
117  $this->ilias->raiseError($this->lng->txt("no_checkbox"),$this->ilias->error_obj->MESSAGE);
118  }
119 
120  if (count($_POST["file"]) > 1)
121  {
122  $this->ilias->raiseError($this->lng->txt("cont_select_max_one_item"),$this->ilias->error_obj->MESSAGE);
123  }
124 
125  if ($_POST["file"][0] == ".." )
126  {
127  $this->ilias->raiseError($this->lng->txt("select_a_file"),$this->ilias->error_obj->MESSAGE);
128  }
129 
130  $cur_subdir = str_replace(".", "", ilUtil::stripSlashes($_GET["cdir"]));
131  $file = (!empty($cur_subdir))
132  ? $this->main_dir."/".$cur_subdir."/".ilUtil::stripSlashes($_POST["file"][0])
133  : $this->main_dir."/".ilUtil::stripSlashes($_POST["file"][0]);
134 
135  // check wether selected item is a directory
136  if (@is_dir($file))
137  {
138  $this->ilias->raiseError($this->lng->txt("select_a_file"),$this->ilias->error_obj->MESSAGE);
139  }
140 
141  $file = (!empty($cur_subdir))
142  ? $cur_subdir."/".ilUtil::stripSlashes($_POST["file"][0])
143  : ilUtil::stripSlashes($_POST["file"][0]);
144 
145  $obj =& $this->commands[$a_nr]["object"];
146  $method = $this->commands[$a_nr]["method"];
147 
148 
149  return $obj->$method($file);
150  }
151 
152 
156  function listFiles()
157  {
158  // create table
159  require_once("./Services/Table/classes/class.ilTableGUI.php");
160  $tbl = new ilTableGUI();
161 
162  // determine directory
163  // FIXME: I have to call stripSlashes here twice, because I could not
164  // determine where the second layer of slashes is added to the
165  // URL Parameter
166  $cur_subdir = ilUtil::stripSlashes(ilUtil::stripSlashes($_GET["cdir"]));
167  $new_subdir = ilUtil::stripSlashes(ilUtil::stripSlashes($_GET["newdir"]));
168 
169  if($new_subdir == "..")
170  {
171  $cur_subdir = substr($cur_subdir, 0, strrpos($cur_subdir, "/"));
172  }
173  else
174  {
175  if (!empty($new_subdir))
176  {
177  if (!empty($cur_subdir))
178  {
179  $cur_subdir = $cur_subdir."/".$new_subdir;
180  }
181  else
182  {
183  $cur_subdir = $new_subdir;
184  }
185  }
186  }
187 
188  $cur_subdir = str_replace("..", "", $cur_subdir);
189 
190  $cur_dir = (!empty($cur_subdir))
191  ? $this->main_dir."/".$cur_subdir
192  : $this->main_dir;
193 
194  // load files templates
195  $this->tpl->addBlockfile("ADM_CONTENT", "adm_content", "tpl.directory.html", false);
196 
197  $this->ctrl->setParameter($this, "cdir", $cur_subdir);
198  $this->tpl->setVariable("FORMACTION1", $this->ctrl->getFormAction($this, "createDirectory"));
199  $this->tpl->setVariable("TXT_NEW_DIRECTORY", $this->lng->txt("cont_new_dir"));
200  $this->tpl->setVariable("TXT_NEW_FILE", $this->lng->txt("cont_new_file"));
201  $this->tpl->setVariable("CMD_NEW_DIR", "createDirectory");
202  $this->tpl->setVariable("CMD_NEW_FILE", "uploadFile");
203  $this->tpl->setVariable("BTN_NEW_DIR", $this->lng->txt("create"));
204  $this->tpl->setVariable("BTN_NEW_FILE", $this->lng->txt("upload"));
205 
206  //
207  $this->tpl->addBlockfile("FILE_TABLE", "files", "tpl.table.html");
208 
209  // load template for table content data
210  $this->tpl->addBlockfile("TBL_CONTENT", "tbl_content", "tpl.directory_row.html", false);
211 
212  $num = 0;
213 
214  $obj_str = ($this->call_by_reference) ? "" : "&obj_id=".$this->obj_id;
215  $this->tpl->setVariable("FORMACTION", $this->ctrl->getFormAction($this));
216 
217  $tbl->setTitle($this->lng->txt("cont_files")." ".$cur_subdir);
218  //$tbl->setHelp("tbl_help.php","icon_help.gif",$this->lng->txt("help"));
219 
220  /*
221  $cols = array("", "", "dir_file", "size");
222  $header_params = array("ref_id" => $_GET["ref_id"], "obj_id" => $_GET["obj_id"],
223  "cmd" => "listFiles", "hier_id" => $_GET["hier_id"]);
224  $tbl->setHeaderVars($cols, $header_params);*/
225  //$tbl->setColumnWidth(array("1%", "1%", "60%", "40%"));
226 
227  // control
228  /*
229  $tbl->setOrderColumn($_GET["sort_by"]);
230  $tbl->setOrderDirection($_GET["sort_order"]);
231  $tbl->setLimit($_GET["limit"]);
232  $tbl->setOffset($_GET["offset"]);
233  $tbl->setMaxCount($this->maxcount);*/ // ???
234  //$tbl->setMaxCount(30); // ???
235 
236  if (!$this->label_enable)
237  {
238  $tbl->setHeaderNames(array("", "", $this->lng->txt("cont_dir_file"),
239  $this->lng->txt("cont_size")));
240  $this->tpl->setVariable("COLUMN_COUNTS", 4);
241  $tbl->setColumnWidth(array("1%", "1%", "60%", "40%"));
242  $cols = array("", "", "dir_file", "size");
243  $header_params = $this->ctrl->getParameterArray($this, "listFiles");
244  /*
245  $header_params = array("ref_id" => $_GET["ref_id"], "obj_id" => $_GET["obj_id"],
246  "cmd" => "listFiles", "cmdClass" => get_class($this),
247  "hier_id" => $_GET["hier_id"]);*/
248  $tbl->setHeaderVars($cols, $header_params);
249  }
250  else
251  {
252  $tbl->setHeaderNames(array("", "", $this->lng->txt("cont_dir_file"),
253  $this->lng->txt("cont_size"), $this->label_header));
254  $this->tpl->setVariable("COLUMN_COUNTS", 5);
255  $tbl->setColumnWidth(array("1%", "1%", "50%", "25%", "25%"));
256  $cols = array("", "", "dir_file", "size", "label");
257  $header_params = $this->ctrl->getParameterArray($this, "listFiles");
258  /*
259  $header_params = array("ref_id" => $_GET["ref_id"], "obj_id" => $_GET["obj_id"],
260  "cmd" => "listFiles", "cmdClass" => get_class($this), "hier_id" => $_GET["hier_id"]);*/
261  $tbl->setHeaderVars($cols, $header_params);
262  }
263 
264  $tbl->setOrderColumn($_GET["sort_by"]);
265  $tbl->setOrderDirection($_GET["sort_order"]);
266  $tbl->setLimit($_GET["limit"]);
267  $tbl->setOffset($_GET["offset"]);
268  $tbl->setMaxCount($this->maxcount); // ???
269 
270  // delete button
271  $this->tpl->setVariable("IMG_ARROW", ilUtil::getImagePath("arrow_downright.gif"));
272  $this->tpl->setCurrentBlock("tbl_action_btn");
273  $this->tpl->setVariable("BTN_NAME", "deleteFile");
274  $this->tpl->setVariable("BTN_VALUE", $this->lng->txt("delete"));
275  $this->tpl->parseCurrentBlock();
276 
277  // unzip button
278  $this->tpl->setCurrentBlock("tbl_action_btn");
279  $this->tpl->setVariable("BTN_NAME", "unzipFile");
280  $this->tpl->setVariable("BTN_VALUE", $this->lng->txt("unzip"));
281  $this->tpl->parseCurrentBlock();
282 
283  // download button
284  $this->tpl->setCurrentBlock("tbl_action_btn");
285  $this->tpl->setVariable("BTN_NAME", "downloadFile");
286  $this->tpl->setVariable("BTN_VALUE", $this->lng->txt("download"));
287  $this->tpl->parseCurrentBlock();
288 
289  // rename button
290  $this->tpl->setCurrentBlock("tbl_action_btn");
291  $this->tpl->setVariable("BTN_NAME", "renameFileForm");
292  $this->tpl->setVariable("BTN_VALUE", $this->lng->txt("rename"));
293  $this->tpl->parseCurrentBlock();
294 
295  // additional commands
296  for ($i=0; $i < count($this->commands); $i++)
297  {
298  $this->tpl->setCurrentBlock("tbl_action_btn");
299  $this->tpl->setVariable("BTN_NAME", "extCommand_".$i);
300  $this->tpl->setVariable("BTN_VALUE", $this->commands[$i]["name"]);
301  $this->tpl->parseCurrentBlock();
302  }
303 
304 
305  // footer
306  $tbl->setFooter("tblfooter",$this->lng->txt("previous"),$this->lng->txt("next"));
307  //$tbl->disable("footer");
308 
309  if (is_dir($cur_dir))
310  {
311  $entries = ilUtil::getDir($cur_dir);
312  }
313  else
314  {
315  $entries = array(array("type" => "dir", "entry" => ".."));
316  }
317 
318  //$objs = ilUtil::sortArray($objs, $_GET["sort_by"], $_GET["sort_order"]);
319  $tbl->setMaxCount(count($entries));
320  $entries = array_slice($entries, $_GET["offset"], $_GET["limit"]);
321 
322  $tbl->render();
323  if(count($entries) > 0)
324  {
325  $i=0;
326  foreach($entries as $entry)
327  {
328  if(($entry["entry"] == ".") || ($entry["entry"] == ".." && empty($cur_subdir)))
329  {
330  continue;
331  }
332 
333  $cfile = (!empty($cur_subdir))
334  ? $cur_subdir."/".$entry["entry"]
335  : $entry["entry"];
336 
337  // label
338  if ($this->label_enable)
339  {
340  $this->tpl->setCurrentBlock("Label");
341  if ($this->file_labels[$cfile] != "")
342  {
343  $this->tpl->setVariable("TXT_LABEL", $this->file_labels[$cfile]);
344  }
345  else
346  {
347  $this->tpl->setVariable("TXT_LABEL", "&nbsp;");
348  }
349  $this->tpl->parseCurrentBlock();
350  }
351 
352  //$this->tpl->setVariable("ICON", $obj["title"]);
353  if($entry["type"] == "dir")
354  {
355  $this->tpl->setCurrentBlock("FileLink");
356  $this->ctrl->setParameter($this, "cdir", $cur_subdir);
357  $this->ctrl->setParameter($this, "newdir", $entry["entry"]);
358  $this->tpl->setVariable("LINK_FILENAME", $this->ctrl->getLinkTarget($this, "listFiles"));
359  $this->tpl->setVariable("TXT_FILENAME", $entry["entry"]);
360  $this->tpl->parseCurrentBlock();
361 
362  $this->tpl->setVariable("ICON", "<img src=\"".
363  ilUtil::getImagePath("icon_cat.gif")."\">");
364  }
365  else
366  {
367  $this->tpl->setCurrentBlock("File");
368  $this->tpl->setVariable("TXT_FILENAME2", $entry["entry"]);
369  $this->tpl->parseCurrentBlock();
370  }
371 
372  $this->tpl->setCurrentBlock("tbl_content");
373  $css_row = ilUtil::switchColor($i++, "tblrow1", "tblrow2");
374  $this->tpl->setVariable("CSS_ROW", $css_row);
375 
376  $this->tpl->setVariable("TXT_SIZE", $entry["size"]);
377  $this->tpl->setVariable("CHECKBOX_ID", $entry["entry"]);
378  $compare = (!empty($cur_subdir))
379  ? $cur_subdir."/".$entry["entry"]
380  : $entry["entry"];
381  $purpose = array();
382 
383  $this->tpl->parseCurrentBlock();
384  }
385  } //if is_array
386  else
387  {
388  $this->tpl->setCurrentBlock("notfound");
389  $this->tpl->setVariable("TXT_OBJECT_NOT_FOUND", $this->lng->txt("obj_not_found"));
390  $this->tpl->setVariable("NUM_COLS", 4);
391  $this->tpl->parseCurrentBlock();
392  }
393 
394  $this->tpl->parseCurrentBlock();
395  }
396 
400  function renameFileForm()
401  {
402  if (!isset($_POST["file"]))
403  {
404  $this->ilias->raiseError($this->lng->txt("no_checkbox"),$this->ilias->error_obj->MESSAGE);
405  }
406 
407  if (count($_POST["file"]) > 1)
408  {
409  $this->ilias->raiseError($this->lng->txt("cont_select_max_one_item"),$this->ilias->error_obj->MESSAGE);
410  }
411 
412  if (ilUtil::stripSlashes($_POST["file"][0]) == ".." )
413  {
414  $this->ilias->raiseError($this->lng->txt("select_a_file"),$this->ilias->error_obj->MESSAGE);
415  }
416 
417  $cur_subdir = str_replace(".", "", ilUtil::stripSlashes($_GET["cdir"]));
418  $file = (!empty($cur_subdir))
419  ? $this->main_dir."/".$cur_subdir."/".ilUtil::stripSlashes($_POST["file"][0])
420  : $this->main_dir."/".ilUtil::stripSlashes($_POST["file"][0]);
421 
422  // load files templates
423  $this->tpl->addBlockfile("ADM_CONTENT", "adm_content", "tpl.file_rename.html", false);
424 
425  $this->ctrl->setParameter($this, "old_name", ilUtil::stripSlashes($_POST["file"][0]));
426  $this->tpl->setVariable("FORMACTION", $this->ctrl->getFormAction($this, "renameFile"));
427  if (@is_dir($file))
428  {
429  $this->tpl->setVariable("TXT_HEADER", $this->lng->txt("rename_dir"));
430  }
431  else
432  {
433  $this->tpl->setVariable("TXT_HEADER", $this->lng->txt("rename_file"));
434  }
435  $this->tpl->setVariable("TXT_NAME", $this->lng->txt("name"));
436  $this->tpl->setVariable("VAL_NAME", ilUtil::stripSlashes($_POST["file"][0]));
437  $this->tpl->setVariable("CMD_CANCEL", "cancelRename");
438  $this->tpl->setVariable("CMD_SUBMIT", "renameFile");
439  $this->tpl->setVariable("TXT_CANCEL", $this->lng->txt("cancel"));
440  $this->tpl->setVariable("TXT_SUBMIT", $this->lng->txt("rename"));
441 
442  $this->tpl->parseCurrentBlock();
443  }
444 
448  function renameFile()
449  {
450  $new_name = str_replace("..", "", ilUtil::stripSlashes($_POST["new_name"]));
451  $new_name = str_replace("/", "", $new_name);
452  if ($new_name == "")
453  {
454  $this->ilias->raiseError($this->lng->txt("enter_new_name"),$this->ilias->error_obj->MESSAGE);
455  }
456 
457  $cur_subdir = str_replace(".", "", ilUtil::stripSlashes($_GET["cdir"]));
458  $dir = (!empty($cur_subdir))
459  ? $this->main_dir."/".$cur_subdir."/"
460  : $this->main_dir."/";
461 
462  rename($dir.ilUtil::stripSlashes($_GET["old_name"]), $dir.$new_name);
463 
464  ilUtil::renameExecutables($this->main_dir);
465 
466  $this->ctrl->redirect($this, "listFiles");
467  }
468 
472  function cancelRename()
473  {
474  $this->ctrl->redirect($this, "listFiles");
475  }
476 
480  function createDirectory()
481  {
482 
483  // determine directory
484  $cur_subdir = str_replace(".", "", ilUtil::stripSlashes($_GET["cdir"]));
485  $cur_dir = (!empty($cur_subdir))
486  ? $this->main_dir."/".$cur_subdir
487  : $this->main_dir;
488 
489  $new_dir = str_replace(".", "", ilUtil::stripSlashes($_POST["new_dir"]));
490  $new_dir = str_replace("/", "", $new_dir);
491 
492  if (!empty($new_dir))
493  {
494  ilUtil::makeDir($cur_dir."/".$new_dir);
495  }
496  $this->ctrl->saveParameter($this, "cdir");
497  $this->ctrl->redirect($this, "listFiles");
498  }
499 
503  function uploadFile()
504  {
505  // determine directory
506  $cur_subdir = str_replace(".", "", ilUtil::stripSlashes($_GET["cdir"]));
507  $cur_dir = (!empty($cur_subdir))
508  ? $this->main_dir."/".$cur_subdir
509  : $this->main_dir;
510  if (is_file($_FILES["new_file"]["tmp_name"]))
511  {
512  move_uploaded_file($_FILES["new_file"]["tmp_name"],
513  $cur_dir."/".ilUtil::stripSlashes($_FILES["new_file"]["name"]));
514  }
515  $this->ctrl->saveParameter($this, "cdir");
516 
517  ilUtil::renameExecutables($this->main_dir);
518 
519  $this->ctrl->redirect($this, "listFiles");
520  }
521 
522 
526  function deleteFile()
527  {
528  if (!isset($_POST["file"]))
529  {
530  $this->ilias->raiseError($this->lng->txt("no_checkbox"),$this->ilias->error_obj->MESSAGE);
531  }
532 
533  foreach ($_POST["file"] as $post_file)
534  {
535  if (ilUtil::stripSlashes($post_file) == "..")
536  {
537  $this->ilias->raiseError($this->lng->txt("no_checkbox"),$this->ilias->error_obj->MESSAGE);
538  break;
539  }
540 
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  $file = $cur_dir."/".ilUtil::stripSlashes($post_file);
546 
547  if (@is_file($file))
548  {
549  unlink($file);
550  }
551 
552  if (@is_dir($file))
553  {
555  }
556  }
557 
558  $this->ctrl->saveParameter($this, "cdir");
559  $this->ctrl->redirect($this, "listFiles");
560  }
561 
565  function unzipFile()
566  {
567  if (!isset($_POST["file"]))
568  {
569  $this->ilias->raiseError($this->lng->txt("no_checkbox"),$this->ilias->error_obj->MESSAGE);
570  }
571 
572  if (count($_POST["file"]) > 1)
573  {
574  $this->ilias->raiseError($this->lng->txt("cont_select_max_one_item"),$this->ilias->error_obj->MESSAGE);
575  }
576 
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  $file = $cur_dir."/".ilUtil::stripSlashes($_POST["file"][0]);
582 
583  if (@is_file($file))
584  {
585  ilUtil::unzip($file, true);
586  }
587 
588  ilUtil::renameExecutables($this->main_dir);
589 
590  $this->ctrl->saveParameter($this, "cdir");
591  $this->ctrl->redirect($this, "listFiles");
592  }
593 
597  function downloadFile()
598  {
599  if (!isset($_POST["file"]))
600  {
601  $this->ilias->raiseError($this->lng->txt("no_checkbox"),$this->ilias->error_obj->MESSAGE);
602  }
603 
604  if (count($_POST["file"]) > 1)
605  {
606  $this->ilias->raiseError($this->lng->txt("cont_select_max_one_item"),$this->ilias->error_obj->MESSAGE);
607  }
608 
609  $cur_subdir = str_replace(".", "", ilUtil::stripSlashes($_GET["cdir"]));
610  $cur_dir = (!empty($cur_subdir))
611  ? $this->main_dir."/".$cur_subdir
612  : $this->main_dir;
613  $file = $cur_dir."/".$_POST["file"][0];
614 
615  if (@is_file($file) && !(@is_dir($file)))
616  {
617  ilUtil::deliverFile($file, $_POST["file"][0]);
618  exit;
619  }
620  else
621  {
622  $this->ctrl->saveParameter($this, "cdir");
623  $this->ctrl->redirect($this, "listFiles");
624  }
625  }
626 
630  function getTabs(&$tabs_gui)
631  {
632  // object usages
633  $tabs_gui->addTarget("cont_list_files",
634  $this->ctrl->getLinkTarget($this, "listFiles"), "listFiles",
635  get_class($this));
636  }
637 
638 
639 }
640 ?>