2011-03-28 14:21:28 +00:00
< ?
authorize ();
/*
'new' if the user is creating a new thread
It will be accompanied with :
$_POST [ 'forum' ]
$_POST [ 'title' ]
$_POST [ 'body' ]
and optionally include :
$_POST [ 'question' ]
$_POST [ 'answers' ]
the latter of which is an array
*/
if ( isset ( $LoggedUser [ 'PostsPerPage' ])) {
$PerPage = $LoggedUser [ 'PostsPerPage' ];
} else {
$PerPage = POSTS_PER_PAGE ;
}
2013-04-15 08:00:54 +00:00
if ( isset ( $_POST [ 'thread' ]) && ! is_number ( $_POST [ 'thread' ])) {
2011-03-28 14:21:28 +00:00
error ( 0 );
}
2013-04-15 08:00:54 +00:00
if ( isset ( $_POST [ 'forum' ]) && ! is_number ( $_POST [ 'forum' ])) {
2011-03-28 14:21:28 +00:00
error ( 0 );
}
2013-04-19 08:00:55 +00:00
// If you're not sending anything, go back
2011-03-28 14:21:28 +00:00
if ( empty ( $_POST [ 'body' ]) || empty ( $_POST [ 'title' ])) {
header ( 'Location: ' . $_SERVER [ 'HTTP_REFERER' ]);
die ();
}
$Body = $_POST [ 'body' ];
2013-04-15 08:00:54 +00:00
if ( $LoggedUser [ 'DisablePosting' ]) {
2013-04-19 08:00:55 +00:00
error ( 'Your posting privileges have been removed' );
2011-03-28 14:21:28 +00:00
}
2012-10-11 08:00:15 +00:00
$Title = Format :: cut_string ( trim ( $_POST [ 'title' ]), 150 , 1 , 0 );
2011-03-28 14:21:28 +00:00
$ForumID = $_POST [ 'forum' ];
2013-04-19 08:00:55 +00:00
if ( ! isset ( $Forums [ $ForumID ])) {
error ( 404 );
}
2011-03-28 14:21:28 +00:00
2013-04-15 08:00:54 +00:00
if ( ! check_forumperm ( $ForumID , 'Write' ) || ! check_forumperm ( $ForumID , 'Create' )) {
2013-02-22 08:00:24 +00:00
error ( 403 );
2011-03-28 14:21:28 +00:00
}
2013-02-22 08:00:24 +00:00
2011-03-28 14:21:28 +00:00
2013-04-19 08:00:55 +00:00
$DB -> query ( "
INSERT INTO forums_topics
( Title , AuthorID , ForumID , LastPostTime , LastPostAuthorID )
2011-03-28 14:21:28 +00:00
Values
2013-04-19 08:00:55 +00:00
( '".db_string($Title)."' , '".$LoggedUser[' ID ']."' , '$ForumID' , '".sqltime()."' , '".$LoggedUser[' ID ']."' ) " );
2011-03-28 14:21:28 +00:00
$TopicID = $DB -> inserted_id ();
2013-04-19 08:00:55 +00:00
$DB -> query ( "
INSERT INTO forums_posts
2011-03-28 14:21:28 +00:00
( TopicID , AuthorID , AddedTime , Body )
2013-04-19 08:00:55 +00:00
VALUES
2011-03-28 14:21:28 +00:00
( '$TopicID' , '".$LoggedUser[' ID ']."' , '".sqltime()."' , '".db_string($Body)."' ) " );
$PostID = $DB -> inserted_id ();
2013-04-19 08:00:55 +00:00
$DB -> query ( "
UPDATE forums
SET
2013-05-30 08:00:30 +00:00
NumPosts = NumPosts + 1 ,
NumTopics = NumTopics + 1 ,
LastPostID = '$PostID' ,
LastPostAuthorID = '".$LoggedUser[' ID ']."' ,
LastPostTopicID = '$TopicID' ,
LastPostTime = '".sqltime()."'
2013-04-19 08:00:55 +00:00
WHERE ID = '$ForumID' " );
2013-02-22 08:00:24 +00:00
2013-04-19 08:00:55 +00:00
$DB -> query ( "
UPDATE forums_topics
SET
2013-05-30 08:00:30 +00:00
NumPosts = NumPosts + 1 ,
LastPostID = '$PostID' ,
LastPostAuthorID = '".$LoggedUser[' ID ']."' ,
LastPostTime = '".sqltime()."'
2013-04-19 08:00:55 +00:00
WHERE ID = '$TopicID' " );
2011-03-28 14:21:28 +00:00
2013-04-15 08:00:54 +00:00
if ( isset ( $_POST [ 'subscribe' ])) {
2013-05-30 08:00:30 +00:00
$DB -> query ( "
INSERT INTO users_subscriptions
VALUES ( $LoggedUser [ ID ], $TopicID ) " );
2011-03-28 14:21:28 +00:00
$Cache -> delete_value ( 'subscriptions_user_' . $LoggedUser [ 'ID' ]);
}
2012-09-12 08:00:27 +00:00
//auto subscribe
2013-01-14 08:00:16 +00:00
/*
2013-04-15 08:00:54 +00:00
if ( check_perms ( 'users_mod' )) {
2013-05-30 08:00:30 +00:00
$DB -> query ( "
SELECT SubscriberID
FROM subscribed_forums
WHERE ForumID = '$ForumID'
AND SubscriberID != '$LoggedUser[ID]' " );
2013-04-15 08:00:54 +00:00
while ( list ( $SubscriberID ) = $DB -> next_record ()) {
$DB -> query ( " INSERT INTO users_subscriptions VALUES ( $SubscriberID , $TopicID ) " );
2013-04-19 08:00:55 +00:00
// $DB->query("INSERT INTO forums_last_read_topics
// (UserID, TopicID, PostID) VALUES
// ('$SubscriberID', '".$TopicID ."', '".db_string($PostID)."')
// ON DUPLICATE KEY UPDATE PostID='$LastPost'");
$Cache -> delete_value ( 'subscriptions_user_' . $SubscriberID );
2013-04-15 08:00:54 +00:00
}
2013-01-14 08:00:16 +00:00
}
2013-04-19 08:00:55 +00:00
*/
2012-09-12 08:00:27 +00:00
2011-03-28 14:21:28 +00:00
if ( empty ( $_POST [ 'question' ]) || empty ( $_POST [ 'answers' ]) || ! check_perms ( 'forums_polls_create' )) {
$NoPoll = 1 ;
} else {
$NoPoll = 0 ;
$Question = trim ( $_POST [ 'question' ]);
$Answers = array ();
$Votes = array ();
2013-02-22 08:00:24 +00:00
2013-05-30 08:00:30 +00:00
//This can cause polls to have answer IDs of 1 3 4 if the second box is empty
2011-03-28 14:21:28 +00:00
foreach ( $_POST [ 'answers' ] as $i => $Answer ) {
2013-05-30 08:00:30 +00:00
if ( $Answer == '' ) {
continue ;
}
2013-04-15 08:00:54 +00:00
$Answers [ $i + 1 ] = $Answer ;
$Votes [ $i + 1 ] = 0 ;
2011-03-28 14:21:28 +00:00
}
2013-02-22 08:00:24 +00:00
2011-03-28 14:21:28 +00:00
if ( count ( $Answers ) < 2 ) {
error ( 'You cannot create a poll with only one answer.' );
2013-04-15 08:00:54 +00:00
} elseif ( count ( $Answers ) > 25 ) {
error ( 'You cannot create a poll with greater than 25 answers.' );
2011-03-28 14:21:28 +00:00
}
2013-02-22 08:00:24 +00:00
2013-05-30 08:00:30 +00:00
$DB -> query ( "
INSERT INTO forums_polls
( TopicID , Question , Answers )
VALUES
( '$TopicID' , '".db_string($Question)."' , '".db_string(serialize($Answers))."' ) " );
$Cache -> cache_value ( 'polls_' . $TopicID , array ( $Question , $Answers , $Votes , '0000-00-00 00:00:00' , '0' ), 0 );
2011-03-28 14:21:28 +00:00
2013-04-15 08:00:54 +00:00
if ( $ForumID == STAFF_FORUM ) {
2013-05-30 08:00:30 +00:00
send_irc ( 'PRIVMSG ' . ADMIN_CHAN . ' :!mod Poll created by ' . $LoggedUser [ 'Username' ] . " : \" $Question\ " https :// " .SSL_SITE_URL.'/forums.php?action=viewthread&threadid='. $TopicID );
2011-03-28 14:21:28 +00:00
}
}
2013-05-30 08:00:30 +00:00
// if cache exists modify it, if not, then it will be correct when selected next, and we can skip this block
2011-03-28 14:21:28 +00:00
if ( $Forum = $Cache -> get_value ( 'forums_' . $ForumID )) {
list ( $Forum ,,, $Stickies ) = $Forum ;
2013-02-22 08:00:24 +00:00
2013-05-30 08:00:30 +00:00
// Remove the last thread from the index
2011-05-12 10:24:03 +00:00
if ( count ( $Forum ) == TOPICS_PER_PAGE && $Stickies < TOPICS_PER_PAGE ) {
array_pop ( $Forum );
2011-03-28 14:21:28 +00:00
}
2013-02-22 08:00:24 +00:00
2011-03-28 14:21:28 +00:00
if ( $Stickies > 0 ) {
2013-05-30 08:00:30 +00:00
$Part1 = array_slice ( $Forum , 0 , $Stickies , true ); // Stickies
$Part3 = array_slice ( $Forum , $Stickies , TOPICS_PER_PAGE - $Stickies - 1 , true ); // Rest of page
2011-03-28 14:21:28 +00:00
} else {
$Part1 = array ();
$Part3 = $Forum ;
}
$Part2 = array ( $TopicID => array (
'ID' => $TopicID ,
'Title' => $Title ,
'AuthorID' => $LoggedUser [ 'ID' ],
'IsLocked' => 0 ,
'IsSticky' => 0 ,
'NumPosts' => 1 ,
'LastPostID' => $PostID ,
'LastPostTime' => sqltime (),
'LastPostAuthorID' => $LoggedUser [ 'ID' ],
'NoPoll' => $NoPoll
2013-04-19 08:00:55 +00:00
)); // Bumped
2011-03-28 14:21:28 +00:00
$Forum = $Part1 + $Part2 + $Part3 ;
2013-04-19 08:00:55 +00:00
$Cache -> cache_value ( 'forums_' . $ForumID , array ( $Forum , '' , 0 , $Stickies ), 0 );
2013-02-22 08:00:24 +00:00
2013-04-19 08:00:55 +00:00
// Update the forum root
2011-03-28 14:21:28 +00:00
$Cache -> begin_transaction ( 'forums_list' );
$Cache -> update_row ( $ForumID , array (
2013-04-19 08:00:55 +00:00
'NumPosts' => '+1' ,
'NumTopics' => '+1' ,
'LastPostID' => $PostID ,
'LastPostAuthorID' => $LoggedUser [ 'ID' ],
'LastPostTopicID' => $TopicID ,
'LastPostTime' => sqltime (),
'Title' => $Title ,
'IsLocked' => 0 ,
'IsSticky' => 0
2011-03-28 14:21:28 +00:00
));
$Cache -> commit_transaction ( 0 );
} else {
2013-04-19 08:00:55 +00:00
// If there's no cache, we have no data, and if there's no data
2011-03-28 14:21:28 +00:00
$Cache -> delete_value ( 'forums_list' );
}
$Cache -> begin_transaction ( 'thread_' . $TopicID . '_catalogue_0' );
$Post = array (
2013-04-19 08:00:55 +00:00
'ID' => $PostID ,
'AuthorID' => $LoggedUser [ 'ID' ],
'AddedTime' => sqltime (),
'Body' => $Body ,
'EditedUserID' => 0 ,
'EditedTime' => '0000-00-00 00:00:00'
2011-03-28 14:21:28 +00:00
);
$Cache -> insert ( '' , $Post );
$Cache -> commit_transaction ( 0 );
$Cache -> begin_transaction ( 'thread_' . $TopicID . '_info' );
2013-04-19 08:00:55 +00:00
$Cache -> update_row ( false , array ( 'Posts' => '+1' , 'LastPostAuthorID' => $LoggedUser [ 'ID' ]));
2011-03-28 14:21:28 +00:00
$Cache -> commit_transaction ( 0 );
2013-07-05 08:00:39 +00:00
header ( " Location: forums.php?action=viewthread&threadid= $TopicID " );
2011-03-28 14:21:28 +00:00
die ();