<?php
namespace App;

use Illuminate\Http\Request;
use \App\User;
use Illuminate\Support\Facades\DB;
use \App\Simpledom;

class Bank {

  public static function fix_angka_bni ($string) {
      $string = substr($string, 4, -3);
      $string = str_replace('.', '', $string);
      return (int)$string;
  }

  public static function fix_angka_bca ($string) {
      $string = str_replace(',', '', $string);
      $string = strtok($string, '.');
      return $string;
  }

  public static function grab_bni($user, $pass, $norek, $jumlah) {
      $ua = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36";
      $cookie = 'bni-cookie.txt';
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
      curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
      curl_setopt($ch, CURLOPT_USERAGENT, $ua);
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_URL,
          'https://ibank.bni.co.id/MBAWeb/FMB;jsessionid=0000gsadMFnW4TJnYCFiblgmvcx:1a1li5jho?page=Thin_SignOnRetRq.xml&MBLocale=bh');
      $result = curl_exec($ch);
      $dom = new Simpledom();
      $dom->load($result);
      $form = $dom->find('form', 0);

      $postdata = 'Num_Field_Err=%22Please+enter+digits+only%21%22&Mand_Field_Err=%22Mandatory+field+is+empty%21%22&CorpId=' .
          urlencode($user) . '&PassWord=' . urlencode($pass) .
          '&__AUTHENTICATE__=Login&CancelPage=HomePage.xml&USER_TYPE=1&MBLocale=bh&language=bh&AUTHENTICATION_REQUEST=True&__JS_ENCRYPT_KEY__=&JavaScriptEnabled=N&deviceID=&machineFingerPrint=&deviceType=&browserType=&uniqueURLStatus=disabled&imc_service_page=SignOnRetRq&Alignment=LEFT&page=SignOnRetRq&locale=en&PageName=Thin_SignOnRetRq.xml&formAction=https%3A%2F%2Fibank.bni.co.id%2FMBAWeb%2FFMB%3Bjsessionid%3D0000gsadMFnW4TJnYCFiblgmvcx%3A1a1li5jho&mConnectUrl=FMB&serviceType=Dynamic';
      $form_action = $form->action;
      curl_setopt($ch, CURLOPT_URL, $form_action);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
      curl_setopt($ch, CURLOPT_POST, 1);
      $result = curl_exec($ch);

      $dom->clear();
      $dom->load($result);
      $link = $dom->find("#MBMenuList", 0);
      parse_str($link->href, $params);

      $jkt_time = time(); // + (3600 * 7);
      $dari_tgl = date('d-M-Y', $jkt_time - (3600 * 24));
      $ke_tgl = date('d-M-Y', $jkt_time);
      $postdata = 'Num_Field_Err=%22Please+enter+digits+only%21%22&Mand_Field_Err=%22Mandatory+field+is+empty%21%22&acc1=OPR%7C0000000' .
          $norek . '%7CBNI+TAPLUS&TxnPeriod=-1&Search_Option=Date&txnSrcFromDate=' . $dari_tgl .
          '&txnSrcToDate=' . $ke_tgl .
          '&FullStmtInqRq=Lanjut&MAIN_ACCOUNT_TYPE=OPR&mbparam=' . urlencode($params['mbparam']) .
          '&MAIN_ACCOUNT_TYPE=OPR&uniqueURLStatus=disabled&imc_service_page=AccountIDSelectRq&Alignment=LEFT&page=AccountIDSelectRq&locale=bh&PageName=AccountTypeSelectRq&formAction=' .
          urlencode($form_action) . '&mConnectUrl=FMB&serviceType=Dynamic';
      curl_setopt($ch, CURLOPT_URL, $form_action);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
      curl_setopt($ch, CURLOPT_POST, 1);
      curl_setopt($ch, CURLOPT_REFERER, $form_action);
      $data1 = curl_exec($ch);

      $dom->clear();
      $dom->load($data1);
      $link = $dom->find("#MBMenuList", 0);
      if (stripos($data1, 'MUTASI REKENING') !== false){
          $dari_tgl = '15-Mar-2017';
          $ke_tgl = date('d-M-Y', $jkt_time);
          $postdata = 'Num_Field_Err=%22Please+enter+digits+only%21%22&Mand_Field_Err=%22Mandatory+field+is+empty%21%22&acc1=OPR%7C0000000' .
              $norek . '%7CBNI+TAPLUS+ANAK&TxnPeriod=LastMonth&Search_Option=Date&txnSrcFromDate=' . $dari_tgl .
              '&txnSrcToDate=' . $ke_tgl .
              '&FullStmtInqRq=Lanjut&MAIN_ACCOUNT_TYPE=OPR&mbparam=' . urlencode($params['mbparam']) .
              '&uniqueURLStatus=disabled&imc_service_page=AccountIDSelectRq&Alignment=LEFT&page=AccountIDSelectRq&locale=bh&PageName=AccountTypeSelectRq&formAction=' .
              urlencode($form_action) . '&mConnectUrl=FMB&serviceType=Dynamic';
          curl_setopt($ch, CURLOPT_URL, $form_action);
          curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
          curl_setopt($ch, CURLOPT_POST, 1);
          curl_setopt($ch, CURLOPT_REFERER, $form_action);
          $data = curl_exec($ch);

      }
      $dom->clear();
      $dom->load($data);
      $count = $dom->find('#s4');
      for($i=0;$i<=count($count)-1;$i++){
          $saldo = str_ireplace(array('Nominal','IDR'),'',$dom->find('#s4',$i));
          $saldo = str_ireplace(array('.',',00'),'',$saldo);
          $tipe = str_ireplace('Tipe','',$dom->find('#s3',$i+1));
          $tanggal = str_ireplace('Tanggal Transaksi','',$dom->find('#s1',$i));
          $uraian = str_ireplace('Uraian Transaksi','',$dom->find('#s2',$i));
          if($saldo == $jumlah){
              $sukses = 1;
          } else {
              $sukses = 0;
          }
      }

