editor.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. class elFinderEditorZohoOffice extends elFinderEditor
  3. {
  4. private static $curlTimeout = 20;
  5. protected $allowed = array('init', 'save');
  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. if (($srcVol = $this->elfinder->getVolume($hash)) && ($file = $srcVol->file($hash))) {
  43. if ($size = $file['size']) {
  44. $src = $srcVol->open($hash);
  45. $fp = tmpfile();
  46. stream_copy_to_stream($src, $fp);
  47. $srcVol->close($src, $hash);
  48. $info = stream_get_meta_data($fp);
  49. if ($info && !empty($info['uri'])) {
  50. $srcFile = $info['uri'];
  51. if (class_exists('CURLFile')) {
  52. $cfile = new CURLFile($srcFile);
  53. $cfile->setPostFilename($file['name']);
  54. $cfile->setMimeType($file['mime']);
  55. } else {
  56. $cfile = '@'.$srcFile;
  57. }
  58. }
  59. }
  60. //$srv = $this->args['service'];
  61. $format = $srcVol->getExtentionByMime($file['mime']);
  62. if (!$format) {
  63. $format = substr($file['name'], strrpos($file['name'], '.') * -1);
  64. }
  65. $cdata = empty($this->args['cdata']) ? '' : $this->args['cdata'];
  66. $lang = $this->args['lang'];
  67. if ($lang === 'jp') {
  68. $lang = 'ja';
  69. }
  70. $srvsName = $this->srvs[$file['mime']];
  71. $data = array(
  72. 'apikey' => ELFINDER_ZOHO_OFFICE_APIKEY,
  73. 'output' => 'url',
  74. 'mode' => 'normaledit',
  75. 'filename' => $file['name'],
  76. 'id' => $hash,
  77. 'format' => $format,
  78. 'lang' => $lang,
  79. 'saveurl' => elFinder::getConnectorUrl().'?cmd=editor&name=ZohoOffice&method=save'.$cdata,
  80. );
  81. if ($cfile) {
  82. $data['content'] = $cfile;
  83. }
  84. $ch = curl_init();
  85. curl_setopt($ch, CURLOPT_URL, $this->urls[$srvsName]);
  86. curl_setopt($ch, CURLOPT_TIMEOUT, self::$curlTimeout);
  87. curl_setopt($ch, CURLOPT_HEADER, 0);
  88. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  89. curl_setopt($ch, CURLOPT_POST, 1);
  90. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  91. $res = curl_exec($ch);
  92. $error = curl_error($ch);
  93. curl_close($ch);
  94. $fp && fclose($fp);
  95. if ($res) {
  96. if (strpos($res, 'RESULT=TRUE') !== false) {
  97. list(, $url) = explode('URL=', $res);
  98. preg_match('/URL=([^\s]+)/', $res, $m);
  99. return array('zohourl' => $m[1]);
  100. } else {
  101. $error = $res;
  102. }
  103. }
  104. if ($error) {
  105. return array('error' => preg_split('/[\r\n]+/', $error));
  106. }
  107. }
  108. }
  109. return array('error' => array('errCmdParams', 'editor.ZohoOffice.init'));
  110. }
  111. public function save()
  112. {
  113. if (isset($_POST) && ! empty($_POST['id'])) {
  114. $hash = $_POST['id'];
  115. if ($volume = $this->elfinder->getVolume($hash)) {
  116. $content = file_get_contents($_FILES['content']['tmp_name']);
  117. if ($volume->putContents($hash, $content)) {
  118. return array('raw' => true, 'error' => '', 'header' => 'HTTP/1.1 200 OK');
  119. }
  120. }
  121. }
  122. return array('raw' => true, 'error' => '', 'header' => 'HTTP/1.1 500 Internal Server Error');
  123. }
  124. }