editor.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. class elFinderEditorOnlineConvert extends elFinderEditor
  3. {
  4. protected $allowed = array('init', 'api');
  5. public function enabled()
  6. {
  7. return defined('ELFINDER_ONLINE_CONVERT_APIKEY') && ELFINDER_ONLINE_CONVERT_APIKEY && (!defined('ELFINDER_DISABLE_ONLINE_CONVERT') || !ELFINDER_DISABLE_ONLINE_CONVERT);
  8. }
  9. public function init()
  10. {
  11. return array('api' => defined('ELFINDER_ONLINE_CONVERT_APIKEY') && ELFINDER_ONLINE_CONVERT_APIKEY && function_exists('curl_init'));
  12. }
  13. public function api()
  14. {
  15. // return array('apires' => array('message' => 'Currently disabled for developping...'));
  16. $endpoint = 'https://api2.online-convert.com/jobs';
  17. $category = $this->argValue('category');
  18. $convert = $this->argValue('convert');
  19. $options = $this->argValue('options');
  20. $source = $this->argValue('source');
  21. $filename = $this->argValue('filename');
  22. $mime = $this->argValue('mime');
  23. $jobid = $this->argValue('jobid');
  24. $string_method = '';
  25. $options = array();
  26. // Currently these converts are make error with API call. I don't know why.
  27. $nonApi = array('android', 'blackberry', 'dpg', 'ipad', 'iphone', 'ipod', 'nintendo-3ds', 'nintendo-ds', 'ps3', 'psp', 'wii', 'xbox');
  28. if (in_array($convert, $nonApi)) {
  29. return array('apires' => array());
  30. }
  31. $ch = null;
  32. if ($convert && $source) {
  33. $request = array(
  34. 'input' => array(array(
  35. 'type' => 'remote',
  36. 'source' => $source
  37. )),
  38. 'conversion' => array(array(
  39. 'target' => $convert
  40. ))
  41. );
  42. if ($filename !== '') {
  43. $request['input'][0]['filename'] = $filename;
  44. }
  45. if ($mime !== '') {
  46. $request['input'][0]['content_type'] = $mime;
  47. }
  48. if ($category) {
  49. $request['conversion'][0]['category'] = $category;
  50. }
  51. if ($options && $options !== 'null') {
  52. $options = json_decode($options, true);
  53. }
  54. if (!is_array($options)) {
  55. $options = array();
  56. }
  57. if ($options) {
  58. $request['conversion'][0]['options'] = $options;
  59. }
  60. $ch = curl_init($endpoint);
  61. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  62. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($request));
  63. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  64. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  65. 'X-Oc-Api-Key: ' . ELFINDER_ONLINE_CONVERT_APIKEY,
  66. 'Content-Type: application/json',
  67. 'cache-control: no-cache'
  68. ));
  69. } else if ($jobid) {
  70. $ch = curl_init($endpoint . '/' . $jobid);
  71. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  72. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
  73. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  74. 'X-Oc-Api-Key: ' . ELFINDER_ONLINE_CONVERT_APIKEY,
  75. 'cache-control: no-cache'
  76. ));
  77. }
  78. if ($ch) {
  79. $response = curl_exec($ch);
  80. $info = curl_getinfo($ch);
  81. $error = curl_error($ch);
  82. curl_close($ch);
  83. if (!empty($error)) {
  84. $res = array('error' => $error);
  85. } else {
  86. $data = json_decode($response, true);
  87. if (isset($data['status']) && isset($data['status']['code']) && $data['status']['code'] === 'completed') {
  88. /** @var elFinderSession $session */
  89. $session = $this->elfinder->getSession();
  90. $urlContentSaveIds = $session->get('urlContentSaveIds', array());
  91. $urlContentSaveIds['OnlineConvert-' . $data['id']] = true;
  92. $session->set('urlContentSaveIds', $urlContentSaveIds);
  93. }
  94. $res = array('apires' => $data);
  95. }
  96. return $res;
  97. } else {
  98. return array('error' => array('errCmdParams', 'editor.OnlineConvert.api'));
  99. }
  100. }
  101. }