editor.php 7.0 KB

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