<?php

class LicenseServer
{

    public static function checkConnection()
    {
        $url = app()->params->licenseApi . '/checkConnection';

        $curlConfigs = array();
        if (!Yii::app()->request->getIsSecureConnection()) {
            $curlConfigs[CURLOPT_SSL_VERIFYPEER] = false;
        }
        $response = CurlHelper::sendGetRequest($url, array(), $curlConfigs);

        if ($response['httpCode'] == 200 && $response['content'] == 'connection established') {
            return true;
        }

        if ($response['httpCode'] == 404 && $response['errmsg'] == '') {
            $response['errmsg'] = 'Bad API URL, API not found...';
        }

        return $response;
    }

    public static function activateLicense($licenseModel)
    {
        $result = LicenseServer::requestLicense($licenseModel);

        if ($result['httpCode'] == 200) {
            $filePath = app()->runtimePath . '/apiLicence.lic';

            if (file_put_contents($filePath, $result['content']) === false) {
                return array(
                    'errorCode' => 486,
                    'errorMessage' => 'Could not write license temp file'
                );
            }

            $dbResult = PhonalisaLicenseManager::storeLicense($filePath);
            if ($dbResult !== true) {
                return array(
                    'errorCode' => substr($dbResult, 0, 3),
                    'errorMessage' => substr($dbResult, 3),
                );
            }

            // remove temp file
            unlink($filePath);
            return true;
        } else if ($result['httpCode'] == 404) {
            return array(
                'errorCode' => 404,
                'errorMessage' => __('API_NOT_FOUND_BAD_API_URL'),
            );
        } else if ($result['httpCode'] == 302) {
            return array(
                'errorCode' => 302,
                'errorMessage' => __('LICENSE_INVALID_API_URL'),
            );
        } else if ($result['httpCode'] == 400) {
            return array(
                'errorCode' => 400,
                'errorMessage' => $result['content'],
            );
        } else {
            $text = strlen($result['content']) > 0 ? $result['content'] : $result['errmsg'];
            return array(
                'errorCode' => $result['httpCode'],
                'errorMessage' => $text,
            );
        }
    }

    public static function requestLicense($license)
    {
        $url = app()->params->licenseApi . '/activate';
        if (empty($license->system_id)) {
            $license->system_id = PhonalisaLicenseManager::getSystemID();
        }
        if (!empty($license->upgrade_code) && empty($license->license_code)) {
            $license->license_code = PhonalisaLicenseManager::getLicenseCode();
        }

        $params = array();
        foreach ($license->attributes as $key => $value) {
            if (($key == 'upgrade_code' && $value == '') || $key == 'data' || $key == 'id') {
                continue;
            }
            if (strpos($key, 'customer_') !== false) {
                $key = str_replace('customer_', 'customer[', $key);
                $key.=']';
            }
            $params[$key] = $value;
        }

        $mac = NetworkInterface::getMacAddress();
        if (!empty($mac)) {
            $params['mac'] = $mac;
        }

        $curlConfigs = array(
            CURLOPT_HEADER => false,
            CURLOPT_CONNECTTIMEOUT_MS => 50000
        );

        if (!Yii::app()->request->getIsSecureConnection()) {
            $curlConfigs[CURLOPT_SSL_VERIFYPEER] = false;
        }

        return CurlHelper::sendPostRequest($url, $params, $curlConfigs);
    }
}
