SendTopic.php 14 KB

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