email_editor.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * This script contains the code to edit and send an e-mail to one of
  6. * the platform's users.
  7. * It can be called from the JavaScript library email_links.lib.php which
  8. * overtakes the mailto: links to use the internal interface instead.
  9. *
  10. * @author Yannick Warnier <ywarnier@beeznest.org>
  11. * @author Julio Montoya <gugli100@gmail.com> Updating form with formvalidator
  12. */
  13. require_once __DIR__.'/../inc/global.inc.php';
  14. if (empty(api_get_user_id())) {
  15. api_not_allowed(true);
  16. }
  17. $_user = api_get_user_info();
  18. $originUrl = Session::read('origin_url');
  19. if (empty($originUrl)) {
  20. Session::write('origin_url', $_SERVER['HTTP_REFERER']);
  21. }
  22. $action = isset($_GET['action']) ? $_GET['action'] : null;
  23. $form = new FormValidator('email_editor', 'post');
  24. $form->addElement('hidden', 'dest');
  25. $form->addElement('text', 'email_address', get_lang('EmailDestination'));
  26. $form->addElement('text', 'email_title', get_lang('EmailTitle'));
  27. $form->freeze('email_address');
  28. $form->addElement('textarea', 'email_text', get_lang('EmailText'), ['rows' => '6']);
  29. $form->addRule('email_address', get_lang('ThisFieldIsRequired'), 'required');
  30. $form->addRule('email_title', get_lang('ThisFieldIsRequired'), 'required');
  31. $form->addRule('email_text', get_lang('ThisFieldIsRequired'), 'required');
  32. $form->addRule('email_address', get_lang('EmailWrong'), 'email');
  33. $form->addButtonSend(get_lang('SendMail'));
  34. switch ($action) {
  35. case 'subscribe_me_to_session':
  36. $sessionName = isset($_GET['session']) ? Security::remove_XSS($_GET['session']) : null;
  37. $objTemplate = new Template();
  38. $objTemplate->assign('session_name', $sessionName);
  39. $objTemplate->assign('user', api_get_user_info());
  40. $mailTemplate = $objTemplate->get_template('mail/subscribe_me_to_session.tpl');
  41. $emailDest = api_get_setting('emailAdministrator');
  42. $emailTitle = get_lang('SubscribeToSessionRequest');
  43. $emailText = $objTemplate->fetch($mailTemplate);
  44. break;
  45. default:
  46. $emailDest = isset($_REQUEST['dest']) ? Security::remove_XSS($_REQUEST['dest']) : '';
  47. $emailTitle = isset($_REQUEST['subject']) ? Security::remove_XSS($_REQUEST['subject']) : '';
  48. $emailText = isset($_REQUEST['body']) ? Security::remove_XSS($_REQUEST['body']) : '';
  49. break;
  50. }
  51. $defaults = [
  52. 'dest' => $emailDest,
  53. 'email_address' => $emailDest,
  54. 'email_title' => $emailTitle,
  55. 'email_text' => $emailText,
  56. ];
  57. $form->setDefaults($defaults);
  58. if ($form->validate()) {
  59. $check = Security::check_token();
  60. Security::clear_token();
  61. if ($check) {
  62. Security::clear_token();
  63. $values = $form->getSubmitValues();
  64. $text = nl2br($values['email_text']).'<br /><br /><br />'.get_lang('EmailSentFromLMS').' '.api_get_path(
  65. WEB_PATH
  66. );
  67. $email_administrator = $values['dest'];
  68. $title = $values['email_title'];
  69. if (!empty($_user['mail'])) {
  70. api_mail_html(
  71. '',
  72. $email_administrator,
  73. $title,
  74. $text,
  75. api_get_person_name($_user['firstname'], $_user['lastname']),
  76. $_user['mail'],
  77. [
  78. 'reply_to' => [
  79. 'mail' => $_user['mail'],
  80. 'name' => api_get_person_name($_user['firstname'], $_user['lastname']),
  81. ],
  82. ]
  83. );
  84. } else {
  85. api_mail_html(
  86. '',
  87. $email_administrator,
  88. $title,
  89. $text,
  90. get_lang('Anonymous')
  91. );
  92. }
  93. Display::addFlash(Display::return_message(get_lang('MessageSent')));
  94. $orig = Session::read('origin_url');
  95. Session::erase('origin_url');
  96. header('Location:'.$orig);
  97. exit;
  98. }
  99. }
  100. $form->addHidden('sec_token', Security::get_token());
  101. Display::display_header(get_lang('SendEmail'));
  102. $form->display();
  103. Display::display_footer();