email_editor.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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('Receiver'));
  26. $form->addElement('text', 'email_title', get_lang('Subject'));
  27. $form->freeze('email_address');
  28. $form->addElement('textarea', 'email_text', get_lang('E-mail content'), ['rows' => '6']);
  29. $form->addRule('email_address', get_lang('Required field'), 'required');
  30. $form->addRule('email_title', get_lang('Required field'), 'required');
  31. $form->addRule('email_text', get_lang('Required field'), 'required');
  32. $form->addRule('email_address', get_lang('The email address is not complete or contains some invalid characters'), 'email');
  33. $form->addButtonSend(get_lang('Send mail'));
  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('Request subscription');
  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. $values = $form->getSubmitValues();
  60. $text = Security::remove_XSS($values['email_text'])."\n\n---\n".get_lang('E-mail sent from the platform').' '.api_get_path(WEB_PATH);
  61. $email_administrator = Security::remove_XSS($values['dest']);
  62. $title = Security::remove_XSS($values['email_title']);
  63. if (!empty($_user['mail'])) {
  64. api_mail_html(
  65. '',
  66. $email_administrator,
  67. $title,
  68. $text,
  69. api_get_person_name($_user['firstname'], $_user['lastname']),
  70. '',
  71. [
  72. 'reply_to' => [
  73. 'mail' => $_user['mail'],
  74. 'name' => api_get_person_name($_user['firstname'], $_user['lastname']),
  75. ],
  76. ]
  77. );
  78. } else {
  79. api_mail_html(
  80. '',
  81. $email_administrator,
  82. $title,
  83. $text,
  84. get_lang('Anonymous')
  85. );
  86. }
  87. $orig = Session::read('origin_url');
  88. Session::erase('origin_url');
  89. header('Location:'.$orig);
  90. exit;
  91. }
  92. Display::display_header(get_lang('Send email'));
  93. $form->display();
  94. Display::display_footer();