<?php

/**
 * Created by PhpStorm.
 * User: Steven
 * Date: 2016/12/2
 * Time: 18:40
 */
require_once __DIR__ . '/../../Utils/commonUtil.class.php';

class utils extends commonUtils
{
    static $format = "xml";
    static $encoding = 'utf-16';
    static $Language = 'CN';
    static $GUID = '38d28506-036c-4594-9d48-0771916ac3cc';
    static $Version = '1.0';
    //static $Url = "http://Supplier.elong.com/HotelAPI/V1.0/SouthBoundService.svc";        //正式环境
    static $Url = "http://test.directconnection.elong.com/HotelAPI/V1.0/SouthBoundService.svc";     //测试环境

    static public function setLoginToken($token)
    {
        self::$LoginToken = $token;
    }


    //生成Token
    static public function generateToken($UserName, $Password)
    {
        $user = array(
            "UserName" => $UserName,
            "Password" => $Password
        );
        $token = self::execute($user);
        self::setLoginToken($token);
        return $token;
    }

    static public function execute($request, $session = null)
    {
        $Url = 'http://' . str_replace(array('http://', 'https://'), '', self::$Url);
        $res = utils::http_post_xml($Url, $request);
        return $res;
    }

    /**
     * 组装请求艺龙接口的XML
     * @param $data
     * @param string $encoding
     * @param string $root
     * @return string
     */
    static public function xml_encode($data, $LoginToken, $root = "")
    {
        $encoding=self::$encoding;
        $GUID = self::$GUID;
        $Version = self::$Version;
        $xml = "<?xml version=\"1.0\" encoding=\"$encoding\"?>\n";
        $xml .= "<$root xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" >";
        $xml .= "<RequestHead><LoginToken>$LoginToken</LoginToken><GUID>$GUID</GUID><Version>$Version</Version></RequestHead>";
        $xml .= parent::data_to_xml($data);
        $xml .= "</$root>";
        return $xml;
    }

    /*
      * 验证post传递的参数是否合法
      * 默认提交的全是POST请求
      */
    static function validatePost()
    {
        $post = array();
        if (isset($_POST) && !empty($_POST)) {
            $post = array_merge($post, $_POST);
        }
        if (isset($_REQUEST) && !empty($_REQUEST)) {
            $post = array_merge($post, $_REQUEST);
        }
        if (isset($_GET) && !empty($_GET)) {
            $post = array_merge($post, $_GET);
        }

        if (!empty($post)) {
            if (isset($_POST['error']))
                unset($_POST['error']);
            foreach (array_keys($post) as $array_key) {
                if (!self::validateString($post[$array_key])) {
                    return false;
                }
            }
        }
        return true;
    }

    /*
     * 验证参数是否合法
     * @return 合法:true 非法:false
     */
    static function validateString($str)
    {
        $check = 0;
        if (is_string($str))
            $check = preg_match('/select |insert |update |delete |union|into|load_file|outfile/', $str);
        return $check == 0 ? true : false;
    }
}