Blame view

tmp/nextend-smart-slider3-joomla-free/admin/libraries/nextend2/nextend/library/nextend.php 6.11 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
<?php

class N2 {

    public static $version = '2.0.21';
    public static $api = 'https://secure.nextendweb.com/api/api.php';

    public static function api($posts, $returnUrl = false) {

        if ($returnUrl) {
            $posts_default = array(
                'platform' => N2Platform::getPlatform()
            );

            return self::$api . '?' . http_build_query($posts + $posts_default);
        }
        if (class_exists('JHttp')) {
            $posts_default = array(
                'platform' => N2Platform::getPlatform()
            );

            $client = new JHttp();
            try {
                $response = $client->post(self::$api, http_build_query($posts + $posts_default, '', '&'), array('Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8'), 5);
            } catch (Exception $e) {
            }
            if (isset($response) && $response && $response->code != '200') {

                if (isset($response->headers['Content-Type'])) {
                    $contentType = $response->headers['Content-Type'];
                }
                $data = $response->body;
            } else {
                /*N2Message::error(n2_('Unable to contact with the licensing server. Possible reasons might be the CURL not enabled in php.ini, call to remote url(secure.nextenedweb.com) is disabled on your server. Contact your server host, and ask them to check these two things!'));
                return array(
                    'status' => 'ERROR_HANDLED'
                );*/
            }
        }
    

        if (!isset($data)) {
            if (function_exists('curl_init') && function_exists('curl_exec') && N2Settings::get('curl', 1)) {
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, self::$api);

                $posts_default = array(
                    'platform' => N2Platform::getPlatform()
                );
                curl_setopt($ch, CURLOPT_POSTFIELDS, $posts + $posts_default);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
                curl_setopt($ch, CURLOPT_TIMEOUT, 30);

                if (N2Settings::get('curl-clean-proxy', 0)) {
                    curl_setopt($ch, CURLOPT_PROXY, '');
                }
                $data        = curl_exec($ch);
                $errorNumber = curl_errno($ch);
                if ($errorNumber == 60 || $errorNumber == 77) {
                    curl_setopt($ch, CURLOPT_CAINFO, N2LIBRARY . '/cacert.pem');
                    $data = curl_exec($ch);
                }
                $contentType     = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
                $error           = curl_error($ch);
                $curlErrorNumber = curl_errno($ch);
                curl_close($ch);

                if ($curlErrorNumber) {
                    N2Message::error($curlErrorNumber . $error);

                    return array(
                        'status' => 'ERROR_HANDLED'
                    );
                }
            } else {
                $posts_default = array(
                    'platform' => N2Platform::getPlatform()
                );

                $opts    = array(
                    'http' => array(
                        'method'  => 'POST',
                        'header'  => 'Content-type: application/x-www-form-urlencoded',
                        'content' => http_build_query($posts + $posts_default)
                    )
                );
                $context = stream_context_create($opts);
                $data    = file_get_contents(self::$api, false, $context);
                if ($data === false) {
                    N2Message::error(n2_('CURL disabled in your php.ini configuration. Please enable it!'));

                    return array(
                        'status' => 'ERROR_HANDLED'
                    );
                }
                $headers = self::parseHeaders($http_response_header);
                if ($headers['status'] != '200') {
                    N2Message::error(n2_('Unable to contact with the licensing server, please try again later!'));

                    return array(
                        'status' => 'ERROR_HANDLED'
                    );
                }
                if (isset($headers['content-type'])) {
                    $contentType = $headers['content-type'];
                }
            }
        }

        switch ($contentType) {
            case 'text/html; charset=UTF-8':
                //CloudFlare challenge
                preg_match('/"your_ip">.*?:[ ]*(.*?)<\/span>/', $data, $matches);
                if (count($matches)) {
                    $blockedIP = $matches[1];

                    N2Message::error(sprintf('Your ip address (%s) is blocked by our hosting provider.<br>Please contact us (support@nextendweb.com) with your ip to whitelist it.', $blockedIP));

                    return array(
                        'status' => 'ERROR_HANDLED'
                    );
                }

                N2Message::error(sprintf('Unexpected response from the API.<br>Please contact us (support@nextendweb.com) with the following log:') . '<br><textarea style="width: 100%;height:200px;font-size:8px;">' . base64_encode($data) . '</textarea>');

                return array(
                    'status' => 'ERROR_HANDLED'
                );
                break;
            case 'application/json':
                return json_decode($data, true);
        }

        return $data;
    }

    private static function parseHeaders(array $headers, $header = null) {
        $output = array();
        if ('HTTP' === substr($headers[0], 0, 4)) {
            list(, $output['status'], $output['status_text']) = explode(' ', $headers[0]);
            unset($headers[0]);
        }
        foreach ($headers as $v) {
            $h = preg_split('/:\s*/', $v);
            if (count($h) >= 2) {
                $output[strtolower($h[0])] = $h[1];
            }
        }
        if (null !== $header) {
            if (isset($output[strtolower($header)])) {
                return $output[strtolower($header)];
            }

            return;
        }

        return $output;
    }
}