Core  3.2
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  * @package FDL
5 */
6 /**
7  * htmlAuthenticator class
8  *
9  * This class provides methods for HTML form based authentication
10  *
11  * @author Anakeen
12  * @version $Id: Class.htmlAuthenticator.php,v 1.8 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  public $auth_session = null;
24  /*
25  * Store the current authenticating user
26  */
27  private $username = '';
28  /**
29  **
30  **
31  *
32  */
33  public function checkAuthentication()
34  {
35  $session = $this->getAuthSession();
36  $this->username = $session->read('username');
37  if ($this->username != "") return Authenticator::AUTH_OK;
38 
39  if (!array_key_exists($this->parms['username'], $_POST)) return Authenticator::AUTH_ASK;
40  if (!array_key_exists($this->parms['password'], $_POST)) return Authenticator::AUTH_ASK;
41 
42  $this->username = getHttpVars($this->parms['username']);
43  if (is_callable(array(
44  $this->provider,
45  'validateCredential'
46  ))) {
47  if (!$this->provider->validateCredential(getHttpVars($this->parms['username']) , getHttpVars($this->parms{'password'}))) {
49  }
50 
51  if (!$this->freedomUserExists(getHttpVars($this->parms['username']))) {
52  if (!$this->tryInitializeUser(getHttpVars($this->parms['username']))) {
54  }
55  }
56  $session->register('username', getHttpVars($this->parms['username']));
57  $session->setuid(getHttpVars($this->parms['username']));
59  }
60 
61  error_log(__CLASS__ . "::" . __FUNCTION__ . " " . "Error: " . get_class($this->provider) . " must implement function validateCredential()");
63  }
64  /**
65  * retrieve authentication session
66  * @return Session the session object
67  */
68  public function getAuthSession()
69  {
70  if (!$this->auth_session) {
71  $this->auth_session = new Session(Session::PARAMNAME);
72  if (array_key_exists(Session::PARAMNAME, $_COOKIE)) {
73  $this->auth_session->Set($_COOKIE[Session::PARAMNAME]);
74  } else {
75  $this->auth_session->Set();
76  }
77  }
78 
79  return $this->auth_session;
80  }
81  /**
82  **
83  **
84  *
85  */
86  function checkAuthorization($opt)
87  {
88  if (is_callable(array(
89  $this->provider,
90  'validateAuthorization'
91  ))) {
92  return $this->provider->validateAuthorization($opt);
93  }
94  return TRUE;
95  }
96  /**
97  **
98  **
99  *
100  */
101  public function askAuthentication($args)
102  {
103  if (empty($args)) $args = array();
104  $session = $this->getAuthSession();
105  /* Force removal of username if it already exists on the session */
106  $session->register('username', '');
107  $session->setuid(Account::ANONYMOUS_ID);
108  if (!isset($args['redirect_uri'])) {
109  $args['redirect_uri'] = $_SERVER['REQUEST_URI'];
110  }
111 
112  header(sprintf('Location: %s', $this->getAuthUrl($args)));
113  return TRUE;
114  }
115  /**
116  * return url used to connect user
117  * @param array $extendedArg
118  * @throws Dcp\Exception
119  * @return string
120  */
121  public function getAuthUrl(array $extendedArg = array())
122  {
123  if (empty($this->parms['auth']['app'])) {
124  throw new \Dcp\Exception("Missing html/auth/app config.");
125  }
127  $location.= 'authent.php?app=' . $this->parms['auth']['app'];
128  if (!empty($this->parms['auth']['action'])) {
129  $location.= '&action=' . $this->parms['auth']['action'];
130  }
131  if (!empty($this->parms['auth']['args'])) {
132  $location.= '&' . $this->parms['auth']['args'];
133  }
134  $sargs = '';
135  foreach ($extendedArg as $k => $v) {
136  $sargs.= sprintf("&%s=%s", $k, urlencode($v));
137  }
138  return $location . $sargs;
139  }
140  /**
141  * ask authentication and redirect
142  * @param string $uri uri to redirect after connection
143  */
144  public function connectTo($uri)
145  {
146  $location = sprintf('%s&redirect_uri=%s', $this->getAuthUrl() , urlencode($uri));
147  header(sprintf('Location: %s', $location));
148  exit(0);
149  }
150  /**
151  **
152  **
153  *
154  */
155  public function getAuthUser()
156  {
157  $session_auth = $this->getAuthSession();
158  $username = $session_auth->read('username');
159  if ($username != '') {
160  return $username;
161  }
162  return $this->username;
163  }
164  /**
165  **
166  **
167  *
168  */
169  public function getAuthPw()
170  {
171  return null;
172  }
173  /**
174  **
175  **
176  *
177  */
178  public function logout($redir_uri = '')
179  {
180  include_once ('WHAT/Class.Session.php');
181  $session_auth = $this->getAuthSession();
182  if (array_key_exists(Session::PARAMNAME, $_COOKIE)) {
183  $session_auth->close();
184  }
185  if ($redir_uri == "") {
186  if (isset($this->parms['auth']['app'])) {
187  header('Location: ' . $this->getAuthUrl());
188  return TRUE;
189  }
190  $redir_uri = GetParam("CORE_BASEURL");
191  }
192  header('Location: ' . $redir_uri);
193  return TRUE;
194  }
195  /**
196  **
197  **
198  *
199  */
200  public function setSessionVar($name, $value)
201  {
202  $session_auth = $this->getAuthSession();
203  $session_auth->register($name, $value);
204 
205  return $session_auth->read($name);
206  }
207  /**
208  **
209  **
210  *
211  */
212  public function getSessionVar($name)
213  {
214  $session_auth = $this->getAuthSession();
215  return $session_auth->read($name);
216  }
217 
218  public function logon()
219  {
220  include_once ('WHAT/Class.ActionRouter.php');
221  include_once ('WHAT/Class.Account.php');
222 
223  $app = $this->getAuthApp();
224  if ($app === false || $app == '') {
225  throw new \Dcp\Exception("Missing or empty auth app definition.");
226  }
227 
228  $account = new Account();
229  if ($account->setLoginName("anonymous") === false) {
230  throw new \Dcp\Exception(sprintf("anonymous account not found."));
231  }
233 
234  $allowList = array(
235  array(
236  'app' => 'AUTHENT'
237  ) ,
238  array(
239  'app' => 'CORE',
240  'action' => 'CORE_CSS'
241  )
242  );
243  $action = $actionRouter->getAction();
244  $app = $action->parent;
245  $allowed = false;
246  foreach ($allowList as $allow) {
247  if (isset($allow['app']) && $allow['app'] == $app->name) {
248  if (!isset($allow['action']) || $allow['action'] == $action->name) {
249  $allowed = true;
250  break;
251  }
252  }
253  }
254  if (!$allowed) {
255  throw new \Dcp\Exception(sprintf("Unauthorized app '%s' with action '%s' for authentication with '%s'.", $action->parent->name, $action->name, get_class($this)));
256  }
257 
258  $actionRouter->executeAction();
259  }
260 }
if(substr($wsh, 0, 1)!= '/') $args
global $action
global $_POST
Definition: chgpasswd.php:17
getAuthUrl(array $extendedArg=array())
const ANONYMOUS_ID
tryInitializeUser($username)
static getWebRootPath($default=false)
if($account->setLoginName("anonymous")===false) $actionRouter
Definition: guest.php:40
$app
if(!$img) $location
Definition: resizeimg.php:143
getHttpVars($name, $def="", $scope="all")
Definition: Lib.Http.php:124
global $_SERVER
switch($command) exit
Definition: checkVault.php:46
$account
Definition: guest.php:36
static freedomUserExists($username)
const PARAMNAME
$value
← centre documentaire © anakeen