From ec7131391d9bccd8e3767f27565be8314039f21e Mon Sep 17 00:00:00 2001 From: frederik Date: Sat, 16 Aug 2025 11:56:57 +0200 Subject: [PATCH] switch to neutral image_provider --- scripts/overview.php | 52 ++++++++++++++++++----------------- scripts/todays_detections.php | 28 ++++++++++--------- 2 files changed, 42 insertions(+), 38 deletions(-) diff --git a/scripts/overview.php b/scripts/overview.php index bde967e..b5fd20f 100644 --- a/scripts/overview.php +++ b/scripts/overview.php @@ -2,7 +2,6 @@ error_reporting(E_ERROR); ini_set('display_errors',1); ini_set('session.gc_maxlifetime', 7200); -ini_set('user_agent', 'PHP_Flickr/1.0'); session_set_cookie_params(7200); session_start(); require_once 'scripts/common.php'; @@ -61,7 +60,7 @@ if(isset($_GET['ajax_detections']) && $_GET['ajax_detections'] == "true" && isse $_SESSION['images'] = []; } $iterations = 0; - $flickr = null; + $image_provider = null; // hopefully one of the 5 most recent detections has an image that is valid, we'll use that one as the most recent detection until the newer ones get their images created while($mostrecent = $result4->fetchArray(SQLITE3_ASSOC)) { @@ -78,24 +77,25 @@ if(isset($_GET['ajax_detections']) && $_GET['ajax_detections'] == "true" && isse $iterations++; - if (!empty($config["FLICKR_API_KEY"])) { - if ($flickr === null) { - $flickr = new Flickr(); - } - if ($_SESSION["FLICKR_FILTER_EMAIL"] !== $flickr->get_uid_from_db()['uid']) { - if (isset($_SESSION["FLICKR_FILTER_EMAIL"])) { + if (!empty($config["IMAGE_PROVIDER"])) { + if ($image_provider === null) { + if ($config["IMAGE_PROVIDER"] === 'FLICKR') { + $image_provider = new Flickr(); + } else { + $image_provider = new Wikipedia(); + } + if ($image_provider->is_reset()) { $_SESSION['images'] = []; } - $_SESSION["FLICKR_FILTER_EMAIL"] = $flickr->get_uid_from_db()['uid']; } - // if we already searched flickr for this species before, use the previous image rather than doing an unneccesary api call + // if we already searched for this species before, use the previous image rather than doing an unneccesary api call $key = array_search($comname, array_column($_SESSION['images'], 0)); if ($key !== false) { $image = $_SESSION['images'][$key]; } else { - $flickr_cache = $flickr->get_image($mostrecent['Sci_Name']); - array_push($_SESSION["images"], array($comname, $flickr_cache["image_url"], $flickr_cache["title"], $flickr_cache["photos_url"], $flickr_cache["author_url"], $flickr_cache["license_url"])); + $cached_image = $image_provider->get_image($mostrecent['Sci_Name']); + array_push($_SESSION["images"], array($comname, $cached_image["image_url"], $cached_image["title"], $cached_image["photos_url"], $cached_image["author_url"], $cached_image["license_url"])); $image = $_SESSION['images'][count($_SESSION['images']) - 1]; } } @@ -123,7 +123,7 @@ if(isset($_GET['ajax_detections']) && $_GET['ajax_detections'] == "true" && isse
- 0) { ?> + 0) { ?>
@@ -348,10 +348,9 @@ while ($row = $result->fetchArray(SQLITE3_ASSOC)) { if (!isset($_SESSION['images'])) { $_SESSION['images'] = []; } -$flickr = null; function display_species($species_list, $title, $show_last_seen=false) { - global $config, $_SESSION, $flickr; + global $config, $_SESSION, $image_provider; $species_count = count($species_list); if ($species_count > 0): ?>
@@ -377,24 +376,27 @@ function display_species($species_list, $title, $show_last_seen=false) { $url_title = $info_url['TITLE']; $image_url = ""; // Default empty image URL - - if (!empty($config["FLICKR_API_KEY"])) { - if ($flickr === null) { - $flickr = new Flickr(); + + if (!empty($config["IMAGE_PROVIDER"])) { + if ($image_provider === null) { + if ($config["IMAGE_PROVIDER"] === 'FLICKR') { + $image_provider = new Flickr(); + } else { + $image_provider = new Wikipedia(); } - if (isset($_SESSION["FLICKR_FILTER_EMAIL"]) && $_SESSION["FLICKR_FILTER_EMAIL"] !== $flickr->get_uid_from_db()['uid']) { - unset($_SESSION['images']); - $_SESSION["FLICKR_FILTER_EMAIL"] = $flickr->get_uid_from_db()['uid']; + if ($image_provider->is_reset()) { + $_SESSION['images'] = []; } + } - // Check if the Flickr image has been cached in the session + // Check if the image has been cached in the session $key = array_search($comname, array_column($_SESSION['images'], 0)); if ($key !== false) { $image = $_SESSION['images'][$key]; } else { // Retrieve the image from Flickr API and cache it - $flickr_cache = $flickr->get_image($todaytable['Sci_Name']); - array_push($_SESSION["images"], array($comname, $flickr_cache["image_url"], $flickr_cache["title"], $flickr_cache["photos_url"], $flickr_cache["author_url"], $flickr_cache["license_url"])); + $cached_image = $image_provider->get_image($todaytable['Sci_Name']); + array_push($_SESSION["images"], array($comname, $cached_image["image_url"], $cached_image["title"], $cached_image["photos_url"], $cached_image["author_url"], $cached_image["license_url"])); $image = $_SESSION['images'][count($_SESSION['images']) - 1]; } $image_url = $image[1] ?? ""; // Get the image URL if available diff --git a/scripts/todays_detections.php b/scripts/todays_detections.php index de66ba5..142bcba 100644 --- a/scripts/todays_detections.php +++ b/scripts/todays_detections.php @@ -5,7 +5,6 @@ $_GET = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING); $_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING); ini_set('session.gc_maxlifetime', 7200); -ini_set('user_agent', 'PHP_Flickr/1.0'); session_set_cookie_params(7200); session_start(); error_reporting(E_ERROR); @@ -186,7 +185,7 @@ if(isset($_GET['ajax_detections']) && $_GET['ajax_detections'] == "true" ) { $_SESSION['images'] = []; } $iterations = 0; - $flickr = null; + $image_provider = null; while($todaytable=$result0->fetchArray(SQLITE3_ASSOC)) { @@ -205,13 +204,16 @@ if(isset($_GET['ajax_detections']) && $_GET['ajax_detections'] == "true" ) { $url = $info_url['URL']; $url_title = $info_url['TITLE']; - if (!empty($config["FLICKR_API_KEY"]) && (isset($_GET['display_limit']) || isset($_GET['hard_limit']) || $_GET['kiosk'] == true) ) { - if ($flickr === null) { - $flickr = new Flickr(); - } - if (isset($_SESSION["FLICKR_FILTER_EMAIL"]) && $_SESSION["FLICKR_FILTER_EMAIL"] !== $flickr->get_uid_from_db()['uid']) { - unset($_SESSION['images']); - $_SESSION["FLICKR_FILTER_EMAIL"] = $flickr->get_uid_from_db()['uid']; + if (!empty($config["IMAGE_PROVIDER"])) { + if ($image_provider === null) { + if ($config["IMAGE_PROVIDER"] === 'FLICKR') { + $image_provider = new Flickr(); + } else { + $image_provider = new Wikipedia(); + } + if ($image_provider->is_reset()) { + $_SESSION['images'] = []; + } } // if we already searched flickr for this species before, use the previous image rather than doing an unneccesary api call @@ -219,8 +221,8 @@ if(isset($_GET['ajax_detections']) && $_GET['ajax_detections'] == "true" ) { if ($key !== false) { $image = $_SESSION['images'][$key]; } else { - $flickr_cache = $flickr->get_image($todaytable['Sci_Name']); - array_push($_SESSION["images"], array($comname, $flickr_cache["image_url"], $flickr_cache["title"], $flickr_cache["photos_url"], $flickr_cache["author_url"], $flickr_cache["license_url"])); + $cached_image = $image_provider->get_image($todaytable['Sci_Name']); + array_push($_SESSION["images"], array($comname, $cached_image["image_url"], $cached_image["title"], $cached_image["photos_url"], $cached_image["author_url"], $cached_image["license_url"])); $image = $_SESSION['images'][count($_SESSION['images']) - 1]; } } @@ -233,7 +235,7 @@ if(isset($_GET['ajax_detections']) && $_GET['ajax_detections'] == "true" ) {
- 0) { ?> + 0) { ?> @@ -252,7 +254,7 @@ if(isset($_GET['ajax_detections']) && $_GET['ajax_detections'] == "true" ) {
- 0) { ?> + 0) { ?>