/ckeditor/ckeditor/plugins/uploadcare/uploadcare-php/uploadcare/lib/5.3-5.4/Api.php

https://bitbucket.org/seth_sokol/ieltsonabike · PHP · 295 lines · 162 code · 20 blank · 113 comment · 29 complexity · 5a5f154f89132c33e7dd7de5d0e7d269 MD5 · raw file

  1. <?php
  2. namespace Uploadcare;
  3. class Api
  4. {
  5. /**
  6. * Uploadcare public key
  7. *
  8. * @var string
  9. */
  10. private $public_key = null;
  11. /**
  12. * Uploadcare secret key
  13. *
  14. * @var string
  15. */
  16. private $secret_key = null;
  17. /**
  18. * API host for requests
  19. *
  20. * @var string
  21. */
  22. private $api_host = 'api.uploadcare.com';
  23. /**
  24. * Widget instance.
  25. *
  26. * @var Widget
  27. */
  28. public $widget = null;
  29. /**
  30. * Uploader instance
  31. *
  32. * @var Uploader
  33. */
  34. public $uploader = null;
  35. /**
  36. * Uploadcare library version
  37. *
  38. * @var string
  39. */
  40. public $version = '1.0.2/5.3';
  41. /**
  42. * Constructor
  43. *
  44. * @param string $public_key A public key given by Uploadcare.com
  45. * @param string $secret_key A private (secret) key given by Uploadcare.com
  46. * @return void
  47. */
  48. public function __construct($public_key, $secret_key)
  49. {
  50. $this->public_key = $public_key;
  51. $this->secret_key = $secret_key;
  52. $this->widget = new Widget($this);
  53. $this->uploader = new Uploader($this);
  54. }
  55. /**
  56. * Returns public key
  57. *
  58. * @return string
  59. */
  60. public function getPublicKey()
  61. {
  62. return $this->public_key;
  63. }
  64. /**
  65. * Return an array of File objects to work with.
  66. *
  67. * @param integer $page Page to be shown.
  68. * @return array
  69. */
  70. public function getFileList($page = 1)
  71. {
  72. $data = $this->__preparedRequest(API_TYPE_FILES, REQUEST_TYPE_GET, array('page' => $page));
  73. $files_raw = (array)$data->results;
  74. $result = array();
  75. foreach ($files_raw as $file_raw) {
  76. $result[] = new File($file_raw->file_id, $this);
  77. }
  78. return $result;
  79. }
  80. /**
  81. * Get info about pagination.
  82. *
  83. * @param integer $page
  84. * @return array
  85. */
  86. public function getFilePaginationInfo($page = 1)
  87. {
  88. $data = (array)$this->__preparedRequest(API_TYPE_FILES, REQUEST_TYPE_GET, array('page' => $page));
  89. unset($data['results']);
  90. return $data;
  91. }
  92. /**
  93. * Run raw request to REST.
  94. *
  95. * @param string $method Request method: GET, POST, HEAD, OPTIONS, PUT, etc
  96. * @param string $path Path to request
  97. * @param string $data Array of data to send.
  98. * @param string $headers Additonal headers.
  99. * @return array
  100. */
  101. public function request($method, $path, $data = array(), $headers = array())
  102. {
  103. $ch = curl_init(sprintf('https://%s%s', $this->api_host, $path));
  104. $this->__setRequestType($ch, $method);
  105. $this->__setHeaders($ch, $headers, $data);
  106. $data = curl_exec($ch);
  107. $ch_info = curl_getinfo($ch);
  108. if ($method == REQUEST_TYPE_DELETE) {
  109. if ($ch_info['http_code'] != 204) {
  110. throw new \Exception('Request returned unexpected http code '.$ch_info['http_code'].'. '.$data);
  111. }
  112. } else {
  113. if ($ch_info['http_code'] != 200) {
  114. throw new \Exception('Request returned unexpected http code '.$ch_info['http_code'].'. '.$data);
  115. }
  116. }
  117. curl_close($ch);
  118. if ($this->public_key == 'demopublickey' || $this->secret_key == 'demoprivatekey') {
  119. trigger_error('You are using the demo account. Please get an Uploadcare account at https://uploadcare.com/accounts/create/', E_USER_WARNING);
  120. }
  121. return json_decode($data);
  122. }
  123. /**
  124. * Make request to API.
  125. * Throws Exception if not http code 200 was returned.
  126. * If http code 200 it will parse returned data form request as JSON.
  127. *
  128. * @param string $type Construct type. Url will be generated using this params. Options: store
  129. * @param string $request_type Request type. Options: get, post, put, delete.
  130. * @param array $params Additional parameters for requests as array.
  131. * @throws Exception
  132. * @return array
  133. */
  134. public function __preparedRequest($type, $request_type = REQUEST_TYPE_GET, $params = array())
  135. {
  136. $url = $this->__getUrl($type, $params);
  137. $ch = $this->__initRequest($type, $params);
  138. $this->__setRequestType($ch, $request_type);
  139. $this->__setHeaders($ch);
  140. $data = curl_exec($ch);
  141. $ch_info = curl_getinfo($ch);
  142. if ($request_type == REQUEST_TYPE_DELETE) {
  143. if ($ch_info['http_code'] != 204) {
  144. throw new \Exception('Request returned unexpected http code '.$ch_info['http_code'].'. '.$data);
  145. }
  146. } else {
  147. if ($ch_info['http_code'] != 200) {
  148. throw new \Exception('Request returned unexpected http code '.$ch_info['http_code'].'. '.$data);
  149. }
  150. }
  151. curl_close($ch);
  152. if ($this->public_key == 'demopublickey' || $this->secret_key == 'demoprivatekey') {
  153. trigger_error('You are using the demo account. Please get an Uploadcare account at https://uploadcare.com/accounts/create/', E_USER_WARNING);
  154. }
  155. return json_decode($data);
  156. }
  157. /**
  158. * Inits curl request and rerturn handler
  159. *
  160. * @param string $type Construct type. Url will be generated using this params. Options: store
  161. * @param array $params Additional parameters for requests as array.
  162. * @return resource
  163. */
  164. private function __initRequest($type, $params = array())
  165. {
  166. $url = $this->__getUrl($type, $params);
  167. return $ch = curl_init($url);
  168. }
  169. /**
  170. * Return url to send request to.
  171. * Throws Exception if wrong type is provided or parameters missing.
  172. *
  173. * @param string $type Construct type.
  174. * @param array $params Additional parameters for requests as array.
  175. * @throws Exception
  176. * @return string
  177. */
  178. private function __getUrl($type, $params = array())
  179. {
  180. switch ($type) {
  181. case API_TYPE_RAW:
  182. return sprintf('https://%s/', $this->api_host);
  183. case API_TYPE_ACCOUNT:
  184. return sprintf('https://%s/account/', $this->api_host);
  185. case API_TYPE_FILES:
  186. return sprintf('https://%s/files/?page=%s', $this->api_host, $params['page']);
  187. case API_TYPE_STORE:
  188. if (array_key_exists(UC_PARAM_FILE_ID, $params) == false) {
  189. throw new \Exception('Please provide "store_id" param for request');
  190. }
  191. return sprintf('https://%s/files/%s/storage/', $this->api_host, $params['file_id']);
  192. case API_TYPE_FILE:
  193. if (array_key_exists(UC_PARAM_FILE_ID, $params) == false) {
  194. throw new \Exception('Please provide "store_id" param for request');
  195. }
  196. return sprintf('https://%s/files/%s/', $this->api_host, $params['file_id']);
  197. default:
  198. throw new \Exception('No api url type is provided for request. Use store, or appropriate constants.');
  199. }
  200. }
  201. /**
  202. * Set request type.
  203. * If request type is wrong an Exception will be thrown.
  204. *
  205. * @param resource $ch. Curl resource.
  206. * @param string $type Request type. Options: get, post, put, delete.
  207. * @throws Exception
  208. * @return void
  209. */
  210. private function __setRequestType($ch, $type = REQUEST_TYPE_GET)
  211. {
  212. switch ($type) {
  213. case REQUEST_TYPE_GET:
  214. case 'GET':
  215. break;
  216. case REQUEST_TYPE_POST:
  217. case 'POST':
  218. curl_setopt($ch, CURLOPT_POST, true);
  219. break;
  220. case REQUEST_TYPE_PUT:
  221. case 'PUT':
  222. curl_setopt($ch, CURLOPT_PUT, true);
  223. break;
  224. case REQUEST_TYPE_DELETE:
  225. case 'DELETE':
  226. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
  227. break;
  228. case REQUEST_TYPE_HEAD:
  229. case 'HEAD':
  230. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'HEAD');
  231. break;
  232. case REQUEST_TYPE_OPTIONS:
  233. case 'OPTIONS':
  234. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'OPTIONS');
  235. break;
  236. default:
  237. throw new \Exception('No request type is provided for request. Use post, put, delete, get or appropriate constants.');
  238. }
  239. }
  240. /**
  241. * Set all the headers for request and set returntrasfer.
  242. *
  243. * @param resource $ch. Curl resource.
  244. * @param array $headers additional headers.
  245. * @param array $data Data array.
  246. * @return void
  247. */
  248. private function __setHeaders($ch, $add_headers = array(), $data = array())
  249. {
  250. $content_length = 0;
  251. if (count($data)) {
  252. $content_length = strlen(http_build_query($data));
  253. }
  254. $headers = array(
  255. sprintf('Host: %s', $this->api_host),
  256. sprintf('Authorization: Uploadcare.Simple %s:%s', $this->public_key, $this->secret_key),
  257. 'Content-Type: application/json',
  258. 'Content-Length: '.$content_length,
  259. 'User-Agent: PHP Uploadcare Module '.$this->version,
  260. sprintf('Date: %s', date('Y-m-d H:i:s')),
  261. ) + $add_headers;
  262. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  263. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  264. }
  265. /**
  266. * Get object of File class by file_id
  267. *
  268. * @param string $file_id Uploadcare file_id
  269. * @return File
  270. */
  271. public function getFile($file_id)
  272. {
  273. return new File($file_id, $this);
  274. }
  275. }