api.php 8.91 KB
<?php
class Flora_Api
{
    const API_VERSION = "0.1";

    protected $appID;
    protected $authKey;
    protected $debug;
    protected $throwOnUpdateVersion;
    protected $lastRawResponse;
    protected $downloadedSize;
    protected $downloadTime;
    /**
     * @var Flora_Api_Response
     */
    protected $lastResponse;
    protected $language = "en";

    protected $currentTime;
    protected $currentCountry = "";
    protected $currentCurrency = "";
    protected $currentCity = "";
    protected $promocode;
    protected $userAuthKey;
    protected $ref;
    protected $utm;

    protected $requests = array();
    protected $requeststime = 0;
    protected $apiUrl;
    protected $requestId;

    function __construct($appID, $authKey, $url = 'http://api.floraconcierge.com', $throwOnUpdateVersion = true)
    {
        if (!function_exists("curl_init")) {
            throw new RuntimeException("Not installed curl php extension");
        }

        $this->apiUrl = $url;
        $this->appID = $appID;
        $this->authKey = $authKey;
        $this->throwOnUpdateVersion = $throwOnUpdateVersion;

        $this->currentTime = 0;
        $this->requestId = isset($_SERVER['REQUEST_ID'])
            ? $_SERVER['REQUEST_ID']
            : 0;
    }

