diff --git a/endpoints/logos/search.php b/endpoints/logos/search.php index 3d1022a0a..f3105a54d 100644 --- a/endpoints/logos/search.php +++ b/endpoints/logos/search.php @@ -1,9 +1,10 @@ 'Failed to fetch data from Google.']); + // If cURL fails to access google images, use brave image search as a backup + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $backupUrl); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + $envVars = array_change_key_case($_SERVER, CASE_LOWER); + $httpProxy = isset($envVars['http_proxy']) ? $envVars['http_proxy'] : null; + $httpsProxy = isset($envVars['https_proxy']) ? $envVars['https_proxy'] : null; + if (!empty($httpProxy)) { + curl_setopt($ch, CURLOPT_PROXY, $httpProxy); + } elseif (!empty($httpsProxy)) { + curl_setopt($ch, CURLOPT_PROXY, $httpsProxy); + } + $response = curl_exec($ch); + if ($response === false) { + echo json_encode(['error' => 'Failed to fetch data from Google.']); + } else { + $imageUrls = extractImageUrlsFromPage($response); + header('Content-Type: application/json'); + echo json_encode(['imageUrls' => $imageUrls]); + } } else { // Parse the HTML response to extract image URLs - $imageUrls = extractImageUrlsFromGoogle($response); + $imageUrls = extractImageUrlsFromPage($response); // Pass the image URLs to the client header('Content-Type: application/json'); @@ -39,7 +59,7 @@ echo json_encode(['error' => 'Invalid request.']); } - function extractImageUrlsFromGoogle($html) { + function extractImageUrlsFromPage($html) { $imageUrls = []; $doc = new DOMDocument(); @@ -48,8 +68,10 @@ function extractImageUrlsFromGoogle($html) { $imgTags = $doc->getElementsByTagName('img'); foreach ($imgTags as $imgTag) { $src = $imgTag->getAttribute('src'); - if (filter_var($src, FILTER_VALIDATE_URL)) { - $imageUrls[] = $src; + if (!strstr($imgTag->getAttribute('class'), "favicon") && !strstr($imgTag->getAttribute('class'), "logo")) { + if (filter_var($src, FILTER_VALIDATE_URL)) { + $imageUrls[] = $src; + } } } diff --git a/includes/version.php b/includes/version.php index 01bc67c2f..9678a216c 100644 --- a/includes/version.php +++ b/includes/version.php @@ -1,3 +1,3 @@ \ No newline at end of file diff --git a/scripts/dashboard.js b/scripts/dashboard.js index b0a235b41..6a162f70f 100644 --- a/scripts/dashboard.js +++ b/scripts/dashboard.js @@ -213,6 +213,9 @@ function displayImageResults(imageSources) { img.onclick = function() { selectWebLogo(src); }; + img.onerror = function() { + this.parentNode.removeChild(this); + }; logoResults.appendChild(img); }); }