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
24require_once './Services/Registration/classes/class.ilRegistrationSettings.php';
25
26/**
27* Class ilRegistrationSettingsGUI
28*
29* @author Stefan Meyer <smeyer.ilias@gmx.de>
30* @version $Id$
31*
32* @ilCtrl_Calls ilRegistrationSettingsGUI:
33*
34* @ingroup ServicesRegistration
35*/
36class ilRegistrationSettingsGUI
37{
38    const CODE_TYPE_REGISTRATION = 1;
39    const CODE_TYPE_EXTENSION = 2;
40
41    public $ctrl;
42    public $tpl;
43    public $ref_id;
44    public $rbacsystem;
45
46    public function __construct()
47    {
48        global $DIC;
49
50        $this->tpl = $DIC['tpl'];
51        $this->ctrl = $DIC->ctrl();
52        $this->rbacsystem = $DIC->rbac();
53
54        $this->lng = $DIC->language();
55        $this->lng->loadLanguageModule('administration');
56        $this->lng->loadLanguageModule('registration');
57        $this->lng->loadLanguageModule('user');
58
59        $this->ref_id = (int) $_GET['ref_id'];
60
61        $this->registration_settings = new ilRegistrationSettings();
62    }
63
64    public function executeCommand()
65    {
66        $next_class = $this->ctrl->getNextClass($this);
67        $cmd = $this->ctrl->getCmd();
68        switch ($next_class) {
69            default:
70                if (!$cmd) {
71                    $cmd = 'view';
72                }
73                $this->$cmd();
74                break;
75        }
76        return true;
77    }
78
79    /**
80     * @param string $a_permission
81     */
82    protected function checkAccess($a_permission)
83    {
84        global $DIC;
85
86        $ilErr = $DIC['ilErr'];
87
88        if (!$this->checkAccessBool($a_permission)) {
89            $ilErr->raiseError($this->lng->txt('msg_no_perm_read'), $ilErr->WARNING);
90        }
91    }
92
93    /**
94     * @param string $a_permission
95     * @return bool
96     */
97    protected function checkAccessBool($a_permission)
98    {
99        global $DIC;
100
101        $access = $DIC->access();
102
103        return $access->checkAccess($a_permission, '', $this->ref_id);
104    }
105
106    /**
107    * set sub tabs
108    * @param	string	$activeTab
109    */
110    public function setSubTabs($activeTab = 'registration_settings')
111    {
112        global $DIC;
113
114        $ilTabs = $DIC['ilTabs'];
115        $lng = $DIC['lng'];
116
117        $ilTabs->addSubTab(
118            "registration_settings",
119            $lng->txt("registration_tab_settings"),
120            $this->ctrl->getLinkTarget($this, 'view')
121        );
122
123        $ilTabs->addSubTab(
124            "registration_codes",
125            $lng->txt("registration_tab_codes"),
126            $this->ctrl->getLinkTarget($this, 'listCodes')
127        );
128
129        $ilTabs->activateSubTab($activeTab);
130    }
131
132    public function initForm()
133    {
134        $this->form_gui = new ilPropertyFormGUI();
135        $this->form_gui->setFormAction($this->ctrl->getFormAction($this, 'save'));
136        $this->form_gui->setTitle($this->lng->txt('reg_settings_header'));
137
138        $reg_type = new ilRadioGroupInputGUI($this->lng->txt('reg_type'), 'reg_type');
139        $reg_type->addOption(new ilRadioOption($this->lng->txt('reg_disabled'), IL_REG_DISABLED));
140        $option = new ilRadioOption($this->lng->txt('reg_direct'), IL_REG_DIRECT);
141        $option->setInfo($this->lng->txt('reg_direct_info'));
142        $cd = new ilCheckboxInputGUI($this->lng->txt('reg_allow_codes'), 'reg_codes_' . IL_REG_DIRECT);
143        $cd->setInfo($this->lng->txt('reg_allow_codes_info'));
144        $option->addSubItem($cd);
145        $reg_type->addOption($option);
146        $option = new ilRadioOption($this->lng->txt('reg_approve'), IL_REG_APPROVE);
147        $option->setInfo($this->lng->txt('reg_approve_info'));
148        $cd = new ilCheckboxInputGUI($this->lng->txt('reg_allow_codes'), 'reg_codes_' . IL_REG_APPROVE);
149        $cd->setInfo($this->lng->txt('reg_allow_codes_info'));
150        $option->addSubItem($cd);
151        $reg_type->addOption($option);
152        $option = new ilRadioOption($this->lng->txt('reg_type_confirmation'), IL_REG_ACTIVATION);
153        $option->setInfo($this->lng->txt('reg_type_confirmation_info'));
154        $lt = new ilNumberInputGUI($this->lng->txt('reg_confirmation_hash_life_time'), 'reg_hash_life_time');
155        $lt->setSize(6); // #8511
156        $lt->setMaxLength(6);
157        $lt->setMinValue(ilRegistrationSettings::REG_HASH_LIFETIME_MIN_VALUE);
158        $lt->setRequired(true);
159        $lt->setInfo($this->lng->txt('reg_confirmation_hash_life_time_info'));
160        $lt->setSuffix($this->lng->txt('seconds'));
161        $option->addSubItem($lt);
162        $cd = new ilCheckboxInputGUI($this->lng->txt('reg_allow_codes'), 'reg_codes_' . IL_REG_ACTIVATION);
163        $cd->setInfo($this->lng->txt('reg_allow_codes_info'));
164        $option->addSubItem($cd);
165        $reg_type->addOption($option);
166        $option = new ilRadioOption($this->lng->txt('registration_reg_type_codes'), IL_REG_CODES);
167        $option->setInfo($this->lng->txt('registration_reg_type_codes_info'));
168        $reg_type->addOption($option);
169        $this->form_gui->addItem($reg_type);
170
171        $pwd_gen = new ilCheckboxInputGUI($this->lng->txt('passwd_generation'), 'reg_pwd');
172        $pwd_gen->setValue(1);
173        $pwd_gen->setInfo($this->lng->txt('reg_info_pwd'));
174        $this->form_gui->addItem($pwd_gen);
175
176        require_once 'Services/Captcha/classes/class.ilCaptchaUtil.php';
177        $cap = new ilCheckboxInputGUI($this->lng->txt('adm_captcha_anonymous_short'), 'activate_captcha_anonym');
178        $cap->setInfo($this->lng->txt('adm_captcha_anonymous_reg'));
179        $cap->setValue(1);
180        if (!ilCaptchaUtil::checkFreetype()) {
181            $cap->setAlert(ilCaptchaUtil::getPreconditionsMessage());
182        }
183        $this->form_gui->addItem($cap);
184
185        $approver = new ilTextInputGUI($this->lng->txt('reg_notification'), 'reg_approver');
186        $approver->setSize(32);
187        $approver->setMaxLength(50);
188        $approver->setInfo($this->lng->txt('reg_notification_info'));
189        $this->form_gui->addItem($approver);
190
191        $roles = new ilRadioGroupInputGUI($this->lng->txt('reg_role_assignment'), 'reg_role_type');
192        $option = new ilRadioOption($this->lng->txt('reg_fixed'), IL_REG_ROLES_FIXED);
193        $list = new ilCustomInputGUI($this->lng->txt('reg_available_roles'));
194        $edit = $this->ctrl->getLinkTarget($this, 'editRoles');
195        $list->setHtml($this->__parseRoleList($this->__prepareRoleList(), $edit));
196        $option->addSubItem($list);
197        $roles->addOption($option);
198        $option = new ilRadioOption($this->lng->txt('reg_email'), IL_REG_ROLES_EMAIL);
199        $list = new ilCustomInputGUI($this->lng->txt('reg_available_roles'));
200        $edit = $this->ctrl->getLinkTarget($this, 'editEmailAssignments');
201        $list->setHtml($this->__parseRoleList($this->__prepareAutomaticRoleList(), $edit));
202        $option->addSubItem($list);
203        $roles->addOption($option);
204        $roles->setInfo($this->lng->txt('registration_codes_override_global_info'));
205        $this->form_gui->addItem($roles);
206
207        $limit = new ilCheckboxInputGUI($this->lng->txt('reg_access_limitations'), 'reg_access_limitation');
208        $limit->setValue(1);
209        $list = new ilCustomInputGUI($this->lng->txt('reg_available_roles'));
210        $edit = $this->ctrl->getLinkTarget($this, 'editRoleAccessLimitations');
211        $list->setHtml($this->__parseRoleList($this->__prepareAccessLimitationRoleList(), $edit));
212        $list->setInfo($this->lng->txt('registration_codes_override_global_info'));
213        $limit->addSubItem($list);
214        $this->form_gui->addItem($limit);
215
216        $domains = new ilTextInputGUI($this->lng->txt('reg_allowed_domains'), 'reg_allowed_domains');
217        $domains->setInfo($this->lng->txt('reg_allowed_domains_info'));
218        $this->form_gui->addItem($domains);
219
220        if ($this->rbacsystem->system()->checkAccess("write", $this->ref_id)) {
221            $this->form_gui->addCommandButton('save', $this->lng->txt('save'));
222        }
223    }
224
225    public function initFormValues()
226    {
227        if ($this->registration_settings->roleSelectionEnabled()) {
228            $role_type = IL_REG_ROLES_FIXED;
229        } elseif ($this->registration_settings->automaticRoleAssignmentEnabled()) {
230            $role_type = IL_REG_ROLES_EMAIL;
231        }
232
233        require_once 'Services/Captcha/classes/class.ilCaptchaUtil.php';
234        $values = array(
235            'reg_type' => $this->registration_settings->getRegistrationType(),
236            'reg_hash_life_time' => (int) $this->registration_settings->getRegistrationHashLifetime(),
237            'reg_pwd' => $this->registration_settings->passwordGenerationEnabled(),
238            'reg_approver' => $this->registration_settings->getApproveRecipientLogins(),
239            'reg_role_type' => $role_type,
240            'reg_access_limitation' => $this->registration_settings->getAccessLimitation(),
241            'reg_allowed_domains' => implode(';', $this->registration_settings->getAllowedDomains()),
242            'activate_captcha_anonym' => ilCaptchaUtil::isActiveForRegistration()
243            );
244
245        $allow_codes = $this->registration_settings->getAllowCodes();
246        $reg_type = $this->registration_settings->getRegistrationType();
247        if ($allow_codes && in_array($reg_type, array(IL_REG_DIRECT, IL_REG_APPROVE, IL_REG_ACTIVATION))) {
248            $values['reg_codes_' . $reg_type] = true;
249        }
250
251        $this->form_gui->setValuesByArray($values);
252    }
253
254    public function view()
255    {
256        global $DIC;
257        if (!$DIC->rbac()->system()->checkAccess("visible,read", $this->ref_id)) {
258            $DIC['ilErr']->raiseError($this->lng->txt("msg_no_perm_read"), $DIC['ilErr']->MESSAGE);
259        }
260
261        $this->setSubTabs();
262
263        // edit new accout mail
264        $this->ctrl->setParameterByClass("ilobjuserfoldergui", "ref_id", USER_FOLDER_ID);
265        if ($DIC->rbac()->system()->checkAccess("write", $this->ref_id)) {
266            $DIC->toolbar()->addButton($this->lng->txt('registration_user_new_account_mail'), $this->ctrl->getLinkTargetByClass(array(
267                "iladministrationgui",
268                "ilobjuserfoldergui"
269            ), "newAccountMail"));
270            $this->ctrl->setParameterByClass("ilobjuserfoldergui", "ref_id", $_GET["ref_id"]);
271        }
272
273        $this->initForm();
274        $this->initFormValues();
275        $this->tpl->setContent($this->form_gui->getHTML());
276    }
277
278    public function save()
279    {
280        global $DIC;
281
282        $ilAccess = $DIC['ilAccess'];
283        $ilErr = $DIC['ilErr'];
284
285        if (!$ilAccess->checkAccess('write', '', $this->ref_id)) {
286            $ilErr->raiseError($this->lng->txt("msg_no_perm_write"), $ilErr->MESSAGE);
287        }
288
289        $this->registration_settings->setRegistrationType((int) $_POST['reg_type']);
290        $this->registration_settings->setPasswordGenerationStatus((int) $_POST['reg_pwd']);
291        $this->registration_settings->setApproveRecipientLogins(ilUtil::stripSlashes($_POST['reg_approver']));
292        $this->registration_settings->setRoleType((int) $_POST['reg_role_type']);
293        $this->registration_settings->setAccessLimitation((int) $_POST['reg_access_limitation']);
294        $this->registration_settings->setAllowedDomains($_POST['reg_allowed_domains']);
295
296        $allow_codes = false;
297        if (in_array((int) $_POST['reg_type'], array(IL_REG_DIRECT, IL_REG_APPROVE, IL_REG_ACTIVATION))) {
298            $allow_codes = (bool) $_POST['reg_codes_' . (int) $_POST['reg_type']];
299        }
300        $this->registration_settings->setAllowCodes($allow_codes);
301
302        if (!preg_match('/^([0]|([1-9][0-9]*))([\.,][0-9][0-9]*)?$/', (int) $_POST['reg_hash_life_time'])) {
303            $this->registration_settings->setRegistrationHashLifetime(ilRegistrationSettings::REG_HASH_LIFETIME_MIN_VALUE);
304        } else {
305            $this->registration_settings->setRegistrationHashLifetime(max((int) $_POST['reg_hash_life_time'], ilRegistrationSettings::REG_HASH_LIFETIME_MIN_VALUE));
306        }
307
308        if ($error_code = $this->registration_settings->validate()) {
309            switch ($error_code) {
310                case ilRegistrationSettings::ERR_UNKNOWN_RCP:
311
312                    ilUtil::sendFailure($this->lng->txt('reg_unknown_recipients') . ' ' . $this->registration_settings->getUnknown());
313                    $this->view();
314                    return false;
315
316                case ilRegistrationSettings::ERR_MISSING_RCP:
317
318                    ilUtil::sendFailure($this->lng->txt('reg_approve_needs_recipient') . ' ' . $this->registration_settings->getUnknown());
319                    $this->view();
320                    return false;
321
322            }
323        }
324
325        require_once 'Services/Captcha/classes/class.ilCaptchaUtil.php';
326        ilCaptchaUtil::setActiveForRegistration((bool) $_POST['activate_captcha_anonym']);
327
328        $this->registration_settings->save();
329        ilUtil::sendSuccess($this->lng->txt('saved_successfully'));
330        $this->view();
331
332        return true;
333    }
334
335    public function editRoles()
336    {
337        include_once './Services/AccessControl/classes/class.ilObjRole.php';
338
339        global $DIC;
340
341        $ilAccess = $DIC['ilAccess'];
342        $ilErr = $DIC['ilErr'];
343        $ilTabs = $DIC['ilTabs'];
344        $ilCtrl = $DIC['ilCtrl'];
345        $rbacreview = $DIC['rbacreview'];
346
347        if (!$ilAccess->checkAccess('write', '', $this->ref_id)) {
348            $ilErr->raiseError($this->lng->txt("msg_no_perm_write"), $ilErr->MESSAGE);
349        }
350
351        $ilTabs->clearTargets();
352        $ilTabs->setBackTarget(
353            $this->lng->txt("registration_settings"),
354            $ilCtrl->getLinkTarget($this, "view")
355        );
356
357        $role_form = new ilPropertyFormGUI();
358        $role_form->setFormAction($this->ctrl->getFormAction($this, 'save'));
359        $role_form->setTitle($this->lng->txt('reg_selectable_roles'));
360
361        $roles = new \ilCheckboxGroupInputGUI($this->lng->txt('reg_available_roles'), 'roles');
362        $allowed_roles = array();
363        foreach ($rbacreview->getGlobalRoles() as $role) {
364            if ($role == SYSTEM_ROLE_ID or $role == ANONYMOUS_ROLE_ID) {
365                continue;
366            }
367            $role_option = new \ilCheckboxOption(ilObjRole::_lookupTitle($role));
368            $role_option->setValue($role);
369            $roles->addOption($role_option);
370
371            $allowed_roles[$role] = ilObjRole::_lookupAllowRegister($role);
372        }
373
374        $roles->setUseValuesAsKeys(true);
375        $roles->setValue($allowed_roles);
376        $role_form->addItem($roles);
377
378
379        if ($this->rbacsystem->system()->checkAccess("write", $this->ref_id)) {
380            $role_form->addCommandButton("updateRoles", $this->lng->txt("save"));
381        }
382        $role_form->addCommandButton("view", $this->lng->txt("cancel"));
383
384
385        $this->tpl->setContent($role_form->getHTML());
386    }
387
388    public function updateRoles()
389    {
390        global $DIC;
391
392        $ilAccess = $DIC['ilAccess'];
393        $ilErr = $DIC['ilErr'];
394        $rbacreview = $DIC['rbacreview'];
395
396        if (!$ilAccess->checkAccess('write', '', $this->ref_id)) {
397            $ilErr->raiseError($this->lng->txt("msg_no_perm_write"), $ilErr->MESSAGE);
398        }
399        // Minimum one role
400        if (count($_POST['roles']) < 1) {
401            ilUtil::sendFailure($this->lng->txt('msg_last_role_for_registration'));
402            $this->editRoles();
403            return false;
404        }
405        // update allow register
406        foreach ($rbacreview->getGlobalRoles() as $role) {
407            if ($role_obj = ilObjectFactory::getInstanceByObjId($role, false)) {
408                $role_obj->setAllowRegister($_POST['roles'][$role] ? 1 : 0);
409                $role_obj->update();
410            }
411        }
412
413        ilUtil::sendSuccess($this->lng->txt('saved_successfully'));
414        $this->view();
415
416        return true;
417    }
418
419    public function editEmailAssignments(ilPropertyFormGUI $form = null)
420    {
421        global $DIC;
422
423        $ilAccess = $DIC['ilAccess'];
424        $ilErr = $DIC['ilErr'];
425        $ilTabs = $DIC['ilTabs'];
426        $ilCtrl = $DIC['ilCtrl'];
427
428        if (!$ilAccess->checkAccess('write', '', $this->ref_id)) {
429            $ilErr->raiseError($this->lng->txt("msg_no_perm_write"), $ilErr->MESSAGE);
430        }
431
432        $ilTabs->clearTargets();
433        $ilTabs->setBackTarget(
434            $this->lng->txt("registration_settings"),
435            $ilCtrl->getLinkTarget($this, "view")
436        );
437
438        $this->__initRoleAssignments();
439
440
441        $form = (empty($form)) ? $this->initEmailAssignmentForm() : $form;
442        $this->tpl->setContent($form->getHTML());
443    }
444
445    public function initEmailAssignmentForm() : ilPropertyFormGUI
446    {
447        global $DIC;
448
449        $rbacreview = $DIC['rbacreview'];
450
451        $role_assignment_form = new ilPropertyFormGUI();
452        $role_assignment_form->setFormAction($this->ctrl->getFormAction($this));
453        $role_assignment_form->setTitle($this->lng->txt('reg_email_role_assignment'));
454
455        $global_roles = ["" => $this->lng->txt("links_select_one")];
456        foreach ($rbacreview->getGlobalRoles() as $role_id) {
457            if ($role_id == ANONYMOUS_ROLE_ID) {
458                continue;
459            }
460
461            $global_roles[$role_id] = ilObjRole::_lookupTitle($role_id);
462            $role_assignments = new ilCheckboxInputGUI(ilObjRole::_lookupTitle($role_id), "role_assigned_$role_id");
463
464            $domains = $this->assignments_obj->getDomainsByRole($role_id);
465
466            $domain = new ilTextInputGUI($this->lng->txt('reg_domain'), "domain_$role_id");
467            $domain->setMulti(true);
468            $domain->setValidationRegexp("/^@.*\.[a-zA-Z]{1,4}$/");
469            if (!empty($domains)) {
470                $domain->setValue($domains[0]);
471                $domain->setMultiValues($domains);
472                $role_assignments->setChecked(true);
473            }
474
475            $role_assignments->addSubItem($domain);
476            $role_assignment_form->addItem($role_assignments);
477        }
478
479        $default_role = new ilSelectInputGUI($this->lng->txt('reg_default'));
480        $default_role->setPostVar("default_role");
481        $default_role->setOptions($global_roles);
482        $default_role->setValue($this->assignments_obj->getDefaultRole());
483        $default_role->setRequired(true);
484        $role_assignment_form->addItem($default_role);
485
486        $role_assignment_form->addCommandButton("saveAssignment", $this->lng->txt("save"));
487        $role_assignment_form->addCommandButton("view", $this->lng->txt("cancel"));
488
489        return $role_assignment_form;
490    }
491
492    public function editRoleAccessLimitations()
493    {
494        global $DIC;
495
496        $lng = $DIC['lng'];
497        $ilAccess = $DIC['ilAccess'];
498        $ilErr = $DIC['ilErr'];
499        $rbacreview = $DIC['rbacreview'];
500
501        if (!$ilAccess->checkAccess('write', '', $this->ref_id)) {
502            $ilErr->raiseError($this->lng->txt("msg_no_perm_write"), $ilErr->MESSAGE);
503        }
504
505        $this->__initRoleAccessLimitations();
506
507        $this->tpl->addBlockfile('ADM_CONTENT', 'adm_content', 'tpl.reg_role_access_limitations.html', 'Services/Registration');
508
509        $this->tpl->setVariable("FORMACTION", $this->ctrl->getFormAction($this));
510        $this->tpl->setVariable("TXT_REG_ROLE_ACCESS_LIMITATIONS", $lng->txt('reg_role_access_limitations'));
511        $this->tpl->setVariable("TXT_ROLE", $lng->txt('obj_role'));
512        $this->tpl->setVariable("TXT_ACCESS_LIMITATION_MODE", $lng->txt('reg_access_limitation_mode'));
513
514        $this->tpl->setVariable("BTN_SAVE", $lng->txt('save'));
515        $this->tpl->setVariable("BTN_CANCEL", $lng->txt('cancel'));
516
517        $counter = 0;
518        include_once './Services/AccessControl/classes/class.ilObjRole.php';
519
520        foreach (ilObjRole::_lookupRegisterAllowed() as $role) {
521            $this->tpl->setCurrentBlock("roles");
522            $this->tpl->setVariable("CSSROW", ilUtil::switchColor(++$counter, 'tblrow1', 'tblrow1'));
523            $this->tpl->setVariable("ROLE_ID", $role['id']);
524            $this->tpl->setVariable("ROLE_TITLE", $role['title']);
525            $this->tpl->setVariable("SEL_ACCESS_LIMITATION", $this->__buildAccessLimitationSelection($role['id']));
526            $this->tpl->setVariable("CSS_DISPLAY_ABSOLUTE", ($this->access_limitations_obj->getMode($role['id']) == 'absolute') ? 'inline' : 'none');
527            $this->tpl->setVariable("CSS_DISPLAY_RELATIVE", ($this->access_limitations_obj->getMode($role['id']) == 'relative') ? 'inline' : 'none');
528            $this->tpl->setVariable("CSS_DISPLAY_UNLIMITED", ($this->access_limitations_obj->getMode($role['id']) == 'unlimited') ? 'inline' : 'none');
529            $this->tpl->setVariable("TXT_ACCESS_LIMITATION_UNLIMITED", $lng->txt('reg_access_limitation_none'));
530
531            $date = $this->__prepareDateSelect($this->access_limitations_obj->getAbsolute($role['id']));
532            $this->tpl->setVariable("SEL_ACCESS_LIMITATION_ABSOLUTE", ilUtil::makeDateSelect('access_limitation_absolute_' . $role['id'], $date['y'], $date['m'], $date['d'], '2007'));
533
534            $this->tpl->setVariable("TXT_DAYS", $lng->txt('days'));
535            $this->tpl->setVariable("TXT_MONTHS", $lng->txt('months'));
536            $this->tpl->setVariable("TXT_YEARS", $lng->txt('years'));
537
538            $this->tpl->setVariable("DAYS", $this->access_limitations_obj->getRelative($role['id'], 'd'));
539            $this->tpl->setVariable("MONTHS", $this->access_limitations_obj->getRelative($role['id'], 'm'));
540            $this->tpl->setVariable("YEARS", $this->access_limitations_obj->getRelative($role['id'], 'y'));
541            $this->tpl->parseCurrentBlock();
542        }
543    }
544
545    public function saveAssignment()
546    {
547        global $DIC;
548
549        $ilAccess = $DIC['ilAccess'];
550        $ilErr = $DIC['ilErr'];
551        $rbacreview = $DIC['rbacreview'];
552
553        if (!$ilAccess->checkAccess('write', '', $this->ref_id)) {
554            $ilErr->raiseError($this->lng->txt("msg_no_perm_write"), $ilErr->MESSAGE);
555        }
556
557        $this->__initRoleAssignments();
558
559        $form = $this->initEmailAssignmentForm();
560        if (!$form->checkInput()) {
561            $form->setValuesByPost();
562            $this->editEmailAssignments($form);
563            return false;
564        }
565
566        $this->assignments_obj->deleteAll();
567
568        $counter = 0;
569        foreach ($rbacreview->getGlobalRoles() as $role_id) {
570            if ($role_id == ANONYMOUS_ROLE_ID) {
571                continue;
572            }
573
574            $domain_input = $form->getInput("domain_$role_id");
575            $role_assigned_input = $form->getInput("role_assigned_$role_id");
576
577
578            if (!empty($role_assigned_input)) {
579                foreach ($domain_input as $domain) {
580                    if (!empty($domain)) {
581                        $this->assignments_obj->setDomain($counter, ilUtil::stripSlashes($domain));
582                        $this->assignments_obj->setRole($counter, ilUtil::stripSlashes($role_id));
583                        $counter++;
584                    }
585                }
586            }
587        }
588
589        $default_role = $form->getInput("default_role");
590        $this->assignments_obj->setDefaultRole((int) $default_role);
591
592        $this->assignments_obj->save();
593        ilUtil::sendSuccess($this->lng->txt('settings_saved'));
594        $this->view();
595        return true;
596    }
597
598    public function saveRoleAccessLimitations()
599    {
600        global $DIC;
601
602        $ilAccess = $DIC['ilAccess'];
603        $ilErr = $DIC['ilErr'];
604        $rbacreview = $DIC['rbacreview'];
605
606        if (!$ilAccess->checkAccess('write', '', $this->ref_id)) {
607            $ilErr->raiseError($this->lng->txt("msg_no_perm_write"), $ilErr->MESSAGE);
608        }
609
610        $this->__initRoleAccessLimitations();
611
612        include_once './Services/AccessControl/classes/class.ilObjRole.php';
613
614        $this->access_limitations_obj->resetAccessLimitations();
615        foreach (ilObjRole::_lookupRegisterAllowed() as $role) {
616            $this->access_limitations_obj->setMode($_POST['access_limitation_mode_' . $role['id']], $role['id']);
617            $this->access_limitations_obj->setAbsolute($_POST['access_limitation_absolute_' . $role['id']], $role['id']);
618            $this->access_limitations_obj->setRelative($_POST['access_limitation_relative_' . $role['id']], $role['id']);
619        }
620
621        if ($err = $this->access_limitations_obj->validate()) {
622            switch ($err) {
623                case IL_REG_ACCESS_LIMITATION_MISSING_MODE:
624                    ilUtil::sendFailure($this->lng->txt('reg_access_limitation_missing_mode'));
625                    break;
626
627                case IL_REG_ACCESS_LIMITATION_OUT_OF_DATE:
628                    ilUtil::sendFailure($this->lng->txt('reg_access_limitation_out_of_date'));
629                    break;
630            }
631            $this->editRoleAccessLimitations();
632            return false;
633        }
634
635
636        $this->access_limitations_obj->save();
637        ilUtil::sendSuccess($this->lng->txt('settings_saved'));
638        $this->view();
639        return true;
640    }
641
642    public function __parseRoleList($roles, $url)
643    {
644        $tpl = new ilTemplate('tpl.registration_roles.html', true, true, 'Services/Registration');
645
646        $tpl->setVariable("EDIT", $this->lng->txt("edit"));
647        $tpl->setVariable("LINK_EDIT", $url);
648
649        if (is_array($roles) && sizeof($roles)) {
650            foreach ($roles as $role) {
651                $tpl->setCurrentBlock("list_item");
652                $tpl->setVariable("LIST_ITEM_ITEM", $role);
653                $tpl->parseCurrentBlock();
654            }
655        } else {
656            $tpl->setVariable("NONE", $this->lng->txt('none'));
657        }
658
659        return $tpl->get();
660    }
661
662    public function __prepareRoleList()
663    {
664        include_once './Services/AccessControl/classes/class.ilObjRole.php';
665
666        $all = array();
667        foreach (ilObjRole::_lookupRegisterAllowed() as $role) {
668            $all[] = $role['title'];
669        }
670        return $all;
671    }
672
673    public function __prepareAutomaticRoleList()
674    {
675        include_once './Services/AccessControl/classes/class.ilObjRole.php';
676        $this->__initRoleAssignments();
677
678        $all = array();
679        foreach ($this->assignments_obj->getAssignments() as $assignment) {
680            if (strlen($assignment['domain']) and $assignment['role']) {
681                $all[] = $assignment['domain'] . ' -> ' . ilObjRole::_lookupTitle($assignment['role']);
682            }
683        }
684
685        if (strlen($this->assignments_obj->getDefaultRole())) {
686            $all[] = $this->lng->txt('reg_default') . ' -> ' . ilObjRole::_lookupTitle($this->assignments_obj->getDefaultRole());
687        }
688
689        return $all;
690    }
691
692    public function __prepareAccessLimitationRoleList()
693    {
694        global $DIC;
695
696        $lng = $DIC['lng'];
697
698        $this->__initRoleAccessLimitations();
699
700        include_once './Services/AccessControl/classes/class.ilObjRole.php';
701
702        $all = array();
703        foreach (ilObjRole::_lookupRegisterAllowed() as $role) {
704            switch ($this->access_limitations_obj->getMode($role['id'])) {
705                case 'absolute':
706                    $txt_access_value = $lng->txt('reg_access_limitation_limited_until');
707                    $txt_access_value .= " " . ilDatePresentation::formatDate(new ilDateTime($this->access_limitations_obj->getAbsolute($role['id'], IL_CAL_UNIX)));
708                    break;
709
710                case 'relative':
711                    $years = $this->access_limitations_obj->getRelative($role['id'], 'y');
712                    $months = $this->access_limitations_obj->getRelative($role['id'], 'm');
713                    $days = $this->access_limitations_obj->getRelative($role['id'], 'd');
714
715                    $txt_access_value = $lng->txt('reg_access_limitation_limited_time') . " ";
716
717                    if ($years) {
718                        $txt_access_value .= $years . " ";
719                        $txt_access_value .= ($years == 1) ? $lng->txt('year') : $lng->txt('years');
720
721                        if ($months) {
722                            if ($days) {
723                                $txt_access_value .= ", ";
724                            } else {
725                                $txt_access_value .= " " . $lng->txt('and') . " ";
726                            }
727                        } elseif ($days) {
728                            $txt_access_value .= " " . $lng->txt('and') . " ";
729                        }
730                    }
731
732                    if ($months) {
733                        $txt_access_value .= $months . " ";
734                        $txt_access_value .= ($months == 1) ? $lng->txt('month') : $lng->txt('months');
735
736                        if ($days) {
737                            $txt_access_value .= " " . $lng->txt('and') . " ";
738                        }
739                    }
740
741                    if ($days) {
742                        $txt_access_value .= $days . " ";
743                        $txt_access_value .= ($days == 1) ? $lng->txt('day') : $lng->txt('days');
744                    }
745                    break;
746
747                default:
748                    $txt_access_value = $lng->txt('reg_access_limitation_none');
749                    break;
750            }
751
752            $all[] = $role['title'] . ' (' . $txt_access_value . ')';
753        }
754
755        return $all;
756    }
757
758    public function __initRoleAssignments()
759    {
760        if (is_object($this->assignments_obj)) {
761            return true;
762        }
763
764        include_once 'Services/Registration/classes/class.ilRegistrationEmailRoleAssignments.php';
765
766        $this->assignments_obj = new ilRegistrationRoleAssignments();
767    }
768
769    public function __initRoleAccessLimitations()
770    {
771        if (is_object($this->access_limitations_obj)) {
772            return true;
773        }
774
775        include_once 'Services/Registration/classes/class.ilRegistrationRoleAccessLimitations.php';
776
777        $this->access_limitations_obj = new ilRegistrationRoleAccessLimitations();
778    }
779
780    public function __buildAccessLimitationSelection($a_role_id)
781    {
782        global $DIC;
783
784        $lng = $DIC['lng'];
785
786        $options = array(
787                        'null' => $lng->txt('please_choose'),
788                        'unlimited' => $lng->txt('reg_access_limitation_mode_unlimited'),
789                        'absolute' => $lng->txt('reg_access_limitation_mode_absolute'),
790                        'relative' => $lng->txt('reg_access_limitation_mode_relative')
791                        );
792
793        $attribs = array('onchange' => 'displayAccessLimitationSelectionForm(document.cmd.access_limitation_mode_' . $a_role_id . ',' . $a_role_id . ')');
794
795        $selected = $this->access_limitations_obj->getMode($a_role_id);
796
797        return ilUtil::formSelect($selected, 'access_limitation_mode_' . $a_role_id, $options, false, true, 0, "", $attribs);
798    }
799
800    public function __prepareDateSelect($a_unix_time)
801    {
802        if (!$a_unix_time) {
803            $a_unix_time = time();
804        }
805
806        return array('y' => date('Y', $a_unix_time),
807                     'm' => date('n', $a_unix_time),
808                     'd' => date('d', $a_unix_time));
809    }
810
811    public function listCodes()
812    {
813        global $DIC;
814        $this->checkAccess("visible,read");
815
816        $this->setSubTabs('registration_codes');
817
818        if ($this->checkAccessBool("write")) {
819            $DIC->toolbar()->addButton(
820                $this->lng->txt("registration_codes_add"),
821                $this->ctrl->getLinkTarget($this, "addCodes")
822            );
823        }
824
825        include_once("./Services/Registration/classes/class.ilRegistrationCodesTableGUI.php");
826        $ctab = new ilRegistrationCodesTableGUI($this, "listCodes");
827        $this->tpl->setContent($ctab->getHTML());
828    }
829
830    public function initAddCodesForm()
831    {
832        global $DIC;
833
834        $rbacreview = $DIC['rbacreview'];
835        $ilObjDataCache = $DIC['ilObjDataCache'];
836        $lng = $DIC['lng'];
837
838        include_once 'Services/Form/classes/class.ilPropertyFormGUI.php';
839
840        $this->form_gui = new ilPropertyFormGUI();
841        $this->form_gui->setFormAction($this->ctrl->getFormAction($this, 'createCodes'));
842        $this->form_gui->setTitle($this->lng->txt('registration_codes_edit_header'));
843
844        $count = new ilNumberInputGUI($this->lng->txt('registration_codes_number'), 'reg_codes_number');
845        $count->setSize(4);
846        $count->setMaxLength(4);
847        $count->setMinValue(1);
848        $count->setMaxValue(1000);
849        $count->setRequired(true);
850        $this->form_gui->addItem($count);
851
852        // type
853        $code_type = new ilCheckboxGroupInputGUI($this->lng->txt('registration_codes_type'), 'code_type');
854        $code_type->setRequired(true);
855
856        $code_type->addOption(
857            new ilCheckboxOption(
858                $this->lng->txt('registration_codes_type_reg'),
859                self::CODE_TYPE_REGISTRATION,
860                $this->lng->txt('registration_codes_type_reg_info')
861                )
862        );
863        $code_type->addOption(
864            new ilCheckboxOption(
865                $this->lng->txt('registration_codes_type_ext'),
866                self::CODE_TYPE_EXTENSION,
867                $this->lng->txt('registration_codes_type_ext_info')
868                )
869        );
870        $this->form_gui->addItem($code_type);
871
872
873        $sec = new ilFormSectionHeaderGUI();
874        $sec->setTitle($this->lng->txt('registration_codes_roles_title'));
875        $this->form_gui->addItem($sec);
876
877        include_once './Services/AccessControl/classes/class.ilObjRole.php';
878        $options = array("" => $this->lng->txt('registration_codes_no_assigned_role'));
879        foreach ($rbacreview->getGlobalRoles() as $role_id) {
880            if (!in_array($role_id, array(SYSTEM_ROLE_ID, ANONYMOUS_ROLE_ID))) {
881                $options[$role_id] = $ilObjDataCache->lookupTitle($role_id);
882            }
883        }
884        $roles = new ilSelectInputGUI($this->lng->txt("registration_codes_roles"), "reg_codes_role");
885        $roles->setInfo($this->lng->txt("registration_codes_override_info"));
886        $roles->setOptions($options);
887        $this->form_gui->addItem($roles);
888
889        $local = new ilTextInputGUI($this->lng->txt("registration_codes_roles_local"), "reg_codes_local");
890        $local->setMulti(true);
891        $local->setDataSource($this->ctrl->getLinkTarget($this, "getLocalRoleAutoComplete", "", true));
892        $this->form_gui->addItem($local);
893
894
895        $sec = new ilFormSectionHeaderGUI();
896        $sec->setTitle($this->lng->txt('reg_access_limitations'));
897        $this->form_gui->addItem($sec);
898
899        $limit = new ilRadioGroupInputGUI($this->lng->txt("reg_access_limitation_mode"), "reg_limit");
900        $limit->setInfo($this->lng->txt("registration_codes_override_info"));
901        $this->form_gui->addItem($limit);
902
903        $opt = new ilRadioOption($this->lng->txt("registration_codes_roles_limitation_none"), "none");
904        $limit->addOption($opt);
905
906        $opt = new ilRadioOption($this->lng->txt("reg_access_limitation_none"), "unlimited");
907        $limit->addOption($opt);
908
909        $opt = new ilRadioOption($this->lng->txt("reg_access_limitation_mode_absolute"), "absolute");
910        $limit->addOption($opt);
911
912        $dt = new ilDateTimeInputGUI($this->lng->txt("reg_access_limitation_mode_absolute_target"), "abs_date");
913        $dt->setRequired(true);
914        $opt->addSubItem($dt);
915
916        $opt = new ilRadioOption($this->lng->txt("reg_access_limitation_mode_relative"), "relative");
917        $limit->addOption($opt);
918
919        $dur = new ilDurationInputGUI($this->lng->txt("reg_access_limitation_mode_relative_target"), "rel_date");
920        $dur->setRequired(true);
921        $dur->setShowMonths(true);
922        $dur->setShowDays(true);
923        $dur->setShowHours(false);
924        $dur->setShowMinutes(false);
925        $opt->addSubItem($dur);
926
927        $this->form_gui->addCommandButton('createCodes', $this->lng->txt('create'));
928        $this->form_gui->addCommandButton('listCodes', $this->lng->txt('cancel'));
929    }
930
931    // see ilRoleAutoCompleteInputGUI
932    public function getLocalRoleAutoComplete()
933    {
934        $q = $_REQUEST["term"];
935        include_once("./Services/AccessControl/classes/class.ilRoleAutoComplete.php");
936        $list = ilRoleAutoComplete::getList($q);
937        echo $list;
938        exit;
939    }
940
941    public function addCodes()
942    {
943        global $DIC;
944
945        $ilAccess = $DIC['ilAccess'];
946        $ilErr = $DIC['ilErr'];
947
948        if (!$ilAccess->checkAccess('write', '', $this->ref_id)) {
949            $ilErr->raiseError($this->lng->txt("msg_no_perm_write"), $ilErr->MESSAGE);
950        }
951
952        $this->setSubTabs('registration_codes');
953
954        $this->initAddCodesForm();
955
956        // default
957        $limit = $this->form_gui->getItemByPostVar("reg_limit");
958        $limit->setValue("none");
959
960        $this->tpl->setContent($this->form_gui->getHTML());
961    }
962
963    public function createCodes()
964    {
965        global $DIC;
966
967        $ilAccess = $DIC['ilAccess'];
968        $ilErr = $DIC['ilErr'];
969        $rbacreview = $DIC['rbacreview'];
970
971        if (!$ilAccess->checkAccess('write', '', $this->ref_id)) {
972            $ilErr->raiseError($this->lng->txt("msg_no_perm_write"), $ilErr->MESSAGE);
973        }
974
975        $this->setSubTabs('registration_codes');
976
977        $this->initAddCodesForm();
978        $valid = $this->form_gui->checkInput();
979        if ($valid) {
980            $number = $this->form_gui->getInput('reg_codes_number');
981            $role = $this->form_gui->getInput('reg_codes_role');
982            $local = $this->form_gui->getInput("reg_codes_local");
983
984            if (is_array($local)) {
985                $role_ids = array();
986                foreach (array_unique($local) as $item) {
987                    if (trim($item)) {
988                        $role_id = $rbacreview->roleExists($item);
989                        if ($role_id) {
990                            $role_ids[] = $role_id;
991                        }
992                    }
993                }
994                if (sizeof($role_ids)) {
995                    $local = $role_ids;
996                }
997            }
998
999            $date = null;
1000            $limit = $this->form_gui->getInput("reg_limit");
1001            switch ($limit) {
1002                case "absolute":
1003                    $date_input = $this->form_gui->getItemByPostVar("abs_date");
1004                    $date = $date_input->getDate()->get(IL_CAL_DATE);
1005                    if ($date < date("Y-m-d")) {
1006                        $date_input->setAlert($this->lng->txt("form_msg_wrong_date"));
1007                        $valid = false;
1008                    }
1009                    break;
1010
1011                case "relative":
1012                    $date = $this->form_gui->getInput("rel_date");
1013                    if (!array_sum($date)) {
1014                        $valid = false;
1015                    } else {
1016                        $date = array(
1017                            "d" => $date["dd"],
1018                            "m" => $date["MM"] % 12,
1019                            "y" => floor($date["MM"] / 12)
1020                        );
1021                    }
1022                    break;
1023
1024                case "none":
1025                    $limit = null;
1026                    break;
1027            }
1028        }
1029
1030        if ($valid) {
1031            include_once './Services/Registration/classes/class.ilRegistrationCode.php';
1032
1033            $stamp = time();
1034            for ($loop = 1; $loop <= $number; $loop++) {
1035                $code_types = (array) $this->form_gui->getInput('code_type');
1036
1037                ilRegistrationCode::create(
1038                    $role,
1039                    $stamp,
1040                    $local,
1041                    $limit,
1042                    $date,
1043                    in_array(self::CODE_TYPE_REGISTRATION, $code_types) ? true : false,
1044                    in_array(self::CODE_TYPE_EXTENSION, $code_types) ? true : false
1045                );
1046            }
1047
1048            ilUtil::sendSuccess($this->lng->txt('saved_successfully'), true);
1049            $this->ctrl->redirect($this, "listCodes");
1050        } else {
1051            $this->form_gui->setValuesByPost();
1052            $this->tpl->setContent($this->form_gui->getHtml());
1053        }
1054    }
1055
1056    public function deleteCodes()
1057    {
1058        $this->checkAccess("write");
1059
1060        include_once './Services/Registration/classes/class.ilRegistrationCode.php';
1061        ilRegistrationCode::deleteCodes($_POST["id"]);
1062
1063        ilUtil::sendSuccess($this->lng->txt('info_deleted'), true);
1064        $this->ctrl->redirect($this, "listCodes");
1065    }
1066
1067    public function deleteConfirmation()
1068    {
1069        $this->checkAccess("write");
1070
1071        global $DIC;
1072
1073        $ilErr = $DIC['ilErr'];
1074        $ilias = $DIC['ilias'];
1075
1076        if (!isset($_POST["id"])) {
1077            $ilErr->raiseError($this->lng->txt("no_checkbox"), $ilErr->MESSAGE);
1078        }
1079
1080        $this->setSubTabs('registration_codes');
1081
1082        include_once './Services/Utilities/classes/class.ilConfirmationGUI.php';
1083        $gui = new ilConfirmationGUI();
1084        $gui->setHeaderText($this->lng->txt("info_delete_sure"));
1085        $gui->setCancel($this->lng->txt("cancel"), "listCodes");
1086        $gui->setConfirm($this->lng->txt("confirm"), "deleteCodes");
1087        $gui->setFormAction($this->ctrl->getFormAction($this, "deleteCodes"));
1088
1089        include_once './Services/Registration/classes/class.ilRegistrationCode.php';
1090        $data = ilRegistrationCode::loadCodesByIds($_POST["id"]);
1091        foreach ($data as $code) {
1092            $gui->addItem("id[]", $code["code_id"], $code["code"]);
1093        }
1094
1095        $this->tpl->setContent($gui->getHTML());
1096    }
1097
1098    public function resetCodesFilter()
1099    {
1100        include_once("./Services/Registration/classes/class.ilRegistrationCodesTableGUI.php");
1101        $utab = new ilRegistrationCodesTableGUI($this, "listCodes");
1102        $utab->resetOffset();
1103        $utab->resetFilter();
1104
1105        $this->listCodes();
1106    }
1107
1108    public function applyCodesFilter()
1109    {
1110        include_once("./Services/Registration/classes/class.ilRegistrationCodesTableGUI.php");
1111        $utab = new ilRegistrationCodesTableGUI($this, "listCodes");
1112        $utab->resetOffset();
1113        $utab->writeFilterToSession();
1114
1115        $this->listCodes();
1116    }
1117
1118    public function exportCodes()
1119    {
1120        global $DIC;
1121
1122        $ilAccess = $DIC['ilAccess'];
1123        $ilErr = $DIC['ilErr'];
1124
1125        if (!$ilAccess->checkAccess('read', '', $this->ref_id)) {
1126            $ilErr->raiseError($this->lng->txt("msg_no_perm_read"), $ilErr->MESSAGE);
1127        }
1128
1129        include_once("./Services/Registration/classes/class.ilRegistrationCodesTableGUI.php");
1130        $utab = new ilRegistrationCodesTableGUI($this, "listCodes");
1131
1132        include_once './Services/Registration/classes/class.ilRegistrationCode.php';
1133        $codes = ilRegistrationCode::getCodesForExport($utab->filter["code"], $utab->filter["role"], $utab->filter["generated"], $utab->filter["alimit"]);
1134
1135        if (sizeof($codes)) {
1136            ilUtil::deliverData(implode("\r\n", $codes), "ilias_registration_codes_" . date("d-m-Y") . ".txt", "text/plain");
1137        } else {
1138            ilUtil::sendFailure($this->lng->txt("registration_export_codes_no_data"));
1139            $this->listCodes();
1140        }
1141    }
1142}
1143