ILIAS  Release_4_1_x_branch Revision 61804
 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  var $active = false;
46 
53  private function __construct()
54  {
55  global $ilSetting,$ilDB;
56 
57  $this->settings = $ilSetting;
58  $this->db = $ilDB;
59 
60  $this->read();
61  }
62 
70  public static function _getInstance()
71  {
72  if(isset(self::$instance) and self::$instance)
73  {
74  return self::$instance;
75  }
76  return self::$instance = new ilRadiusSettings();
77  }
78 
79  public function isActive()
80  {
81  return $this->active ? true : false;
82  }
83  public function setActive($a_status)
84  {
85  $this->active = $a_status;
86  }
87  public function setPort($a_port)
88  {
89  $this->port = $a_port;
90  }
91  public function getPort()
92  {
93  return $this->port;
94  }
95  public function setSecret($a_secret)
96  {
97  $this->secret = $a_secret;
98  }
99  public function getSecret()
100  {
101  return $this->secret;
102  }
103  public function setServerString($a_server_string)
104  {
105  $this->server_string = $a_server_string;
106  $this->servers = explode(',',$this->server_string);
107  }
108  public function getServersAsString()
109  {
110  return implode(',',$this->servers);
111  }
112  public function getServers()
113  {
114  return $this->servers ? $this->servers : array();
115  }
116  public function setName($a_name)
117  {
118  $this->name = $a_name;
119  }
120  public function getName()
121  {
122  return $this->name;
123  }
124 
131  public function toPearAuthArray()
132  {
133  foreach($this->getServers() as $server)
134  {
135  $auth_params['servers'][] = array($server,$this->getPort(),$this->getSecret());
136  }
137  return $auth_params ? $auth_params : array();
138  }
139 
147  public function getDefaultRole()
148  {
149  return $this->default_role;
150  }
151 
152  public function setDefaultRole($a_role)
153  {
154  $this->default_role = $a_role;
155  }
156 
163  public function enabledCreation()
164  {
165  return $this->creation;
166  }
167 
175  public function enableCreation($a_status)
176  {
177  $this->creation = $a_status;
178  }
179 
187  public function enableAccountMigration($a_status)
188  {
189  $this->account_migration = $a_status;
190  }
191 
198  public function isAccountMigrationEnabled()
199  {
200  return $this->account_migration ? true : false;
201  }
202 
209  public function getCharset()
210  {
211  return $this->charset ? 1 : 0;
212  }
213 
221  public function setCharset($a_charset)
222  {
223  $this->charset = $a_charset;
224  }
225 
232  public function save()
233  {
234  // first delete old servers
235  $this->settings->deleteLike('radius_server%');
236 
237  $this->settings->set('radius_active',$this->isActive() ? 1 : 0);
238  $this->settings->set('radius_port',$this->getPort());
239  $this->settings->set('radius_shared_secret',$this->getSecret());
240  $this->settings->set('radius_name',$this->getName());
241  $this->settings->set('radius_creation',$this->enabledCreation() ? 1 : 0);
242  $this->settings->set('radius_migration',$this->isAccountMigrationEnabled() ? 1 : 0);
243  $this->settings->set('radius_charset',$this->getCharset() ? 1 : 0);
244 
245  $counter = 0;
246  foreach($this->getServers() as $server)
247  {
248  if(++$counter == 1)
249  {
250  $this->settings->set('radius_server',trim($server));
251  }
252  else
253  {
254  $this->settings->set('radius_server'.$counter,trim($server));
255  }
256  }
257 
258  include_once('./Services/AccessControl/classes/class.ilObjRole.php');
259  ilObjRole::_resetAuthMode('radius');
260 
261  if($this->getDefaultRole())
262  {
263  ilObjRole::_updateAuthMode(array($this->getDefaultRole() => 'radius'));
264  }
265  return true;
266  }
267 
274  public function validateRequired()
275  {
276  $ok = strlen($this->getServersAsString()) and strlen($this->getPort()) and strlen($this->getSecret()) and strlen($this->getName());
277 
278  $role_ok = true;
279  if($this->enabledCreation() and !$this->getDefaultRole())
280  {
281  $role_ok = false;
282  }
283  return $ok and $role_ok;
284  }
285 
292  public function validatePort()
293  {
294  return preg_match("/^[0-9]{0,5}$/",$this->getPort()) == 1;
295  }
296 
303  public function validateServers()
304  {
305  $servers = explode(",",$this->server_string);
306 
307  foreach ($servers as $server)
308  {
309  $server = trim($server);
310 
311  if (!ilUtil::isIPv4($server) and !ilUtil::isDN($server))
312  {
313  return false;
314  }
315  }
316  return true;
317  }
318 
319 
326  private function read()
327  {
328  $all_settings = $this->settings->getAll();
329 
330  $sets = array("radius_active" => "setActive",
331  "radius_port" => "setPort",
332  "radius_shared_secret" => "setSecret",
333  "radius_name" => "setName",
334  "radius_creation" => "enableCreation",
335  "radius_migration" => "enableAccountMigration",
336  "radius_charset" => "setCharset"
337  );
338  foreach ($sets as $s => $m)
339  {
340  if (isset($all_settings[$s]))
341  {
342  $this->$m($all_settings[$s]);
343  }
344  }
345 
346  reset($all_settings);
347  foreach($all_settings as $k => $v)
348  {
349  if (substr($k, 0, 13) == "radius_server")
350  {
351  $this->servers[] = $v;
352  }
353  }
354 
355  include_once('./Services/AccessControl/classes/class.ilObjRole.php');
356  $roles = ilObjRole::_getRolesByAuthMode('radius');
357  $this->default_role = 0;
358  if (isset($roles[0]) && $roles[0])
359  {
360  $this->default_role = $roles[0];
361  }
362  }
363 }
364 
365 
366 ?>