1
0
This repository has been archived on 2023-11-30. You can view files and clone it, but cannot push or open issues or pull requests.
dtux__avion-poeme/cron.php

222 lines
8.1 KiB
PHP
Raw Normal View History

2021-04-29 10:32:58 +02:00
<?php
2021-05-31 12:27:51 +02:00
//chdir('/var/www/html/avion-poeme/');
2021-05-31 12:27:51 +02:00
require_once 'session_init.php';
2021-04-29 10:32:58 +02:00
require_once 'config.inc.php';
require_once 'db.class.php';
require_once 'log.php';
require_once 'envoi_courriel.inc.php';
$db = new db();
2021-05-31 12:27:51 +02:00
//test();
$_SERVER["PHP_AUTH_USER"] = "cron";
//peripetieDatee(); // désactivée pour l'instant
2021-04-29 10:32:58 +02:00
envoiCourrielEnattente();
2021-06-17 18:35:51 +02:00
print "envoi des péripéties" . EOLH;
//peripeties();
2021-06-17 18:35:51 +02:00
print "Envoi des arrivées" . EOLH;
2021-04-29 10:32:58 +02:00
arrivee();
2021-10-30 12:37:27 +02:00
anonymisation();
2021-04-29 10:32:58 +02:00
2021-10-30 12:37:27 +02:00
################################### FONCTIONS #################################""
2021-04-29 10:32:58 +02:00
function peripetieDatee()
{
global $db;
$flag = false;
$db->urlAvion = $base_url . "/?avionpoeme=" . $db->idAvion;
//recherche des péripéties datées
$query = "SELECT * FROM peripeties WHERE quand!='' AND DATEDIFF(quand,CURDATE())=0";
$db->query($query);
$peripetieTable = $result = $db->result->fetch_all(MYSQLI_ASSOC);
$query = "SELECT * FROM avionpoeme WHERE atteri=0";
$db->query($query);
while ( ($result = $db->result->fetch_assoc()) )
{
$dicoExpe = getLang( $result['expeLang'] );
$dicoDest = getLang( $result['destLang'] );
foreach( $peripetieTable as $p )
{
$indexObjet = "Objet_" . $p['indexText'];
$objetMail = $dicoExpe["Objet_" . $p['indexText']];
$mailText = $dicoExpe[$p['indexText']] .EOL .EOL ;
2021-06-17 18:35:51 +02:00
print $objetMail . EOLH;
print $mailText . EOLH;
envoiMail($result['expeMail'], $result['destMail'], $objetMail, $mailText, false);
2021-04-29 10:32:58 +02:00
sleep(1);
envoiMail("contact@avion-poe.me", $result['expeMail'], $objetMail, $mailText, true);
2021-04-29 10:32:58 +02:00
sleep(1);
}
}
}
function peripeties() //recherche des péripéties non datées
{
global $db, $base_url;
2021-06-17 18:35:51 +02:00
$r = 0;
$s = 0;
2021-04-29 10:32:58 +02:00
// ouverture DB peripéties
$dbPeripetie = new db();
// requête Mysql
$query = "SELECT *, UNIX_TIMESTAMP(date) as datePeripetie FROM avionPeripetie LEFT JOIN peripeties USING (idPeripetie) WHERE mailSent=false AND TIMEDIFF(CONCAT_WS(' ',CURDATE(),CURTIME()), date) > '00:00'";
$dbPeripetie->query($query);
$peripeties = $dbPeripetie->result->fetch_all(MYSQLI_ASSOC);
$dbPeripetie->close();
//print_r($peripeties);
2021-04-29 10:32:58 +02:00
foreach ( $peripeties as $peripetie)
{
2021-06-17 18:35:51 +02:00
print "péripétie " . $peripetie["idAP"] . " - avion poème n° " . $peripetie['idAvion'] .EOLH;
2021-04-29 10:32:58 +02:00
$db->getPlane($peripetie['idAvion']);
2021-06-17 18:35:51 +02:00
if ($db->connect->affected_rows == 0)
2021-04-29 10:32:58 +02:00
{
2021-06-17 18:35:51 +02:00
log_error("Péripétie " . $peripetie["idAP"] . "n'a pas d'avion associé idAvion=>" . $peripetie['idAvion']);
print "Péripétie " . $peripetie["idAP"] . "n'a pas d'avion associé idAvion=>" . $peripetie['idAvion'];
2021-04-29 10:32:58 +02:00
}
2021-06-17 18:35:51 +02:00
else
{
//print_r($db);
$dicoExpe = getLang( $db->expeLang );
$dicoDest = getLang( $db->destLang );
// TODO calcul de la nouvelle date d'arrivée
$startTime = date("c", $db->startTime);
$deliveryTime = date("c", $db->deliveryTime);
$dateExpe = new DateTime($startTime);
print_r($dateExpe);
$dateDest = new DateTime($deliveryTime);
print_r($dateDest);
2021-06-17 18:35:51 +02:00
$interval = $dateExpe->diff($dateDest);
$numericInterval = (($interval->days*24 + $interval->h)*60 + $interval->i)*60 + $interval->s;
$addDate = $numericInterval * 15/100; //TODO calculer d'après les données de la péripétie(voir table péripétie de la base de données)
2021-06-17 18:35:51 +02:00
$db->deliveryTime += $addDate;
$db->deliveryTimeServer += $addDate;
$query = "UPDATE avionpoeme SET deliveryTime=FROM_UNIXTIME(" . $db->deliveryTime . "), deliveryTimeServer=FROM_UNIXTIME(" . $db->deliveryTimeServer . ") WHERE idAvion='" . $peripetie['idAvion'] . "'" ;
$db->query($query);
$db->urlAvion = $base_url . "/?avionpoeme=" . $db->uid;
$db->datePeripetie = formatteDate($db->expeLang, $db->deliveryTime, $db->startTZ);
$indexObjet = "Objet_" . $peripetie['indexText'];
$result = replaceVariables($db, $dicoExpe["Objet_" . $peripetie['indexText']], $dicoExpe);
$objetMail = $result["text"];
$result = replaceVariables($db, $dicoExpe[$peripetie['indexText']], $dicoExpe);
$mailText = $result["text"];
//$html = $result['html'];
sleep(1);
print "envoi courriel à l'expéditeur : " . $db->expeMail . EOLH . $mailText .EOLH;
$r = envoiMail("contact@avion-poe.me", $db->expeMail, $objetMail, $mailText);
2021-06-17 18:35:51 +02:00
$db->datePeripetie = formatteDate($db->destLang, $db->deliveryTime, $db->deliveryTZ);
$result = replaceVariables($db, $dicoDest["Objet_" . $peripetie['indexText']], $dicoDest);
$objetMail = $result["text"];
$result = replaceVariables($db, $dicoDest[$peripetie['indexText']], $dicoDest);
$mailText = $result["text"];
2021-06-17 18:35:51 +02:00
print "envoi courriel au destinataire : " . $db->destMail . EOLH . $mailText .EOLH;
$s = envoiMail($db->expeMail, $db->destMail, $objetMail, $mailText);
2021-06-17 18:35:51 +02:00
sleep(1);
//print "r=" . $r ." => s=" .$s .EOLH;
if ($r AND $s)
{
$query = "UPDATE avionPeripetie SET mailSent=true WHERE idAP='" . $peripetie['idAP'] ."'";
$db->query($query);
}else
{
print "table avion péripétie non mise à jour" . EOL;
2021-06-17 18:35:51 +02:00
print "UPDATE avionPeripetie SET mailSent=true WHERE idAP='" . $peripetie['idAP'] ."'" .EOLH;
}
sleep(1);
}
2021-04-29 10:32:58 +02:00
}
}
function arrivee()
{
global $db;
2021-06-17 18:35:51 +02:00
$r = 0;
$s = 0;
2021-04-29 10:32:58 +02:00
//recherche des avions atteris
2021-05-31 12:27:51 +02:00
$query = "SELECT * FROM avionpoeme WHERE atteri = 0 AND TIMEDIFF(CONCAT_WS(' ',CURDATE(),CURTIME()),deliveryTimeServer) > '00:00'";
2021-04-29 10:32:58 +02:00
$db->query($query);
$avionArrive = $db->result->fetch_all(MYSQLI_ASSOC);
2021-10-30 12:37:27 +02:00
//var_dump($avionArrive);
2021-04-29 10:32:58 +02:00
foreach ($avionArrive as $avion)
{
print "avion poème n° " . $avion['idAvion'] . " a atterri" . EOLH;
2021-04-29 10:32:58 +02:00
$dicoExpe = getLang( $avion['expeLang'] );
$dicoDest = getLang( $avion['destLang'] );
2021-06-17 18:35:51 +02:00
//envoi notification arrivée expéditeur
2021-04-29 10:32:58 +02:00
$indexObjet = "Objet_";
2021-06-17 18:35:51 +02:00
$index = "Notification_Arrivee_Expediteur";
$objetMail = replaceVariables( (object)$avion, $dicoExpe[$indexObjet . $index], $dicoExpe);
$result = replaceVariables((object)$avion, $dicoExpe[$index], $dicoExpe);
2021-05-31 12:27:51 +02:00
$mailText= $result["text"];
2021-06-17 18:35:51 +02:00
print "envoi courriel à l'expéditeur : " . $avion['expeMail'] . EOLH . $mailText .EOLH;
$r = envoiMail("contact@avion-poe.me", $avion['expeMail'], $objetMail["text"], $mailText);
2021-05-31 12:27:51 +02:00
2021-06-17 18:35:51 +02:00
//envoi notification arrivée destinataire
$index = "Notification_Arrivee_Destinataire";
$objetMail = replaceVariables((object)$avion, $dicoDest[$indexObjet . $index], $dicoDest);
$result = replaceVariables((object)$avion, $dicoDest[$index], $dicoDest);
2021-05-31 12:27:51 +02:00
$mailText = $result["text"];
$html = $result['html'];
if ( $html )
{
$mailText = "<html><head></head>\n<body>" . $mailText . "</body></html>";
}
2021-06-17 18:35:51 +02:00
print "envoi courriel au destinataire : " . $avion['destMail'] . EOLH . $mailText .EOLH;
sleep(3);
$s = envoiMail($avion['expeMail'], $avion['destMail'], $objetMail["text"], $mailText, $html);
2021-04-29 10:32:58 +02:00
if ($r AND $s)
{
$query = "UPDATE avionpoeme SET atteri=true WHERE idAvion='" . $avion['idAvion'] ."'";
$db->query($query);
}
sleep(3);
2021-04-29 10:32:58 +02:00
}
}
function envoiCourrielEnattente()
{
global $db;
$query = "SELECT * FROM courriels";
$db->query($query);
$courriels = $db->result->fetch_all(MYSQLI_ASSOC);
foreach( $courriels as $courriel)
{
//TODO changer l'expéditeur, doit être l'expéditeur de l'avion-poême.
$r = envoiMail("contact@avion-poe.me", $courriel['destinataire'], $courriel['objet'], $courriel['message'], $courriel['html']);
2021-04-29 10:32:58 +02:00
if ($r)
{
$query = "DELETE FROM courriels WHERE id='" . $courriel['id'] . "'";
$db->query($query);
}
sleep(3);
2021-04-29 10:32:58 +02:00
}
}
function test()
{
$lang = array( 'fr', 'en', 'es', 'de', 'ar', 'bg', 'zh', 'ja', 'pt');
foreach ($lang as $value)
{
getLang( $value );
print $value;
print json_last_error_msg() .EOLH;
}
}
2021-10-30 12:37:27 +02:00
function anonymisation()
{
global $db;
$query = "SELECT idAvion FROM avionpoeme WHERE atteri = 1 AND public = 0 AND DATEDIFF(CONCAT_WS(' ',CURDATE(),CURTIME()),deliveryTimeServer) > '7'";
$db->query($query);
$avionArrive = $db->result->fetch_all(MYSQLI_ASSOC);
//print_r($avionArrive);
foreach($avionArrive as $avion)
{
$query = "UPDATE avionpoeme SET message='', expeMail='', destMail='' WHERE idAvion='" . $avion['idAvion'] ."'";
$db->query($query);
}
}
2021-04-29 10:32:58 +02:00
$db->close();
?>