<?php

	function ProxyKontrol($proxy_id){

		global $db;

		$query = $db->from('proxy')->where('proxy_id', $proxy_id)->first();

		
		if($query){

			if($query['proxy_user'] && $query['proxy_pass']){

				$ip = $query['proxy_ip'] . ':' . $query['proxy_port'];
				$user = $query['proxy_user'] . ':' . $query['proxy_pass'];
				

				$link = "https://www.instagram.com/graphql/query/?query_id=17888483320059182&id=314216&first=20&after=";
				$ch = curl_init();
				curl_setopt($ch, CURLOPT_URL, $link);
				curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36');
				curl_setopt($ch, CURLOPT_HEADER, 0);
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
				curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
				curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL , 1);
				
				curl_setopt($ch, CURLOPT_USERAGENT, "bots");
				curl_setopt($ch, CURLOPT_PROXYTYPE, 'HTTPS');
				curl_setopt($ch, CURLOPT_PROXY, $query['proxy_ip']);
				curl_setopt($ch, CURLOPT_PROXYPORT, $query['proxy_port']);
				curl_setopt($ch, CURLOPT_PROXYUSERPWD,$user);

				


				$result = curl_exec($ch);
				curl_close($ch);
				$object		= json_decode($result , true);
				print_r($object);

			}else{ 

				$ip = $query['proxy_ip'] . ':' . $query['proxy_port'];

				
				$link = "https://www.instagram.com/graphql/query/?query_id=17888483320059182&id=314216&first=20&after=";
				$ch = curl_init();
				curl_setopt($ch, CURLOPT_URL, $link);
				curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36');
				curl_setopt($ch, CURLOPT_HEADER, 0);
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
				curl_setopt($ch, CURLOPT_PROXY, $ip);
				$result = curl_exec($ch);
				curl_close($ch);
				$object		= json_decode($result , true);

			}



			if($object){

				$photoCount		=  $object['data']['user']["edge_owner_to_timeline_media"]["count"];
				$photos    		=  $object['data']['user']["edge_owner_to_timeline_media"]["edges"];

				if($photos){

					if($query['proxy_status'] == 0){

						$update = $db->update('proxy')->where('proxy_id', $proxy_id)
						->set(array(
							'proxy_status' => 1
						));

						$return = true;
					}else{

						$return = true;

					}

				}else{

					$return = false;

				}						

			}else{

				$return = false;

			}

		}else{

			$return = false;

		}

		return $return;

	}

	function ProxyOtoSiparis($username){

		global $db;

		$proxyQuery = $db->from('proxy')->where('proxy_status', 1)->orderby('proxy_id', 'ASC')->first();

		if($proxyQuery){

			$Kontrol = ProxyKontrol($proxyQuery['proxy_id']);

			if(!$Kontrol){

				$update = $db->update('proxy')->where('proxy_id', $proxyQuery['proxy_id'])
				->set(array(
					'proxy_status' => 0
				));

				if($update){

					$proxyQuery = $db->from('proxy')->where('proxy_status', 1)->orderby('proxy_id', 'ASC')->first();

				}

			}

			if($proxyQuery['proxy_user'] && $proxyQuery['proxy_pass']){

				$proxyauth = $proxyQuery['proxy_user'].':'.$proxyQuery['proxy_pass'];
				$proxy = $proxyQuery['proxy_ip'].':'.$proxyQuery['proxy_port'];

				$instagramUserId = instagram_usernameConvert($username);
				$link = "https://www.instagram.com/graphql/query/?query_id=17888483320059182&id=314216&first=20&after=";
				$ch = curl_init();
				curl_setopt($ch, CURLOPT_URL, $link);
				curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36');
				curl_setopt($ch, CURLOPT_HEADER, 0);
				curl_setopt($ch, CURLOPT_PROXY, $proxy);
				curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth);
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
				curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 33);
				$result = curl_exec($ch);
				curl_close($ch);
				$object	= json_decode($result , true);

			}else{

				$proxy = $proxyQuery['proxy_ip'].':'.$proxyQuery['proxy_port'];

				$instagramUserId = instagram_usernameConvert($username);
				$link = "https://www.instagram.com/graphql/query/?query_id=17888483320059182&id=314216&first=20&after=";
				$ch = curl_init();
				curl_setopt($ch, CURLOPT_URL, $link);
				curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36');
				curl_setopt($ch, CURLOPT_HEADER, 0);
				curl_setopt($ch, CURLOPT_PROXY, $proxy);
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
				curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 33);
				$result = curl_exec($ch);
				curl_close($ch);
				$object	= json_decode($result , true);

			}

		}else{

			$instagramUserId = instagram_usernameConvert($username);
			$link = "https://www.instagram.com/graphql/query/?query_id=17888483320059182&id=314216&first=20&after=";
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, $link);
			curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36');
			curl_setopt($ch, CURLOPT_HEADER, 0);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 33);
			$result = curl_exec($ch);
			curl_close($ch);
			$object	= json_decode($result , true);

		}

		return $object;

	}