From b2fad1734375fcd953fbcd2a334b757dba36e985 Mon Sep 17 00:00:00 2001
From: Damien <damien.burel@maarch.org>
Date: Fri, 31 Jan 2020 17:24:42 +0100
Subject: [PATCH] FEAT #12346 TIME 1:20 Resource modification & only documents

---
 .../controllers/JnlpController.php            | 19 ++++++++-
 .../controllers/OnlyOfficeController.php      | 14 +++++++
 .../controllers/ConvertPdfController.php      |  3 +-
 .../resource/controllers/ResController.php    | 16 ++++++--
 .../controllers/ResourceControlController.php | 40 ++++++++++++-------
 5 files changed, 73 insertions(+), 19 deletions(-)

diff --git a/src/app/contentManagement/controllers/JnlpController.php b/src/app/contentManagement/controllers/JnlpController.php
index dfaf8973c5a..26b96e8d751 100755
--- a/src/app/contentManagement/controllers/JnlpController.php
+++ b/src/app/contentManagement/controllers/JnlpController.php
@@ -17,6 +17,7 @@ namespace ContentManagement\controllers;
 use Attachment\models\AttachmentModel;
 use Docserver\models\DocserverModel;
 use Resource\controllers\ResController;
+use Resource\models\ResModel;
 use Slim\Http\Request;
 use Slim\Http\Response;
 use SrcCore\controllers\UrlController;
@@ -254,6 +255,23 @@ class JnlpController
 
                 file_put_contents($tmpPath . $newFileOnTmp, base64_decode($mergedDocument['encodedDocument']));
                 $pathToCopy = $tmpPath . $newFileOnTmp;
+            } elseif ($queryParams['objectType'] == 'resourceModification') {
+                if (!ResController::hasRightByResId(['resId' => [$queryParams['objectId']], 'userId' => $GLOBALS['id']])) {
+                    $xmlResponse = JnlpController::generateResponse(['type' => 'ERROR', 'data' => ['ERROR' => "Resource out of perimeter"]]);
+                    $response->write($xmlResponse);
+                    return $response->withHeader('Content-Type', 'application/xml');
+                }
+                $resource = ResModel::getById(['resId' => $body['objectId'], 'select' => ['docserver_id', 'path', 'filename']]);
+                if (empty($resource['filename'])) {
+                    $xmlResponse = JnlpController::generateResponse(['type' => 'ERROR', 'data' => ['ERROR' => "Resource has no file"]]);
+                    $response->write($xmlResponse);
+                    return $response->withHeader('Content-Type', 'application/xml');
+                }
+
+                $docserver  = DocserverModel::getByDocserverId(['docserverId' => $resource['docserver_id'], 'select' => ['path_template']]);
+                $pathToCopy = $docserver['path_template'] . str_replace('#', DIRECTORY_SEPARATOR, $resource['path']) . $resource['filename'];
+                $extension  = pathinfo($pathToCopy, PATHINFO_EXTENSION);
+                $newFileOnTmp = "tmp_file_{$GLOBALS['id']}_{$args['jnlpUniqueId']}.{$extension}";
             } elseif ($queryParams['objectType'] == 'attachmentModification') {
                 $attachment = AttachmentModel::getById(['id' => $queryParams['objectId'], 'select' => ['docserver_id', 'path', 'filename', 'res_id_master']]);
                 if (empty($attachment)) {
@@ -268,7 +286,6 @@ class JnlpController
                 }
 
                 $docserver  = DocserverModel::getByDocserverId(['docserverId' => $attachment['docserver_id'], 'select' => ['path_template']]);
-
                 $pathToCopy = $docserver['path_template'] . str_replace('#', DIRECTORY_SEPARATOR, $attachment['path']) . $attachment['filename'];
                 $extension  = pathinfo($pathToCopy, PATHINFO_EXTENSION);
                 $newFileOnTmp = "tmp_file_{$GLOBALS['id']}_{$args['jnlpUniqueId']}.{$extension}";
diff --git a/src/app/contentManagement/controllers/OnlyOfficeController.php b/src/app/contentManagement/controllers/OnlyOfficeController.php
index adaeaf99f0a..4bd05966fb8 100644
--- a/src/app/contentManagement/controllers/OnlyOfficeController.php
+++ b/src/app/contentManagement/controllers/OnlyOfficeController.php
@@ -17,6 +17,7 @@ namespace ContentManagement\controllers;
 use Attachment\models\AttachmentModel;
 use Docserver\models\DocserverModel;
 use Resource\controllers\ResController;
+use Resource\models\ResModel;
 use Respect\Validation\Validator;
 use Slim\Http\Request;
 use Slim\Http\Response;
@@ -87,6 +88,19 @@ class OnlyOfficeController
                 'data' => $dataToMerge
             ]);
             $fileContent = base64_decode($mergedDocument['encodedDocument']);
