<?php
	set_time_limit(0);
    ini_set('memory_limit', '-1');
    require_once("includes/config.php");
    
	
    //$user_id = 13610;
	$user_id = 561;
    $missingStr = "";
    $result = $conn->query("select * from users where id = ".$user_id);
    if($result->num_rows < 1){
        die("invalid user id");
    }
    $row = $result->fetch_assoc();
    $shopurl = $row['shopurl'];
    $token = $row['token'];
	
	$existingSKUs = array();
	$result = $conn->query("select * from shopifyproducts where user_id = ".$user_id);
	echo $user_id;
	if ($result->num_rows > 0) {
		while($row = $result->fetch_assoc()) {	
			$existingSKUs[] = $row['productid'];
		}
	}
	mysqli_autocommit($conn, FALSE);
	fetchCatalog($user_id, $shopurl, $token, $existingSKUs, "");
	mysqli_commit($conn);
	mysqli_autocommit($conn, TRUE);    
	echo $missingStr;
	@mail("khariwal.rohit@gmail.com", "missing image data - ".$user_id, $missingStr);

	function fetchCatalog($user_id, $shopurl, $token, $existingSKUs, $nextLink){
		echo $user_id;
		global $conn, $missingStr;
		$productUrl = $nextLink;
		if($nextLink == ""){
			$productUrl="https://".$shopurl."/admin/api/2023-04/products.json?limit=100";
		}		
		$session = curl_init();
		curl_setopt($session, CURLOPT_URL, $productUrl);
		curl_setopt($session, CURLOPT_HTTPGET, 1);
		curl_setopt($session, CURLOPT_HEADER, true);
		curl_setopt($session, CURLOPT_HTTPHEADER, array('X-Shopify-Access-Token:'.$token));
		curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($session,CURLOPT_SSL_VERIFYPEER,false);
		$response = curl_exec($session);	
		$response_arr = explode("\n",$response);
		if(strstr(($response_arr[0]), "200")){
			$header_size = curl_getinfo($session, CURLINFO_HEADER_SIZE);
			curl_close($session);
			$header = substr($response, 0, $header_size);
			$body = substr($response, $header_size);
			$products_array = json_decode($body, true);	
			if($products_array){
				foreach($products_array['products'] as $product) {	
					$product_id = $product["id"];
					$images = $product["images"];
					if(count($images) < 1){
						$missingStr = $missingStr."','".$product_id;
					}
				}					
				mysqli_commit($conn);
			}
			$pattern = '/rel="previous",\s*<(.*)>;\s*rel="next"/smiU';
			$res1 = preg_match_all($pattern, $header, $result1);
			sleep(1);
			if($res1){			
				$nextLink = isset($result1[1][0])?trim($result1[1][0]):"";			
				fetchCatalog($user_id, $shopurl, $token, $existingSKUs, $nextLink);
			} else {
			    $pattern = '/Link:\s*<(.*)>;\s*rel="next"/smiU';
			    $res1 = preg_match_all($pattern, $header, $result1);
			    if($res1){			
				    $nextLink = isset($result1[1][0])?trim($result1[1][0]):"";			
				    fetchCatalog($user_id, $shopurl, $token, $existingSKUs, $nextLink);
			    } else {
			        echo $productUrl;
		            print_r($response_arr);        
			    }
			}
		} else if(strstr(($response_arr[0]), "HTTP/2 500")){
		    sleep(1);
		    fetchCatalog($user_id, $shopurl, $token, $existingSKUs, $nextLink);
		} else if(strstr(($response_arr[0]), "HTTP/2 429")){
		    sleep(2);
		    fetchCatalog($user_id, $shopurl, $token, $existingSKUs, $nextLink);
		} else {
		    echo $productUrl;
		    print_r($response_arr);
		}		
	}
?>