2008-11-17 11:00:00 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class bdd
|
|
|
|
{
|
|
|
|
private $session;
|
2008-11-04 11:00:00 +00:00
|
|
|
|
2008-11-17 11:00:00 +00:00
|
|
|
private $reponse;
|
2008-11-04 11:00:00 +00:00
|
|
|
|
2008-11-17 11:00:00 +00:00
|
|
|
var $database;
|
2008-11-04 11:00:00 +00:00
|
|
|
|
2008-11-17 11:00:00 +00:00
|
|
|
var $num_rows;
|
2008-11-04 11:00:00 +00:00
|
|
|
|
|
|
|
var $nodb;
|
2008-11-17 11:00:00 +00:00
|
|
|
|
2008-11-04 11:00:00 +00:00
|
|
|
function connexion($db=NULL,$h=NULL,$u=NULL,$p=NULL)
|
2008-11-17 11:00:00 +00:00
|
|
|
{
|
|
|
|
if($this->session)
|
|
|
|
{
|
|
|
|
$this->deconnexion();
|
|
|
|
}
|
2008-11-04 11:00:00 +00:00
|
|
|
|
|
|
|
global $var___db,$config;
|
|
|
|
|
|
|
|
$u = empty($u)?$var___db['mysql']['login']:$u;
|
|
|
|
|
|
|
|
$p = empty($p)?$var___db['mysql']['password']:$p;
|
|
|
|
|
|
|
|
$h = empty($h)?$var___db['mysql']['host']:$h;
|
|
|
|
|
|
|
|
$this->session = @mysql_connect($h,$u,$p);
|
|
|
|
|
2008-11-17 11:00:00 +00:00
|
|
|
if (!$this->session)
|
|
|
|
{
|
2008-11-04 11:00:00 +00:00
|
|
|
if($config['db_log'])
|
|
|
|
{
|
|
|
|
file_log('Erreur Mysql: Connexion à la base de donnée impossible.',2);
|
|
|
|
if(!empty($this->nodb) && function_exists($this->nodb))
|
|
|
|
{
|
|
|
|
call_user_func($this->nodb);
|
|
|
|
}
|
|
|
|
elseif(array_key_exists('no_db',$config) && function_exists($config['no_db']))
|
|
|
|
{
|
|
|
|
call_user_func($config['no_db']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FALSE;
|
2008-11-17 11:00:00 +00:00
|
|
|
}
|
2008-11-04 11:00:00 +00:00
|
|
|
|
2008-11-17 11:00:00 +00:00
|
|
|
mysql_query('SET CHARACTER SET "utf8"',$this->session);
|
2008-11-04 11:00:00 +00:00
|
|
|
|
|
|
|
$db = empty($db)?$var___db['mysql']['db']:$db;
|
|
|
|
|
|
|
|
$dbl = mysql_select_db($db,$this->session);
|
|
|
|
if($dbl) $this->database = $db;
|
2008-11-17 11:00:00 +00:00
|
|
|
}
|
2008-11-04 11:00:00 +00:00
|
|
|
|
2008-11-17 11:00:00 +00:00
|
|
|
function deconnexion()
|
|
|
|
{
|
|
|
|
if($this->session)
|
|
|
|
{
|
2008-11-04 11:00:00 +00:00
|
|
|
$var = @mysql_close($this->session);
|
2008-11-17 11:00:00 +00:00
|
|
|
$this->session = FALSE;
|
|
|
|
return $var;
|
|
|
|
}
|
|
|
|
else return FALSE;
|
|
|
|
}
|
2008-11-04 11:00:00 +00:00
|
|
|
|
2008-11-17 11:00:00 +00:00
|
|
|
function erreur($flag=TRUE)
|
|
|
|
{
|
|
|
|
if($this->session)
|
|
|
|
{
|
|
|
|
$var = mysql_error($this->session);
|
|
|
|
if($flag) echo $var;
|
2008-11-04 11:00:00 +00:00
|
|
|
return($var);
|
2008-11-17 11:00:00 +00:00
|
|
|
}
|
|
|
|
else return FALSE;
|
|
|
|
}
|
|
|
|
function db($db=NULL)
|
|
|
|
{
|
|
|
|
if($this->session)
|
|
|
|
{
|
2008-11-04 11:00:00 +00:00
|
|
|
global $var___db;
|
|
|
|
$db = empty($db)?$var___db['db']:$db;
|
|
|
|
|
2008-11-17 11:00:00 +00:00
|
|
|
$var = mysql_select_db($db,$this->session);
|
|
|
|
if($var) $this->database = $db;
|
|
|
|
return $var;
|
|
|
|
}
|
|
|
|
else return FALSE;
|
|
|
|
}
|
2008-11-04 11:00:00 +00:00
|
|
|
|
2008-11-17 11:00:00 +00:00
|
|
|
function escape(&$var)
|
|
|
|
{
|
|
|
|
if($this->session)
|
|
|
|
{
|
|
|
|
$var = mysql_real_escape_string($var,$this->session);
|
|
|
|
return $var;
|
|
|
|
}
|
|
|
|
else return FALSE;
|
|
|
|
}
|
2008-11-04 11:00:00 +00:00
|
|
|
|
2008-11-17 11:00:00 +00:00
|
|
|
function query($q)
|
|
|
|
{
|
|
|
|
if($this->session)
|
|
|
|
{
|
|
|
|
$this->reponse = mysql_query($q,$this->session);
|
2008-11-04 11:00:00 +00:00
|
|
|
|
|
|
|
global $config;
|
|
|
|
|
2008-11-17 11:00:00 +00:00
|
|
|
if(!$this->reponse)
|
|
|
|
{
|
2008-11-04 11:00:00 +00:00
|
|
|
if($config['db_log'] == '1')
|
|
|
|
{
|
|
|
|
file_log('Erreur Mysql: " '.$this->erreur(FALSE).' ", avec la requète: { '.$q.' }.',1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
echo("Requete à la base de donnée invalide");
|
|
|
|
}
|
2008-11-17 11:00:00 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2008-11-04 11:00:00 +00:00
|
|
|
|
2008-11-17 11:00:00 +00:00
|
|
|
$this->num_rows = @mysql_num_rows($this->reponse);
|
2008-11-04 11:00:00 +00:00
|
|
|
|
2008-11-17 11:00:00 +00:00
|
|
|
if($this->num_rows == 0)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-11-04 11:00:00 +00:00
|
|
|
|
2008-11-17 11:00:00 +00:00
|
|
|
elseif($this->num_rows >= 1)
|
|
|
|
{
|
|
|
|
for($i=0; $var = mysql_fetch_assoc($this->reponse); $i++)
|
|
|
|
{
|
|
|
|
$sortie[$i] = $var;
|
|
|
|
}
|
|
|
|
return $sortie;
|
|
|
|
}
|
2008-11-04 11:00:00 +00:00
|
|
|
|
2008-11-17 11:00:00 +00:00
|
|
|
else return FALSE;
|
|
|
|
}
|
|
|
|
else return FALSE;
|
|
|
|
}
|
2008-11-04 11:00:00 +00:00
|
|
|
|
2008-11-17 11:00:00 +00:00
|
|
|
function unique_query($q)
|
|
|
|
{
|
|
|
|
if($this->session)
|
|
|
|
{
|
|
|
|
$this->reponse = mysql_query($q,$this->session);
|
2008-11-04 11:00:00 +00:00
|
|
|
|
|
|
|
global $config;
|
|
|
|
|
2008-11-17 11:00:00 +00:00
|
|
|
if(!$this->reponse)
|
|
|
|
{
|
2008-11-04 11:00:00 +00:00
|
|
|
if($config['db_log'] == '1')
|
|
|
|
{
|
|
|
|
file_log('Erreur Mysql: " '.$this->erreur(FALSE).' ", avec la requète: { '.$q.' }.',1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
echo("Requete à la base de donnée invalide");
|
|
|
|
}
|
2008-11-17 11:00:00 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2008-11-04 11:00:00 +00:00
|
|
|
|
2008-11-17 11:00:00 +00:00
|
|
|
$this->num_rows = @mysql_num_rows($this->reponse);
|
2008-11-04 11:00:00 +00:00
|
|
|
|
2008-11-17 11:00:00 +00:00
|
|
|
if($this->num_rows == 0 || $this->num_rows > 1)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-11-04 11:00:00 +00:00
|
|
|
|
2008-11-17 11:00:00 +00:00
|
|
|
elseif($this->num_rows == 1)
|
|
|
|
{
|
|
|
|
return mysql_fetch_assoc($this->reponse);
|
|
|
|
}
|
2008-11-04 11:00:00 +00:00
|
|
|
|
2008-11-17 11:00:00 +00:00
|
|
|
else return FALSE;
|
|
|
|
}
|
|
|
|
else return FALSE;
|
|
|
|
}
|
2008-11-04 11:00:00 +00:00
|
|
|
|
2008-11-17 11:00:00 +00:00
|
|
|
function affected()
|
|
|
|
{
|
|
|
|
if($this->session)
|
|
|
|
{
|
|
|
|
$affected = mysql_affected_rows($this->session);
|
|
|
|
if($affected == 0)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
elseif($affected >= 1)
|
|
|
|
{
|
|
|
|
return $affected;
|
|
|
|
}
|
|
|
|
else return FALSE;
|
|
|
|
}
|
|
|
|
else return FALSE;
|
|
|
|
}
|
2008-11-04 11:00:00 +00:00
|
|
|
|
2008-11-17 11:00:00 +00:00
|
|
|
function check($table,$cond)
|
|
|
|
{
|
|
|
|
if($this->session && !empty($this->database))
|
|
|
|
{
|
|
|
|
$hashcheck = $this->unique_query("SELECT hash FROM $table WHERE $cond");
|
2008-11-04 11:00:00 +00:00
|
|
|
|
2008-11-17 11:00:00 +00:00
|
|
|
if($this->num_rows != 1) return FALSE;
|
2008-11-04 11:00:00 +00:00
|
|
|
|
2008-11-17 11:00:00 +00:00
|
|
|
$hashcheck = $hashcheck['hash'];
|
2008-11-04 11:00:00 +00:00
|
|
|
|
2008-11-17 11:00:00 +00:00
|
|
|
$columns = $this->query("SHOW COLUMNS FROM $table");
|
2008-11-04 11:00:00 +00:00
|
|
|
|
2008-11-17 11:00:00 +00:00
|
|
|
$i = 0;
|
|
|
|
$j = 0;
|
2008-11-04 11:00:00 +00:00
|
|
|
|
2008-11-17 11:00:00 +00:00
|
|
|
foreach($columns as $key => $value)
|
|
|
|
{
|
|
|
|
if($value['Field'] == 'hash' || $value['Field'] == 'active')
|
|
|
|
{
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$array[$j] = $value['Field'];
|
|
|
|
$j++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if($i != 2) return FALSE;
|
2008-11-04 11:00:00 +00:00
|
|
|
|
2008-11-17 11:00:00 +00:00
|
|
|
$array = implode(",", $array);
|
2008-11-04 11:00:00 +00:00
|
|
|
|
2008-11-17 11:00:00 +00:00
|
|
|
$var = $this->unique_query("SELECT $array FROM $table WHERE $cond");
|
2008-11-04 11:00:00 +00:00
|
|
|
|
2008-11-17 11:00:00 +00:00
|
|
|
if($this->num_rows == 1)
|
|
|
|
{
|
2008-11-04 11:00:00 +00:00
|
|
|
|
2008-11-17 11:00:00 +00:00
|
|
|
$var = implode('',$var);
|
2008-11-04 11:00:00 +00:00
|
|
|
|
2008-11-17 11:00:00 +00:00
|
|
|
$hash = hash_var($var);
|
2008-11-04 11:00:00 +00:00
|
|
|
|
2008-11-17 11:00:00 +00:00
|
|
|
if($hashcheck === $hash)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else;
|
|
|
|
{
|
|
|
|
$this->query("UPDATE $table SET active='0' WHERE $cond");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else return FALSE;
|
|
|
|
}
|
|
|
|
else return FALSE;
|
|
|
|
}
|
2008-11-04 11:00:00 +00:00
|
|
|
|
2008-11-17 11:00:00 +00:00
|
|
|
function hash($table,$cond)
|
|
|
|
{
|
|
|
|
if($this->session && !empty($this->database))
|
|
|
|
{
|
|
|
|
$columns = $this->query("SHOW COLUMNS FROM $table");
|
2008-11-04 11:00:00 +00:00
|
|
|
|
2008-11-17 11:00:00 +00:00
|
|
|
$i = 0;
|
|
|
|
$j = 0;
|
2008-11-04 11:00:00 +00:00
|
|
|
|
2008-11-17 11:00:00 +00:00
|
|
|
foreach($columns as $key => $value)
|
|
|
|
{
|
|
|
|
if($value['Field'] == 'hash' || $value['Field'] == 'active')
|
|
|
|
{
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$array[$j] = $value['Field'];
|
|
|
|
$j++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if($i != 2) return FALSE;
|
2008-11-04 11:00:00 +00:00
|
|
|
|
2008-11-17 11:00:00 +00:00
|
|
|
$array = implode(",", $array);
|
2008-11-04 11:00:00 +00:00
|
|
|
|
2008-11-17 11:00:00 +00:00
|
|
|
$var = $this->unique_query("SELECT $array FROM $table WHERE $cond");
|
2008-11-04 11:00:00 +00:00
|
|
|
|
2008-11-17 11:00:00 +00:00
|
|
|
if($this->num_rows == 1)
|
|
|
|
{
|
2008-11-04 11:00:00 +00:00
|
|
|
|
2008-11-17 11:00:00 +00:00
|
|
|
$var = implode('',$var);
|
2008-11-04 11:00:00 +00:00
|
|
|
|
2008-11-17 11:00:00 +00:00
|
|
|
$hash = hash_var($var);
|
2008-11-04 11:00:00 +00:00
|
|
|
|
2008-11-17 11:00:00 +00:00
|
|
|
$r = $this->query("UPDATE $table SET active='1', hash='$hash' WHERE $cond");
|
2008-11-04 11:00:00 +00:00
|
|
|
|
2008-11-17 11:00:00 +00:00
|
|
|
return $this->affected();
|
|
|
|
}
|
|
|
|
else return FALSE;
|
|
|
|
}
|
|
|
|
else return FALSE;
|
|
|
|
}
|
|
|
|
function control($table)
|
|
|
|
{
|
|
|
|
if($this->session && !empty($this->database))
|
|
|
|
{
|
|
|
|
$xid = $this->query("SELECT xid FROM $table");
|
|
|
|
if($this->num_rows != 0)
|
|
|
|
{
|
|
|
|
foreach($xid as $key => $value)
|
|
|
|
{
|
|
|
|
$vxid = $value['xid'];
|
|
|
|
$var = $this->check($table,"xid = '$vxid'");
|
|
|
|
if(!$var) return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else return FALSE;
|
|
|
|
}
|
|
|
|
else return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|