editor.php
226 lines
| 1 | <?php |
| 2 | |
| 3 | class elFinderEditorZohoOffice extends elFinderEditor |
| 4 | { |
| 5 | private static $curlTimeout = 20; |
| 6 | |
| 7 | protected $allowed = array('init', 'save', 'chk'); |
| 8 | |
| 9 | protected $editor_settings = array( |
| 10 | 'writer' => array( |
| 11 | 'unit' => 'mm', |
| 12 | 'view' => 'pageview' |
| 13 | ), |
| 14 | 'sheet' => array( |
| 15 | 'country' => 'US' |
| 16 | ), |
| 17 | 'show' => array() |
| 18 | ); |
| 19 | |
| 20 | private $urls = array( |
| 21 | 'writer' => 'https://api.office-integrator.com/writer/officeapi/v1/document', |
| 22 | 'sheet' => 'https://api.office-integrator.com/sheet/officeapi/v1/spreadsheet', |
| 23 | 'show' => 'https://api.office-integrator.com/show/officeapi/v1/presentation', |
| 24 | ); |
| 25 | |
| 26 | private $srvs = array( |
| 27 | 'application/msword' => 'writer', |
| 28 | 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'writer', |
| 29 | 'application/pdf' => 'writer', |
| 30 | 'application/vnd.oasis.opendocument.text' => 'writer', |
| 31 | 'application/rtf' => 'writer', |
| 32 | 'text/html' => 'writer', |
| 33 | 'application/vnd.ms-excel' => 'sheet', |
| 34 | 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'sheet', |
| 35 | 'application/vnd.oasis.opendocument.spreadsheet' => 'sheet', |
| 36 | 'application/vnd.sun.xml.calc' => 'sheet', |
| 37 | 'text/csv' => 'sheet', |
| 38 | 'text/tab-separated-values' => 'sheet', |
| 39 | 'application/vnd.ms-powerpoint' => 'show', |
| 40 | 'application/vnd.openxmlformats-officedocument.presentationml.presentation' => 'show', |
| 41 | 'application/vnd.openxmlformats-officedocument.presentationml.slideshow' => 'show', |
| 42 | 'application/vnd.oasis.opendocument.presentation' => 'show', |
| 43 | 'application/vnd.sun.xml.impress' => 'show', |
| 44 | ); |
| 45 | |
| 46 | private $myName = ''; |
| 47 | |
| 48 | protected function extentionNormrize($extention, $srvsName) { |
| 49 | switch($srvsName) { |
| 50 | case 'writer': |
| 51 | if (!in_array($extention, array('zdoc', 'docx', 'rtf', 'odt', 'html', 'txt'))) { |
| 52 | $extention = 'docx'; |
| 53 | } |
| 54 | break; |
| 55 | case 'sheet': |
| 56 | if (!in_array($extention, array('zsheet', 'xls', 'xlsx', 'ods', 'csv', 'tsv'))) { |
| 57 | $extention = 'xlsx'; |
| 58 | } |
| 59 | break; |
| 60 | case 'show': |
| 61 | if (!in_array($extention, array('zslides', 'pptx', 'pps', 'ppsx', 'odp', 'sxi'))) { |
| 62 | $extention = 'pptx'; |
| 63 | } |
| 64 | break; |
| 65 | |
| 66 | } |
| 67 | return $extention; |
| 68 | } |
| 69 | |
| 70 | public function __construct($elfinder, $args) |
| 71 | { |
| 72 | parent::__construct($elfinder, $args); |
| 73 | $this->myName = preg_replace('/^elFinderEditor/i', '', get_class($this)); |
| 74 | } |
| 75 | |
| 76 | public function enabled() |
| 77 | { |
| 78 | return defined('ELFINDER_ZOHO_OFFICE_APIKEY') && ELFINDER_ZOHO_OFFICE_APIKEY && function_exists('curl_init'); |
| 79 | } |
| 80 | |
| 81 | public function init() |
| 82 | { |
| 83 | if (!defined('ELFINDER_ZOHO_OFFICE_APIKEY') || !function_exists('curl_init')) { |
| 84 | return array('error', array(elFinder::ERROR_CONF, '`ELFINDER_ZOHO_OFFICE_APIKEY` or curl extension')); |
| 85 | } |
| 86 | if (!empty($this->args['target'])) { |
| 87 | $fp = $cfile = null; |
| 88 | $hash = $this->args['target']; |
| 89 | /** @var elFinderVolumeDriver $srcVol */ |
| 90 | if (($srcVol = $this->elfinder->getVolume($hash)) && ($file = $srcVol->file($hash))) { |
| 91 | $cdata = empty($this->args['cdata']) ? '' : $this->args['cdata']; |
| 92 | $cookie = $this->elfinder->getFetchCookieFile(); |
| 93 | $save = false; |
| 94 | $ch = curl_init(); |
| 95 | $conUrl = elFinder::getConnectorUrl(); |
| 96 | curl_setopt($ch, CURLOPT_URL, $conUrl . (strpos($conUrl, '?') !== false? '&' : '?') . 'cmd=editor&name=' . $this->myName . '&method=chk&args[target]=' . rawurlencode($hash) . $cdata); |
| 97 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
| 98 | if ($cookie) { |
| 99 | curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie); |
| 100 | curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); |
| 101 | } |
| 102 | $res = curl_exec($ch); |
| 103 | curl_close($ch); |
| 104 | if ($res) { |
| 105 | if ($data = json_decode($res, true)) { |
| 106 | $save = !empty($data['cansave']); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | if ($size = $file['size']) { |
| 111 | $src = $srcVol->open($hash); |
| 112 | $fp = tmpfile(); |
| 113 | stream_copy_to_stream($src, $fp); |
| 114 | $srcVol->close($src, $hash); |
| 115 | $info = stream_get_meta_data($fp); |
| 116 | if ($info && !empty($info['uri'])) { |
| 117 | $srcFile = $info['uri']; |
| 118 | if (class_exists('CURLFile')) { |
| 119 | $cfile = new CURLFile($srcFile); |
| 120 | $cfile->setPostFilename($file['name']); |
| 121 | $cfile->setMimeType($file['mime']); |
| 122 | } else { |
| 123 | $cfile = '@' . $srcFile; |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | //$srv = $this->args['service']; |
| 128 | $srvsName = $this->srvs[$file['mime']]; |
| 129 | $format = $this->extentionNormrize($srcVol->getExtentionByMime($file['mime']), $srvsName); |
| 130 | if (!$format) { |
| 131 | $format = substr($file['name'], strrpos($file['name'], '.') * -1); |
| 132 | } |
| 133 | $lang = $this->args['lang']; |
| 134 | if ($lang === 'jp') { |
| 135 | $lang = 'ja'; |
| 136 | } |
| 137 | $data = array( |
| 138 | 'apikey' => ELFINDER_ZOHO_OFFICE_APIKEY, |
| 139 | 'callback_settings' => array( |
| 140 | 'save_format' => $format, |
| 141 | 'save_url_params' => array( |
| 142 | 'hash' => $hash |
| 143 | ) |
| 144 | ), |
| 145 | 'editor_settings' => $this->editor_settings[$srvsName], |
| 146 | 'document_info' => array( |
| 147 | 'document_name' => substr($file['name'], 0, strlen($file['name']) - strlen($format)- 1) |
| 148 | ) |
| 149 | ); |
| 150 | $data['editor_settings']['language'] = $lang; |
| 151 | if ($save) { |
| 152 | $conUrl = elFinder::getConnectorUrl(); |
| 153 | $data['callback_settings']['save_url'] = $conUrl . (strpos($conUrl, '?') !== false? '&' : '?') . 'cmd=editor&name=' . $this->myName . '&method=save' . $cdata; |
| 154 | } |
| 155 | foreach($data as $_k => $_v) { |
| 156 | if (is_array($_v)){ |
| 157 | $data[$_k] = json_encode($_v); |
| 158 | } |
| 159 | } |
| 160 | if ($cfile) { |
| 161 | $data['document'] = $cfile; |
| 162 | } |
| 163 | $ch = curl_init(); |
| 164 | curl_setopt($ch, CURLOPT_URL, $this->urls[$srvsName]); |
| 165 | curl_setopt($ch, CURLOPT_TIMEOUT, self::$curlTimeout); |
| 166 | curl_setopt($ch, CURLOPT_HEADER, 0); |
| 167 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
| 168 | curl_setopt($ch, CURLOPT_POST, 1); |
| 169 | curl_setopt($ch, CURLOPT_POSTFIELDS, $data); |
| 170 | $res = curl_exec($ch); |
| 171 | $error = curl_error($ch); |
| 172 | curl_close($ch); |
| 173 | |
| 174 | $fp && fclose($fp); |
| 175 | |
| 176 | if ($res && $res = @json_decode($res, true)) { |
| 177 | if (!empty($res['document_url'])) { |
| 178 | $ret = array('zohourl' => $res['document_url']); |
| 179 | if (!$save) { |
| 180 | $ret['warning'] = 'exportToSave'; |
| 181 | } |
| 182 | return $ret; |
| 183 | } else { |
| 184 | $error = $res; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | if ($error) { |
| 189 | return array('error' => is_string($error)? preg_split('/[\r\n]+/', $error) : 'Error code: ' . $error); |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | return array('error' => array('errCmdParams', 'editor.' . $this->myName . '.init')); |
| 195 | } |
| 196 | |
| 197 | public function save() |
| 198 | { |
| 199 | if (!empty($_POST) && !empty($_POST['hash']) && !empty($_FILES) && !empty($_FILES['content'])) { |
| 200 | $hash = $_POST['hash']; |
| 201 | /** @var elFinderVolumeDriver $volume */ |
| 202 | if ($volume = $this->elfinder->getVolume($hash)) { |
| 203 | if ($content = file_get_contents($_FILES['content']['tmp_name'])) { |
| 204 | if ($volume->putContents($hash, $content)) { |
| 205 | return array('raw' => true, 'error' => '', 'header' => 'HTTP/1.1 200 OK'); |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | return array('raw' => true, 'error' => '', 'header' => 'HTTP/1.1 500 Internal Server Error'); |
| 211 | } |
| 212 | |
| 213 | public function chk() |
| 214 | { |
| 215 | $hash = $this->args['target']; |
| 216 | $res = false; |
| 217 | /** @var elFinderVolumeDriver $volume */ |
| 218 | if ($volume = $this->elfinder->getVolume($hash)) { |
| 219 | if ($file = $volume->file($hash)) { |
| 220 | $res = (bool)$file['write']; |
| 221 | } |
| 222 | } |
| 223 | return array('cansave' => $res); |
| 224 | } |
| 225 | } |
| 226 |