195 lines
5.7 KiB
PHP
195 lines
5.7 KiB
PHP
<?php
|
|
if ( !isset($site) )
|
|
{
|
|
require_once( "config.inc.php" );
|
|
header( 'Location: ' . $accueil );
|
|
}
|
|
|
|
require_once "config.inc.php";
|
|
require_once "log.php";
|
|
|
|
$table_prefix = "";
|
|
class db
|
|
{
|
|
private $server = "db351634-avionpoeme.sql-pro.online.net";
|
|
private $port = 3306;
|
|
private $user = "db120904";
|
|
private $passwd = "AvionPoème*DB";
|
|
private $database = "db351634_avionpoeme";
|
|
public $connect;
|
|
public $result;
|
|
// avion
|
|
public $uid;
|
|
public $message;
|
|
public $expeMail;
|
|
public $expeKnown;
|
|
public $expeLang;
|
|
public $destLang;
|
|
public $startLat;
|
|
public $startLon;
|
|
public $startName;
|
|
public $startTime;
|
|
public $startTZ;
|
|
public $destMail;
|
|
public $destLat;
|
|
public $destLon;
|
|
public $destName;
|
|
public $public;
|
|
public $color;
|
|
public $deliveryTime;
|
|
public $deliveryTZ;
|
|
public $deliveryTimeServer;
|
|
public $deliveryMethod;
|
|
// peripeties
|
|
public $idPeripetie;
|
|
public $datePeripetie;
|
|
public $effetPeripetie;
|
|
|
|
function __construct()
|
|
{
|
|
$this->open();
|
|
}
|
|
|
|
function open()
|
|
{
|
|
if ( !$this->connect )
|
|
{
|
|
$this->connect = new mysqli( $this->server, $this->user, $this->passwd, $this->database );
|
|
if ( $this->connect->connect_errno )
|
|
{
|
|
log_error( "Échec de la connexion : => " . $this->connect->connect_error . "<br />" . __file__ . ' ligne ' . __line__);
|
|
return false;
|
|
}
|
|
}
|
|
$this->connect->set_charset("utf8");
|
|
return true;
|
|
}
|
|
|
|
function close()
|
|
{
|
|
$this->connect->close();
|
|
$this->connect = 0;
|
|
}
|
|
|
|
function protect( $string )
|
|
{
|
|
return $this->connect->real_escape_string( $string );
|
|
}
|
|
|
|
function query( $string )
|
|
{
|
|
|
|
//log_write( $string );
|
|
if ( empty( $this->connect ) ) $this->open();
|
|
|
|
$this->result = $this->connect->query( $string ) ;
|
|
|
|
if ( $this->connect->error ) log_error( "Échec de la commande query => " . $this->connect->error . "<br />" . __file__ . ' ligne ' . __line__ . "\n" . $string, true, true);
|
|
//print_r( $this );
|
|
}
|
|
|
|
function newPlane() //return 0 on error
|
|
{
|
|
$query = "INSERT INTO avionpoeme VALUES ('0','";
|
|
$query .= $this->protect($this->message) . "', '";
|
|
$query .= $this->protect($this->expeMail) . "', ";
|
|
$query .= $this->protect($this->expeKnown) . ", '";
|
|
$query .= $this->protect($this->expeLang) . "', '";
|
|
$query .= $this->protect($this->destLang) . "', '";
|
|
$query .= $this->protect($this->startLat) . "', '";
|
|
$query .= $this->protect($this->startLon) . "', '";
|
|
$query .= $this->protect($this->startName) . "', ";
|
|
$query .= "FROM_UNIXTIME(" . $this->protect($this->startTime) . "), '";
|
|
$query .= $this->protect($this->startTZ) . "', '";
|
|
$query .= $this->protect($this->destMail) . "', '";
|
|
$query .= $this->protect($this->destLat) . "', '";
|
|
$query .= $this->protect($this->destLon) . "', '";
|
|
$query .= $this->protect($this->destName) . "', ";
|
|
$query .= $this->protect($this->public) . ", '";
|
|
$query .= $this->protect($this->color) . "', ";
|
|
$query .= "FROM_UNIXTIME(" . $this->protect($this->deliveryTime) . "), '";
|
|
$query .= $this->protect($this->deliveryTZ) . "', ";
|
|
$query .= "FROM_UNIXTIME(" . $this->protect($this->deliveryTimeServer) . "), '";
|
|
$query .= $this->protect($this->deliveryMethod) . "'";
|
|
$query .= ")";
|
|
//print_r($this);
|
|
$this->query($query);
|
|
$this->uid = $this->connect->insert_id;
|
|
echo "{'uid':'" . $this->uid . "'}";
|
|
|
|
//newPeripetie($uid);
|
|
}
|
|
|
|
function getCurrentPlanes()
|
|
{
|
|
$query = "SELECT idAvion as uid, UNIX_TIMESTAMP(deliveryTime) as deliveryTime, deliveryTZ, destLat, destLon, destName, UNIX_TIMESTAMP(startTime) as startTime, startTZ, startLon, startLat, startName, color FROM avionpoeme WHERE deliveryTimeServer>NOW()";
|
|
$this->query($query);
|
|
$planes = $this->result->fetch_all(MYSQLI_ASSOC);
|
|
print json_encode($planes);
|
|
return $planes;
|
|
}
|
|
|
|
function getAnthology($page=0)
|
|
{
|
|
$query = "SELECT COUNT(*) as count FROM avionpoeme WHERE deliveryTimeServer<NOW() AND public='1'";
|
|
$this->query($query);
|
|
$result = $this->result->fetch_assoc();
|
|
$n_pages = floor(($result["count"] / RESULTS_BY_PAGE))+1;
|
|
if ($page <= 0) //renvoie le nombre de ligne d'anthologie
|
|
{
|
|
print '{"nAnthology":"' . $result["count"] . '","nPages":"' . $n_pages .'"}';
|
|
return $n_pages;
|
|
}else //renvoie les lignes d'anthologie
|
|
{
|
|
if ($page > $n_pages) $page=$n_pages;
|
|
$query = "SELECT UNIX_TIMESTAMP(startTime) as startTime, startTZ, startName, message, UNIX_TIMESTAMP(deliveryTime) as deliveryTime , deliveryTZ, destName FROM avionpoeme WHERE deliveryTimeServer<NOW() AND public='1' ORDER BY startTime ASC LIMIT " . ($page-1)*RESULTS_BY_PAGE . "," . RESULTS_BY_PAGE;
|
|
$this->query($query);
|
|
$planes = $this->result->fetch_all(MYSQLI_ASSOC);
|
|
print json_encode($planes, JSON_FORCE_OBJECT);
|
|
return $planes;
|
|
}
|
|
}
|
|
|
|
function savePeripetie()
|
|
{
|
|
global $db;
|
|
$query = "INSERT INTO avionPeripetie VALUES ('0','";
|
|
$query .= $this->protect($this->uid) . ",'";
|
|
$query .= $this->protect($this->idPeripetie) . "', '";
|
|
$query .= $this->protect($this->datePeripetie) . "', '";
|
|
$query .= $this->protect($this->effetPeripetie) . "', '";
|
|
$query .= $this->protect($this->expeMail) . "', '";
|
|
$query .= $this->protect($this->destMail) . "')";
|
|
echo $query;
|
|
//$this->query($query);
|
|
}
|
|
|
|
function newPeripetie()
|
|
{
|
|
global $db;
|
|
mt_getrandmax ( );
|
|
$query = "SELECT COUNT(*) as count FROM peripeties";
|
|
$this->query($query);
|
|
$result = $this->result->fetch_assoc();
|
|
$db->idPeripetie = round(mt_rand(1,$result["count"]));
|
|
$query = "SELECT * FROM peripeties WHERE idPeripetie='" . $db->idPeripetie . "'";
|
|
$query = "SELECT COUNT(*) as count FROM peripeties";
|
|
$this->query($query);
|
|
$result = $this->result->fetch_assoc();
|
|
//calcul de la probabilité de la péripétie
|
|
|
|
|
|
//ajustement de l'effet
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$this->savePeripetie();
|
|
}
|
|
}
|
|
|
|
|
|
?>
|