diff --git a/sql/develop.sql b/sql/develop.sql index 78f77e0bf345c15e07647c5120e4873f60735179..91dc00840e498c2aee6b608ad60b19bd906a1174 100755 --- a/sql/develop.sql +++ b/sql/develop.sql @@ -60,17 +60,14 @@ DROP TABLE IF EXISTS emails; CREATE TABLE emails ( id serial NOT NULL, -res_id INTEGER NOT NULL, user_id INTEGER NOT NULL, sender json DEFAULT '{}' NOT NULL, recipients json DEFAULT '[]' NOT NULL, -cc json DEFAULT '[]', -cci json DEFAULT '[]', +cc json DEFAULT '[]' NOT NULL, +cci json DEFAULT '[]' NOT NULL, object character varying(256) NOT NULL, body text, -document json DEFAULT '{}' NOT NULL, -attachments json DEFAULT '[]', -notes json DEFAULT '[]', +document json, is_html boolean NOT NULL DEFAULT TRUE, status character varying(16) NOT NULL, message_exchange_id text, diff --git a/sql/structure.sql b/sql/structure.sql index a23d8dd9f772a3b80e85243613d93cabbe55e26b..d6a7abdfd11acd13c457b4ef07df02e30327eb78 100755 --- a/sql/structure.sql +++ b/sql/structure.sql @@ -2164,17 +2164,14 @@ WITH (OIDS=FALSE); CREATE TABLE emails ( id serial NOT NULL, -res_id INTEGER NOT NULL, user_id INTEGER NOT NULL, sender json DEFAULT '{}' NOT NULL, recipients json DEFAULT '[]' NOT NULL, -cc json DEFAULT '[]', -cci json DEFAULT '[]', +cc json DEFAULT '[]' NOT NULL, +cci json DEFAULT '[]' NOT NULL, object character varying(256) NOT NULL, body text, -document json DEFAULT '{}' NOT NULL, -attachments json DEFAULT '[]', -notes json DEFAULT '[]', +document json, is_html boolean NOT NULL DEFAULT TRUE, status character varying(16) NOT NULL, message_exchange_id text, diff --git a/src/app/attachment/controllers/AttachmentController.php b/src/app/attachment/controllers/AttachmentController.php index f20e8df055b072f7dc3f8e0eff21f4796e80b917..c4095288cf69bba2d99fdd69fd7730aaa35b8c6d 100755 --- a/src/app/attachment/controllers/AttachmentController.php +++ b/src/app/attachment/controllers/AttachmentController.php @@ -28,6 +28,7 @@ use Slim\Http\Request; use Slim\Http\Response; use SrcCore\models\CoreConfigModel; use Resource\controllers\StoreController; +use SrcCore\models\ValidatorModel; use Template\controllers\TemplateController; use SrcCore\models\DatabaseModel; use Resource\models\ResModel; @@ -187,7 +188,7 @@ class AttachmentController $id = (empty($attachmentTodisplay['res_id']) ? $attachmentTodisplay['res_id_version'] : $attachmentTodisplay['res_id']); $isVersion = empty($attachmentTodisplay['res_id']); - $convertedAttachment = ConvertPdfController::getConvertedPdfById(['select' => ['docserver_id', 'path', 'filename'], 'resId' => $id, 'collId' => 'attachments_coll', 'isVersion' => $isVersion]); + $convertedAttachment = ConvertPdfController::getConvertedPdfById(['resId' => $id, 'collId' => 'attachments_coll', 'isVersion' => $isVersion]); if (empty($convertedAttachment['errors'])) { $attachmentTodisplay = $convertedAttachment; } @@ -304,6 +305,56 @@ class AttachmentController return $response->withHeader('Content-Type', $mimeType); } + public static function getEncodedDocument(array $aArgs) + { + ValidatorModel::notEmpty($aArgs, ['id']); + ValidatorModel::intVal($aArgs, ['id']); + ValidatorModel::boolType($aArgs, ['original']); + ValidatorModel::boolType($aArgs, ['isVersion']); + + $document = AttachmentModel::getById(['select' => ['docserver_id', 'path', 'filename', 'title'], 'id' => $aArgs['id'], 'isVersion' => $aArgs['isVersion']]); + + if (empty($aArgs['original'])) { + $convertedDocument = ConvertPdfController::getConvertedPdfById(['resId' => $aArgs['id'], 'collId' => 'attachments_coll', 'isVersion' => $aArgs['isVersion']]); + + if (empty($convertedDocument['errors'])) { + $document['docserver_id'] = $convertedDocument['docserver_id']; + $document['path'] = $convertedDocument['path']; + $document['filename'] = $convertedDocument['filename']; + $document['fingerprint'] = $convertedDocument['fingerprint']; + } + } + + $docserver = DocserverModel::getByDocserverId(['docserverId' => $document['docserver_id'], 'select' => ['path_template', 'docserver_type_id']]); + if (empty($docserver['path_template']) || !file_exists($docserver['path_template'])) { + return ['errors' => 'Docserver does not exist']; + } + + $pathToDocument = $docserver['path_template'] . str_replace('#', DIRECTORY_SEPARATOR, $document['path']) . $document['filename']; + if (!file_exists($pathToDocument)) { + return ['errors' => 'Document not found on docserver']; + } + + $docserverType = DocserverTypeModel::getById(['id' => $docserver['docserver_type_id'], 'select' => ['fingerprint_mode']]); + $fingerprint = StoreController::getFingerPrint(['filePath' => $pathToDocument, 'mode' => $docserverType['fingerprint_mode']]); + if (!empty($document['fingerprint']) && $document['fingerprint'] != $fingerprint) { + ['errors' => 'Fingerprints do not match']; + } + + $fileContent = file_get_contents($pathToDocument); + if ($fileContent === false) { + return ['errors' => 'Document not found on docserver']; + } + + + $encodedDocument = base64_encode($fileContent); + + $pathInfo = pathinfo($pathToDocument); + $fileName = (empty($document['title']) ? 'document' : $document['title']) . ".{$pathInfo['extension']}"; + + return ['encodedDocument' => $encodedDocument, 'fileName' => $fileName]; + } + public function generateAttachForMailing(array $aArgs) { $attachments = AttachmentModel::getOnView([ diff --git a/src/app/attachment/models/AttachmentModelAbstract.php b/src/app/attachment/models/AttachmentModelAbstract.php index c2de4050136d5c215682e76a5d4509b951638e7f..839d9a3a859e558e6023a9c090cccb2ae5eafc8c 100755 --- a/src/app/attachment/models/AttachmentModelAbstract.php +++ b/src/app/attachment/models/AttachmentModelAbstract.php @@ -44,6 +44,7 @@ abstract class AttachmentModelAbstract ValidatorModel::notEmpty($aArgs, ['id']); ValidatorModel::intVal($aArgs, ['id']); ValidatorModel::boolType($aArgs, ['isVersion']); + ValidatorModel::arrayType($aArgs, ['select']); if (!empty($aArgs['isVersion'])) { $table = 'res_version_attachments'; diff --git a/src/app/configuration/controllers/ConfigurationController.php b/src/app/configuration/controllers/ConfigurationController.php index 518835e3b7e421f889638aa4f421080a1b386ed8..0aa1bfc0e123bbd40f71827b32b99fa0ec621170 100644 --- a/src/app/configuration/controllers/ConfigurationController.php +++ b/src/app/configuration/controllers/ConfigurationController.php @@ -61,11 +61,9 @@ class ConfigurationController private static function checkMailer(array $aArgs) { - $check = Validator::stringType()->notEmpty()->validate($aArgs['type']); - if (!$check) { - return ['errors' => "configuration mode is missing", 'code' => 400]; + if (!Validator::stringType()->notEmpty()->validate($aArgs['type'])) { + return ['errors' => 'configuration mode is missing', 'code' => 400]; } - if ($aArgs['type'] == 'smtp') { $check = Validator::stringType()->notEmpty()->validate($aArgs['host']); diff --git a/src/app/convert/controllers/ConvertPdfController.php b/src/app/convert/controllers/ConvertPdfController.php index b390c8f21bc8eacc66d262262f747b8351be2eeb..f8f55dfbcd7909c644c0c8c3fb3fa457d3c2bd1f 100755 --- a/src/app/convert/controllers/ConvertPdfController.php +++ b/src/app/convert/controllers/ConvertPdfController.php @@ -139,7 +139,6 @@ class ConvertPdfController ValidatorModel::notEmpty($aArgs, ['resId', 'collId']); ValidatorModel::intVal($aArgs, ['resId']); ValidatorModel::boolType($aArgs, ['isVersion']); - ValidatorModel::arrayType($aArgs, ['select']); $convertedDocument = AdrModel::getConvertedDocumentById([ 'select' => ['docserver_id','path', 'filename', 'fingerprint'], diff --git a/src/app/email/controllers/EmailController.php b/src/app/email/controllers/EmailController.php index b41a04b892bbef0b36f41c24e04351134f250a60..0e879918e2f8ce57705a410687119d7492506eea 100644 --- a/src/app/email/controllers/EmailController.php +++ b/src/app/email/controllers/EmailController.php @@ -14,6 +14,8 @@ namespace Email\controllers; +use Attachment\controllers\AttachmentController; +use Attachment\models\AttachmentModel; use Configuration\models\ConfigurationModel; use Email\models\EmailModel; use Entity\models\EntityModel; @@ -35,27 +37,48 @@ class EmailController } $data = $request->getParams(); - $check = Validator::intVal()->notEmpty()->validate($data['resId']); - $check = $check && Validator::arrayType()->notEmpty()->validate($data['sender']); + $check = Validator::arrayType()->notEmpty()->validate($data['sender']); $check = $check && Validator::stringType()->notEmpty()->validate($data['sender']['email']); $check = $check && Validator::arrayType()->notEmpty()->validate($data['recipients']); $check = $check && Validator::stringType()->notEmpty()->validate($data['object']); - $check = $check && Validator::arrayType()->notEmpty()->validate($data['document']); - $check = $check && Validator::boolType()->validate($data['document']['isLinked']); - $check = $check && Validator::boolType()->validate($data['document']['original']); $check = $check && Validator::boolType()->validate($data['isHtml']); + $check = $check && Validator::stringType()->notEmpty()->validate($data['status']); if (!$check) { return $response->withStatus(400)->withJson(['errors' => 'Bad Request']); } - if (!ResController::hasRightByResId(['resId' => $data['resId'], 'userId' => $GLOBALS['userId']])) { - return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']); + if (!empty($data['document'])) { + $check = Validator::intVal()->notEmpty()->validate($data['document']['id']); + $check = $check && Validator::boolType()->validate($data['document']['isLinked']); + $check = $check && Validator::boolType()->validate($data['document']['original']); + if (!$check) { + return $response->withStatus(400)->withJson(['errors' => 'Bad document data']); + } + if (!ResController::hasRightByResId(['resId' => $data['document']['id'], 'userId' => $GLOBALS['userId']])) { + return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']); + } + if (!empty($data['document']['attachments'])) { + if (!is_array($data['document']['attachments'])) { + return $response->withStatus(400)->withJson(['errors' => 'Document[attachments] is not an array']); + } + foreach ($data['document']['attachments'] as $attachment) { + $check = Validator::intVal()->notEmpty()->validate($attachment['id']); + $check = $check && Validator::boolType()->validate($attachment['isVersion']); + $check = $check && Validator::boolType()->validate($attachment['original']); + if (!$check) { + return $response->withStatus(400)->withJson(['errors' => 'Bad document[attachments] data']); + } + $checkAttachment = AttachmentModel::getById(['id' => $attachment['id'], 'isVersion' => $attachment['isVersion'], 'select' => ['res_id_master']]); + if (empty($checkAttachment) || $checkAttachment['res_id_master'] != $data['document']['id']) { + return $response->withStatus(400)->withJson(['errors' => 'Bad document[attachments][id]']); + } + } + } } $user = UserModel::getByLogin(['login' => $GLOBALS['userId'], 'select' => ['id']]); $id = EmailModel::create([ - 'resId' => $data['resId'], 'userId' => $user['id'], 'sender' => json_encode($data['sender']), 'recipients' => json_encode($data['recipients']), @@ -63,19 +86,20 @@ class EmailController 'cci' => empty($data['cci']) ? '[]' : json_encode($data['cci']), 'object' => $data['object'], 'body' => $data['body'], - 'document' => json_encode($data['document']), - 'attachments' => empty($data['attachments']) ? '[]' : json_encode($data['attachments']), - 'notes' => empty($data['notes']) ? '[]' : json_encode($data['notes']), + 'document' => empty($data['document']) ? null : json_encode($data['document']), 'isHtml' => $data['isHtml'] ? 'true' : 'false', - 'messageExchangeId' => $data['messageExchangeId'], + 'status' => $data['status'] == 'DRAFT' ? 'DRAFT' : 'WAITING', + 'messageExchangeId' => $data['messageExchangeId'] ]); - $isSent = EmailController::sendEmail(['emailId' => $id, 'userId' => $user['id']]); + if ($data['status'] != 'DRAFT') { + $isSent = EmailController::sendEmail(['emailId' => $id, 'userId' => $user['id']]); - if (!empty($isSent['success'])) { - EmailModel::update(['set' => ['status' => 'S'], 'where' => ['id = ?'], 'data' => [$id]]); - } else { - EmailModel::update(['set' => ['status' => 'E'], 'where' => ['id = ?'], 'data' => [$id]]); + if (!empty($isSent['success'])) { + EmailModel::update(['set' => ['status' => 'SENT', 'send_date' => 'CURRENT_TIMESTAMP'], 'where' => ['id = ?'], 'data' => [$id]]); + } else { + EmailModel::update(['set' => ['status' => 'ERROR'], 'where' => ['id = ?'], 'data' => [$id]]); + } } return $response->withJson(['success' => 'success']); @@ -138,17 +162,28 @@ class EmailController //TODO M2M - $email['document'] = (array)json_decode($email['document']); - if ($email['document']['isLinked']) { + if (!empty($email['document'])) { + $email['document'] = (array)json_decode($email['document']); + if ($email['document']['isLinked']) { - $encodedDocument = ResController::getEncodedDocument(['resId' => $email['res_id'], 'original' => $email['document']['original']]); - if (empty($encodedDocument['errors'])) { - $phpmailer->addStringAttachment(base64_decode($encodedDocument['encodedDocument']), $encodedDocument['fileName']); + $encodedDocument = ResController::getEncodedDocument(['resId' => $email['document']['id'], 'original' => $email['document']['original']]); + if (empty($encodedDocument['errors'])) { + $phpmailer->addStringAttachment(base64_decode($encodedDocument['encodedDocument']), $encodedDocument['fileName']); + } + } + if (!empty($email['document']['attachments'])) { + $email['document']['attachments'] = (array)$email['document']['attachments']; + foreach ($email['document']['attachments'] as $attachment) { + $attachment = (array)$attachment; + $encodedDocument = AttachmentController::getEncodedDocument(['id' => $attachment['id'], 'isVersion' => $attachment['isVersion'], 'original' => $attachment['original']]); + if (empty($encodedDocument['errors'])) { + $phpmailer->addStringAttachment(base64_decode($encodedDocument['encodedDocument']), $encodedDocument['fileName']); + } + } } + //TODO NOTES } -// $phpmailer->addAttachment($resFile['file_path']); //TODO - $isSent = $phpmailer->send(); if (!$isSent) { diff --git a/src/app/email/models/EmailModel.php b/src/app/email/models/EmailModel.php index b0ea3ef48e840627912f742aa36a7a5132e4d342..6f91f7a1a32fcfa19ae0243c73577bd38e1297b8 100644 --- a/src/app/email/models/EmailModel.php +++ b/src/app/email/models/EmailModel.php @@ -41,9 +41,9 @@ class EmailModel public static function create(array $aArgs) { - ValidatorModel::notEmpty($aArgs, ['resId', 'userId', 'sender', 'recipients', 'cc', 'cci', 'object', 'attachments', 'notes', 'document', 'isHtml']); - ValidatorModel::intVal($aArgs, ['resId', 'userId']); - ValidatorModel::stringType($aArgs, ['sender', 'recipients', 'cc', 'cci', 'object', 'body', 'attachments', 'notes', 'messageExchangeId', 'document', 'isHtml']); + ValidatorModel::notEmpty($aArgs, ['userId', 'sender', 'recipients', 'cc', 'cci', 'object', 'isHtml', 'status']); + ValidatorModel::intVal($aArgs, ['userId']); + ValidatorModel::stringType($aArgs, ['sender', 'recipients', 'cc', 'cci', 'object', 'body', 'messageExchangeId', 'document', 'isHtml', 'status']); $nextSequenceId = DatabaseModel::getNextSequenceValue(['sequenceId' => 'emails_id_seq']); @@ -51,7 +51,6 @@ class EmailModel 'table' => 'emails', 'columnsValues' => [ 'id' => $nextSequenceId, - 'res_id' => $aArgs['resId'], 'user_id' => $aArgs['userId'], 'sender' => $aArgs['sender'], 'recipients' => $aArgs['recipients'], @@ -60,10 +59,8 @@ class EmailModel 'object' => $aArgs['object'], 'body' => empty($aArgs['body']) ? null : $aArgs['body'], 'document' => $aArgs['document'], - 'attachments' => $aArgs['attachments'], - 'notes' => $aArgs['notes'], 'is_html' => $aArgs['isHtml'], - 'status' => 'W', + 'status' => $aArgs['status'], 'message_exchange_id' => empty($aArgs['messageExchangeId']) ? null : $aArgs['messageExchangeId'], 'creation_date' => 'CURRENT_TIMESTAMP' ] diff --git a/src/app/resource/controllers/ResController.php b/src/app/resource/controllers/ResController.php index d18c09e6d0d641048585fe130b6838a12bff3c08..ae18c8f34e95851e625e3e88d55f36c524494ede 100755 --- a/src/app/resource/controllers/ResController.php +++ b/src/app/resource/controllers/ResController.php @@ -525,7 +525,7 @@ class ResController } else { $collId = "attachments_coll"; } - $convertedDocument = ConvertPdfController::getConvertedPdfById(['select' => ['docserver_id', 'path', 'filename'], 'resId' => $id, 'collId' => $collId, 'isVersion' => $isVersion]); + $convertedDocument = ConvertPdfController::getConvertedPdfById(['resId' => $id, 'collId' => $collId, 'isVersion' => $isVersion]); if (empty($convertedDocument['errors'])) { $attachmentTodisplay = $convertedDocument; } @@ -535,7 +535,7 @@ class ResController $document['fingerprint'] = $attachmentTodisplay['fingerprint']; } } else { - $convertedDocument = ConvertPdfController::getConvertedPdfById(['select' => ['docserver_id', 'path', 'filename'], 'resId' => $aArgs['resId'], 'collId' => 'letterbox_coll', 'isVersion' => false]); + $convertedDocument = ConvertPdfController::getConvertedPdfById(['resId' => $aArgs['resId'], 'collId' => 'letterbox_coll', 'isVersion' => false]); if (empty($convertedDocument['errors'])) { $document['docserver_id'] = $convertedDocument['docserver_id'];