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