ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilRadiusSettings.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2006 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 
33 {
36 
37 
38  private $settings;
39  private $db;
40  private static $instance = null;
41 
42  private $account_migration = false;
43 
44  private $servers = array();
45 
52  private function __construct()
53  {
54  global $ilSetting,$ilDB;
55 
56  $this->settings = $ilSetting;
57  $this->db = $ilDB;
58 
59  $this->read();
60  }
61 
69  public static function _getInstance()
70  {
71  if(isset(self::$instance) and self::$instance)
72  {
73  return self::$instance;
74  }
75  return self::$instance = new ilRadiusSettings();
76  }
77 
78  public function isActive()
79  {
80  return $this->active ? true : false;
81  }
82  public function setActive($a_status)
83  {
84  $this->active = $a_status;
85  }
86  public function setPort($a_port)
87  {
88  $this->port = $a_port;
89  }
90  public function getPort()
91  {
92  return $this->port;
93  }
94  public function setSecret($a_secret)
95  {
96  $this->secret = $a_secret;
97  }
98  public function getSecret()
99  {
100  return $this->secret;
101  }
102  public function setServerString($a_server_string)
103  {
104  $this->server_string = $a_server_string;
105  $this->servers = explode(',',$this->server_string);
106  }
107  public function getServersAsString()
108  {
109  return implode(',',$this->servers);
110  }
111  public function getServers()
112  {
113  return $this->servers ? $this->servers : array();
114  }
115  public function setName($a_name)
116  {
117  $this->name = $a_name;
118  }
119  public function getName()
120  {
121  return $this->name;
122  }
123 
130  public function toPearAuthArray()
131  {
132  foreach($this->getServers() as $server)
133  {
134  $auth_params['servers'][] = array($server,$this->getPort(),$this->getSecret());
135  }
136  return $auth_params ? $auth_params : array();
137  }
138 
146  public function getDefaultRole()
147  {
148  return $this->default_role;
149  }
150 
151  public function setDefaultRole($a_role)
152  {
153  $this->default_role = $a_role;
154  }
155 
162  public function enabledCreation()
163  {
164  return $this->creation;
165  }
166 
174  public function enableCreation($a_status)
175  {
176  $this->creation = $a_status;
177  }
178 
186  public function enableAccountMigration($a_status)
187  {
188  $this->account_migration = $a_status;
189  }
190 
197  public function isAccountMigrationEnabled()
198  {
199  return $this->account_migration ? true : false;
200  }
201 
208  public function getCharset()
209  {
210  return $this->charset ? 1 : 0;
211  }
212 
220  public function setCharset($a_charset)
221  {
222  $this->charset = $a_charset;
223  }
224 
231  public function save()
232  {
233  // first delete old servers
234  $this->settings->deleteLike('radius_server%');
235 
236  $this->settings->set('radius_active',$this->isActive() ? 1 : 0);
237  $this->settings->set('radius_port',$this->getPort());
238  $this->settings->set('radius_shared_secret',$this->getSecret());
239  $this->settings->set('radius_name',$this->getName());
240  $this->settings->set('radius_creation',$this->enabledCreation() ? 1 : 0);
241  $this->settings->set('radius_migration',$this->isAccountMigrationEnabled() ? 1 : 0);
242  $this->settings->set('radius_charset',$this->getCharset() ? 1 : 0);
243 
244  $counter = 0;
245  foreach($this->getServers() as $server)
246  {
247  if(++$counter == 1)
248  {
249  $this->settings->set('radius_server',trim($server));
250  }
251  else
252  {
253  $this->settings->set('radius_server'.$counter,trim($server));
254  }
255  }
256 
257  include_once('./Services/AccessControl/classes/class.ilObjRole.php');
258  ilObjRole::_resetAuthMode('radius');
259 
260  if($this->getDefaultRole())
261  {
262  ilObjRole::_updateAuthMode(array($this->getDefaultRole() => 'radius'));
263  }
264  return true;
265  }
266 
273  public function validateRequired()
274  {
275  $ok = strlen($this->getServersAsString()) and strlen($this->getPort()) and strlen($this->getSecret()) and strlen($this->getName());
276 
277  $role_ok = true;
278  if($this->enabledCreation() and !$this->getDefaultRole())
279  {
280  $role_ok = false;
281  }
282  return $ok and $role_ok;
283  }
284 
291  public function validatePort()
292  {
293  return preg_match("/^[0-9]{0,5}$/",$this->getPort()) == 1;
294  }
295 
302  public function validateServers()
303  {
304  $servers = explode(",",$this->server_string);
305 
306  foreach ($servers as $server)
307  {
308  $server = trim($server);
309 
310  if (!ilUtil::isIPv4($server) and !ilUtil::isDN($server))
311  {
312  return false;
313  }
314  }
315  return true;
316  }
317 
318 
325  private function read()
326  {
327  $all_settings = $this->settings->getAll();
328 
329  $this->setActive($all_settings['radius_active']);
330  $this->setPort($all_settings['radius_port']);
331  $this->setSecret($all_settings['radius_shared_secret']);
332  $this->setName($all_settings['radius_name']);
333  $this->enableCreation($all_settings['radius_creation']);
334  $this->enableAccountMigration($all_settings['radius_migration']);
335  $this->setCharset($all_settings['radius_charset']);
336 
337  reset($all_settings);
338  foreach($all_settings as $k => $v)
339  {
340  if (substr($k, 0, 13) == "radius_server")
341  {
342  $this->servers[] = $v;
343  }
344  }
345 
346  include_once('./Services/AccessControl/classes/class.ilObjRole.php');
347  $roles = ilObjRole::_getRolesByAuthMode('radius');
348  $this->default_role = $roles[0] ? $roles[0] : 0;
349  }
350 }
351 
352 
353 ?>