editor.php 8.4 KB

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