Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release/2.0.0 #4

Merged
merged 17 commits into from
Jan 24, 2020
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
# Change Log

## 2.0.0
--------------------
- Add: `de`, `fi`, `fr`, `hr`, `hu`, `it`, `ja`, `nl`, `pl`, `pt_br`, `vi` translations
- Fix: Editors will now use portal language if user language is null
- Fix: `author` and `created` fields are now passed correctly
- Fix: `firstname` and `lastname` fields are deprecated, using `name` instead
- Fix: Allow editing only for Office Open XML formats
- Fix: Issue with `/` at the end of Document Service url

- Enh: Better way to pass editors config
- Enh: JWT support
- Enh: Files properly marked as updated now
- Enh: Option to convert `.doc .odt .xls. ods .ppt .odp .txt .csv` files to Office Open XML


## 1.0.2
--------------------
- Fix: PHP 7.2 compatibility issues


## 1.0.1
-----------------------
- Enh: Updated translations
15 changes: 12 additions & 3 deletions Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,20 @@ public static function onFileHandlerCollection($event)

/* @var $module \humhub\modules\onlydocuments\Module */
$module = Yii::$app->getModule('onlydocuments');
$file = $event->sender->file;

if ($module->getDocumentType($event->sender->file) !== null) {
if ($collection->type == FileHandlerCollection::TYPE_EDIT) {
if ($module->getDocumentType($file) !== null) {
$canEdit = $collection->type == FileHandlerCollection::TYPE_EDIT && $module->canEdit($file);
$canConvert = $collection->type == FileHandlerCollection::TYPE_EDIT && $module->canConvert($file);
$canView = $collection->type == FileHandlerCollection::TYPE_VIEW && $module->canView($file);

if ($canEdit) {
$collection->register(new filehandler\EditFileHandler());
} elseif ($collection->type === FileHandlerCollection::TYPE_VIEW) {
}
if ($canConvert) {
$collection->register(new filehandler\ConvertFileHandler());
}
if ($canView) {
$collection->register(new filehandler\ViewFileHandler());
}
}
Expand Down
118 changes: 117 additions & 1 deletion Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use yii\helpers\Json;
use humhub\modules\file\libs\FileHelper;
use humhub\libs\CURLHelper;
use \Firebase\JWT\JWT;

/**
* File Module
Expand Down Expand Up @@ -52,9 +53,33 @@ class Module extends \humhub\components\Module
*/
public $textExtensions = ['docx', 'doc', 'odt', 'rtf', 'txt', 'html', 'htm', 'mht', 'pdf', 'djvu', 'fb2', 'epub', 'xps'];

/**
* @var string[] allowed for editing extensions
*/
public $editableExtensions = ['xlsx', 'ppsx', 'pptx', 'docx' ];
public $convertableExtensions = ['doc','odt','xls','ods','ppt','odp','txt','csv'];


public $convertsTo = [
'doc' => 'docx',
'odt' => 'docx',
'txt' => 'docx',
'xls' => 'xlsx',
'ods' => 'xlsx',
'csv' => 'xlsx',
'ppt' => 'pptx',
'odp' => 'pptx',
];



public function isJwtEnabled() {
return !empty($this->getJwtSecret());
}

public function getJwtSecret() {
return $this->settings->get('jwtSecret');
}

public function getServerUrl()
{
return $this->settings->get('serverUrl');
Expand Down Expand Up @@ -84,6 +109,23 @@ public function getDocumentType($file)
return null;
}

public function canEdit($file)
{
$fileExtension = FileHelper::getExtension($file);
return in_array($fileExtension, $this->editableExtensions);
}

public function canConvert($file)
{
$fileExtension = FileHelper::getExtension($file);
return in_array($fileExtension, $this->convertableExtensions);
}

public function canView($file)
{
return !empty($this->getDocumentType($file));
}

/**
* @inheritdoc
*/
Expand All @@ -94,6 +136,22 @@ public function getConfigUrl()
]);
}

/**
* Generate unique document key
*
* @return string
*/
public function generateDocumentKey($file)
{
if (!empty($file->onlydocuments_key)) {
return $file->onlydocuments_key;
}

$key = substr(strtolower(md5(Yii::$app->security->generateRandomString(20))), 0, 20);
$file->updateAttributes(['onlydocuments_key' => $key]);
return $key;
}

public function commandService($data)
{
$url = $this->getServerUrl() . '/coauthoring/CommandService.ashx';
Expand All @@ -105,7 +163,16 @@ public function commandService($data)
'timeout' => 10
]);
$http->setMethod('POST');
$headers = $http->getRequest()->getHeaders();

if ($this->isJwtEnabled()) {
$data['token'] = JWT::encode($data, $this->getJwtSecret());
$headers->addHeaderLine('Authorization', 'Bearer ' . JWT::encode(['payload' => $data], $this->getJwtSecret()));
}

$http->setRawBody(Json::encode($data));
$headers->addHeaderLine('Accept', 'application/json');

$response = $http->send();
$json = $response->getBody();
} catch (\Exception $ex) {
Expand All @@ -121,4 +188,53 @@ public function commandService($data)
}
}

