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