    /**
     * @throws InvalidArgumentException
     * @param $api
     * @param array|object $data
     * @param array|object $post
     * @return Flora_Api_Response
     */
    public function call($api, $data = array(), $post = array())
    {
        $url = $this->apiUrl;
        $url = $url . "/v1/" . ltrim($api);
        $data = array_filter($data);

        if (!is_array($data)) {
            throw new InvalidArgumentException('Bad argument $params type. Supported only array');
        }

        $params = array();
        foreach ($data as $k => $v) {
            if ($k{0} != "_") {
                $params[$k] = $v;
            }
        }
        ksort($params);
        $forSign = array();
        foreach ($params as $k => $v) {
            $forSign[] = "$k=$v";
        }
        $forSign = join("", $forSign);
        $sign = sha1($this->authKey . ":" . $forSign);

        $data['_sign'] = $sign;
        $data['_api_id'] = $this->appID;
        $data['_lang'] = $this->language;
        $data['_country'] = $this->currentCountry;
        $data['_city'] = $this->currentCity;
        $data['_date'] = $this->currentTime;
        $data['_currency'] = $this->currentCurrency;
        $data['_ip'] = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : "127.0.0.1";

        $data['_browser'] = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : "";
        $data['_accept'] = isset($_SERVER['HTTP_ACCEPT']) ? $_SERVER['HTTP_ACCEPT'] : "";
        $data['_accept_language'] = isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : "";
        $data['_encoding'] = isset($_SERVER['HTTP_ACCEPT_ENCODING']) ? $_SERVER['HTTP_ACCEPT_ENCODING'] : "";

        if (trim($this->promocode)) {
            $data['_promo'] = $this->promocode;
        }
        if (trim($this->userAuthKey)) {
            $data['_user_auth_key'] = $this->userAuthKey;
        }
        if (trim($this->ref)) {
            $data['_ref'] = $this->ref;
        }
        if (trim($this->utm)) {
            $data['_utm'] = $this->utm;
        }

        $url .= "?" . http_build_query($data, "", "&");

        $reqstart = microtime(1);

        $ch = curl_init($url);
        if ($this->requestId){
            curl_setopt($ch, CURLOPT_HTTPHEADER, [
                'X-Request-ID: ' . $this->requestId
            ]);
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
        if ($post) {
            $json = json_encode($post);
            if (!$json) {
                $json = json_encode(self::utf8_encode_recursive($post));
            }
            curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
        }
        $result = curl_exec($ch);

        $info = curl_getinfo($ch);
        $this->downloadedSize = $info['size_download'];
        $this->downloadTime = $info['total_time'];

        curl_close($ch);

        $this->lastRawResponse = $result;

        $response = new Flora_Api_Response($result, ltrim($api), $data, $this->throwOnUpdateVersion, $this->getDebug(), $url);
        $this->lastResponse = $response;

        if ($this->debug) {
            $this->requests[] = array(
                'url' => $url,
                'api' => $api,
                'data' => $data,
                'post' => $post,
                'info' => $info
            );
            $this->requeststime += microtime(1) - $reqstart;
        }

        return $response;
    }

    public function getAppID()
    {
        return $this->appID;
    }

    public function getAuthKey()
    {
        return $this->authKey;
    }

    public function getLastRawResponse()
    {
        return $this->lastRawResponse;
    }

    public function getLastResponse()
    {
        return $this->lastResponse;
    }

    public function setLanguage($language)
    {
        $this->language = $language;
    }

    public function getLanguage()
    {
        return $this->language;
    }

    public function getServiceGeo()
    {
        return new Flora_Api_Service_Geo($this);
    }

    public function getServiceShop()
    {
        return new Flora_Api_Service_Shop($this);
    }

    public function getServiceUsers()
    {
        return new Flora_Api_Service_Users($this);
    }

    public function getServiceSystem()
    {
        return new Flora_Api_Service_System($this);
    }

    public function getServiceInvoices()
    {
        return new Flora_Api_Service_Invoices($this);
    }

    public function getServicePartner()
    {
        return new Flora_Api_Service_Partner($this);
    }

    public function getServicePhone()
    {
        return new Flora_Api_Service_Phone($this);
    }

    public function getServiceImg()
    {
        return new Flora_Api_Service_Img($this);
    }

    public function getServiceDiscount()
    {
        return new Flora_Api_Service_Discount($this);
    }

    public function getValidationService()
    {
        return new Flora_Api_Service_Validation($this);
    }

    public function getDownloadedSize()
    {
        return $this->downloadedSize;
    }

    public function getDownloadTime()
    {
        return $this->downloadTime;
    }

    public function setCurrentCountry($currentCountry)
    {
        if ((int)$currentCountry) {
            $this->currentCountry = (int)$currentCountry;
        }
    }

    public function getCurrentCountry()
    {
        return $this->currentCountry;
    }

    public function setCurrentCity($currentCity)
    {
        if ((int)$currentCity) {
            $this->currentCity = (int)$currentCity;
        }
    }

    public function getCurrentCity()
    {
        return $this->currentCity;
    }

    public function setCurrentTime($currentTime)
    {
        if ((int)$currentTime) {
            $this->currentTime = (int)$currentTime;
        }
    }

    public function getCurrentTime()
    {
        return $this->currentTime;
    }

    public function setEnviroument($time = '', $country = '', $currency = '')
    {
        $this->setCurrentCountry($country);
        $this->setCurrentTime($time);
        $this->setCurrentCurrency($currency);
    }

    public function setCurrentCurrency($currentCurrency)
    {
        if (trim($currentCurrency)) {
            $this->currentCurrency = $currentCurrency;
        }
    }

    public function getCurrentCurrency()
    {
        return $this->currentCurrency;
    }

    public function setPromocode($promocode)
    {
        $this->promocode = $promocode;
    }

    public function getPromocode()
    {
        return $this->promocode;
    }

    public function setUserAuthKey($userAuthKey)
    {
        $this->userAuthKey = $userAuthKey;
    }

    public function getUserAuthKey()
    {
        return $this->userAuthKey;
    }

    public function setRef($ref)
    {
        $this->ref = $ref;
    }

    public function getRef()
    {
        return $this->ref;
    }

    public function setUtm($ref)
    {
        $this->utm = $ref;
    }

    public function getUtm()
    {
        return $this->utm;
    }

    public function setDebug($debug)
    {
        $this->debug = (bool)$debug;
    }

    public function getDebug()
    {
        return $this->debug;
    }

    public function getRequests()
    {
        return $this->requests;
    }

    public function getRequeststime()
    {
        return $this->requeststime;
    }

    protected static function utf8_encode_recursive($array)
    {
        $result = array();
        foreach ($array as $key => $value) {
            if (is_array($value)) {
                $result[$key] = self::utf8_encode_recursive($value);
            } else if (is_string($value)) {
                $result[$key] = utf8_encode($value);
            } else {
                $result[$key] = $value;
            }
        }
        return $result;
    }
}