1 (edited by aaronproctor 2016-03-11 07:38)

Topic: [Integration] Post to PunBB Forum from a PHP Form

Based on what I shared at http://punbb.informer.com/forums/topic/ … nbb-forum/ and have hosted at http://fwdlabs.com/resources/code/, I just got a question from "janereigytasuda" about how one could post to a PunBB forum from a PHP form outside of PunBB.

This variant of my other integration would really be for an administrator to create a domino effect and bypasses all of the handy security features of PunBB. That said, maybe it's useful to someone.

PHP variables to configure before the query:

$myPunBBPrefix = MySQL table prefix (e.g. punbb_)
$myPunBBUsername = PunBB username (e.g. admin)
$myPunBBSubject = PunBB topic title (e.g. "New Topic")
$myPunBBDate = strtotime() of a YYYY-MM-DD date (e.g. 2016-01-28)
$myPunBBForumID = PunBB forum ID number (e.g. 1)
$myPunBBUserID = PunBB user ID (e.g. 1)
$myPunBBMessage = PunBB post within a topic (e.g. "This is a post.")
$myPunBBUserIP = PunBB user IP address

Note: mysql_query() is was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. MySQLi or PDO_MySQL should be used instead for more future-friendly code.

mysql_query("INSERT INTO ".$myPunBBPrefix."topics (poster, subject, posted, last_post, last_poster, forum_id) VALUES(
    '".$myPunBBUsername."',
    '".$myPunBBSubject."',
    ".$myPunBBDate.",
    ".$myPunBBDate.",
    '".$myPunBBUsername."',
    '".$myPunBBForumID."'
    )");
$myPunBBNewTopicID = mysql_insert_id();

First, we need to create a new topic. The above is the "insert." PunBB's topic needs the poster's username, the subject, the date string, the last posted string, the last poster's username (which can match the original poster's username), and the forum ID number. If you create an input form that sends that all to the right MySQL table, it will then show up in punBB as a new topic.

mysql_query("INSERT INTO ".$myPunBBPrefix."posts (poster, poster_id, message, posted, topic_id, poster_ip) VALUES(
    '".$myPunBBUsername."',
    ".$punbbUserID.",
    '".$myPunBBMessage."',
    ".$myPunBBDate.",
    ".$myPunBBNewTopicID.",
    '".$myPunBBUserIP."'
    )");
$myPunBBNewPostID = mysql_insert_id();

Second, we need to create the post within that topic. PunBB's posts need the poster's username, the poster's user ID, the message of that post, the date string, the topic ID, and the user's IP address. This then makes the post show up within the new topic.

Hope someone here finds it useful. I'll post it along with some other open-source work at http://fwdlabs.com/resources/code/.