PageRenderTime 52ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/post.php

https://github.com/gencer/fluxbb
PHP | 896 lines | 633 code | 189 blank | 74 comment | 183 complexity | 2848db73384f3e5ac3056eb335ec1638 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * Copyright (C) 2008-2012 FluxBB
  4. * based on code by Rickard Andersson copyright (C) 2002-2008 PunBB
  5. * License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
  6. */
  7. define('PUN_ROOT', dirname(__FILE__).'/');
  8. require PUN_ROOT.'include/common.php';
  9. if ($pun_user['g_read_board'] == '0')
  10. message($lang->t('No view'));
  11. $tid = isset($_GET['tid']) ? intval($_GET['tid']) : 0;
  12. $fid = isset($_GET['fid']) ? intval($_GET['fid']) : 0;
  13. if ($tid < 1 && $fid < 1 || $tid > 0 && $fid > 0)
  14. message($lang->t('Bad request'));
  15. // Fetch some info about the topic and/or the forum
  16. $query = $db->select(array('fid' => 'f.id', 'forum_name' => 'f.forum_name', 'moderators' => 'f.moderators', 'redirect_url' => 'f.redirect_url', 'post_replies' => 'fp.post_replies', 'post_topics' => 'fp.post_topics'), 'forums AS f');
  17. $query->leftJoin('fp', 'forum_perms AS fp', 'fp.forum_id = f.id AND fp.group_id = :group_id');
  18. $query->where = '(fp.read_forum IS NULL OR fp.read_forum = 1) AND '.($tid ? 't.id' : 'f.id').' = :id';
  19. $params = array(':group_id' => $pun_user['g_id']);
  20. $params[':id'] = ($tid ? $tid : $fid);
  21. if ($tid)
  22. {
  23. $query->fields['subect'] = 't.subject';
  24. $query->fields['closed'] = 't.closed';
  25. $query->fields['is_subscribed'] = 's.user_id AS is_subscribed';
  26. $query->innerJoin('t', 'topics AS t', 't.forum_id = f.id');
  27. $query->leftJoin('s', 'topic_subscriptions AS s', 't.id = s.topic_id AND s.user_id = :user_id');
  28. $params[':user_id'] = $pun_user['id'];
  29. }
  30. $result = $query->run($params);
  31. if (empty($result))
  32. message($lang->t('Bad request'));
  33. $cur_posting = $result[0];
  34. unset ($result, $query, $params);
  35. $is_subscribed = $tid && $cur_posting['is_subscribed'];
  36. // Is someone trying to post into a redirect forum?
  37. if ($cur_posting['redirect_url'] != '')
  38. message($lang->t('Bad request'));
  39. // Sort out who the moderators are and if we are currently a moderator (or an admin)
  40. $mods_array = ($cur_posting['moderators'] != '') ? unserialize($cur_posting['moderators']) : array();
  41. $is_admmod = ($pun_user['g_id'] == PUN_ADMIN || ($pun_user['g_moderator'] == '1' && array_key_exists($pun_user['username'], $mods_array))) ? true : false;
  42. if ($tid && $pun_config['o_censoring'] == '1')
  43. $cur_posting['subject'] = censor_words($cur_posting['subject']);
  44. // Do we have permission to post?
  45. if ((($tid && (($cur_posting['post_replies'] == '' && $pun_user['g_post_replies'] == '0') || $cur_posting['post_replies'] == '0')) ||
  46. ($fid && (($cur_posting['post_topics'] == '' && $pun_user['g_post_topics'] == '0') || $cur_posting['post_topics'] == '0')) ||
  47. (isset($cur_posting['closed']) && $cur_posting['closed'] == '1')) &&
  48. !$is_admmod)
  49. message($lang->t('No permission'));
  50. // Load the post.php language file
  51. $lang->load('post');
  52. // Start with a clean slate
  53. $errors = array();
  54. // Did someone just hit "Submit" or "Preview"?
  55. if (isset($_POST['form_sent']))
  56. {
  57. // Flood protection
  58. if (!isset($_POST['preview']) && $pun_user['last_post'] != '' && (time() - $pun_user['last_post']) < $pun_user['g_post_flood'])
  59. $errors[] = $lang->t('Flood start').' '.$pun_user['g_post_flood'].' '.$lang->t('flood end');
  60. // If it's a new topic
  61. if ($fid)
  62. {
  63. $subject = pun_trim($_POST['req_subject']);
  64. if ($pun_config['o_censoring'] == '1')
  65. $censored_subject = pun_trim(censor_words($subject));
  66. if ($subject == '')
  67. $errors[] = $lang->t('No subject');
  68. else if ($pun_config['o_censoring'] == '1' && $censored_subject == '')
  69. $errors[] = $lang->t('No subject after censoring');
  70. else if (pun_strlen($subject) > 70)
  71. $errors[] = $lang->t('Too long subject');
  72. else if ($pun_config['p_subject_all_caps'] == '0' && is_all_uppercase($subject) && !$pun_user['is_admmod'])
  73. $errors[] = $lang->t('All caps subject');
  74. }
  75. // If the user is logged in we get the username and email from $pun_user
  76. if (!$pun_user['is_guest'])
  77. {
  78. $username = $pun_user['username'];
  79. $email = $pun_user['email'];
  80. }
  81. // Otherwise it should be in $_POST
  82. else
  83. {
  84. $username = pun_trim($_POST['req_username']);
  85. $email = strtolower(trim(($pun_config['p_force_guest_email'] == '1') ? $_POST['req_email'] : $_POST['email']));
  86. $banned_email = false;
  87. // Load the register.php/prof_reg.php language files
  88. $lang->load('prof_reg');
  89. $lang->load('register');
  90. // It's a guest, so we have to validate the username
  91. check_username($username);
  92. if ($pun_config['p_force_guest_email'] == '1' || $email != '')
  93. {
  94. require PUN_ROOT.'include/email.php';
  95. if (!is_valid_email($email))
  96. $errors[] = $lang->t('Invalid email');
  97. // Check if it's a banned email address
  98. // we should only check guests because members addresses are already verified
  99. if ($pun_user['is_guest'] && is_banned_email($email))
  100. {
  101. if ($pun_config['p_allow_banned_email'] == '0')
  102. $errors[] = $lang->t('Banned email');
  103. $banned_email = true; // Used later when we send an alert email
  104. }
  105. }
  106. }
  107. // Clean up message from POST
  108. $orig_message = $message = pun_linebreaks(pun_trim($_POST['req_message']));
  109. // Here we use strlen() not pun_strlen() as we want to limit the post to PUN_MAX_POSTSIZE bytes, not characters
  110. if (strlen($message) > PUN_MAX_POSTSIZE)
  111. $errors[] = $lang->t('Too long message', forum_number_format(PUN_MAX_POSTSIZE));
  112. else if ($pun_config['p_message_all_caps'] == '0' && is_all_uppercase($message) && !$pun_user['is_admmod'])
  113. $errors[] = $lang->t('All caps message');
  114. // Validate BBCode syntax
  115. if ($pun_config['p_message_bbcode'] == '1')
  116. {
  117. require PUN_ROOT.'include/parser.php';
  118. $message = preparse_bbcode($message, $errors);
  119. }
  120. if (empty($errors))
  121. {
  122. if ($message == '')
  123. $errors[] = $lang->t('No message');
  124. else if ($pun_config['o_censoring'] == '1')
  125. {
  126. // Censor message to see if that causes problems
  127. $censored_message = pun_trim(censor_words($message));
  128. if ($censored_message == '')
  129. $errors[] = $lang->t('No message after censoring');
  130. }
  131. }
  132. $hide_smilies = isset($_POST['hide_smilies']) ? '1' : '0';
  133. $subscribe = isset($_POST['subscribe']) ? '1' : '0';
  134. $stick_topic = isset($_POST['stick_topic']) && $is_admmod ? '1' : '0';
  135. // Replace four-byte characters (MySQL cannot handle them)
  136. $message = strip_bad_multibyte_chars($message);
  137. $now = time();
  138. // Did everything go according to plan?
  139. if (empty($errors) && !isset($_POST['preview']))
  140. {
  141. require PUN_ROOT.'include/search_idx.php';
  142. // If it's a reply
  143. if ($tid)
  144. {
  145. if (!$pun_user['is_guest'])
  146. {
  147. $new_tid = $tid;
  148. // Insert the new post
  149. $query = $db->insert(array('poster' => ':poster', 'poster_id' => ':poster_id', 'poster_ip' => ':poster_ip', 'message' => ':message', 'hide_smilies' => ':hide_smilies', 'posted' => ':now', 'topic_id' => ':topic_id'), 'posts');
  150. $params = array(':poster' => $username, ':poster_id' => $pun_user['id'], ':poster_ip' => get_remote_address(), ':message' => $message, ':hide_smilies' => $hide_smilies, ':now' => $now, ':topic_id' => $tid);
  151. $query->run($params);
  152. $new_pid = $db->insertId();
  153. unset ($query, $params);
  154. // To subscribe or not to subscribe, that ...
  155. if ($pun_config['o_topic_subscriptions'] == '1')
  156. {
  157. if ($subscribe && !$is_subscribed)
  158. {
  159. $query = $db->insert(array('user_id' => ':user_id', 'topic_id' => ':topic_id'), 'topic_subscriptions');
  160. $params = array(':user_id' => $pun_user['id'], ':topic_id' => $tid);
  161. $query->run($params);
  162. unset ($query, $params);
  163. }
  164. else if (!$subscribe && $is_subscribed)
  165. {
  166. $query = $db->delete('topic_subscriptions');
  167. $query->where = 'user_id = :user_id AND topic_id = :topic_id';
  168. $params = array(':user_id' => $pun_user['id'], ':topic_id' => $tid);
  169. $query->run($params);
  170. unset ($query, $params);
  171. }
  172. }
  173. }
  174. else
  175. {
  176. // It's a guest. Insert the new post
  177. $query = $db->insert(array('poster' => ':poster', 'poster_ip' => ':poster_ip', 'poster_email' => ':poster_email', 'message' => ':message', 'hide_smilies' => ':hide_smilies', 'posted' => ':now', 'topic_id' => ':topic_id'), 'posts');
  178. $params = array(':poster' => $username, ':poster_ip' => get_remote_address(), ':poster_email' => empty($email) ? null : $email, ':message' => $message, ':hide_smilies' => $hide_smilies, ':now' => $now, ':topic_id' => $tid);
  179. $query->run($params);
  180. $new_pid = $db->insertId();
  181. unset ($query, $params);
  182. }
  183. // Count number of replies in the topic
  184. $query = $db->select(array('num_replies' => '(COUNT(p.id) - 1) AS num_replies'), 'posts AS p');
  185. $query->where = 'p.topic_id = :topic_id';
  186. $params = array(':topic_id' => $tid);
  187. $result = $query->run($params);
  188. $num_replies = $result[0]['num_replies'];
  189. unset ($result, $query, $params);
  190. // Update topic
  191. $query = $db->update(array('num_replies' => ':num_replies', 'last_post' => ':now', 'last_post_id' => ':last_post_id', 'last_poster' => ':last_poster'), 'topics');
  192. $query->where = 'id = :topic_id';
  193. $params = array(':num_replies' => $num_replies, ':now' => $now, ':last_post_id' => $new_pid, ':last_poster' => $username, ':topic_id' => $tid);
  194. $query->run($params);
  195. unset ($query, $params);
  196. update_search_index('post', $new_pid, $message);
  197. update_forum($cur_posting['id']);
  198. // Should we send out notifications?
  199. if ($pun_config['o_topic_subscriptions'] == '1')
  200. {
  201. // Get the post time for the previous post in this topic
  202. $query = $db->select(array('posted' => 'p.posted'), 'posts AS p');
  203. $query->where = 'p.topic_id = :topic_id';
  204. $query->order = array('pid' => 'p.id DESC');
  205. $query->offset = 1;
  206. $query->limit = 1;
  207. $params = array(':topic_id' => $tid);
  208. $result = $query->run($params);
  209. $previous_post_time = $result[0]['posted'];
  210. unset ($result, $query, $params);
  211. // Get any subscribed users that should be notified (banned users are excluded)
  212. $query = $db->select(array('id' => 'u.id', 'email' => 'u.email', 'notify_with_post' => 'u.notify_with_post', 'language' => 'u.language'), 'users AS u');
  213. $query->innerJoin('ts', 'topic_subscriptions AS ts', 'u.id = ts.user_id');
  214. $query->leftJoin('fp', 'forum_perms AS fp', 'fp.forum_id = :forum_id AND fp.group_id = u.group_id');
  215. $query->leftJoin('o', 'online AS o', 'u.id = o.user_id');
  216. $query->leftJoin('b', 'bans AS b', 'u.username = b.username');
  217. $query->where = 'b.username IS NULL AND COALESCE(o.logged, u.last_visit) > :last_post AND (fp.read_forum IS NULL OR fp.read_forum = 1) AND ts.topic_id = :topic_id AND u.id != :user_id';
  218. $params = array(':forum_id' => $cur_posting['id'], ':last_post' => $previous_post_time, ':topic_id' => $tid, ':user_id' => $pun_user['id']);
  219. $result = $query->run($params);
  220. if (!empty($result))
  221. {
  222. require_once PUN_ROOT.'include/email.php';
  223. $notification_emails = array();
  224. if ($pun_config['o_censoring'] == '1')
  225. $cleaned_message = bbcode2email($censored_message, -1);
  226. else
  227. $cleaned_message = bbcode2email($message, -1);
  228. // Loop through subscribed users and send emails
  229. foreach ($result as $cur_subscriber)
  230. {
  231. // Is the subscription email for $cur_subscriber['language'] cached or not?
  232. if (!isset($notification_emails[$cur_subscriber['language']]))
  233. {
  234. if (file_exists(PUN_ROOT.'lang/'.$cur_subscriber['language'].'/mail_templates/new_reply.tpl'))
  235. {
  236. // Load the "new reply" template
  237. $mail_tpl = trim(file_get_contents(PUN_ROOT.'lang/'.$cur_subscriber['language'].'/mail_templates/new_reply.tpl'));
  238. // Load the "new reply full" template (with post included)
  239. $mail_tpl_full = trim(file_get_contents(PUN_ROOT.'lang/'.$cur_subscriber['language'].'/mail_templates/new_reply_full.tpl'));
  240. // The first row contains the subject (it also starts with "Subject:")
  241. $first_crlf = strpos($mail_tpl, "\n");
  242. $mail_subject = trim(substr($mail_tpl, 8, $first_crlf-8));
  243. $mail_message = trim(substr($mail_tpl, $first_crlf));
  244. $first_crlf = strpos($mail_tpl_full, "\n");
  245. $mail_subject_full = trim(substr($mail_tpl_full, 8, $first_crlf-8));
  246. $mail_message_full = trim(substr($mail_tpl_full, $first_crlf));
  247. $mail_subject = str_replace('<topic_subject>', $cur_posting['subject'], $mail_subject);
  248. $mail_message = str_replace('<topic_subject>', $cur_posting['subject'], $mail_message);
  249. $mail_message = str_replace('<replier>', $username, $mail_message);
  250. $mail_message = str_replace('<post_url>', get_base_url().'/viewtopic.php?pid='.$new_pid.'#p'.$new_pid, $mail_message);
  251. $mail_message = str_replace('<unsubscribe_url>', get_base_url().'/misc.php?action=unsubscribe&tid='.$tid, $mail_message);
  252. $mail_message = str_replace('<board_mailer>', $pun_config['o_board_title'], $mail_message);
  253. $mail_subject_full = str_replace('<topic_subject>', $cur_posting['subject'], $mail_subject_full);
  254. $mail_message_full = str_replace('<topic_subject>', $cur_posting['subject'], $mail_message_full);
  255. $mail_message_full = str_replace('<replier>', $username, $mail_message_full);
  256. $mail_message_full = str_replace('<message>', $cleaned_message, $mail_message_full);
  257. $mail_message_full = str_replace('<post_url>', get_base_url().'/viewtopic.php?pid='.$new_pid.'#p'.$new_pid, $mail_message_full);
  258. $mail_message_full = str_replace('<unsubscribe_url>', get_base_url().'/misc.php?action=unsubscribe&tid='.$tid, $mail_message_full);
  259. $mail_message_full = str_replace('<board_mailer>', $pun_config['o_board_title'], $mail_message_full);
  260. $notification_emails[$cur_subscriber['language']][0] = $mail_subject;
  261. $notification_emails[$cur_subscriber['language']][1] = $mail_message;
  262. $notification_emails[$cur_subscriber['language']][2] = $mail_subject_full;
  263. $notification_emails[$cur_subscriber['language']][3] = $mail_message_full;
  264. $mail_subject = $mail_message = $mail_subject_full = $mail_message_full = null;
  265. }
  266. }
  267. // We have to double check here because the templates could be missing
  268. if (isset($notification_emails[$cur_subscriber['language']]))
  269. {
  270. if ($cur_subscriber['notify_with_post'] == '0')
  271. pun_mail($cur_subscriber['email'], $notification_emails[$cur_subscriber['language']][0], $notification_emails[$cur_subscriber['language']][1]);
  272. else
  273. pun_mail($cur_subscriber['email'], $notification_emails[$cur_subscriber['language']][2], $notification_emails[$cur_subscriber['language']][3]);
  274. }
  275. }
  276. unset($cleaned_message);
  277. }
  278. unset ($result, $query, $params);
  279. }
  280. }
  281. // If it's a new topic
  282. else if ($fid)
  283. {
  284. // Create the topic
  285. $query = $db->insert(array('poster' => ':poster', 'subject' => ':subject', 'posted' => ':now', 'last_post' => ':now', 'last_poster' => ':last_poster', 'sticky' => ':sticky', 'forum_id' => ':forum_id'), 'topics');
  286. $params = array(':poster' => $username, ':subject' => $subject, ':now' => $now, ':last_poster' => $username, ':sticky' => $stick_topic, ':forum_id' => $fid);
  287. $query->run($params);
  288. $new_tid = $db->insertId();
  289. unset ($query, $params);
  290. if (!$pun_user['is_guest'])
  291. {
  292. // To subscribe or not to subscribe, that ...
  293. if ($pun_config['o_topic_subscriptions'] == '1' && $subscribe)
  294. {
  295. $query = $db->insert(array('user_id' => ':user_id', 'topic_id' => ':topic_id'), 'topic_subscriptions');
  296. $params = array(':user_id' => $pun_user['id'], ':topic_id' => $new_tid);
  297. $query->run($params);
  298. unset ($query, $params);
  299. }
  300. // Create the post ("topic post")
  301. $query = $db->insert(array('poster' => ':poster', 'poster_id' => ':poster_id', 'poster_ip' => ':poster_ip', 'message' => ':message', 'hide_smilies' => ':hide_smilies', 'posted' => ':now', 'topic_id' => ':topic_id'), 'posts');
  302. $params = array(':poster' => $username, ':poster_id' => $pun_user['id'], ':poster_ip' => get_remote_address(), ':message' => $message, ':hide_smilies' => $hide_smilies, ':now' => $now, ':topic_id' => $new_tid);
  303. $query->run($params);
  304. $new_pid = $db->insertId();
  305. unset ($query, $params);
  306. }
  307. else
  308. {
  309. // Create the post ("topic post")
  310. $query = $db->insert(array('poster' => ':poster', 'poster_ip' => ':poster_ip', 'poster_email' => ':poster_email', 'message' => ':message', 'hide_smilies' => ':hide_smilies', 'posted' => ':posted', 'topic_id' => ':topic_id'), 'posts');
  311. $params = array(':poster' => $username, ':poster_ip' => get_remote_address(), ':poster_email' => empty($email) ? null : $email, ':message' => $message, ':hide_smilies' => $hide_smilies, ':now' => $now, ':topic_id' => $new_tid);
  312. $query->run($params);
  313. $new_pid = $db->insertId();
  314. unset ($query, $params);
  315. }
  316. // Update the topic with last_post_id
  317. $query = $db->update(array('last_post_id' => ':new_pid', 'first_post_id' => ':new_pid'), 'topics');
  318. $query->where = 'id = :topic_id';
  319. $params = array(':new_pid' => $new_pid, ':topic_id' => $new_tid);
  320. $query->run($params);
  321. unset ($query, $params);
  322. update_search_index('post', $new_pid, $message, $subject);
  323. update_forum($fid);
  324. // Should we send out notifications?
  325. if ($pun_config['o_forum_subscriptions'] == '1')
  326. {
  327. // Get any subscribed users that should be notified (banned users are excluded)
  328. $query = $db->select(array('id' => 'u.id', 'email' => 'u.email', 'notify_with_post' => 'u.notify_with_post', 'language' => 'u.language'), 'users AS u');
  329. $query->innerJoin('fs', 'forum_subscriptions AS fs', 'u.id = fs.user_id');
  330. $query->leftJoin('fp', 'forum_perms AS fp', 'fp.forum_id = :forum_id AND fp.group_id = u.group_id');
  331. $query->leftJoin('b', 'bans AS b', 'u.username = b.username');
  332. $query->where = 'b.username IS NULL AND (fp.read_forum IS NULL OR fp.read_forum = 1) AND fs.forum_id = :forum_id AND u.id != :user_id';
  333. $params = array(':forum_id' => $cur_posting['id'], ':user_id' => $pun_user['id']);
  334. $result = $query->run($params);
  335. if (!empty($result))
  336. {
  337. require_once PUN_ROOT.'include/email.php';
  338. $notification_emails = array();
  339. if ($pun_config['o_censoring'] == '1')
  340. $cleaned_message = bbcode2email($censored_message, -1);
  341. else
  342. $cleaned_message = bbcode2email($message, -1);
  343. // Loop through subscribed users and send emails
  344. foreach ($result as $cur_subscriber)
  345. {
  346. // Is the subscription email for $cur_subscriber['language'] cached or not?
  347. if (!isset($notification_emails[$cur_subscriber['language']]))
  348. {
  349. if (file_exists(PUN_ROOT.'lang/'.$cur_subscriber['language'].'/mail_templates/new_topic.tpl'))
  350. {
  351. // Load the "new topic" template
  352. $mail_tpl = trim(file_get_contents(PUN_ROOT.'lang/'.$cur_subscriber['language'].'/mail_templates/new_topic.tpl'));
  353. // Load the "new topic full" template (with post included)
  354. $mail_tpl_full = trim(file_get_contents(PUN_ROOT.'lang/'.$cur_subscriber['language'].'/mail_templates/new_topic_full.tpl'));
  355. // The first row contains the subject (it also starts with "Subject:")
  356. $first_crlf = strpos($mail_tpl, "\n");
  357. $mail_subject = trim(substr($mail_tpl, 8, $first_crlf-8));
  358. $mail_message = trim(substr($mail_tpl, $first_crlf));
  359. $first_crlf = strpos($mail_tpl_full, "\n");
  360. $mail_subject_full = trim(substr($mail_tpl_full, 8, $first_crlf-8));
  361. $mail_message_full = trim(substr($mail_tpl_full, $first_crlf));
  362. $mail_subject = str_replace('<forum_name>', $cur_posting['forum_name'], $mail_subject);
  363. $mail_message = str_replace('<topic_subject>', $pun_config['o_censoring'] == '1' ? $censored_subject : $subject, $mail_message);
  364. $mail_message = str_replace('<forum_name>', $cur_posting['forum_name'], $mail_message);
  365. $mail_message = str_replace('<poster>', $username, $mail_message);
  366. $mail_message = str_replace('<topic_url>', get_base_url().'/viewtopic.php?id='.$new_tid, $mail_message);
  367. $mail_message = str_replace('<unsubscribe_url>', get_base_url().'/misc.php?action=unsubscribe&fid='.$cur_posting['id'], $mail_message);
  368. $mail_message = str_replace('<board_mailer>', $pun_config['o_board_title'], $mail_message);
  369. $mail_subject_full = str_replace('<forum_name>', $cur_posting['forum_name'], $mail_subject_full);
  370. $mail_message_full = str_replace('<topic_subject>', $pun_config['o_censoring'] == '1' ? $censored_subject : $subject, $mail_message_full);
  371. $mail_message_full = str_replace('<forum_name>', $cur_posting['forum_name'], $mail_message_full);
  372. $mail_message_full = str_replace('<poster>', $username, $mail_message_full);
  373. $mail_message_full = str_replace('<message>', $cleaned_message, $mail_message_full);
  374. $mail_message_full = str_replace('<topic_url>', get_base_url().'/viewtopic.php?id='.$new_tid, $mail_message_full);
  375. $mail_message_full = str_replace('<unsubscribe_url>', get_base_url().'/misc.php?action=unsubscribe&fid='.$cur_posting['id'], $mail_message_full);
  376. $mail_message_full = str_replace('<board_mailer>', $pun_config['o_board_title'], $mail_message_full);
  377. $notification_emails[$cur_subscriber['language']][0] = $mail_subject;
  378. $notification_emails[$cur_subscriber['language']][1] = $mail_message;
  379. $notification_emails[$cur_subscriber['language']][2] = $mail_subject_full;
  380. $notification_emails[$cur_subscriber['language']][3] = $mail_message_full;
  381. $mail_subject = $mail_message = $mail_subject_full = $mail_message_full = null;
  382. }
  383. }
  384. // We have to double check here because the templates could be missing
  385. if (isset($notification_emails[$cur_subscriber['language']]))
  386. {
  387. if ($cur_subscriber['notify_with_post'] == '0')
  388. pun_mail($cur_subscriber['email'], $notification_emails[$cur_subscriber['language']][0], $notification_emails[$cur_subscriber['language']][1]);
  389. else
  390. pun_mail($cur_subscriber['email'], $notification_emails[$cur_subscriber['language']][2], $notification_emails[$cur_subscriber['language']][3]);
  391. }
  392. }
  393. unset($cleaned_message);
  394. }
  395. unset ($result, $query, $params);
  396. }
  397. }
  398. // If we previously found out that the email was banned
  399. if ($pun_user['is_guest'] && $banned_email && $pun_config['o_mailing_list'] != '')
  400. {
  401. // Load the "banned email post" template
  402. $mail_tpl = trim(file_get_contents(PUN_ROOT.'lang/'.$pun_user['language'].'/mail_templates/banned_email_post.tpl'));
  403. // The first row contains the subject
  404. $first_crlf = strpos($mail_tpl, "\n");
  405. $mail_subject = trim(substr($mail_tpl, 8, $first_crlf-8));
  406. $mail_message = trim(substr($mail_tpl, $first_crlf));
  407. $mail_message = str_replace('<username>', $username, $mail_message);
  408. $mail_message = str_replace('<email>', $email, $mail_message);
  409. $mail_message = str_replace('<post_url>', get_base_url().'/viewtopic.php?pid='.$new_pid.'#p'.$new_pid, $mail_message);
  410. $mail_message = str_replace('<board_mailer>', $pun_config['o_board_title'], $mail_message);
  411. pun_mail($pun_config['o_mailing_list'], $mail_subject, $mail_message);
  412. }
  413. // If the posting user is logged in, increment his/her post count
  414. if (!$pun_user['is_guest'])
  415. {
  416. $query = $db->update(array('num_posts' => 'num_posts + 1', 'last_post' => ':now'), 'users');
  417. $query->where = 'id = :user_id';
  418. $params = array(':now' => $now, ':user_id' => $pun_user['id']);
  419. $query->run($params);
  420. unset ($query, $params);
  421. $tracked_topics = get_tracked_topics();
  422. $tracked_topics['topics'][$new_tid] = time();
  423. set_tracked_topics($tracked_topics);
  424. }
  425. else
  426. {
  427. $query = $db->update(array('last_post' => ':now'), 'online');
  428. $query->where = 'ident = :ident';
  429. $params = array(':now' => $now, ':ident' => get_remote_address());
  430. $query->run($params);
  431. unset ($query, $params);
  432. }
  433. redirect('viewtopic.php?pid='.$new_pid.'#p'.$new_pid, $lang->t('Post redirect'));
  434. }
  435. }
  436. // If a topic ID was specified in the url (it's a reply)
  437. if ($tid)
  438. {
  439. $action = $lang->t('Post a reply');
  440. $form = '<form id="post" method="post" action="post.php?action=post&amp;tid='.$tid.'" onsubmit="this.submit.disabled=true;if(process_form(this)){return true;}else{this.submit.disabled=false;return false;}">';
  441. // If a quote ID was specified in the url
  442. if (isset($_GET['qid']))
  443. {
  444. $qid = intval($_GET['qid']);
  445. if ($qid < 1)
  446. message($lang->t('Bad request'));
  447. $query = $db->select(array('poster' => 'p.poster', 'message' => 'p.message'), 'posts AS p');
  448. $query->where = 'p.id = :qid AND p.topic_id = :tid';
  449. $params = array(':qid' => $qid, ':tid' => $tid);
  450. $result = $query->run($params);
  451. if (empty($result))
  452. message($lang->t('Bad request'));
  453. $cur_quote = $result[0];
  454. unset ($result, $query, $params);
  455. // If the message contains a code tag we have to split it up (text within [code][/code] shouldn't be touched)
  456. if (strpos($cur_quote['message'], '[code]') !== false && strpos($cur_quote['message'], '[/code]') !== false)
  457. {
  458. list($inside, $outside) = split_text($cur_quote['message'], '[code]', '[/code]');
  459. $cur_quote['message'] = implode("\1", $outside);
  460. }
  461. // Remove [img] tags from quoted message
  462. $cur_quote['message'] = preg_replace('%\[img(?:=(?:[^\[]*?))?\]((ht|f)tps?://)([^\s<"]*?)\[/img\]%U', '\1\3', $cur_quote['message']);
  463. // If we split up the message before we have to concatenate it together again (code tags)
  464. if (isset($inside))
  465. {
  466. $outside = explode("\1", $cur_quote['message']);
  467. $cur_quote['message'] = '';
  468. $num_tokens = count($outside);
  469. for ($i = 0; $i < $num_tokens; ++$i)
  470. {
  471. $cur_quote['message'] .= $outside[$i];
  472. if (isset($inside[$i]))
  473. $cur_quote['message'] .= '[code]'.$inside[$i].'[/code]';
  474. }
  475. unset($inside);
  476. }
  477. if ($pun_config['o_censoring'] == '1')
  478. $cur_quote['message'] = censor_words($cur_quote['message']);
  479. $cur_quote['message'] = pun_htmlspecialchars($cur_quote['message']);
  480. if ($pun_config['p_message_bbcode'] == '1')
  481. {
  482. // If username contains a square bracket, we add "" or '' around it (so we know when it starts and ends)
  483. if (strpos($cur_quote['poster'], '[') !== false || strpos($cur_quote['poster'], ']') !== false)
  484. {
  485. if (strpos($cur_quote['poster'], '\'') !== false)
  486. $cur_quote['poster'] = '"'.$cur_quote['poster'].'"';
  487. else
  488. $cur_quote['poster'] = '\''.$cur_quote['poster'].'\'';
  489. }
  490. else
  491. {
  492. // Get the characters at the start and end of $cur_quote['poster']
  493. $ends = substr($cur_quote['poster'], 0, 1).substr($cur_quote['poster'], -1, 1);
  494. // Deal with quoting "Username" or 'Username' (becomes '"Username"' or "'Username'")
  495. if ($ends == '\'\'')
  496. $cur_quote['poster'] = '"'.$cur_quote['poster'].'"';
  497. else if ($ends == '""')
  498. $cur_quote['poster'] = '\''.$cur_quote['poster'].'\'';
  499. }
  500. $quote = '[quote='.$cur_quote['poster'].']'.$cur_quote['message'].'[/quote]'."\n";
  501. }
  502. else
  503. $quote = '> '.$cur_quote['poster'].' '.$lang->t('wrote')."\n\n".'> '.$cur_quote['message']."\n";
  504. }
  505. }
  506. // If a forum ID was specified in the url (new topic)
  507. else if ($fid)
  508. {
  509. $action = $lang->t('Post new topic');
  510. $form = '<form id="post" method="post" action="post.php?action=post&amp;fid='.$fid.'" onsubmit="return process_form(this)">';
  511. }
  512. else
  513. message($lang->t('Bad request'));
  514. $page_title = array(pun_htmlspecialchars($pun_config['o_board_title']), $action);
  515. $required_fields = array('req_email' => $lang->t('Email'), 'req_subject' => $lang->t('Subject'), 'req_message' => $lang->t('Message'));
  516. $focus_element = array('post');
  517. if (!$pun_user['is_guest'])
  518. $focus_element[] = ($fid) ? 'req_subject' : 'req_message';
  519. else
  520. {
  521. $required_fields['req_username'] = $lang->t('Guest name');
  522. $focus_element[] = 'req_username';
  523. }
  524. define('PUN_ACTIVE_PAGE', 'index');
  525. require PUN_ROOT.'header.php';
  526. ?>
  527. <div class="linkst">
  528. <div class="inbox">
  529. <ul class="crumbs">
  530. <li><a href="index.php"><?php echo $lang->t('Index') ?></a></li>
  531. <li><span>»&#160;</span><a href="viewforum.php?id=<?php echo $cur_posting['id'] ?>"><?php echo pun_htmlspecialchars($cur_posting['forum_name']) ?></a></li>
  532. <?php if (isset($cur_posting['subject'])): ?> <li><span>»&#160;</span><a href="viewtopic.php?id=<?php echo $tid ?>"><?php echo pun_htmlspecialchars($cur_posting['subject']) ?></a></li>
  533. <?php endif; ?> <li><span>»&#160;</span><strong><?php echo $action ?></strong></li>
  534. </ul>
  535. </div>
  536. </div>
  537. <?php
  538. // If there are errors, we display them
  539. if (!empty($errors))
  540. {
  541. ?>
  542. <div id="posterror" class="block">
  543. <h2><span><?php echo $lang->t('Post errors') ?></span></h2>
  544. <div class="box">
  545. <div class="inbox error-info">
  546. <p><?php echo $lang->t('Post errors info') ?></p>
  547. <ul class="error-list">
  548. <?php
  549. foreach ($errors as $cur_error)
  550. echo "\t\t\t\t".'<li><strong>'.$cur_error.'</strong></li>'."\n";
  551. ?>
  552. </ul>
  553. </div>
  554. </div>
  555. </div>
  556. <?php
  557. }
  558. else if (isset($_POST['preview']))
  559. {
  560. require_once PUN_ROOT.'include/parser.php';
  561. $preview_message = parse_message($message, $hide_smilies);
  562. ?>
  563. <div id="postpreview" class="blockpost">
  564. <h2><span><?php echo $lang->t('Post preview') ?></span></h2>
  565. <div class="box">
  566. <div class="inbox">
  567. <div class="postbody">
  568. <div class="postright">
  569. <div class="postmsg">
  570. <?php echo $preview_message."\n" ?>
  571. </div>
  572. </div>
  573. </div>
  574. </div>
  575. </div>
  576. </div>
  577. <?php
  578. }
  579. $cur_index = 1;
  580. ?>
  581. <div id="postform" class="blockform">
  582. <h2><span><?php echo $action ?></span></h2>
  583. <div class="box">
  584. <?php echo $form."\n" ?>
  585. <div class="inform">
  586. <fieldset>
  587. <legend><?php echo $lang->t('Write message legend') ?></legend>
  588. <div class="infldset txtarea">
  589. <input type="hidden" name="form_sent" value="1" />
  590. <?php
  591. if ($pun_user['is_guest'])
  592. {
  593. $email_label = ($pun_config['p_force_guest_email'] == '1') ? '<strong>'.$lang->t('Email').' <span>'.$lang->t('Required').'</span></strong>' : $lang->t('Email');
  594. $email_form_name = ($pun_config['p_force_guest_email'] == '1') ? 'req_email' : 'email';
  595. ?>
  596. <label class="conl required"><strong><?php echo $lang->t('Guest name') ?> <span><?php echo $lang->t('Required') ?></span></strong><br /><input type="text" name="req_username" value="<?php if (isset($_POST['req_username'])) echo pun_htmlspecialchars($username); ?>" size="25" maxlength="25" tabindex="<?php echo $cur_index++ ?>" /><br /></label>
  597. <label class="conl<?php echo ($pun_config['p_force_guest_email'] == '1') ? ' required' : '' ?>"><?php echo $email_label ?><br /><input type="text" name="<?php echo $email_form_name ?>" value="<?php if (isset($_POST[$email_form_name])) echo pun_htmlspecialchars($email); ?>" size="50" maxlength="80" tabindex="<?php echo $cur_index++ ?>" /><br /></label>
  598. <div class="clearer"></div>
  599. <?php
  600. }
  601. if ($fid): ?>
  602. <label class="required"><strong><?php echo $lang->t('Subject') ?> <span><?php echo $lang->t('Required') ?></span></strong><br /><input class="longinput" type="text" name="req_subject" value="<?php if (isset($_POST['req_subject'])) echo pun_htmlspecialchars($subject); ?>" size="80" maxlength="70" tabindex="<?php echo $cur_index++ ?>" /><br /></label>
  603. <?php endif; ?> <label class="required"><strong><?php echo $lang->t('Message') ?> <span><?php echo $lang->t('Required') ?></span></strong><br />
  604. <textarea name="req_message" rows="20" cols="95" tabindex="<?php echo $cur_index++ ?>"><?php echo isset($_POST['req_message']) ? pun_htmlspecialchars($orig_message) : (isset($quote) ? $quote : ''); ?></textarea><br /></label>
  605. <ul class="bblinks">
  606. <li><span><a href="help.php#bbcode" onclick="window.open(this.href); return false;"><?php echo $lang->t('BBCode') ?></a> <?php echo ($pun_config['p_message_bbcode'] == '1') ? $lang->t('on') : $lang->t('off'); ?></span></li>
  607. <li><span><a href="help.php#img" onclick="window.open(this.href); return false;"><?php echo $lang->t('img tag') ?></a> <?php echo ($pun_config['p_message_bbcode'] == '1' && $pun_config['p_message_img_tag'] == '1') ? $lang->t('on') : $lang->t('off'); ?></span></li>
  608. <li><span><a href="help.php#smilies" onclick="window.open(this.href); return false;"><?php echo $lang->t('Smilies') ?></a> <?php echo ($pun_config['o_smilies'] == '1') ? $lang->t('on') : $lang->t('off'); ?></span></li>
  609. </ul>
  610. </div>
  611. </fieldset>
  612. <?php
  613. $checkboxes = array();
  614. if ($is_admmod)
  615. $checkboxes[] = '<label><input type="checkbox" name="stick_topic" value="1" tabindex="'.($cur_index++).'"'.(isset($_POST['stick_topic']) ? ' checked="checked"' : '').' />'.$lang->t('Stick topic').'<br /></label>';
  616. if (!$pun_user['is_guest'])
  617. {
  618. if ($pun_config['o_smilies'] == '1')
  619. $checkboxes[] = '<label><input type="checkbox" name="hide_smilies" value="1" tabindex="'.($cur_index++).'"'.(isset($_POST['hide_smilies']) ? ' checked="checked"' : '').' />'.$lang->t('Hide smilies').'<br /></label>';
  620. if ($pun_config['o_topic_subscriptions'] == '1')
  621. {
  622. $subscr_checked = false;
  623. // If it's a preview
  624. if (isset($_POST['preview']))
  625. $subscr_checked = isset($_POST['subscribe']) ? true : false;
  626. // If auto subscribed
  627. else if ($pun_user['auto_notify'])
  628. $subscr_checked = true;
  629. // If already subscribed to the topic
  630. else if ($is_subscribed)
  631. $subscr_checked = true;
  632. $checkboxes[] = '<label><input type="checkbox" name="subscribe" value="1" tabindex="'.($cur_index++).'"'.($subscr_checked ? ' checked="checked"' : '').' />'.($is_subscribed ? $lang->t('Stay subscribed') : $lang->t('Subscribe')).'<br /></label>';
  633. }
  634. }
  635. else if ($pun_config['o_smilies'] == '1')
  636. $checkboxes[] = '<label><input type="checkbox" name="hide_smilies" value="1" tabindex="'.($cur_index++).'"'.(isset($_POST['hide_smilies']) ? ' checked="checked"' : '').' />'.$lang->t('Hide smilies').'<br /></label>';
  637. if (!empty($checkboxes))
  638. {
  639. ?>
  640. </div>
  641. <div class="inform">
  642. <fieldset>
  643. <legend><?php echo $lang->t('Options') ?></legend>
  644. <div class="infldset">
  645. <div class="rbox">
  646. <?php echo implode("\n\t\t\t\t\t\t\t", $checkboxes)."\n" ?>
  647. </div>
  648. </div>
  649. </fieldset>
  650. <?php
  651. }
  652. ?>
  653. </div>
  654. <p class="buttons"><input type="submit" name="submit" value="<?php echo $lang->t('Submit') ?>" tabindex="<?php echo $cur_index++ ?>" accesskey="s" /> <input type="submit" name="preview" value="<?php echo $lang->t('Preview') ?>" tabindex="<?php echo $cur_index++ ?>" accesskey="p" /> <a href="javascript:history.go(-1)"><?php echo $lang->t('Go back') ?></a></p>
  655. </form>
  656. </div>
  657. </div>
  658. <?php
  659. // Check to see if the topic review is to be displayed
  660. if ($tid && $pun_config['o_topic_review'] != '0')
  661. {
  662. require_once PUN_ROOT.'include/parser.php';
  663. ?>
  664. <div id="postreview">
  665. <h2><span><?php echo $lang->t('Topic review') ?></span></h2>
  666. <?php
  667. $query = $db->select(array('poster' => 'p.poster', 'message' => 'p.message', 'hide_smilies' => 'p.hide_smilies', 'posted' => 'p.posted'), 'posts AS p');
  668. $query->where = 'p.topic_id = :tid';
  669. $query->order = array('id' => 'p.id DESC');
  670. $query->limit = $pun_config['o_topic_review'];
  671. $params = array(':tid' => $tid);
  672. $result = $query->run($params);
  673. // Set background switching on
  674. $post_count = 0;
  675. foreach ($result as $cur_post)
  676. {
  677. $post_count++;
  678. $cur_post['message'] = parse_message($cur_post['message'], $cur_post['hide_smilies']);
  679. ?>
  680. <div class="blockpost">
  681. <div class="box<?php echo ($post_count % 2 == 0) ? ' roweven' : ' rowodd' ?>">
  682. <div class="inbox">
  683. <div class="postbody">
  684. <div class="postleft">
  685. <dl>
  686. <dt><strong><?php echo pun_htmlspecialchars($cur_post['poster']) ?></strong></dt>
  687. <dd><span><?php echo format_time($cur_post['posted']) ?></span></dd>
  688. </dl>
  689. </div>
  690. <div class="postright">
  691. <div class="postmsg">
  692. <?php echo $cur_post['message']."\n" ?>
  693. </div>
  694. </div>
  695. </div>
  696. <div class="clearer"></div>
  697. </div>
  698. </div>
  699. </div>
  700. <?php
  701. }
  702. unset ($result, $query, $params);
  703. ?>
  704. </div>
  705. <?php
  706. }
  707. require PUN_ROOT.'footer.php';