new_message.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. if (isset($_GET['prefill'])) {
  236. switch ($_GET['prefill']) {
  237. case 'ofaj':
  238. $sendToUser = isset($_GET['send_to_user']) ? $_GET['send_to_user'] : 0;
  239. $sendToUserFullName = '';
  240. if ($sendToUser) {
  241. $sendToUserInfo = api_get_user_info($sendToUser);
  242. $sendToUserFirstname = $sendToUserInfo['firstname'];
  243. }
  244. $currentUserInfo = api_get_user_info();
  245. $default['title'] = get_lang('OfajEndLPSubject');
  246. $default['content'] = sprintf(
  247. get_lang('OfajEndLpDescription'),
  248. $sendToUserFirstname,
  249. $currentUserInfo['firstname'],
  250. $currentUserInfo['firstname']
  251. );
  252. break;
  253. }
  254. }
  255. $form->setDefaults($default);
  256. $html = '';
  257. if ($form->validate()) {
  258. $check = Security::check_token('post');
  259. $disabled = api_get_configuration_value('disable_token_in_new_message');
  260. if ($disabled) {
  261. $check = true;
  262. }
  263. if ($check) {
  264. $user_list = $default['users'];
  265. $file_comments = $_POST['legend'];
  266. $title = $default['title'];
  267. $content = $default['content'];
  268. $group_id = isset($default['group_id']) ? $default['group_id'] : null;
  269. $parent_id = isset($default['parent_id']) ? $default['parent_id'] : null;
  270. $forwardId = isset($_POST['forward_id']) ? $_POST['forward_id'] : false;
  271. if (is_array($user_list) && count($user_list) > 0) {
  272. // All is well, send the message
  273. foreach ($user_list as $userId) {
  274. $res = MessageManager::send_message(
  275. $userId,
  276. $title,
  277. $content,
  278. $_FILES,
  279. $file_comments,
  280. $group_id,
  281. $parent_id,
  282. 0,
  283. 0,
  284. null,
  285. false,
  286. $forwardId,
  287. [],
  288. true
  289. );
  290. if ($res) {
  291. $userInfo = api_get_user_info($userId);
  292. Display::addFlash(Display::return_message(
  293. get_lang('MessageSentTo')."&nbsp;<b>".$userInfo['complete_name_with_username']."</b>",
  294. 'confirmation',
  295. false
  296. ));
  297. }
  298. }
  299. MessageManager::cleanAudioMessage();
  300. } else {
  301. Display::addFlash(Display::return_message('ErrorSendingMessage', 'error'));
  302. }
  303. }
  304. Security::clear_token();
  305. header('Location: '.api_get_path(WEB_CODE_PATH).'messages/inbox.php');
  306. exit;
  307. } else {
  308. $token = Security::get_token();
  309. $form->addElement('hidden', 'sec_token');
  310. $form->setConstants(['sec_token' => $token]);
  311. $html .= $form->returnForm();
  312. }
  313. return $html;
  314. }
  315. if ($allowSocial) {
  316. $this_section = SECTION_SOCIAL;
  317. $interbreadcrumb[] = [
  318. 'url' => api_get_path(WEB_CODE_PATH).'social/home.php',
  319. 'name' => get_lang('SocialNetwork'),
  320. ];
  321. } else {
  322. $this_section = SECTION_MYPROFILE;
  323. $interbreadcrumb[] = [
  324. 'url' => api_get_path(WEB_CODE_PATH).'auth/profile.php',
  325. 'name' => get_lang('Profile'),
  326. ];
  327. }
  328. $interbreadcrumb[] = [
  329. 'url' => api_get_path(WEB_CODE_PATH).'messages/inbox.php',
  330. 'name' => get_lang('Messages'),
  331. ];
  332. $group_id = isset($_REQUEST['group_id']) ? (int) $_REQUEST['group_id'] : 0;
  333. $social_right_content = null;
  334. if ($group_id != 0) {
  335. $social_right_content .= '<div class=actions>';
  336. $social_right_content .= '<a href="'.api_get_path(WEB_CODE_PATH).'social/group_view.php?id='.$group_id.'">'.
  337. Display::return_icon('back.png', api_xml_http_response_encode(get_lang('ComposeMessage'))).'</a>';
  338. $social_right_content .= '<a href="'.api_get_path(WEB_CODE_PATH).'messages/new_message.php?group_id='.$group_id.'">'.
  339. Display::return_icon('message_new.png', api_xml_http_response_encode(get_lang('ComposeMessage'))).'</a>';
  340. $social_right_content .= '</div>';
  341. } else {
  342. if ($allowSocial) {
  343. } else {
  344. $social_right_content .= '<div class=actions>';
  345. if (api_get_setting('allow_message_tool') === 'true') {
  346. $social_right_content .= '<a href="'.api_get_path(WEB_CODE_PATH).'messages/new_message.php">'.
  347. Display::return_icon('message_new.png', get_lang('ComposeMessage')).'</a>';
  348. $social_right_content .= '<a href="'.api_get_path(WEB_CODE_PATH).'messages/inbox.php">'.
  349. Display::return_icon('inbox.png', get_lang('Inbox')).'</a>';
  350. $social_right_content .= '<a href="'.api_get_path(WEB_CODE_PATH).'messages/outbox.php">'.
  351. Display::return_icon('outbox.png', get_lang('Outbox')).'</a>';
  352. }
  353. $social_right_content .= '</div>';
  354. }
  355. }
  356. // LEFT COLUMN
  357. $social_left_content = '';
  358. if ($allowSocial) {
  359. // Block Social Menu
  360. $social_menu_block = SocialManager::show_social_menu('messages');
  361. $social_right_content .= '<div class="row">';
  362. $social_right_content .= '<div class="col-md-12">';
  363. $social_right_content .= '<div class="actions">';
  364. $social_right_content .= '<a href="'.api_get_path(WEB_CODE_PATH).'messages/inbox.php">'.
  365. Display::return_icon('back.png', get_lang('Back'), [], 32).'</a>';
  366. $social_right_content .= '</div>';
  367. $social_right_content .= '</div>';
  368. $social_right_content .= '<div class="col-md-12">';
  369. }
  370. // MAIN CONTENT
  371. if (!isset($_POST['compose'])) {
  372. if (isset($_GET['re_id'])) {
  373. $social_right_content .= show_compose_reply_to_message(
  374. $_GET['re_id'],
  375. api_get_user_id(),
  376. $tpl
  377. );
  378. } elseif (isset($_GET['send_to_user'])) {
  379. $social_right_content .= show_compose_to_user($_GET['send_to_user'], $tpl);
  380. } else {
  381. $social_right_content .= show_compose_to_any($tpl);
  382. }
  383. } else {
  384. $restrict = false;
  385. if (isset($_POST['users'])) {
  386. $restrict = true;
  387. } elseif (isset($_POST['group_id'])) {
  388. $restrict = true;
  389. } elseif (isset($_POST['hidden_user'])) {
  390. $restrict = true;
  391. }
  392. $default['title'] = $_POST['title'];
  393. $default['content'] = $_POST['content'];
  394. // comes from a reply button
  395. if (isset($_GET['re_id']) || isset($_GET['forward_id'])) {
  396. $social_right_content .= manageForm($default, null, null, $tpl);
  397. } else {
  398. // post
  399. if ($restrict) {
  400. if (!isset($_POST['group_id'])) {
  401. $default['users'] = isset($_POST['users']) ? $_POST['users'] : null;
  402. } else {
  403. $default['group_id'] = (int) $_POST['group_id'];
  404. }
  405. if (isset($_POST['hidden_user'])) {
  406. $default['users'] = [$_POST['hidden_user']];
  407. }
  408. $social_right_content .= manageForm($default, null, null, $tpl);
  409. } else {
  410. $social_right_content .= Display::return_message(get_lang('ErrorSendingMessage'), 'error');
  411. }
  412. }
  413. }
  414. if ($allowSocial) {
  415. $social_right_content .= '</div>';
  416. $social_right_content .= '</div>';
  417. }
  418. // Block Social Avatar
  419. SocialManager::setSocialUserBlock($tpl, api_get_user_id(), 'messages');
  420. MessageManager::cleanAudioMessage();
  421. if ($allowSocial) {
  422. $tpl->assign('social_menu_block', $social_menu_block);
  423. $tpl->assign('social_right_content', $social_right_content);
  424. $social_layout = $tpl->get_template('social/inbox.tpl');
  425. $tpl->display($social_layout);
  426. } else {
  427. $content = $social_right_content;
  428. $tpl->assign('content', $content);
  429. $tpl->display_one_col_template();
  430. }