1<?php
2/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4/**
5 * Workspace deep link handler GUI
6 *
7 * @author Jörg Lützenkirchen <luetzenkirchen@leifos.com>
8 * @version $Id$
9 *
10 * @ilCtrl_Calls ilSharedResourceGUI: ilObjBlogGUI, ilObjFileGUI, ilObjTestVerificationGUI
11 * @ilCtrl_Calls ilSharedResourceGUI: ilObjExerciseVerificationGUI, ilObjLinkResourceGUI
12 * @ilCtrl_Calls ilSharedResourceGUI: ilObjPortfolioGUI
13 *
14 * @ingroup ServicesPersonalWorkspace
15 */
16class ilSharedResourceGUI
17{
18    /**
19     * @var ilCtrl
20     */
21    protected $ctrl;
22
23    /**
24     * @var ilTemplate
25     */
26    protected $tpl;
27
28    /**
29     * @var ilMainMenuGUI
30     */
31    protected $main_menu;
32
33    /**
34     * @var ilLocatorGUI
35     */
36    protected $locator;
37
38    /**
39     * @var ilObjUser
40     */
41    protected $user;
42
43    /**
44     * @var ilLanguage
45     */
46    protected $lng;
47
48    /**
49     * @var ilObjectDefinition
50     */
51    protected $obj_definition;
52
53    /**
54     * @var ilTabsGUI
55     */
56    protected $tabs;
57
58    /**
59     * @var ilDB
60     */
61    protected $db;
62
63    protected $node_id;
64    protected $portfolio_id;
65    protected $access_handler;
66
67    public function __construct()
68    {
69        global $DIC;
70
71        $this->ctrl = $DIC->ctrl();
72        $this->tpl = $DIC["tpl"];
73        $this->main_menu = $DIC["ilMainMenu"];
74        $this->locator = $DIC["ilLocator"];
75        $this->user = $DIC->user();
76        $this->lng = $DIC->language();
77        $this->obj_definition = $DIC["objDefinition"];
78        $this->tabs = $DIC->tabs();
79        $this->db = $DIC->database();
80        $ilCtrl = $DIC->ctrl();
81
82        $ilCtrl->saveParameter($this, "wsp_id");
83        $ilCtrl->saveParameter($this, "prt_id");
84        $this->node_id = (int) $_GET["wsp_id"];
85        $this->portfolio_id = (int) $_GET["prt_id"];
86    }
87
88    public function executeCommand()
89    {
90        $ilCtrl = $this->ctrl;
91        $tpl = $this->tpl;
92        $ilMainMenu = $this->main_menu;
93        $ilLocator = $this->locator;
94        $ilUser = $this->user;
95        $lng = $this->lng;
96
97        $next_class = $ilCtrl->getNextClass($this);
98        $cmd = $ilCtrl->getCmd();
99
100        $tpl->loadStandardTemplate();
101
102        // #8509
103        $ilMainMenu->setActive("desktop");
104
105        // #12096
106        if ($ilUser->getId() != ANONYMOUS_USER_ID &&
107            $next_class &&
108            !in_array($next_class, array("ilobjbloggui", "ilobjportfoliogui"))) {
109            include_once "Services/PersonalWorkspace/classes/class.ilWorkspaceAccessHandler.php";
110            include_once "Services/PersonalWorkspace/classes/class.ilWorkspaceTree.php";
111            $tree = new ilWorkspaceTree($ilUser->getId());
112            $access_handler = new ilWorkspaceAccessHandler($tree);
113            $owner_id = $tree->lookupOwner($this->node_id);
114            $obj_id = $tree->lookupObjectId($this->node_id);
115
116            $lng->loadLanguageModule("wsp");
117
118            // see ilPersonalWorkspaceGUI
119            if ($owner_id != $ilUser->getId()) {
120                $ilCtrl->setParameterByClass("ildashboardgui", "dsh", $owner_id);
121                $link = $ilCtrl->getLinkTargetByClass("ildashboardgui", "jumptoworkspace");
122                $ilLocator->addItem($lng->txt("wsp_tab_shared"), $link);
123
124                include_once "Services/User/classes/class.ilUserUtil.php";
125                $ilLocator->addItem(ilUserUtil::getNamePresentation($owner_id), $link);
126            } else {
127                $link = $ilCtrl->getLinkTargetByClass("ildashboardgui", "jumptoworkspace");
128                $ilLocator->addItem($lng->txt("wsp_tab_personal"), $link);
129            }
130
131            $link = $access_handler->getGotoLink($this->node_id, $obj_id);
132            $ilLocator->addItem(ilObject::_lookupTitle($obj_id), $link);
133            $tpl->setLocator($ilLocator);
134        }
135
136        switch ($next_class) {
137            case "ilobjbloggui":
138                $bgui = new ilObjBlogGUI($this->node_id, ilObject2GUI::WORKSPACE_NODE_ID);
139                $ilCtrl->forwardCommand($bgui);
140                break;
141
142            case "ilobjfilegui":
143                $fgui = new ilObjFileGUI($this->node_id, ilObject2GUI::WORKSPACE_NODE_ID);
144                $ilCtrl->forwardCommand($fgui);
145                break;
146
147            case "ilobjtestverificationgui":
148                $tgui = new ilObjTestVerificationGUI($this->node_id, ilObject2GUI::WORKSPACE_NODE_ID);
149                $ilCtrl->forwardCommand($tgui);
150                break;
151
152            case "ilobjexerciseverificationgui":
153                $egui = new ilObjExerciseVerificationGUI($this->node_id, ilObject2GUI::WORKSPACE_NODE_ID);
154                $ilCtrl->forwardCommand($egui);
155                break;
156
157            case "ilobjlinkresourcegui":
158                include_once "Modules/WebResource/classes/class.ilObjLinkResourceGUI.php";
159                $lgui = new ilObjLinkResourceGUI($this->node_id, ilObject2GUI::WORKSPACE_NODE_ID);
160                $ilCtrl->forwardCommand($lgui);
161                break;
162
163            case "ilobjportfoliogui":
164                include_once "Modules/Portfolio/classes/class.ilObjPortfolioGUI.php";
165                $pgui = new ilObjPortfolioGUI($this->portfolio_id, ilObject2GUI::PORTFOLIO_OBJECT_ID);
166                $ilCtrl->forwardCommand($pgui);
167                break;
168
169            default:
170                if (!$cmd) {
171                    $cmd = "process";
172                }
173                $this->$cmd();
174        }
175
176        $tpl->printToStdout();
177    }
178
179    protected function process()
180    {
181        if (!$this->node_id && !$this->portfolio_id) {
182            exit("invalid call");
183        }
184
185        // if already logged in, we need to re-check for public password
186        if ($this->node_id) {
187            if (!self::hasAccess($this->node_id)) {
188                exit("no permission");
189            }
190            $this->redirectToResource($this->node_id);
191        } else {
192            if (!self::hasAccess($this->portfolio_id, true)) {
193                exit("no permission");
194            }
195            $this->redirectToResource($this->portfolio_id, true);
196        }
197    }
198
199    public static function hasAccess($a_node_id, $a_is_portfolio = false)
200    {
201        global $DIC;
202
203        $ilUser = $DIC->user();
204        $ilSetting = $DIC->settings();
205
206        // if we have current user - check with normal access handler
207        if ($ilUser->getId() != ANONYMOUS_USER_ID) {
208            if (!$a_is_portfolio) {
209                include_once "Services/PersonalWorkspace/classes/class.ilWorkspaceAccessHandler.php";
210                include_once "Services/PersonalWorkspace/classes/class.ilWorkspaceTree.php";
211                $tree = new ilWorkspaceTree($ilUser->getId());
212                $access_handler = new ilWorkspaceAccessHandler($tree);
213            } else {
214                include_once "Modules/Portfolio/classes/class.ilPortfolioAccessHandler.php";
215                $access_handler = new ilPortfolioAccessHandler();
216            }
217            if ($access_handler->checkAccess("read", "", $a_node_id)) {
218                return true;
219            }
220        }
221
222        // not logged in yet or no read access
223        include_once "Services/PersonalWorkspace/classes/class.ilWorkspaceAccessGUI.php";
224
225        if (!$a_is_portfolio) {
226            include_once "Services/PersonalWorkspace/classes/class.ilWorkspaceAccessHandler.php";
227            $shared = ilWorkspaceAccessHandler::_getPermissions($a_node_id);
228        } else {
229            // #12059
230            if (!$ilSetting->get('user_portfolios')) {
231                return false;
232            }
233
234            // #12039
235            include_once "Modules/Portfolio/classes/class.ilObjPortfolio.php";
236            $prtf = new ilObjPortfolio($a_node_id, false);
237            if (!$prtf->isOnline()) {
238                return false;
239            }
240
241            include_once "Modules/Portfolio/classes/class.ilPortfolioAccessHandler.php";
242            $shared = ilPortfolioAccessHandler::_getPermissions($a_node_id);
243        }
244
245        // object is "public"
246        if (in_array(ilWorkspaceAccessGUI::PERMISSION_ALL, $shared)) {
247            return true;
248        }
249
250        // password protected
251        if (in_array(ilWorkspaceAccessGUI::PERMISSION_ALL_PASSWORD, $shared)) {
252            if (!$a_is_portfolio) {
253                ilUtil::redirect("ilias.php?baseClass=ilSharedResourceGUI&cmd=passwordForm&wsp_id=" . $a_node_id);
254            } else {
255                ilUtil::redirect("ilias.php?baseClass=ilSharedResourceGUI&cmd=passwordForm&prt_id=" . $a_node_id);
256            }
257        }
258
259        return false;
260    }
261
262    protected function redirectToResource($a_node_id, $a_is_portfolio = false)
263    {
264        $ilCtrl = $this->ctrl;
265        $objDefinition = $this->obj_definition;
266
267        if (!$a_is_portfolio) {
268            include_once "Services/PersonalWorkspace/classes/class.ilWorkspaceAccessHandler.php";
269            $object_data = ilWorkspaceAccessHandler::getObjectDataFromNode($a_node_id);
270            if (!$object_data["obj_id"]) {
271                exit("invalid object");
272            }
273        } else {
274            if (!ilObject::_lookupType($a_node_id, false)) {
275                exit("invalid object");
276            }
277            $object_data["obj_id"] = $a_node_id;
278            $object_data["type"] = "prtf";
279        }
280
281        $class = $objDefinition->getClassName($object_data["type"]);
282        $gui = "ilobj" . $class . "gui";
283
284        switch ($object_data["type"]) {
285            case "blog":
286                $ilCtrl->setParameterByClass($gui, "wsp_id", $a_node_id);
287                $ilCtrl->setParameterByClass($gui, "gtp", (int) $_GET["gtp"]);
288                $ilCtrl->setParameterByClass($gui, "edt", $_GET["edt"]);
289                $ilCtrl->redirectByClass($gui, "preview");
290
291                // no break
292            case "tstv":
293            case "excv":
294            case "crsv":
295            case "scov":
296            case "cmxv":
297            case "ltiv":
298                $ilCtrl->setParameterByClass($gui, "wsp_id", $a_node_id);
299                $ilCtrl->redirectByClass($gui, "deliver");
300
301                // no break
302            case "file":
303            case "webr":
304                $ilCtrl->setParameterByClass($gui, "wsp_id", $a_node_id);
305                $ilCtrl->redirectByClass($gui);
306
307                // no break
308            case "prtf":
309                $ilCtrl->setParameterByClass($gui, "prt_id", $a_node_id);
310                $ilCtrl->setParameterByClass($gui, "gtp", (int) $_GET["gtp"]);
311                if ($_GET["back_url"]) {
312                    $ilCtrl->setParameterByClass($gui, "back_url", rawurlencode($_GET["back_url"]));
313                }
314                $ilCtrl->redirectByClass($gui, "preview");
315
316                // no break
317            default:
318                exit("invalid object type");
319        }
320    }
321
322    protected function passwordForm($form = null)
323    {
324        $tpl = $this->tpl;
325        $lng = $this->lng;
326
327        $lng->loadLanguageModule("wsp");
328
329        $tpl->setTitle($lng->txt("wsp_password_protected_resource"));
330        $tpl->setDescription($lng->txt("wsp_password_protected_resource_info"));
331
332        if (!$form) {
333            $form = $this->initPasswordForm();
334        }
335
336        $tpl->setContent($form->getHTML());
337    }
338
339    protected function initPasswordForm()
340    {
341        $ilCtrl = $this->ctrl;
342        $lng = $this->lng;
343        $ilUser = $this->user;
344        $ilTabs = $this->tabs;
345
346        if ($this->node_id) {
347            include_once "Services/PersonalWorkspace/classes/class.ilWorkspaceAccessHandler.php";
348            $object_data = ilWorkspaceAccessHandler::getObjectDataFromNode($this->node_id);
349        } else {
350            $object_data["title"] = ilObject::_lookupTitle($this->portfolio_id);
351        }
352
353        include_once "Services/Form/classes/class.ilPropertyFormGUI.php";
354        $form = new ilPropertyFormGUI();
355        $form->setFormAction($ilCtrl->getFormAction($this, "checkPassword"));
356        $form->setTitle($lng->txt("wsp_password_for") . ": " . $object_data["title"]);
357
358        $password = new ilPasswordInputGUI($lng->txt("password"), "password");
359        $password->setRetype(false);
360        $password->setRequired(true);
361        $password->setSkipSyntaxCheck(true); // #17757
362        $form->addItem($password);
363
364        $form->addCommandButton("checkPassword", $lng->txt("submit"));
365
366        if ($ilUser->getId() && $ilUser->getId() != ANONYMOUS_USER_ID) {
367            $ilTabs->setBackTarget($lng->txt("back"), $ilCtrl->getLinkTarget($this, "cancelPassword"));
368            $form->addCommandButton("cancelPassword", $lng->txt("cancel"));
369        }
370
371        return $form;
372    }
373
374    protected function cancelPassword()
375    {
376        $ilUser = $this->user;
377
378        if ($ilUser->getId() && $ilUser->getId() != ANONYMOUS_USER_ID) {
379            if ($this->node_id) {
380                include_once "Services/PersonalWorkspace/classes/class.ilWorkspaceAccessHandler.php";
381                include_once "Services/PersonalWorkspace/classes/class.ilWorkspaceTree.php";
382                $tree = new ilWorkspaceTree($ilUser->getId());
383                $owner = $tree->lookupOwner($this->node_id);
384                ilUtil::redirect("ilias.php?baseClass=ilDashboardGUI&cmd=jumpToWorkspace&dsh=" . $owner);
385            } else {
386                include_once "Modules/Portfolio/classes/class.ilObjPortfolio.php";
387                $prtf = new ilObjPortfolio($this->portfolio_id, false);
388                $owner = $prtf->getOwner();
389                ilUtil::redirect("ilias.php?baseClass=ilDashboardGUI&cmd=jumpToPortfolio&dsh=" . $owner);
390            }
391        }
392    }
393
394    protected function checkPassword()
395    {
396        $ilDB = $this->db;
397        $lng = $this->lng;
398
399        $lng->loadLanguageModule("wsp");
400
401        $form = $this->initPasswordForm();
402        if ($form->checkInput()) {
403            $input = md5($form->getInput("password"));
404            if ($this->node_id) {
405                include_once "Services/PersonalWorkspace/classes/class.ilWorkspaceAccessHandler.php";
406                $password = ilWorkspaceAccessHandler::getSharedNodePassword($this->node_id);
407            } else {
408                include_once "Modules/Portfolio/classes/class.ilPortfolioAccessHandler.php";
409                $password = ilPortfolioAccessHandler::getSharedNodePassword($this->portfolio_id);
410            }
411            if ($input == $password) {
412                if ($this->node_id) {
413                    ilWorkspaceAccessHandler::keepSharedSessionPassword($this->node_id, $input);
414                    $this->redirectToResource($this->node_id);
415                } else {
416                    ilPortfolioAccessHandler::keepSharedSessionPassword($this->portfolio_id, $input);
417                    $this->redirectToResource($this->portfolio_id, true);
418                }
419            } else {
420                $item = $form->getItemByPostVar("password");
421                $item->setAlert($lng->txt("wsp_invalid_password"));
422                ilUtil::sendFailure($lng->txt("form_input_not_valid"));
423            }
424        }
425
426        $form->setValuesByPost();
427        $this->passwordForm($form);
428    }
429}
430