home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 April / PCWorld_2005-04_cd.bin / akce / web / unitednuke / unitednuke.exe / html / includes / functions_post.php < prev    next >
PHP Script  |  2004-01-10  |  44KB  |  899 lines

  1. <?php
  2. /***************************************************************************
  3.  *                            functions_post.php
  4.  *                            -------------------
  5.  *   begin                : Saturday, Feb 13, 2001
  6.  *   copyright            : (C) 2001 The phpBB Group
  7.  *   email                : support@phpbb.com
  8.  *
  9.  *   $Id: functions_post.php,v 1.9.2.35 2003/06/09 19:35:56 psotfx Exp $
  10.  *
  11.  *
  12.  ***************************************************************************/
  13. /***************************************************************************
  14. * phpbb2 forums port version 2.0.5 (c) 2003 - Nuke Cops (http://nukecops.com)
  15. *
  16. * Ported by Nuke Cops to phpbb2 standalone 2.0.5 Test
  17. * and debugging completed by the Elite Nukers and site members.
  18. *
  19. * You run this package at your sole risk. Nuke Cops and affiliates cannot
  20. * be held liable if anything goes wrong. You are advised to test this
  21. * package on a development system. Backup everything before implementing
  22. * in a production environment. If something goes wrong, you can always
  23. * backout and restore your backups.
  24. *
  25. * Installing and running this also means you agree to the terms of the AUP
  26. * found at Nuke Cops.
  27. *
  28. * This is version 2.0.5 of the phpbb2 forum port for PHP-Nuke. Work is based
  29. * on Tom Nitzschner's forum port version 2.0.6. Tom's 2.0.6 port was based
  30. * on the phpbb2 standalone version 2.0.3. Our version 2.0.5 from Nuke Cops is
  31. * now reflecting phpbb2 standalone 2.0.5 that fixes some bugs and the
  32. * invalid_session error message.
  33. ***************************************************************************/
  34. /***************************************************************************
  35.  *   This file is part of the phpBB2 port to Nuke 6.0 (c) copyright 2002
  36.  *   by Tom Nitzschner (tom@toms-home.com)
  37.  *   http://bbtonuke.sourceforge.net (or http://www.toms-home.com)
  38.  *
  39.  *   As always, make a backup before messing with anything. All code
  40.  *   release by me is considered sample code only. It may be fully
  41.  *   functual, but you use it at your own risk, if you break it,
  42.  *   you get to fix it too. No waranty is given or implied.
  43.  *
  44.  *   Please post all questions/request about this port on http://bbtonuke.sourceforge.net first,
  45.  *   then on my site. All original header code and copyright messages will be maintained
  46.  *   to give credit where credit is due. If you modify this, the only requirement is
  47.  *   that you also maintain all original copyright messages. All my work is released
  48.  *   under the GNU GENERAL PUBLIC LICENSE. Please see the README for more information.
  49.  *
  50.  ***************************************************************************/
  51. /***************************************************************************
  52.  *
  53.  *   This program is free software; you can redistribute it and/or modify
  54.  *   it under the terms of the GNU General Public License as published by
  55.  *   the Free Software Foundation; either version 2 of the License, or
  56.  *   (at your option) any later version.
  57.  *
  58.  ***************************************************************************/
  59.  
  60. if (!defined('IN_PHPBB'))
  61. {
  62.         die('Hacking attempt');
  63. }
  64.  
  65. $html_entities_match = array('#&(?!(\#[0-9]+;))#', '#<#', '#>#');
  66. $html_entities_replace = array('&', '<', '>');
  67.  
  68. $unhtml_specialchars_match = array('#>#', '#<#', '#"#', '#&#');
  69. $unhtml_specialchars_replace = array('>', '<', '"', '&');
  70.  
  71. //
  72. // This function will prepare a posted message for
  73. // entry into the database.
  74. //
  75. function prepare_message($message, $html_on, $bbcode_on, $smile_on, $bbcode_uid = 0)
  76. {
  77.         global $board_config, $html_entities_match, $html_entities_replace;
  78.  
  79.         //
  80.         // Clean up the message
  81.         //
  82.         $message = trim($message);
  83.  
  84.         if ($html_on)
  85.         {
  86.                 $allowed_html_tags = split(',', $board_config['allow_html_tags']);
  87.  
  88.                 $end_html = 0;
  89.                 $start_html = 1;
  90.                 $tmp_message = '';
  91.                 $message = ' ' . $message . ' ';
  92.  
  93.                 while ($start_html = strpos($message, '<', $start_html))
  94.                 {
  95.                         $tmp_message .= preg_replace($html_entities_match, $html_entities_replace, substr($message, $end_html + 1, ($start_html - $end_html - 1)));
  96.  
  97.                         if ($end_html = strpos($message, '>', $start_html))
  98.                         {
  99.                                 $length = $end_html - $start_html + 1;
  100.                                 $hold_string = substr($message, $start_html, $length);
  101.  
  102.                                 if (($unclosed_open = strrpos(' ' . $hold_string, '<')) != 1)
  103.                                 {
  104.                                         $tmp_message .= preg_replace($html_entities_match, $html_entities_replace, substr($hold_string, 0, $unclosed_open - 1));
  105.                                         $hold_string = substr($hold_string, $unclosed_open - 1);
  106.                                 }
  107.  
  108.                                 $tagallowed = false;
  109.                                 for ($i = 0; $i < sizeof($allowed_html_tags); $i++)
  110.                                 {
  111.                                         $match_tag = trim($allowed_html_tags[$i]);
  112.                                         if (preg_match('#^<\/?' . $match_tag . '[> ]#i', $hold_string))
  113.                                         {
  114.                                                 $tagallowed = (preg_match('#^<\/?' . $match_tag . ' .*?(style[ ]*?=|on[\w]+[ ]*?=)#i', $hold_string)) ? false : true;
  115.                                         }
  116.                                 }
  117.  
  118.                                 $tmp_message .= ($length && !$tagallowed) ? preg_replace($html_entities_match, $html_entities_replace, $hold_string) : $hold_string;
  119.  
  120.                                 $start_html += $length;
  121.                         }
  122.                         else
  123.                         {
  124.                                 $tmp_message .= preg_replace($html_entities_match, $html_entities_replace, substr($message, $start_html, strlen($message)));
  125.  
  126.                                 $start_html = strlen($message);
  127.                                 $end_html = $start_html;
  128.                         }
  129.                 }
  130.  
  131.                 if ($end_html != strlen($message) && $tmp_message != '')
  132.                 {
  133.                         $tmp_message .= preg_replace($html_entities_match, $html_entities_replace, substr($message, $end_html + 1));
  134.                 }
  135.  
  136.                 $message = ($tmp_message != '') ? trim($tmp_message) : trim($message);
  137.         }
  138.         else
  139.         {
  140.                 $message = preg_replace($html_entities_match, $html_entities_replace, $message);
  141.         }
  142.  
  143.         if($bbcode_on && $bbcode_uid != '')
  144.         {
  145.                 $message = bbencode_first_pass($message, $bbcode_uid);
  146.         }
  147.  
  148.         return $message;
  149. }
  150.  
  151. function unprepare_message($message)
  152. {
  153.         global $unhtml_specialchars_match, $unhtml_specialchars_replace;
  154.  
  155.         return preg_replace($unhtml_specialchars_match, $unhtml_specialchars_replace, $message);
  156. }
  157.  
  158. //
  159. // Prepare a message for posting
  160. //
  161. function prepare_post(&$mode, &$post_data, &$bbcode_on, &$html_on, &$smilies_on, &$error_msg, &$username, &$bbcode_uid, &$subject, &$message, &$poll_title, &$poll_options, &$poll_length)
  162. {
  163.         global $board_config, $userdata, $lang, $phpEx, $phpbb_root_path;
  164.  
  165.         // Check username
  166.         if (!empty($username))
  167.         {
  168.                 $username = trim(strip_tags($username));
  169.  
  170.                 if (!$userdata['session_logged_in'] || ($userdata['session_logged_in'] && $username != $userdata['username']))
  171.                 {
  172.                         include("includes/functions_validate.php");
  173.  
  174.                         $result = validate_username($username);
  175.                         if ($result['error'])
  176.                         {
  177.                                 $error_msg .= (!empty($error_msg)) ? '<br />' . $result['error_msg'] : $result['error_msg'];
  178.                         }
  179.                 }
  180.                 else
  181.                 {
  182.                         $username = '';
  183.                 }
  184.         }
  185.  
  186.         // Check subject
  187.         if (!empty($subject))
  188.         {
  189.                 $subject = htmlspecialchars(trim($subject));
  190.         }
  191.         else if ($mode == 'newtopic' || ($mode == 'editpost' && $post_data['first_post']))
  192.         {
  193.                 $error_msg .= (!empty($error_msg)) ? '<br />' . $lang['Empty_subject'] : $lang['Empty_subject'];
  194.         }
  195.  
  196.         // Check message
  197.         if (!empty($message))
  198.         {
  199.                 $bbcode_uid = ($bbcode_on) ? make_bbcode_uid() : '';
  200.                 $message = prepare_message(trim($message), $html_on, $bbcode_on, $smilies_on, $bbcode_uid);
  201.         }
  202.         else if ($mode != 'delete' && $mode != 'poll_delete')
  203.         {
  204.                 $error_msg .= (!empty($error_msg)) ? '<br />' . $lang['Empty_message'] : $lang['Empty_message'];
  205.         }
  206.  
  207.         //
  208.         // Handle poll stuff
  209.         //
  210.         if ($mode == 'newtopic' || ($mode == 'editpost' && $post_data['first_post']))
  211.         {
  212.                 $poll_length = (isset($poll_length)) ? max(0, intval($poll_length)) : 0;
  213.  
  214.                 if (!empty($poll_title))
  215.                 {
  216.                         $poll_title = htmlspecialchars(trim($poll_title));
  217.                 }
  218.  
  219.                 if(!empty($poll_options))
  220.                 {
  221.                         $temp_option_text = array();
  222.                         while(list($option_id, $option_text) = @each($poll_options))
  223.                         {
  224.                                 $option_text = trim($option_text);
  225.                                 if (!empty($option_text))
  226.                                 {
  227.                                         $temp_option_text[$option_id] = htmlspecialchars($option_text);
  228.                                 }
  229.                         }
  230.                         $option_text = $temp_option_text;
  231.  
  232.                         if (count($poll_options) < 2)
  233.                         {
  234.                                 $error_msg .= (!empty($error_msg)) ? '<br />' . $lang['To_few_poll_options'] : $lang['To_few_poll_options'];
  235.                         }
  236.                         else if (count($poll_options) > $board_config['max_poll_options'])
  237.                         {
  238.                                 $error_msg .= (!empty($error_msg)) ? '<br />' . $lang['To_many_poll_options'] : $lang['To_many_poll_options'];
  239.                         }
  240.                         else if ($poll_title == '')
  241.                         {
  242.                                 $error_msg .= (!empty($error_msg)) ? '<br />' . $lang['Empty_poll_title'] : $lang['Empty_poll_title'];
  243.                         }
  244.                 }
  245.         }
  246.  
  247.         return;
  248. }
  249.  
  250. //
  251. // Post a new topic/reply/poll or edit existing post/poll
  252. //
  253. function submit_post($mode, &$post_data, &$message, &$meta, &$forum_id, &$topic_id, &$post_id, &$poll_id, &$topic_type, &$bbcode_on, &$html_on, &$smilies_on, &$attach_sig, &$bbcode_uid, &$post_username, &$post_subject, &$post_message, &$poll_title, &$poll_options, &$poll_length)
  254. {
  255.         global $board_config, $lang, $db, $phpbb_root_path, $phpEx;
  256.         global $userdata, $user_ip;
  257.  
  258.         include("includes/functions_search.php");
  259.  
  260.         $current_time = time();
  261.  
  262.         if ($mode == 'newtopic' || $mode == 'reply' || $mode == 'editpost')
  263.         {
  264.                 //
  265.                 // Flood control
  266.                 //
  267.                 $where_sql = ($userdata['user_id'] == ANONYMOUS) ? "poster_ip = '$user_ip'" : 'poster_id = ' . $userdata['user_id'];
  268.                 $sql = "SELECT MAX(post_time) AS last_post_time
  269.                         FROM " . POSTS_TABLE . "
  270.                         WHERE $where_sql";
  271.                 if ($result = $db->sql_query($sql))
  272.                 {
  273.                         if ($row = $db->sql_fetchrow($result))
  274.                         {
  275.                                 if (intval($row['last_post_time']) > 0 && ($current_time - intval($row['last_post_time'])) < intval($board_config['flood_interval']))
  276.                                 {
  277.                                         message_die(GENERAL_MESSAGE, $lang['Flood_Error']);
  278.                                 }
  279.                         }
  280.                 }
  281.         }
  282.  
  283.         if ($mode == 'editpost')
  284.         {
  285.                 remove_search_post($post_id);
  286.         }
  287.  
  288.         if ($mode == 'newtopic' || ($mode == 'editpost' && $post_data['first_post']))
  289.         {
  290.                 $topic_vote = (!empty($poll_title) && count($poll_options) >= 2) ? 1 : 0;
  291.  
  292.                 $sql  = ($mode != "editpost") ? "INSERT INTO " . TOPICS_TABLE . " (topic_title, topic_poster, topic_time, forum_id, topic_status, topic_type, topic_vote) VALUES ('$post_subject', " . $userdata['user_id'] . ", $current_time, $forum_id, " . TOPIC_UNLOCKED . ", $topic_type, $topic_vote)" : "UPDATE " . TOPICS_TABLE . " SET topic_title = '$post_subject', topic_type = $topic_type " . (($post_data['edit_vote'] || !empty($poll_title)) ? ", topic_vote = " . $topic_vote : "") . " WHERE topic_id = $topic_id";
  293.                 if (!$db->sql_query($sql))
  294.                 {
  295.                         message_die(GENERAL_ERROR, 'Error in posting', '', __LINE__, __FILE__, $sql);
  296.                 }
  297.  
  298.                 if ($mode == 'newtopic')
  299.                 {
  300.                         $topic_id = $db->sql_nextid();
  301.                 }
  302.         }
  303.  
  304.         $edited_sql = ($mode == 'editpost' && !$post_data['last_post'] && $post_data['poster_post']) ? ", post_edit_time = $current_time, post_edit_count = post_edit_count + 1 " : "";
  305.         $sql = ($mode != "editpost") ? "INSERT INTO " . POSTS_TABLE . " (topic_id, forum_id, poster_id, post_username, post_time, poster_ip, enable_bbcode, enable_html, enable_smilies, enable_sig) VALUES ($topic_id, $forum_id, " . $userdata['user_id'] . ", '$post_username', $current_time, '$user_ip', $bbcode_on, $html_on, $smilies_on, $attach_sig)" : "UPDATE " . POSTS_TABLE . " SET post_username = '$post_username', enable_bbcode = $bbcode_on, enable_html = $html_on, enable_smilies = $smilies_on, enable_sig = $attach_sig" . $edited_sql . " WHERE post_id = $post_id";
  306.         if (!$db->sql_query($sql, BEGIN_TRANSACTION))
  307.         {
  308.                 message_die(GENERAL_ERROR, 'Error in posting', '', __LINE__, __FILE__, $sql);
  309.         }
  310.  
  311.         if ($mode != 'editpost')
  312.         {
  313.                 $post_id = $db->sql_nextid();
  314.         }
  315.  
  316.         $sql = ($mode != 'editpost') ? "INSERT INTO " . POSTS_TEXT_TABLE . " (post_id, post_subject, bbcode_uid, post_text) VALUES ($post_id, '$post_subject', '$bbcode_uid', '$post_message')" : "UPDATE " . POSTS_TEXT_TABLE . " SET post_text = '$post_message',  bbcode_uid = '$bbcode_uid', post_subject = '$post_subject' WHERE post_id = $post_id";
  317.         if (!$db->sql_query($sql))
  318.         {
  319.                 message_die(GENERAL_ERROR, 'Error in posting', '', __LINE__, __FILE__, $sql);
  320.         }
  321.  
  322.         add_search_words('single', $post_id, stripslashes($post_message), stripslashes($post_subject));
  323.  
  324.         //
  325.         // Add poll
  326.         //
  327.         if (($mode == 'newtopic' || ($mode == 'editpost' && $post_data['edit_poll'])) && !empty($poll_title) && count($poll_options) >= 2)
  328.         {
  329.                 $sql = (!$post_data['has_poll']) ? "INSERT INTO " . VOTE_DESC_TABLE . " (topic_id, vote_text, vote_start, vote_length) VALUES ($topic_id, '$poll_title', $current_time, " . ($poll_length * 86400) . ")" : "UPDATE " . VOTE_DESC_TABLE . " SET vote_text = '$poll_title', vote_length = " . ($poll_length * 86400) . " WHERE topic_id = $topic_id";
  330.                 if (!$db->sql_query($sql))
  331.                 {
  332.                         message_die(GENERAL_ERROR, 'Error in posting', '', __LINE__, __FILE__, $sql);
  333.                 }
  334.  
  335.                 $delete_option_sql = '';
  336.                 $old_poll_result = array();
  337.                 if ($mode == 'editpost' && $post_data['has_poll'])
  338.                 {
  339.                         $sql = "SELECT vote_option_id, vote_result
  340.                                 FROM " . VOTE_RESULTS_TABLE . "
  341.                                 WHERE vote_id = $poll_id
  342.                                 ORDER BY vote_option_id ASC";
  343.                         if (!($result = $db->sql_query($sql)))
  344.                         {
  345.                                 message_die(GENERAL_ERROR, 'Could not obtain vote data results for this topic', '', __LINE__, __FILE__, $sql);
  346.                         }
  347.  
  348.                         while ($row = $db->sql_fetchrow($result))
  349.                         {
  350.                                 $old_poll_result[$row['vote_option_id']] = $row['vote_result'];
  351.  
  352.                                 if (!isset($poll_options[$row['vote_option_id']]))
  353.                                 {
  354.                                         $delete_option_sql .= ($delete_option_sql != '') ? ', ' . $row['vote_option_id'] : $row['vote_option_id'];
  355.                                 }
  356.                         }
  357.                 }
  358.                 else
  359.                 {
  360.                         $poll_id = $db->sql_nextid();
  361.                 }
  362.  
  363.                 @reset($poll_options);
  364.  
  365.                 $poll_option_id = 1;
  366.                 while (list($option_id, $option_text) = each($poll_options))
  367.                 {
  368.                         if (!empty($option_text))
  369.                         {
  370.                                 $option_text = str_replace("\'", "''", htmlspecialchars($option_text));
  371.                                 $poll_result = ($mode == "editpost" && isset($old_poll_result[$option_id])) ? $old_poll_result[$option_id] : 0;
  372.  
  373.                                 $sql = ($mode != "editpost" || !isset($old_poll_result[$option_id])) ? "INSERT INTO " . VOTE_RESULTS_TABLE . " (vote_id, vote_option_id, vote_option_text, vote_result) VALUES ($poll_id, $poll_option_id, '$option_text', $poll_result)" : "UPDATE " . VOTE_RESULTS_TABLE . " SET vote_option_text = '$option_text', vote_result = $poll_result WHERE vote_option_id = $option_id AND vote_id = $poll_id";
  374.                                 if (!$db->sql_query($sql))
  375.                                 {
  376.                                         message_die(GENERAL_ERROR, 'Error in posting', '', __LINE__, __FILE__, $sql);
  377.                                 }
  378.                                 $poll_option_id++;
  379.                         }
  380.                 }
  381.  
  382.                 if ($delete_option_sql != '')
  383.                 {
  384.                         $sql = "DELETE FROM " . VOTE_RESULTS_TABLE . "
  385.                                 WHERE vote_option_id IN ($delete_option_sql)
  386.                                         AND vote_id = $poll_id";
  387.                         if (!$db->sql_query($sql))
  388.                         {
  389.                                 message_die(GENERAL_ERROR, 'Error deleting pruned poll options', '', __LINE__, __FILE__, $sql);
  390.                         }
  391.                 }
  392.         }
  393.  
  394.         $meta = '<meta http-equiv="refresh" content="3;url=' . append_sid("viewtopic.$phpEx?" . POST_POST_URL . "=" . $post_id) . '#' . $post_id . '">';
  395.         $message = $lang['Stored'] . '<br /><br />' . sprintf($lang['Click_view_message'], '<a href="' . append_sid("viewtopic.$phpEx?" . POST_POST_URL . "=" . $post_id) . '#' . $post_id . '">', '</a>') . '<br /><br />' . sprintf($lang['Click_return_forum'], '<a href="' . append_sid("viewforum.$phpEx?" . POST_FORUM_URL . "=$forum_id") . '">', '</a>');
  396.  
  397.         return false;
  398. }
  399.  
  400. //
  401. // Update post stats and details
  402. //
  403. function update_post_stats(&$mode, &$post_data, &$forum_id, &$topic_id, &$post_id, &$user_id)
  404. {
  405.         global $db;
  406.  
  407.         $sign = ($mode == 'delete') ? '- 1' : '+ 1';
  408.         $forum_update_sql = "forum_posts = forum_posts $sign";
  409.         $topic_update_sql = '';
  410.  
  411.         if ($mode == 'delete')
  412.         {
  413.                 if ($post_data['last_post'])
  414.                 {
  415.                         if ($post_data['first_post'])
  416.                         {
  417.                                 $forum_update_sql .= ', forum_topics = forum_topics - 1';
  418.                         }
  419.                         else
  420.                         {
  421.  
  422.                                 $topic_update_sql .= 'topic_replies = topic_replies - 1';
  423.  
  424.                                 $sql = "SELECT MAX(post_id) AS last_post_id
  425.                                         FROM " . POSTS_TABLE . "
  426.                                         WHERE topic_id = $topic_id";
  427.                                 if (!($result = $db->sql_query($sql)))
  428.                                 {
  429.                                         message_die(GENERAL_ERROR, 'Error in deleting post', '', __LINE__, __FILE__, $sql);
  430.                                 }
  431.  
  432.                                 if ($row = $db->sql_fetchrow($result))
  433.                                 {
  434.                                         $topic_update_sql .= ', topic_last_post_id = ' . $row['last_post_id'];
  435.                                 }
  436.                         }
  437.  
  438.                         if ($post_data['last_topic'])
  439.                         {
  440.                                 $sql = "SELECT MAX(post_id) AS last_post_id
  441.                                         FROM " . POSTS_TABLE . "
  442.                                         WHERE forum_id = $forum_id";
  443.                                 if (!($result = $db->sql_query($sql)))
  444.                                 {
  445.                                         message_die(GENERAL_ERROR, 'Error in deleting post', '', __LINE__, __FILE__, $sql);
  446.                                 }
  447.  
  448.                                 if ($row = $db->sql_fetchrow($result))
  449.                                 {
  450.                                         $forum_update_sql .= ($row['last_post_id']) ? ', forum_last_post_id = ' . $row['last_post_id'] : ', forum_last_post_id = 0';
  451.                                 }
  452.                         }
  453.                 }
  454.                 else if ($post_data['first_post'])
  455.                 {
  456.                         $sql = "SELECT MIN(post_id) AS first_post_id
  457.                                 FROM " . POSTS_TABLE . "
  458.                                 WHERE topic_id = $topic_id";
  459.                         if (!($result = $db->sql_query($sql)))
  460.                         {
  461.                                 message_die(GENERAL_ERROR, 'Error in deleting post', '', __LINE__, __FILE__, $sql);
  462.                         }
  463.  
  464.                         if ($row = $db->sql_fetchrow($result))
  465.                         {
  466.                                 $topic_update_sql .= 'topic_replies = topic_replies - 1, topic_first_post_id = ' . $row['first_post_id'];
  467.                         }
  468.                 }
  469.                 else
  470.                 {
  471.                         $topic_update_sql .= 'topic_replies = topic_replies - 1';
  472.                 }
  473.         }
  474.         else if ($mode != 'poll_delete')
  475.         {
  476.                 $forum_update_sql .= ", forum_last_post_id = $post_id" . (($mode == 'newtopic') ? ", forum_topics = forum_topics $sign" : "");
  477.                 $topic_update_sql = "topic_last_post_id = $post_id" . (($mode == 'reply') ? ", topic_replies = topic_replies $sign" : ", topic_first_post_id = $post_id");
  478.         }
  479.         else
  480.         {
  481.                 $topic_update_sql .= 'topic_vote = 0';
  482.         }
  483.  
  484.         $sql = "UPDATE " . FORUMS_TABLE . " SET
  485.                 $forum_update_sql
  486.                 WHERE forum_id = $forum_id";
  487.         if (!$db->sql_query($sql))
  488.         {
  489.                 message_die(GENERAL_ERROR, 'Error in posting', '', __LINE__, __FILE__, $sql);
  490.         }
  491.  
  492.         if ($topic_update_sql != '')
  493.         {
  494.                 $sql = "UPDATE " . TOPICS_TABLE . " SET
  495.                         $topic_update_sql
  496.                         WHERE topic_id = $topic_id";
  497.                 if (!$db->sql_query($sql))
  498.                 {
  499.                         message_die(GENERAL_ERROR, 'Error in posting', '', __LINE__, __FILE__, $sql);
  500.                 }
  501.         }
  502.  
  503.         if ($mode != 'poll_delete')
  504.         {
  505.                 $sql = "UPDATE " . USERS_TABLE . "
  506.                         SET user_posts = user_posts $sign
  507.                         WHERE user_id = $user_id";
  508.                 if (!$db->sql_query($sql, END_TRANSACTION))
  509.                 {
  510.                         message_die(GENERAL_ERROR, 'Error in posting', '', __LINE__, __FILE__, $sql);
  511.                 }
  512.         }
  513.  
  514.         return;
  515. }
  516.  
  517. //
  518. // Delete a post/poll
  519. //
  520. function delete_post($mode, &$post_data, &$message, &$meta, &$forum_id, &$topic_id, &$post_id, &$poll_id)
  521. {
  522.         global $board_config, $lang, $db, $phpbb_root_path, $phpEx;
  523.         global $userdata, $user_ip;
  524.  
  525.         if ($mode != 'poll_delete')
  526.         {
  527.         include("includes/functions_search.php");
  528.  
  529.                 $sql = "DELETE FROM " . POSTS_TABLE . "
  530.                         WHERE post_id = $post_id";
  531.                 if (!$db->sql_query($sql))
  532.                 {
  533.                         message_die(GENERAL_ERROR, 'Error in deleting post', '', __LINE__, __FILE__, $sql);
  534.                 }
  535.  
  536.                 $sql = "DELETE FROM " . POSTS_TEXT_TABLE . "
  537.                         WHERE post_id = $post_id";
  538.                 if (!$db->sql_query($sql))
  539.                 {
  540.                         message_die(GENERAL_ERROR, 'Error in deleting post', '', __LINE__, __FILE__, $sql);
  541.                 }
  542.  
  543.                 if ($post_data['last_post'])
  544.                 {
  545.                         if ($post_data['first_post'])
  546.                         {
  547.                                 $forum_update_sql .= ', forum_topics = forum_topics - 1';
  548.                                 $sql = "DELETE FROM " . TOPICS_TABLE . "
  549.                                         WHERE topic_id = $topic_id
  550.                                                 OR topic_moved_id = $topic_id";
  551.                                 if (!$db->sql_query($sql))
  552.                                 {
  553.                                         message_die(GENERAL_ERROR, 'Error in deleting post', '', __LINE__, __FILE__, $sql);
  554.                                 }
  555.  
  556.                                 $sql = "DELETE FROM " . TOPICS_WATCH_TABLE . "
  557.                                         WHERE topic_id = $topic_id";
  558.                                 if (!$db->sql_query($sql))
  559.                                 {
  560.                                         message_die(GENERAL_ERROR, 'Error in deleting post', '', __LINE__, __FILE__, $sql);
  561.                                 }
  562.                         }
  563.                 }
  564.  
  565.                 remove_search_post($post_id);
  566.         }
  567.  
  568.         if ($mode == 'poll_delete' || ($mode == 'delete' && $post_data['first_post'] && $post_data['last_post']) && $post_data['has_poll'] && $post_data['edit_poll'])
  569.         {
  570.                 $sql = "DELETE FROM " . VOTE_DESC_TABLE . "
  571.                         WHERE topic_id = $topic_id";
  572.                 if (!$db->sql_query($sql))
  573.                 {
  574.                         message_die(GENERAL_ERROR, 'Error in deleting poll', '', __LINE__, __FILE__, $sql);
  575.                 }
  576.  
  577.                 $sql = "DELETE FROM " . VOTE_RESULTS_TABLE . "
  578.                         WHERE vote_id = $poll_id";
  579.                 if (!$db->sql_query($sql))
  580.                 {
  581.                         message_die(GENERAL_ERROR, 'Error in deleting poll', '', __LINE__, __FILE__, $sql);
  582.                 }
  583.  
  584.                 $sql = "DELETE FROM " . VOTE_USERS_TABLE . "
  585.                         WHERE vote_id = $poll_id";
  586.                 if (!$db->sql_query($sql))
  587.                 {
  588.                         message_die(GENERAL_ERROR, 'Error in deleting poll', '', __LINE__, __FILE__, $sql);
  589.                 }
  590.         }
  591.  
  592.         if ($mode == 'delete' && $post_data['first_post'] && $post_data['last_post'])
  593.         {
  594.                 $meta = '<meta http-equiv="refresh" content="3;url=' . append_sid("viewforum.$phpEx?" . POST_FORUM_URL . '=' . $forum_id) . '">';
  595.                 $message = $lang['Deleted'];
  596.         }
  597.         else
  598.         {
  599.                 $meta = '<meta http-equiv="refresh" content="3;url=' . append_sid("viewtopic.$phpEx?" . POST_TOPIC_URL . '=' . $topic_id) . '">';
  600.                 $message = (($mode == 'poll_delete') ? $lang['Poll_delete'] : $lang['Deleted']) . '<br /><br />' . sprintf($lang['Click_return_topic'], '<a href="' . append_sid("viewtopic.$phpEx?" . POST_TOPIC_URL . "=$topic_id") . '">', '</a>');
  601.         }
  602.  
  603.         $message .=  '<br /><br />' . sprintf($lang['Click_return_forum'], '<a href="' . append_sid("viewforum.$phpEx?" . POST_FORUM_URL . "=$forum_id") . '">', '</a>');
  604.  
  605.         return;
  606. }
  607.  
  608. //
  609. // Handle user notification on new post
  610. //
  611. function user_notification($mode, &$post_data, &$topic_title, &$forum_id, &$topic_id, &$post_id, &$notify_user)
  612. {
  613.         global $board_config, $lang, $db, $phpbb_root_path, $phpEx;
  614.         global $userdata, $user_ip;
  615.  
  616.         $current_time = time();
  617.  
  618.         if ($mode == 'delete')
  619.         {
  620.                 $delete_sql = (!$post_data['first_post'] && !$post_data['last_post']) ? " AND user_id = " . $userdata['user_id'] : '';
  621.                 $sql = "DELETE FROM " . TOPICS_WATCH_TABLE . " WHERE topic_id = $topic_id" . $delete_sql;
  622.                 if (!$db->sql_query($sql))
  623.                 {
  624.                         message_die(GENERAL_ERROR, 'Could not change topic notify data', '', __LINE__, __FILE__, $sql);
  625.                 }
  626.         }
  627.         else
  628.         {
  629.                 if ($mode == 'reply')
  630.                 {
  631.                         $sql = "SELECT ban_userid
  632.                                 FROM " . BANLIST_TABLE;
  633.                         if (!($result = $db->sql_query($sql)))
  634.                         {
  635.                                 message_die(GENERAL_ERROR, 'Could not obtain banlist', '', __LINE__, __FILE__, $sql);
  636.                         }
  637.  
  638.                         $user_id_sql = '';
  639.                         while ($row = $db->sql_fetchrow($result))
  640.                         {
  641.                                 if (isset($row['ban_userid']) && !empty($row['ban_userid']))
  642.                                 {
  643.                                         $user_id_sql .= ', ' . $row['ban_userid'];
  644.                                 }
  645.                         }
  646.  
  647.                         $sql = "SELECT u.user_id, u.user_email, u.user_lang
  648.                                 FROM " . TOPICS_WATCH_TABLE . " tw, " . USERS_TABLE . " u
  649.                                 WHERE tw.topic_id = $topic_id
  650.                                         AND tw.user_id NOT IN (" . $userdata['user_id'] . ", " . ANONYMOUS . $user_id_sql . ")
  651.                                         AND tw.notify_status = " . TOPIC_WATCH_UN_NOTIFIED . "
  652.                                         AND u.user_id = tw.user_id";
  653.                         if (!($result = $db->sql_query($sql)))
  654.                         {
  655.                                 message_die(GENERAL_ERROR, 'Could not obtain list of topic watchers', '', __LINE__, __FILE__, $sql);
  656.                         }
  657.  
  658.                         $update_watched_sql = '';
  659.                         $bcc_list_ary = array();
  660.  
  661.                         if ($row = $db->sql_fetchrow($result))
  662.                         {
  663.                                 // Sixty second limit
  664.                                 @set_time_limit(60);
  665.  
  666.                                 do
  667.                                 {
  668.                                         if ($row['user_email'] != '')
  669.                                         {
  670.                                                 $bcc_list_ary[$row['user_lang']][] = $row['user_email'];
  671.                                         }
  672.                                         $update_watched_sql .= ($update_watched_sql != '') ? ', ' . $row['user_id'] : $row['user_id'];
  673.                                 }
  674.                                 while ($row = $db->sql_fetchrow($result));
  675.  
  676.                                 //
  677.                                 // Let's do some checking to make sure that mass mail functions
  678.                                 // are working in win32 versions of php.
  679.                                 //
  680.                                 if (preg_match('/[c-z]:\\\.*/i', getenv('PATH')) && !$board_config['smtp_delivery'])
  681.                                 {
  682.                                         $ini_val = (@phpversion() >= '4.0.0') ? 'ini_get' : 'get_cfg_var';
  683.  
  684.                                         // We are running on windows, force delivery to use our smtp functions
  685.                                         // since php's are broken by default
  686.                                         $board_config['smtp_delivery'] = 1;
  687.                                         $board_config['smtp_host'] = @$ini_val('SMTP');
  688.                                 }
  689.  
  690.                                 if (sizeof($bcc_list_ary))
  691.                                 {
  692.                                         include("includes/emailer.php");
  693.                                         $emailer = new emailer($board_config['smtp_delivery']);
  694.  
  695.                                         $script_name = preg_replace('/^\/?(.*?)\/?$/', '\1', trim($board_config['script_path']));
  696.                         $script_name = 'modules.php?name=Forums&file=viewtopic';
  697.                                         $server_name = trim($board_config['server_name']);
  698.                                         $server_protocol = ($board_config['cookie_secure']) ? 'https://' : 'http://';
  699.                                         $server_port = ($board_config['server_port'] <> 80) ? ':' . trim($board_config['server_port']) . '/' : '/';
  700.  
  701.                                         $orig_word = array();
  702.                                         $replacement_word = array();
  703.                                         obtain_word_list($orig_word, $replacement_word);
  704.  
  705.                                         $emailer->from($board_config['board_email']);
  706.                                         $emailer->replyto($board_config['board_email']);
  707.  
  708.                                         $topic_title = (count($orig_word)) ? preg_replace($orig_word, $replacement_word, unprepare_message($topic_title)) : unprepare_message($topic_title);
  709.  
  710.                                         @reset($bcc_list_ary);
  711.                                         while (list($user_lang, $bcc_list) = each($bcc_list_ary))
  712.                                         {
  713.                                                 $emailer->use_template('topic_notify', $user_lang);
  714.  
  715.                                                 for ($i = 0; $i < count($bcc_list); $i++)
  716.                                                 {
  717.                                                         $emailer->bcc($bcc_list[$i]);
  718.                                                 }
  719.  
  720.                                                 // The Topic_reply_notification lang string below will be used
  721.                                                 // if for some reason the mail template subject cannot be read
  722.                                                 // ... note it will not necessarily be in the posters own language!
  723.                                                 $emailer->set_subject($lang['Topic_reply_notification']);
  724.  
  725.                                                 // This is a nasty kludge to remove the username var ... till (if?)
  726.                                                 // translators update their templates
  727.                                                 $emailer->msg = preg_replace('#[ ]?{USERNAME}#', '', $emailer->msg);
  728.  
  729.                                                 $emailer->assign_vars(array(
  730.                                                         'EMAIL_SIG' => (!empty($board_config['board_email_sig'])) ? str_replace('<br />', "\n", "-- \n" . $board_config['board_email_sig']) : '',
  731.                                                         'SITENAME' => $board_config['sitename'],
  732.                                                         'TOPIC_TITLE' => $topic_title,
  733.  
  734.                                                         'U_TOPIC' => $server_protocol . $server_name . $server_port . $script_name . '&' . POST_POST_URL . "=$post_id#$post_id",
  735.                                                         'U_STOP_WATCHING_TOPIC' => $server_protocol . $server_name . $server_port . $script_name . '&' . POST_TOPIC_URL . "=$topic_id&unwatch=topic")
  736.                                                 );
  737.  
  738.                                                 $emailer->send();
  739.                                                 $emailer->reset();
  740.                                         }
  741.                                 }
  742.                         }
  743.                         $db->sql_freeresult($result);
  744.  
  745.                         if ($update_watched_sql != '')
  746.                         {
  747.                                 $sql = "UPDATE " . TOPICS_WATCH_TABLE . "
  748.                                         SET notify_status = " . TOPIC_WATCH_NOTIFIED . "
  749.                                         WHERE topic_id = $topic_id
  750.                                                 AND user_id IN ($update_watched_sql)";
  751.                                 $db->sql_query($sql);
  752.                         }
  753.                 }
  754.  
  755.                 $sql = "SELECT topic_id
  756.                         FROM " . TOPICS_WATCH_TABLE . "
  757.                         WHERE topic_id = $topic_id
  758.                                 AND user_id = " . $userdata['user_id'];
  759.                 if (!($result = $db->sql_query($sql)))
  760.                 {
  761.                         message_die(GENERAL_ERROR, 'Could not obtain topic watch information', '', __LINE__, __FILE__, $sql);
  762.                 }
  763.  
  764.                 $row = $db->sql_fetchrow($result);
  765.  
  766.                 if (!$notify_user && !empty($row['topic_id']))
  767.                 {
  768.                         $sql = "DELETE FROM " . TOPICS_WATCH_TABLE . "
  769.                                 WHERE topic_id = $topic_id
  770.                                         AND user_id = " . $userdata['user_id'];
  771.                         if (!$db->sql_query($sql))
  772.                         {
  773.                                 message_die(GENERAL_ERROR, 'Could not delete topic watch information', '', __LINE__, __FILE__, $sql);
  774.                         }
  775.                 }
  776.                 else if ($notify_user && empty($row['topic_id']))
  777.                 {
  778.                         $sql = "INSERT INTO " . TOPICS_WATCH_TABLE . " (user_id, topic_id, notify_status)
  779.                                 VALUES (" . $userdata['user_id'] . ", $topic_id, 0)";
  780.                         if (!$db->sql_query($sql))
  781.                         {
  782.                                 message_die(GENERAL_ERROR, 'Could not insert topic watch information', '', __LINE__, __FILE__, $sql);
  783.                         }
  784.                 }
  785.         }
  786. }
  787.  
  788. //
  789. // Fill smiley templates (or just the variables) with smileys
  790. // Either in a window or inline
  791. //
  792. function generate_smilies($mode, $page_id)
  793. {
  794.         global $db, $board_config, $template, $lang, $images, $theme, $phpEx, $phpbb_root_path;
  795.         global $user_ip, $session_length, $starttime;
  796.         global $userdata;
  797.  
  798.         $inline_columns = 4;
  799.         $inline_rows = 5;
  800.         $window_columns = 8;
  801.  
  802.         if ($mode == 'window')
  803.         {
  804.                 $userdata = session_pagestart($user_ip, $page_id, $nukeuser);
  805.                 init_userprefs($userdata);
  806.  
  807.                 $gen_simple_header = TRUE;
  808.  
  809.                 $page_title = $lang['Review_topic'] . " - $topic_title";
  810.                 include("includes/page_header_review.php");
  811.  
  812.                 $template->set_filenames(array(
  813.                         'smiliesbody' => 'posting_smilies.tpl')
  814.                 );
  815.         }
  816.  
  817.         $sql = "SELECT emoticon, code, smile_url
  818.                 FROM " . SMILIES_TABLE . "
  819.                 ORDER BY smilies_id";
  820.         if ($result = $db->sql_query($sql))
  821.         {
  822.                 $num_smilies = 0;
  823.                 $rowset = array();
  824.                 while ($row = $db->sql_fetchrow($result))
  825.                 {
  826.                         if (empty($rowset[$row['smile_url']]))
  827.                         {
  828.                                 $rowset[$row['smile_url']]['code'] = str_replace("'", "\\'", str_replace('\\', '\\\\', $row['code']));
  829.                                 $rowset[$row['smile_url']]['emoticon'] = $row['emoticon'];
  830.                                 $num_smilies++;
  831.                         }
  832.                 }
  833.  
  834.                 if ($num_smilies)
  835.                 {
  836.                         $smilies_count = ($mode == 'inline') ? min(19, $num_smilies) : $num_smilies;
  837.                         $smilies_split_row = ($mode == 'inline') ? $inline_columns - 1 : $window_columns - 1;
  838.  
  839.                         $s_colspan = 0;
  840.                         $row = 0;
  841.                         $col = 0;
  842.  
  843.                         while (list($smile_url, $data) = @each($rowset))
  844.                         {
  845.                                 if (!$col)
  846.                                 {
  847.                                         $template->assign_block_vars('smilies_row', array());
  848.                                 }
  849.  
  850.                                 $template->assign_block_vars('smilies_row.smilies_col', array(
  851.                                         'SMILEY_CODE' => $data['code'],
  852.                                         'SMILEY_IMG' => $board_config['smilies_path'] . '/' . $smile_url,
  853.                                         'SMILEY_DESC' => $data['emoticon'])
  854.                                 );
  855.  
  856.                                 $s_colspan = max($s_colspan, $col + 1);
  857.  
  858.                                 if ($col == $smilies_split_row)
  859.                                 {
  860.                                         if ($mode == 'inline' && $row == $inline_rows - 1)
  861.                                         {
  862.                                                 break;
  863.                                         }
  864.                                         $col = 0;
  865.                                         $row++;
  866.                                 }
  867.                                 else
  868.                                 {
  869.                                         $col++;
  870.                                 }
  871.                         }
  872.  
  873.                         if ($mode == 'inline' && $num_smilies > $inline_rows * $inline_columns)
  874.                         {
  875.                                 $template->assign_block_vars('switch_smilies_extra', array());
  876.  
  877.                                 $template->assign_vars(array(
  878.                                         'L_MORE_SMILIES' => $lang['More_emoticons'],
  879.                                         'U_MORE_SMILIES' => append_sid("posting.$phpEx?mode=smilies&popup=1"))
  880.                                 );
  881.                         }
  882.  
  883.                         $template->assign_vars(array(
  884.                                 'L_EMOTICONS' => $lang['Emoticons'],
  885.                                 'L_CLOSE_WINDOW' => $lang['Close_window'],
  886.                                 'S_SMILIES_COLSPAN' => $s_colspan)
  887.                         );
  888.                 }
  889.         }
  890.  
  891.         if ($mode == 'window')
  892.         {
  893.                 $template->pparse('smiliesbody');
  894.  
  895.                 include("includes/page_tail_review.php");
  896.         }
  897. }
  898.  
  899. ?>