Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.htmlAuthenticator.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
5  * @package FDL
6 */
7 /**
8  * htmlAuthenticator class
9  *
10  * This class provides methods for HTML form based authentication
11  *
12  * @author Anakeen 2009
13  * @version $Id: Class.htmlAuthenticator.php,v 1.8 2009/01/16 13:33:00 jerome Exp $
14  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
15  * @package FDL
16  * @subpackage
17  */
18 /**
19  */
20 include_once ('WHAT/Class.Authenticator.php');
21 
23 {
24 
25  public $auth_session = null;
26  /*
27  * Store the current authenticating user
28  */
29  private $username = '';
30  /**
31  **
32  **
33  *
34  */
35  public function checkAuthentication()
36  {
37  $session = $this->getAuthSession();
38 
39  $this->username = $session->read('username');
40  if ($this->username != "") return Authenticator::AUTH_OK;
41 
42  if (!array_key_exists($this->parms{'username'}, $_POST)) return Authenticator::AUTH_ASK;
43  if (!array_key_exists($this->parms{'password'}, $_POST)) return Authenticator::AUTH_ASK;
44 
45  $this->username = getHttpVars($this->parms{'username'});
46  if (is_callable(array(
47  $this->provider,
48  'validateCredential'
49  ))) {
50  if (!$this->provider->validateCredential(getHttpVars($this->parms{'username'}), getHttpVars($this->parms{'password'}))) {
52  }
53 
54  if (!$this->freedomUserExists(getHttpVars($this->parms{'username'}))) {
55  if (!$this->tryInitializeUser(getHttpVars($this->parms{'username'}))) {
57  }
58  }
59  $session->register('username', getHttpVars($this->parms{'username'}));
60  $session->setuid(getHttpVars($this->parms{'username'}));
62  }
63 
64  error_log(__CLASS__ . "::" . __FUNCTION__ . " " . "Error: " . get_class($this->provider) . " must implement function validateCredential()");
66  }
67  /**
68  * retrieve authentification session
69  * @return Session the session object
70  */
71  public function getAuthSession()
72  {
73  if (!$this->auth_session) {
74  include_once ('WHAT/Class.Session.php');
75  $this->auth_session = new Session($this->parms{'cookie'});
76  if (array_key_exists($this->parms{'cookie'}, $_COOKIE)) {
77  $this->auth_session->Set($_COOKIE[$this->parms{'cookie'}]);
78  } else {
79  $this->auth_session->Set();
80  }
81  }
82  return $this->auth_session;
83  }
84  /**
85  **
86  **
87  *
88  */
89  function checkAuthorization($opt)
90  {
91  if (is_callable(array(
92  $this->provider,
93  'validateAuthorization'
94  ))) {
95  return $this->provider->validateAuthorization($opt);
96  }
97  return TRUE;
98  }
99  /**
100  **
101  **
102  *
103  */
104  public function askAuthentication($args = array())
105  {
106 
107  $parsed_referer = parse_url($_SERVER['HTTP_REFERER']);
108 
109  $referer_uri = "";
110  if ($parsed_referer['path'] != "") {
111  $referer_uri.= $parsed_referer['path'];
112  }
113  if ($parsed_referer['query'] != "") {
114  $referer_uri.= "?" . $parsed_referer['query'];
115  }
116  if ($parsed_referer['fragment'] != "") {
117  $referer_uri.= "#" . $parsed_referer['fragment'];
118  }
119  $session = $this->getAuthSession();
120  /* Force removal of username if it already exists on the session */
121  $session->register('username', '');
122  $session->setuid(ANONYMOUS_ID);
123  // error_log("referer_uri = ".$referer_uri." / REQUEST_URI = ".$_SERVER['REQUEST_URI']);
124  if ($referer_uri == "") {
125  // error_log("Setting fromuri = ".$_SERVER['REQUEST_URI']);
126  $session->register('fromuri', $_SERVER['REQUEST_URI']);
127  } else if ($session->read('fromuri') == "" && $referer_uri != $_SERVER['REQUEST_URI']) {
128  // error_log("Setting fromuri = ".$_SERVER['REQUEST_URI']);
129  $session->register('fromuri', $_SERVER['REQUEST_URI']);
130  }
131 
132  if (array_key_exists('authurl', $this->parms)) {
133  $sargs = '';
134  foreach ($args as $k => $v) $sargs.= sprintf("&%s=%s", $k, urlencode($v));
135 
136  $location = '';
137  if (substr($this->parms{'authurl'}, 0, 9) == "guest.php") {
138  $dirname = dirname($_SERVER["SCRIPT_NAME"]);
139  $location = str_replace('//', '/', $dirname . '/' . $this->parms{'authurl'});
140  if (strpos($location, '?') === false && $sargs != '') {
141  $sargs = sprintf('?%s', $sargs);
142  }
143  } else {
144  $location = $this->parms{'authurl'};
145  if (strpos($location, '?') === false && $sargs != '') {
146  $sargs = sprintf('?%s', $sargs);
147  }
148  }
149 
150  header(sprintf('Location: %s%s', $location, $sargs));
151  return TRUE;
152  }
153 
154  error_log(__CLASS__ . "::" . __FUNCTION__ . " " . "Error: no authurl of askAuthentication() method defined for " . $this->parms{'type'} . $this->parms{'provider'} . "Provider");
155  return FALSE;
156  }
157  /**
158  **
159  **
160  *
161  */
162  public function getAuthUser()
163  {
164  $session_auth = $this->getAuthSession();
165  $username = $session_auth->read('username');
166  if ($username != '') {
167  return $username;
168  }
169  return $this->username;
170  }
171  /**
172  **
173  **
174  *
175  */
176  public function getAuthPw()
177  {
178  return null;
179  }
180  /**
181  **
182  **
183  *
184  */
185  public function logout($redir_uri = '')
186  {
187  $session_auth = $this->getAuthSession();
188  if (array_key_exists($this->parms{'cookie'}, $_COOKIE)) {
189  // error_log("Closing auth session for cookie : ".$this->parms{'cookie'});
190  $session_auth->close();
191  }
192  if ($redir_uri == "") {
193  if (array_key_exists('authurl', $this->parms)) {
194  header('Location: ' . $this->parms['authurl']);
195  return TRUE;
196  }
197  $redir_uri = GetParam("CORE_BASEURL");
198  }
199  header('Location: ' . $redir_uri);
200  return TRUE;
201  }
202  /**
203  **
204  **
205  *
206  */
207  public function setSessionVar($name, $value)
208  {
209  $session_auth = $this->getAuthSession();
210  $session_auth->register($name, $value);
211 
212  return $session_auth->read($name);
213  }
214  /**
215  **
216  **
217  *
218  */
219  public function getSessionVar($name)
220  {
221  $session_auth = $this->getAuthSession();
222  return $session_auth->read($name);
223  }
224 }
225 ?>
← centre documentaire © anakeen - published under CC License - Dynacase