ILIAS  Release_3_10_x_branch Revision 61812
 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  $query = "DELETE FROM settings WHERE keyword LIKE('radius_server%')";
235  $this->db->query($query);
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  $this->setActive($all_settings['radius_active']);
331  $this->setPort($all_settings['radius_port']);
332  $this->setSecret($all_settings['radius_shared_secret']);
333  $this->setName($all_settings['radius_name']);
334  $this->enableCreation($all_settings['radius_creation']);
335  $this->enableAccountMigration($all_settings['radius_migration']);
336  $this->setCharset($all_settings['radius_charset']);
337 
338  $query = "SELECT value FROM settings WHERE keyword LIKE 'radius_server%' ORDER BY keyword ASC";
339  $res = $this->db->query($query);
340 
341  while ($row = $res->fetchRow())
342  {
343  $this->servers[] = $row[0];
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 ?>