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 . "
" . __file__ . ' ligne ' . __line__, false, false); 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->errno > 0) log_error("Échec de la commande query => " . $this->connect->error . "
" . __file__ . ' ligne ' . __line__ . "\n" . $string, true, false); //print_r( $this ); } function newPlane() //return 0 on error { global $base_url; 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; } $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 .= "0')"; $this->query($query); $this->uid = $this->connect->insert_id; echo "{'uid':'" . $this->uid . "'}"; $this->startTimeRFC822 = formatteDate($this->expeLang, $this->startTime, $this->startTZ); $this->deliveryTimeRFC822 = formatteDate($this->destLang, $this->deliveryTime, $this->deliveryTZ); $this->urlAvion = $base_url . "/?avionpoeme=" . $this->uid; $this->newPeripetie(); courrielEnvoi($this); } function getPlane($uid) { global $base_url; $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 . "'"; $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"]; $this->startTimeRFC822 = formatteDate($this->expeLang, $this->startTime, $this->startTZ); $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"]; $this->deliveryTimeRFC822 = formatteDate($this->destLang, $this->deliveryTime, $this->deliveryTZ); $this->deliveryTimeServer = $plane["deliveryTimeServer"]; $this->deliveryMethod = $plane["deliveryMethod"]; $this->public = $plane["public"]; $this->uid = $plane["idAvion"]; $this->urlAvion = $base_url . "/?avionpoeme=" . $this->uid; } 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()"; $this->query($query); $planes = $this->result->fetch_all(MYSQLI_ASSOC); print json_encode($planes); //log_write(json_encode($planes), INFO); return $planes; } function getAnthology($page = 0) { $query = "SELECT COUNT(*) as count FROM avionpoeme WHERE deliveryTimeServerquery($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 deliveryTimeServerquery($query); $planes = $this->result->fetch_all(MYSQLI_ASSOC); print json_encode($planes, JSON_FORCE_OBJECT); return $planes; } } function savePeripetie() { $query = "INSERT INTO avionPeripetie ('expeMail', 'idAvion', 'idPeripetie', 'date'), VALUES ("; if ($this->expeKnown) { $query .= "'" . $this->protect($this->expeMail) . "',"; }else { $query .= "'contact@avion-poe.me',"; } $query .= "'" . $this->protect($this->uid) . "',"; $query .= "'" . $this->protect($this->idPeripetie) . "', "; $query .= "FROM_UNIXTIME(" . $this->protect($this->datePeripetie) . "))"; log_write("savePeripetie " . $query); //$query .= $this->protect($this->effetPeripetie) . "', '"; //$query .= $this->protect($this->expeMail) . "', '"; //$query .= $this->protect($this->destMail) . "')"; $this->query($query); } function newPeripetie() { $query = "SELECT *, DATEDIFF(quand,CURDATE()) as datePeripetie FROM peripeties WHERE quand IS NULL"; $this->query($query); while (($result = $this->result->fetch_assoc())) { $this->idPeripetie = $result["idPeripetie"]; if ((mt_rand(0, 100) <= $result["probabilite"])) //calcul de la probabilité de la péripétie { log_write("newPeripetie"); $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 } } } 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; } }