      $postdata = 'Num_Field_Err=%22Please+enter+digits+only%21%22&Mand_Field_Err=%22Mandatory+field+is+empty%21%22&__LOGOUT__=Keluar&mbparam=' .
      urlencode($params['mbparam']) .
      '&uniqueURLStatus=disabled&imc_service_page=SignOffUrlRq&Alignment=LEFT&page=SignOffUrlRq&locale=bh&PageName=LoginRs&formAction=' .
      urlencode($form_action) . '&mConnectUrl=FMB&serviceType=Dynamic';
      curl_setopt($ch, CURLOPT_URL, $form_action);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
      curl_setopt($ch, CURLOPT_POST, 1);
      curl_setopt($ch, CURLOPT_REFERER, $form_action);
      $result = curl_exec($ch);
      curl_close($ch);

      return $sukses;
  }

  public static function grab_bca ($user, $pass, $tgl) {
      $user_ip = '202.62.16.186';
      $ua = "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko) '.
      Chrome/44.0.2403.89 Safari/537.36";
      $cookie = base_path() . '/cache/bca-cookie.txt';

      $ch = curl_init();

      curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
      curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
      curl_setopt($ch, CURLOPT_USERAGENT, $ua);
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_URL, 'https://ibank.klikbca.com');
      $info = curl_exec($ch);

      $a = strstr($info, 'var s = document.createElement(\'script\'), attrs = { src: (window.location.protocol ==',
          1);
      $a = strstr($a, 'function getCurNum(){');

      $b = array(
          'return "',
          'function getCurNum(){',
          '";',
          '}',
          '{',
          '(function()',
          );

      $b = str_replace($b, '', $a);
      $curnum = trim($b);
      $params = 'value%28actions%29=login&value%28user_id%29=' . $user .
          '&value%28CurNum%29=' . $curnum . '&value%28user_ip%29=' . $user_ip .
          '&value%28browser_info%29=' . $ua . '&value%28mobile%29=false&value%28pswd%29=' .
          $pass . '&value%28Submit%29=LOGIN';
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_URL, 'https://ibank.klikbca.com/authentication.do');
      curl_setopt($ch, CURLOPT_REFERER, 'https://ibank.klikbca.com');
      curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
      curl_setopt($ch, CURLOPT_POST, 1);
      $info = curl_exec($ch);

      // Buka menu
      curl_setopt($ch, CURLOPT_URL,
          'https://ibank.klikbca.com/nav_bar_indo/menu_bar.htm');
      curl_setopt($ch, CURLOPT_REFERER, 'https://ibank.klikbca.com/authentication.do');
      $info = curl_exec($ch);

      // Buka Informasi Rekening
      curl_setopt($ch, CURLOPT_URL,
          'https://ibank.klikbca.com/nav_bar_indo/account_information_menu.htm');
      curl_setopt($ch, CURLOPT_REFERER, 'https://ibank.klikbca.com/authentication.do');
      $info = curl_exec($ch);

      // Buka Mutasi Rekening
      curl_setopt($ch, CURLOPT_URL,
          'https://ibank.klikbca.com/accountstmt.do?value(actions)=acct_stmt');
      curl_setopt($ch, CURLOPT_REFERER,
          'https://ibank.klikbca.com/nav_bar_indo/account_information_menu.htm');
      curl_setopt($ch, CURLOPT_POST, 1);
      $info = curl_exec($ch);

      // Parameter untuk Lihat Mutasi Rekening
      $params = array();

      $jkt_time = time() + (3600 * 7);
      $t1 = explode('-', $tgl);
      $t0 = explode('-', $tgl);

      $params[] = 'value%28startDt%29=' . $t0[0];
      $params[] = 'value%28startMt%29=' . $t0[1];
      $params[] = 'value%28startYr%29=' . $t0[2];
      $params[] = 'value%28endDt%29=' . $t1[0];
      $params[] = 'value%28endMt%29=' . $t1[1];
      $params[] = 'value%28endYr%29=' . $t1[2];
      $params[] = 'value%28D1%29=0';
      $params[] = 'value%28r1%29=1';
      $params[] = 'value%28fDt%29=';
      $params[] = 'value%28tDt%29=';
      $params[] = 'value%28submit1%29=Lihat+Mutasi+Rekening';

      $params = implode('&', $params);

      // Buka Lihat Mutasi Rekening & simpan hasilnya di $source
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_URL,
          'https://ibank.klikbca.com/accountstmt.do?value(actions)=acctstmtview');
      curl_setopt($ch, CURLOPT_REFERER,
          'https://ibank.klikbca.com/nav_bar_indo/account_information_menu.htm');
      curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
      curl_setopt($ch, CURLOPT_POST, 1);

      $source = curl_exec($ch);

      // Logout, cURL close, hapus cookies
      curl_setopt($ch, CURLOPT_URL,
          'https://ibank.klikbca.com/authentication.do?value(actions)=logout');
      curl_setopt($ch, CURLOPT_REFERER,
          'https://ibank.klikbca.com/nav_bar_indo/account_information_menu.htm');
      $info = curl_exec($ch);
      curl_close($ch);
      @unlink($cookie);
      return $source;
  }

}
