new_message.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.messages
  5. */
  6. /**
  7. * This script shows a compose area (wysiwyg editor if supported, otherwise
  8. * a simple textarea) where the user can type a message.
  9. * There are three modes
  10. * - standard: type a message, select a user to send it to, press send
  11. * - reply on message (when pressing reply when viewing a message)
  12. * - send to specific user (when pressing send message in the who is online list).
  13. */
  14. $cidReset = true;
  15. require_once __DIR__.'/../inc/global.inc.php';
  16. api_block_anonymous_users();
  17. if (api_get_setting('allow_message_tool') !== 'true') {
  18. api_not_allowed(true);
  19. }
  20. $logInfo = [
  21. 'tool' => 'Messages',
  22. 'action' => 'new_message',
  23. 'action_details' => isset($_GET['re_id']) ? 're_id' : '',
  24. ];
  25. Event::registerLog($logInfo);
  26. $allowSocial = api_get_setting('allow_social_tool') === 'true';
  27. $nameTools = api_xml_http_response_encode(get_lang('Messages'));
  28. $htmlHeadXtra[] = '<script>
  29. var counter_image = 1;
  30. function add_image_form() {
  31. // Multiple filepaths for image form
  32. var filepaths = document.getElementById("file_uploads");
  33. if (document.getElementById("filepath_"+counter_image)) {
  34. counter_image = counter_image + 1;
  35. } else {
  36. counter_image = counter_image;
  37. }
  38. var elem1 = document.createElement("div");
  39. elem1.setAttribute("id","filepath_"+counter_image);
  40. filepaths.appendChild(elem1);
  41. id_elem1 = "filepath_"+counter_image;
  42. id_elem1 = "\'"+id_elem1+"\'";
  43. document.getElementById("filepath_"+counter_image).innerHTML = "<div class=\"form-group\" ><label class=\"col-sm-4\">'.get_lang('FilesAttachment').'</label><input class=\"col-sm-8\" type=\"file\" name=\"attach_"+counter_image+"\" /></div><div class=\"form-group\" ><label class=\"col-sm-4\">'.get_lang('Description').'</label><div class=\"col-sm-8\"><input style=\"width:100%\" type=\"text\" name=\"legend[]\" /></div></div>";
  44. if (filepaths.childNodes.length == 6) {
  45. var link_attach = document.getElementById("link-more-attach");
  46. if (link_attach) {
  47. link_attach.innerHTML="";
  48. }
  49. }
  50. }
  51. </script>';
  52. $nameTools = get_lang('ComposeMessage');
  53. $tpl = new Template(get_lang('ComposeMessage'));
  54. /**
  55. * Shows the compose area + a list of users to select from.
  56. */
  57. function show_compose_to_any($tpl)
  58. {
  59. $default['user_list'] = 0;
  60. $html = manageForm($default, null, null, $tpl);
  61. return $html;
  62. }
  63. function show_compose_reply_to_message($message_id, $receiver_id, $tpl)
  64. {
  65. $table = Database::get_main_table(TABLE_MESSAGE);
  66. $receiver_id = (int) $receiver_id;
  67. $message_id = (int) $message_id;
  68. $query = "SELECT user_sender_id
  69. FROM $table
  70. WHERE user_receiver_id = ".$receiver_id." AND id = ".$message_id;
  71. $result = Database::query($query);
  72. $row = Database::fetch_array($result, 'ASSOC');
  73. $userInfo = api_get_user_info($row['user_sender_id']);
  74. if (empty($row['user_sender_id']) || empty($userInfo)) {
  75. $html = get_lang('InvalidMessageId');
  76. return $html;
  77. }
  78. $default['users'] = [$row['user_sender_id']];
  79. $html = manageForm($default, null, $userInfo['complete_name_with_username'], $tpl);
  80. return $html;
  81. }
  82. function show_compose_to_user($receiver_id, $tpl)
  83. {
  84. $userInfo = api_get_user_info($receiver_id);
  85. $html = get_lang('To').':&nbsp;<strong>'.$userInfo['complete_name'].'</strong>';
  86. $default['title'] = api_xml_http_response_encode(get_lang('EnterTitle'));
  87. $default['users'] = [$receiver_id];
  88. $html .= manageForm($default, null, '', $tpl);
  89. return $html;
  90. }
  91. /**
  92. * @param $default
  93. * @param null $select_from_user_list
  94. * @param string $sent_to
  95. * @param Template $tpl
  96. *
  97. * @return string
  98. */
  99. function manageForm($default, $select_from_user_list = null, $sent_to = '', $tpl = null)
  100. {
  101. $group_id = isset($_REQUEST['group_id']) ? (int) $_REQUEST['group_id'] : null;
  102. $message_id = isset($_GET['message_id']) ? (int) $_GET['message_id'] : null;
  103. $form = new FormValidator(
  104. 'compose_message',
  105. null,
  106. api_get_self(),
  107. null,
  108. ['enctype' => 'multipart/form-data']
  109. );
  110. if (empty($group_id)) {
  111. if (isset($select_from_user_list)) {
  112. $form->addText(
  113. 'id_text_name',
  114. get_lang('SendMessageTo'),
  115. true,
  116. [
  117. 'id' => 'id_text_name',
  118. 'onkeyup' => 'send_request_and_search()',
  119. 'autocomplete' => 'off',
  120. ]
  121. );
  122. $form->addRule('id_text_name', get_lang('ThisFieldIsRequired'), 'required');
  123. $form->addElement('html', '<div id="id_div_search" style="padding:0px" class="message-select-box" >&nbsp;</div>');
  124. $form->addElement('hidden', 'user_list', 0, ['id' => 'user_list']);
  125. } else {
  126. if (!empty($sent_to)) {
  127. $form->addLabel(get_lang('SendMessageTo'), $sent_to);
  128. }
  129. if (empty($default['users'])) {
  130. //fb select
  131. $form->addElement(
  132. 'select_ajax',
  133. 'users',
  134. get_lang('SendMessageTo'),
  135. [],
  136. [
  137. 'multiple' => 'multiple',
  138. 'url' => api_get_path(WEB_AJAX_PATH).'message.ajax.php?a=find_users',
  139. ]
  140. );
  141. } else {
  142. $form->addElement('hidden', 'hidden_user', $default['users'][0], ['id' => 'hidden_user']);
  143. }
  144. }
  145. } else {
  146. $userGroup = new UserGroup();
  147. $group_info = $userGroup->get($group_id);
  148. $form->addElement('label', get_lang('ToGroup'), api_xml_http_response_encode($group_info['name']));
  149. $form->addElement('hidden', 'group_id', $group_id);
  150. $form->addElement('hidden', 'parent_id', $message_id);
  151. }
  152. $form->addText('title', get_lang('Subject'), true);
  153. $form->addHtmlEditor(
  154. 'content',
  155. get_lang('Message'),
  156. false,
  157. false,
  158. ['ToolbarSet' => 'Messages', 'Width' => '100%', 'Height' => '250', 'style' => true]
  159. );
  160. if (isset($_GET['re_id'])) {
  161. $message_reply_info = MessageManager::get_message_by_id($_GET['re_id']);
  162. $default['title'] = get_lang('MailSubjectReplyShort').' '.Security::remove_XSS($message_reply_info['title']);
  163. $form->addHidden('re_id', (int) $_GET['re_id']);
  164. $form->addHidden('save_form', 'save_form');
  165. // Adding reply mail
  166. $user_reply_info = api_get_user_info($message_reply_info['user_sender_id']);
  167. $default['content'] = '<link href="';
  168. $default['content'] .= api_get_cdn_path(api_get_path(WEB_CSS_PATH).'themes/'.api_get_visual_theme().'/document.css');
  169. $default['content'] .= '" media="screen" rel="stylesheet" type="text/css" />';
  170. $default['content'] .= '<p><br/></p>'.sprintf(
  171. get_lang('XWroteY'),
  172. $user_reply_info['complete_name'],
  173. Security::filter_terms($message_reply_info['content'])
  174. );
  175. }
  176. if (isset($_GET['forward_id'])) {
  177. $forwardId = (int) $_GET['forward_id'];
  178. $message_reply_info = MessageManager::get_message_by_id($forwardId);
  179. $attachments = MessageManager::getAttachmentLinkList($forwardId, MessageManager::MESSAGE_TYPE_INBOX);
  180. if (!empty($attachments)) {
  181. $fileListToString = !empty($attachments) ? implode('<br />', $attachments) : '';
  182. $form->addLabel('', $fileListToString);
  183. }
  184. $default['title'] = '['.get_lang('MailSubjectForwardShort').": ".Security::remove_XSS($message_reply_info['title']).']';
  185. $form->addHidden('forward_id', $forwardId);
  186. $form->addHidden('save_form', 'save_form');
  187. $receiverInfo = api_get_user_info($message_reply_info['user_receiver_id']);
  188. $forwardMessage = '---------- '.get_lang('ForwardedMessage').' ---------'.'<br />';
  189. $forwardMessage .= get_lang('Date').': '.api_get_local_time($message_reply_info['send_date']).'<br />';
  190. $forwardMessage .= get_lang('Subject').': '.Security::remove_XSS($message_reply_info['title']).'<br />';
  191. $forwardMessage .= get_lang('To').': '.$receiverInfo['complete_name'].' - '.$receiverInfo['email'].' <br />';
  192. $default['content'] = '<p><br/></p>'.$forwardMessage.'<br />'.Security::filter_terms($message_reply_info['content']);
  193. }
  194. if (empty($group_id)) {
  195. $form->addLabel(
  196. '',
  197. '<div id="file_uploads"><div id="filepath_1">
  198. <div id="filepaths" class="form-horizontal">
  199. <div id="paths-file" class="form-group">
  200. <label class="col-sm-4">'.get_lang('FilesAttachment').'</label>
  201. <input class="col-sm-8" type="file" name="attach_1"/>
  202. </div>
  203. </div>
  204. <div id="paths-description" class="form-group">
  205. <label class="col-sm-4">'.get_lang('Description').'</label>
  206. <div class="col-sm-8">
  207. <input id="file-descrtiption" class="form-control" type="text" name="legend[]" />
  208. </div>
  209. </div>
  210. </div>
  211. </div>'
  212. );
  213. $form->addLabel(
  214. '',
  215. '<span id="link-more-attach"><a class="btn btn-default" href="javascript://" onclick="return add_image_form()">'.
  216. get_lang('AddOneMoreFile').'</a></span>&nbsp;('.
  217. sprintf(
  218. get_lang('MaximunFileSizeX'),
  219. format_file_size(api_get_setting('message_max_upload_filesize'))
  220. ).')'
  221. );
  222. }
  223. $form->addLabel(
  224. '',
  225. '<iframe
  226. frameborder="0" height="200" width="100%" scrolling="no"
  227. src="'.api_get_path(WEB_CODE_PATH).'messages/record_audio.php"></iframe>'
  228. );
  229. $form->addButtonSend(get_lang('SendMessage'), 'compose');
  230. $form->setRequiredNote('<span class="form_required">*</span> <small>'.get_lang('ThisFieldIsRequired').'</small>');
  231. if (!empty($group_id) && !empty($message_id)) {
  232. $message_info = MessageManager::get_message_by_id($message_id);
  233. $default['title'] = get_lang('MailSubjectReplyShort')." ".$message_info['title'];
  234. }
  235. $form->setDefaults($default);
  236. $html = '';
  237. if ($form->validate()) {
  238. $check = Security::check_token('post');
  239. $disabled = api_get_configuration_value('disable_token_in_new_message');
  240. if ($disabled) {
  241. $check = true;
  242. }
  243. if ($check) {
  244. $user_list = $default['users'];
  245. $file_comments = $_POST['legend'];
  246. $title = $default['title'];
  247. $content = $default['content'];
  248. $group_id = isset($default['group_id']) ? $default['group_id'] : null;
  249. $parent_id = isset($default['parent_id']) ? $default['parent_id'] : null;
  250. $forwardId = isset($_POST['forward_id']) ? $_POST['forward_id'] : false;
  251. if (is_array($user_list) && count($user_list) > 0) {
  252. // All is well, send the message
  253. foreach ($user_list as $userId) {
  254. $res = MessageManager::send_message(
  255. $userId,
  256. $title,
  257. $content,
  258. $_FILES,
  259. $file_comments,
  260. $group_id,
  261. $parent_id,
  262. 0,
  263. 0,
  264. null,
  265. false,
  266. $forwardId,
  267. [],
  268. true
  269. );
  270. if ($res) {
  271. $userInfo = api_get_user_info($userId);
  272. Display::addFlash(Display::return_message(
  273. get_lang('MessageSentTo')."&nbsp;<b>".$userInfo['complete_name_with_username']."</b>",
  274. 'confirmation',
  275. false
  276. ));
  277. }
  278. }
  279. MessageManager::cleanAudioMessage();
  280. } else {
  281. Display::addFlash(Display::return_message('ErrorSendingMessage', 'error'));
  282. }
  283. }
  284. Security::clear_token();
  285. header('Location: '.api_get_path(WEB_CODE_PATH).'messages/inbox.php');
  286. exit;
  287. } else {
  288. $token = Security::get_token();
  289. $form->addElement('hidden', 'sec_token');
  290. $form->setConstants(['sec_token' => $token]);
  291. $html .= $form->returnForm();
  292. }
  293. return $html;
  294. }
  295. if ($allowSocial) {
  296. $this_section = SECTION_SOCIAL;
  297. $interbreadcrumb[] = [
  298. 'url' => api_get_path(WEB_CODE_PATH).'social/home.php',
  299. 'name' => get_lang('SocialNetwork'),
  300. ];
  301. } else {
  302. $this_section = SECTION_MYPROFILE;
  303. $interbreadcrumb[] = [
  304. 'url' => api_get_path(WEB_CODE_PATH).'auth/profile.php',
  305. 'name' => get_lang('Profile'),
  306. ];
  307. }
  308. $interbreadcrumb[] = [
  309. 'url' => api_get_path(WEB_CODE_PATH).'messages/inbox.php',
  310. 'name' => get_lang('Messages'),
  311. ];
  312. $group_id = isset($_REQUEST['group_id']) ? (int) $_REQUEST['group_id'] : 0;
  313. $social_right_content = null;
  314. if ($group_id != 0) {
  315. $social_right_content .= '<div class=actions>';
  316. $social_right_content .= '<a href="'.api_get_path(WEB_CODE_PATH).'social/group_view.php?id='.$group_id.'">'.
  317. Display::return_icon('back.png', api_xml_http_response_encode(get_lang('ComposeMessage'))).'</a>';
  318. $social_right_content .= '<a href="'.api_get_path(WEB_CODE_PATH).'messages/new_message.php?group_id='.$group_id.'">'.
  319. Display::return_icon('message_new.png', api_xml_http_response_encode(get_lang('ComposeMessage'))).'</a>';
  320. $social_right_content .= '</div>';
  321. } else {
  322. if ($allowSocial) {
  323. } else {
  324. $social_right_content .= '<div class=actions>';
  325. if (api_get_setting('allow_message_tool') === 'true') {
  326. $social_right_content .= '<a href="'.api_get_path(WEB_CODE_PATH).'messages/new_message.php">'.
  327. Display::return_icon('message_new.png', get_lang('ComposeMessage')).'</a>';
  328. $social_right_content .= '<a href="'.api_get_path(WEB_CODE_PATH).'messages/inbox.php">'.
  329. Display::return_icon('inbox.png', get_lang('Inbox')).'</a>';
  330. $social_right_content .= '<a href="'.api_get_path(WEB_CODE_PATH).'messages/outbox.php">'.
  331. Display::return_icon('outbox.png', get_lang('Outbox')).'</a>';
  332. }
  333. $social_right_content .= '</div>';
  334. }
  335. }
  336. // LEFT COLUMN
  337. $social_left_content = '';
  338. if ($allowSocial) {
  339. // Block Social Menu
  340. $social_menu_block = SocialManager::show_social_menu('messages');
  341. $social_right_content .= '<div class="row">';
  342. $social_right_content .= '<div class="col-md-12">';
  343. $social_right_content .= '<div class="actions">';
  344. $social_right_content .= '<a href="'.api_get_path(WEB_CODE_PATH).'messages/inbox.php">'.
  345. Display::return_icon('back.png', get_lang('Back'), [], 32).'</a>';
  346. $social_right_content .= '</div>';
  347. $social_right_content .= '</div>';
  348. $social_right_content .= '<div class="col-md-12">';
  349. }
  350. // MAIN CONTENT
  351. if (!isset($_POST['compose'])) {
  352. if (isset($_GET['re_id'])) {
  353. $social_right_content .= show_compose_reply_to_message(
  354. $_GET['re_id'],
  355. api_get_user_id(),
  356. $tpl
  357. );
  358. } elseif (isset($_GET['send_to_user'])) {
  359. $social_right_content .= show_compose_to_user($_GET['send_to_user'], $tpl);
  360. } else {
  361. $social_right_content .= show_compose_to_any($tpl);
  362. }
  363. } else {
  364. $restrict = false;
  365. if (isset($_POST['users'])) {
  366. $restrict = true;
  367. } elseif (isset($_POST['group_id'])) {
  368. $restrict = true;
  369. } elseif (isset($_POST['hidden_user'])) {
  370. $restrict = true;
  371. }
  372. $default['title'] = $_POST['title'];
  373. $default['content'] = $_POST['content'];
  374. // comes from a reply button
  375. if (isset($_GET['re_id']) || isset($_GET['forward_id'])) {
  376. $social_right_content .= manageForm($default, null, null, $tpl);
  377. } else {
  378. // post
  379. if ($restrict) {
  380. if (!isset($_POST['group_id'])) {
  381. $default['users'] = isset($_POST['users']) ? $_POST['users'] : null;
  382. } else {
  383. $default['group_id'] = (int) $_POST['group_id'];
  384. }
  385. if (isset($_POST['hidden_user'])) {
  386. $default['users'] = [$_POST['hidden_user']];
  387. }
  388. $social_right_content .= manageForm($default, null, null, $tpl);
  389. } else {
  390. $social_right_content .= Display::return_message(get_lang('ErrorSendingMessage'), 'error');
  391. }
  392. }
  393. }
  394. if ($allowSocial) {
  395. $social_right_content .= '</div>';
  396. $social_right_content .= '</div>';
  397. }
  398. // Block Social Avatar
  399. SocialManager::setSocialUserBlock($tpl, api_get_user_id(), 'messages');
  400. MessageManager::cleanAudioMessage();
  401. if ($allowSocial) {
  402. $tpl->assign('social_menu_block', $social_menu_block);
  403. $tpl->assign('social_right_content', $social_right_content);
  404. $social_layout = $tpl->get_template('social/inbox.tpl');
  405. $tpl->display($social_layout);
  406. } else {
  407. $content = $social_right_content;
  408. $tpl->assign('content', $content);
  409. $tpl->display_one_col_template();
  410. }