From e9dc6acc6ec4dec83766d87860162835f24fd250 Mon Sep 17 00:00:00 2001
From: Adhidarma Hadiwinoto <gua@adhisimon.org>
Date: Wed, 8 Jul 2015 15:15:49 +0700
Subject: [PATCH] initial commit

---
 KiselScrap.php    | 298 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 contoh respon.txt |  22 ++++
 input_session.php |  25 +++++
 kisel.conf.php    |   9 ++
 ping_kisel.php    |  15 +++
 scrap_kisel.php   |  83 +++++++++++++++
 6 files changed, 452 insertions(+)
 create mode 100644 KiselScrap.php
 create mode 100644 contoh respon.txt
 create mode 100644 input_session.php
 create mode 100644 kisel.conf.php
 create mode 100644 ping_kisel.php
 create mode 100644 scrap_kisel.php

diff --git a/KiselScrap.php b/KiselScrap.php
new file mode 100644
index 0000000..3da5a92
--- /dev/null
+++ b/KiselScrap.php
@@ -0,0 +1,298 @@
+<?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 = '';	
+	
+	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) {
+				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 .= "<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 .= "<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 
+				#$this->send_form($this->resp_target, '', $arr_resp);
+			}
+		}
+	}
+
+	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;
+        }
+
+	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);
+	}
+	
+}
+
+?>
diff --git a/contoh respon.txt b/contoh respon.txt
new file mode 100644
index 0000000..65e4645
--- /dev/null
+++ b/contoh respon.txt	
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<trx_response>
+    <product>Telkomsel 10K</product>
+    <msisdn>0812 - 9555655</msisdn>
+    <harga>Rp. 10200.0,-</harga>
+    <ref_num>150707171380</ref_num>
+    <kode_voucher>0031000845623814</kode_voucher>
+    <info>TRANSAKSI SUKSES !!!</info>
+</trx_response>
+
+<?xml version="1.0" encoding="UTF-8"?>
+<trx_response>
+    <product>Telkomsel 10K</product>
+    <msisdn>0815 - 232322</msisdn>
+    <harga>Rp. 10200.0,-</harga>
+    <info>Phone number's not found</info>
+</trx_response>
+
+<?xml version="1.0" encoding="UTF-8"?>
+<trx_response>
+    <info>Error Parsing</info>
+</trx_response>
diff --git a/input_session.php b/input_session.php
new file mode 100644
index 0000000..8cdd070
--- /dev/null
+++ b/input_session.php
@@ -0,0 +1,25 @@
+<?php
+
+include('kisel.conf.php');
+
+$redis = new Redis();
+$redis->connect($redis_url);
+
+$sid = '';
+if (!empty($_REQUEST['session_id'])) {    
+    $sid = $_REQUEST['session_id'];
+    $redis->set($redis_prefix . '01.sessionid', $_REQUEST['session_id']);
+} 
+
+$sid = $redis->get($redis_prefix . '01.sessionid');
+
+?>
+<html>
+<body>
+
+<form  method="POST">
+Session ID: <input type="text" name="session_id" value="<?php echo $sid; ?>"><br>
+<input type="submit" value="Simpan Session">
+</form>
+</body>
+</html>
diff --git a/kisel.conf.php b/kisel.conf.php
new file mode 100644
index 0000000..f77f7b9
--- /dev/null
+++ b/kisel.conf.php
@@ -0,0 +1,9 @@
+<?php
+
+$welcome_url = 'http://112.78.34.155/isolusi/welcome.html';
+$purchasetelco_url = 'http://112.78.34.155/isolusi/purchasetelco.html';
+$resp_target = ""; 
+$redis_url = 'redis.intranet.reload97.com';
+$redis_prefix = 'kisel';
+
+?>
diff --git a/ping_kisel.php b/ping_kisel.php
new file mode 100644
index 0000000..3793760
--- /dev/null
+++ b/ping_kisel.php
@@ -0,0 +1,15 @@
+<?php
+
+include('kisel.conf.php');
+include('KiselScrap.php');
+
+$kisel = new KiselScrap();
+$redis = new Redis();
+
+$redis->connect($redis_url);
+$kisel->setSessionId($redis->get($redis_prefix.'01.sessionid'));
+$kisel->setRandomUserAgent();
+
+# get welcome page
+$kisel->get_page($welcome_url);
+?>
diff --git a/scrap_kisel.php b/scrap_kisel.php
new file mode 100644
index 0000000..cd30874
--- /dev/null
+++ b/scrap_kisel.php
@@ -0,0 +1,83 @@
+<?php
+
+include('kisel.conf.php');
+include('KiselScrap.php');
+
+#$arrinqtelco = array('customerId' => '08122027896',
+#			'product' => 'SI10',
+#			'inquiry' => 'Inquiry',
+#			'_sourcePage' => 'fRCY-X8ZXOIcbgrf1BkkxBM7KTI7S6exLVsnXnUiXn8=',
+#			'__fp' => 'wIlI6mnh04Y='
+#		);
+
+$arrinqtelconext = array('goToPin' => 'next',
+			'_sourcePage' => 'fRCY-X8ZXOIcbgrf1BkkxBM7KTI7S6exLVsnXnUiXn8=',
+			'__fp' => 'wIlI6mnh04Y='
+		);
+
+$kisel = new KiselScrap();
+
+$redis = new Redis();
+$redis->connect($redis_url);
+$kisel->setSessionId($redis->get($redis_prefix.'01.sessionid'));
+
+// get querystring 
+$kisel->setTrxParam($_REQUEST['reqid'], $_REQUEST['msisdn'], $_REQUEST['product']);
+$arrinqtelco = array('customerId' => $kisel->msisdn,
+			'product' => $kisel->product,
+			'inquiry' => 'Inquiry',
+			'_sourcePage' => 'fRCY-X8ZXOIcbgrf1BkkxBM7KTI7S6exLVsnXnUiXn8=',
+			'__fp' => 'wIlI6mnh04Y='
+);
+
+$kisel->setRandomUserAgent();
+
+$log = "";
+// Format the date and time
+$date = date("Y-m-d H:i:s");
+
+
+$log = "[$date] Step-1 : get welcome page\n";
+$log .= "--------------------------------\n";
+$kisel->get_page($welcome_url);
+if(!$kisel->session_status) {
+	print "masuk";
+	$log .= "Error Session Invalid. Please input session";
+	$err = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";	
+	$err .= "<trx_response>\n";
+	$err .= "<product>".$_REQUEST['product']."</product>\n";
+	$err .= "<msisdn>".$_REQUEST['msisdn']."</msisdn>\n";
+	$err .= "<info>SESSION INVALID !!!</info>\n";
+	$err .= "</trx_response>";
+	print $err;
+}
+sleep(1);
+if($kisel->session_status) {
+	$log .= "[$date] Step-2 : get purchase telco page\n";
+	$log .= "--------------------------------\n";
+	$kisel->get_page($purchasetelco_url);
+	#$prevbal = array();
+	#$prevbal = $kisel->balance();
+	#print_r($prevbal);
+	sleep(1);
+
+	$log .= "[$date] Step-3 : send inquiry telco\n";
+	$log .= "--------------------------------\n";
+	$kisel->send_form($purchasetelco_url, $purchasetelco_url, $arrinqtelco);
+	$kisel->parseResponse("//table[@class='tblogin']/tbody/tr/td",0);
+	sleep(1);
+
+	$log .= "[$date] Step-4 : send purchase telco\n";
+	$log .= "--------------------------------\n";
+	$kisel->send_form($purchasetelco_url, $purchasetelco_url, $arrinqtelconext);
+	$kisel->parseResponse("//table[@class='tblogin']/tbody/tr/td",1);
+	$kisel->printResponseXML();
+
+	/*
+	$fd = fopen("scrapper_kisel.log", "a+");
+	fwrite($fd, $log);
+	fclose($fd);
+	*/
+}
+
+?>
-- 
1.9.0