HifiPlanet-Website/hifi/api/src/Services/GooglePlacesReviewsFetcher.php
2026-10-20 20:44:27 +00:00

228 lines
9 KiB
PHP

<?php
namespace App\Services;
use PDO;
/**
* Holt Gesamtbewertung + die (von Google gewaehlten, max. 5) neuesten Rezensionen
* eines Google-Business-Eintrags ueber die Places API (New) und cached sie in der
* eigenen Datenbank (Tabelle google_reviews + app_settings.google_rating*). Die
* oeffentliche Seite liest ausschliesslich aus diesem Cache - nie live von Google,
* damit kein Seitenaufruf ein API-Kontingent verbraucht (siehe refresh_google_reviews.php
* fuer den geplanten, taeglichen Refresh).
*/
class GooglePlacesReviewsFetcher
{
private const ENDPOINT = 'https://places.googleapis.com/v1/places/';
private const FIELD_MASK = 'displayName,rating,userRatingCount,reviews';
// Profilfotos der Bewerter werden beim Refresh heruntergeladen und lokal ausgeliefert -
// direkt von googleusercontent.com geladen ginge bei jedem Seitenaufruf die IP-Adresse
// des Besuchers ohne Einwilligung an Google (siehe Datenschutzerklaerung).
private const PHOTO_DIR = __DIR__ . '/../../uploads/reviews/';
private const PHOTO_MAX_BYTES = 512 * 1024;
private int $timeout;
public function __construct(int $timeout = 12)
{
$this->timeout = $timeout;
}
/**
* Reiner API-Aufruf ohne Datenbankzugriff - eigenstaendig testbar.
* @return array{ok: bool, rating?: float, rating_count?: int, reviews?: array, error?: string}
*/
public function fetch(string $apiKey, string $placeId): array
{
$ch = curl_init(self::ENDPOINT . rawurlencode($placeId));
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => $this->timeout,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_HTTPHEADER => [
'X-Goog-Api-Key: ' . $apiKey,
'X-Goog-FieldMask: ' . self::FIELD_MASK,
'Accept-Language: de',
],
]);
$body = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlError = curl_error($ch);
curl_close($ch);
if ($body === false || $curlError !== '') {
return ['ok' => false, 'error' => 'Verbindung zu Google fehlgeschlagen: ' . $curlError];
}
$json = json_decode($body, true);
if ($status !== 200) {
$message = is_array($json) ? ($json['error']['message'] ?? null) : null;
return ['ok' => false, 'error' => "Google-Antwort HTTP $status" . ($message ? ": $message" : '')];
}
if (!is_array($json)) {
return ['ok' => false, 'error' => 'Ungültige Antwort von Google (kein JSON)'];
}
$reviews = [];
foreach ((array) ($json['reviews'] ?? []) as $review) {
$reviews[] = [
'google_review_id' => is_string($review['name'] ?? null) ? $review['name'] : null,
'author_name' => (string) ($review['authorAttribution']['displayName'] ?? 'Google-Nutzer'),
'profile_photo_url' => $review['authorAttribution']['photoUri'] ?? null,
'rating' => (int) ($review['rating'] ?? 5),
'review_text' => $review['text']['text'] ?? ($review['originalText']['text'] ?? null),
'relative_time_description' => $review['relativePublishTimeDescription'] ?? null,
'review_time' => isset($review['publishTime']) ? strtotime((string) $review['publishTime']) ?: null : null,
];
}
return [
'ok' => true,
'name' => $json['displayName']['text'] ?? null,
'rating' => isset($json['rating']) ? (float) $json['rating'] : null,
'rating_count' => isset($json['userRatingCount']) ? (int) $json['userRatingCount'] : null,
'reviews' => $reviews,
];
}
/**
* Ruft fetch() auf und schreibt das Ergebnis in die Datenbank. Bei einem Fehler
* bleiben zuvor erfolgreich geladene Rezensionen unangetastet (die Seite zeigt
* dann weiter den letzten guten Stand) - nur die Fehlermeldung wird vermerkt,
* damit sie im Admin-Bereich sichtbar ist.
* @return array{ok: bool, error?: string, rating?: float, rating_count?: int, reviews_count?: int}
*/
public function refreshAndStore(PDO $db, string $apiKey, string $placeId): array
{
if ($apiKey === '' || $placeId === '') {
$error = 'Kein API-Key oder keine Place-ID hinterlegt';
$this->recordError($db, $error);
return ['ok' => false, 'error' => $error];
}
$result = $this->fetch($apiKey, $placeId);
if (!$result['ok']) {
$this->recordError($db, $result['error']);
return $result;
}
$photoNames = [];
foreach ($result['reviews'] as $i => $review) {
$local = $this->storePhoto($review['profile_photo_url']);
$result['reviews'][$i]['profile_photo_url'] = $local;
if ($local !== null) {
$photoNames[] = basename($local);
}
}
$db->beginTransaction();
try {
$db->exec('DELETE FROM google_reviews');
$stmt = $db->prepare(
'INSERT INTO google_reviews
(google_review_id, author_name, profile_photo_url, rating, review_text, relative_time_description, review_time, sort_order)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)'
);
foreach ($result['reviews'] as $i => $review) {
$stmt->execute([
$review['google_review_id'],
$review['author_name'],
$review['profile_photo_url'],
$review['rating'],
$review['review_text'],
$review['relative_time_description'],
$review['review_time'],
$i,
]);
}
$db->exec('INSERT IGNORE INTO app_settings (id) VALUES (1)');
$update = $db->prepare(
'UPDATE app_settings
SET google_rating = ?, google_rating_count = ?, google_reviews_updated_at = NOW(), google_reviews_error = NULL
WHERE id = 1'
);
$update->execute([$result['rating'], $result['rating_count']]);
$db->commit();
$this->removeStalePhotos($photoNames);
} catch (\Throwable $e) {
$db->rollBack();
$error = 'Speichern fehlgeschlagen: ' . $e->getMessage();
$this->recordError($db, $error);
return ['ok' => false, 'error' => $error];
}
return [
'ok' => true,
'rating' => $result['rating'],
'rating_count' => $result['rating_count'],
'reviews_count' => count($result['reviews']),
];
}
/**
* Laedt ein Profilfoto von Google herunter und legt es unter uploads/reviews/ ab.
* Dateiname = Hash der URL, damit ein taeglicher Refresh unveraenderte Fotos nicht neu
* schreibt. Liefert den oeffentlichen Pfad oder null (dann zeigt die Seite Initialen).
*/
private function storePhoto(?string $url): ?string
{
if (!is_string($url) || !preg_match('#^https://[a-z0-9.-]+\.googleusercontent\.com/#i', $url)) {
return null;
}
if (!is_dir(self::PHOTO_DIR) && !@mkdir(self::PHOTO_DIR, 0755, true)) {
return null;
}
$filename = sha1($url) . '.jpg';
$destination = self::PHOTO_DIR . $filename;
if (!is_file($destination)) {
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => $this->timeout,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 3,
]);
$body = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$type = (string) curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
curl_close($ch);
if (!is_string($body) || $status !== 200 || $body === '' || strlen($body) > self::PHOTO_MAX_BYTES
|| !str_starts_with(strtolower($type), 'image/')) {
return null;
}
if (@file_put_contents($destination, $body) === false) {
return null;
}
}
$config = require __DIR__ . '/../../config/config.php';
return rtrim($config['app']['base_path'], '/') . '/api/uploads/reviews/' . $filename;
}
/** Entfernt Fotos von Rezensionen, die Google nicht mehr liefert. */
private function removeStalePhotos(array $keep): void
{
foreach (glob(self::PHOTO_DIR . '*.jpg') ?: [] as $file) {
if (!in_array(basename($file), $keep, true)) {
@unlink($file);
}
}
}
private function recordError(PDO $db, string $error): void
{
$db->exec('INSERT IGNORE INTO app_settings (id) VALUES (1)');
$stmt = $db->prepare('UPDATE app_settings SET google_reviews_error = ? WHERE id = 1');
$stmt->execute([$error]);
}
}