Newer
Older
<?php
/*
*
* Copyright 2013 Maarch
*
* This file is part of Maarch Framework.
*
* Maarch Framework is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Maarch Framework is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Maarch Framework. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @brief Script to return ajax result
*
* @file sendmail_ajax_content.php
* @author Yves Christian Kpakpo <dev@maarch.org>
* @date $date$
* @version $Revision$
* @ingroup sendmail
*/
require_once "core".DIRECTORY_SEPARATOR."class".DIRECTORY_SEPARATOR."class_request.php";
require_once "core".DIRECTORY_SEPARATOR."class".DIRECTORY_SEPARATOR."class_db_pdo.php";
require_once "core".DIRECTORY_SEPARATOR."class".DIRECTORY_SEPARATOR."class_security.php";
require_once 'apps' . DIRECTORY_SEPARATOR . $_SESSION['config']['app_id']
. DIRECTORY_SEPARATOR . 'class' . DIRECTORY_SEPARATOR
. 'class_indexing_searching_app.php';
require_once 'apps' . DIRECTORY_SEPARATOR . $_SESSION['config']['app_id']
. DIRECTORY_SEPARATOR . 'class' . DIRECTORY_SEPARATOR
. 'class_users.php';
require_once 'modules/notifications/notifications_tables_definition.php';
require_once "modules" . DIRECTORY_SEPARATOR . "sendmail" . DIRECTORY_SEPARATOR
. "class" . DIRECTORY_SEPARATOR . "class_modules_tools.php";
require_once 'modules/sendmail/Controllers/SendMessageExchangeController.php';
$core_tools = new core_tools();
$request = new request();
$db = new Database();
$sec = new security();
$is = new indexing_searching_app();
$users_tools = new class_users();
$sendmail_tools = new sendmail();
if ($_SESSION['features']['send_to_contact_with_mandatory_attachment'] == true && !isset($_REQUEST['join_attachment']) && $_REQUEST['action'] == 'send_to_contact_with_mandatory_attachment') {
$error = $request->wash_html(_PLEASE_CHOOSE_AN_ATTACHMENT, 'NONE');
$status = 1;
echo "{status : " . $status . ", content : '" . addslashes(_parse($content)) . "', error : '" . addslashes(_parse_error($error)) . "', exec_js : '".addslashes($js)."'}";
$text = str_replace("\r\n", PHP_EOL, $text);
$text = str_replace("\r", PHP_EOL, $text);
$text = str_replace(PHP_EOL, "\\n ", $text);
$text = str_replace("###", "\\n ", $text);
return $text;
}
$core_tools->load_lang();
$status = 0;
$error = $content = $js = $parameters = '';
$labels_array = array();
if (isset($_REQUEST['mode']) && !empty($_REQUEST['mode'])) {
$mode = $_REQUEST['mode'];
} else {
$error = _ERROR_IN_SENDMAIL_FORM_GENERATION;
$status = 1;
}
//Identifier of the element wich is noted
$identifier = '';
if (isset($_REQUEST['identifier']) && ! empty($_REQUEST['identifier'])) {
$identifier = trim($_REQUEST['identifier']);
}
//formContent of the element wich is noted
$formContent = '';
if (isset($_GET['formContent']) && ! empty($_GET['formContent'])) {
$formContent = trim($_GET['formContent']);
}
//Collection
if (isset($_REQUEST['coll_id']) && ! empty($_REQUEST['coll_id'])) {
$collId = trim($_REQUEST['coll_id']);
$parameters .= '&coll_id='.$_REQUEST['coll_id'];
$view = $sec->retrieve_view_from_coll_id($collId);
$table = $sec->retrieve_table_from_coll($collId);
}
//Keep some origin parameters
if (isset($_REQUEST['size']) && !empty($_REQUEST['size'])) {
$parameters .= '&size='.$_REQUEST['size'];
}
if (isset($_REQUEST['order']) && !empty($_REQUEST['order'])) {
$parameters .= '&order='.$_REQUEST['order'];
if (isset($_REQUEST['order_field']) && !empty($_REQUEST['order_field'])) {
$parameters .= '&order_field='.$_REQUEST['order_field'];
}
}
if (isset($_REQUEST['what']) && !empty($_REQUEST['what'])) {
$parameters .= '&what='.$_REQUEST['what'];
}
if (isset($_REQUEST['template']) && !empty($_REQUEST['template'])) {
$parameters .= '&template='.$_REQUEST['template'];
}
if (isset($_REQUEST['start']) && !empty($_REQUEST['start'])) {
$parameters .= '&start='.$_REQUEST['start'];
}
//Keep the origin to reload the origin list
$list_origin = $origin = '';
if (isset($_REQUEST['origin']) && !empty($_REQUEST['origin'])) {
//
$origin = $_REQUEST['origin'];
if ($_REQUEST['origin'] == "document") {
//From document
$list_origin = "window.parent.loadList('".$_SESSION['config']['businessappurl']
."index.php?display=true&module=sendmail&page=sendmail&identifier="
.$identifier."&origin=document".$parameters."', 'divList', true);";
}
}
//Path to actual script
$path_to_script = $_SESSION['config']['businessappurl']
."index.php?display=true&module=sendmail&page=sendmail_ajax_content&identifier="
.$identifier."&origin=".$origin.$parameters;
switch ($mode) {
case 'up':
if (isset($_REQUEST['id']) && !empty($_REQUEST['id'])) {
$parameters .= '&id='.$_REQUEST['id'];
} else {
$error = $request->wash_html(_ID.' '._IS_EMPTY.'!', 'NONE');
$js = "window.parent.destroyModal('form_email');";
$error = $request->wash_html(_IDENTIFIER.' '._IS_EMPTY.'!', 'NONE');
$js = "window.parent.destroyModal('form_email');";
//Reset arry of adresses
unset($_SESSION['adresses']);
$_SESSION['adresses'] = array();
//Show iframe
$content .='<iframe name="form_mail" id="form_mail" src="'
. $_SESSION['config']['businessappurl']
. 'index.php?display=true&module=sendmail&page=mail_form&identifier='
. $identifier.'&origin=document&coll_id='.$collId.'&mode='.$mode.$parameters.'&formContent='.$_GET['formContent'].'" '
. 'frameborder="0" width="100%" style="height:540px;padding:0px;overflow-x:hidden;overflow-y: auto;"></iframe>';
if ($formContent == 'messageExchange') {
$return = SendMessageExchangeController::createMessageExchange($_REQUEST);
if (is_array($return['errors'])) {
$error = implode(", ", $return['errors']);
} else {
$error = $return['errors'];
}
$status = 1;
}
//Reload and show message
$js = $list_origin."window.parent.top.$('main_info').innerHTML = '"._EMAIL_ADDED."';";
$userEntitiesMails = array();
if ($core_tools->test_service('use_mail_services', 'sendmail', false)) {
$userEntitiesMails = $sendmail_tools->checkAttachedEntitiesMails($_SESSION['user']['UserId']);
}
if (empty($identifier)) {
$error = $request->wash_html(_IDENTIFIER.' '._IS_EMPTY.'!', 'NONE');
} elseif (!in_array($_REQUEST['sender_email'], array_keys($userEntitiesMails)) && $core_tools->test_service('use_mail_services', 'sendmail', false)) {
$error = $request->wash_html(_INCORRECT_SENDER, 'NONE');
if (isset($_SESSION['adresses']['to']) && count($_SESSION['adresses']['to']) > 0) {
if (!empty($_REQUEST['object'])) {
//Check adress for to
$to = join(',', $_SESSION['adresses']['to']);
$error = $sendmail_tools->CheckEmailAdress($to);
(isset($_SESSION['adresses']['cc']) && count($_SESSION['adresses']['cc']) > 0)?
$cc = join(',', $_SESSION['adresses']['cc']) : $cc = '';
$error = $sendmail_tools->CheckEmailAdress($cc);
(isset($_SESSION['adresses']['cci']) && count($_SESSION['adresses']['cci']) > 0)?
$cci = join(',', $_SESSION['adresses']['cci']) : $cci = '';
$error = $sendmail_tools->CheckEmailAdress($cci);
)? $res_master_attached = true : $res_master_attached = false;
$document = ['id' => $identifier, 'isLinked' => $res_master_attached, 'original' => false];
$attachments = [];
//attachment
if (isset($_REQUEST['join_attachment']) && count($_REQUEST['join_attachment']) > 0) {
foreach ($_REQUEST['join_attachment'] as $rawAttachment) {
$id = $rawAttachment;
$original = true;
if (strpos($rawAttachment, '#') !== false) {
$id = substr($rawAttachment, 0, strpos($rawAttachment, '#'));
$original = false;
}
$attachments[] = [
'id' => (int)$id,
'isVersion' => false,
'original' => $original,
];
}
//Version attachment
if (isset($_REQUEST['join_version_attachment']) && count($_REQUEST['join_version_attachment']) > 0) {
foreach ($_REQUEST['join_version_attachment'] as $rawAttachment) {
$id = $rawAttachment;
$original = true;
if (strpos($rawAttachment, '#') !== false) {
$id = substr($rawAttachment, 0, strpos($rawAttachment, '#'));
$original = false;
}
$attachments[] = [
'id' => (int)$id,
'isVersion' => true,
'original' => $original,
];
}
if (!empty($attachments)) {
$document['attachments'] = $attachments;
if (isset($_REQUEST['notes']) && count($_REQUEST['notes']) > 0) {
$document['notes'] = [];
foreach ($_REQUEST['notes'] as $note) {
$document['notes'][] = (int)$note;
}
if (!empty($_REQUEST['is_html']) && $_REQUEST['is_html'] == 'Y') {
$isHtml = true;
$body = $_REQUEST['body_from_html'];
}
//Status
if ($_REQUEST['for'] == 'save') {
} elseif ($_REQUEST['for'] == 'send') {
$userInfo = \User\models\UserModel::getByLogin(['login' => $_SESSION['user']['UserId'], 'select' => ['id', 'mail']]);
$aSenderInfo = explode(",", $_REQUEST['sender_email']);

Florian Azizian
committed
if (!empty($aSenderInfo[1]) && !empty($aSenderInfo[0])) {
$entityInfo = \Entity\models\EntityModel::getByEntityId(['entityId' => $aSenderInfo[0], 'select' => ['id']]);
}
$isSent = \Email\controllers\EmailController::createEmail([
'userId' => $userInfo['id'],
'data' => [
'sender' => empty($entityInfo) ? ['email' => $userInfo['mail']] : ['email' => $aSenderInfo[1], 'entityId' => $entityInfo['id']],
'recipients' => explode(",", $to),
'cc' => explode(",", $cc),
'cci' => explode(",", $cci),
'object' => (empty($_REQUEST['object']) ? '' : substr($_REQUEST['object'], 0, 100)),
'body' => $body,
'document' => $document,
'isHtml' => $isHtml,
'status' => $email_status
]
]);
if (!empty($isSent['errors'])) {
$error = $isSent['errors'];
//Reload and show message
$js = $list_origin."window.parent.top.$('main_info').innerHTML = '"._EMAIL_ADDED."';";
} else {
$status = 1;
}
} else {
$status = 1;
}
} else {
$status = 1;
}
} else {
$error = $request->wash_html(_EMAIL_OBJECT.' '._IS_EMPTY.'!', 'NONE');
$error = $request->wash_html(_SEND_TO.' '._IS_EMPTY.'!', 'NONE');
$status = 1;
}
}
}
break;
case 'updated':
if (isset($_REQUEST['id']) && !empty($_REQUEST['id'])) {
//Email ID
$id = $_REQUEST['id'];
$userEntitiesMails = array();
if ($core_tools->test_service('use_mail_services', 'sendmail', false)) {
$userEntitiesMails = $sendmail_tools->checkAttachedEntitiesMails($_SESSION['user']['UserId']);
}
$error = $request->wash_html(_IDENTIFIER.' '._IS_EMPTY.'!', 'NONE');
} elseif (!in_array($_REQUEST['sender_email'], array_keys($userEntitiesMails)) && $core_tools->test_service('use_mail_services', 'sendmail', false)) {
$error = $request->wash_html(_INCORRECT_SENDER, 'NONE');
if ((isset($_SESSION['adresses']['to']) && count($_SESSION['adresses']['to']) > 0) || $_REQUEST['for'] == 'save') {
if (!empty($_REQUEST['object']) || $_REQUEST['for'] == 'save') {
if (!empty($_SESSION['adresses']['to'])) {
$to = join(',', $_SESSION['adresses']['to']);
$error = $sendmail_tools->CheckEmailAdress($to);
}
if (empty($error)) {
//Check adress for cc
(isset($_SESSION['adresses']['cc']) && count($_SESSION['adresses']['cc']) > 0)?
$cc = join(',', $_SESSION['adresses']['cc']) : $cc = '';
$error = $sendmail_tools->CheckEmailAdress($cc);
if (empty($error)) {
//Check adress for cci
(isset($_SESSION['adresses']['cci']) && count($_SESSION['adresses']['cci']) > 0)?
$cci = join(',', $_SESSION['adresses']['cci']) : $cci = '';
$error = $sendmail_tools->CheckEmailAdress($cci);
)? $res_master_attached = true : $res_master_attached = false;
$document = ['id' => $identifier, 'isLinked' => $res_master_attached, 'original' => false];
$attachments = [];
//attachment
if (isset($_REQUEST['join_attachment']) && count($_REQUEST['join_attachment']) > 0) {
foreach ($_REQUEST['join_attachment'] as $rawAttachment) {
$id = $rawAttachment;
$original = true;
if (strpos($rawAttachment, '#') !== false) {
$id = substr($rawAttachment, 0, strpos($rawAttachment, '#'));
$original = false;
}
$attachments[] = [
'id' => (int)$id,
'isVersion' => false,
'original' => $original,
];
}
if (isset($_REQUEST['join_version_attachment']) && count($_REQUEST['join_version_attachment']) > 0) {
foreach ($_REQUEST['join_version_attachment'] as $rawAttachment) {
$id = $rawAttachment;
$original = true;
if (strpos($rawAttachment, '#') !== false) {
$id = substr($rawAttachment, 0, strpos($rawAttachment, '#'));
$original = false;
}
$attachments[] = [
'id' => (int)$id,
'isVersion' => true,
'original' => $original,
];
}
if (!empty($attachments)) {
$document['attachments'] = $attachments;
}
//Notes
if (isset($_REQUEST['notes']) && count($_REQUEST['notes']) > 0) {
$document['notes'] = [];
foreach ($_REQUEST['notes'] as $note) {
$document['notes'][] = (int)$note;
}
if (!empty($_REQUEST['is_html']) && $_REQUEST['is_html'] == 'Y') {
$isHtml = true;
$body = $_REQUEST['body_from_html'];
//Status
if ($_REQUEST['for'] == 'save') {
} elseif ($_REQUEST['for'] == 'send') {
$userInfo = \User\models\UserModel::getByLogin(['login' => $_SESSION['user']['UserId'], 'select' => ['id', 'mail']]);
$aSenderInfo = explode(",", $_REQUEST['sender_email']);
if (!empty($aSenderInfo[1])) {
$entityInfo = \Entity\models\EntityModel::getByEntityId(['entityId' => $aSenderInfo[0], 'select' => ['id']]);
}
if (!empty($isSent['errors'])) {
$error = $isSent['errors'];
$js = $list_origin."window.parent.top.$('main_info').innerHTML = '"._EMAIL_UPDATED."';";
} else {
$status = 1;
}
} else {
$status = 1;
}
} else {
$status = 1;
}
} else {
$error = $request->wash_html(_EMAIL_OBJECT.' '._IS_EMPTY.'!', 'NONE');
$error = $request->wash_html(_SEND_TO.' '._IS_EMPTY.'!', 'NONE');
$error = $request->wash_html(_ID.' '._IS_EMPTY.'!', 'NONE');
$js = "window.parent.destroyModal('form_email');";
}
break;
case 'del':
if (isset($_REQUEST['id']) && !empty($_REQUEST['id'])) {
$id = $_REQUEST['id'];
$environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'DELETE']);
$request = \Slim\Http\Request::createFromEnvironment($environment);
\Email\controllers\EmailController::delete($request, new \Slim\Http\Response(), ['id' => $id]);
$js = $list_origin."window.parent.top.$('main_info').innerHTML = '"._EMAIL_REMOVED."';";
$error = $request->wash_html(_ID.' '._IS_EMPTY.'!', 'NONE');
$js = "window.parent.destroyModal('form_email');";
case 'adress':
if (isset($_REQUEST['for']) && isset($_REQUEST['field']) && isset($_REQUEST['email'])) {
if (isset($_REQUEST['email']) && !empty($_REQUEST['email'])) {
//Clean up email
$email = trim($_REQUEST['email']);
//Reset session adresses if necessary
if (!isset($_SESSION['adresses'][$_REQUEST['field']])) {
$_SESSION['adresses'][$_REQUEST['field']] = array();
}
//For ADD
if ($_REQUEST['for'] == 'add') {
array_push($_SESSION['adresses'][$_REQUEST['field']], $email);
//For DEL
} elseif ($_REQUEST['for'] == 'del') {
//unset adress in array
unset($_SESSION['adresses'][$_REQUEST['field']][$_REQUEST['index']]);
//If no adresse for field, unset the entire sub-array
if (count($_SESSION['adresses'][$_REQUEST['field']]) == 0) {
unset($_SESSION['adresses'][$_REQUEST['field']]);
}
//Get content
$content = $sendmail_tools->updateAdressInputField($path_to_script, $_SESSION['adresses'], $_REQUEST['field']);
} else {
$error = $request->wash_html(_EMAIL.' '._IS_EMPTY.'!', 'NONE');
$error = $request->wash_html(_UNKNOW_ERROR.'!', 'NONE');
if (isset($_REQUEST['for']) && isset($_REQUEST['field']) && isset($_REQUEST['contactAddress'])) {
if (isset($_REQUEST['contactAddress']) && !empty($_REQUEST['contactAddress'])) {
$contactAddress = trim($_REQUEST['contactAddress']);
if (!isset($_SESSION['adresses'][$_REQUEST['field']])) {
$_SESSION['adresses'][$_REQUEST['field']] = array();
}
if ($_REQUEST['for'] == 'add') {
$contactLabel = \Contact\models\ContactModel::getContactFullLabel(['addressId' => $contactAddress]);
$contactInfo = \Contact\models\ContactModel::getFullAddressById(['addressId' => $contactAddress]);
$contactCommunication = \Contact\models\ContactModel::getContactCommunication(['contactId' => $contactInfo[0]['contact_id']]);
$_SESSION['adresses'][$_REQUEST['field']][$contactAddress] = $contactLabel.'. ('._COMMUNICATION_TYPE.' : '.$contactCommunication['value'].'))';
} elseif ($_REQUEST['for'] == 'del') {
unset($_SESSION['adresses'][$_REQUEST['field']][$_REQUEST['index']]);
//If no adresse for field, unset the entire sub-array
if (count($_SESSION['adresses'][$_REQUEST['field']]) == 0) {
unset($_SESSION['adresses'][$_REQUEST['field']]);
}
$content = $sendmail_tools->updateContactInputField($path_to_script, $_SESSION['adresses'], $_REQUEST['field']);
} else {
$error = $request->wash_html(_EMAIL.' '._IS_EMPTY.'!', 'NONE');
$status = 1;
}
$error = $request->wash_html(_UNKNOW_ERROR.'!', 'NONE');
$status = 1;
}
break;
case 'download':
require_once 'modules/export_seda/RequestSeda.php';
$RequestSeda = new RequestSeda();
$messageExchangeData = $RequestSeda->getMessageByIdentifierAndResId(['message_id' => $_GET['id'], 'res_id_master' => $_GET['identifier']]);
$docserver = \Docserver\models\DocserverModel::getByDocserverId(['docserverId' => $messageExchangeData->docserver_id]);
$docserverType = \Docserver\models\DocserverTypeModel::getById(['id' => $docserver['docserver_type_id']]);
$pathDirectory = str_replace('#', DIRECTORY_SEPARATOR, $messageExchangeData->path);
$filePath = $docserver['path_template'] . $pathDirectory . $messageExchangeData->filename;
$fingerprint = \Resource\controllers\StoreController::getFingerPrint([
'mode' => $docserverType['fingerprint_mode'],
if ($fingerprint != $messageExchangeData->fingerprint) {
echo _PB_WITH_FINGERPRINT_OF_DOCUMENT;
exit;
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: public');
header('Content-Description: File Transfer');
header('Content-Type: ' . strtolower(mime_content_type($filePath)));
header("Content-length: " . filesize($filePath));
header('Content-Disposition: attachment; filename=' . basename($messageExchangeData->reference.'.zip') . ';');
header("Content-Type: application/force-download");
header('Content-Transfer-Encoding: binary');
readfile($filePath);
exit();
}
exit();
}
echo "{status : " . $status . ", content : '" . addslashes(_parse($content)) . "', error : '" . addslashes(_parse_error($error)) . "', exec_js : '".addslashes($js)."'}";