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

https://bitbucket.org/seth_sokol/ieltsonabike · PHP · 238 lines · 139 code · 21 blank · 78 comment · 11 complexity · 02edf192cd1227bfa046827e82c61f2a MD5 · raw file

  1. <?php
  2. namespace Uploadcare;
  3. class Uploader
  4. {
  5. /**
  6. * Base upload host
  7. *
  8. * @var string
  9. */
  10. private $host = 'upload.uploadcare.com';
  11. /**
  12. * Api instance
  13. *
  14. * @var Api
  15. */
  16. private $api = null;
  17. /**
  18. * Constructor
  19. */
  20. public function __construct(Api $api)
  21. {
  22. $this->api = $api;
  23. }
  24. /**
  25. * Check file status.
  26. * Return array of json data
  27. *
  28. * @param string $file_id
  29. * @return array
  30. */
  31. public function status($token)
  32. {
  33. $data = array(
  34. 'token' => $token,
  35. );
  36. $ch = $this->__initRequest('status', $data);
  37. $this->__setHeaders($ch);
  38. $data = $this->__runRequest($ch);
  39. return $data;
  40. }
  41. /**
  42. * Upload file from url and get File instance
  43. *
  44. * @param string $url An url of file to be uploaded.
  45. * @return File
  46. */
  47. public function fromUrl($url, $check_status = true, $timeout = 1, $max_attempts = 5)
  48. {
  49. $data = array(
  50. '_' => time(),
  51. 'source_url' => $url,
  52. 'pub_key' => $this->api->getPublicKey(),
  53. );
  54. $ch = $this->__initRequest('from_url', $data);
  55. $this->__setHeaders($ch);
  56. $data = $this->__runRequest($ch);
  57. $token = $data->token;
  58. if ($check_status) {
  59. $success = false;
  60. $attempts = 0;
  61. while (!$success) {
  62. $data = $this->status($token);
  63. if ($data->status == 'success') {
  64. $success = true;
  65. }
  66. if ($attempts == $max_attempts) {
  67. throw new \Exception('Cannot store file, max attempts reached, upload is not successful');
  68. }
  69. sleep($timeout);
  70. $attempts++;
  71. }
  72. } else {
  73. return $token;
  74. }
  75. $file_id = $data->file_id;
  76. return new File($file_id, $this->api);
  77. }
  78. /**
  79. * Upload file from local path.
  80. *
  81. * @param string $path
  82. * @return File
  83. */
  84. public function fromPath($path)
  85. {
  86. $data = array(
  87. 'UPLOADCARE_PUB_KEY' => $this->api->getPublicKey(),
  88. 'file' => '@'.$path,
  89. );
  90. $ch = $this->__initRequest('base');
  91. $this->__setRequestType($ch);
  92. $this->__setData($ch, $data);
  93. $this->__setHeaders($ch);
  94. $data = $this->__runRequest($ch);
  95. $file_id = $data->file;
  96. return new File($file_id, $this->api);
  97. }
  98. /**
  99. * Upload file from file pointer
  100. *
  101. * @param resourse $fp
  102. * @return File
  103. */
  104. public function fromResource($fp)
  105. {
  106. $tmpfile = tempnam(sys_get_temp_dir(), 'ucr');
  107. $temp = fopen($tmpfile, 'w');
  108. while (!feof($fp)) {
  109. fwrite($temp, fread($fp, 8192));
  110. }
  111. fclose($temp);
  112. fclose($fp);
  113. $data = array(
  114. 'UPLOADCARE_PUB_KEY' => $this->api->getPublicKey(),
  115. 'file' => '@'.$tmpfile,
  116. );
  117. $ch = $this->__initRequest('base');
  118. $this->__setRequestType($ch);
  119. $this->__setData($ch, $data);
  120. $this->__setHeaders($ch);
  121. $data = $this->__runRequest($ch);
  122. $file_id = $data->file;
  123. return new File($file_id, $this->api);
  124. }
  125. /**
  126. * Upload file from string using mime-type.
  127. *
  128. * @param string $content
  129. * @param string $mime_type
  130. * @return File
  131. */
  132. public function fromContent($content, $mime_type)
  133. {
  134. $tmpfile = tempnam(sys_get_temp_dir(), 'ucr');
  135. $temp = fopen($tmpfile, 'w');
  136. fwrite($temp, $content);
  137. fclose($temp);
  138. $data = array(
  139. 'UPLOADCARE_PUB_KEY' => $this->api->getPublicKey(),
  140. 'file' => sprintf('@%s;type=%s', $tmpfile, $mime_type),
  141. );
  142. $ch = $this->__initRequest('base');
  143. $this->__setRequestType($ch);
  144. $this->__setData($ch, $data);
  145. $this->__setHeaders($ch);
  146. $data = $this->__runRequest($ch);
  147. $file_id = $data->file;
  148. return new File($file_id, $this->api);
  149. }
  150. /**
  151. * Init upload request and return curl resource
  152. *
  153. * @param array $data
  154. * @return resource
  155. */
  156. private function __initRequest($type, $data = null)
  157. {
  158. $url = sprintf('https://%s/%s/', $this->host, $type);
  159. if (is_array($data)) {
  160. $url = sprintf('%s?%s', $url, http_build_query($data));
  161. }
  162. $ch = curl_init($url);
  163. return $ch;
  164. }
  165. /**
  166. * Set request type for curl resrouce
  167. *
  168. * @param resource $ch
  169. * @return void
  170. */
  171. private function __setRequestType($ch)
  172. {
  173. curl_setopt($ch, CURLOPT_POST, true);
  174. }
  175. /**
  176. * Set all the headers for request and set returntrasfer.
  177. *
  178. * @param resource $ch. Curl resource.
  179. * @return void
  180. */
  181. private function __setHeaders($ch)
  182. {
  183. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  184. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  185. 'User-Agent: PHP Uploadcare Module '.$this->api->version,
  186. ));
  187. }
  188. /**
  189. * Set data to be posted on request
  190. *
  191. * @param resource $ch. Curl resource
  192. * @param array $data
  193. * @return void
  194. */
  195. private function __setData($ch, $data = array())
  196. {
  197. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  198. }
  199. /**
  200. * Run prepared curl request.
  201. * Throws Exception of not 200 http code
  202. *
  203. * @param resource $ch. Curl resource
  204. * @throws Exception
  205. * @return array
  206. */
  207. private function __runRequest($ch)
  208. {
  209. $data = curl_exec($ch);
  210. $ch_info = curl_getinfo($ch);
  211. if ($ch_info['http_code'] != 200) {
  212. throw new \Exception('Request returned unexpected http code '.$ch_info['http_code'].'. '.$data);
  213. }
  214. curl_close($ch);
  215. return json_decode($data);
  216. }
  217. }