In phpBB 3.0, you can create a new post in a pretty simple way. There are a few tricky parts to it though.
phpBB's submit_post api can be found in includes/functions_posting.php. Nameley the submit_post() function. This function has following parameters: $mode, $subject, $username, $topic_type, &$poll, &$data, $update_message = true.
The parameters
$mode is either 'post', 'reply' or 'edit'.
$subject contains the subject of the post/topic.
$username only needs to be suplied if the $user->data['is_registered'] is set to false, and contains the guest username of the post.
$topic_type is either POST_NORMAL, POST_STICKY, POST_ANNOUNCE or POST_GLOBAL, depending on the type of topic you want to submit. It's only needed for creating a new topic, or a reply in a global announcement.
$poll is passed by reference. It's an array that contains poll information. It takes the following form (source):
- Code: Select all
$poll = array(
'poll_title' => 'String that is the poll question',
'poll_start' => 'Timestamp when the poll "opens" or 0',
'poll_max_options' => 'Integer count of the maximum number of options a user can select',
'poll_length' => 'Integer number of days until the poll closes',
'poll_vote_change' => boolean value: whether users may change their vote or not,'
'poll_options' => array(0 => 'option title 1', 1 => 'option title 2' [...] etc),
);
To edit a post, simply use $mode = 'edit' in the arguments, and provide the post_id and topic_id in the $data array.
$post_data is passed by reference like $poll. It is an array of options that are documented later on.
$update_message is automatically set to true when $mode is 'post' or 'reply', there's no good reason why it should be false. It's an optional argument that defaults to true, so best just ignore it.
Before we submit our post - auth & userdata
The problem with submit_post(), and that is that it uses the current user data for the post, and there's no way to specify an other user. The same problem exists with permissions, the user needs posting permission for the new topic.
So, what we do to solve this problem is the following. We back up $user and $auth and then overwrite them with new data. So, let's back them up:
- Code: Select all
$backup = array(
'user' => $user,
'auth' => $auth,
);
So now we overwrite them with the new data, let's assume the new user id is stored in $user_id. The following code will overwrite the users userdata and re-auth (get the new users permissions).
- Code: Select all
$sql = 'SELECT *
FROM ' . USERS_TABLE . '
WHERE user_id = ' . $user_id;
$result = $db->sql_query($sql);
$row = $db->sql_fetchrow($result);
$db->sql_freeresult($result);
$user->data = array_merge($user->data, $row);
$auth->acl($user->data);
One thing we also need to overwrite is the users ip.
- Code: Select all
$user->ip = '0.0.0.0';
Now would follow the code for submitting the post, which will be mentioned later in this article. First i'll show how to restore the old user and auth data. It's simply:
- Code: Select all
$user = $backup['user'];
$auth = $backup['auth'];
Submitting the post
Now to actually use the code, we'll have to call the submit_post() function. What you need to supply depends on if your post is a reply, a new topic or an edit.
For creating a new topic, $mode needs to be 'post', and you need to supply following keys for $data: topic_title, forum_id, icon_id, enable_bbcode, enable_smilies, enable_urls, enable_sig, message, message_md5, bbcode_bitfield, bbcode_uid, post_edit_locked, topic_time_limit (if sticky or announcement), topic_approved, post_time, enable_indexing, forum_name, notify, notify_set. The following keys get written: topic_id, post_id.
For creating a reply, $mode needs to be either 'reply' or 'quote', and you need to supply following keys for $data: topic_title, forum_id, icon_id, enable_bbcode, enable_smilies, enable_urls, enable_sig, message, message_md5, bbcode_bitfield, bbcode_uid, post_edit_locked, topic_time_limit (if sticky or announcement), post_time, enable_indexing, forum_name, notify, notify_set. The following keys get written: post_id.
For editing a post, $mode needs to be 'edit', and you need to supply following keys for $data: topic_replies_real, topic_first_post_id, topic_last_post_id, post_id, topic_title, poster_id, post_approved, topic_approved, post_edit_reason, forum_id, post_edit_user, icon_id, enable_bbcode, enable_smilies, enable_urls, enable_sig, message_md5, message, bbcode_bitfield, bbcode_uid, post_edit_locked, topic_time_limit (if sticky or announcement), topic_approved, post_approved, topic_id.
How to get the topic_id/post_id
After submitting the post, where is the topic_id or post_id? Well, $data is passed by reference, which means that if the function changes the variable, the original variable is also changed. As you can see above:
The following keys get written: post_id.
So after submitting the post, you can access the post id using $data['post_id'].