SendTopic.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. <?php
  2. /**
  3. * The functions in this file deal with sending topics to a friend or moderator
  4. * Simple Machines Forum (SMF)
  5. *
  6. * @package SMF
  7. * @author Simple Machines http://www.simplemachines.org
  8. * @copyright 2014 Simple Machines and individual contributors
  9. * @license http://www.simplemachines.org/about/smf/license.php BSD
  10. *
  11. * @version 2.1 Alpha 1
  12. */
  13. if (!defined('SMF'))
  14. die('No direct access...');
  15. /**
  16. * The main handling function for sending specialist (Or otherwise) emails to a user.
  17. */
  18. function EmailUser()
  19. {
  20. global $context;
  21. // Don't index anything here.
  22. $context['robot_no_index'] = true;
  23. // Load the template.
  24. loadTemplate('SendTopic');
  25. $sub_actions = array(
  26. 'email' => 'CustomEmail',
  27. );
  28. if (!isset($_GET['sa']) || !isset($sub_actions[$_GET['sa']]))
  29. $_GET['sa'] = 'email';
  30. $sub_actions[$_GET['sa']]();
  31. }
  32. /**
  33. * Allow a user to send an email.
  34. * Send an email to the user - allow the sender to write the message.
  35. * Can either be passed a user ID as uid or a message id as msg.
  36. * Does not check permissions for a message ID as there is no information disclosed.
  37. */
  38. function CustomEmail()
  39. {
  40. global $context, $user_info, $smcFunc, $txt, $scripturl, $sourcedir;
  41. // Can the user even see this information?
  42. if ($user_info['is_guest'])
  43. fatal_lang_error('no_access', false);
  44. isAllowedTo('send_email_to_members');
  45. // Are we sending to a user?
  46. $context['form_hidden_vars'] = array();
  47. if (isset($_REQUEST['uid']))
  48. {
  49. $request = $smcFunc['db_query']('', '
  50. SELECT email_address AS email, real_name AS name, id_member, hide_email
  51. FROM {db_prefix}members
  52. WHERE id_member = {int:id_member}',
  53. array(
  54. 'id_member' => (int) $_REQUEST['uid'],
  55. )
  56. );
  57. $context['form_hidden_vars']['uid'] = (int) $_REQUEST['uid'];
  58. }
  59. elseif (isset($_REQUEST['msg']))
  60. {
  61. $request = $smcFunc['db_query']('', '
  62. SELECT IFNULL(mem.email_address, m.poster_email) AS email, IFNULL(mem.real_name, m.poster_name) AS name, IFNULL(mem.id_member, 0) AS id_member, hide_email
  63. FROM {db_prefix}messages AS m
  64. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
  65. WHERE m.id_msg = {int:id_msg}',
  66. array(
  67. 'id_msg' => (int) $_REQUEST['msg'],
  68. )
  69. );
  70. $context['form_hidden_vars']['msg'] = (int) $_REQUEST['msg'];
  71. }
  72. if (empty($request) || $smcFunc['db_num_rows']($request) == 0)
  73. fatal_lang_error('cant_find_user_email');
  74. $row = $smcFunc['db_fetch_assoc']($request);
  75. $smcFunc['db_free_result']($request);
  76. // Are you sure you got the address?
  77. if (empty($row['email']))
  78. fatal_lang_error('cant_find_user_email');
  79. // Can they actually do this?
  80. $context['show_email_address'] = showEmailAddress(!empty($row['hide_email']), $row['id_member']);
  81. if ($context['show_email_address'] === 'no')
  82. fatal_lang_error('no_access', false);
  83. // Setup the context!
  84. $context['recipient'] = array(
  85. 'id' => $row['id_member'],
  86. 'name' => $row['name'],
  87. 'email' => $row['email'],
  88. 'email_link' => ($context['show_email_address'] == 'yes_permission_override' ? '<em>' : '') . '<a href="mailto:' . $row['email'] . '">' . $row['email'] . '</a>' . ($context['show_email_address'] == 'yes_permission_override' ? '</em>' : ''),
  89. 'link' => $row['id_member'] ? '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['name'] . '</a>' : $row['name'],
  90. );
  91. // Can we see this person's email address?
  92. $context['can_view_receipient_email'] = $context['show_email_address'] == 'yes' || $context['show_email_address'] == 'yes_permission_override';
  93. // Are we actually sending it?
  94. if (isset($_POST['send']) && isset($_POST['email_body']))
  95. {
  96. require_once($sourcedir . '/Subs-Post.php');
  97. checkSession();
  98. // If it's a guest sort out their names.
  99. if ($user_info['is_guest'])
  100. {
  101. if (empty($_POST['y_name']) || $_POST['y_name'] == '_' || trim($_POST['y_name']) == '')
  102. fatal_lang_error('no_name', false);
  103. if (empty($_POST['y_email']))
  104. fatal_lang_error('no_email', false);
  105. if (preg_match('~^[0-9A-Za-z=_+\-/][0-9A-Za-z=_\'+\-/\.]*@[\w\-]+(\.[\w\-]+)*(\.[\w]{2,6})$~', $_POST['y_email']) == 0)
  106. fatal_lang_error('email_invalid_character', false);
  107. $from_name = trim($_POST['y_name']);
  108. $from_email = trim($_POST['y_email']);
  109. }
  110. else
  111. {
  112. $from_name = $user_info['name'];
  113. $from_email = $user_info['email'];
  114. }
  115. // Check we have a body (etc).
  116. if (trim($_POST['email_body']) == '' || trim($_POST['email_subject']) == '')
  117. fatal_lang_error('email_missing_data');
  118. // We use a template in case they want to customise!
  119. $replacements = array(
  120. 'EMAILSUBJECT' => $_POST['email_subject'],
  121. 'EMAILBODY' => $_POST['email_body'],
  122. 'SENDERNAME' => $from_name,
  123. 'RECPNAME' => $context['recipient']['name'],
  124. );
  125. // Don't let them send too many!
  126. spamProtection('sendmail');
  127. // Get the template and get out!
  128. $emaildata = loadEmailTemplate('send_email', $replacements);
  129. sendmail($context['recipient']['email'], $emaildata['subject'], $emaildata['body'], $from_email, 'custemail', false, 1, null, true);
  130. // Now work out where to go!
  131. if (isset($_REQUEST['uid']))
  132. redirectexit('action=profile;u=' . (int) $_REQUEST['uid']);
  133. elseif (isset($_REQUEST['msg']))
  134. redirectexit('msg=' . (int) $_REQUEST['msg']);
  135. else
  136. redirectexit();
  137. }
  138. $context['sub_template'] = 'custom_email';
  139. $context['page_title'] = $txt['send_email'];
  140. }
  141. /**
  142. * Report a post to the moderator... ask for a comment.
  143. * Gathers data from the user to report abuse to the moderator(s).
  144. * Uses the ReportToModerator template, main sub template.
  145. * Requires the report_any permission.
  146. * Uses ReportToModerator2() if post data was sent.
  147. * Accessed through ?action=reporttm.
  148. */
  149. function ReportToModerator()
  150. {
  151. global $txt, $topic, $context, $smcFunc;
  152. $context['robot_no_index'] = true;
  153. // No guests!
  154. is_not_guest();
  155. // You can't use this if it's off or you are not allowed to do it.
  156. isAllowedTo('report_any');
  157. // If they're posting, it should be processed by ReportToModerator2.
  158. if ((isset($_POST[$context['session_var']]) || isset($_POST['save'])) && empty($context['post_errors']))
  159. ReportToModerator2();
  160. // We need a message ID to check!
  161. if (empty($_REQUEST['msg']) && empty($_REQUEST['mid']))
  162. fatal_lang_error('no_access', false);
  163. // For compatibility, accept mid, but we should be using msg. (not the flavor kind!)
  164. $_REQUEST['msg'] = empty($_REQUEST['msg']) ? (int) $_REQUEST['mid'] : (int) $_REQUEST['msg'];
  165. // Check the message's ID - don't want anyone reporting a post they can't even see!
  166. $result = $smcFunc['db_query']('', '
  167. SELECT m.id_msg, m.id_member, t.id_member_started
  168. FROM {db_prefix}messages AS m
  169. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = {int:current_topic})
  170. WHERE m.id_msg = {int:id_msg}
  171. AND m.id_topic = {int:current_topic}
  172. LIMIT 1',
  173. array(
  174. 'current_topic' => $topic,
  175. 'id_msg' => $_REQUEST['msg'],
  176. )
  177. );
  178. if ($smcFunc['db_num_rows']($result) == 0)
  179. fatal_lang_error('no_board', false);
  180. list ($_REQUEST['msg'], $member, $starter) = $smcFunc['db_fetch_row']($result);
  181. $smcFunc['db_free_result']($result);
  182. // Show the inputs for the comment, etc.
  183. loadLanguage('Post');
  184. loadTemplate('SendTopic');
  185. addInlineJavascript('
  186. var error_box = $("#error_box");
  187. $("#report_comment").keyup(function() {
  188. var post_too_long = $("#error_post_too_long");
  189. if ($(this).val().length > 254)
  190. {
  191. if (post_too_long.length == 0)
  192. {
  193. error_box.show();
  194. if ($.trim(error_box.html()) == \'\')
  195. error_box.append("<ul id=\'error_list\'></ul>");
  196. $("#error_list").append("<li id=\'error_post_too_long\' class=\'error\'>" + ' . JavaScriptEscape($txt['post_too_long']) . ' + "</li>");
  197. }
  198. }
  199. else
  200. {
  201. post_too_long.remove();
  202. if ($("#error_list li").length == 0)
  203. error_box.hide();
  204. }
  205. });', true);
  206. $context['comment_body'] = !isset($_POST['comment']) ? '' : trim($_POST['comment']);
  207. // This is here so that the user could, in theory, be redirected back to the topic.
  208. $context['start'] = $_REQUEST['start'];
  209. $context['message_id'] = $_REQUEST['msg'];
  210. $context['page_title'] = $txt['report_to_mod'];
  211. $context['sub_template'] = 'report';
  212. }
  213. /**
  214. * Send the emails.
  215. * Sends off emails to all the moderators.
  216. * Sends to administrators and global moderators. (1 and 2)
  217. * Called by ReportToModerator(), and thus has the same permission and setting requirements as it does.
  218. * Accessed through ?action=reporttm when posting.
  219. */
  220. function ReportToModerator2()
  221. {
  222. global $txt, $topic, $user_info, $modSettings, $sourcedir, $context, $smcFunc;
  223. // Sorry, no guests allowed... Probably just trying to spam us anyway
  224. is_not_guest();
  225. // You must have the proper permissions!
  226. isAllowedTo('report_any');
  227. // Make sure they aren't spamming.
  228. spamProtection('reporttm');
  229. require_once($sourcedir . '/Subs-Post.php');
  230. // No errors, yet.
  231. $post_errors = array();
  232. // Check their session.
  233. if (checkSession('post', '', false) != '')
  234. $post_errors[] = 'session_timeout';
  235. // Make sure we have a comment and it's clean.
  236. if (!isset($_POST['comment']) || $smcFunc['htmltrim']($_POST['comment']) === '')
  237. $post_errors[] = 'no_comment';
  238. $poster_comment = strtr($smcFunc['htmlspecialchars']($_POST['comment']), array("\r" => '', "\t" => ''));
  239. if ($smcFunc['strlen']($poster_comment) > 254)
  240. $post_errors[] = 'post_too_long';
  241. // Guests need to provide their address!
  242. if ($user_info['is_guest'])
  243. {
  244. $_POST['email'] = !isset($_POST['email']) ? '' : trim($_POST['email']);
  245. if ($_POST['email'] === '')
  246. $post_errors[] = 'no_email';
  247. elseif (preg_match('~^[0-9A-Za-z=_+\-/][0-9A-Za-z=_\'+\-/\.]*@[\w\-]+(\.[\w\-]+)*(\.[\w]{2,6})$~', $_POST['email']) == 0)
  248. $post_errors[] = 'bad_email';
  249. isBannedEmail($_POST['email'], 'cannot_post', sprintf($txt['you_are_post_banned'], $txt['guest_title']));
  250. $user_info['email'] = $smcFunc['htmlspecialchars']($_POST['email']);
  251. }
  252. // Could they get the right verification code?
  253. if ($user_info['is_guest'] && !empty($modSettings['guests_report_require_captcha']))
  254. {
  255. require_once($sourcedir . '/Subs-Editor.php');
  256. $verificationOptions = array(
  257. 'id' => 'report',
  258. );
  259. $context['require_verification'] = create_control_verification($verificationOptions, true);
  260. if (is_array($context['require_verification']))
  261. $post_errors = array_merge($post_errors, $context['require_verification']);
  262. }
  263. // Any errors?
  264. if (!empty($post_errors))
  265. {
  266. loadLanguage('Errors');
  267. $context['post_errors'] = array();
  268. foreach ($post_errors as $post_error)
  269. $context['post_errors'][$post_error] = $txt['error_' . $post_error];
  270. return ReportToModerator();
  271. }
  272. // Get the basic topic information, and make sure they can see it.
  273. $_POST['msg'] = (int) $_POST['msg'];
  274. $request = $smcFunc['db_query']('', '
  275. SELECT m.id_topic, m.id_board, m.subject, m.body, m.id_member AS id_poster, m.poster_name, mem.real_name
  276. FROM {db_prefix}messages AS m
  277. LEFT JOIN {db_prefix}members AS mem ON (m.id_member = mem.id_member)
  278. WHERE m.id_msg = {int:id_msg}
  279. AND m.id_topic = {int:current_topic}
  280. LIMIT 1',
  281. array(
  282. 'current_topic' => $topic,
  283. 'id_msg' => $_POST['msg'],
  284. )
  285. );
  286. if ($smcFunc['db_num_rows']($request) == 0)
  287. fatal_lang_error('no_board', false);
  288. $message = $smcFunc['db_fetch_assoc']($request);
  289. $smcFunc['db_free_result']($request);
  290. $poster_name = un_htmlspecialchars($message['real_name']) . ($message['real_name'] != $message['poster_name'] ? ' (' . $message['poster_name'] . ')' : '');
  291. $reporterName = un_htmlspecialchars($user_info['name']) . ($user_info['name'] != $user_info['username'] && $user_info['username'] != '' ? ' (' . $user_info['username'] . ')' : '');
  292. $subject = un_htmlspecialchars($message['subject']);
  293. $request = $smcFunc['db_query']('', '
  294. SELECT id_report, ignore_all
  295. FROM {db_prefix}log_reported
  296. WHERE id_msg = {int:id_msg}
  297. AND (closed = {int:not_closed} OR ignore_all = {int:ignored})
  298. ORDER BY ignore_all DESC',
  299. array(
  300. 'id_msg' => $_POST['msg'],
  301. 'not_closed' => 0,
  302. 'ignored' => 1,
  303. )
  304. );
  305. if ($smcFunc['db_num_rows']($request) != 0)
  306. list ($id_report, $ignore) = $smcFunc['db_fetch_row']($request);
  307. $smcFunc['db_free_result']($request);
  308. // If we're just going to ignore these, then who gives a monkeys...
  309. if (!empty($ignore))
  310. redirectexit('topic=' . $topic . '.msg' . $_POST['msg'] . '#msg' . $_POST['msg']);
  311. // Already reported? My god, we could be dealing with a real rogue here...
  312. if (!empty($id_report))
  313. $smcFunc['db_query']('', '
  314. UPDATE {db_prefix}log_reported
  315. SET num_reports = num_reports + 1, time_updated = {int:current_time}
  316. WHERE id_report = {int:id_report}',
  317. array(
  318. 'current_time' => time(),
  319. 'id_report' => $id_report,
  320. )
  321. );
  322. // Otherwise, we shall make one!
  323. else
  324. {
  325. if (empty($message['real_name']))
  326. $message['real_name'] = $message['poster_name'];
  327. $smcFunc['db_insert']('',
  328. '{db_prefix}log_reported',
  329. array(
  330. 'id_msg' => 'int', 'id_topic' => 'int', 'id_board' => 'int', 'id_member' => 'int', 'membername' => 'string',
  331. 'subject' => 'string', 'body' => 'string', 'time_started' => 'int', 'time_updated' => 'int',
  332. 'num_reports' => 'int', 'closed' => 'int',
  333. ),
  334. array(
  335. $_POST['msg'], $message['id_topic'], $message['id_board'], $message['id_poster'], $message['real_name'],
  336. $message['subject'], $message['body'] , time(), time(), 1, 0,
  337. ),
  338. array('id_report')
  339. );
  340. $id_report = $smcFunc['db_insert_id']('{db_prefix}log_reported', 'id_report');
  341. }
  342. // Now just add our report...
  343. if ($id_report)
  344. {
  345. $smcFunc['db_insert']('',
  346. '{db_prefix}log_reported_comments',
  347. array(
  348. 'id_report' => 'int', 'id_member' => 'int', 'membername' => 'string',
  349. 'member_ip' => 'string', 'comment' => 'string', 'time_sent' => 'int',
  350. ),
  351. array(
  352. $id_report, $user_info['id'], $user_info['name'],
  353. $user_info['ip'], $poster_comment, time(),
  354. ),
  355. array('id_comment')
  356. );
  357. // And get ready to notify people.
  358. $smcFunc['db_insert']('insert',
  359. '{db_prefix}background_tasks',
  360. array('task_file' => 'string', 'task_class' => 'string', 'task_data' => 'string', 'claimed_time' => 'int'),
  361. array('$sourcedir/tasks/MsgReport-Notify.php', 'MsgReport_Notify_Background', serialize(array(
  362. 'report_id' => $id_report,
  363. 'msg_id' => $_POST['msg'],
  364. 'topic_id' => $message['id_topic'],
  365. 'board_id' => $message['id_board'],
  366. 'sender_id' => $context['user']['id'],
  367. 'sender_name' => $context['user']['name'],
  368. 'time' => time(),
  369. )), 0),
  370. array('id_task')
  371. );
  372. }
  373. // Keep track of when the mod reports get updated, that way we know when we need to look again.
  374. updateSettings(array('last_mod_report_action' => time()));
  375. // Back to the post we reported!
  376. redirectexit('reportsent;topic=' . $topic . '.msg' . $_POST['msg'] . '#msg' . $_POST['msg']);
  377. }
  378. ?>