rendered paste body<?php
class ClassIdentify {
// class v2
// methods:
// row => Read Or Write
var $duree_de_vie_r; // 6 mois
var $duree_de_vie_w; // 3 hueres
var $statut = array();
var $membres = array(); var $membres_meta = array(); var $membres_test = array();
function ClassIdentify(){
$this->duree_de_vie_r = 3600 * 24 * 30 * 6; // 6 mois
$this->duree_de_vie_w = 3600 * 10; // 10 heures
}
function genAuthKey($idmembre,$row){
$authkey = md5(time()+rand(0,100));
while(mysql_num_rows(query('select idmembre from /DBTPREFIX/tempident where authkey="'.$authkey.'"'))){
$authkey = md5(time()+rand(0,100));
}
$exptime = ($row == 'read') ? (mytime() + $this->duree_de_vie_r) : (mytime() + $this->duree_de_vie_w);
if(!mysql_num_rows($q = query("select * from /DBTPREFIX/tempident where idmembre='".$idmembre."' and row='".$row."'")))
query("insert into /DBTPREFIX/tempident (idmembre,authkey,timestamp,row)
values ('".$idmembre."','".$authkey."','".$exptime."','".$row."')");
else {
$f = mysql_fetch_object($q);
$authkey = $f->authkey;
query("update /DBTPREFIX/tempident set timestamp='".$exptime."' where idmembre='".$idmembre."' and row='".$row."'");
}
return array($authkey,$exptime);
}
function register($table,$id,$auth,$row,$check=true){ global $DOMAIN;
if($check === true)
$retour = $this->check($table,$id,$auth,true);
if($check === true && $retour != "AuthValid"){
return $retour;
} else {
list($ak,$exptime) = $this->genAuthKey($id,$row);
$extn = ($row == 'read') ? 'r' : 'w';
if($row == 'read')// || !isset($_COOKIE[$table.'_id']) || empty($_COOKIE[$table.'_id']))
setcookie($table.'_id',$id,$exptime,"/",$DOMAIN);
setcookie($table.'_auth'.$extn,$ak,$exptime,"/",$DOMAIN);
if(isset($retour))
return $retour;
}
}
function check($table,$id,$auth,$test=false,$checkpasswd=false){
// NoCookies => Les cookies ne sont pas présents
// NoIDs => Les identifiants ne sont pas présents
// InvalidId => Identifiants inexistants ou invalides
// UnactivatedAccount => Compte non activé
// BlockedAccount => Compte bloqué pour raisons diverses
// AuthValid => User valide (avec $ID), tout va bien :-)
if($checkpasswd === true){
if(!$id || !$auth)
return "NoCookies";
if(!mysql_num_rows($q = query('select * from /DBTPREFIX/'.$table.' where
pseudo="'.$id.'" and motdepasse=MD5("'.$auth.'")')))
return "InvalidId";
else
$ligne2 = mysql_fetch_object($q);
} else {
if(!$id || !$auth)
return "NoCookies";
if(mysql_num_rows($q=query('select timestamp from /DBTPREFIX/tempident where authkey="'.$auth.'" and idmembre="'.$id.'"'))){
$ligne = mysql_fetch_object($q);
$ligne2 = mysql_fetch_object(query('select * from /DBTPREFIX/'.$table.' where id="'.$id.'"')); // err si pas de ligne avec cet id, mais ça sera tant pis pour la personne concernée
$statut = $ligne2->statut;
} else {
return "InvalidId";
}
}
if($ligne2->statut=='0')
return "UnactivatedAccount";
elseif($ligne2->statut=='2')
return "BlockedAccount"; // à changer si nécessaire
elseif($ligne2->statut=='1'){
if($test === false){
$this->{$table.'_meta'}['valid'] = true;
$this->{$table.'_meta'}['statut'] = "AuthValid";
} elseif($test === true){
$this->{$table.'_test'}['valid'] = true;
$this->{$table.'_test'}['fetch'] = $ligne2;
}
$this->$table = $ligne2;
return "AuthValid";
}
}
function identify($table,$row='write'){
global $CAHTML;
//$this->{$table.'_meta'}['valid'] = false; // pour pas déclencher de notice lors du test final
$id = (isset($_COOKIE[$table.'_id'])) ? $_COOKIE[$table.'_id'] : '';
$auth = ($row == 'read' && isset($_COOKIE[$table.'_authr'])) ? $_COOKIE[$table.'_authr'] : (($row == 'write' && isset($_COOKIE[$table.'_authw'])) ? $_COOKIE[$table.'_authw'] : '');
// $retour = $this->check($table,$id,$auth,false);
$retour = $this->register($table,$id,$auth,$row,true);
//echo $retour;
if($retour != 'AuthValid' && $row == 'write'){
$_SESSION['redirectTo'] = $_SERVER["REQUEST_URI"];
if($retour == 'NoCookies')
header('Location: '.$CAHTML.$table.'/identification.php?nosaka=1'); // 茶菓 [さか] /(n) gateaux/
else
header('Location: '.$CAHTML.$table.'/erreur_d_identification.php?errid='.$retour);
exit; // ce exit est inutile, donc indispensable
}
}
function logout($table){ global $DOMAIN;
setcookie($table.'_id','false',mytime()-1,"/",$DOMAIN);
setcookie($table.'_auth','false',mytime()-1,"/",$DOMAIN);
}
}
?>