Source for file BitBoardPost.php
Documentation is available at BitBoardPost.php
* Messageboards class to illustrate best practices when creating a new bitweaver package that
* builds on core bitweaver functionality, such as the Liberty CMS engine
* @author spider <spider@steelsun.com>
require_once( LIBERTY_PKG_PATH. 'LibertyMime.php' );
require_once( BOARDS_PKG_PATH. 'BitBoardTopic.php' );
* Class that handles posts to a board
* During initialisation, be sure to call our base constructors
function BitBoardPost($pCommentId = NULL, $pContentId = NULL, $pInfo = NULL) {
parent::__construct($pCommentId,$pContentId,$pInfo);
* This function verifies the data for a post
function verify( &$pParamHash ) {
if( isset ( $pParamHash['is_approved'] ) ) {
if( !is_numeric( $pParamHash['is_approved'] ) || $pParamHash['is_approved'] > 1 || $pParamHash['is_approved'] < 0 ) {
$this->mErrors[]= ("Invalid post approved state");
$pParamHash['post_store']['is_approved'] = $pParamHash['is_approved'];
if( isset ( $pParamHash['is_warned'] ) ) {
if( !is_numeric( $pParamHash['warned'] ) || $pParamHash['is_warned'] > 1 || $pParamHash['is_warned'] < 0 ) {
$this->mErrors[]= ("Invalid warned state");
$pParamHash['post_store']['is_warned'] = $pParamHash['is_warned'];
if( !empty( $pParamHash['warned_message'] ) ) {
$pParamHash['post_store']['warned_message'] = $pParamHash['warned_message'];
if( !empty( $pParamHash['warned_message'] ) ) {
$pParamHash['post_store']['warned_message'] = $pParamHash['warned_message'];
if( !empty( $pParamHash['migrate_post_id'] ) ) {
$pParamHash['post_store']['migrate_post_id'] = $pParamHash['migrate_post_id'];
return( count( $this->mErrors ) == 0 && !empty( $pParamHash['post_store'] ) );
* This function stores a post
function store( &$pParamHash ) {
//$gBitSystem->verifyPermission('p_boards_update');
//$pParamHash = (($pParamHash + 1)%2);
$query_sel = "SELECT * FROM `". BIT_DB_PREFIX. "boards_posts` WHERE `comment_id` = ?";
$isStored = $this->mDb->getOne( $query_sel, array( $this->mCommentId ) );
$result = $this->mDb->associateUpdate( 'boards_posts', $pParamHash['post_store'], array( 'comment_id' => $this->mCommentId ) );
$pParamHash['post_store']['comment_id'] = $this->mCommentId;
$result = $this->mDb->associateInsert( 'boards_posts', $pParamHash['post_store'] );
* This function gets the meta data relating to a post
if (!isset ($this->mInfo['accepted'])) {
if (!empty($data['warned_message'])) {
$data['warned_message'] = str_replace("\n","<br />\n",$data['warned_message']);
* This function removes a bitboard entry
$this->mDb->StartTrans();
// parent actually has deletion of rows in boards for constraint reasons
$this->mDb->CompleteTrans();
$this->mDb->RollbackTrans();
* This function gets all the post relating to a topic
function getComments( $pContentId = NULL, $pMaxComments = NULL, $pOffset = NULL, $pSortOrder = NULL, $pDisplayMode = NULL ) {
global $gBitUser, $gBitSystem;
$joinSql = $selectSql = $whereSql = '';
$mid = 'thread_forward_sequence ASC';
if (!empty($pSortOrder)) {
if ($pSortOrder == 'commentDate_desc') {
} else if ($pSortOrder == 'commentDate_asc') {
} elseif ($pSortOrder == 'thread_asc') {
$mid = 'thread_forward_sequence ASC';
// thread newest first is harder...
} elseif ($pSortOrder == 'thread_desc') {
$mid = 'thread_reverse_sequence ASC';
$mid = $this->mDb->convertSortmode( $pSortOrder );
$mid = 'order by ' . $mid;
$select1 = ', lcp.content_type_guid as parent_content_type_guid, lcp.title as parent_title ';
$join1 = " LEFT OUTER JOIN `". BIT_DB_PREFIX. "liberty_content` lcp ON (lcp.content_id = lcom.parent_id) ";
$mid2 = "lcom.`thread_forward_sequence` LIKE '". sprintf("%09d.",$contentId). "%'";
if ($gBitSystem->isFeatureActive('boards_posts_anon_moderation') && !($gBitUser->hasPermission('p_boards_update') || $gBitUser->hasPermission('p_boards_post_update'))) {
$whereSql .= " AND ((post.`is_approved` = 1) OR (lc.`user_id` >= 0))";
$pListHash = array( 'content_id' => $contentId, 'max_records' => $pMaxComments, 'offset'=> $pOffset, 'sort_mode'=> $pSortOrder, 'display_mode' => $pDisplayMode, 'has_comment_view_perm' => TRUE );
$this->getServicesSql( 'content_list_sql_function', $selectSql, $joinSql, $whereSql, $bindVars, $this, $pListHash );
$sql = "SELECT lcom.`comment_id`, lcom.`parent_id`, lcom.`root_id`,
lcom.`thread_forward_sequence`, lcom.`thread_reverse_sequence`, lcom.`anon_name`, lc.*, uu.`email`, uu.`real_name`, uu.`login`,
uu.registration_date AS registration_date,
tf_ava.`file_name` AS `avatar_file_name`, tf_ava.`mime_type` AS `avatar_mime_type`, tf_ava.`user_id` AS `avatar_user_id`, ta_ava.`attachment_id` AS `avatar_attachment_id`
INNER JOIN `". BIT_DB_PREFIX. "liberty_content` lc ON (lcom.`content_id` = lc.`content_id`)
INNER JOIN `". BIT_DB_PREFIX. "users_users` uu ON (lc.`user_id` = uu.`user_id`)
LEFT OUTER JOIN `". BIT_DB_PREFIX. "liberty_attachments` ta_ava ON ( uu.`avatar_attachment_id`=ta_ava.`attachment_id` )
LEFT OUTER JOIN `". BIT_DB_PREFIX. "liberty_files` tf_ava ON ( tf_ava.`file_id`=ta_ava.`foreign_id` )
LEFT JOIN `". BIT_DB_PREFIX. "boards_posts` post ON (post.`comment_id` = lcom.`comment_id`)
WHERE $mid2 $whereSql $mid";
$flat_comments = array();
if( $result = $this->mDb->query( $sql, $bindVars, $pMaxComments, $pOffset ) ) {
while( $row = $result->FetchRow() ) {
if (empty($row['anon_name'])) {
$row['anon_name'] = "Anonymous";
if( $row['avatar_file_name'] ) {
'source_file' => liberty_mime_get_source_file( array( 'user_id'=> $row['avatar_user_id'], 'file_name'=> $row['avatar_file_name'], 'mime_type'=> $row['avatar_mime_type'], 'attachment_id'=> $row['avatar_attachment_id'] ) ),
$row['user_avatar_url'] = FALSE;
if (!empty($row['warned_message'])) {
$row['warned_message'] = str_replace("\n","<br />\n",$row['warned_message']);
$row['data'] = trim( $row['data'] );
$row['parsed_data'] = $this->parseData( $row );
$row['level'] = substr_count ( $row['thread_forward_sequence'], '.' ) - 1;
$row['is_editable'] = $c->userCanEdit();
if( $gBitSystem->isFeatureActive( 'comments_allow_attachments' ) ){
// get attachments for each comment
$query = "SELECT * FROM `". BIT_DB_PREFIX. "liberty_attachments` la WHERE la.`content_id`=? ORDER BY la.`pos` ASC, la.`attachment_id` ASC";
if( $result2 = $this->mDb->query( $query,array( (int) $row['content_id'] ))) {
while( $row2 = $result2->fetchRow() ) {
if( $func = $gLibertySystem->getPluginFunction( $row2['attachment_plugin_guid'], 'load_function', 'mime' )) {
// we will pass the preferences by reference that the plugin can easily update them
if( empty( $row['storage'][$row2['attachment_id']] )) {
$row['storage'][$row2['attachment_id']] = array();
$row['storage'][$row2['attachment_id']] = $func( $row2, $row['storage'][$row2['attachment_id']] );
print "No load_function for ". $row2['attachment_plugin_guid'];
// end get attachements for each comment
$flat_comments[$row['content_id']] = $row;
# now select comments wanted
* This function gets a list of posts
global $gBitUser, $gBitSystem;
$joinSql = $selectSql = $whereSql = '';
// $mid = 'ORDER BY `thread_forward_sequence` ASC';
if( !empty( $pListHash['content_id'] ) ) {
$selectSql = ', lcp.content_type_guid as parent_content_type_guid, lcp.title as parent_title ';
$joinSql .= " LEFT OUTER JOIN `". BIT_DB_PREFIX. "liberty_content` lcp ON (lcp.content_id = lcom.parent_id) ";
$whereSql .= " AND `thread_forward_sequence` LIKE '". sprintf("%09d.",$contentId). "%'";
if ($gBitSystem->isFeatureActive('boards_posts_anon_moderation') && !($gBitUser->hasPermission('p_boards_update') || $gBitUser->hasPermission('p_boards_post_update'))) {
$whereSql .= " AND ((post.`is_approved` = 1) OR (lc.`user_id` >= 0))";
$this->getServicesSql( 'content_list_sql_function', $selectSql, $joinSql, $whereSql, $bindVars, $this );
if( !empty( $pListHash['board_id'] ) ) {
$joinSql .= "INNER JOIN `". BIT_DB_PREFIX. "boards` b ON (b.`content_id` = bm.`board_content_id`)";
$whereSql .= ' AND b.`board_id`=? ';
array_push( $bindVars, (int) $pListHash['board_id'] );
if( BitBase::verifyId( $pListHash['user_id'] ) ) {
$whereSql .= ' AND lc.`user_id`=? ';
if( !empty( $whereSql ) ) {
$whereSql = preg_replace( '/^[\s]*AND\b/i', 'WHERE ', $whereSql );
$sql = "SELECT lcom.`comment_id`, lcom.`parent_id`, lcom.`root_id`, lcom.`thread_forward_sequence`, lcom.`thread_reverse_sequence`, lcom.`anon_name`, lc.*, uu.`email`, uu.`real_name`, uu.`login`, post.is_approved, post.is_warned, post.warned_message, uu.registration_date AS registration_date,
tf_ava.`file_name` AS `avatar_file_name`, tf_ava.`mime_type` AS `avatar_mime_type`, tf_ava.`user_id` AS `avatar_user_id`, ta_ava.`attachment_id` AS `avatar_attachment_id`
INNER JOIN `". BIT_DB_PREFIX. "boards_map` bm ON (lcom.`root_id` = bm.`topic_content_id`)
INNER JOIN `". BIT_DB_PREFIX. "liberty_content` lc ON (lcom.`content_id` = lc.`content_id`)
INNER JOIN `". BIT_DB_PREFIX. "users_users` uu ON (lc.`user_id` = uu.`user_id`)
LEFT OUTER JOIN `". BIT_DB_PREFIX. "liberty_attachments` ta_ava ON ( uu.`avatar_attachment_id`=ta_ava.`attachment_id` )
LEFT OUTER JOIN `". BIT_DB_PREFIX. "liberty_files` tf_ava ON ( tf_ava.`file_id`=ta_ava.`foreign_id` )
LEFT JOIN `". BIT_DB_PREFIX. "boards_posts` post ON (post.`comment_id` = lcom.`comment_id`)
$whereSql ORDER BY ". $this->mDb->convertSortmode( $pListHash['sort_mode'] );
if( $result = $this->mDb->query( $sql, $bindVars, $pListHash['max_records'], $pListHash['offset'] ) ) {
while( $row = $result->FetchRow() ) {
if (empty($row['anon_name'])) $row['anon_name'] = "Anonymous";
if( !empty( $row['avatar_file_name'] )) {
'source_file' => liberty_mime_get_source_file( array( 'user_id'=> $row['avatar_user_id'], 'file_name'=> $row['avatar_file_name'], 'mime_type'=> $row['avatar_mime_type'], 'attachment_id'=> $row['avatar_attachment_id'] ) ),
$row['user_avatar_url'] = FALSE;
unset ($row['avatar_file_name']);
if (!empty($row['warned_message'])) {
$row['warned_message'] = str_replace("\n","<br />\n",$row['warned_message']);
$row['data'] = trim($row['data']);
$row['parsed_data'] = $this->parseData( $row );
$row['level'] = substr_count ( $row['thread_forward_sequence'], '.' ) - 1;
$row['topic_id'] = boards_get_topic_comment( $row['thread_forward_sequence'] );
$row['display_url'] = static::getDisplayUrlFromHash( $row );
$row['is_editable'] = $c->userCanEdit();
* This function counts the posts relating to a topic
$joinSql = $selectSql = $whereSql = '';
$paramHash = array( 'include_comments' => TRUE );
$this->getServicesSql( 'content_list_sql_function', $selectSql, $joinSql, $whereSql, $bindVars, $this, $paramHash );
INNER JOIN `". BIT_DB_PREFIX. "liberty_content` lc ON (lcom.`content_id` = lc.`content_id`)
INNER JOIN `". BIT_DB_PREFIX. "users_users` uu ON (lc.`user_id` = uu.`user_id`) $joinSql
LEFT JOIN `". BIT_DB_PREFIX. "boards_posts` post ON (post.`comment_id` = lcom.`comment_id`)
WHERE lcom.`thread_forward_sequence` LIKE '". sprintf("%09d.",$contentId). "%' $whereSql
$ret = $this->mDb->getOne( $sql, $bindVars );
* This function generates a valid lookup URL
$ret = static::getDisplayUrlFromHash( $urlHash );
* Generates the URL to the bitboard page
* @return the link to display the page.
if( static::verifyId( $pParamHash['comment_id'] ) ) {
if( $gBitSystem->isFeatureActive( 'pretty_urls' ) || $gBitSystem->isFeatureActive( 'pretty_urls_extended' ) ) {
$rewrite_tag = $gBitSystem->isFeatureActive( 'pretty_urls_extended' ) ? 'view/' : '';
$ret = BOARDS_PKG_URL. $rewrite_tag. "topic/". $pParamHash['topic_id'];
$ret = BOARDS_PKG_URL. "index.php?t=". $pParamHash['topic_id'];
if( $pParamHash['comment_id'] != $pParamHash['topic_id'] ) {
$ret .= '#comment_'. $pParamHash['comment_id'];
return boards_get_topic_comment( $this->getField( 'thread_forward_sequence') );
$data['is_approved'] = 1;
global $gBitSystem, $gBitUser;
$gBitSystem->fatalError("No Warning Message Given. <br />A post cannot be warned without a message");
$data['warned_message']= $message;
if ($gBitSystem->isPackageActive('messages')) {
require_once(MESSAGES_PKG_PATH. 'Messages.php');
$message = "Your post \"". $this->mInfo['title']. "\" [http://". $_SERVER['HTTP_HOST']. $this->getContactUrl(). "] has been warned with the following message:\n$message\n";
'to_login' => $userInfo['login'],
'to' => $userInfo['real_name'],
'subject' => tra( 'Warned Post' ). ': '. $this->mInfo['title'],
$pm->postMessage( $msgHash );
$query_sel = "SELECT COUNT(*) FROM `". BIT_DB_PREFIX. "boards_posts` WHERE comment_id=?";
|