*/ /** Takes the email and the password of the user */ class authparams { public $email = null; public $password = null; /** Parse the different authentication processes to found the email/password of the user. If non is found, return "anonymous", "anonymous" */ public function __construct ($authprocesses=array("session","post")) { if (php_sapi_name () === "cli") { $this->email = "cli"; $this->password = ""; } else { foreach ($authprocesses as $authprocess) { try { $res = $this->$authprocess(); $this->email = $res["email"]; $this->password = $res["password"]; break; } catch (Exception $e) { $this->email = "anonymous"; $this->password = "anonymous"; } } } } /** Get informations from $POST variables */ public function post() { if (!isset ($_POST["email"]) || !isset ($_POST["password"])) throw new Exception ("No POST provided", 401); return array ("email"=>$_POST["email"], "password"=>$_POST["password"]); } /** Get informations from previous recorded session */ public function session() { if (!isset ($_SESSION)) throw new Exception ("No session previously opened", 401); if (!isset ($_SESSION["domframework"]["auth"]["email"]) || !isset ($_SESSION["domframework"]["auth"]["password"])) throw new Exception ("No previous email in session", 401); return array ("email"=>$_SESSION["domframework"]["auth"]["email"], "password"=>$_SESSION["domframework"]["auth"]["password"]); } /** Get informations from a HTTP authentication */ public function http() { $realm = dgettext("domframework", "Restricted access"); if (!isset($_SERVER['PHP_AUTH_USER'])) { header("WWW-Authenticate: Basic realm=\"$realm\""); header("HTTP/1.0 401 Unauthorized"); die ($realm); } else { return array ("email"=>$_SERVER["PHP_AUTH_USER"], "password"=>$_SERVER["PHP_AUTH_PW"]); } } }