| Current Path : /home/h/a/p/happyrenas/myreco.online/includes/ |
Linux webd005.cluster105.gra.hosting.ovh.net 5.15.206-ovh-vps-grsec-zfs-classid #1 SMP Fri May 15 02:41:25 UTC 2026 x86_64 |
| Current File : /home/h/a/p/happyrenas/myreco.online/includes/fonctions_image.php |
<?
/*********************************************************************
* đ TELECHARGEMENT DE LA PHOTO VIA LE LIEN DANS LA TABLE
*********************************************************************/
function telechargerPhotoDepuisTable($token, $table) {
sleep(1);
global $db;
if (!in_array($table, ['heb', 'vis'])) {
return ['statut' => 'table_invalide'];
}
// RequĂȘte
$reponse = $db->get_row("SELECT token, photo FROM `$table` WHERE token = '" . addslashes($token) . "'");
if (!$reponse || empty($reponse->photo)) {
return ['statut' => 'erreur_photo_absente'];
incrementerEchecsTelechargementPhoto($db, $table, $token);
}
if ($reponse->telechargementphoto_echecs >= 3) {
return "Trop dâĂ©checs pour ce lieu. TĂ©lĂ©chargement bloquĂ©.";
}
// PremiĂšre URL (au cas oĂč plusieurs seraient prĂ©sentes)
$photos = explode(',', $reponse->photo);
$first_image = trim($photos[0]);
if (!filter_var($first_image, FILTER_VALIDATE_URL)) {
return ['statut' => 'erreur_url_invalide'];
incrementerEchecsTelechargementPhoto($db, $table, $token);
}
// Répertoire de destination selon la table
$type_dir = ($table === 'heb') ? 'hebergement' : 'visite';
$first_letter = strtoupper(substr($reponse->token, 0, 2));
$destination_dir = DOSSIER_RACINE."/upload/$type_dir/" . $first_letter . "/";
if (!is_dir($destination_dir)) {
if (!mkdir($destination_dir, 0777, true)) {
return ['statut' => 'erreur_mkdir'];
}
}
// Extension du fichier
$extension = pathinfo(parse_url($first_image, PHP_URL_PATH), PATHINFO_EXTENSION);
if (!$extension) $extension = 'jpg';
$destination_file = $destination_dir . $reponse->token . '.' . $extension;
// Télécharger
$image_data = @file_get_contents($first_image);
if ($image_data === false) {
return ['statut' => 'erreur_telechargement : '.$first_image];
incrementerEchecsTelechargementPhoto($db, $table, $token);
}
if (file_put_contents($destination_file, $image_data) === false) {
return ['statut' => 'erreur_ecriture'];
}
$photo_name = basename($destination_file);
$chemin_relatif = "/upload/$type_dir/$first_letter/" . $photo_name;
// SuccĂšs : on remet le compteur Ă 0
$db->query("UPDATE `$table` SET telechargementphoto_echecs = 0 WHERE id = " . (int)$reponse->id);
$db->query("UPDATE ".$table." SET photo_locale = '" . $db->escape($chemin_relatif) . "' WHERE id = " . (int)$reponse->id);
return [
'statut' => 'ok',
'fichier' => $photo_name,
'chemin' => $destination_file,
'url' => $chemin_relatif
];
}
/**********************************************
* đ TELECHARGEMENT DE LA PHOTO VIA API GOOGLE
**********************************************/
function telechargerPhotoLieuGoogle($token, $table)
{
global $db, $apiKey,$GOOGLE_FRONT_KEY, $GOOGLE_SERVER_KEY;
// Déterminer la table et le type
if ($table == "heb") {
$reponse = $db->get_row("SELECT * FROM heb WHERE token = '" . addslashes($token) . "'");
$type = "hebergement";
} elseif ($table == "vis") {
$reponse = $db->get_row("SELECT * FROM vis WHERE token = '" . addslashes($token) . "'");
$type = "visite";
} else {
return "Table inconnue.";
}
if (!$reponse) {
return "Aucune fiche trouvée pour ce token.";
}
if ($reponse->api_echecs >= 3) {
return "Trop dâĂ©checs pour ce lieu. TĂ©lĂ©chargement bloquĂ©.";
}
if (empty($reponse->place_id)) {
return "Aucune place_id trouvée pour ce token.";
}
// Répertoire
$first_letter = strtoupper(substr($reponse->token, 0, 2));
$destination_dir = DOSSIER_RACINE."/upload/{$type}/{$first_letter}/";
if (!is_dir($destination_dir)) {
if (!mkdir($destination_dir, 0777, true)) {
return "Erreur lors de la création du dossier $destination_dir";
}
}
// Appel API Google
$detailsUrl = "https://maps.googleapis.com/maps/api/place/details/json?place_id={$reponse->place_id}&fields=photos&key={$GOOGLE_SERVER_KEY}";
$response = @file_get_contents($detailsUrl);
// Historisation appel API
$db->query("UPDATE api_historique SET api_appels = api_appels + 1");
if ($response === false) {
incrementerEchecAPI($db, $table, $token);
return "Ăchec de la requĂȘte Ă l'API Google Place Details.";
}
$data = json_decode($response, true);
if (!isset($data['status']) || $data['status'] !== "OK") {
incrementerEchecAPI($db, $table, $token);
return "Erreur API Google : " . ($data['status'] ?? 'inconnu');
}
if (empty($data['result']['photos'])) {
incrementerEchecAPI($db, $table, $token);
return "Aucune photo disponible pour ce lieu.";
}
// Télécharger la photo
$photo_reference = $data['result']['photos'][0]['photo_reference'];
$photoUrl = "https://maps.googleapis.com/maps/api/place/photo?maxwidth=1200&photoreference=$photo_reference&key=$GOOGLE_SERVER_KEY";
// Historisation appel API
$db->query("UPDATE api_historique SET api_appels = api_appels + 1");
$imageContent = @file_get_contents($photoUrl);
if ($imageContent === false) {
incrementerEchecAPI($db, $table, $token);
return "Ăchec du tĂ©lĂ©chargement de la photo.";
}
$filename = $destination_dir . $reponse->token . ".jpg";
if (!file_put_contents($filename, $imageContent)) {
incrementerEchecAPI($db, $table, $token);
return "Impossible d'enregistrer l'image.";
}
// SuccĂšs : on remet le compteur Ă 0
$db->query("UPDATE `$table` SET api_echecs = 0 WHERE token = '" . addslashes($token) . "'");
$db->query("UPDATE ".$table." SET photo_locale = '" . $db->escape($filename) . "' WHERE id = " . (int)$reponse->id);
return "Photo enregistrée dans $filename.";
}
/*****************************************************
* đ RECUPERER LE CHEMIN DE LA PHOTO STOCKEE EN LOCAL
****************************************************/
function recupererCheminPhotoLocale($token, $table) {
global $db;
if (!in_array($table, ['heb', 'vis'])) {
return ['statut' => 'table_invalide', 'chemin' => null];
}
// RequĂȘte
$reponse = $db->get_row("SELECT id,token FROM `$table` WHERE token = '" . addslashes($token) . "'");
if (!$reponse) {
return ['statut' => 'token_introuvable', 'chemin' => null];
}
$type_dir = ($table === 'heb') ? 'hebergement' : 'visite';
$first_letter = strtoupper(substr($reponse->token, 0, 2));
$search_pattern = DOSSIER_RACINE."/upload/$type_dir/$first_letter/{$reponse->token}.*";
// Cherche le fichier correspondant
$fichiers = glob($search_pattern);
if (empty($fichiers)) {
return ['statut' => 'non_trouvee', 'chemin' => null];
}
$fichier_absolu = $fichiers[0];
$extension = pathinfo($fichier_absolu, PATHINFO_EXTENSION);
$chemin_relatif = "/upload/$type_dir/$first_letter/{$reponse->token}." . $extension;
$db->query("UPDATE ".$table." SET photo_locale = '" . $db->escape($chemin_relatif) . "' WHERE id = " . (int)$reponse->id);
return [
'statut' => 'ok',
'chemin' => $chemin_relatif,
'url' => $chemin_relatif,
'fichier' => basename($chemin_relatif),
'date' => file_exists($fichier_absolu) ? date("d-m-Y H:i:s", filemtime($fichier_absolu)) : null
];
}
/*******************************************************************
* đ PROCESSUS GLOBAL D'APPEL DE FONCTIONS POUR RECUPERER LA PHOTO
******************************************************************/
function verifierOuRecupererPhoto($token, $table) {
global $db;
// Ătape 1 : vĂ©rifier si une photo est dĂ©jĂ prĂ©sente localement
$check = recupererCheminPhotoLocale($token, $table);
if ($check['statut'] === 'ok') {
return [
'statut' => 'Photo existante',
'chemin' => $check['chemin'],
'url' => $check['url'],
'date' => $check['date'],
];
}
// Ătape 2 : tĂ©lĂ©chargement depuis le champ `photo`
$res = telechargerPhotoDepuisTable($token, $table);
if ($res && $res['statut'] === 'ok') {
return [
'statut' => 'photo_telechargee_champ',
'chemin' => $res['chemin'],
'url' => $res['url'],
'date' => file_exists($res['chemin']) ? date("d-m-Y H:i:s", filemtime($res['chemin'])) : null
];
}
// Ătape 3 : tĂ©lĂ©chargement via Google Place
$res = telechargerPhotoLieuGoogle($token, $table);
if (is_array($res) && $res['statut'] === 'ok') {
return [
'statut' => 'photo_telechargee_google',
'chemin' => $res['chemin'],
'url' => $res['url'],
'date' => file_exists($res['chemin']) ? date("d-m-Y H:i:s", filemtime($res['chemin'])) : null
];
}
// Ăchec
return [
'statut' => 'echec_toutes_methodes',
'chemin' => null,
'url' => null,
'date' => null
];
}
/*******************************************************************
* đ PROCESSUS GLOBAL D'APPEL DE FONCTIONS POUR RECUPERER LA PHOTO HEBERGEMENT
******************************************************************/
function verifierOuRecupererPhotoHeb($token, $table) {
global $db;
// Ătape 1 : vĂ©rifier si une photo est dĂ©jĂ prĂ©sente localement
$check = recupererCheminPhotoLocale($token, $table);
if ($check['statut'] === 'ok') {
return [
'statut' => 'Photo existante',
'chemin' => $check['chemin'],
'url' => $check['url'],
'date' => $check['date'],
];
}
// Ătape 2 : tĂ©lĂ©chargement depuis le champ `photo`
$res = telechargerPhotoDepuisTable($token, $table);
if ($res && $res['statut'] === 'ok') {
return [
'statut' => 'photo_telechargee_champ',
'chemin' => $res['chemin'],
'url' => $res['url'],
'date' => file_exists($res['chemin']) ? date("d-m-Y H:i:s", filemtime($res['chemin'])) : null
];
}
// Ăchec
return [
'statut' => 'echec_toutes_methodes',
'chemin' => null,
'url' => null,
'date' => null
];
}
?>