+        } elseif ($body['objectType'] == 'resourceModification') {
+            if (!ResController::hasRightByResId(['resId' => [$body['objectId']], 'userId' => $GLOBALS['id']])) {
+                return $response->withStatus(400)->withJson(['errors' => 'Resource out of perimeter']);
+            }
+            $resource = ResModel::getById(['resId' => $body['objectId'], 'select' => ['docserver_id', 'path', 'filename']]);
+            if (empty($resource['filename'])) {
+                return $response->withStatus(400)->withJson(['errors' => 'Resource has no file']);
+            }
+
+            $docserver  = DocserverModel::getByDocserverId(['docserverId' => $resource['docserver_id'], 'select' => ['path_template']]);
+
+            $path = $docserver['path_template'] . str_replace('#', DIRECTORY_SEPARATOR, $resource['path']) . $resource['filename'];
+            $fileContent = file_get_contents($path);
         } elseif ($body['objectType'] == 'attachmentModification') {
             $attachment = AttachmentModel::getById(['id' => $body['objectId'], 'select' => ['docserver_id', 'path', 'filename', 'res_id_master']]);
             if (empty($attachment)) {
diff --git a/src/app/convert/controllers/ConvertPdfController.php b/src/app/convert/controllers/ConvertPdfController.php
index 0fb34de2838..ace73a6407f 100755
--- a/src/app/convert/controllers/ConvertPdfController.php
+++ b/src/app/convert/controllers/ConvertPdfController.php
@@ -209,7 +209,7 @@ class ConvertPdfController
         return $convertedDocument;
     }
 
-    private static function canConvert(array $args)
+    public static function canConvert(array $args)
     {
         ValidatorModel::notEmpty($args, ['extension']);
         ValidatorModel::stringType($args, ['extension']);
@@ -220,6 +220,7 @@ class ConvertPdfController
             foreach ($loadedXml->FORMAT as $value) {
                 if (strtoupper((string)$value->name) == strtoupper($args['extension']) && (string)$value->canConvert == 'true') {
                     $canConvert = true;
+                    break;
                 }
             }
         }
diff --git a/src/app/resource/controllers/ResController.php b/src/app/resource/controllers/ResController.php
index b0225c6f3f2..dddc974be10 100755
--- a/src/app/resource/controllers/ResController.php
+++ b/src/app/resource/controllers/ResController.php
@@ -250,7 +250,9 @@ class ResController extends ResourceControlController
             unset($body['diffusionList']);
         }
 
-        $control = ResourceControlController::controlUpdateResource(['body' => $body, 'resId' => $args['resId'], 'isProcessing' => $isProcessing]);
+        $onlyDocument = !empty($queryParams['onlyDocument']);
+
+        $control = ResourceControlController::controlUpdateResource(['body' => $body, 'resId' => $args['resId'], 'isProcessing' => $isProcessing, 'onlyDocument' => $onlyDocument]);
         if (!empty($control['errors'])) {
             return $response->withStatus(400)->withJson(['errors' => $control['errors']]);
         }
@@ -268,15 +270,23 @@ class ResController extends ResourceControlController
             ]);
         }
 
+        if ($onlyDocument) {
+            $body = [
+                'encodedFile'   => $body['encodedFile'],
+                'format'        => $body['format']
+            ];
+        }
         $body['resId'] = $args['resId'];
         $resId = StoreController::storeResource($body);
         if (empty($resId) || !empty($resId['errors'])) {
             return $response->withStatus(500)->withJson(['errors' => '[ResController update] ' . $resId['errors']]);
         }
 
