/Resources/public/ckeditor4/ckeditor/plugins/uploadcare/uploadcare-php/uploadcare/lib/5.2/Api.php

https://bitbucket.org/ZephyrQG2/sf2bundleeditable · PHP · 302 lines · 164 code · 20 blank · 118 comment · 30 complexity · 0a3b20083b28ff511da9fd19c27edcd0 MD5 · raw file

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