From ea297a711297a41da511bbd5ca7b35c52278caf2 Mon Sep 17 00:00:00 2001
From: Damien <damien.burel@maarch.org>
Date: Mon, 6 Jan 2020 16:14:15 +0100
Subject: [PATCH] FEAT #12635 TIME 0:15 Only office available

---
 core/class/class_functions.php                   |  3 ++-
 rest/index.php                                   |  7 ++++---
 .../controllers/OnlyOfficeController.php         | 16 ++++++++++++++++
 3 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/core/class/class_functions.php b/core/class/class_functions.php
index 509fee12deb..ba6253ac958 100755
--- a/core/class/class_functions.php
+++ b/core/class/class_functions.php
@@ -707,7 +707,8 @@ class functions
     public function return_bytes($val)
     {
         $val = trim($val);
-        $last = strtolower($val{strlen($val)-1});
+        $strlen = strlen($val)-1;
+        $last = strtolower($val.$strlen);
         switch($last) {
             // 'G' modifier available since PHP 5.1.0
             case 'g':
diff --git a/rest/index.php b/rest/index.php
index 369be2a6f29..46717cff691 100755
--- a/rest/index.php
+++ b/rest/index.php
@@ -145,9 +145,6 @@ $app->post('/convertedFile', \Convert\controllers\ConvertPdfController::class .
 $app->get('/convertedFile/{filename}', \Convert\controllers\ConvertPdfController::class . ':getConvertedFileByFilename');
 
 //ContentManagement
-$app->post('/onlyOfficeCallback', function (\Slim\Http\Request $request, \Slim\Http\Response $response) {
-    return $response->withJson(['error' => 0]);
-});
 $app->post('/jnlp', \ContentManagement\controllers\JnlpController::class . ':generateJnlp');
 $app->get('/jnlp/{jnlpUniqueId}', \ContentManagement\controllers\JnlpController::class . ':renderJnlp');
 $app->post('/jnlp/{jnlpUniqueId}', \ContentManagement\controllers\JnlpController::class . ':processJnlp');
@@ -157,6 +154,10 @@ $app->get('/onlyOffice/configuration', \ContentManagement\controllers\OnlyOffice
 $app->post('/onlyOffice/mergedFile', \ContentManagement\controllers\OnlyOfficeController::class . ':saveMergedFile');
 $app->get('/onlyOffice/mergedFile', \ContentManagement\controllers\OnlyOfficeController::class . ':getMergedFile');
 $app->get('/onlyOffice/encodedFile', \ContentManagement\controllers\OnlyOfficeController::class . ':getEncodedFileFromUrl');
+$app->get('/onlyOffice/available', \ContentManagement\controllers\OnlyOfficeController::class . ':isAvailable');
+$app->post('/onlyOfficeCallback', function (\Slim\Http\Request $request, \Slim\Http\Response $response) {
+    return $response->withJson(['error' => 0]);
+});
 
 //CustomFields
 $app->get('/customFields', \CustomField\controllers\CustomFieldController::class . ':get');
diff --git a/src/app/contentManagement/controllers/OnlyOfficeController.php b/src/app/contentManagement/controllers/OnlyOfficeController.php
index 6115d1b694d..3b8c915851b 100644
--- a/src/app/contentManagement/controllers/OnlyOfficeController.php
+++ b/src/app/contentManagement/controllers/OnlyOfficeController.php
@@ -164,4 +164,20 @@ class OnlyOfficeController
 
         return $response->withJson(['encodedFile' => base64_encode($fileContent)]);
     }
+
+    public static function isAvailable(Request $request, Response $response)
+    {
+        $loadedXml = CoreConfigModel::getXmlLoaded(['path' => 'apps/maarch_entreprise/xml/documentEditorsConfig.xml']);
+        if (empty($loadedXml) || empty($loadedXml->onlyoffice->enabled) || $loadedXml->onlyoffice->enabled == 'false' || empty($loadedXml->onlyoffice->server_uri)) {
+            return $response->withStatus(400)->withJson(['errors' => 'Onlyoffice is not enabled']);
+        }
+
+        $uri = (string)$loadedXml->onlyoffice->server_uri;
+
+        $exec = shell_exec("ping -c 1 -w 5 {$uri}");
+
+        $isAvailable = strpos($exec, '1 packets transmitted, 1 received') !== false;
+
+        return $response->withJson(['isAvailable' => $isAvailable]);
+    }
 }
-- 
GitLab