PageRenderTime 6022ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/post.php

https://gitlab.com/LibreTitan/Panther
PHP | 788 lines | 597 code | 135 blank | 56 comment | 273 complexity | e7a32fa022c0a3f4589d99baf9932d0a MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright (C) 2015 Panther (https://www.pantherforum.org)
  4. * based on code by FluxBB copyright (C) 2008-2012 FluxBB
  5. * License: http://www.gnu.org/licenses/gpl.html GPL version 3 or higher
  6. */
  7. if (!defined('PANTHER'))
  8. {
  9. define('PANTHER_ROOT', __DIR__.'/');
  10. require PANTHER_ROOT.'include/common.php';
  11. }
  12. // Tell header.php we should use the editor
  13. define('POSTING', 1);
  14. if ($panther_user['g_read_board'] == '0')
  15. message($lang_common['No view'], false, '403 Forbidden');
  16. if ($panther_user['is_bot'])
  17. message($lang_common['No permission']);
  18. if ($panther_user['g_robot_test'] == '1')
  19. {
  20. if (file_exists(FORUM_CACHE_DIR.'cache_robots.php'))
  21. include FORUM_CACHE_DIR.'cache_robots.php';
  22. if (!defined('PANTHER_ROBOTS_LOADED'))
  23. {
  24. if (!defined('FORUM_CACHE_FUNCTIONS_LOADED'))
  25. require PANTHER_ROOT.'include/cache.php';
  26. generate_robots_cache();
  27. require FORUM_CACHE_DIR.'cache_robots.php';
  28. }
  29. }
  30. $tid = isset($_GET['tid']) ? intval($_GET['tid']) : 0;
  31. $fid = isset($_GET['fid']) ? intval($_GET['fid']) : 0;
  32. if ($tid < 1 && $fid < 1 || $tid > 0 && $fid > 0 || !$tid && !$fid)
  33. message($lang_common['Bad request'], false, '404 Not Found');
  34. $data = array(
  35. ':gid' => $panther_user['g_id'],
  36. );
  37. // Fetch some info about the topic and/or the forum
  38. if ($tid)
  39. {
  40. $data[':id'] = $panther_user['id'];
  41. $data[':tid'] = $tid;
  42. $ps = $db->run('SELECT f.id, f.forum_name, f.moderators, f.increment_posts, f.password, f.redirect_url, f.force_approve, fp.post_replies, fp.post_polls, fp.post_topics, fp.upload, t.subject, t.archived, t.closed, s.user_id AS is_subscribed FROM '.$db->prefix.'topics AS t INNER JOIN '.$db->prefix.'forums AS f ON f.id=t.forum_id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id=:gid) LEFT JOIN '.$db->prefix.'topic_subscriptions AS s ON (t.id=s.topic_id AND s.user_id=:id) WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND t.id=:tid', $data);
  43. }
  44. else
  45. {
  46. $data[':fid'] = $fid;
  47. $ps = $db->run('SELECT f.id, f.forum_name, f.moderators, f.increment_posts, f.password, f.redirect_url, f.force_approve, fp.post_replies, fp.post_polls, fp.post_topics, fp.upload, 0 AS archived FROM '.$db->prefix.'forums AS f LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id=:gid) WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND f.id=:fid', $data);
  48. }
  49. if (!$ps->rowCount())
  50. message($lang_common['Bad request'], false, '404 Not Found');
  51. $cur_posting = $ps->fetch();
  52. $is_subscribed = $tid && $cur_posting['is_subscribed'];
  53. // Is someone trying to post into a redirect forum?
  54. if ($cur_posting['redirect_url'] != '')
  55. message($lang_common['Bad request'], false, '404 Not Found');
  56. // Sort out who the moderators are and if we are currently a moderator (or an admin)
  57. $mods_array = ($cur_posting['moderators'] != '') ? unserialize($cur_posting['moderators']) : array();
  58. $is_admmod = ($panther_user['is_admin'] || ($panther_user['g_moderator'] == '1' && $panther_user['g_global_moderator'] || array_key_exists($panther_user['username'], $mods_array))) ? true : false;
  59. if ($tid && $panther_config['o_censoring'] == '1')
  60. $cur_posting['subject'] = censor_words($cur_posting['subject']);
  61. // Do we have permission to post?
  62. if ((($tid && (($cur_posting['post_replies'] == '' && $panther_user['g_post_replies'] == '0') || $cur_posting['post_replies'] == '0')) ||
  63. ($fid && (($cur_posting['post_topics'] == '' && $panther_user['g_post_topics'] == '0') || $cur_posting['post_topics'] == '0')) ||
  64. (isset($cur_posting['closed']) && $cur_posting['closed'] == '1')) &&
  65. !$is_admmod)
  66. message($lang_common['No permission'], false, '403 Forbidden');
  67. if ($cur_posting['password'] != '')
  68. {
  69. if ($fid)
  70. check_forum_login_cookie($fid, $cur_posting['password']);
  71. else
  72. check_forum_login_cookie($cur_posting['id'], $cur_posting['password']);
  73. }
  74. // Load the post.php language file
  75. require PANTHER_ROOT.'lang/'.$panther_user['language'].'/post.php';
  76. check_posting_ban();
  77. if ($cur_posting['archived'] == '1')
  78. message($lang_post['Topic archived']);
  79. // Start with a clean slate
  80. $errors = array();
  81. // Did someone just hit "Submit" or "Preview"?
  82. if (isset($_POST['form_sent']))
  83. {
  84. ($hook = get_extensions('post_before_validation')) ? eval($hook) : null;
  85. // Flood protection
  86. if (!isset($_POST['preview']) && $panther_user['last_post'] != '' && (time() - $panther_user['last_post']) < $panther_user['g_post_flood'])
  87. $errors[] = sprintf($lang_post['Flood start'], $panther_user['g_post_flood'], $panther_user['g_post_flood'] - (time() - $panther_user['last_post']));
  88. // Make sure they got here from the site
  89. confirm_referrer('post.php');
  90. // If it's a new topic
  91. if ($fid)
  92. {
  93. $subject = isset($_POST['req_subject']) ? panther_trim($_POST['req_subject']) : '';
  94. if ($panther_config['o_censoring'] == '1')
  95. $censored_subject = panther_trim(censor_words($subject));
  96. if ($subject == '')
  97. $errors[] = $lang_post['No subject'];
  98. else if ($panther_config['o_censoring'] == '1' && $censored_subject == '')
  99. $errors[] = $lang_post['No subject after censoring'];
  100. else if (panther_strlen($subject) > 70)
  101. $errors[] = $lang_post['Too long subject'];
  102. else if ($panther_config['p_subject_all_caps'] == '0' && is_all_uppercase($subject) && !$panther_user['is_admmod'])
  103. $errors[] = $lang_post['All caps subject'];
  104. }
  105. if (!empty($panther_robots) && $panther_user['g_robot_test'] == '1')
  106. {
  107. $id = isset($_POST['id']) ? intval($_POST['id']) : 0;
  108. $answer = isset($_POST['answer']) ? panther_trim($_POST['answer']) : '';
  109. if (!isset($panther_robots[$id]) || $answer != $panther_robots[$id]['answer'])
  110. $errors[] = $lang_common['Robot test fail'];
  111. }
  112. // If the user is logged in we get the username and email from $panther_user
  113. if (!$panther_user['is_guest'])
  114. {
  115. $username = $panther_user['username'];
  116. $email = $panther_user['email'];
  117. }
  118. // Otherwise it should be in $_POST
  119. else
  120. {
  121. $username = isset($_POST['req_username']) ? panther_trim($_POST['req_username']) : '';
  122. $email = strtolower(panther_trim(($panther_config['p_force_guest_email'] == '1') ? $_POST['req_email'] : $_POST['email']));
  123. $banned_email = false;
  124. // Load the register.php/prof_reg.php language files
  125. require PANTHER_ROOT.'lang/'.$panther_user['language'].'/prof_reg.php';
  126. require PANTHER_ROOT.'lang/'.$panther_user['language'].'/register.php';
  127. // It's a guest, so we have to validate the username
  128. check_username($username);
  129. if ($panther_config['p_force_guest_email'] == '1' || $email != '')
  130. {
  131. require PANTHER_ROOT.'include/email.php';
  132. if (!$mailer->is_valid_email($email))
  133. $errors[] = $lang_common['Invalid email'];
  134. // Check if it's a banned email address
  135. // we should only check guests because members' addresses are already verified
  136. if ($panther_user['is_guest'] && $mailer->is_banned_email($email))
  137. {
  138. if ($panther_config['p_allow_banned_email'] == '0')
  139. $errors[] = $lang_prof_reg['Banned email'];
  140. $banned_email = true; // Used later when we send an alert email
  141. }
  142. }
  143. }
  144. // Clean up message from POST
  145. $orig_message = $message = isset($_POST['req_message']) ? panther_linebreaks(panther_trim($_POST['req_message'])) : '';
  146. // Here we use strlen() not panther_strlen() as we want to limit the post to PANTHER_MAX_POSTSIZE bytes, not characters
  147. if (strlen($message) > PANTHER_MAX_POSTSIZE)
  148. $errors[] = sprintf($lang_post['Too long message'], forum_number_format(PANTHER_MAX_POSTSIZE));
  149. else if ($panther_config['p_message_all_caps'] == '0' && is_all_uppercase($message) && !$panther_user['is_admmod'])
  150. $errors[] = $lang_post['All caps message'];
  151. // Validate BBCode syntax
  152. if ($panther_config['p_message_bbcode'] == '1')
  153. {
  154. require PANTHER_ROOT.'include/parser.php';
  155. $message = $parser->preparse_bbcode($message, $errors);
  156. }
  157. if (empty($errors))
  158. {
  159. if ($message == '')
  160. $errors[] = $lang_post['No message'];
  161. else if ($panther_config['o_censoring'] == '1')
  162. {
  163. // Censor message to see if that causes problems
  164. $censored_message = panther_trim(censor_words($message));
  165. if ($censored_message == '')
  166. $errors[] = $lang_post['No message after censoring'];
  167. }
  168. }
  169. $hide_smilies = isset($_POST['hide_smilies']) ? '1' : '0';
  170. $subscribe = isset($_POST['subscribe']) ? '1' : '0';
  171. $stick_topic = isset($_POST['stick_topic']) && $is_admmod ? '1' : '0';
  172. $add_poll = isset($_POST['add_poll']) && $fid && $cur_posting['post_polls'] != '0' && $panther_user['g_post_polls'] == '1' && $panther_config['o_polls'] == '1' ? 1 : 0;
  173. $topic_approve = (!$is_admmod && ($cur_posting['force_approve'] == '1' || $cur_posting['force_approve'] == '3' || $panther_user['g_moderate_posts'] == '1')) ? 0 : 1;
  174. $post_approve = (!$is_admmod && ($cur_posting['force_approve'] == '2' || $cur_posting['force_approve'] == '3' || $panther_user['g_moderate_posts'] == '1')) ? 0 : 1;
  175. // Replace four-byte characters (MySQL cannot handle them)
  176. $message = strip_bad_multibyte_chars($message);
  177. $now = time();
  178. ($hook = get_extensions('post_after_validation')) ? eval($hook) : null;
  179. // Did everything go according to plan?
  180. if (empty($errors) && !isset($_POST['preview']))
  181. {
  182. require PANTHER_ROOT.'include/search_idx.php';
  183. // If it's a reply
  184. if ($tid)
  185. {
  186. if (!$panther_user['is_guest'])
  187. {
  188. $new_tid = $tid;
  189. // Insert the new post
  190. $insert = array(
  191. 'poster' => $username,
  192. 'poster_id' => $panther_user['id'],
  193. 'poster_ip' => get_remote_address(),
  194. 'message' => $message,
  195. 'hide_smilies' => $hide_smilies,
  196. 'posted' => $now,
  197. 'topic_id' => $tid,
  198. 'approved' => $post_approve,
  199. );
  200. $db->insert('posts', $insert);
  201. $new_pid = $db->lastInsertId($db->prefix.'posts');
  202. // To subscribe or not to subscribe, that ...
  203. if ($panther_config['o_topic_subscriptions'] == '1')
  204. {
  205. if ($subscribe && !$is_subscribed)
  206. {
  207. $data = array(
  208. 'user_id' => $panther_user['id'],
  209. 'topic_id' => $tid,
  210. );
  211. $db->insert('topic_subscriptions', $data);
  212. }
  213. else if (!$subscribe && $is_subscribed)
  214. {
  215. $data = array(
  216. ':id' => $panther_user['id'],
  217. ':tid' => $tid,
  218. );
  219. $db->delete('topic_subscriptions', 'user_id=:uid AND topic_id=:tid', $data);
  220. }
  221. }
  222. }
  223. else
  224. {
  225. // It's a guest. Insert the new post
  226. $insert_email = ($panther_config['p_force_guest_email'] == '1' || $email != '') ? $email : NULL;
  227. $insert = array(
  228. 'poster' => $username,
  229. 'poster_ip' => get_remote_address(),
  230. 'poster_email' => $insert_email,
  231. 'message' => $message,
  232. 'hide_smilies' => $hide_smilies,
  233. 'posted' => $now,
  234. 'topic_id' => $tid,
  235. 'approved' => $post_approve,
  236. );
  237. $db->insert('posts', $insert);
  238. $new_pid = $db->lastInsertId($db->prefix.'posts');
  239. }
  240. if ($post_approve == '1')
  241. {
  242. // Update topic
  243. $data = array(
  244. ':now' => $now,
  245. ':last_post_id' => $new_pid,
  246. ':last_poster' => $username,
  247. ':id' => $tid,
  248. );
  249. $db->run('UPDATE '.$db->prefix.'topics SET num_replies=num_replies+1, last_post=:now, last_post_id=:last_post_id, last_poster=:last_poster WHERE id=:id', $data);
  250. update_search_index('post', $new_pid, $message);
  251. update_forum($cur_posting['id']);
  252. require_once PANTHER_ROOT.'include/email.php';
  253. $cur_posting['message'] = ($panther_config['o_censoring'] == '1') ? $censored_message : $message;
  254. $mailer->handle_topic_subscriptions($tid, $cur_posting, $username, $new_pid);
  255. }
  256. else
  257. {
  258. require_once PANTHER_ROOT.'include/email.php';
  259. $info = array(
  260. 'message' => array(
  261. '<username>' => $username,
  262. '<topic_title>' => $cur_posting['subject'],
  263. '<post_url>' => panther_link($panther_url['admin_posts']),
  264. )
  265. );
  266. $mail_tpl = $mailer->parse(PANTHER_ROOT.'lang/'.$panther_user['language'].'/mail_templates/new_post.tpl', $info);
  267. $mailer->send($panther_config['o_mailing_list'], $mail_tpl['subject'], $mail_tpl['message']);
  268. }
  269. }
  270. // If it's a new topic
  271. else if ($fid)
  272. {
  273. // Create the topic
  274. $insert = array(
  275. 'poster' => $username,
  276. 'subject' => $subject,
  277. 'posted' => $now,
  278. 'last_post' => $now,
  279. 'last_poster' => $username,
  280. 'sticky' => $stick_topic,
  281. 'forum_id' => $fid,
  282. 'approved' => $topic_approve,
  283. );
  284. $db->insert('topics', $insert);
  285. $new_tid = $db->lastInsertId($db->prefix.'topics');
  286. if (!$panther_user['is_guest'])
  287. {
  288. // To subscribe or not to subscribe, that ...
  289. $data = array(
  290. 'user_id' => $panther_user['id'],
  291. 'topic_id' => $new_tid,
  292. );
  293. if ($panther_config['o_topic_subscriptions'] == '1' && $subscribe)
  294. $db->insert('topic_subscriptions', $data);
  295. // Create the post ("topic post")
  296. $insert = array(
  297. 'poster' => $username,
  298. 'poster_id' => $panther_user['id'],
  299. 'poster_ip' => get_remote_address(),
  300. 'message' => $message,
  301. 'hide_smilies' => $hide_smilies,
  302. 'posted' => $now,
  303. 'topic_id' => $new_tid,
  304. 'approved' => $topic_approve,
  305. );
  306. }
  307. else
  308. {
  309. $insert_email = ($panther_config['p_force_guest_email'] == '1' || $email != '') ? $email : NULL;
  310. // Create the post ("topic post")
  311. $insert = array(
  312. 'poster' => $username,
  313. 'poster_ip' => get_remote_address(),
  314. 'poster_email' => $insert_email,
  315. 'message' => $message,
  316. 'hide_smilies' => $hide_smilies,
  317. 'posted' => $now,
  318. 'topic_id' => $new_tid,
  319. 'approved' => $topic_approve,
  320. );
  321. }
  322. $db->insert('posts', $insert);
  323. $new_pid = $db->lastInsertId($db->prefix.'posts');
  324. // Update the topic with last_post_id
  325. $update = array(
  326. 'last_post_id' => $new_pid,
  327. 'first_post_id' => $new_pid,
  328. );
  329. $data = array(
  330. ':id' => $new_tid,
  331. );
  332. $db->update('topics', $update, 'id=:id', $data);
  333. if ($topic_approve)
  334. {
  335. update_search_index('post', $new_pid, $message, $subject);
  336. update_forum($fid);
  337. require_once PANTHER_ROOT.'include/email.php';
  338. $cur_posting['subject'] = ($panther_config['o_censoring'] == '1') ? $censored_subject : $subject;
  339. $cur_posting['message'] = ($panther_config['o_censoring'] == '1') ? $censored_message : $message;
  340. $mailer->handle_forum_subscriptions($cur_posting, $username, $new_tid);
  341. }
  342. else
  343. {
  344. require_once PANTHER_ROOT.'include/email.php';
  345. $info = array(
  346. 'message' => array(
  347. '<username>' => $panther_user['username'],
  348. '<forum_name>' => $cur_posting['forum_name'],
  349. '<post_url>' => panther_link($panther_url['admin_posts']),
  350. )
  351. );
  352. // Load the "new post" template
  353. $mail_tpl = $mailer->parse(PANTHER_ROOT.'lang/'.$panther_user['language'].'/mail_templates/new_post_topic.tpl', $info);
  354. $mailer->send($panther_config['o_mailing_list'], $mail_tpl['subject'], $mail_tpl['message']);
  355. }
  356. }
  357. // If we previously found out that the email was banned
  358. if ($panther_user['is_guest'] && $banned_email && $panther_config['o_mailing_list'] != '')
  359. {
  360. $info = array(
  361. 'message' => array(
  362. '<username>' => $username,
  363. '<email>' => $email,
  364. '<post_url>' => panther_link($panther_url['post'], array($new_pid)),
  365. )
  366. );
  367. $mail_tpl = $mailer->parse(PANTHER_ROOT.'lang/'.$panther_user['language'].'/mail_templates/banned_email_post.tpl', $info);
  368. $mailer->send($panther_config['o_mailing_list'], $mail_tpl['subject'], $mail_tpl['message']);
  369. }
  370. if (isset($_FILES['attached_file']))
  371. {
  372. if (isset($_FILES['attached_file']['error']) && $_FILES['attached_file']['error'] != 0 && $_FILES['attached_file']['error'] != 4)
  373. message(file_upload_error_message($_FILES['attached_file']['error']), __FILE__, __LINE__);
  374. if ($_FILES['attached_file']['size'] != 0 && is_uploaded_file($_FILES['attached_file']['tmp_name']))
  375. {
  376. $can_upload = false;
  377. if ($panther_user['is_admin'])
  378. $can_upload = true;
  379. else
  380. {
  381. $can_upload = ($panther_user['g_attach_files'] == '1' && ($cur_posting['upload'] == '1' || $cur_posting['upload'] == '')) ? true : false;
  382. $max_size = ($panther_user['g_max_size'] == '0' && $panther_user['g_attach_files'] == '1') ? $panther_config['o_max_upload_size'] : $panther_user['g_max_size'];
  383. if ($can_upload && $_FILES['attached_file']['size'] > $max_size)
  384. $can_upload = false;
  385. if (!check_file_extension($_FILES['attached_file']['name']))
  386. $can_upload = false;
  387. }
  388. if ($can_upload)
  389. {
  390. if (!create_attachment($_FILES['attached_file']['name'], $_FILES['attached_file']['type'], $_FILES['attached_file']['size'], $_FILES['attached_file']['tmp_name'], $new_pid, strlen($message)))
  391. message($lang_post['Attachment error']);
  392. }
  393. else // Remove file as it's either dangerous or they've attempted to URL hack. Either way, there's no need for it.
  394. unlink($_FILES['attached_file']['tmp_name']);
  395. }
  396. }
  397. // If the posting user is logged in, increment his/her post count
  398. if (!$panther_user['is_guest'])
  399. {
  400. if ($fid && $topic_approve == '1' || $tid && $post_approve == '1')
  401. {
  402. $data = array(
  403. ':id' => $panther_user['id'],
  404. ':last_post' => $now,
  405. );
  406. $update = ($cur_posting['increment_posts'] == '1') ? 'num_posts=num_posts+1, ' : '';
  407. $db->run('UPDATE '.$db->prefix.'users SET '.$update.'last_post=:last_post WHERE id=:id', $data);
  408. // Promote this user to a new group if enabled
  409. if ($panther_user['g_promote_next_group'] != 0 && $panther_user['num_posts'] + 1 >= $panther_user['g_promote_min_posts'] && $cur_posting['increment_posts'] == '1')
  410. {
  411. $update = array(
  412. 'group_id' => $panther_user['g_promote_next_group'],
  413. );
  414. $data = array(
  415. 'id' => $panther_user['id'],
  416. );
  417. $db->update('users', $update, 'id=:id', $data);
  418. }
  419. }
  420. else
  421. {
  422. $update = array(
  423. 'last_post' => $now,
  424. );
  425. $data = array(
  426. ':id' => $panther_user['id'],
  427. );
  428. $db->update('users', $update, 'id=:id', $data);
  429. }
  430. // Topic tracking stuff...
  431. $tracked_topics = get_tracked_topics();
  432. $tracked_topics['topics'][$new_tid] = time();
  433. set_tracked_topics($tracked_topics);
  434. }
  435. else
  436. {
  437. $update = array(
  438. 'last_post' => $now,
  439. );
  440. $data = array(
  441. ':ident' => get_remote_address(),
  442. );
  443. $db->update('online', $update, 'ident=:ident', $data);
  444. }
  445. ($hook = get_extensions('post_after_posted')) ? eval($hook) : null;
  446. if ($add_poll)
  447. $redirect = panther_link($panther_url['poll_add'], array($new_tid));
  448. switch (true)
  449. {
  450. case $fid && $topic_approve == '0':
  451. $redirect_lang = $lang_post['Topic moderation redirect'];
  452. if (!isset($redirect))
  453. $redirect = panther_link($panther_url['forum'], array($cur_posting['id'], url_friendly($subject)));
  454. break;
  455. case $tid && $post_approve == '0':
  456. $redirect_lang = $lang_post['Post moderation redirect'];
  457. if (!isset($redirect))
  458. $redirect = panther_link($panther_url['topic'], array($tid, url_friendly($cur_posting['subject'])));
  459. break;
  460. default:
  461. $redirect_lang = $lang_post['Post redirect'];
  462. if (!isset($redirect))
  463. $redirect = panther_link($panther_url['post'], array($new_pid));
  464. break;
  465. }
  466. redirect($redirect, $redirect_lang);
  467. }
  468. }
  469. // If a topic ID was specified in the url (it's a reply)
  470. if ($tid)
  471. {
  472. $post_link = panther_link($panther_url['new_reply'], array($tid));
  473. $action = $lang_post['Post a reply'];
  474. // If a quote ID was specified in the url
  475. if (isset($_GET['qid']))
  476. {
  477. $qid = intval($_GET['qid']);
  478. if ($qid < 1)
  479. message($lang_common['Bad request'], false, '404 Not Found');
  480. $data = array(
  481. ':id' => $qid,
  482. ':tid' => $tid,
  483. );
  484. $ps = $db->select('posts', 'poster, message', $data, 'id=:id AND topic_id=:tid');
  485. if (!$ps->rowCount())
  486. message($lang_common['Bad request'], false, '404 Not Found');
  487. list($q_poster, $q_message) = $ps->fetch(PDO::FETCH_NUM);
  488. // If the message contains a code tag we have to split it up (text within [code][/code] shouldn't be touched)
  489. if (strpos($q_message, '[code]') !== false && strpos($q_message, '[/code]') !== false)
  490. {
  491. list($inside, $outside) = split_text($q_message, '[code]', '[/code]');
  492. $q_message = implode("\1", $outside);
  493. }
  494. // Remove [img] tags from quoted message
  495. $q_message = preg_replace('%\[img(?:=(?:[^\[]*?))?\]((ht|f)tps?://)([^\s<"]*?)\[/img\]%U', '\1\3', $q_message);
  496. // If we split up the message before we have to concatenate it together again (code tags)
  497. if (isset($inside))
  498. {
  499. $outside = explode("\1", $q_message);
  500. $q_message = '';
  501. $num_tokens = count($outside);
  502. for ($i = 0; $i < $num_tokens; ++$i)
  503. {
  504. $q_message .= $outside[$i];
  505. if (isset($inside[$i]))
  506. $q_message .= '[code]'.$inside[$i].'[/code]';
  507. }
  508. unset($inside);
  509. }
  510. if ($panther_config['o_censoring'] == '1')
  511. $q_message = censor_words($q_message);
  512. if ($panther_config['p_message_bbcode'] == '1')
  513. {
  514. // If username contains a square bracket, we add "" or '' around it (so we know when it starts and ends)
  515. if (strpos($q_poster, '[') !== false || strpos($q_poster, ']') !== false)
  516. {
  517. if (strpos($q_poster, '\'') !== false)
  518. $q_poster = '"'.$q_poster.'"';
  519. else
  520. $q_poster = '\''.$q_poster.'\'';
  521. }
  522. else
  523. {
  524. // Get the characters at the start and end of $q_poster
  525. $ends = substr($q_poster, 0, 1).substr($q_poster, -1, 1);
  526. // Deal with quoting "Username" or 'Username' (becomes '"Username"' or "'Username'")
  527. if ($ends == '\'\'')
  528. $q_poster = '"'.$q_poster.'"';
  529. else if ($ends == '""')
  530. $q_poster = '\''.$q_poster.'\'';
  531. }
  532. $quote = '[quote='.$q_poster.']'.$q_message.'[/quote]'."\n";
  533. }
  534. else
  535. $quote = '> '.$q_poster.' '.$lang_common['wrote']."\n\n".'> '.$q_message."\n";
  536. }
  537. }
  538. else if ($fid) // If a forum ID was specified in the url (new topic)
  539. {
  540. $post_link = panther_link($panther_url['new_topic'], array($fid));
  541. $action = $lang_post['Post new topic'];
  542. }
  543. $page_title = array($panther_config['o_board_title'], $action);
  544. $required_fields = array('req_email' => $lang_common['Email'], 'req_subject' => $lang_common['Subject'], 'req_message' => $lang_common['Message']);
  545. $focus_element = array('post');
  546. if (!$panther_user['is_guest'])
  547. $focus_element[] = ($fid) ? 'req_subject' : 'req_message';
  548. else
  549. {
  550. $required_fields['req_username'] = $lang_post['Guest name'];
  551. $focus_element[] = 'req_username';
  552. }
  553. if (!empty($panther_robots) && $panther_user['g_robot_test'] == '1')
  554. $required_fields['answer'] = $lang_common['Robot title'];
  555. ($hook = get_extensions('post_before_header')) ? eval($hook) : null;
  556. $can_upload = false;
  557. if ($panther_user['is_admin'])
  558. $can_upload = true;
  559. else if ($panther_user['g_attach_files'] == '1' && ($cur_posting['upload'] == '1' || $cur_posting['upload'] == ''))
  560. $can_upload = true;
  561. $max_size = ($panther_user['g_max_size'] == '0' && $panther_user['g_attach_files'] == '1') ? $panther_config['o_max_upload_size'] : $panther_user['g_max_size'];
  562. define('PANTHER_ACTIVE_PAGE', 'index');
  563. require PANTHER_ROOT.'header.php';
  564. ($hook = get_extensions('post_after_header')) ? eval($hook) : null;
  565. $checkboxes = array();
  566. if ($fid && $is_admmod)
  567. $checkboxes[] = array('name' => 'stick_topic', 'checked' => (isset($_POST['stick_topic']) ? true : false), 'title' => $lang_common['Stick topic']);
  568. if ($fid && $cur_posting['post_polls'] != '0' && $panther_user['g_post_polls'] == '1' && $panther_config['o_polls'] == '1')
  569. $checkboxes[] = array('name' => 'add_poll', 'checked' => (isset($_POST['add_poll']) ? true : false), 'title' => $lang_post['Add poll']);
  570. if (!$panther_user['is_guest'])
  571. {
  572. if ($panther_config['o_smilies'] == '1')
  573. $checkboxes[] = array('name' => 'hide_smilies', 'checked' => (isset($_POST['hide_smilies']) ? true : false), 'title' => $lang_post['Hide smilies']);
  574. if ($panther_config['o_topic_subscriptions'] == '1')
  575. {
  576. $subscr_checked = false;
  577. // If it's a preview
  578. if (isset($_POST['preview']))
  579. $subscr_checked = isset($_POST['subscribe']) ? true : false;
  580. // If auto subscribed
  581. else if ($panther_user['auto_notify'])
  582. $subscr_checked = true;
  583. // If already subscribed to the topic
  584. else if ($is_subscribed)
  585. $subscr_checked = true;
  586. $checkboxes[] = array('name' => 'subscribe', 'checked' => (($subscr_checked) ? true : false), 'title' => (($is_subscribed ? $lang_post['Stay subscribed'] : $lang_post['Subscribe'])));
  587. }
  588. }
  589. else if ($panther_config['o_smilies'] == '1')
  590. $checkboxes[] = array('name' => 'hide_smilies', 'checked' => (isset($_POST['hide_smilies']) ? true : false), 'title' => $lang_post['Hide smilies']);
  591. // Check to see if the topic review is to be displayed
  592. $posts = array();
  593. if ($tid && $panther_config['o_topic_review'] != '0')
  594. {
  595. require_once PANTHER_ROOT.'include/parser.php';
  596. $data = array(
  597. ':id' => $tid,
  598. );
  599. $ps = $db->run('SELECT p.poster, p.message, p.hide_smilies, p.posted, u.group_id FROM '.$db->prefix.'posts AS p LEFT JOIN '.$db->prefix.'users AS u ON (p.poster=u.username) WHERE p.topic_id=:id ORDER BY p.id DESC LIMIT '.$panther_config['o_topic_review'], $data);
  600. foreach ($ps as $cur_post)
  601. $posts[] = array('username' => colourize_group($cur_post['poster'], $cur_post['group_id']), 'posted' => format_time($cur_post['posted']), 'message' => $parser->parse_message($cur_post['message'], $cur_post['hide_smilies']));
  602. }
  603. $render = array(
  604. 'lang_common' => $lang_common,
  605. 'lang_post' => $lang_post,
  606. 'posts' => $posts,
  607. 'errors' => $errors,
  608. 'index_link' => panther_link($panther_url['index']),
  609. 'forum_link' => panther_link($panther_url['forum'], array($cur_posting['id'], url_friendly($cur_posting['forum_name']))),
  610. 'cur_posting' => $cur_posting,
  611. 'POST' => $_POST,
  612. 'action' => $action,
  613. 'fid' => $fid,
  614. 'tid' => $tid,
  615. 'csrf_token' => generate_csrf_token(),
  616. 'panther_config' => $panther_config,
  617. 'message' => isset($_POST['req_message']) ? $orig_message : (isset($quote) ? $quote : ''),
  618. 'panther_user' => $panther_user,
  619. 'can_upload' => $can_upload,
  620. 'checkboxes' => $checkboxes,
  621. 'quickpost_links' => array(
  622. 'bbcode' => panther_link($panther_url['help'], array('bbcode')),
  623. 'url' => panther_link($panther_url['help'], array('url')),
  624. 'img' => panther_link($panther_url['help'], array('img')),
  625. 'smilies' => panther_link($panther_url['help'], array('smilies')),
  626. ),
  627. );
  628. if (isset($cur_posting['subject']))
  629. $render['topic_link'] = panther_link($panther_url['topic'], array($tid, url_friendly($cur_posting['subject'])));
  630. if (isset($_POST['preview']))
  631. {
  632. require_once PANTHER_ROOT.'include/parser.php';
  633. $render['preview'] = $parser->parse_message($message, $hide_smilies);
  634. }
  635. if ($panther_user['is_guest'])
  636. {
  637. $email_form_name = ($panther_config['p_force_guest_email'] == '1') ? 'req_email' : 'email';
  638. $render['username'] = (isset($username)) ? $username : '';
  639. $render['email'] = (isset($_POST[$email_form_name])) ? $email : '';
  640. $render['email_form_name'] = $email_form_name;
  641. }
  642. if ($can_upload)
  643. $render['max_size'] = $max_size;
  644. if (!empty($panther_robots) && $panther_user['g_robot_test'] == '1')
  645. {
  646. $id = array_rand($panther_robots);
  647. $render['robot_id'] = $id;
  648. $render['test'] = $panther_robots[$id];
  649. }
  650. ($hook = get_extensions('post_before_submit')) ? eval($hook) : null;
  651. $tpl = load_template('post.tpl');
  652. echo $tpl->render($render);
  653. require PANTHER_ROOT.'footer.php';