PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/nacridan/forum/edit.php

https://gitlab.com/nacridan/Nacridan
PHP | 291 lines | 213 code | 56 blank | 22 comment | 78 complexity | a02cf2169f703b2b6cce3e8362c7103d MD5 | raw file
  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_common['No view'], false, '403 Forbidden');
  11. $id = isset($_GET['id']) ? intval($_GET['id']) : 0;
  12. if ($id < 1)
  13. message($lang_common['Bad request'], false, '404 Not Found');
  14. // Fetch some info about the post, the topic and the forum
  15. $result = $db->query('SELECT f.id AS fid, f.forum_name, f.moderators, f.redirect_url, fp.post_replies, fp.post_topics, t.id AS tid, t.subject, t.posted, t.first_post_id, t.sticky, t.closed, p.poster, p.poster_id, p.message, p.hide_smilies FROM '.$db->prefix.'posts AS p INNER JOIN '.$db->prefix.'topics AS t ON t.id=p.topic_id 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='.$pun_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND p.id='.$id) or error('Unable to fetch post info', __FILE__, __LINE__, $db->error());
  16. if (!$db->num_rows($result))
  17. message($lang_common['Bad request'], false, '404 Not Found');
  18. $cur_post = $db->fetch_assoc($result);
  19. // Sort out who the moderators are and if we are currently a moderator (or an admin)
  20. $mods_array = ($cur_post['moderators'] != '') ? unserialize($cur_post['moderators']) : array();
  21. $is_admmod = ($pun_user['g_id'] == PUN_ADMIN || ($pun_user['g_moderator'] == '1' && array_key_exists($pun_user['username'], $mods_array))) ? true : false;
  22. $can_edit_subject = $id == $cur_post['first_post_id'];
  23. if ($pun_config['o_censoring'] == '1')
  24. {
  25. $cur_post['subject'] = censor_words($cur_post['subject']);
  26. $cur_post['message'] = censor_words($cur_post['message']);
  27. }
  28. // Do we have permission to edit this post?
  29. if (($pun_user['g_edit_posts'] == '0' ||
  30. $cur_post['poster_id'] != $pun_user['id'] ||
  31. $cur_post['closed'] == '1') &&
  32. !$is_admmod)
  33. message($lang_common['No permission'], false, '403 Forbidden');
  34. if ($is_admmod && $pun_user['g_id'] != PUN_ADMIN && in_array($cur_post['poster_id'], get_admin_ids()))
  35. message($lang_common['No permission'], false, '403 Forbidden');
  36. // Load the post.php language file
  37. require PUN_ROOT.'lang/'.$pun_user['language'].'/post.php';
  38. // Start with a clean slate
  39. $errors = array();
  40. if (isset($_POST['form_sent']))
  41. {
  42. // Make sure they got here from the site
  43. confirm_referrer('edit.php');
  44. // If it's a topic it must contain a subject
  45. if ($can_edit_subject)
  46. {
  47. $subject = pun_trim($_POST['req_subject']);
  48. if ($pun_config['o_censoring'] == '1')
  49. $censored_subject = pun_trim(censor_words($subject));
  50. if ($subject == '')
  51. $errors[] = $lang_post['No subject'];
  52. else if ($pun_config['o_censoring'] == '1' && $censored_subject == '')
  53. $errors[] = $lang_post['No subject after censoring'];
  54. else if (pun_strlen($subject) > 70)
  55. $errors[] = $lang_post['Too long subject'];
  56. else if ($pun_config['p_subject_all_caps'] == '0' && is_all_uppercase($subject) && !$pun_user['is_admmod'])
  57. $errors[] = $lang_post['All caps subject'];
  58. }
  59. // Clean up message from POST
  60. $message = pun_linebreaks(pun_trim($_POST['req_message']));
  61. // Here we use strlen() not pun_strlen() as we want to limit the post to PUN_MAX_POSTSIZE bytes, not characters
  62. if (strlen($message) > PUN_MAX_POSTSIZE)
  63. $errors[] = sprintf($lang_post['Too long message'], forum_number_format(PUN_MAX_POSTSIZE));
  64. else if ($pun_config['p_message_all_caps'] == '0' && is_all_uppercase($message) && !$pun_user['is_admmod'])
  65. $errors[] = $lang_post['All caps message'];
  66. // Validate BBCode syntax
  67. if ($pun_config['p_message_bbcode'] == '1')
  68. {
  69. require PUN_ROOT.'include/parser.php';
  70. $message = preparse_bbcode($message, $errors);
  71. }
  72. if (empty($errors))
  73. {
  74. if ($message == '')
  75. $errors[] = $lang_post['No message'];
  76. else if ($pun_config['o_censoring'] == '1')
  77. {
  78. // Censor message to see if that causes problems
  79. $censored_message = pun_trim(censor_words($message));
  80. if ($censored_message == '')
  81. $errors[] = $lang_post['No message after censoring'];
  82. }
  83. }
  84. $hide_smilies = isset($_POST['hide_smilies']) ? '1' : '0';
  85. $stick_topic = isset($_POST['stick_topic']) ? '1' : '0';
  86. if (!$is_admmod)
  87. $stick_topic = $cur_post['sticky'];
  88. // Replace four-byte characters (MySQL cannot handle them)
  89. $message = strip_bad_multibyte_chars($message);
  90. // Did everything go according to plan?
  91. if (empty($errors) && !isset($_POST['preview']))
  92. {
  93. $edited_sql = (!isset($_POST['silent']) || !$is_admmod) ? ', edited='.time().', edited_by=\''.$db->escape($pun_user['username']).'\'' : '';
  94. require PUN_ROOT.'include/search_idx.php';
  95. if ($can_edit_subject)
  96. {
  97. // Update the topic and any redirect topics
  98. $db->query('UPDATE '.$db->prefix.'topics SET subject=\''.$db->escape($subject).'\', sticky='.$stick_topic.' WHERE id='.$cur_post['tid'].' OR moved_to='.$cur_post['tid']) or error('Unable to update topic', __FILE__, __LINE__, $db->error());
  99. // We changed the subject, so we need to take that into account when we update the search words
  100. update_search_index('edit', $id, $message, $subject);
  101. }
  102. else
  103. update_search_index('edit', $id, $message);
  104. // Update the post
  105. $db->query('UPDATE '.$db->prefix.'posts SET message=\''.$db->escape($message).'\', hide_smilies='.$hide_smilies.$edited_sql.' WHERE id='.$id) or error('Unable to update post', __FILE__, __LINE__, $db->error());
  106. redirect('viewtopic.php?pid='.$id.'#p'.$id, $lang_post['Edit redirect']);
  107. }
  108. }
  109. $page_title = array(pun_htmlspecialchars($pun_config['o_board_title']), $lang_post['Edit post']);
  110. $required_fields = array('req_subject' => $lang_common['Subject'], 'req_message' => $lang_common['Message']);
  111. $focus_element = array('edit', 'req_message');
  112. define('PUN_ACTIVE_PAGE', 'index');
  113. require PUN_ROOT.'header.php';
  114. $cur_index = 1;
  115. ?>
  116. <div class="linkst">
  117. <div class="inbox">
  118. <ul class="crumbs">
  119. <li><a href="index.php"><?php echo $lang_common['Index'] ?></a></li>
  120. <li><span>»&#160;</span><a href="viewforum.php?id=<?php echo $cur_post['fid'] ?>"><?php echo pun_htmlspecialchars($cur_post['forum_name']) ?></a></li>
  121. <li><span>»&#160;</span><a href="viewtopic.php?id=<?php echo $cur_post['tid'] ?>"><?php echo pun_htmlspecialchars($cur_post['subject']) ?></a></li>
  122. <li><span>»&#160;</span><strong><?php echo $lang_post['Edit post'] ?></strong></li>
  123. </ul>
  124. </div>
  125. </div>
  126. <?php
  127. // If there are errors, we display them
  128. if (!empty($errors))
  129. {
  130. ?>
  131. <div id="posterror" class="block">
  132. <h2><span><?php echo $lang_post['Post errors'] ?></span></h2>
  133. <div class="box">
  134. <div class="inbox error-info">
  135. <p><?php echo $lang_post['Post errors info'] ?></p>
  136. <ul class="error-list">
  137. <?php
  138. foreach ($errors as $cur_error)
  139. echo "\t\t\t\t".'<li><strong>'.$cur_error.'</strong></li>'."\n";
  140. ?>
  141. </ul>
  142. </div>
  143. </div>
  144. </div>
  145. <?php
  146. }
  147. else if (isset($_POST['preview']))
  148. {
  149. require_once PUN_ROOT.'include/parser.php';
  150. $preview_message = parse_message($message, $hide_smilies);
  151. ?>
  152. <div id="postpreview" class="blockpost">
  153. <h2><span><?php echo $lang_post['Post preview'] ?></span></h2>
  154. <div class="box">
  155. <div class="inbox">
  156. <div class="postbody">
  157. <div class="postright">
  158. <div class="postmsg">
  159. <?php echo $preview_message."\n" ?>
  160. </div>
  161. </div>
  162. </div>
  163. </div>
  164. </div>
  165. </div>
  166. <?php
  167. }
  168. ?>
  169. <div id="editform" class="blockform">
  170. <h2><span><?php echo $lang_post['Edit post'] ?></span></h2>
  171. <div class="box">
  172. <form id="edit" method="post" action="edit.php?id=<?php echo $id ?>&amp;action=edit" onsubmit="return process_form(this)">
  173. <div class="inform">
  174. <fieldset>
  175. <legend><?php echo $lang_post['Edit post legend'] ?></legend>
  176. <input type="hidden" name="form_sent" value="1" />
  177. <div class="infldset txtarea">
  178. <?php if ($can_edit_subject): ?> <label class="required"><strong><?php echo $lang_common['Subject'] ?> <span><?php echo $lang_common['Required'] ?></span></strong><br />
  179. <input class="longinput" type="text" name="req_subject" size="80" maxlength="70" tabindex="<?php echo $cur_index++ ?>" value="<?php echo pun_htmlspecialchars(isset($_POST['req_subject']) ? $_POST['req_subject'] : $cur_post['subject']) ?>" /><br /></label>
  180. <?php endif; ?> <label class="required"><strong><?php echo $lang_common['Message'] ?> <span><?php echo $lang_common['Required'] ?></span></strong><br />
  181. <textarea name="req_message" rows="20" cols="95" tabindex="<?php echo $cur_index++ ?>"><?php echo pun_htmlspecialchars(isset($_POST['req_message']) ? $message : $cur_post['message']) ?></textarea><br /></label>
  182. <ul class="bblinks">
  183. <li><span><a href="help.php#bbcode" onclick="window.open(this.href); return false;"><?php echo $lang_common['BBCode'] ?></a> <?php echo ($pun_config['p_message_bbcode'] == '1') ? $lang_common['on'] : $lang_common['off']; ?></span></li>
  184. <li><span><a href="help.php#url" onclick="window.open(this.href); return false;"><?php echo $lang_common['url tag'] ?></a> <?php echo ($pun_config['p_message_bbcode'] == '1' && $pun_user['g_post_links'] == '1') ? $lang_common['on'] : $lang_common['off']; ?></span></li>
  185. <li><span><a href="help.php#img" onclick="window.open(this.href); return false;"><?php echo $lang_common['img tag'] ?></a> <?php echo ($pun_config['p_message_bbcode'] == '1' && $pun_config['p_message_img_tag'] == '1') ? $lang_common['on'] : $lang_common['off']; ?></span></li>
  186. <li><span><a href="help.php#smilies" onclick="window.open(this.href); return false;"><?php echo $lang_common['Smilies'] ?></a> <?php echo ($pun_config['o_smilies'] == '1') ? $lang_common['on'] : $lang_common['off']; ?></span></li>
  187. </ul>
  188. </div>
  189. </fieldset>
  190. <?php
  191. $checkboxes = array();
  192. if ($can_edit_subject && $is_admmod)
  193. {
  194. if (isset($_POST['stick_topic']) || $cur_post['sticky'] == '1')
  195. $checkboxes[] = '<label><input type="checkbox" name="stick_topic" value="1" checked="checked" tabindex="'.($cur_index++).'" />'.$lang_common['Stick topic'].'<br /></label>';
  196. else
  197. $checkboxes[] = '<label><input type="checkbox" name="stick_topic" value="1" tabindex="'.($cur_index++).'" />'.$lang_common['Stick topic'].'<br /></label>';
  198. }
  199. if ($pun_config['o_smilies'] == '1')
  200. {
  201. if (isset($_POST['hide_smilies']) || $cur_post['hide_smilies'] == '1')
  202. $checkboxes[] = '<label><input type="checkbox" name="hide_smilies" value="1" checked="checked" tabindex="'.($cur_index++).'" />'.$lang_post['Hide smilies'].'<br /></label>';
  203. else
  204. $checkboxes[] = '<label><input type="checkbox" name="hide_smilies" value="1" tabindex="'.($cur_index++).'" />'.$lang_post['Hide smilies'].'<br /></label>';
  205. }
  206. if ($is_admmod)
  207. {
  208. if ((isset($_POST['form_sent']) && isset($_POST['silent'])) || !isset($_POST['form_sent']))
  209. $checkboxes[] = '<label><input type="checkbox" name="silent" value="1" tabindex="'.($cur_index++).'" checked="checked" />'.$lang_post['Silent edit'].'<br /></label>';
  210. else
  211. $checkboxes[] = '<label><input type="checkbox" name="silent" value="1" tabindex="'.($cur_index++).'" />'.$lang_post['Silent edit'].'<br /></label>';
  212. }
  213. if (!empty($checkboxes))
  214. {
  215. ?>
  216. </div>
  217. <div class="inform">
  218. <fieldset>
  219. <legend><?php echo $lang_common['Options'] ?></legend>
  220. <div class="infldset">
  221. <div class="rbox">
  222. <?php echo implode("\n\t\t\t\t\t\t\t", $checkboxes)."\n" ?>
  223. </div>
  224. </div>
  225. </fieldset>
  226. <?php
  227. }
  228. ?>
  229. </div>
  230. <p class="buttons"><input type="submit" name="submit" value="<?php echo $lang_common['Submit'] ?>" tabindex="<?php echo $cur_index++ ?>" accesskey="s" /> <input type="submit" name="preview" value="<?php echo $lang_post['Preview'] ?>" tabindex="<?php echo $cur_index++ ?>" accesskey="p" /> <a href="javascript:history.go(-1)"><?php echo $lang_common['Go back'] ?></a></p>
  231. </form>
  232. </div>
  233. </div>
  234. <?php
  235. require PUN_ROOT.'footer.php';