| Current Path : /home/h/a/p/happyrenas/find.myreco.online/v2/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/find.myreco.online/v2/includes/fonctions_image.php |
<?
/*********************************************************************
* đ TELECHARGEMENT DE LA PHOTO VIA LE LIEN DANS LA TABLE
*********************************************************************/
function telechargerPhotoDepuisTable($token, $table) {
global $db;
if (!in_array($table, ['heb', 'vis'], true)) {
return ['statut' => 'table_invalide'];
}
// â
On récupÚre TOUT ce qu'on utilise
$reponse = $db->get_row("
SELECT id, token, photo, telechargementphoto_echecs, photo_locale
FROM `$table`
WHERE token = '" . $db->escape($token) . "'
LIMIT 1
");
if (!$reponse) {
return ['statut' => 'erreur_token_inconnu'];
}
// Si déjà présent, inutile de re-télécharger
if (!empty($reponse->photo_locale)) {
return ['statut' => 'deja_en_local', 'url' => $reponse->photo_locale];
}
if (empty($reponse->photo)) {
incrementerEchecsTelechargementPhoto($db, $table, $token);
return ['statut' => 'erreur_photo_absente'];
}
if ((int)$reponse->telechargementphoto_echecs >= 3) {
return ['statut' => 'bloque_trop_echecs'];
}
$photos = explode(',', $reponse->photo);
$first_image = trim($photos[0]);
if (!filter_var($first_image, FILTER_VALIDATE_URL)) {
incrementerEchecsTelechargementPhoto($db, $table, $token);
return ['statut' => 'erreur_url_invalide'];
}
// Répertoire
$type_dir = ($table === 'heb') ? 'hebergement' : 'visite';
$first_letter = strtoupper(substr($reponse->token, 0, 2));
$destination_dir = rtrim(UPLOADS_WWW_ROOT, '/') . "/upload/$type_dir/" . $first_letter . "/";
if (!is_dir($destination_dir) && !mkdir($destination_dir, 0777, true)) {
return ['statut' => 'erreur_mkdir'];
}
// Extension (souvent vide sur googleusercontent)
$extension = pathinfo(parse_url($first_image, PHP_URL_PATH), PATHINFO_EXTENSION);
if (!$extension || strlen($extension) > 5) $extension = 'jpg';
$destination_file = $destination_dir . $reponse->token . '.' . $extension;
// â
Téléchargement avec timeout
$ch = curl_init($first_image);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_CONNECTTIMEOUT => 4,
CURLOPT_TIMEOUT => 8,
CURLOPT_USERAGENT => 'StayFinder/1.0',
]);
$image_data = curl_exec($ch);
$http = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($image_data === false || $http < 200 || $http >= 300 || strlen($image_data) < 200) {
incrementerEchecsTelechargementPhoto($db, $table, $token);
return ['statut' => 'erreur_telechargement'];
}
if (file_put_contents($destination_file, $image_data) === false) {
incrementerEchecsTelechargementPhoto($db, $table, $token);
return ['statut' => 'erreur_ecriture'];
}
$photo_name = basename($destination_file);
$chemin_relatif = "/upload/$type_dir/$first_letter/" . $photo_name;
// â
reset échecs + update photo_locale + date insertion
$db->query("UPDATE `$table` SET telechargementphoto_echecs = 0, photo_locale = '".$db->escape($chemin_relatif)."', photo_locale_dateinsert = NOW() WHERE id = ".(int)$reponse->id);
return [
'statut' => 'ok',
'fichier' => $photo_name,
'chemin' => $destination_file,
'url' => $chemin_relatif
];
}
/*******************************
* đ INCREMENTER LES ECHECS TELECHARGEMENT PHOTO
*******************************/
function incrementerEchecsTelechargementPhoto($db, $table, $token)
{
$db->query("
UPDATE `$table`
SET telechargementphoto_echecs = IFNULL(telechargementphoto_echecs, 0) + 1
WHERE token = '" . addslashes($token) . "'
");
}
function telechargerPhotoLieuGoogle(string $token, string $table = 'heb', int $maxwidth = 1200): array
{
global $db, $GOOGLE_SERVER_KEY;
if (!in_array($table, ['heb', 'vis'], true)) {
return ['ok' => false, 'statut' => 'table_invalide'];
}
$type_dir = ($table === 'heb') ? 'hebergement' : 'visite';
// On ne prend que ce qui nous sert
$row = $db->get_row("
SELECT id, token, place_id, photo_locale, telechargementphoto_echecs
FROM `$table`
WHERE token = '" . $db->escape($token) . "'
LIMIT 1
");
if (!$row) {
return ['ok' => false, 'statut' => 'introuvable'];
}
$fail = (int)($row->telechargementphoto_echecs ?? 0);
if ($fail >= 3) {
return ['ok' => false, 'statut' => 'bloque_trop_echecs'];
}
if (empty($row->place_id)) {
incrementerEchecsTelechargementPhoto($db, $table, $token);
return ['ok' => false, 'statut' => 'place_id_absent'];
}
// 1) Place Details (Legacy) => récupérer photo_reference
// doc: Place Details (Legacy) :contentReference[oaicite:1]{index=1}
$detailsUrl = 'https://maps.googleapis.com/maps/api/place/details/json'
. '?place_id=' . urlencode($row->place_id)
. '&fields=photos'
. '&key=' . urlencode($GOOGLE_SERVER_KEY);
$detailsJson = curlGet($detailsUrl, 8);
if ($detailsJson['ok'] === false) {
incrementerEchecsTelechargementPhoto($db, $table, $token);
return ['ok' => false, 'statut' => 'details_http', 'error' => $detailsJson['error']];
}
$data = json_decode($detailsJson['body'], true);
if (!is_array($data) || ($data['status'] ?? '') !== 'OK') {
incrementerEchecsTelechargementPhoto($db, $table, $token);
return ['ok' => false, 'statut' => 'details_api', 'error' => ($data['status'] ?? 'unknown')];
}
$photoRef = $data['result']['photos'][0]['photo_reference'] ?? '';
if (!$photoRef) {
incrementerEchecsTelechargementPhoto($db, $table, $token);
return ['ok' => false, 'statut' => 'no_photos'];
}
// 2) Place Photo (Legacy) => tĂ©lĂ©charger lâimage (rĂ©ponse souvent en redirect)
// doc: Place Photos (Legacy) :contentReference[oaicite:2]{index=2}
$photoUrl = 'https://maps.googleapis.com/maps/api/place/photo'
. '?maxwidth=' . (int)$maxwidth
. '&photo_reference=' . urlencode($photoRef)
. '&key=' . urlencode($GOOGLE_SERVER_KEY);
$img = curlGetBinary($photoUrl, 12);
if ($img['ok'] === false || empty($img['body'])) {
incrementerEchecsTelechargementPhoto($db, $table, $token);
return ['ok' => false, 'statut' => 'photo_download_failed', 'error' => $img['error'] ?? ''];
}
// Déduire extension (par défaut jpg)
$ctype = strtolower($img['content_type'] ?? '');
$ext = 'jpg';
if ($ctype && strpos($ctype, 'png') !== false) $ext = 'png';
if ($ctype && strpos($ctype, 'webp') !== false) $ext = 'webp';
// 3) Ecriture sur www.myreco.online (PAS find.myreco.online)
$first_letter = strtoupper(substr($row->token, 0, 2));
$dirAbs = rtrim(UPLOADS_WWW_ROOT, '/') . "/upload/$type_dir/$first_letter/";
if (!is_dir($dirAbs) && !mkdir($dirAbs, 0775, true)) {
incrementerEchecsTelechargementPhoto($db, $table, $token);
return ['ok' => false, 'statut' => 'mkdir_failed', 'error' => $dirAbs];
}
$fileAbs = $dirAbs . $row->token . '.' . $ext;
if (@file_put_contents($fileAbs, $img['body']) === false) {
incrementerEchecsTelechargementPhoto($db, $table, $token);
return ['ok' => false, 'statut' => 'write_failed'];
}
$rel = "/upload/$type_dir/$first_letter/" . $row->token . '.' . $ext;
// Reset échecs + maj photo_locale + date insertion
$db->query("UPDATE `$table` SET telechargementphoto_echecs = 0, photo_locale = '".$db->escape($rel)."', photo_locale_dateinsert = NOW() WHERE id = ".(int)$row->id);
return ['ok' => true, 'statut' => 'ok', 'url' => $rel];
}
/* -----------------------
Helpers CURL
------------------------ */
function curlGet(string $url, int $timeout = 8): array
{
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_TIMEOUT => $timeout,
CURLOPT_CONNECTTIMEOUT => $timeout,
CURLOPT_USERAGENT => 'Find.MyReco/1.0 (+https://find.myreco.online)',
]);
$body = curl_exec($ch);
$err = curl_error($ch);
$code = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($body === false || $code >= 400) {
return ['ok' => false, 'error' => $err ?: "HTTP $code", 'body' => (string)$body];
}
return ['ok' => true, 'body' => (string)$body];
}
function curlGetBinary(string $url, int $timeout = 12): array
{
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_TIMEOUT => $timeout,
CURLOPT_CONNECTTIMEOUT => $timeout,
CURLOPT_USERAGENT => 'Find.MyReco/1.0 (+https://find.myreco.online)',
]);
$body = curl_exec($ch);
$err = curl_error($ch);
$code = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
$ctype = (string)curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
curl_close($ch);
if ($body === false || $code >= 400) {
return ['ok' => false, 'error' => $err ?: "HTTP $code"];
}
return ['ok' => true, 'body' => $body, 'content_type' => $ctype];
}
?>