<?php
namespace common\lib;

use common\helpers\Utils;

class Mt4Api {
    /*
    define('T_PAGE_ENCODING', 'windows-1251');            // Web: Page encoding

    define('T_SMTP_SERVER',   'mail.mycompany.com');      // SMTP: Server
    define('T_SMTP_LOGIN',    'login');                   // SMTP: Login
    define('T_SMTP_PASSWORD', 'password');                // SMTP: Password
    define('T_SMTP_FROM',     'hello@mycompany.com');     // SMTP: From
    define('T_SMTP_FROM_NAME','Mr. Hello');               // SMTP: From Name 

    define('T_MT4_HOST',      '114.55.37.146');               // MetaTrader: Server
    define('T_MT4_PORT',       443);                      // MetaTrader: Port

    define('T_PLUGIN_MASTER', 'password');                  // Plugin: Master Password

    define('T_MYSQL_SERVER',  '114.55.37.146');             // MySQL: Server
    define('T_MYSQL_LOGIN',   'root');             // MySQL: Login
    define('T_MYSQL_PASSWORD','QOFX@dmin2016');          // MySQL: Password
    define('T_MYSQL_DB_NAME', 'mt4');         // MySQL: Database
    */

    protected $realip = '103.29.142.69';
    protected $demoip = '47.52.153.85';
    /*
    public function Group($group){
        switch($group){
            case 'demoforex':     $ret=1; break;
            case 'demoforex-usd': $ret=2; break;
            case 'demoforex-eur': $ret=3; break;
            case 'demoforex-jpy': $ret=4; break;
            default:              $ret=0; break;
        }
        return($ret);
    }
    */
    public function MQ_Query($query,$real = 0 ){
        $ret='error';
        //---- open socket
       
        if( $real ){
            $ptr=@fsockopen($this->realip,'443',$errno,$errstr,5);
        }else{
            $ptr=@fsockopen($this->demoip,'443',$errno,$errstr,5);
        }
		
        //---- check connection
        if($ptr){
        //---- send request
            if(fputs($ptr,"W$query\nQUIT\n")!=FALSE){
                //---- clear default answer
                $ret='';
                //---- receive answer
                while(!feof($ptr)) {
                    $line=fgets($ptr,128);
                    if($line=="end\r\n") break; 
                    $ret.= $line;
                } 
            }
            fclose($ptr);
        }
        //---- return answer
        return $ret;
    }
    
