diff --git a/callback.php b/callback.php index ce57f4c..f4c468f 100644 --- a/callback.php +++ b/callback.php @@ -75,6 +75,9 @@ case 'download': $callbackResponseArray = download(); exit(json_encode($callbackResponseArray)); + case 'empty': + $callbackResponseArray = emptyFile(); + exit(json_encode($callbackResponseArray)); default: $callbackResponseArray['status'] = 'error'; $callbackResponseArray['error'] = '404 Method not found'; @@ -210,3 +213,51 @@ function download() readfile($filePath); exit; } + +/** + * Downloading empty file by the document service. + */ +function emptyFile() +{ + global $plugin; + global $type; + global $courseCode; + global $userId; + global $docId; + global $groupId; + global $sessionId; + global $courseInfo; + global $appSettings; + global $jwtManager; + + + if ($type !== 'empty') { + $result['status'] = 'error'; + $result['error'] = 'Download empty with other action'; + return $result; + } + + if ($jwtManager->isJwtEnabled()) { + $token = substr(getallheaders()[$appSettings->getJwtHeader()], strlen('Bearer ')); + try { + $payload = $jwtManager->decode($token, $appSettings->getJwtKey()); + } catch (UnexpectedValueException $e) { + $result['status'] = 'error'; + $result['error'] = '403 Access denied'; + return $result; + } + } + + $template = TemplateManager::getEmptyTemplate('docx'); + + if (!$template) { + $result['status'] = 'error'; + $result['error'] = 'File not found'; + return $result; + } + + @header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document'); + @header('Content-Disposition: attachment; filename='.'docx.docx'); + readfile($template); + exit; +} \ No newline at end of file diff --git a/create.php b/create.php index 73ffdbc..70a080f 100644 --- a/create.php +++ b/create.php @@ -77,7 +77,6 @@ $values = $form->exportValues(); $fileType = $values['fileFormat']; - var_dump($fileType); $fileExt = $documentManager->getDocExtByType($fileType); $result = OnlyofficeDocumentManager::createFile( diff --git a/lib/onlyofficeAppRequests.php b/lib/onlyofficeAppRequests.php index b4854d5..4c4b240 100644 --- a/lib/onlyofficeAppRequests.php +++ b/lib/onlyofficeAppRequests.php @@ -29,47 +29,18 @@ class OnlyofficeAppRequests extends RequestService public function __construct($settingsManager, $httpClient, $jwtManager) { parent::__construct($settingsManager, $httpClient, $jwtManager); - $tempFile = self::createTempFile(); - $this->convertFileUrl = $tempFile['fileUrl']; - $this->convertFilePath = $tempFile['filePath']; - } - - public function __destruct() - { - unlink($this->convertFilePath); } public function getFileUrlForConvert() { - return $this->convertFileUrl; - } - - /** - * Create temporary file for convert service testing. - * - * @return array - */ - private function createTempFile() - { - $fileUrl = null; - $fileName = 'convert.docx'; - $fileExt = strtolower(pathinfo($fileName, PATHINFO_EXTENSION)); - $baseName = strtolower(pathinfo($fileName, PATHINFO_FILENAME)); - $templatePath = TemplateManager::getEmptyTemplate($fileExt); - $folderPath = api_get_path(SYS_PLUGIN_PATH).$this->settingsManager->plugin->getPluginName(); - $filePath = $folderPath.'/'.$fileName; - - if ($fp = @fopen($filePath, 'w')) { - $content = file_get_contents($templatePath); - fputs($fp, $content); - fclose($fp); - chmod($filePath, api_get_permissions_for_new_files()); - $fileUrl = api_get_path(WEB_PLUGIN_PATH).$this->settingsManager->plugin->getPluginName().'/'.$fileName; - } - - return [ - 'fileUrl' => $fileUrl, - 'filePath' => $filePath, + $data = [ + 'type' => 'empty', + 'courseId' => api_get_course_int_id(), + 'userId' => api_get_user_id(), + 'sessionId' => api_get_session_id(), ]; + $hashUrl = $this->jwtManager->getHash($data); + return api_get_path(WEB_PLUGIN_PATH).'onlyoffice/callback.php?hash='.$hashUrl; } + }