game/includes/onyx/bdd.class.php

292 lines
6.4 KiB
PHP

<?php
/*
/!\ erreur désactivée ligne 54 par Némunaire, a corriger :
Warning: mysql_close(): 17 is not a valid MySQL-Link resource in ...\onyx\bdd.class.php on line 54
Modifié ligne 34 pour avoir un affichage du site en mode mini en cas de crash du serveur de base de données (avec Hfr, ça arrive de temps en temps :D)
*/
class bdd
{
private $session;
private $reponse;
var $database;
var $num_rows;
function connexion($h=NULL,$u=NULL,$p=NULL,$db=NULL)
{
if($this->session)
{
$this->deconnexion();
}
global $var___mysql, $config;
$u = empty($u)?$var___mysql['login']:$u;
$p = empty($p)?$var___mysql['password']:$p;
$h = empty($h)?$config['db_host']:$h;
@$this->session = mysql_connect($h,$u,$p) or require('secours.php');
if (!$this->session)
{
die('Connexion a la base de donnée impossible.');
}
mysql_query('SET CHARACTER SET "utf8"',$this->session);
if(!empty($db))
{
$var = mysql_select_db($db,$this->session);
if($var) $this->database = $db;
}
}
function deconnexion()
{
if($this->session)
{
@$var = mysql_close($this->session);
$this->session = FALSE;
return $var;
}
else return FALSE;
}
function erreur($flag=TRUE)
{
if($this->session)
{
$var = mysql_error($this->session);
if($flag) echo $var;
return $var;
}
else return FALSE;
}
function db($db=NULL)
{
if($this->session)
{
global $config;
$db = empty($db)?$config['db_name']:$db;
$var = mysql_select_db($db,$this->session);
if($var) $this->database = $db;
return $var;
}
else return FALSE;
}
function escape(&$var)
{
if($this->session)
{
$var = mysql_real_escape_string($var,$this->session);
return $var;
}
else return FALSE;
}
function query($q)
{
if($this->session)
{
$this->reponse = mysql_query($q,$this->session);
if(!$this->reponse)
{
echo("Requete à la base de donnée invalide");
return FALSE;
}
$this->num_rows = @mysql_num_rows($this->reponse);
if($this->num_rows == 0)
{
return NULL;
}
elseif($this->num_rows >= 1)
{
for($i=0; $var = mysql_fetch_assoc($this->reponse); $i++)
{
$sortie[$i] = $var;
}
return $sortie;
}
else return FALSE;
}
else return FALSE;
}
function unique_query($q)
{
if($this->session)
{
$this->reponse = mysql_query($q,$this->session);
if(!$this->reponse)
{
echo("Requete à la base de donnée invalide");
return FALSE;
}
$this->num_rows = @mysql_num_rows($this->reponse);
if($this->num_rows == 0 || $this->num_rows > 1)
{
return NULL;
}
elseif($this->num_rows == 1)
{
return mysql_fetch_assoc($this->reponse);
}
else return FALSE;
}
else return FALSE;
}
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;
}
function check($table,$cond)
{
if($this->session && !empty($this->database))
{
$hashcheck = $this->unique_query("SELECT hash FROM $table WHERE $cond");
if($this->num_rows != 1) return FALSE;
$hashcheck = $hashcheck['hash'];
$columns = $this->query("SHOW COLUMNS FROM $table");
$i = 0;
$j = 0;
foreach($columns as $key => $value)
{
if($value['Field'] == 'hash' || $value['Field'] == 'active')
{
$i++;
}
else
{
$array[$j] = $value['Field'];
$j++;
}
}
if($i != 2) return FALSE;
$array = implode(",", $array);
$var = $this->unique_query("SELECT $array FROM $table WHERE $cond");
if($this->num_rows == 1)
{
$var = implode('',$var);
$hash = hash_var($var);
if($hashcheck === $hash)
{
return TRUE;
}
else;
{
$this->query("UPDATE $table SET active='0' WHERE $cond");
return FALSE;
}
}
else return FALSE;
}
else return FALSE;
}
function hash($table,$cond)
{
if($this->session && !empty($this->database))
{
$columns = $this->query("SHOW COLUMNS FROM $table");
$i = 0;
$j = 0;
foreach($columns as $key => $value)
{
if($value['Field'] == 'hash' || $value['Field'] == 'active')
{
$i++;
}
else
{
$array[$j] = $value['Field'];
$j++;
}
}
if($i != 2) return FALSE;
$array = implode(",", $array);
$var = $this->unique_query("SELECT $array FROM $table WHERE $cond");
if($this->num_rows == 1)
{
$var = implode('',$var);
$hash = hash_var($var);
$r = $this->query("UPDATE $table SET active='1', hash='$hash' WHERE $cond");
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;
}
}
?>