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
24
25define('IL_REG_DISABLED', 1);
26define('IL_REG_DIRECT', 2);
27define('IL_REG_APPROVE', 3);
28define('IL_REG_ACTIVATION', 4);
29define('IL_REG_CODES', 5);
30
31define('IL_REG_ROLES_FIXED', 1);
32define('IL_REG_ROLES_EMAIL', 2);
33
34define('IL_REG_ERROR_UNKNOWN', 1);
35define('IL_REG_ERROR_NO_PERM', 2);
36
37/**
38* Class ilObjAuthSettingsGUI
39*
40* @author Stefan Meyer <smeyer.ilias@gmx.de>
41* @version $Id$
42*
43* @ingroup ServicesRegistration
44*/
45class ilRegistrationSettings
46{
47    const ERR_UNKNOWN_RCP = 1;
48    const ERR_MISSING_RCP = 2;
49
50    const REG_HASH_LIFETIME_MIN_VALUE = 60;
51
52    private $registration_type;
53    private $password_generation_enabled;
54    private $access_limitation;
55    private $approve_recipient_logins;
56    private $approve_recipient_ids;
57    private $role_type;
58    private $unknown;
59    private $reg_hash_life_time = 0;
60    private $reg_allow_codes = false;
61    private $allowed_domains;
62
63    public function __construct()
64    {
65        $this->__read();
66    }
67
68    public function getRegistrationType()
69    {
70        return $this->registration_type;
71    }
72    public function setRegistrationType($a_type)
73    {
74        $this->registration_type = $a_type;
75    }
76
77    public static function _lookupRegistrationType()
78    {
79        global $DIC;
80
81        $ilSetting = $DIC['ilSetting'];
82
83        $ret = (int) $ilSetting->get('new_registration_type', IL_REG_DISABLED);
84
85        if ($ret < 1 or $ret > 5) {
86            //data is corrupted and should be processed like "No Registration possible" (#18261)
87            $ret = IL_REG_DISABLED;
88        }
89
90        return $ret;
91    }
92
93    public function enabled()
94    {
95        return $this->registration_type != IL_REG_DISABLED;
96    }
97    public function directEnabled()
98    {
99        return $this->registration_type == IL_REG_DIRECT;
100    }
101    public function approveEnabled()
102    {
103        return $this->registration_type == IL_REG_APPROVE;
104    }
105    public function activationEnabled()
106    {
107        return $this->registration_type == IL_REG_ACTIVATION;
108    }
109    public function registrationCodeRequired()
110    {
111        return $this->registration_type == IL_REG_CODES;
112    }
113
114    public function passwordGenerationEnabled()
115    {
116        return $this->password_generation_enabled;
117    }
118    public function setPasswordGenerationStatus($a_status)
119    {
120        $this->password_generation_enabled = $a_status;
121    }
122
123    public function getAccessLimitation()
124    {
125        return $this->access_limitation;
126    }
127
128    public function setAccessLimitation($a_access_limitation)
129    {
130        $this->access_limitation = $a_access_limitation;
131    }
132
133    public function setApproveRecipientLogins($a_rec_string)
134    {
135        $this->approve_recipient_logins = $a_rec_string;
136        $this->approve_recipient_ids = array();
137
138        // convert logins to array of ids
139        foreach (explode(',', trim($this->approve_recipient_logins)) as $login) {
140            if ($uid = ilObjUser::_lookupId(trim($login))) {
141                $this->approve_recipient_ids[] = $uid;
142            }
143        }
144    }
145    public function getApproveRecipientLogins()
146    {
147        return $this->approve_recipient_logins;
148    }
149    public function getApproveRecipients()
150    {
151        return $this->approve_recipient_ids ? $this->approve_recipient_ids : array();
152    }
153    public function getUnknown()
154    {
155        return implode(',', $this->unknown);
156    }
157
158    public function roleSelectionEnabled()
159    {
160        return $this->role_type == IL_REG_ROLES_FIXED;
161    }
162    public function automaticRoleAssignmentEnabled()
163    {
164        return $this->role_type == IL_REG_ROLES_EMAIL;
165    }
166    public function setRoleType($a_type)
167    {
168        $this->role_type = $a_type;
169    }
170
171    public function setRegistrationHashLifetime($a_lifetime)
172    {
173        $this->reg_hash_life_time = $a_lifetime;
174
175        return $this;
176    }
177
178    public function getRegistrationHashLifetime()
179    {
180        return max($this->reg_hash_life_time, self::REG_HASH_LIFETIME_MIN_VALUE);
181    }
182
183    public function setAllowCodes($a_allow_codes)
184    {
185        $this->reg_allow_codes = (bool) $a_allow_codes;
186
187        return $this;
188    }
189
190    public function getAllowCodes()
191    {
192        return $this->reg_allow_codes;
193    }
194
195    public function setAllowedDomains($a_value)
196    {
197        $a_value = array_map(
198            function ($value) {
199                return trim($value);
200            },
201            explode(";", trim($a_value))
202        );
203
204        $this->allowed_domains = $a_value;
205    }
206
207    public function getAllowedDomains()
208    {
209        return (array) $this->allowed_domains;
210    }
211
212    public function validate()
213    {
214        $this->unknown = array();
215        $this->mail_perm = array();
216
217        $login_arr = explode(',', $this->getApproveRecipientLogins());
218        $login_arr = $login_arr ? $login_arr : array();
219        foreach ($login_arr as $recipient) {
220            if (!$recipient = trim($recipient)) {
221                continue;
222            }
223            if (!ilObjUser::_lookupId($recipient)) {
224                $this->unknown[] = $recipient;
225                continue;
226            } else {
227                $valid = $recipient;
228            }
229        }
230        if (count($this->unknown)) {
231            return self::ERR_UNKNOWN_RCP;
232        }
233        if ($this->getRegistrationType() == IL_REG_APPROVE and !count((array) $valid)) {
234            return self::ERR_MISSING_RCP;
235        }
236        return 0;
237    }
238
239
240    public function save()
241    {
242        global $DIC;
243
244        $ilias = $DIC['ilias'];
245
246        $ilias->setSetting('reg_role_assignment', $this->role_type);
247        $ilias->setSetting('new_registration_type', $this->registration_type);
248        $ilias->setSetting('passwd_reg_auto_generate', $this->password_generation_enabled);
249        $ilias->setSetting('approve_recipient', addslashes(serialize($this->approve_recipient_ids)));
250        $ilias->setSetting('reg_access_limitation', $this->access_limitation);
251        $ilias->setSetting('reg_hash_life_time', $this->reg_hash_life_time);
252        $ilias->setSetting('reg_allow_codes', $this->reg_allow_codes);
253        $ilias->setSetting('reg_allowed_domains', implode(';', $this->allowed_domains));
254
255        return true;
256    }
257
258    public function __read()
259    {
260        global $DIC;
261
262        $ilias = $DIC['ilias'];
263
264        //static method validates value
265        $this->registration_type = self::_lookupRegistrationType();
266
267        $this->role_type = $ilias->getSetting('reg_role_assignment', 1);
268        $this->password_generation_enabled = $ilias->getSetting('passwd_reg_auto_generate');
269        $this->access_limitation = $ilias->getSetting('reg_access_limitation');
270        $this->reg_hash_life_time = $ilias->getSetting('reg_hash_life_time');
271        $this->reg_allow_codes = (bool) $ilias->getSetting('reg_allow_codes');
272
273        $this->approve_recipient_ids = unserialize(stripslashes($ilias->getSetting('approve_recipient')));
274        $this->approve_recipient_ids = $this->approve_recipient_ids ?
275            $this->approve_recipient_ids :
276            array();
277
278        // create login array
279        $tmp_logins = array();
280        foreach ($this->approve_recipient_ids as $id) {
281            if ($login = ilObjUser::_lookupLogin($id)) {
282                $tmp_logins[] = $login;
283            }
284        }
285        $this->approve_recipient_logins = implode(',', $tmp_logins);
286
287        $this->setAllowedDomains($ilias->getSetting('reg_allowed_domains'));
288    }
289}
290