PluginProbe ʕ •ᴥ•ʔ
File Manager Pro – Filester / 1.8.2
File Manager Pro – Filester v1.8.2
2.1.1 trunk 1.6.1 1.7.6 1.8 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 1.8.9 1.9 2.0 2.0.1 2.0.2 2.1.0
filester / includes / File_manager / lib / php / editors / ZohoOffice / editor.php
filester / includes / File_manager / lib / php / editors / ZohoOffice Last commit date
editor.php 1 year ago
editor.php
207 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://writer.zoho.com/writer/officeapi/v1/document',
22 'sheet' => 'https://sheet.zoho.com/sheet/officeapi/v1/spreadsheet',
23 'show' => 'https://show.zoho.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 public function __construct($elfinder, $args)
49 {
50 parent::__construct($elfinder, $args);
51 $this->myName = preg_replace('/^elFinderEditor/i', '', get_class($this));
52 }
53
54 public function enabled()
55 {
56 return defined('ELFINDER_ZOHO_OFFICE_APIKEY') && ELFINDER_ZOHO_OFFICE_APIKEY && function_exists('curl_init');
57 }
58
59 public function init()
60 {
61 if (!defined('ELFINDER_ZOHO_OFFICE_APIKEY') || !function_exists('curl_init')) {
62 return array('error', array(elFinder::ERROR_CONF, '`ELFINDER_ZOHO_OFFICE_APIKEY` or curl extension'));
63 }
64 if (!empty($this->args['target'])) {
65 $fp = $cfile = null;
66 $hash = $this->args['target'];
67 /** @var elFinderVolumeDriver $srcVol */
68 if (($srcVol = $this->elfinder->getVolume($hash)) && ($file = $srcVol->file($hash))) {
69 $cdata = empty($this->args['cdata']) ? '' : $this->args['cdata'];
70 $cookie = $this->elfinder->getFetchCookieFile();
71 $save = false;
72 $ch = curl_init();
73 $conUrl = elFinder::getConnectorUrl();
74 curl_setopt($ch, CURLOPT_URL, $conUrl . (strpos($conUrl, '?') !== false? '&' : '?') . 'cmd=editor&name=' . $this->myName . '&method=chk&args[target]=' . rawurlencode($hash) . $cdata);
75 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
76 if ($cookie) {
77 curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
78 curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
79 }
80 $res = curl_exec($ch);
81 curl_close($ch);
82 if ($res) {
83 if ($data = json_decode($res, true)) {
84 $save = !empty($data['cansave']);
85 }
86 }
87
88 if ($size = $file['size']) {
89 $src = $srcVol->open($hash);
90 $fp = tmpfile();
91 stream_copy_to_stream($src, $fp);
92 $srcVol->close($src, $hash);
93 $info = stream_get_meta_data($fp);
94 if ($info && !empty($info['uri'])) {
95 $srcFile = $info['uri'];
96 if (class_exists('CURLFile')) {
97 $cfile = new CURLFile($srcFile);
98 $cfile->setPostFilename($file['name']);
99 $cfile->setMimeType($file['mime']);
100 } else {
101 $cfile = '@' . $srcFile;
102 }
103 }
104 }
105 //$srv = $this->args['service'];
106 $format = $srcVol->getExtentionByMime($file['mime']);
107 if (!$format) {
108 $format = substr($file['name'], strrpos($file['name'], '.') * -1);
109 }
110 $lang = $this->args['lang'];
111 if ($lang === 'jp') {
112 $lang = 'ja';
113 }
114 $srvsName = $this->srvs[$file['mime']];
115 $data = array(
116 'apikey' => ELFINDER_ZOHO_OFFICE_APIKEY,
117 'callback_settings' => array(
118 'save_format' => $format,
119 'context_info' => array(
120 'hash' => $hash
121 )
122 ),
123 'editor_settings' => $this->editor_settings[$srvsName],
124 'document_info' => array(
125 'document_name' => substr($file['name'], 0, strlen($file['name']) - strlen($format)- 1)
126 )
127 );
128 $data['editor_settings']['language'] = $lang;
129 if ($save) {
130 $conUrl = elFinder::getConnectorUrl();
131 $data['callback_settings']['save_url'] = $conUrl . (strpos($conUrl, '?') !== false? '&' : '?') . 'cmd=editor&name=' . $this->myName . '&method=save' . $cdata;
132 }
133 foreach($data as $_k => $_v) {
134 if (is_array($_v)){
135 $data[$_k] = json_encode($_v);
136 }
137 }
138 if ($cfile) {
139 $data['document'] = $cfile;
140 }
141 $ch = curl_init();
142 curl_setopt($ch, CURLOPT_URL, $this->urls[$srvsName]);
143 curl_setopt($ch, CURLOPT_TIMEOUT, self::$curlTimeout);
144 curl_setopt($ch, CURLOPT_HEADER, 0);
145 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
146 curl_setopt($ch, CURLOPT_POST, 1);
147 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
148 $res = curl_exec($ch);
149 $error = curl_error($ch);
150 curl_close($ch);
151
152 $fp && fclose($fp);
153
154 if ($res && $res = @json_decode($res, true)) {
155 if (!empty($res['document_url'])) {
156 $ret = array('zohourl' => $res['document_url']);
157 if (!$save) {
158 $ret['warning'] = 'exportToSave';
159 }
160 return $ret;
161 } else {
162 $error = $res;
163 }
164 }
165
166 if ($error) {
167 return array('error' => is_string($error)? preg_split('/[\r\n]+/', $error) : 'Error code: ' . $error);
168 }
169 }
170 }
171
172 return array('error' => array('errCmdParams', 'editor.' . $this->myName . '.init'));
173 }
174
175 public function save()
176 {
177 if (!empty($_POST) && !empty($_POST['id']) && !empty($_FILES) && !empty($_FILES['content'])) {
178 $data = @json_decode(str_replace('&quot;', '"', $_POST['id']), true);
179 if (!empty($data['hash'])) {
180 $hash = $data['hash'];
181 /** @var elFinderVolumeDriver $volume */
182 if ($volume = $this->elfinder->getVolume($hash)) {
183 if ($content = file_get_contents($_FILES['content']['tmp_name'])) {
184 if ($volume->putContents($hash, $content)) {
185 return array('raw' => true, 'error' => '', 'header' => 'HTTP/1.1 200 OK');
186 }
187 }
188 }
189 }
190 }
191 return array('raw' => true, 'error' => '', 'header' => 'HTTP/1.1 500 Internal Server Error');
192 }
193
194 public function chk()
195 {
196 $hash = $this->args['target'];
197 $res = false;
198 /** @var elFinderVolumeDriver $volume */
199 if ($volume = $this->elfinder->getVolume($hash)) {
200 if ($file = $volume->file($hash)) {
201 $res = (bool)$file['write'];
202 }
203 }
204 return array('cansave' => $res);
205 }
206 }
207