public function convertService($file, $ts)
{
$url = $this->getServerUrl() . '/ConvertService.ashx';
$key = $this->generateDocumentKey($file);

$ext = FileHelper::getExtension($file);
$data = [
'async' => true,
'embeddedfonts' => true,
'filetype' => $ext,
'outputtype' => $this->convertsTo[$ext],
'key' => $key . $ts,
'url' => Url::to(['/onlydocuments/backend/download', 'key' => $key], true),
];

try {
$http = new \Zend\Http\Client($url, [
'adapter' => '\Zend\Http\Client\Adapter\Curl',
'curloptions' => CURLHelper::getOptions(),
'timeout' => 10
]);
$http->setMethod('POST');
$headers = $http->getRequest()->getHeaders();

if ($this->isJwtEnabled()) {
$data['token'] = JWT::encode($data, $this->getJwtSecret());
$headers->addHeaderLine('Authorization', 'Bearer ' . JWT::encode(['payload' => $data], $this->getJwtSecret()));
}

$http->setRawBody(Json::encode($data));
$headers->addHeaderLine('Accept', 'application/json');

$response = $http->send();
$json = $response->getBody();
} catch (\Exception $ex) {
$error = 'Could not get document server response! ' . $ex->getMessage();
Yii::error($error);
return [ 'error' => $error ];
}

try {
return Json::decode($json);
} catch (\yii\base\InvalidParamException $ex) {
$error = 'Could not get document server response! ' . $ex->getMessage();
Yii::error($error);
return [ 'error' => $error ];
}
}

}
51 changes: 45 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,66 @@
# HumHub ONLYOFFICE integration plugin

This plugin enables users to edit office documents from [HumHub](https://www.humhub.com/) using ONLYOFFICE Document Server. Currently the following document formats can be opened and edited with this plugin: DOCX, XLSX, PPTX.
This plugin enables users to edit office documents from [HumHub](https://www.humhub.com/) using ONLYOFFICE Document Server.

## Features
* Currently the following document formats can be opened and edited with this plugin: DOCX, XLSX, PPTX.
* The following formats are available for view only: ODT, ODS, ODP, DOC, XLS, PPT, TXT, PDF.
* The plugin will create a new *Edit/View* menu option for Office documents.
* This allows multiple users to collaborate in real time and to save back those changes to HumHub.
* The following formats can be converted to Office Open XML: ODT, ODS, ODP, DOC, XLS, PPT, TXT, CSV.


## Installing ONLYOFFICE Document Server

You will need an instance of ONLYOFFICE Document Server that is resolvable and connectable both from HumHub and any end clients. If that is not the case, use the official ONLYOFFICE Document Server documetnations page: [Document Server for Linux](http://helpcenter.onlyoffice.com/server/linux/document/linux-installation.aspx). ONLYOFFICE Document Server must also be able to POST to HumHub directly.
You will need an instance of ONLYOFFICE Document Server that is resolvable and connectable both from HumHub and any end clients. If that is not the case, use the official ONLYOFFICE Document Server documentations page: [Document Server for Linux](http://helpcenter.onlyoffice.com/server/linux/document/linux-installation.aspx). ONLYOFFICE Document Server must also be able to POST to HumHub directly.

The easiest way to start an instance of ONLYOFFICE Document Server is to use [Docker](https://github.com/onlyoffice/Docker-DocumentServer).


## Installing ONLYOFFICE Document Server


## Installing HumHub ONLYOFFICE integration plugin

Either install it from HumHub Marketplace or simply clone the repository inside one of the folder specified by `moduleAutoloadPaths` parameter. Please see [HumHub Documentation](http://docs.humhub.org/dev-environment.html#external-modules-directory) for more information.


## Configuring HumHub CONLYOFFICE integration plugin

Navigate to `Administration` -> `Modules` find the plugin under Installed tab and click `Configure`.


## How it works

The ONLYOFFICE integration follows the API documented here https://api.onlyoffice.com/editors/basic:
The ONLYOFFICE integration follows the API documented [here](https://api.onlyoffice.com/editors/basic):

* When creating a new file, the user will be provided with Document, Spreadsheet or Presentation options in the `Create document` menu.

* The browser invokes the `index` method in the `/controllers/CreateController.php` controller.

* Or, when opening an existing file, the user will be provided with `View document` or `Edit document` depending on an extension.

* A popup is opened and the `index` method of the `/controllers/OpenController.php` controller is invoked.

* The app prepares a JSON object with the following properties:

* **url** - the URL that ONLYOFFICE Document Server uses to download the document;
* **callbackUrl** - the URL that ONLYOFFICE Document Server informs about status of the document editing;
* **key** - the random MD5 hash to instruct ONLYOFFICE Document Server whether to download the document again or not;
* **title** - the document Title (name);
* **id** - the identification of the user;
* **name** - the name of the user.

* HumHub takes this object and constructs a page from `views/open/index.php` template, filling in all of those values so that the client browser can load up the editor.

* The client browser makes a request for the javascript library from ONLYOFFICE Document Server and sends ONLYOFFICE Document Server the DocEditor configuration with the above properties.

* Then ONLYOFFICE Document Server downloads the document from HumHub and the user begins editing.

* ONLYOFFICE Document Server sends a POST request to the _callbackUrl_ to inform HumHub that a user is editing the document.

* When all users and client browsers are done with editing, they close the editing window.

* After [10 seconds](https://api.onlyoffice.com/editors/save#savedelay) of inactivity, ONLYOFFICE Document Server sends a POST to the _callbackUrl_ letting HumHub know that the clients have finished editing the document and closed it.

* HumHub downloads the new version of the document, replacing the old one.


## ONLYOFFICE Document Server editions
Expand Down
13 changes: 3 additions & 10 deletions controllers/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,14 @@ public function actionIndex()
$this->view->saved();
}

$version = $this->getDocumentServerVersion();

return $this->render('index', ['model' => $model, 'version' => $version]);
$response = $this->getDocumentServerVersion();
return $this->render('index', ['model' => $model, 'view' => $response]);
}

private function getDocumentServerVersion()
{
$module = Yii::$app->getModule('onlydocuments');
$response = $module->commandService(['c' => 'version']);

if (isset($response['version'])) {
return $response['version'];
}

return null;
return $module->commandService(['c' => 'version']);
}

}
Loading