ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilCtrl.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
11 class ilCtrl
12 {
13  const IL_RTOKEN_NAME = 'rtoken';
14 
16  var $forward; // forward array
17  var $parent; // parent array (reverse forward)
18  var $save_parameter; // save parameter array
19  var $return; // return commmands
20  var $call_hist = array(); // calling history
21  var $debug = array();
22 
26  function ilCtrl()
27  {
28  global $ilBench;
29 
30  $this->bench =& $ilBench;
31 
32  // initialisation
33  $this->init();
34 
35  // this information should go to xml files one day
36  $this->stored_trees = array
37  ("ilrepositorygui", "ilpersonaldesktopgui",
38  "illmpresentationgui", "illmeditorgui",
39  "iladministrationgui");
40  }
41 
42  function debug($str)
43  {
44 //echo "<br>".$str;
45  $this->debug[] = $str;
46  }
47 
48  function getDebug()
49  {
50  return $this->debug;
51  }
52 
56  function init()
57  {
58  $this->transit = array();
59  $this->forward = array(); // forward array
60  $this->forwards = array(); // forward array
61  $this->parent = array(); // parent array (reverse forward)
62  $this->save_parameter = array(); // save parameter array
63  $this->parameter = array(); // save parameter array
64  $this->return = ""; // return commmands
65  $this->location = array();
66  $this->tab = array();
67  $this->current_node = 0;
68  $this->module_dir = "";
69  $this->service_dir = "";
70  $this->call_node = array();
71  $this->root_class = "";
72  }
73 
83  function callBaseClass()
84  {
85  global $ilDB;
86 
87  $baseClass = strtolower($_GET["baseClass"]);
88 
89  // get class information
90  $mc_set = $ilDB->query("SELECT * FROM module_class WHERE LOWER(class) = ".
91  $ilDB->quote($baseClass, "text"));
92  $mc_rec = $ilDB->fetchAssoc($mc_set);
93  $module = $mc_rec["module"];
94  $class = $mc_rec["class"];
95  $class_dir = $mc_rec["dir"];
96 
97  if ($module != "")
98  {
99  $m_set = $ilDB->query("SELECT * FROM il_component WHERE name = ".
100  $ilDB->quote($module, "text"));
101  $m_rec = $ilDB->fetchAssoc($m_set);
102  $this->module_dir = $m_rec["type"]."/".$m_rec["name"];
103  include_once $this->module_dir."/".$class_dir."/class.".$class.".php";
104  }
105  else // check whether class belongs to a service
106  {
107  $mc_set = $ilDB->query("SELECT * FROM service_class WHERE LOWER(class) = ".
108  $ilDB->quote($baseClass, "text"));
109  $mc_rec = $ilDB->fetchAssoc($mc_set);
110 
111  $service = $mc_rec["service"];
112  $class = $mc_rec["class"];
113  $class_dir = $mc_rec["dir"];
114 
115  if ($service == "")
116  {
117  echo "Could not find entry in modules.xml or services.xml for".
118  $baseClass;
119  exit;
120  }
121 
122  // get service information
123  $m_set = $ilDB->query("SELECT * FROM il_component WHERE name = ".
124  $ilDB->quote($service, "text"));
125  $m_rec = $ilDB->fetchAssoc($m_set);
126  $this->service_dir = $m_rec["type"]."/".$m_rec["name"];
127 
128  include_once $this->service_dir."/".$class_dir."/class.".$class.".php";;
129  }
130 
131  // forward processing to base class
132  $this->getCallStructure(strtolower($baseClass));
133  $base_class_gui =& new $class();
134  $this->forwardCommand($base_class_gui);
135  }
136 
140  function getModuleDir()
141  {
142  return $this->module_dir;
143  }
144 
156  function &forwardCommand(&$a_gui_object)
157  {
158  $class = strtolower(get_class($a_gui_object));
159 
160  $nr = $this->getNodeIdForTargetClass($this->current_node, $class);
161  if ($nr > 0)
162  {
163  $current_node = $this->current_node;
164 
165  $this->current_node = $nr;
166 
167  if (DEVMODE == "1")
168  {
169  $this->call_hist[] = array("class" => get_class($a_gui_object),
170  "mode" => "execComm", "cmd" => $this->getCmd());
171  }
172 
173  $html = $a_gui_object->executeCommand();
174 
175  // reset current node
176  $this->current_node = $current_node;
177 
178  return $html;
179 
180  }
181  echo "ERROR: Can't forward to class $class."; exit;
182 //echo "end forward<br>";
183  }
184 
194  function &getHTML(&$a_gui_object)
195  {
196  $class = strtolower(get_class($a_gui_object));
197 
198  $nr = $this->getNodeIdForTargetClass($this->current_node, $class);
199  if ($nr > 0)
200  {
201  $current_node = $this->current_node;
202 
203  // set current node to new gui class
204  $this->current_node = $nr;
205 
206  if (DEVMODE == "1")
207  {
208  $this->call_hist[] = array("class" => get_class($a_gui_object),
209  "mode" => "getHtml", "cmd" => $this->getCmd());
210  }
211 
212  // get block
213  $html = $a_gui_object->getHTML();
214 
215  // reset current node
216  $this->current_node = $current_node;
217 
218  // return block
219  return $html;
220  }
221  echo "ERROR: Can't getHTML from class $class."; exit;
222  }
223 
227  function setContext($a_obj_id, $a_obj_type, $a_sub_obj_id = 0, $a_sub_obj_type = "")
228  {
229  $this->context_obj_id = $a_obj_id;
230  $this->context_obj_type = $a_obj_type;
231  $this->context_sub_obj_id = $a_sub_obj_id;
232  $this->context_sub_obj_type = $a_sub_obj_type;
233  }
234 
240  public function getContextObjId()
241  {
242  return $this->context_obj_id;
243  }
244 
250  public function getContextObjType()
251  {
252  return $this->context_obj_type;
253  }
254 
260  public function getContextSubObjId()
261  {
262  return $this->context_sub_obj_id;
263  }
264 
270  public function getContextSubObjType()
271  {
272  return $this->context_sub_obj_type;
273  }
274 
296  function getNodeIdForTargetClass($a_par_node, $a_class)
297  {
298  $class = strtolower($a_class);
299 
300  // target class is class of current node id
301  if ($class == $this->call_node[$a_par_node]["class"])
302  {
303  return $a_par_node;
304  }
305 
306  // target class is child of current node id
307  foreach($this->call_node as $nr => $node)
308  {
309  if (($node["parent"] == $a_par_node) &&
310  ($node["class"] == $class))
311  {
312  return $nr;
313  }
314  }
315 
316  // target class is sibling
317  $par = $this->call_node[$a_par_node]["parent"];
318  if ($par != 0)
319  {
320  foreach($this->call_node as $nr => $node)
321  {
322  if (($node["parent"] == $par) &&
323  ($node["class"] == $class))
324  {
325  return $nr;
326  }
327  }
328  }
329 
330  // target class is parent
331  while($par != 0)
332  {
333  if ($this->call_node[$par]["class"] == $class)
334  {
335  return $par;
336  }
337  $par = $this->call_node[$par]["parent"];
338  }
339 
340  // Please do NOT change these lines.
341  // Developers must be aware, if they use classes unknown to the controller
342  // otherwise certain problem will be extremely hard to track down...
343  echo "ERROR: Can't find target class $a_class for node $a_par_node ".
344  "(".$this->call_node[$a_par_node]["class"].").<br>";
345  error_log( "ERROR: Can't find target class $a_class for node $a_par_node ".
346  "(".$this->call_node[$a_par_node]["class"].")");
347 
348  if (DEVMODE == 1)
349  {
350  try
351  {
352  throw new Exception("");
353  }
354  catch(Exception $e)
355  {
356  echo "<pre>".$e->getTraceAsString()."</pre>";
357  }
358  }
359 
360  exit;
361  }
362 
368  function getCmdNode()
369  {
370  return $_GET["cmdNode"];
371  }
372 
380  function addLocation($a_title, $a_link, $a_target = "", $a_ref_id = 0)
381  {
382  $this->location[] = array("title" => $a_title,
383  "link" => $a_link, "target" => $a_target, "ref_id" => $a_ref_id);
384  }
385 
391  function getLocations()
392  {
393  return $this->location;
394  }
395 
404  function addTab($a_lang_var, $a_link, $a_cmd, $a_class)
405  {
406  $a_class = strtolower($a_class);
407 
408  $this->tab[] = array("lang_var" => $a_lang_var,
409  "link" => $a_link, "cmd" => $a_cmd, "class" => $a_class);
410  }
411 
417  function getTabs()
418  {
419  return $this->tab;
420  }
421 
425  function getCallHistory()
426  {
427  return $this->call_hist;
428  }
429 
448  function getCallStructure($a_class, $a_nr = 0, $a_parent = 0)
449  {
450  global $ilDB, $ilLog, $ilUser;
451 
452  $a_class = strtolower($a_class);
453 
454  if (in_array($a_class, $this->stored_trees))
455  {
456 
457  $set = $ilDB->query("SELECT * FROM ctrl_structure WHERE root_class = ".
458  $ilDB->quote($a_class, "text"));
459  $rec = $ilDB->fetchAssoc($set);
460  $this->call_node = unserialize($rec["call_node"]);
461  $this->forward = unserialize($rec["forward"]);
462  $this->parent = unserialize($rec["parent"]);
463  $this->root_class = $a_class;
464  }
465  else
466  {
467  $this->readCallStructure($a_class, $a_nr, $a_parent);
468  }
469 //var_dump($this->call_node);
470 //var_dump($this->forward);
471 //var_dump($this->parent);
472 //var_dump($this->root_class);
473  // check whether command node and command class fit together
474  if ($_GET["cmdNode"] > 0)
475  {
476  if (strtolower($this->call_node[$_GET["cmdNode"]]["class"]) !=
477  strtolower($_GET["cmdClass"]))
478  {
479  if (DEVMODE)
480  {
481  die ("Internal Error: ilCtrl Node Error. cmdClass: '".$_GET["cmdClass"]
482  ."', cmdNode: '".$_GET["cmdNode"]."' . Internally cmdNode is assigned to ".
483  "class '".$this->call_node[$_GET["cmdNode"]]["class"]."'.");
484  }
485  else
486  {
487  if (is_object($ilLog))
488  {
489  if (is_object($ilUser))
490  {
491  $user_str = "User: ".$ilUser->getLogin()." (".$ilUser->getId()."), ";
492  }
493  $ilLog->write("Invalid Request (class ilCtrl). Possible attack or Control Structure broken (see Setup). ".
494  $user_str."IP: ".$_SERVER["REMOTE_ADDR"].", URI: ".$_SERVER["REQUEST_URI"]);
495  }
496  ilUtil::sendFailure("Sorry, but the request includes invalid parameters." ,true);
497  ilUtil::redirect("repository.php?cmd=frameset");
498  }
499  }
500  }
501  }
502 
508  {
509  global $ilDB;
510 
511  $ilDB->manipulate("DELETE FROM ctrl_structure");
512 
513  foreach ($this->stored_trees as $root_gui_class)
514  {
515  $this->call_node = array();
516  $this->forward = array();
517  $this->parent = array();
518  $this->readCallStructure($root_gui_class);
519 /* $ilDB->manipulate(sprintf("INSERT INTO ctrl_structure ".
520  "(root_class, call_node, forward, parent) VALUES (%s,%s,%s,%s)",
521  $ilDB->quote($root_gui_class, "text"),
522  $ilDB->quote(serialize($this->call_node), "clob"),
523  $ilDB->quote(serialize($this->forward), "clob"),
524  $ilDB->quote(serialize($this->parent), "clob")));*/
525  $ilDB->insert("ctrl_structure", array(
526  "root_class" => array("text", $root_gui_class),
527  "call_node" => array("text", serialize($this->call_node)),
528  "forward" => array("text", serialize($this->forward)),
529  "parent" => array("clob", serialize($this->parent))));
530  }
531  }
532 
536  function readCallStructure($a_class, $a_nr = 0, $a_parent = 0)
537  {
538  global $ilDB;
539 
540  $a_class = strtolower($a_class);
541 
542  $a_nr++;
543 
544  // determine call node structure
545  $this->call_node[$a_nr] = array("class" => $a_class, "parent" => $a_parent);
546 
547 //echo "<br>nr:$a_nr:class:$a_class:parent:$a_parent:";
548  $call_set = $ilDB->query("SELECT * FROM ctrl_calls WHERE parent = ".
549  $ilDB->quote(strtolower($a_class), "text").
550  " ORDER BY child", array("text"));
551  $a_parent = $a_nr;
552  while ($call_rec = $ilDB->fetchAssoc($call_set))
553  {
554  $a_nr = $this->readCallStructure($call_rec["child"], $a_nr, $a_parent);
555  $forw[] = $call_rec["child"];
556  }
557 
558  // determin forward and parent array
559  $this->forwards($a_class, $forw);
560 //echo "<br>forwards:".$a_class."<br>"; var_dump($forw);
561 
562  // determine root class
563  $this->root_class = $a_class;
564  return $a_nr;
565  }
566 
567 
576  function forwards($a_from_class, $a_to_class)
577  {
578  $a_from_class = strtolower($a_from_class);
579 
580  if (is_array($a_to_class))
581  {
582  foreach($a_to_class as $to_class)
583  {
584  if ($a_from_class != "" && $to_class != "")
585  {
586  if (!is_array($this->forward[$a_from_class]) || !in_array(strtolower($to_class), $this->forward[$a_from_class]))
587  {
588  $this->forward[$a_from_class][] = strtolower($to_class);
589  }
590  if (!is_array($this->parent[strtolower($to_class)]) || !in_array($a_from_class, $this->parent[strtolower($to_class)]))
591  {
592  $this->parent[strtolower($to_class)][] = $a_from_class;
593  }
594  }
595  }
596  }
597  else
598  {
599  $to_class = $a_to_class;
600  if ($a_from_class != "" && $to_class != "")
601  {
602  if (!is_array($this->forward[$a_from_class]) || !in_array(strtolower($to_class), $this->forward[$a_from_class]))
603  {
604  $this->forward[$a_from_class][] = strtolower($to_class);
605  }
606  if (!is_array($this->parent[strtolower($to_class)]) || !in_array($a_from_class, $this->parent[strtolower($to_class)]))
607  {
608  $this->parent[strtolower($to_class)][] = $a_from_class;
609  }
610  }
611  }
612  }
613 
614 
634  function saveParameter(&$a_obj, $a_parameter)
635  {
636  if (is_object($a_obj))
637  {
638  $this->saveParameterByClass(get_class($a_obj), $a_parameter);
639  }
640  }
641 
642  function saveParameterByClass($a_class, $a_parameter)
643  {
644  if (is_array($a_parameter))
645  {
646  foreach($a_parameter as $parameter)
647  {
648  $this->save_parameter[strtolower($a_class)][] = $parameter;
649  }
650  }
651  else
652  {
653  $this->save_parameter[strtolower($a_class)][] = $a_parameter;
654  }
655  }
656 
657 
682  function setParameter(&$a_obj, $a_parameter, $a_value)
683  {
684  $this->parameter[strtolower(get_class($a_obj))][$a_parameter] = $a_value;
685  }
686 
687 
697  function setParameterByClass($a_class, $a_parameter, $a_value)
698  {
699  $this->parameter[strtolower($a_class)][$a_parameter] = $a_value;
700  }
701 
702 
709  function clearParameters(&$a_obj)
710  {
711  $this->clearParametersByClass(strtolower(get_class($a_obj)));
712  }
713 
720  function clearParametersByClass($a_class)
721  {
722  $this->parameter[strtolower($a_class)] = array();
723  }
724 
733  function getNextClass()
734  {
735 //echo "getNextClass:";
736  $cmdNode = $this->getCmdNode();
737  if ($cmdNode == "")
738  {
739  return false;
740  }
741  else
742  {
743  if ($this->current_node == $cmdNode)
744  {
745 //echo "1:".$this->call_node[$cmdNode]["class"]."<br>";
746  //return $this->call_node[$cmdNode]["class"];
747  return "";
748  }
749  else
750  {
751  $path = $this->getPathNew($this->current_node, $cmdNode);
752 
753 //echo "2:".$this->call_node[$path[1]]["class"]."<br>";
754  return $this->call_node[$path[1]]["class"];
755  }
756  }
757  }
758 
765  function lookupClassPath($a_class_name)
766  {
767  global $ilDB;
768  $a_class_name = strtolower($a_class_name);
769 
770  $class_set = $ilDB->query("SELECT * FROM ctrl_classfile WHERE class = ".
771  $ilDB->quote($a_class_name, "text"));
772  $class_rec = $ilDB->fetchAssoc($class_set);
773 
774  if ($class_rec["plugin_path"] != "")
775  {
776  return $class_rec["plugin_path"]."/".$class_rec["filename"];
777  }
778  else
779  {
780  return $class_rec["filename"];
781  }
782  }
783 
792  function getClassForClasspath($a_class_path)
793  {
794  $path = pathinfo($a_class_path);
795  $file = $path["basename"];
796  $class = substr($file, 6, strlen($file) - 10);
797 
798  return $class;
799  }
800 
809  function getPathNew($a_source_node, $a_target_node)
810  {
811 //if ($this->getCmdClass() == "ilmailfoldergui") echo "-".$a_source_node."-".$a_target_node."-";
812  $path_rev = array();
813  $c_target = $a_target_node;
814  while ($a_source_node != $c_target)
815  {
816  $path_rev[] = $c_target;
817  $c_target = $this->call_node[$c_target]["parent"];
818  if(!($c_target > 0))
819  {
820  echo "ERROR: Path not found. Source:".$a_source_node.
821  " (".$this->call_node[$a_source_node]["class"].")".
822  ", Target:".$a_target_node.
823  " (".$this->call_node[$a_target_node]["class"].")";
824  exit;
825  }
826  }
827  if ($a_source_node == $c_target)
828  {
829  $path_rev[] = $c_target;
830  }
831  $path = array();
832  for ($i=0; $i<count($path_rev); $i++)
833  {
834  $path[] = $path_rev[count($path_rev) - ($i + 1)];
835  }
836 
837 //if ($this->getCmdClass() == "ilmailfoldergui") var_dump($path);
838  return $path;
839  }
840 
841 
847  function setTargetScript($a_target_script)
848  {
849  $this->target_script = $a_target_script;
850  }
851 
852 
858  function getTargetScript()
859  {
860  return $this->target_script;
861  }
862 
863 
870  function initBaseClass($a_base_class)
871  {
872  $_GET["baseClass"] = $a_base_class;
873  $_GET["cmd"] = "";
874  $_GET["cmdClass"] = "";
875  $_GET["cmdNode"] = "";
876  $this->init();
877  }
878 
886  function getCmd($a_default_cmd = "", $a_safe_commands = "")
887  {
888  $cmd = $_GET["cmd"];
889  if($cmd == "post")
890  {
891  if (is_array($_POST["cmd"]))
892  {
893  reset($_POST["cmd"]);
894  }
895  $cmd = @key($_POST["cmd"]);
896 
897  // verify command
898  if ($this->verified_cmd != "")
899  {
900  return $this->verified_cmd;
901  }
902  else
903  {
904  if (!$this->verifyToken() &&
905  (!is_array($a_safe_commands) || !in_array($cmd, $a_safe_commands)))
906  {
907  return $a_default_cmd;
908  }
909  }
910 
911  $this->verified_cmd = $cmd;
912  if($cmd == "" && isset($_POST["select_cmd2"])) // selected command in multi-list (table2)
913  {
914  $cmd = $_POST["selected_cmd2"];
915  $this->verified_cmd = $cmd;
916  }
917  if($cmd == "" && isset($_POST["select_cmd"])) // selected command in multi-list (table2)
918  {
919  $cmd = $_POST["selected_cmd"];
920  $this->verified_cmd = $cmd;
921  }
922  if($cmd == "")
923  {
924  $cmd = $_GET["fallbackCmd"];
925  $this->verified_cmd = $cmd;
926  }
927  }
928  if($cmd == "")
929  {
930  $cmd = $a_default_cmd;
931  }
932  return $cmd;
933  }
934 
945  function setCmd($a_cmd)
946  {
947  $_GET["cmd"] = $a_cmd;
948  }
949 
960  function setCmdClass($a_cmd_class)
961  {
962  $a_cmd_class = strtolower($a_cmd_class);
963  $nr = $this->getNodeIdForTargetClass($this->current_node, $a_cmd_class);
964  $_GET["cmdClass"] = $a_cmd_class;
965  $_GET["cmdNode"] = $nr;
966  }
967 
971  function getCmdClass()
972  {
973  return strtolower($_GET["cmdClass"]);
974  }
975 
984  function getFormAction(&$a_gui_obj, $a_fallback_cmd = "", $a_anchor = "", $a_asynch = false)
985  {
986  $script = $this->getFormActionByClass(strtolower(get_class($a_gui_obj)),
987  $a_fallback_cmd, $a_anchor, $a_asynch);
988  return $script;
989  }
990 
996  function getFormActionByClass($a_class, $a_fallback_cmd = "", $a_anchor = "", $a_asynch = false)
997  {
998  $a_class = strtolower($a_class);
999 
1000  $tok = $this->getRequestToken();
1001 //echo "-$tok-";
1002 
1003  $script = $this->getLinkTargetByClass($a_class, "post", "", $a_asynch);
1004  if ($a_fallback_cmd != "")
1005  {
1006  $script = ilUtil::appendUrlParameterString($script, "fallbackCmd=".$a_fallback_cmd);
1007  }
1008  $script = ilUtil::appendUrlParameterString($script, self::IL_RTOKEN_NAME.'='.$this->getRequestToken());
1009  if ($a_anchor != "")
1010  {
1011  $script = $script."#".$a_anchor;
1012  }
1013 
1014  return $script;
1015  }
1016 
1023  public function appendRequestTokenParameterString($a_url)
1024  {
1025  return ilUtil::appendUrlParameterString($a_url, self::IL_RTOKEN_NAME.'='.$this->getRequestToken());
1026  }
1027 
1031  function getRequestToken()
1032  {
1033  global $ilDB, $ilUser;
1034 
1035  if ($this->rtoken != "")
1036  {
1037  return $this->rtoken;
1038  }
1039  else
1040  {
1041  if (is_object($ilDB) && is_object($ilUser) && $ilUser->getId() > 0 &&
1042  $ilUser->getId() != ANONYMOUS_USER_ID)
1043  {
1044  $res = $ilDB->query("SELECT token FROM il_request_token WHERE user_id = ".
1045  $ilDB->quote($ilUser->getId(), "integer").
1046  " AND session_id = ".$ilDB->quote(session_id(), "text"));
1047  $rec = $ilDB->fetchAssoc($res);
1048 
1049  if ($rec["token"] != "")
1050  {
1051  return $rec["token"];
1052  }
1053 
1054  $this->rtoken = md5(uniqid(rand(), true));
1055 
1056  // IMPORTANT: Please do NOT try to move this implementation to a
1057  // session basis. This will fail due to framesets that are used
1058  // occasionally in ILIAS, e.g. in the chat, where multiple
1059  // forms are loaded in different frames.
1060  $ilDB->manipulate("INSERT INTO il_request_token (user_id, token, stamp, session_id) VALUES ".
1061  "(".
1062  $ilDB->quote($ilUser->getId(), "integer").",".
1063  $ilDB->quote($this->rtoken, "text").",".
1064  $ilDB->now().",".
1065  $ilDB->quote(session_id(), "text").")");
1066  return $this->rtoken;
1067  }
1068  //$this->rtoken = md5(uniqid(rand(), true));
1069  }
1070  return "";
1071  }
1072 
1076  function verifyToken()
1077  {
1078  global $ilDB, $ilUser;
1079 
1080  if (is_object($ilUser) && is_object($ilDB) && $ilUser->getId() > 0 &&
1081  $ilUser->getId() != ANONYMOUS_USER_ID)
1082  {
1083  if ($_GET["rtoken"] == "")
1084  {
1085  echo "ilCtrl::No Request Token Given!"; // for debugging, maybe changed later
1086  return false;
1087  }
1088 
1089  $set = $ilDB->query("SELECT * FROM il_request_token WHERE ".
1090  " user_id = ".$ilDB->quote($ilUser->getId(), "integer")." AND ".
1091  " token = ".$ilDB->quote($_GET[self::IL_RTOKEN_NAME]), "text");
1092  if ($ilDB->numRows($set) > 0)
1093  {
1094  // remove used token
1095  /*
1096  $ilDB->query("DELETE FROM il_request_token WHERE ".
1097  " user_id = ".$ilDB->quote($ilUser->getId())." AND ".
1098  " token = ".$ilDB->quote($_GET[self::IL_RTOKEN_NAME]));
1099  */
1100 
1101  // remove tokens from older sessions
1102  $ilDB->manipulate("DELETE FROM il_request_token WHERE ".
1103  " user_id = ".$ilDB->quote($ilUser->getId(), "integer")." AND ".
1104  " session_id != ".$ilDB->quote(session_id(), "text"));
1105  return true;
1106  }
1107  else
1108  {
1109  return false;
1110  }
1111 
1112  if ($_SESSION["rtokens"][$_GET[self::IL_RTOKEN_NAME]] != "")
1113  {
1114  // remove used token
1115  unset($_SESSION["rtokens"][$_GET[self::IL_RTOKEN_NAME]]);
1116 
1117  // remove old tokens
1118  if (count($_SESSION["rtokens"]) > 100)
1119  {
1120  $to_remove = array();
1121  $sec = 7200; // two hours
1122 
1123  foreach($_SESSION["rtokens"] as $tok => $time)
1124  {
1125  if (time() - $time > $sec)
1126  {
1127  $to_remove[] = $tok;
1128  }
1129  }
1130  foreach($to_remove as $tok)
1131  {
1132  unset($_SESSION["rtokens"][$tok]);
1133  }
1134  }
1135 
1136  return true;
1137  }
1138  return false;
1139  }
1140  else
1141  {
1142  return true; // do not verify, if user or db object is missing
1143  }
1144 
1145  return false;
1146  }
1147 
1148  function redirect(&$a_gui_obj, $a_cmd = "", $a_anchor = "")
1149  {
1150  global $ilBench;
1151 
1152 //echo "<br>class:".get_class($a_gui_obj).":";
1153  $script = $this->getLinkTargetByClass(strtolower(get_class($a_gui_obj)), $a_cmd);
1154 //echo "<br>$script";
1155  if (is_object($ilBench))
1156  {
1157  $ilBench->save();
1158  }
1159  if ($a_anchor != "")
1160  {
1161  $script = $script."#".$a_anchor;
1162  }
1163  ilUtil::redirect($script);
1164  }
1165 
1166 
1173  function redirectByClass($a_class, $a_cmd = "")
1174  {
1175  // $a_class may be an array
1176  //$a_class = strtolower($a_class);
1177 
1178 //echo "<br>class:".get_class($a_gui_obj).":";
1179  $script = $this->getLinkTargetByClass($a_class, $a_cmd);
1180 //echo "<br>script:$script:";
1181  ilUtil::redirect($script);
1182  }
1183 
1184  function isAsynch()
1185  {
1186  if ($_GET["cmdMode"] == "asynch")
1187  {
1188  return true;
1189  }
1190  else
1191  {
1192  return false;
1193  }
1194  }
1195 
1196 
1205  function getLinkTarget(&$a_gui_obj, $a_cmd = "", $a_anchor = "", $a_asynch = false)
1206  {
1207 //echo "<br>getLinkTarget";
1208  $script = $this->getLinkTargetByClass(strtolower(get_class($a_gui_obj)), $a_cmd, $a_anchor, $a_asynch);
1209  return $script;
1210  }
1211 
1212 
1223  function getLinkTargetByClass($a_class, $a_cmd = "", $a_anchor = "", $a_asynch = false)
1224  {
1225  // note: $a_class may be an array
1226  //$a_class = strtolower($a_class);
1227 
1228 //echo "<br>getLinkTargetByClass";
1229  $script = $this->getTargetScript();
1230  $script = $this->getUrlParameters($a_class, $script, $a_cmd);
1231 
1232  if ($a_asynch)
1233  {
1234  $script.= "&cmdMode=asynch";
1235  }
1236 
1237  if ($a_anchor != "")
1238  {
1239  $script = $script."#".$a_anchor;
1240  }
1241 
1242  return $script;
1243  }
1244 
1248  function setReturn(&$a_gui_obj, $a_cmd)
1249  {
1250  $script = $this->getTargetScript();
1251  $script = $this->getUrlParameters(strtolower(get_class($a_gui_obj)), $script, $a_cmd);
1252 //echo "<br>setReturn:".get_class($a_gui_obj).":".$script.":<br>";
1253  $this->return[strtolower(get_class($a_gui_obj))] = $script;
1254  }
1255 
1259  function setReturnByClass($a_class, $a_cmd)
1260  {
1261  // may not be an array!
1262  $a_class = strtolower($a_class);
1263 
1264  $script = $this->getTargetScript();
1265  $script = $this->getUrlParameters($a_class, $script, $a_cmd);
1266 //echo "<br>setReturn:".get_class($a_gui_obj).":".$script.":<br>";
1267  $this->return[strtolower($a_class)] = $script;
1268  }
1269 
1273  function returnToParent(&$a_gui_obj, $a_anchor = "")
1274  {
1275  $script = $this->getParentReturn($a_gui_obj);
1276 
1277  $script = ilUtil::appendUrlParameterString($script,
1278  "redirectSource=".strtolower(get_class($a_gui_obj)));
1279  $script = ilUtil::appendUrlParameterString($script,
1280  "cmdMode=".$_GET["cmdMode"]);
1281  if ($a_anchor != "")
1282  {
1283  $script = $script."#".$a_anchor;
1284  }
1285 
1286  ilUtil::redirect($script);
1287  }
1288 
1289 
1296  {
1297  return $_GET["redirectSource"];
1298  }
1299 
1303  function getParentReturn(&$a_gui_obj)
1304  {
1305  return $this->getParentReturnByClass(strtolower(get_class($a_gui_obj)));
1306  }
1307 
1308 
1309  function getParentReturnByClass($a_class)
1310  {
1311  $a_class = strtolower($a_class);
1312  $ret_class = $this->searchReturnClass($a_class);
1313 //echo ":$ret_class:";
1314  if($ret_class)
1315  {
1316 //echo ":".$this->return[$ret_class].":";
1317  return $this->return[$ret_class];
1318  }
1319  }
1320 
1324  function searchReturnClass($a_class)
1325  {
1326  $a_class = strtolower($a_class);
1327 
1328  $nr = $this->getNodeIdForTargetClass($this->current_node, $a_class);
1329  $path = $this->getPathNew(1, $nr);
1330 //var_dump($path);
1331  for($i = count($path)-2; $i>=0; $i--)
1332  {
1333 //echo "<br>:$i:".$path[$i].":".$this->call_node[$path[$i]]["class"]
1334 // .":".$this->return[$this->call_node[$path[$i]]["class"]].":";
1335  if ($this->return[$this->call_node[$path[$i]]["class"]] != "")
1336  {
1337  return $this->call_node[$path[$i]]["class"];
1338  }
1339  }
1340 
1341  return false;
1342  }
1343 
1344  function getUrlParameters($a_class, $a_str, $a_cmd = "", $a_transits = "")
1345  {
1346  // note: $a_class may be an array!
1347  //$a_class = strtolower($a_class);
1348 
1349  $params = $this->getParameterArrayByClass($a_class, $a_cmd, $a_transits);
1350 
1351  foreach ($params as $par => $value)
1352  {
1353  if (strlen($value))
1354  {
1355  $a_str = ilUtil::appendUrlParameterString($a_str, $par."=".$value);
1356  }
1357  }
1358 
1359  return $a_str;
1360  }
1361 
1362  function appendTransitClasses($a_str)
1363  {
1364  if (is_array($_GET["cmdTransit"]))
1365  {
1366  reset($_GET["cmdTransit"]);
1367  foreach ($_GET["cmdTransit"] as $transit)
1368  {
1369  $a_str = ilUtil::appendUrlParameterString($a_str, "cmdTransit[]=".$transit);
1370  }
1371  }
1372  return $a_str;
1373  }
1374 
1375  function getTransitArray()
1376  {
1377  $trans_arr = array();
1378  if (is_array($_GET["cmdTransit"]))
1379  {
1380  reset($_GET["cmdTransit"]);
1381  foreach ($_GET["cmdTransit"] as $key => $transit)
1382  {
1383  $trans_arr["cmdTransit[".$key."]"] = $transit;
1384  }
1385  }
1386  return $trans_arr;
1387  }
1388 
1389  function addTransit($a_class)
1390  {
1391  $a_class = strtolower($a_class);
1392  $_GET["cmdTransit"][] = $a_class;
1393  }
1394 
1395  function getParameterArray(&$a_gui_obj, $a_cmd = "", $a_incl_transit = true)
1396  {
1397  $par_arr = $this->getParameterArrayByClass(strtolower(get_class($a_gui_obj)), $a_cmd,
1398  $trans_arr);
1399 
1400  return $par_arr;
1401  }
1402 
1406  function getParameterArrayByClass($a_class, $a_cmd = "", $a_transits = "")
1407  {
1408 //echo "<br>getparameter for $a_class";
1409  if ($a_class == "")
1410  {
1411  return array();
1412  }
1413 
1414  if (!is_array($a_class))
1415  {
1416  $a_class = array($a_class);
1417  }
1418 
1419  $nr = $this->current_node;
1420  foreach ($a_class as $class)
1421  {
1422 //echo "<br>-$class-";
1423  $class = strtolower($class);
1424  $nr = $this->getNodeIdForTargetClass($nr, $class);
1425  $target_class = $class;
1426 //echo "-$nr-";
1427  }
1428 
1429  $path = $this->getPathNew(1, $nr);
1430 //echo "$nr";
1431 //var_dump($path);
1432  $params = array();
1433 
1434  // append parameters of parent classes
1435  foreach($path as $node_id)
1436  {
1437  $class = $this->call_node[$node_id]["class"];
1438  if (is_array($this->save_parameter[$class]))
1439  {
1440  foreach($this->save_parameter[$class] as $par)
1441  {
1442  $params[$par] = $_GET[$par];
1443  }
1444  }
1445 
1446  if (is_array($this->parameter[$class]))
1447  {
1448  foreach($this->parameter[$class] as $par => $value)
1449  {
1450  $params[$par] = $value;
1451  }
1452  }
1453  }
1454 
1455  if ($a_cmd != "")
1456  {
1457  $params["cmd"] = $a_cmd;
1458  }
1459 
1460  $params["cmdClass"] = $target_class;
1461  $params["cmdNode"] = $nr;
1462  $params["baseClass"] = $_GET["baseClass"];
1463 
1464  return $params;
1465  }
1466 
1467 
1468 } // END class.ilCtrl
1469 ?>