Commit e9dc6acc6ec4dec83766d87860162835f24fd250

Authored by Adhidarma Hadiwinoto
0 parents
Exists in master

initial commit

Showing 6 changed files with 452 additions and 0 deletions Side-by-side Diff

... ... @@ -0,0 +1,298 @@
  1 +<?php
  2 +
  3 +Class KiselScrap {
  4 +
  5 + # Object properties
  6 + var $reqid = '';
  7 + var $msisdn = '';
  8 + var $product = '';
  9 + var $user_agent = '';
  10 + var $session_id = '';
  11 + var $session_status = 1;
  12 + var $resp_str = '';
  13 + var $resp_target = '';
  14 + var $resp_xml = '';
  15 +
  16 + function setRandomUserAgent()
  17 + {
  18 + $userAgents=array(
  19 + "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)",
  20 + "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6",
  21 + "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)",
  22 + "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)",
  23 + "Opera/9.20 (Windows NT 6.0; U; en)",
  24 + "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; en) Opera 8.50",
  25 + "Mozilla/4.0 (compatible; MSIE 6.0; MSIE 5.5; Windows NT 5.1) Opera 7.02 [en]",
  26 + "Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; fr; rv:1.7) Gecko/20040624 Firefox/0.9",
  27 + "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/48 (like Gecko) Safari/48"
  28 + );
  29 + $random = rand(0,count($userAgents)-1);
  30 +
  31 + $this->user_agent = $userAgents[$random];
  32 + }
  33 +
  34 + function setTrxParam($reqid, $msisdn, $product) {
  35 + $this->reqid = $reqid;
  36 + $this->msisdn = $msisdn;
  37 + $this->product = $product;
  38 + }
  39 +
  40 + function getRandomUserAgent() {
  41 + return $this->user_agent;
  42 + }
  43 +
  44 + function get_page($url){
  45 +
  46 + if (!function_exists('curl_init')){
  47 + die('Sorry cURL is not installed!');
  48 + }
  49 +
  50 + $ch = curl_init();
  51 + curl_setopt($ch, CURLOPT_URL, $url);
  52 + curl_setopt($ch, CURLOPT_REFERER, $url);
  53 + curl_setopt($ch, CURLOPT_USERAGENT, $this->getRandomUserAgent());
  54 + curl_setopt($ch, CURLOPT_HEADER, 1);
  55 +
  56 + curl_setopt($ch, CURLOPT_COOKIE, 'JSESSIONID=' . $this->getSessionId());
  57 + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  58 + session_write_close();
  59 + $output = curl_exec($ch);
  60 +
  61 + $date = date("Y-m-d H:i:s");
  62 + if(!curl_errno($ch))
  63 + {
  64 + $info = curl_getinfo($ch);
  65 + $log = "[$date] Took " . $info['total_time'] . ' seconds to send a request to ' . $info['url']."\n";
  66 + } else {
  67 + $log = "[$date] error" . curl_errno($ch)."\n";
  68 + }
  69 +
  70 + #$fd = fopen("scrapper_kisel.log", "a+");
  71 + #fwrite($fd, $log);
  72 + #fclose($fd);
  73 +
  74 + curl_close($ch);
  75 +
  76 + if(preg_match("/HTTP\/1\.1 302 Found/i", $output)) {
  77 + $this->session_status = -1;
  78 + }
  79 + return 1;
  80 + }
  81 +
  82 + /**
  83 + * Determine if a variable is iterable. i.e. can be used to loop over.
  84 + *
  85 + * @return bool
  86 + */
  87 + function isIterable($var)
  88 + {
  89 + return $var !== null
  90 + && (is_array($var)
  91 + || $var instanceof Traversable
  92 + || $var instanceof Iterator
  93 + || $var instanceof IteratorAggregate);
  94 + }
  95 +
  96 + function clearResponseStr() {
  97 + $this->resp_str = '';
  98 + }
  99 +
  100 + function printResponseStr() {
  101 + print $this->resp_str."\n";
  102 + }
  103 +
  104 + function printResponseXML() {
  105 + print $this->resp_xml;
  106 + }
  107 +
  108 + function parseResponse($query,$type) {
  109 + $dom = new DOMDocument;
  110 + libxml_use_internal_errors(true);
  111 + #print $this->resp_str;
  112 + if (!@$dom->loadHTML($this->resp_str))
  113 + {
  114 + $errors="";
  115 + foreach (libxml_get_errors() as $error) {
  116 + $errors.=$error->message."\n";
  117 + }
  118 + libxml_clear_errors();
  119 + print "libxml errors: $errors \n";
  120 + return;
  121 + }
  122 +
  123 + $xpath = new DOMXPath($dom);
  124 + $entries = $xpath->query($query);
  125 +
  126 + if ($this->isIterable($entries))
  127 + {
  128 + $resp_value = '';
  129 + foreach ($entries as $entry) {
  130 + if(ltrim(rtrim($entry->nodeValue)) != '' and !preg_match("/setTimeout/",$entry->nodeValue)) {
  131 + $resp_value .= ltrim(rtrim($entry->nodeValue))."|";
  132 + }
  133 + }
  134 + $arr_resp = explode("|", $resp_value);
  135 +
  136 + if($type == 1) {
  137 + if (preg_match("/TRANSAKSI SUKSES/i", $this->resp_str)) {
  138 + if(count($arr_resp) > 14){
  139 + $xml_str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
  140 + $xml_str .= "<trx_response>";
  141 + $xml_str .= "<product>$arr_resp[5]</product>";
  142 + $xml_str .= "<msisdn>$arr_resp[7]</msisdn>";
  143 + $xml_str .= "<harga>$arr_resp[11]</harga>";
  144 + $xml_str .= "<ref_num>$arr_resp[3]</ref_num>";
  145 + $xml_str .= "<kode_voucher>$arr_resp[13]</kode_voucher>";
  146 + $xml_str .= "<info>$arr_resp[14]</info>";
  147 + $xml_str .= "</trx_response>";
  148 + }
  149 + else {
  150 + $xml_str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
  151 + $xml_str .= "<trx_response>";
  152 + $xml_str .= "<info>Error Parsing</info>";
  153 + $xml_str .= "</trx_response>";
  154 + }
  155 + } else {
  156 + if(count($arr_resp) > 13){
  157 + $xml_str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
  158 + $xml_str .= "<trx_response>";
  159 + $xml_str .= "<product>$arr_resp[5]</product>";
  160 + $xml_str .= "<msisdn>$arr_resp[7]</msisdn>";
  161 + $xml_str .= "<harga>$arr_resp[11]</harga>";
  162 + $xml_str .= "<info>$arr_resp[13]</info>";
  163 + $xml_str .= "</trx_response>";
  164 + }
  165 + else {
  166 + $xml_str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
  167 + $xml_str .= "<trx_response>";
  168 + $xml_str .= "<info>Error Parsing</info>";
  169 + $xml_str .= "</trx_response>";
  170 + }
  171 + }
  172 +
  173 + $this->resp_xml = $xml_str;
  174 + # hit response transaksi ke ST24
  175 + #$this->send_form($this->resp_target, '', $arr_resp);
  176 + }
  177 + }
  178 + }
  179 +
  180 + function balance() {
  181 + $dom = new DOMDocument;
  182 + libxml_use_internal_errors(true);
  183 + if (!@$dom->loadHTML($this->resp_str))
  184 + {
  185 + $errors="";
  186 + foreach (libxml_get_errors() as $error) {
  187 + $errors.=$error->message."\n";
  188 + }
  189 + libxml_clear_errors();
  190 + print "libxml errors: $errors \n";
  191 + return;
  192 + }
  193 +
  194 + $xpath = new DOMXPath($dom);
  195 + $entries = $xpath->query("//div[@id='header']/table[@width='98%']/tr/td[@align='center']");
  196 +
  197 + $resp_value = '';
  198 + foreach ($entries as $entry) {
  199 + if(ltrim(rtrim($entry->nodeValue)) != '' ) {
  200 + $resp_value .= $entry->nodeValue;
  201 + }
  202 + }
  203 + $resp_value = ltrim(rtrim($resp_value));
  204 + print $resp_value."\n";
  205 + # Stock Anda Saat ini : ; 10K = 1; 20K = 1; 25K = 0; 50K = 1; 100K = 1; 150K = 1; 200K = 1; 300K = 1; 500K = 1
  206 + 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);
  207 + #print_r($matches);
  208 + $arr_stk = array();
  209 + $arr_stk[$matches[1]] = $matches[2];
  210 + $arr_stk[$matches[3]] = $matches[4];
  211 + $arr_stk[$matches[5]] = $matches[6];
  212 + $arr_stk[$matches[7]] = $matches[8];
  213 + $arr_stk[$matches[9]] = $matches[10];
  214 + $arr_stk[$matches[11]] = $matches[12];
  215 + $arr_stk[$matches[13]] = $matches[14];
  216 + $arr_stk[$matches[15]] = $matches[16];
  217 + $arr_stk[$matches[17]] = $matches[18];
  218 + return $arr_stk;
  219 + }
  220 +
  221 + function send_form($url, $url_ref, $fields){
  222 +
  223 + $postvars = "";
  224 + foreach($fields as $k => $v)
  225 + {
  226 + $postvars .= $k . '='.$v.'&';
  227 + }
  228 + $postvars = rtrim($postvars, '&');
  229 +
  230 + if (!function_exists('curl_init')){
  231 + die('Sorry cURL is not installed!');
  232 + }
  233 +
  234 + $ch = curl_init();
  235 + curl_setopt($ch, CURLOPT_URL,$url);
  236 + curl_setopt($ch, CURLOPT_REFERER, $url_ref);
  237 + curl_setopt($ch, CURLOPT_USERAGENT, $this->getRandomUserAgent());
  238 + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  239 + curl_setopt($ch, CURLOPT_HEADER, 1);
  240 +
  241 + curl_setopt($ch, CURLOPT_COOKIE, "JSESSIONID=".$this->getSessionId());
  242 + curl_setopt($ch, CURLOPT_POST, count($fields));
  243 + curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
  244 + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  245 + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  246 + unset($postvars);
  247 +
  248 + ob_start();
  249 + $resp_str = curl_exec($ch);
  250 + ob_end_clean();
  251 +
  252 + $date = date("Y-m-d H:i:s");
  253 + if(!curl_errno($ch))
  254 + {
  255 + $info = curl_getinfo($ch);
  256 + $log = "[$date] Took " . $info['total_time'] . ' seconds to send a request to ' . $info['url']."\n";
  257 + } else {
  258 + $log = "[$date] error" . curl_errno($ch)."\n";
  259 + }
  260 +
  261 + #$fd = fopen("scrapper_kisel.log", "a+");
  262 + #fwrite($fd, $log);
  263 + #fclose($fd);
  264 +
  265 + curl_close ($ch);
  266 + $this->resp_str = $resp_str;
  267 + return 1;
  268 +
  269 + }
  270 +
  271 + function setSessionId($session_id){
  272 + $this->session_id = $session_id;
  273 + }
  274 +
  275 + function getSessionId() {
  276 + return $this->session_id;
  277 + }
  278 +
  279 +
  280 + function do_logout($sess_id) {
  281 + if (!function_exists('curl_init')){
  282 + die('Sorry cURL is not installed!');
  283 + }
  284 +
  285 + $ch = curl_init();
  286 + curl_setopt($ch, CURLOPT_URL, $url);
  287 + curl_setopt($ch, CURLOPT_REFERER, $url_ref);
  288 + curl_setopt($ch, CURLOPT_USERAGENT, $this->getRandomUserAgent());
  289 + curl_setopt($ch, CURLOPT_HEADER, 1);
  290 + curl_setopt($ch, CURLOPT_COOKIE, "JSESSIONID=".$this->get_sessionid());
  291 + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  292 + curl_exec($ch);
  293 + curl_close($ch);
  294 + }
  295 +
  296 +}
  297 +
  298 +?>
