From 39049f9b839ad704003f3495204ca35ef61c4ee6 Mon Sep 17 00:00:00 2001
From: Damien <damien.burel@maarch.org>
Date: Mon, 27 Apr 2020 15:30:05 +0200
Subject: [PATCH] FEAT #13664 TIME 1:00 Fix action + replace invalid chars

---
 .../action/controllers/ActionMethodController.php | 15 +++++++++++++++
 .../alfresco/controllers/AlfrescoController.php   |  4 +++-
 src/core/models/CurlModel.php                     | 10 +++++++---
 3 files changed, 25 insertions(+), 4 deletions(-)

diff --git a/src/app/action/controllers/ActionMethodController.php b/src/app/action/controllers/ActionMethodController.php
index 3a220630f0d..a703cf6f122 100644
--- a/src/app/action/controllers/ActionMethodController.php
+++ b/src/app/action/controllers/ActionMethodController.php
@@ -16,6 +16,7 @@ use AcknowledgementReceipt\models\AcknowledgementReceiptModel;
 use Action\models\ActionModel;
 use Action\models\BasketPersistenceModel;
 use Action\models\ResMarkAsReadModel;
+use Alfresco\controllers\AlfrescoController;
 use Attachment\controllers\AttachmentController;
 use Attachment\models\AttachmentModel;
 use Convert\controllers\ConvertPdfController;
@@ -78,6 +79,7 @@ class ActionMethodController
         'giveOpinionParallelAction'                 => 'giveOpinionParallel',
         'validateParallelOpinionDiffusionAction'    => 'validateParallelOpinionDiffusion',
         'reconcileAction'                           => 'reconcile',
+        'sendAlfrescoAction'                        => 'sendResourceAlfresco',
         'noConfirmAction'                           => null
     ];
 
@@ -883,6 +885,19 @@ class ActionMethodController
         return true;
     }
 
+    public static function sendResourceAlfresco(array $args)
+    {
+        ValidatorModel::notEmpty($args, ['resId']);
+        ValidatorModel::intVal($args, ['resId']);
+
+        $sent = AlfrescoController::sendResource(['resId' => $args['resId'], 'userId' => $GLOBALS['id'], 'folderId' => $args['data']['folderId'], 'folderName' => $args['data']['folderName']]);
+        if (!empty($sent['errors'])) {
+            return ['errors' => [$sent['errors']]];
+        }
+
+        return ['history' => $sent['history']];
+    }
+
     public static function reconcile(array $args)
     {
         ValidatorModel::notEmpty($args, ['resId', 'data']);
diff --git a/src/app/external/alfresco/controllers/AlfrescoController.php b/src/app/external/alfresco/controllers/AlfrescoController.php
index 84cefee831e..f8cbf9026eb 100644
--- a/src/app/external/alfresco/controllers/AlfrescoController.php
+++ b/src/app/external/alfresco/controllers/AlfrescoController.php
@@ -409,7 +409,7 @@ class AlfrescoController
         $search = addslashes($queryParams['search']);
         $body = [
             'query' => [
-                'query'     => "select * from cmis:folder where CONTAINS ('cmis:name:*{$search}*')",
+                'query'     => "select * from cmis:folder where CONTAINS ('cmis:name:*{$search}*') and IN_TREE('{$entityInformations['alfresco']['nodeId']}')",
                 'language'  => 'cmis',
             ],
             'fields' => ['id', 'name']
@@ -462,6 +462,7 @@ class AlfrescoController
         if (empty($entity)) {
             return ['errors' => 'User has no primary entity'];
         }
+        $entityInformations = json_decode($entity['external_id'], true);
         if (empty($entityInformations['alfresco'])) {
             return ['errors' => 'User primary entity has not enough alfresco informations'];
         }
@@ -506,6 +507,7 @@ class AlfrescoController
         }
         $resourceFolderId = $curlResponse['response']['entry']['id'];
 
+        $document['subject'] = str_replace([':', '*', '\'', '"', '>', '<'], ' ', $document['subject']);
         $multipartBody = [
             'filedata' => ['isFile' => true, 'filename' => $document['subject'], 'content' => $fileContent],
         ];
diff --git a/src/core/models/CurlModel.php b/src/core/models/CurlModel.php
index 253ba6c0b58..1b69eb95096 100755
--- a/src/core/models/CurlModel.php
+++ b/src/core/models/CurlModel.php
@@ -284,9 +284,13 @@ class CurlModel
         $postData = '';
         foreach ($args['body'] as $key => $value) {
             $postData .= "--{$delimiter}\r\n";
-            $postData .= "Content-Disposition: form-data; name=\"{$key}\"\r\n";
-            $postData .= "\r\n";
-            $postData .= "{$value}\r\n";
+            if (is_array($value) && $value['isFile']) {
+                $postData .= "Content-Disposition: form-data; name=\"{$key}\"; filename=\"{$value['filename']}\"\r\n";
+                $postData .= "\r\n{$value['content']}\r\n";
+            } else {
+                $postData .= "Content-Disposition: form-data; name=\"{$key}\"\r\n";
+                $postData .= "\r\n{$value}\r\n";
+            }
         }
         $postData .= "--{$delimiter}--\r\n";
 
-- 
GitLab