Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.basicAuthenticator.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 /**
7  * basicAuthenticator class
8  *
9  * This class provides methods for HTTP Basic authentication
10  *
11  * @author Anakeen
12  * @version $Id: Class.basicAuthenticator.php,v 1.3 2009/01/16 13:33:00 jerome Exp $
13  * @package FDL
14  * @subpackage
15  */
16 /**
17  */
18 include_once ('WHAT/Class.Authenticator.php');
19 
21 {
22 
23  const basicAuthorizationScheme = "Basic";
24  protected $auth_session = null;
25  public function checkAuthentication()
26  {
27  if (array_key_exists('logout', $_COOKIE) && $_COOKIE['logout'] == "true") {
28  setcookie('logout', '', time() - 3600, null, null, null, true);
30  }
31 
32  if (!array_key_exists('PHP_AUTH_USER', $_SERVER)) {
33  error_log(__CLASS__ . "::" . __FUNCTION__ . " " . "Error: undefined _SERVER[PHP_AUTH_USER]");
35  }
36 
37  if (!array_key_exists('PHP_AUTH_PW', $_SERVER)) {
38  error_log(__CLASS__ . "::" . __FUNCTION__ . " " . "Error: undefined _SERVER[PHP_AUTH_PW] for user " . $_SERVER['PHP_AUTH_USER']);
40  }
41 
42  if (!is_callable(array(
43  $this->provider,
44  'validateCredential'
45  ))) {
46  error_log(__CLASS__ . "::" . __FUNCTION__ . " " . "Error: " . $this->parms{'type'} . $this->parms{'provider'} . "Provider must implement validateCredential()");
48  }
49 
50  if (!$this->provider->validateCredential($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'])) {
52  }
53 
54  if (!$this->freedomUserExists($_SERVER['PHP_AUTH_USER'])) {
55  if (!$this->tryInitializeUser($_SERVER['PHP_AUTH_USER'])) {
57  }
58  }
59 
60  $session = $this->getAuthSession();
61  $session->register('username', $this->getAuthUser());
62  $session->setuid($this->getAuthUser());
64  }
65 
66  public function checkAuthorization($opt)
67  {
68  return TRUE;
69  }
70 
71  public function askAuthentication($args)
72  {
73  header('HTTP/1.1 401 Authentication Required');
74  header('WWW-Authenticate: Basic realm="' . $this->parms{'realm'} . '"');
75  header('Connection: close');
76  return TRUE;
77  }
78 
79  public function getAuthUser()
80  {
81  return isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : null;
82  }
83 
84  public function getAuthPw()
85  {
86  return isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : null;
87  }
88 
89  public function logout($redir_uri = '')
90  {
91  setcookie('logout', 'true', 0, null, null, null, true);
92 
93  if ($redir_uri == '') {
94  $pUri = parse_url($_SERVER['REQUEST_URI']);
95  if (preg_match(':(?P<path>.*/)[^/]*$:', $pUri['path'], $m)) {
96  $redir_uri = $m['path'];
97  }
98  }
99  header('Location: ' . $redir_uri);
100  return TRUE;
101  }
102 
103  public function setSessionVar($name, $value)
104  {
105  $session = $this->getAuthSession();
106  $session->register($name, $value);
107  return $session->read($name);
108  }
109  public function getSessionVar($name)
110  {
111  $session = $this->getAuthSession();
112  return $session->read($name);
113  }
114  /**
115  *
116  */
117  public function getAuthSession()
118  {
119  if (!$this->auth_session) {
120  $this->auth_session = new Session(Session::PARAMNAME, false);
121 
122  $this->auth_session->Set();
123  }
124  return $this->auth_session;
125  }
126 }
if(substr($wsh, 0, 1)!= '/') $args
tryInitializeUser($username)
global $_SERVER
static freedomUserExists($username)
const PARAMNAME
$value
← centre documentaire © anakeen