KumbiaPHP  beta2
Framework PHP
 Todo Estructuras de Datos Namespaces Archivos Funciones Variables Páginas
auth.php
Ir a la documentación de este archivo.
1 <?php
23 require_once CORE_PATH . 'libs/auth/auth_interface.php';
24 
31 class Auth
32 {
33 
39  private $adapter;
45  private $adapter_object = null;
52  private $active_session = false;
58  private $expire_time = 3600;
64  private $extra_args = array();
68  private $sleep_time = 0;
74  private static $is_valid = null;
80  private static $active_identity = array();
81 
87  public function __construct()
88  {
89  $extra_args = Util::getParams(func_get_args());
90  if (isset($extra_args[0])) {
91  $adapter = $extra_args[0];
92  unset($extra_args[0]);
93  } else {
94  $adapter = 'model';
95  }
96  $this->set_adapter($adapter, $this, $extra_args);
97  }
98 
102  public function set_adapter($adapter, $auth = null, $extra_args = array())
103  {
104  if (!in_array($adapter, array('digest', 'http', 'model', 'kerberos5', 'radius'))) {
105  throw new kumbiaException("Adaptador de autenticaci&oacute;n '$adapter' no soportado");
106  }
107  $this->adapter = Util::camelcase($adapter);
108  require_once CORE_PATH . "libs/auth/adapters/{$adapter}_auth.php";
109  $adapter_class = $this->adapter . 'Auth';
110  $this->extra_args = $extra_args;
111  $this->adapter_object = new $adapter_class($auth, $extra_args);
112  }
113 
118  public function get_adapter_name()
119  {
120  return $this->adapter;
121  }
122 
128  public function authenticate()
129  {
130  $result = $this->adapter_object->authenticate();
134  if ($result && $this->active_session) {
135  $user_hash = md5(serialize($this->extra_args));
136  $filename = APP_PATH . 'temp/cache/' . base64_encode('auth');
137  if (file_exists($filename)) {
138  $fp = fopen($filename, 'r');
139  while (!feof($fp)) {
140  $line = fgets($fp);
141  $user = explode(':', $line);
142  if ($user_hash == $user[0]) {
143  if ($user[1] + $user[2] > time()) {
144  if ($this->sleep_time) {
145  sleep($this->sleep_time);
146  }
147  self::$active_identity = array();
148  self::$is_valid = false;
149  return false;
150  } else {
151  fclose($fp);
152  $this->destroy_active_session();
153  file_put_contents($filename, $user_hash . ':' . time() . ':' . $this->expire_time . "\n");
154  }
155  }
156  }
157  fclose($fp);
158  $fp = fopen($filename, 'a');
159  fputs($fp, $user_hash . ':' . time() . ':' . $this->expire_time . "\n");
160  fclose($fp);
161  } else {
162  file_put_contents($filename, $user_hash . ':' . time() . ':' . $this->expire_time . "\n");
163  }
164  }
165  if (!$result) {
166  if ($this->sleep_time) {
167  sleep($this->sleep_time);
168  }
169  }
170  $_SESSION['KUMBIA_AUTH_IDENTITY'][Config::get('config.application.namespace_auth')] = $this->adapter_object->get_identity();
171  self::$active_identity = $this->adapter_object->get_identity();
172  $_SESSION['KUMBIA_AUTH_VALID'][Config::get('config.application.namespace_auth')] = $result;
173  self::$is_valid = $result;
174  return $result;
175  }
176 
182  public function authenticate_with_http()
183  {
184  if (!$_SERVER['PHP_AUTH_USER']) {
185  header('WWW-Authenticate: Basic realm="basic"');
186  header('HTTP/1.0 401 Unauthorized');
187  return false;
188  } else {
189  $options = array("username" => $_SERVER['PHP_AUTH_USER'], "password" => $_SERVER['PHP_AUTH_PW']);
190  $this->adapter_object->set_params($options);
191  return $this->authenticate();
192  }
193  }
194 
200  public function get_identity()
201  {
202  return $this->adapter_object->get_identity();
203  }
204 
210  public function set_active_session($value, $time = 3600)
211  {
212  $this->active_session = $value;
213  $this->expire_time = $time;
214  }
215 
220  public function destroy_active_session()
221  {
222  $user_hash = md5(serialize($this->extra_args));
223  $filename = APP_PATH . 'temp/cache/' . base64_encode('auth');
224  $lines = file($filename);
225  $lines_out = array();
226  foreach ($lines as $line) {
227  if (substr($line, 0, 32) != $user_hash) {
228  $lines_out[] = $line;
229  }
230  }
231  file_put_contents($filename, join("\n", $lines_out));
232  }
233 
239  public function get_adapter_instance()
240  {
241  return $this->adapter_object;
242  }
243 
250  public function sleep_on_fail($value, $time = 2)
251  {
252  $time = (int) $time;
253  if ($time < 0) {
254  $time = 0;
255  }
256  if ($value) {
257  $this->sleep_time = $time;
258  } else {
259  $this->sleep_time = 0;
260  }
261  }
262 
268  static public function is_valid()
269  {
270  if (!is_null(self::$is_valid)) {
271  return self::$is_valid;
272  } else {
273  self::$is_valid = isset($_SESSION['KUMBIA_AUTH_VALID'][Config::get('config.application.namespace_auth')]) ? $_SESSION['KUMBIA_AUTH_VALID'][Config::get('config.application.namespace_auth')] : null;
274  return self::$is_valid;
275  }
276  }
277 
283  static public function get_active_identity()
284  {
285  if (count(self::$active_identity)) {
286  return self::$active_identity;
287  } else {
288  self::$active_identity = $_SESSION['KUMBIA_AUTH_IDENTITY'][Config::get('config.application.namespace_auth')];
289  return self::$active_identity;
290  }
291  }
292 
299  public static function get($var = null)
300  {
301  if ($var) {
302  return $_SESSION['KUMBIA_AUTH_IDENTITY'][Config::get('config.application.namespace_auth')][$var];
303  }
304  }
305 
310  static public function destroy_identity()
311  {
312  self::$is_valid = null;
313  unset($_SESSION['KUMBIA_AUTH_VALID'][Config::get('config.application.namespace_auth')]);
314  self::$active_identity = null;
315  unset($_SESSION['KUMBIA_AUTH_IDENTITY'][Config::get('config.application.namespace_auth')]);
316  }
317 
318 }