2021-03-22 12:20:39 +01:00
< ? php
2021-12-14 22:33:54 +01:00
if ( ! isset ( $site )) {
require_once ( " config.inc.php " );
header ( 'Location: ' . $accueil );
2021-03-23 20:26:52 +01:00
}
require_once " config.inc.php " ;
require_once " log.php " ;
2021-04-29 10:32:58 +02:00
require_once " fonctions.inc.php " ;
2021-03-23 20:26:52 +01:00
2021-03-22 12:20:39 +01:00
$table_prefix = " " ;
class db
{
2021-12-14 22:33:54 +01:00
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 ;
2021-03-29 21:50:41 +02:00
// avion
2021-03-22 12:20:39 +01:00
public $uid ;
public $message ;
public $expeMail ;
public $expeKnown ;
public $expeLang ;
public $destLang ;
public $startLat ;
public $startLon ;
public $startName ;
public $startTime ;
2021-04-29 10:32:58 +02:00
public $startTimeRFC822 ;
2021-03-25 10:26:40 +01:00
public $startTZ ;
2021-03-22 12:20:39 +01:00
public $destMail ;
public $destLat ;
public $destLon ;
public $destName ;
2021-03-23 20:26:52 +01:00
public $public ;
2021-03-22 12:20:39 +01:00
public $color ;
public $deliveryTime ;
2021-04-29 10:32:58 +02:00
public $deliveryTimeRFC822 ;
2021-03-22 12:20:39 +01:00
public $deliveryTZ ;
public $deliveryTimeServer ;
2021-03-29 21:50:41 +02:00
public $deliveryMethod ;
2021-04-29 10:32:58 +02:00
public $avionJournalier ;
public $urlAvion ;
2021-03-29 21:50:41 +02:00
// peripeties
public $idPeripetie ;
public $datePeripetie ;
public $effetPeripetie ;
2021-03-22 12:20:39 +01:00
2021-12-14 22:33:54 +01:00
function __construct ()
{
$this -> open ();
}
2021-03-22 12:20:39 +01:00
2021-12-14 22:33:54 +01:00
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__ , false , false );
2021-03-22 12:20:39 +01:00
return false ;
}
}
$this -> connect -> set_charset ( " utf8 " );
return true ;
2021-12-14 22:33:54 +01:00
}
2021-03-22 12:20:39 +01:00
2021-12-14 22:33:54 +01:00
function close ()
{
$this -> connect -> close ();
$this -> connect = 0 ;
}
2021-03-22 12:20:39 +01:00
2021-12-14 22:33:54 +01:00
function protect ( $string )
{
return $this -> connect -> real_escape_string ( $string );
}
2021-03-22 12:20:39 +01:00
2021-12-14 22:33:54 +01:00
function query ( $string )
2021-03-22 12:20:39 +01:00
{
//log_write( $string );
2021-12-14 22:33:54 +01:00
if ( empty ( $this -> connect )) $this -> open ();
2021-03-22 12:20:39 +01:00
2021-12-14 22:33:54 +01:00
$this -> result = $this -> connect -> query ( $string );
2021-03-22 12:20:39 +01:00
2021-12-14 22:33:54 +01:00
if ( $this -> connect -> errno > 0 ) log_error ( " Échec de la commande query => " . $this -> connect -> error . " <br /> " . __file__ . ' ligne ' . __line__ . " \n " . $string , true , false );
2021-03-22 12:20:39 +01:00
//print_r( $this );
2021-12-14 22:33:54 +01:00
}
2021-03-22 12:20:39 +01:00
2021-12-14 22:33:54 +01:00
function newPlane () //return 0 on error
{
2021-04-29 10:32:58 +02:00
global $base_url ;
2021-12-14 22:33:54 +01:00
if ( round ( $this -> startLat , 3 , PHP_ROUND_HALF_DOWN ) == round ( $this -> destLat , 3 , PHP_ROUND_HALF_DOWN ) && round ( $this -> startLon , 3 , PHP_ROUND_HALF_DOWN ) == round ( $this -> destLon , 3 , PHP_ROUND_HALF_DOWN )) {
$this -> destLon += 0.001 ;
$this -> destLat += 0.001 ;
2021-10-30 12:15:02 +02:00
}
2021-03-23 20:26:52 +01:00
$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 ) . " ', ' " ;
2021-03-25 10:26:40 +01:00
$query .= $this -> protect ( $this -> startName ) . " ', " ;
$query .= " FROM_UNIXTIME( " . $this -> protect ( $this -> startTime ) . " ), ' " ;
$query .= $this -> protect ( $this -> startTZ ) . " ', ' " ;
2021-03-23 20:26:52 +01:00
$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 ) . " , ' " ;
2021-03-25 10:26:40 +01:00
$query .= $this -> protect ( $this -> color ) . " ', " ;
$query .= " FROM_UNIXTIME( " . $this -> protect ( $this -> deliveryTime ) . " ), ' " ;
$query .= $this -> protect ( $this -> deliveryTZ ) . " ', " ;
2021-03-29 21:50:41 +02:00
$query .= " FROM_UNIXTIME( " . $this -> protect ( $this -> deliveryTimeServer ) . " ), ' " ;
2021-04-07 21:08:08 +02:00
$query .= $this -> protect ( $this -> deliveryMethod ) . " ',' " ;
$query .= " 0') " ;
2021-03-23 20:26:52 +01:00
$this -> query ( $query );
2021-03-29 21:50:41 +02:00
$this -> uid = $this -> connect -> insert_id ;
echo " { 'uid':' " . $this -> uid . " '} " ;
2021-12-14 22:33:54 +01:00
$this -> startTimeRFC822 = formatteDate ( $this -> expeLang , $this -> startTime , $this -> startTZ );
$this -> deliveryTimeRFC822 = formatteDate ( $this -> destLang , $this -> deliveryTime , $this -> deliveryTZ );
2021-04-29 10:32:58 +02:00
$this -> urlAvion = $base_url . " /?avionpoeme= " . $this -> uid ;
2021-04-07 21:08:08 +02:00
$this -> newPeripetie ();
2021-05-31 12:27:51 +02:00
courrielEnvoi ( $this );
2021-03-22 12:20:39 +01:00
}
2021-03-23 20:26:52 +01:00
2021-04-29 10:32:58 +02:00
function getPlane ( $uid )
{
global $base_url ;
2021-12-14 22:33:54 +01:00
$query = " SELECT idAvion, message, expeMail, expeKnown, expeLang, destLang, startLat, startLon, startName, UNIX_TIMESTAMP(startTime) as startTime, startTZ, destMail, destLat, destLon, destName, color, UNIX_TIMESTAMP(deliveryTime) as deliveryTime, deliveryTZ, UNIX_TIMESTAMP(deliveryTimeServer) as deliveryTimeServer, deliveryMethod, public FROM avionpoeme WHERE idAvion=' " . $uid . " ' " ;
2021-04-29 10:32:58 +02:00
$this -> query ( $query );
$plane = $this -> result -> fetch_array ( MYSQLI_ASSOC );
$this -> message = $plane [ " message " ];
$this -> expeMail = $plane [ " expeMail " ];
$this -> expeKnown = $plane [ " expeKnown " ];
$this -> expeLang = $plane [ " expeLang " ];
$this -> destLang = $plane [ " destLang " ];
$this -> startLat = $plane [ " startLat " ];
$this -> startLon = $plane [ " startLon " ];
$this -> startName = $plane [ " startName " ];
$this -> startTime = $plane [ " startTime " ];
$this -> startTZ = $plane [ " startTZ " ];
2021-12-14 22:33:54 +01:00
$this -> startTimeRFC822 = formatteDate ( $this -> expeLang , $this -> startTime , $this -> startTZ );
2021-04-29 10:32:58 +02:00
$this -> destMail = $plane [ " destMail " ];
$this -> destLat = $plane [ " destLat " ];
$this -> destLon = $plane [ " destLon " ];
$this -> destName = $plane [ " destName " ];
$this -> color = $plane [ " color " ];
$this -> deliveryTime = $plane [ " deliveryTime " ];
$this -> deliveryTZ = $plane [ " deliveryTZ " ];
2021-12-14 22:33:54 +01:00
$this -> deliveryTimeRFC822 = formatteDate ( $this -> destLang , $this -> deliveryTime , $this -> deliveryTZ );
2021-04-29 10:32:58 +02:00
$this -> deliveryTimeServer = $plane [ " deliveryTimeServer " ];
$this -> deliveryMethod = $plane [ " deliveryMethod " ];
$this -> public = $plane [ " public " ];
$this -> uid = $plane [ " idAvion " ];
$this -> urlAvion = $base_url . " /?avionpoeme= " . $this -> uid ;
}
2021-12-14 22:33:54 +01:00
function getCurrentPlanes ()
{
$query = " SELECT idAvion as uid, UNIX_TIMESTAMP(deliveryTime) as deliveryTime, deliveryTZ, destLat, destLon, destName, destMail, message, expeMail, deliveryTimeServer, deliveryMethod, UNIX_TIMESTAMP(startTime) as startTime, startTZ, startLon, startLat, startName, color, message FROM avionpoeme WHERE deliveryTimeServer>NOW() " ;
2021-03-24 21:26:05 +01:00
$this -> query ( $query );
$planes = $this -> result -> fetch_all ( MYSQLI_ASSOC );
2021-03-23 20:26:52 +01:00
print json_encode ( $planes );
2021-06-17 18:35:51 +02:00
//log_write(json_encode($planes), INFO);
2021-03-29 21:50:41 +02:00
return $planes ;
2021-03-23 20:26:52 +01:00
}
2021-12-14 22:33:54 +01:00
function getAnthology ( $page = 0 )
2021-03-23 20:26:52 +01:00
{
2021-03-29 21:50:41 +02:00
$query = " SELECT COUNT(*) as count FROM avionpoeme WHERE deliveryTimeServer<NOW() AND public='1' " ;
2021-03-24 21:26:05 +01:00
$this -> query ( $query );
$result = $this -> result -> fetch_assoc ();
2021-12-14 22:33:54 +01:00
$n_pages = floor (( $result [ " count " ] / RESULTS_BY_PAGE )) + 1 ;
2021-03-24 21:26:05 +01:00
if ( $page <= 0 ) //renvoie le nombre de ligne d'anthologie
2021-03-23 20:26:52 +01:00
{
2021-12-14 22:33:54 +01:00
print '{"nAnthology":"' . $result [ " count " ] . '","nPages":"' . $n_pages . '"}' ;
2021-03-26 20:21:38 +01:00
return $n_pages ;
2021-12-14 22:33:54 +01:00
} else //renvoie les lignes d'anthologie
2021-03-23 20:26:52 +01:00
{
2021-12-14 22:33:54 +01:00
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 deliveryTime DESC LIMIT " . ( $page - 1 ) * RESULTS_BY_PAGE . " , " . RESULTS_BY_PAGE ;
2021-03-24 21:26:05 +01:00
$this -> query ( $query );
$planes = $this -> result -> fetch_all ( MYSQLI_ASSOC );
print json_encode ( $planes , JSON_FORCE_OBJECT );
2021-03-26 20:21:38 +01:00
return $planes ;
2021-03-23 20:26:52 +01:00
}
}
2021-03-29 21:50:41 +02:00
function savePeripetie ()
{
2021-04-29 10:32:58 +02:00
2022-02-15 15:52:12 +01:00
$query = " INSERT INTO avionPeripetie ('idAvion', 'idPeripetie', 'date', 'expeMail'), VALUES ( " ;
$query .= " ' " . $this -> protect ( $this -> uid ) . " ', " ;
$query .= " ' " . $this -> protect ( $this -> idPeripetie ) . " ', " ;
$query .= " FROM_UNIXTIME( " . $this -> protect ( $this -> datePeripetie ) . " ), " ;
2022-02-10 19:57:35 +01:00
if ( $this -> expeKnown )
{
2022-02-15 15:52:12 +01:00
$query .= " ' " . $this -> protect ( $this -> expeMail ) . " ' " ;
2022-02-10 19:57:35 +01:00
} else
{
2022-02-15 15:52:12 +01:00
$query .= " 'contact@avion-poe.me' " ;
2022-02-10 19:57:35 +01:00
}
2022-02-15 15:52:12 +01:00
$query .= " ) " ;
2021-04-29 10:32:58 +02:00
log_write ( " savePeripetie " . $query );
2021-04-07 21:08:08 +02:00
//$query .= $this->protect($this->effetPeripetie) . "', '";
//$query .= $this->protect($this->expeMail) . "', '";
2021-06-17 18:35:51 +02:00
//$query .= $this->protect($this->destMail) . "')";
2021-04-07 21:08:08 +02:00
$this -> query ( $query );
2021-03-29 21:50:41 +02:00
}
function newPeripetie ()
{
2021-04-07 21:08:08 +02:00
$query = " SELECT *, DATEDIFF(quand,CURDATE()) as datePeripetie FROM peripeties WHERE quand IS NULL " ;
$this -> query ( $query );
2021-12-14 22:33:54 +01:00
while (( $result = $this -> result -> fetch_assoc ())) {
2021-04-07 21:08:08 +02:00
$this -> idPeripetie = $result [ " idPeripetie " ];
2021-12-14 22:33:54 +01:00
if (( mt_rand ( 0 , 100 ) <= $result [ " probabilite " ])) //calcul de la probabilité de la péripétie
2021-04-07 21:08:08 +02:00
{
2021-04-29 10:32:58 +02:00
log_write ( " newPeripetie " );
2021-04-07 21:08:08 +02:00
$this -> datePeripetie = mt_rand ( $this -> startTime , $this -> deliveryTime ); //ajustement de l'effet
$this -> savePeripetie ();
break ; //une seule péripétie par avionpoème. Enlever le break si pas de limites sur le nombre de péripéties
}
}
2021-03-29 21:50:41 +02:00
}
2021-04-29 10:32:58 +02:00
function avionJournalier ()
{
$query = " SELECT COUNT(*) as count FROM avionpoeme WHERE DATEDIFF(startTime, NOW())=0 " ;
$this -> query ( $query );
$result = $this -> result -> fetch_assoc ();
$this -> avionJournalier = $result [ " count " ];
return $this -> avionJournalier ;
}
2021-03-22 12:20:39 +01:00
}