<?php
/**
*
* @author Benjamin Gillissen <hide@address.com>
*
* **************************************************************
Copyright (C) 2009 Benjamin Gillissen
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details at:
http://www.gnu.org/copyleft/gpl.html
* **************************************************************
*/
class chal_basic extends realm {
private $chal, $ticket;
public function __construct($realm, $chal){
parent::__construct($realm);
$this->chal = $chal;
unset($realm, $chal);
}
public function get_chaldata(){ return FALSE; }
public function seticket($ticket){
$this->ticket = $ticket;
}
public function geticket(){ return $this->getcook(); }
public function precheck($chaldata){ return TRUE; }
public function sendform($err=NULL){
$this->delcook();
if ( NULL !== $err ){
die(langs::dico_translate($err, NULL, 'realm'));
}
if ( headers_sent($file, $line)) {
errors::raise('Basic-HTTP Authentification failure : output already started at '.$file.':'.$line, CORE_LOG_ALERT, 'REALM');
}
//if no ssl drop a warning about pass sent clear across network
$this->setcook($this->ticket);
header('WWW-Authenticate: Basic realm="'.$this->realm.'"');
header('HTTP/1.0 401 Unauthorized');
exit;
}
public function istrying(){
$bool = ( isset($_SERVER['PHP_AUTH_USER']) AND isset($_SERVER['PHP_AUTH_PW']) );
$basic = $this->getcook();
if ( FALSE === $bool ){
$this->delcook();
return FALSE;
}
return (FALSE !== $basic);
}
public function authenticate(){
$this->delcook();
return $this->authcheck($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']);
}
private function setcook($ticket){
$COOKRef = configs::get('realm', 'realm', Array($this->realm, 'cookname'));
$basic = cookies::read('basic', $COOKRef);
$basic[$this->realm] = $ticket;
cookies::set('basic', $basic, $COOKRef);
}
private function delcook(){
$COOKRef = configs::get('realm', 'realm', Array($this->realm, 'cookname'));
$basic = cookies::read('basic', $COOKRef);
unset($basic[$this->realm]);
cookies::set('basic', $basic, $COOKRef);
}
private function getcook(){
$COOKRef = configs::get('realm', 'realm', Array($this->realm, 'cookname'));
$basic = cookies::read('basic', $COOKRef);
if ( !isset($basic[$this->realm]) ){ return FALSE; }
return $basic[$this->realm];
}
}