... ... @@ -0,0 +1,22 @@
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<trx_response>
  3 + <product>Telkomsel 10K</product>
  4 + <msisdn>0812 - 9555655</msisdn>
  5 + <harga>Rp. 10200.0,-</harga>
  6 + <ref_num>150707171380</ref_num>
  7 + <kode_voucher>0031000845623814</kode_voucher>
  8 + <info>TRANSAKSI SUKSES !!!</info>
  9 +</trx_response>
  10 +
  11 +<?xml version="1.0" encoding="UTF-8"?>
  12 +<trx_response>
  13 + <product>Telkomsel 10K</product>
  14 + <msisdn>0815 - 232322</msisdn>
  15 + <harga>Rp. 10200.0,-</harga>
  16 + <info>Phone number's not found</info>
  17 +</trx_response>
  18 +
  19 +<?xml version="1.0" encoding="UTF-8"?>
  20 +<trx_response>
  21 + <info>Error Parsing</info>
  22 +</trx_response>
... ... @@ -0,0 +1,25 @@
  1 +<?php
  2 +
  3 +include('kisel.conf.php');
  4 +
  5 +$redis = new Redis();
  6 +$redis->connect($redis_url);
  7 +
  8 +$sid = '';
  9 +if (!empty($_REQUEST['session_id'])) {
  10 + $sid = $_REQUEST['session_id'];
  11 + $redis->set($redis_prefix . '01.sessionid', $_REQUEST['session_id']);
  12 +}
  13 +
  14 +$sid = $redis->get($redis_prefix . '01.sessionid');
  15 +
  16 +?>
  17 +<html>
  18 +<body>
  19 +
  20 +<form method="POST">
  21 +Session ID: <input type="text" name="session_id" value="<?php echo $sid; ?>"><br>
  22 +<input type="submit" value="Simpan Session">
  23 +</form>
  24 +</body>
  25 +</html>
