×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
1
Language: PHP
Posted by: user6d450
Added: May 24, 2019 9:53 AM
Views: 3915
Tags: no tags
  1. <?php
  2.  
  3. /*
  4.  * To change this license header, choose License Headers in Project Properties.
  5.  * To change this template file, choose Tools | Templates
  6.  * and open the template in the editor.
  7.  */
  8.  
  9. //include_once '../config.php';
  10. include_once('apiBaseClass.php');
  11.  
  12. class ethClass
  13. {
  14.     public function __construct($userId) {
  15.         if(empty($userId))
  16.         {
  17.             $this->userId = $_SESSION['USERID'];
  18.         }
  19.         else
  20.         {
  21.             $this->userId = $userId;
  22.         }
  23.        
  24.         $this->apiurl = 'http://nodeapi.luckday.net/Gateway/ETHService.asmx/';
  25.        
  26.         $this->siteno = 'C1000001';
  27.        
  28.         $this->secretkey = 'pgZrBEymyfvIHJJi2XjIIsw=';
  29.        
  30.         $this->iv = 'pgZrBEym';
  31.        
  32.        
  33.         $this->etherscankey = 'YG5AMHVGY1SHUQ95CJYXAVJSCJ57ZM6IAM';
  34.        
  35.         $this->etherscanurl = 'https://api.etherscan.io/api?';
  36.  
  37.         $this->req_time=time();
  38.        
  39.     }
  40.    
  41.     public function createAccount($accountName)
  42.     {
  43.         $service = 'CreateAddress';
  44.        
  45.         $accountNo = $this->encrypt_tripledes($accountName);
  46.        
  47.         $signature = md5(sha1($accountNo.$this->siteno));
  48.        
  49.         $request  = array('siteNo' => "$this->siteno",'account'=>"$accountNo",'signature'=>"$signature" );
  50.  
  51.         $request_log=json_encode($request);
  52.  
  53.         $request = http_build_query($request);
  54.        
  55.         $new_url  = $this->apiurl.$service;
  56.        
  57.         $response = $this->callCurl($new_url, $request);
  58.        
  59.         $response = simplexml_load_string($response);
  60.        
  61.         $res_decode = json_decode($response[0],true);
  62.        
  63.         if($res_decode['Status'] == 1)//10
  64.         {
  65.             $array = array('status'=>'success','message'=>$res_decode['Result']);
  66.         }
  67.         else
  68.         {
  69.             $array = array('status'=>'error','message'=>'API Error');
  70.         }
  71.        
  72.         $this->insertCoinApiLog($request_log,$response,$this->req_time,'1'); //insert log;
  73.  
  74.         return json_encode($array);
  75.     }
  76.    
  77.     public function getBalance($address,$walletType)
  78.     {
  79.         $service = 'GetBalance';
  80.        
  81.         $enc_address = $this->encrypt_tripledes($address);
  82.        
  83.         $signature = md5(sha1($enc_address.$walletType.$this->siteno));
  84.        
  85.         $request = array('siteNo'=>"$this->siteno",'currency'=>"$walletType",'address'=>$enc_address,'signature'=>$signature);
  86.        
  87.         $request_log=json_encode($request);
  88.  
  89.         $request = http_build_query($request);
  90.        
  91.         $new_url  = $this->apiurl.$service;
  92.        
  93.         $response = $this->callCurl($new_url, $request);
  94.        
  95.         $response = simplexml_load_string($response);
  96.        
  97.         $res_decode = json_decode($response[0],true);
  98.        
  99.         if($res_decode['Status'] == 1)
  100.         {
  101.             $array = array('status'=>'success','message'=>$res_decode['Result']);
  102.         }
  103.         else
  104.         {
  105.             $array = array('status'=>'error','message'=>'API Error');
  106.         }
  107.        
  108.         $this->insertCoinApiLog($request_log,$response,$this->req_time,'2'); //insert log;
  109.  
  110.         return json_encode($array);
  111.        
  112.     }
  113.  
  114.  
  115.     public function transferCoin($fromAddress,$toAddress,$quantity,$walletType,$transId)
  116.     {
  117.        
  118.         $service = 'Transfer';
  119.        
  120.         $multiplier = 100000000;
  121.        
  122.         $fromAddress = $this->encrypt_tripledes($fromAddress);
  123.        
  124.         $toAddress = $this->encrypt_tripledes($toAddress);
  125.        
  126.         $nValue = bcmul($quantity, $multiplier);
  127.        
  128.         $nValue = $this->encrypt_tripledes($nValue);
  129.        
  130.         $guid   = $transId;
  131.        
  132.        
  133.         $signature = md5(sha1($walletType.$fromAddress.$guid.$nValue.$this->siteno.$toAddress));
  134.        
  135.         $request = array('siteNo'=>"$this->siteno",'currency'=>"$walletType",'fromAddress'=>$fromAddress,
  136.                          'toAddress'=>$toAddress,'nValue'=>$nValue,'guid'=>$guid,'signature'=>$signature);
  137.  
  138.         $request_log=json_encode($request);
  139.  
  140.         $request = http_build_query($request);
  141.        
  142.         $new_url  = $this->apiurl.$service;
  143.        
  144.          "Request URL : ".$new_url."<br>";
  145.        
  146.          " Request Param : ".$request."<br>";
  147.        
  148.         $response = $this->callCurl($new_url, $request);
  149.        
  150.         $response;
  151.        
  152.         $response = simplexml_load_string($response);
  153.        
  154.         $res_decode = json_decode($response[0],true);
  155.        
  156.         if($res_decode['Status'] == 1)
  157.         {
  158.             $array = array('status'=>'success','message'=>$res_decode['Result']);
  159.         }
  160.         else
  161.         {
  162.             //$array = array('status'=>'error','message'=>'API Error');
  163.             $array = $res_decode;  
  164.         }
  165.        
  166.         $this->insertCoinApiLog($request_log,$response,$this->req_time,'3'); //insert log;
  167.  
  168.         return json_encode($array);
  169.        
  170.     }
  171.  
  172.    
  173.     public function transferHistory($address,$fromDate,$toDate,$walletType,$page=1)
  174.     {
  175.         $service = 'GetTransferList';
  176.        
  177.         $enc_address = $this->encrypt_tripledes($address);
  178.        
  179.         $signature = md5(sha1($enc_address.$walletType.$toDate.$page.$this->siteno.$fromDate));
  180.        
  181.         $request = array('siteNo'=>"$this->siteno",'currency'=>"$walletType",'address'=>$enc_address,
  182.             'startDate'=>$fromDate,'endDate'=>$toDate,'page'=>$page,'signature'=>$signature);
  183.  
  184.         $request_log=json_encode($request);
  185.  
  186.         $request = http_build_query($request);
  187.        
  188.         $new_url  = $this->apiurl.$service;
  189.        
  190.         $response = $this->callCurl($new_url, $request);
  191.        
  192.         $response = simplexml_load_string($response);
  193.          
  194.         $res_decode = json_decode($response[0],true);
  195.        
  196.         if($res_decode['Status'] == 1)
  197.         {
  198.             $array = array('status'=>'success','message'=>$res_decode['Result'],'count'=>$res_decode['Count']);
  199.         }
  200.         else
  201.         {
  202.             $array = array('status'=>'error','message'=>'API Error');
  203.         }
  204.        
  205.         $this->insertCoinApiLog($request_log,$response,$this->req_time,'4'); //insert log;
  206.  
  207.         return json_encode($array);
  208.     }
  209.    
  210.  
  211.     public function getCoinPrice() //For ETH
  212.     {
  213.         //module=stats&action=ethprice&apikey=YourApiKeyToken
  214.        
  215.         $requestUrl = $this->etherscanurl.'module=stats&action=ethprice&apikey='.$this->etherscankey;
  216.        
  217.         $response = file_get_contents($requestUrl);
  218.        
  219.         $rep = json_decode($response,true);
  220.        
  221.         if($rep['status'] == 1)
  222.         {
  223.             $array = array('status'=>'success','message'=>$rep['result']['ethusd']);
  224.         }
  225.         else
  226.         {
  227.             $array = array('status'=>'error','message'=>'Something Went Wrong..');
  228.         }
  229.  
  230.         $request_log=json_encode($requestUrl);
  231.  
  232.         $this->insertCoinApiLog($request_log,$response,$this->req_time,'5'); //insert log;
  233.  
  234.         return json_encode($array);
  235.     }
  236.    
  237.     public function getCnyConversion()
  238.     {
  239.         $url = 'https://data.fixer.io/api/convert?access_key=96adf4af98271d2de2db1a78a69de9c5&from=USD&to=CNY&amount=1';
  240.        
  241.         $getCongtent = file_get_contents($url);
  242.        
  243.         //echo $getCongtent;
  244.        
  245.         $json_decode = json_decode($getCongtent,true);
  246.        
  247.        
  248.         if($json_decode['success'] == true)
  249.         {
  250.                  $array = array('status'=>'success','message'=>$json_decode['result']);
  251.         }
  252.         else
  253.         {
  254.             $array = array('status'=>'error','message'=>'Something Went Wrong..');
  255.         }
  256.  
  257.         $request_log=json_encode($url);
  258.  
  259.         $this->insertCoinApiLog($request_log,$getCongtent,$this->req_time,'6'); //insert log;
  260.  
  261.         return json_encode($array);
  262.     }
  263.  
  264.  
  265.     public function getPublicTransactionList($address) //ETH
  266.     {
  267.         $requestUrl = $this->etherscanurl.'module=account&action=txlist&address=='.$address.''
  268.                 . '&startblock=0&endblock=99999999&sort=asc&apikey='.$this->etherscankey;
  269.        
  270.         $requestUrl = $this->etherscanurl.'module=account&action=txlist&address='.$address.'&startblock=0&endblock=99999999&sort=asc&apikey='.$this->etherscankey;
  271.         $response = file_get_contents($requestUrl);
  272.         //echo $response;
  273.         $rep = json_decode($response,true);
  274.        
  275.         if($rep['status'] == 1)
  276.         {
  277.             $array = array('status'=>'success','message'=>$rep['result']);
  278.         }
  279.         else
  280.         {
  281.             $array = array('status'=>'error','message'=>'Something Went Wrong..');
  282.         }
  283.  
  284.         $request_log=json_encode($requestUrl);
  285.  
  286.         $this->insertCoinApiLog($request_log,$getCongtent,$this->req_time,'7'); //insert log;
  287.  
  288.         return json_encode($array);
  289.     }
  290.  
  291.    
  292.  
  293.  
  294.     protected function callCurl($url,$request)
  295.     {
  296.         $ch = curl_init($url);
  297.         //curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  298.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  299.         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  300.         curl_setopt($ch, CURLOPT_POST, 1);
  301.         curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
  302.         curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  303.         curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); //Timeout
  304.         $return = curl_exec($ch);
  305.        
  306.         return $return;
  307.     }
  308.  
  309.     private function encrypt_tripledes($input, $base64 = true)
  310.     {
  311.          $size = 8;
  312.       $input = $this->pkcs5_Pad($input, $size);
  313.       $encryption_descriptor = mcrypt_module_open(MCRYPT_3DES, '', 'cbc', '');
  314.       mcrypt_generic_init($encryption_descriptor, $this->secretkey, $this->iv);
  315.       $data = mcrypt_generic($encryption_descriptor, $input);
  316.       mcrypt_generic_deinit($encryption_descriptor);
  317.       mcrypt_module_close($encryption_descriptor);
  318.  
  319.       return bin2hex($data);
  320.     }
  321.    
  322.     private function pkcs5_Pad($text)
  323.     {
  324.       $pad = 8 - (strlen($text) % 8);
  325.       return $text . str_repeat(chr($pad), $pad);
  326.     }
  327.  
  328.     /**
  329.     *
  330.     *inser coin transaction log
  331.     */
  332.  
  333.     private function insertCoinApiLog($request,$response,$req_time,$type)
  334.     {
  335.         $api  =  new api();
  336.  
  337.         $time = time();
  338.  
  339.         $ip = $_SERVER['REMOTE_ADDR'];
  340.  
  341.         $playerId=$this->userId;
  342.        
  343.         $headers = json_encode(apache_request_headers());
  344.  
  345.         $server  = json_encode($_SERVER);
  346.        
  347.             $conn   = $api->getConn();
  348.             $stmt   = $conn->prepare("INSERT INTO coin_api_log SET "
  349.                     . "user_id                = :user_id,"
  350.                     . "type                   = :type,"
  351.                     . "server                 = :server,"
  352.                     . "header                 = :header,"
  353.                     . "request                = :request,"
  354.                     . "response               = :response,"
  355.                     . "req_time               = :req_time,"
  356.                     . "res_time               = :res_time,"
  357.                     . "ip                     = :ip");
  358.  
  359.             $stmt->bindParam(':user_id', $playerId,PDO::PARAM_STR);
  360.             $stmt->bindParam(':type', $type,PDO::PARAM_STR);
  361.             $stmt->bindParam(':server', $server,PDO::PARAM_STR);
  362.             $stmt->bindParam(':header', $headers,PDO::PARAM_STR);
  363.             $stmt->bindParam(':request', $request,PDO::PARAM_STR);
  364.             $stmt->bindParam(':response', $response,PDO::PARAM_STR);
  365.             $stmt->bindParam(':req_time', $req_time,PDO::PARAM_STR);
  366.             $stmt->bindParam(':res_time', $time,PDO::PARAM_STR);
  367.             $stmt->bindParam(':ip', $ip,PDO::PARAM_STR);
  368.             $stmt->execute();  
  369.          
  370.     }
  371.    
  372. }
  373.  
  374.  
  375. ?>
  376.