| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* E2pdf Api Model |
| 5 |
* |
| 6 |
* @copyright Copyright 2017 https://e2pdf.com |
| 7 |
* @license GPL v2 |
| 8 |
* @version 1 |
| 9 |
* @link https://e2pdf.com |
| 10 |
* @since 0.00.01 |
| 11 |
*/ |
| 12 |
if (!defined('ABSPATH')) { |
| 13 |
die('Access denied.'); |
| 14 |
} |
| 15 |
|
| 16 |
class Model_E2pdf_Api extends Model_E2pdf_Model { |
| 17 |
|
| 18 |
protected $api = NULL; |
| 19 |
|
| 20 |
const API_URL = 'https://api.e2pdf.com/'; |
| 21 |
|
| 22 |
function __construct() { |
| 23 |
parent::__construct(); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Submit API Request to Remote Server |
| 28 |
* |
| 29 |
* @param string $key - Return Value by Key |
| 30 |
* |
| 31 |
* @return mixed |
| 32 |
*/ |
| 33 |
public function request($key = false) { |
| 34 |
if ($this->api->action) { |
| 35 |
|
| 36 |
$data = array( |
| 37 |
'api_url' => site_url(), |
| 38 |
'api_license_key' => $this->get_license(), |
| 39 |
'api_processor' => get_option('e2pdf_processor') ? get_option('e2pdf_processor') : 0, |
| 40 |
); |
| 41 |
$request_url = self::API_URL . $this->api->action; |
| 42 |
|
| 43 |
$timeout = get_option('e2pdf_connection_timeout') === false ? 300 : get_option('e2pdf_connection_timeout'); |
| 44 |
|
| 45 |
$ch = curl_init($request_url); |
| 46 |
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
| 47 |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
| 48 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 49 |
if (!ini_get('safe_mode') && !ini_get('open_basedir')) { |
| 50 |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
| 51 |
} |
| 52 |
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); |
| 53 |
|
| 54 |
if (!empty($this->api->data)) { |
| 55 |
$data = array_merge($data, $this->api->data); |
| 56 |
} |
| 57 |
|
| 58 |
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); |
| 59 |
|
| 60 |
$json = curl_exec($ch); |
| 61 |
$curl_errno = curl_errno($ch); |
| 62 |
$curl_error = curl_error($ch); |
| 63 |
curl_close($ch); |
| 64 |
|
| 65 |
if ($curl_errno > 0) { |
| 66 |
$response['error'] = "[{$curl_errno}] {$curl_error}"; |
| 67 |
} else { |
| 68 |
|
| 69 |
if (($this->api->action === 'template/build' || $this->api->action === 'template/parse') && get_option('e2pdf_developer')) { |
| 70 |
$model_e2pdf_options = new Model_E2pdf_Options(); |
| 71 |
if (in_array($this->helper->get_ip(), $model_e2pdf_options->get_option('e2pdf_developer_ips'))) { |
| 72 |
$data = array( |
| 73 |
'request_url' => $request_url, |
| 74 |
'data' => $data, |
| 75 |
'response' => $json |
| 76 |
); |
| 77 |
var_dump("<PRE>"); |
| 78 |
var_dump($data); |
| 79 |
die(); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
$result = json_decode($json, true); |
| 84 |
$response = $result['response']; |
| 85 |
|
| 86 |
|
| 87 |
if (($this->api->action === 'common/activate' || $this->api->action === 'license/update') && isset($response['license_key'])) { |
| 88 |
update_option('e2pdf_license', $response['license_key']); |
| 89 |
} |
| 90 |
|
| 91 |
if (empty($response)) { |
| 92 |
$response['error'] = __("Something went wrong!", "e2pdf"); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
if ($key) { |
| 97 |
if (isset($response[$key])) { |
| 98 |
return $response[$key]; |
| 99 |
} else { |
| 100 |
return false; |
| 101 |
} |
| 102 |
} else { |
| 103 |
return $response; |
| 104 |
} |
| 105 |
} |
| 106 |
return false; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Remove API Request options |
| 111 |
*/ |
| 112 |
public function flush() { |
| 113 |
$this->api = null; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Set options for API Request |
| 118 |
* |
| 119 |
* @param string $key - Key |
| 120 |
* @param mixed $value - Value |
| 121 |
*/ |
| 122 |
public function set($key, $value = false) { |
| 123 |
if (!$this->api) { |
| 124 |
$this->api = new stdClass(); |
| 125 |
} |
| 126 |
if (is_array($key)) { |
| 127 |
foreach ($key as $attr => $value) { |
| 128 |
$this->api->$attr = $value; |
| 129 |
} |
| 130 |
} else { |
| 131 |
$this->api->$key = $value; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Get License |
| 137 |
* |
| 138 |
* @return string - License Key |
| 139 |
*/ |
| 140 |
public function get_license() { |
| 141 |
return get_option('e2pdf_license'); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Get Domain |
| 146 |
* |
| 147 |
* @return string - Domain |
| 148 |
*/ |
| 149 |
public function get_domain() { |
| 150 |
return site_url(); |
| 151 |
} |
| 152 |
|
| 153 |
} |
| 154 |
|