KiselScrap.php 10 KB
<?php

Class KiselScrap {

	# Object properties
	var $reqid = '';
	var $msisdn = '';
	var $product = '';	
	var $user_agent = '';
	var $session_id = '';
	var $session_status = 1;
	var $resp_str = '';
	var $resp_target = '';
	var $resp_xml = '';
	var $source_page = '';
	var $fp = '';	
	
	function setRandomUserAgent()
	{
    		$userAgents=array(
        			"Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)",
				"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6",
        			"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)",
        			"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)",
        			"Opera/9.20 (Windows NT 6.0; U; en)",
        			"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; en) Opera 8.50",
        			"Mozilla/4.0 (compatible; MSIE 6.0; MSIE 5.5; Windows NT 5.1) Opera 7.02 [en]",
        			"Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; fr; rv:1.7) Gecko/20040624 Firefox/0.9",
        			"Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/48 (like Gecko) Safari/48"       
    			);
    		$random = rand(0,count($userAgents)-1);
 
    		$this->user_agent = $userAgents[$random];
	}

	function setTrxParam($reqid, $msisdn, $product) {
		$this->reqid = $reqid;
		$this->msisdn = $msisdn;
		$this->product = $product;
	}

	function getRandomUserAgent() {
		return $this->user_agent;
	}

	function get_page($url){
 
		if (!function_exists('curl_init')){
			die('Sorry cURL is not installed!');
		}
 
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_REFERER, $url);
		curl_setopt($ch, CURLOPT_USERAGENT, $this->getRandomUserAgent());
		curl_setopt($ch, CURLOPT_HEADER, 1);
	
		curl_setopt($ch, CURLOPT_COOKIE, 'JSESSIONID=' . $this->getSessionId());
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		session_write_close();
		$output = curl_exec($ch);
			
		$date = date("Y-m-d H:i:s");
		if(!curl_errno($ch))
                {
                        $info = curl_getinfo($ch);
                        $log = "[$date] Took " . $info['total_time'] . ' seconds to send a request to ' . $info['url']."\n";
                } else {
                        $log = "[$date] error" . curl_errno($ch)."\n";
                }
				
		$fd = fopen("scrapper_kisel.log", "a+");
		fwrite($fd, $log);
		fclose($fd);

		curl_close($ch);
			
		if(preg_match("/HTTP\/1\.1 302 Found/i", $output)) {
			$this->session_status = -1; 
		} 
		return 1;
	}

	/**
 	* Determine if a variable is iterable. i.e. can be used to loop over.
 	*
 	* @return bool
 	*/
	function isIterable($var)
	{
    		return $var !== null 
        	&& (is_array($var) 
            	|| $var instanceof Traversable 
            	|| $var instanceof Iterator 
            	|| $var instanceof IteratorAggregate);
	}

	function clearResponseStr() {
		$this->resp_str = '';
	}

	function printResponseStr() {
		print $this->resp_str."\n";
	}

	function printResponseXML() {
		print $this->resp_xml;
	}

	function parseResponse($query,$type) {
		$dom = new DOMDocument;
                libxml_use_internal_errors(true);
		#print $this->resp_str;
                if (!@$dom->loadHTML($this->resp_str))
                {
                        $errors="";
                        foreach (libxml_get_errors() as $error)  {
                                $errors.=$error->message."\n";
                        }
                        libxml_clear_errors();
                        print "libxml errors: $errors \n";
                        return;
                }

		$xpath = new DOMXPath($dom);
                $entries = $xpath->query($query);

		if ($this->isIterable($entries))
		{
			$resp_value = '';
			foreach ($entries as $entry) {
				if(ltrim(rtrim($entry->nodeValue)) != '' and !preg_match("/setTimeout/",$entry->nodeValue)) {
					$resp_value .= ltrim(rtrim($entry->nodeValue))."|";
				}
			}
			$arr_resp = explode("|", $resp_value);
			
			if($type == 1) {
				$strBal = $this->balance();
				if (preg_match("/TRANSAKSI SUKSES/i", $this->resp_str)) {
					if(count($arr_resp) > 14){
						$xml_str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";	
						$xml_str .= "<trx_response>";
						$xml_str .= "<product>$arr_resp[5]</product>";
						$xml_str .= "<msisdn>$arr_resp[7]</msisdn>";
						$xml_str .= "<harga>$arr_resp[11]</harga>";
						$xml_str .= "<ref_num>$arr_resp[3]</ref_num>";
						$xml_str .= "<kode_voucher>$arr_resp[13]</kode_voucher>";
						$xml_str .= "<stock>$strBal</stock>";
						$xml_str .= "<info>$arr_resp[14]</info>";
						$xml_str .= "</trx_response>";
					}
					else {
						$xml_str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";	
						$xml_str .= "<trx_response>";
						$xml_str .= "<info>Error Parsing</info>";
						$xml_str .= "</trx_response>";
					}
				} else {
					if(count($arr_resp) > 13){	
						$xml_str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";	
						$xml_str .= "<trx_response>";
						$xml_str .= "<product>$arr_resp[5]</product>";
						$xml_str .= "<msisdn>$arr_resp[7]</msisdn>";
						$xml_str .= "<harga>$arr_resp[11]</harga>";
						$xml_str .= "<stock>$strBal</stock>";
						$xml_str .= "<info>$arr_resp[13]</info>";
						$xml_str .= "</trx_response>";
					}
					else {
						$xml_str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";	
						$xml_str .= "<trx_response>";
						$xml_str .= "<info>Error Parsing</info>";
						$xml_str .= "</trx_response>";
					}
				}
				
				$this->resp_xml = $xml_str;
				# hit response transaksi ke ST24 
			}
			else {
				preg_match("/name=\"_sourcePage\" value=\"(\\S+)\"/s", $this->resp_str, $matches);
                		$this->source_page = $matches[1];

                		preg_match("/name=\"__fp\" value=\"(\\S+)\"/s", $this->resp_str, $matches2);
                		$this->fp = $matches2[1];
			}
		}
	}

	function balance() {
                $dom = new DOMDocument;
                libxml_use_internal_errors(true);
                if (!@$dom->loadHTML($this->resp_str))
                {
                        $errors="";
                        foreach (libxml_get_errors() as $error)  {
                                $errors.=$error->message."\n";
                        }
                        libxml_clear_errors();
                        print "libxml errors: $errors \n";
                        return;
                }

                $xpath = new DOMXPath($dom);
                $entries = $xpath->query("//div[@id='header']/table[@width='98%']/tr/td[@align='center']");

                $resp_value = '';
                foreach ($entries as $entry) {
                        if(ltrim(rtrim($entry->nodeValue)) != '' ) {
                                $resp_value .= $entry->nodeValue;
                        }
                }
                $resp_value = ltrim(rtrim($resp_value));
                
		#print $resp_value."\n";
                # Stock Anda Saat ini : ; 10K = 1; 20K = 1; 25K = 0; 50K = 1; 100K = 1; 150K = 1; 200K = 1; 300K = 1; 500K = 1
                #preg_match("/^Stock Anda Saat ini : ; (\\S+) = (\\S+); (\\S+) = (\\S+); (\\S+) = (\\S+); (\\S+) = (\\S+); (\\S+) = (\\S+); (\\S+) = (\\S+); (\\S+) = (\\S+); (\\S+) = (\\S+); (\\S+) = (\\S+)/s", $resp_value, $matches);       
                #print_r($matches);
                #$arr_stk = array();
		#$arr_stk[$matches[1]] = $matches[2];
                #$arr_stk[$matches[3]] = $matches[4];
                #$arr_stk[$matches[5]] = $matches[6];
                #$arr_stk[$matches[7]] = $matches[8];
                #$arr_stk[$matches[9]] = $matches[10];
                #$arr_stk[$matches[11]] = $matches[12];
                #$arr_stk[$matches[13]] = $matches[14];
                #$arr_stk[$matches[15]] = $matches[16];
                #$arr_stk[$matches[17]] = $matches[18];
                #return $arr_stk;

		preg_match("/^Stock Anda Saat ini : ; (\\S+ \= \\S+ \\S+ \= \\S+ \\S+ \= \\S+ \\S+ \= \\S+ \\S+ \= \\S+ \\S+ \= \\S+ \\S+ \= \\S+ \\S+ \= \\S+ \\S+ \= \\S+)/s", $resp_value, $matches);        
                return $matches[1];
        }

	function send_form($url, $url_ref, $fields){

		$postvars = "";
		foreach($fields as $k => $v) 
   		{ 
      			$postvars .= $k . '='.$v.'&'; 
   		}
   		$postvars = rtrim($postvars, '&');	
  			
		if (!function_exists('curl_init')){
			die('Sorry cURL is not installed!');
		}

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL,$url);
  		curl_setopt($ch, CURLOPT_REFERER, $url_ref);
		curl_setopt($ch, CURLOPT_USERAGENT, $this->getRandomUserAgent());
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($ch, CURLOPT_HEADER, 1);

		curl_setopt($ch, CURLOPT_COOKIE, "JSESSIONID=".$this->getSessionId());
		curl_setopt($ch, CURLOPT_POST, count($fields));
  		curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		unset($postvars);

		ob_start();
		$resp_str = curl_exec($ch);
		ob_end_clean();
		
		$date = date("Y-m-d H:i:s");
		if(!curl_errno($ch))
                {
                        $info = curl_getinfo($ch);
                        $log = "[$date] Took " . $info['total_time'] . ' seconds to send a request to ' . $info['url']."\n";
                } else {
                        $log = "[$date] error" . curl_errno($ch)."\n";
                }
				
		$fd = fopen("scrapper_kisel.log", "a+");
		fwrite($fd, $log);
		fclose($fd);
                curl_close ($ch);
                $this->resp_str =  $resp_str;
		return 1;

	}

	function setSessionId($session_id){
		$this->session_id = $session_id;
	}

	function getSessionId() {
		return $this->session_id;
	}

	
	function do_logout($sess_id) {
		if (!function_exists('curl_init')){
			die('Sorry cURL is not installed!');
		}
 
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_REFERER, $url_ref);
		curl_setopt($ch, CURLOPT_USERAGENT, $this->getRandomUserAgent());
		curl_setopt($ch, CURLOPT_HEADER, 1);
		curl_setopt($ch, CURLOPT_COOKIE, "JSESSIONID=".$this->get_sessionid());
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_exec($ch);
		curl_close($ch);
	}
	
}

?>