# e2pdf/1.01.01/classes/model/e2pdf-api.php

E2Pdf – Export Pdf Tool for WordPress, version 1.01.01. 155 lines.

- Page: https://pluginprobe.com/plugins/e2pdf/1.01.01/code/classes/model/e2pdf-api.php
- Raw: https://pluginprobe.com/plugins/e2pdf/1.01.01/raw/classes/model/e2pdf-api.php
- Modified: 2018-11-07T14:59:18+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/e2pdf/1.01.01/code/classes/model/e2pdf-api.php#L10-L20`.

```php
<?php

/**
 * E2pdf Api Model
 * 
 * @copyright  Copyright 2017 https://e2pdf.com
 * @license    GPL v2
 * @version    1
 * @link       https://e2pdf.com
 * @since      0.00.01
 */
if (!defined('ABSPATH')) {
    die('Access denied.');
}

class Model_E2pdf_Api extends Model_E2pdf_Model {

    protected $api = NULL;

    const API_URL = 'https://api.e2pdf.com/';
    const DEFAULT_CURL_TIMEOUT = 300;

    function __construct() {
        parent::__construct();
    }

    /**
     * Submit API Request to Remote Server
     * 
     * @param string $key - Return Value by Key
     * 
     * @return mixed 
     */
    public function request($key = false) {
        if ($this->api->action) {

            $data = array(
                'api_url' => site_url(),
                'api_license_key' => $this->get_license(),
                'api_processor' => get_option('e2pdf_processor') ? get_option('e2pdf_processor') : 0,
            );
            $request_url = self::API_URL . $this->api->action;

            $e2pdf_connection_timeout = get_option('e2pdf_connection_timeout') === false ? self::DEFAULT_CURL_TIMEOUT : get_option('e2pdf_connection_timeout');

            $ch = curl_init($request_url);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            if (!ini_get('safe_mode') && !ini_get('open_basedir')) {
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
            }
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $e2pdf_connection_timeout);

            if (!empty($this->api->data)) {
                $data = array_merge($data, $this->api->data);
            }

            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

            $json = curl_exec($ch);
            $curl_errno = curl_errno($ch);
            $curl_error = curl_error($ch);
            curl_close($ch);

            if ($curl_errno > 0) {
                $response['error'] = "[{$curl_errno}] {$curl_error}";
            } else {

                if (($this->api->action === 'template/build' || $this->api->action === 'template/parse') && get_option('e2pdf_developer')) {
                    $model_e2pdf_options = new Model_E2pdf_Options();
                    if (in_array($this->helper->get_ip(), $model_e2pdf_options->get_option('e2pdf_developer_ips'))) {
                        $data = array(
                            'request_url' => $request_url,
                            'data' => $data,
                            'response' => $json
                        );
                        var_dump("<PRE>");
                        var_dump($data);
                        die();
                    }
                }

                $result = json_decode($json, true);
                $response = $result['response'];


                if (($this->api->action === 'common/activate' || $this->api->action === 'license/update') && isset($response['license_key'])) {
                    update_option('e2pdf_license', $response['license_key']);
                }

                if (empty($response)) {
                    $response['error'] = __("Something went wrong!", "e2pdf");
                }
            }

            if ($key) {
                if (isset($response[$key])) {
                    return $response[$key];
                } else {
                    return false;
                }
            } else {
                return $response;
            }
        }
        return false;
    }

    /**
     * Remove API Request options
     */
    public function flush() {
        $this->api = null;
    }

    /**
     * Set options for API Request
     * 
     * @param string $key - Key
     * @param mixed $value - Value
     */
    public function set($key, $value = false) {
        if (!$this->api) {
            $this->api = new stdClass();
        }
        if (is_array($key)) {
            foreach ($key as $attr => $value) {
                $this->api->$attr = $value;
            }
        } else {
            $this->api->$key = $value;
        }
    }

    /**
     * Get License
     * 
     * @return string - License Key
     */
    public function get_license() {
        return get_option('e2pdf_license');
    }

    /**
     * Get Domain
     * 
     * @return string - Domain
     */
    public function get_domain() {
        return site_url();
    }

}

```
