'Please provide a valid domain']); exit; } // Add http:// if not present if (!preg_match('~^(?:f|ht)tps?://~i', $domain)) { $domain = 'http://' . $domain; } // Try to get the content from the domain try { $context = stream_context_create([ 'http' => [ 'timeout' => 30, 'user_agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36', ] ]); $content = @file_get_contents($domain, false, $context); if ($content === false) { echo json_encode(['error' => 'Could not access the domain']); exit; } // Create a DOM object $dom = new DOMDocument(); // Suppress warnings from invalid HTML @$dom->loadHTML($content); // Extract all image elements $images = $dom->getElementsByTagName('img'); $imageUrls = []; foreach ($images as $image) { $src = $image->getAttribute('src'); // Skip empty sources if (empty($src)) { continue; } // Handle relative URLs if (strpos($src, 'http') !== 0) { // If src starts with //, add http: if (strpos($src, '//') === 0) { $src = 'http:' . $src; } // If src starts with /, add domain elseif (strpos($src, '/') === 0) { $parsedUrl = parse_url($domain); $baseUrl = $parsedUrl['scheme'] . '://' . $parsedUrl['host']; $src = $baseUrl . $src; } // Otherwise, assume it's a relative path else { $parsedUrl = parse_url($domain); $baseUrl = $parsedUrl['scheme'] . '://' . $parsedUrl['host']; $path = isset($parsedUrl['path']) ? $parsedUrl['path'] : ''; // Remove filename from path if it exists $path = preg_replace('/\/[^\/]*$/', '/', $path); $src = $baseUrl . $path . $src; } } // Add to our list of URLs if it's not already there if (!in_array($src, $imageUrls)) { $imageUrls[] = $src; } } // Return the list of images echo json_encode(['images' => $imageUrls]); } catch (Exception $e) { echo json_encode(['error' => 'Error: ' . $e->getMessage()]); exit; } // Important: exit after sending JSON to avoid sending HTML too exit; } // Check if this is an AJAX request for uploading images if (isset($_SERVER['CONTENT_TYPE']) && strpos($_SERVER['CONTENT_TYPE'], 'application/json') !== false) { header('Content-Type: application/json'); // Get the raw POST data and decode the JSON $jsonData = file_get_contents('php://input'); $data = json_decode($jsonData, true); // Check if we have images to process if (!isset($data['images']) || empty($data['images'])) { echo json_encode(['error' => 'No images provided']); exit; } // Directory to save images $uploadDir = 'assets/images/media/'; $successCount = 0; $errorMessages = []; // Process each image URL foreach ($data['images'] as $imageUrl) { // Generate a unique filename $fileTitle = uniqid() . '_' . basename(parse_url($imageUrl, PHP_URL_PATH)); $fileName = $uploadDir . $fileTitle; // Clean the filename to avoid security issues //$fileName = preg_replace('/[^a-zA-Z0-9_.-]/', '_', $fileName); try { // Create a context with a timeout for file_get_contents $context = stream_context_create([ 'http' => [ 'timeout' => 30, 'user_agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36' ] ]); // Fetch the image $imageContent = @file_get_contents($imageUrl, false, $context); if ($imageContent === false) { $errorMessages[] = "Failed to download: $imageUrl"; continue; } // Save the image if (file_put_contents($fileName, $imageContent)) { //STORE MEDIA DATA $payload = [ 'title' => $fileTitle, 'full_path' => $fileName ]; $payload = json_encode($payload, JSON_UNESCAPED_UNICODE); //API call $responses = ioServer('/v2/media', $payload); $inserted_media = json_decode($responses,true); //STORE MEDIA RELATED TO DEALER WHEN ROWID IS SEND if (isset($_SESSION['autoFetchRowID']) && $inserted_media['rowID'] !=''){ $dealer_id = $_SESSION['autoFetchRowID']; $payload_2 = json_encode(array("rowID" => $dealer_id, "dealer_media" => $inserted_media['rowID']), JSON_UNESCAPED_UNICODE); //API call ioServer('/v2/dealers', $payload_2); } $successCount++; } else { $errorMessages[] = "Failed to save: $imageUrl"; } } catch (Exception $e) { $errorMessages[] = "Error processing $imageUrl: " . $e->getMessage(); } } // Return the results $result = [ 'success' => $successCount, 'total' => count($data['images']) ]; if (!empty($errorMessages)) { $result['errors'] = $errorMessages; } //RESET S_SESSION VARIABLE if (isset($_SESSION['autoFetchRowID'])){ unset($_SESSION['autoFetchRowID']); } echo json_encode($result); exit; } } template_header('Media_scanner', 'media_scanner', 'manage'); $view ='
Loading images...