-        ResController::updateAdjacentData(['body' => $body, 'resId' => $args['resId']]);
+        if (!$onlyDocument) {
+            ResController::updateAdjacentData(['body' => $body, 'resId' => $args['resId']]);
+        }
 
-        if (!empty($body['encodedFile'])) {
+        if ($onlyDocument) {
             ConvertPdfController::convert([
                 'resId'     => $args['resId'],
                 'collId'    => 'letterbox_coll',
diff --git a/src/app/resource/controllers/ResourceControlController.php b/src/app/resource/controllers/ResourceControlController.php
index bc78ea41c1e..41b317946e1 100644
--- a/src/app/resource/controllers/ResourceControlController.php
+++ b/src/app/resource/controllers/ResourceControlController.php
@@ -15,6 +15,7 @@
 namespace Resource\controllers;
 
 use Contact\models\ContactModel;
+use Convert\controllers\ConvertPdfController;
 use Convert\models\AdrModel;
 use CustomField\models\CustomFieldModel;
 use Doctype\models\DoctypeModel;
@@ -118,7 +119,11 @@ class ResourceControlController
     {
         $body = $args['body'];
 
-        $resource = ResModel::getById(['resId' => $args['resId'], 'select' => ['status', 'model_id', 'external_signatory_book_id']]);
+        if (empty($body)) {
+            return ['errors' => 'Body is not set or empty'];
+        }
+
+        $resource = ResModel::getById(['resId' => $args['resId'], 'select' => ['status', 'model_id', 'external_signatory_book_id', 'format']]);
         if (empty($resource['status'])) {
             return ['errors' => 'Resource status is empty. It can not be modified'];
         }
@@ -127,26 +132,33 @@ class ResourceControlController
             return ['errors' => 'Resource can not be modified because of status'];
         }
 
-        if (empty($body)) {
-            return ['errors' => 'Body is not set or empty'];
-        } elseif (!Validator::intVal()->notEmpty()->validate($body['doctype'])) {
-            return ['errors' => 'Body doctype is empty or not an integer'];
-        } elseif (!empty($body['encodedFile']) && !empty($resource['external_signatory_book_id'])) {
-            return ['errors' => 'Resource is in external signature book, file can not be modified'];
-        } elseif (!empty($body['encodedFile']) && ResourceControlController::isSigned(['resId' => $args['resId']])) {
-            return ['errors' => 'Resource is signed, file can not be modified'];
+        if ($args['onlyDocument'] && empty($body['encodedFile'])) {
+            return ['errors' => 'Body encodedFile is not set or empty'];
+        } elseif (!empty($body['encodedFile'])) {
+            if (!empty($resource['external_signatory_book_id'])) {
+                return ['errors' => 'Resource is in external signature book, file can not be modified'];
+            } elseif (ResourceControlController::isSigned(['resId' => $args['resId']])) {
+                return ['errors' => 'Resource is signed, file can not be modified'];
+            } elseif (!empty($resource['format']) && !ConvertPdfController::canConvert(['extension' => $resource['format']])) {
+                return ['errors' => 'Resource is not convertible, file can not be modified'];
+            }
+            $control = ResourceControlController::controlFileData(['body' => $body]);
+            if (!empty($control['errors'])) {
+                return ['errors' => $control['errors']];
+            }
+            if ($args['onlyDocument']) {
+                return true;
+            }
         }
 
+        if (!Validator::intVal()->notEmpty()->validate($body['doctype'])) {
+            return ['errors' => 'Body doctype is empty or not an integer'];
+        }
         $doctype = DoctypeModel::getById(['id' => $body['doctype'], 'select' => [1]]);
         if (empty($doctype)) {
             return ['errors' => 'Body doctype does not exist'];
         }
 
-        $control = ResourceControlController::controlFileData(['body' => $body]);
-        if (!empty($control['errors'])) {
-            return ['errors' => $control['errors']];
-        }
-
         $control = ResourceControlController::controlAdjacentData(['body' => $body, 'isWebServiceUser' => false]);
         if (!empty($control['errors'])) {
             return ['errors' => $control['errors']];
-- 
GitLab