    public function addAccount($data, $real = 0){
        //字段:
        //INVESTOR   STATUS  ZIPCODE  ID  SEND_REPORTS PHONE_PASSWORD 属性不明       
        $data['MASTER'] = 'password';
        $data['IP'] = isset($data['IP']) && trim($data['IP']) != '' ? trim($data['IP']) : Utils::getClientIp();
        $data['GROUP'] = isset($data['GROUP']) ? trim($data['GROUP']) : '';
        $data['NAME'] = isset($data['NAME']) ? trim($data['NAME']) : '';
        $data['PASSWORD'] = isset($data['PASSWORD']) ? trim($data['PASSWORD']) : '';
        $data['INVESTOR'] = isset($data['INVESTOR']) ? trim($data['INVESTOR']) : '';
        $data['EMAIL'] = isset($data['EMAIL']) ? trim($data['EMAIL']) : '';
        $data['COUNTRY'] = isset($data['COUNTRY']) ? trim($data['COUNTRY']) : 'China';
        $data['STATE'] = isset($data['STATE']) ? trim($data['STATE']) : '';
        $data['CITY'] = isset($data['CITY']) ? trim($data['CITY']) : '';
        $data['ADDRESS'] = isset($data['ADDRESS']) ? trim($data['ADDRESS']) : '';
        $data['PHONE'] = isset($data['PHONE']) ? trim($data['PHONE']) : '';
        $data['PHONE_PASSWORD'] = isset($data['PHONE_PASSWORD']) ? trim($data['PHONE_PASSWORD']) : '';
        $data['STATUS'] = '';
        $data['ZIPCODE'] = isset($data['ZIPCODE']) ? trim($data['ZIPCODE']) : '';
        $data['ID'] = isset($data['ID']) ? trim($data['ID']) : '';
        $data['LEVERAGE'] = isset($data['LEVERAGE']) ? trim($data['LEVERAGE']) : '';
        $data['AGENT'] = 0;
        $data['SEND_REPORTS'] = 1;
        $data['DEPOSIT'] = '';
        if ($real == 1) {
            $data['COMMENT'] = 'XCBVARSTDUSD';
            $data['LOGIN'] = isset($data['LOGIN']) ? $data['LOGIN'] : '';
        } else {
            $data['GROUP'] = 1;
            $data['COMMENT'] = 'demoforex';
            $data['LEVERAGE'] = 100;
            $data['DEPOSIT'] = 100000;
            $data['LOGIN'] = '';
        }
        /* $query = 'NEWACCOUNT MASTER=password|IP='.$ip.'|GROUP='.$data['accounttype'].'|NAME='.$data['username'].'|PASSWORD='.$data['password'].
            '|INVESTOR=|EMAIL='.$data['email'].'|COUNTRY=hk|STATE=hk|CITY=hk|ADDRESS=hk|COMMENT='.$comment.'|PHONE='.$data['phone'].
            '|PHONE_PASSWORD=|STATUS=resident|ZIPCODE=000000|ID=10|LEVERAGE='.$leverage.'|AGENT='.$data['agent'].
            '|SEND_REPORTS=1|DEPOSIT='.$deposit; 2016-5-24 7:30修改*/
        //$data['username'] = iconv("utf-8", "gbk", $data['username']);
		/*
        $query = 'NEWACCOUNT MASTER=password|IP='.$data['ip'].'|GROUP='.$group.'|NAME='.$data['username'].'|PASSWORD='.$data['password'].
            '|INVESTOR='.$data['investor'].'|EMAIL='.$data['email'].'|COUNTRY=hk|STATE=hk|CITY=hk|ADDRESS=hk|COMMENT='.$comment.'|PHONE=15814419156
             |PHONE_PASSWORD='.$data['phone_password'].'|STATUS=resident|ZIPCODE=000000|ID=10|LEVERAGE='.$leverage.'|AGENT=1|SEND_REPORTS=1|DEPOSIT='.$deposit;

		*/
		/*
		$data['username'] = iconv("utf-8", "gbk", $data['username']);
        $query = 'NEWACCOUNT MASTER=xcoq123|IP='.$data['ip'].'|GROUP=1|NAME='.$data['username'].'|PASSWORD='.$data['password'].
            '|INVESTOR=|EMAIL=email|COUNTRY=China|STATE=hk|CITY=hk|ADDRESS=hk|COMMENT='.$comment.'|PHONE=+8613659856084
             |PHONE_PASSWORD=|STATUS=resident|ZIPCODE=123|ID=10|LEVERAGE='.$leverage.'|AGENT=123|SEND_REPORTS=1|DEPOSIT='.$deposit;
		
		echo $query;
		
		echo '<br>';
		echo '<br>';
		*/
		
		$data['NAME'] = iconv("utf-8", "gbk", $data['NAME']);
		 //--- prepare query
		$query="NEWACCOUNT MASTER={$data['MASTER']}|IP={$data['IP']}|GROUP={$data['GROUP']}|NAME={$data['NAME']}|".
          "PASSWORD={$data['PASSWORD']}|INVESTOR={$data['INVESTOR']}|EMAIL={$data['EMAIL']}|COUNTRY={$data['COUNTRY']}|".
          "STATE={$data['STATE']}|CITY={$data['CITY']}|ADDRESS={$data['ADDRESS']}|COMMENT={$data['COMMENT']}|".
          "PHONE={$data['PHONE']}|PHONE_PASSWORD={$data['PHONE_PASSWORD']}|STATUS={$data['STATUS']}|ZIPCODE={$data['ZIPCODE']}|".
          "ID={$data['ID']}|LEVERAGE={$data['LEVERAGE']}|AGENT={$data['AGENT']}|SEND_REPORTS={$data['SEND_REPORTS']}|".
          "DEPOSIT={$data['DEPOSIT']}|LOGIN={$data['LOGIN']}";
		//--- send request
   
		//echo $query;die;
   
        $ret = $this->MQ_Query($query,$real);
        $info = $this->qgstr($ret);
        return $info;
    }
    
    //处理api返回的信息 如: OK LOGIN=50010  or ERROR IP is blocked. Please wait 60 secs and try again. 
    
    protected function qgstr($str)
	{
        if( strstr($str, 'OK')){
            $tmp = explode('=', $str);
			$login = str_replace('\r\n', '', $tmp[1]);
			return array('Ret'=>0, 'Obj'=> array('Login' => $login));	
        }else{
            return array('Ret'=>1, 'str'=>$str);
        }
    }
    
}
