<?php
/***************************************************************************
* insert_post.php
* -------------------------------
* Author : netclectic - Adrian Cockburn - [email protected]
* Created : Monday, Sept 23, 2002
* Modified : Sunday, Feb 23, 2003
***************************************************************************/
if ( !defined('IN_PHPBB') )
{
die('Hacking attempt');
}
include_once($phpbb_root_path . 'includes/bbcode.'.$phpEx);
include_once($phpbb_root_path . 'includes/functions_post.'.$phpEx);
include_once($phpbb_root_path . 'includes/functions_search.'.$phpEx);
function insert_post(
$message,
$subject,
$forum_id,
$user_id,
$user_name,
$user_attach_sig,
$topic_id = NULL,
$topic_type = POST_NORMAL,
$do_notification = false,
$notify_user = false,
$current_time = 0,
$error_die_function = '',
$html_on = 0,
$bbcode_on = 1,
$smilies_on = 1 )
{
global $db, $board_config, $user_ip;
// initialise some variables
$topic_vote = 0;
$poll_title = '';
$poll_options = '';
$poll_length = '';
$mode = 'reply';
$bbcode_uid = ($bbcode_on) ? make_bbcode_uid() : '';
$error_die_function = ($error_die_function == '') ? "message_die" : $error_die_function;
$current_time = ($current_time == 0) ? time() : $current_time;
// parse the message and the subject
$message = str_replace("\'", "''", prepare_message(trim($message), $html_on, $bbcode_on, $smilies_on, $bbcode_uid));
$subject = str_replace("\'", "''", trim($subject));
$username = str_replace("\'", "''", trim(strip_tags($user_name)));
// if this is a new topic then insert the topic details
if ( is_null($topic_id) )
{
$mode = 'newtopic';
$sql = "INSERT INTO " . TOPICS_TABLE . " (topic_title, topic_poster, topic_time, forum_id, topic_status, topic_type, topic_vote) VALUES ('$subject', " . $user_id . ", $current_time, $forum_id, " . TOPIC_UNLOCKED . ", $topic_type, $topic_vote)";
if ( !$db->sql_query($sql, BEGIN_TRANSACTION) )
{
$error_die_function(GENERAL_ERROR, 'Error in posting', '', __LINE__, __FILE__, $sql);
}
$topic_id = $db->sql_nextid();
}
// insert the post details using the topic id
$sql = "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, " . $user_id . ", '$username', $current_time, '$user_ip', $bbcode_on, $html_on, $smilies_on, $user_attach_sig)";
if ( !$db->sql_query($sql, BEGIN_TRANSACTION) )
{
$error_die_function(GENERAL_ERROR, 'Error in posting', '', __LINE__, __FILE__, $sql);
}
$post_id = $db->sql_nextid();
// insert the actual post text for our new post
$sql = "INSERT INTO " . POSTS_TEXT_TABLE . " (post_id, post_subject, bbcode_uid, post_text) VALUES ($post_id, '$subject', '$bbcode_uid', '$message')";
if ( !$db->sql_query($sql, BEGIN_TRANSACTION) )
{
$error_die_function(GENERAL_ERROR, 'Error in posting', '', __LINE__, __FILE__, $sql);
}
// update the post counts etc.
$newpostsql = ($mode == 'newtopic') ? ',forum_topics = forum_topics + 1' : '';
$sql = "UPDATE " . FORUMS_TABLE . " SET
forum_posts = forum_posts + 1,
forum_last_post_id = $post_id
$newpostsql
WHERE forum_id = $forum_id";
if ( !$db->sql_query($sql, BEGIN_TRANSACTION) )
{
$error_die_function(GENERAL_ERROR, 'Error in posting', '', __LINE__, __FILE__, $sql);
}
// update the first / last post ids for the topic
$first_post_sql = ( $mode == 'newtopic' ) ? ", topic_first_post_id = $post_id " : ' , topic_replies=topic_replies+1';
$sql = "UPDATE " . TOPICS_TABLE . " SET
topic_last_post_id = $post_id
$first_post_sql
WHERE topic_id = $topic_id";
if ( !$db->sql_query($sql, BEGIN_TRANSACTION) )
{
$error_die_function(GENERAL_ERROR, 'Error in posting', '', __LINE__, __FILE__, $sql);
}
// update the user's post count and commit the transaction
$sql = "UPDATE " . USERS_TABLE . " SET
user_posts = user_posts + 1
WHERE user_id = $user_id";
if ( !$db->sql_query($sql, END_TRANSACTION) )
{
$error_die_function(GENERAL_ERROR, 'Error in posting', '', __LINE__, __FILE__, $sql);
}
// add the search words for our new post
switch ($board_config['version'])
{
case '.0.0' :
case '.0.1' :
case '.0.2' :
case '.0.3' :
add_search_words($post_id, stripslashes($message), stripslashes($subject));
break;
default :
add_search_words('', $post_id, stripslashes($message), stripslashes($subject));
break;
}
// do we need to do user notification
if ( ($mode == 'reply') && $do_notification )
{
$post_data = array();
user_notification($mode, $post_data, $subject, $forum_id, $topic_id, $post_id, $notify_user);
}
// if all is well then return the id of our new post
return array('post_id'=>$post_id, 'topic_id'=>$topic_id);
}
?>