... ... @@ -0,0 +1,9 @@
  1 +<?php
  2 +
  3 +$welcome_url = 'http://112.78.34.155/isolusi/welcome.html';
  4 +$purchasetelco_url = 'http://112.78.34.155/isolusi/purchasetelco.html';
  5 +$resp_target = "";
  6 +$redis_url = 'redis.intranet.reload97.com';
  7 +$redis_prefix = 'kisel';
  8 +
  9 +?>
... ... @@ -0,0 +1,15 @@
  1 +<?php
  2 +
  3 +include('kisel.conf.php');
  4 +include('KiselScrap.php');
  5 +
  6 +$kisel = new KiselScrap();
  7 +$redis = new Redis();
  8 +
  9 +$redis->connect($redis_url);
  10 +$kisel->setSessionId($redis->get($redis_prefix.'01.sessionid'));
  11 +$kisel->setRandomUserAgent();
  12 +
  13 +# get welcome page
  14 +$kisel->get_page($welcome_url);
  15 +?>
... ... @@ -0,0 +1,83 @@
  1 +<?php
  2 +
  3 +include('kisel.conf.php');
  4 +include('KiselScrap.php');
  5 +
  6 +#$arrinqtelco = array('customerId' => '08122027896',
  7 +# 'product' => 'SI10',
  8 +# 'inquiry' => 'Inquiry',
  9 +# '_sourcePage' => 'fRCY-X8ZXOIcbgrf1BkkxBM7KTI7S6exLVsnXnUiXn8=',
  10 +# '__fp' => 'wIlI6mnh04Y='
  11 +# );
  12 +
  13 +$arrinqtelconext = array('goToPin' => 'next',
  14 + '_sourcePage' => 'fRCY-X8ZXOIcbgrf1BkkxBM7KTI7S6exLVsnXnUiXn8=',
  15 + '__fp' => 'wIlI6mnh04Y='
  16 + );
  17 +
  18 +$kisel = new KiselScrap();
  19 +
  20 +$redis = new Redis();
  21 +$redis->connect($redis_url);
  22 +$kisel->setSessionId($redis->get($redis_prefix.'01.sessionid'));
  23 +
  24 +// get querystring
  25 +$kisel->setTrxParam($_REQUEST['reqid'], $_REQUEST['msisdn'], $_REQUEST['product']);
  26 +$arrinqtelco = array('customerId' => $kisel->msisdn,
  27 + 'product' => $kisel->product,
  28 + 'inquiry' => 'Inquiry',
  29 + '_sourcePage' => 'fRCY-X8ZXOIcbgrf1BkkxBM7KTI7S6exLVsnXnUiXn8=',
  30 + '__fp' => 'wIlI6mnh04Y='
  31 +);
  32 +
  33 +$kisel->setRandomUserAgent();
  34 +
  35 +$log = "";
  36 +// Format the date and time
  37 +$date = date("Y-m-d H:i:s");
  38 +
  39 +
  40 +$log = "[$date] Step-1 : get welcome page\n";
  41 +$log .= "--------------------------------\n";
  42 +$kisel->get_page($welcome_url);
  43 +if(!$kisel->session_status) {
  44 + print "masuk";
  45 + $log .= "Error Session Invalid. Please input session";
  46 + $err = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
  47 + $err .= "<trx_response>\n";
  48 + $err .= "<product>".$_REQUEST['product']."</product>\n";
  49 + $err .= "<msisdn>".$_REQUEST['msisdn']."</msisdn>\n";
  50 + $err .= "<info>SESSION INVALID !!!</info>\n";
  51 + $err .= "</trx_response>";
  52 + print $err;
  53 +}
  54 +sleep(1);
  55 +if($kisel->session_status) {
  56 + $log .= "[$date] Step-2 : get purchase telco page\n";
  57 + $log .= "--------------------------------\n";
  58 + $kisel->get_page($purchasetelco_url);
  59 + #$prevbal = array();
  60 + #$prevbal = $kisel->balance();
  61 + #print_r($prevbal);
  62 + sleep(1);
  63 +
  64 + $log .= "[$date] Step-3 : send inquiry telco\n";
  65 + $log .= "--------------------------------\n";
  66 + $kisel->send_form($purchasetelco_url, $purchasetelco_url, $arrinqtelco);
  67 + $kisel->parseResponse("//table[@class='tblogin']/tbody/tr/td",0);
  68 + sleep(1);
  69 +
  70 + $log .= "[$date] Step-4 : send purchase telco\n";
  71 + $log .= "--------------------------------\n";
  72 + $kisel->send_form($purchasetelco_url, $purchasetelco_url, $arrinqtelconext);
  73 + $kisel->parseResponse("//table[@class='tblogin']/tbody/tr/td",1);
  74 + $kisel->printResponseXML();
  75 +
  76 + /*
  77 + $fd = fopen("scrapper_kisel.log", "a+");
  78 + fwrite($fd, $log);
  79 + fclose($fd);
  80 + */
  81 +}
  82 +
  83 +?>