(Solved) How to get the BBCode for some post?

Started by KristopherWindsor, June 06, 2009, 06:12:13 PM

Previous topic - Next topic

KristopherWindsor

...
$topic = intval($_GET['topic']);
getTopic();
$thistopic = $context['previous_posts'];
foreach ($thistopic as $c => $apost)
...


In this code, $apost will contain (among other things), the HTML version of a post, but I would like to get the BBCode version for editing. IE, I want to get smilies, not <img> tags.
How can I do this?

Thanks. :)

KristopherWindsor

#1
Nevermind... I solved it by modifying some mod. It directly queries the DB.

The modified version of the mod:

// --- Begin modification - SSI Topic and Replies ---
// Grab posts from a certain topic
function ssi_topic($topic = null, $num_replies = null, $start = null,  $output_method = 'echo')
{
global $scripturl, $db_prefix, $txt, $settings, $modSettings, $context;
global $func, $memberContext;

loadLanguage('Stats');

// Topic variable set?
if ($topic !== null)
// Yes, let's use it (integer, please)
$topic = (int) $topic;
// No? What about a GET variable?
elseif (isset($_GET['topic']))
$topic = (int) $_GET['topic'];
// Well, what else are we going to do?
else
die('error 3.141592653589793');

// Number of replies per page
if ($num_replies === null)
//$num_replies = isset($_GET['num_replies']) ? (int) $_GET['num_replies'] : 10;
$num_replies = 10;
else
$num_replies = (int) $num_replies;


// Reply to start at
if ($start === null)
$start = isset($_GET['start']) ? (int) $_GET['start'] : 0;
else
$start = (int) $start;

$num_replies = max(0, $num_replies);
$start = max(0, $start);

// Get some information about the topic (ie. the first post)
// !!! This should check logged in user's permissions, rather than checking if guests are allowed.
$result = db_query("
SELECT
t.ID_TOPIC, t.ID_BOARD, t.ID_FIRST_MSG, t.ID_LAST_MSG,
t.ID_MEMBER_STARTED, t.numReplies, t.locked, m.ID_MEMBER,
m.icon, m.subject, m.posterTime, m.body, m.smileysEnabled,
m.ID_MSG,
IFNULL(mem.realName, m.posterName) AS posterName,
b.name AS board_name
FROM ({$db_prefix}topics AS t, {$db_prefix}messages AS m, {$db_prefix}boards AS b)
LEFT JOIN {$db_prefix}members AS mem ON (mem.ID_MEMBER = m.ID_MEMBER)
WHERE t.ID_TOPIC = $topic
AND m.ID_MSG = t.ID_FIRST_MSG
AND b.ID_BOARD = t.ID_BOARD
AND FIND_IN_SET(-1, b.memberGroups)", __FILE__, __LINE__);

// No results? That's not good!
if (mysql_num_rows($result) == 0)
die('error 3.141592653589793');

// Get the topic info
$row = mysql_fetch_assoc($result);
mysql_free_result($result);

// Censor it
censorText($row['body']);
censorText($row['subject']);

// Parse BBC in the message
$row['body'] = parse_bbc($row['body'], $row['smileysEnabled'], $row['ID_MSG']);

// Start our array of information
$return = array(
'id' => $row['ID_TOPIC'],
'icon' => '<img src="' .  $settings['images_url'] . '/post/' . $row['icon'] . '.gif" align="middle" alt="' . $row['icon'] . '" border="0" />',
'href' => $scripturl . '?topic=' . $row['ID_TOPIC'] . '.0',
'link' => '<a href="' . $scripturl . '?topic=' . $row['ID_TOPIC'] . '.0">' . $row['numReplies'] . ' ' . ($row['numReplies'] == 1 ? $txt['smf_news_1'] : $txt['smf_news_2']) . '</a>',
'reply_count' => $row['numReplies'],
'locked' => !empty($row['locked']),

// !!! Better way to do this?
'pageindex' => constructPageIndex('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . '?start=%d', $start, $row['numReplies'], $num_replies, true),

'replies' => array()
);

// Get each post and poster in this topic
$result = db_query("
SELECT m.ID_MSG, m.ID_MEMBER
FROM {$db_prefix}messages AS m, {$db_prefix}topics AS t
WHERE m.ID_TOPIC = $topic
AND t.ID_TOPIC = $topic
ORDER BY m.ID_MSG ASC
LIMIT $start, $num_replies", __FILE__, __LINE__);

// Was there any replies?
if (mysql_num_rows($result) != 0)
{
$messages = array();
$posters = array();
// Loop through each post
while ($row = mysql_fetch_assoc($result))
{
// If it wasn't a guest, add them to the posters array
if (!empty($row['ID_MEMBER']))
$posters[] = $row['ID_MEMBER'];
// Add this message to the messages array
$messages[] = $row['ID_MSG'];
}
mysql_free_result($result);
$posters = array_unique($posters);

// Load the member data of all the members that posted in this topic
loadMemberData($posters);

// Now, let's get all the replies (posts)
$result = db_query("
SELECT
m.ID_MSG, m.posterTime, m.ID_MEMBER, m.subject,
IFNULL(mem.realName, m.posterName) AS posterName,
m.posterIP, m.smileysEnabled, m.modifiedTime,
m.modifiedName, m.body, m.icon
FROM {$db_prefix}messages AS m
LEFT JOIN {$db_prefix}members AS mem ON (mem.ID_MEMBER = m.ID_MEMBER)
WHERE ID_MSG IN (" . implode(', ', $messages) . ")
ORDER BY m.ID_MSG ASC", __FILE__, __LINE__);

$counter = $start;
$lastpostid = false;
// Loop through each reply
// !!! This will probably use way too much memory for large topics!!
// !!! Callbacks instead?
while ($row = mysql_fetch_assoc($result))
{
// Try loading the member's data.
//If it couldn't load, or the user was a guest, use some failsafe values
if (!loadMemberContext($row['ID_MEMBER']))
{
// Notice this information isn't used anywhere else....
$memberContext[$row['ID_MEMBER']]['name'] = $row['posterName'];
$memberContext[$row['ID_MEMBER']]['id'] = 0;
$memberContext[$row['ID_MEMBER']]['group'] = $txt[28];
$memberContext[$row['ID_MEMBER']]['link'] = $row['posterName'];
$memberContext[$row['ID_MEMBER']]['email'] = $row['posterEmail'];
$memberContext[$row['ID_MEMBER']]['hide_email'] = $row['posterEmail'] == '' || (!empty($modSettings['guest_hideContacts']) && $user_info['is_guest']);
$memberContext[$row['ID_MEMBER']]['is_guest'] = true;
}
else
{
$memberContext[$row['ID_MEMBER']]['can_view_profile'] = allowedTo('profile_view_any') || ($row['ID_MEMBER'] == $ID_MEMBER && allowedTo('profile_view_own'));
$memberContext[$row['ID_MEMBER']]['is_topic_starter'] = $row['ID_MEMBER'] == $return['poster']['id'];
}

$memberContext[$row['ID_MEMBER']]['ip'] = $row['posterIP'];

$bbcodebody = $row['body'];

// Censor it
censorText($row['body']);
censorText($row['subject']);

// Parse BBC in the message
$row['body'] = parse_bbc($row['body'], $row['smileysEnabled'], $row['ID_MSG']);

// Add this to our messages array
$lastpostid = $row['ID_MSG'];
$return['replies'][$row['ID_MSG']] = array(
'number' => $counter + 1,
'alternate' => $counter % 2,
'id' => $row['ID_MSG'],
'href' => $scripturl . '?topic=' . $topic . '.msg' . $row['ID_MSG'] . '#msg' . $row['ID_MSG'],
'link' => '<a href="' . $scripturl . '?topic=' . $topic . '.msg' . $row['ID_MSG'] . '#msg' . $row['ID_MSG'] . '">' . $row['subject'] . '</a>',
'poster' => &$memberContext[$row['ID_MEMBER']],
'icon' => '<img src="' .  $settings['images_url'] . '/post/' . $row['icon'] . '.gif" align="middle" alt="' . $row['icon'] . '" border="0" />',
'subject' => $row['subject'],
'time' => timeformat($row['posterTime']),
'timestamp' => forum_time(true, $row['posterTime']),
'modified' => array(
'time' => timeformat($row['modifiedTime']),
'timestamp' => forum_time(true, $row['modifiedTime']),
'name' => $row['modifiedName']
),
'body' => $row['body'],
'bbcode' => $bbcodebody,
'new' => empty($row['isRead']),
'first_new' => isset($context['start_from']) && $context['start_from'] == $counter,
'can_modify' => allowedTo('modify_any') || (allowedTo('modify_replies') && $context['user']['started']) || (allowedTo('modify_own') && $row['ID_MEMBER'] == $ID_MEMBER && (empty($modSettings['edit_disable_time']) || $row['posterTime'] + $modSettings['edit_disable_time'] * 60 > time())),
'can_remove' => allowedTo('delete_any') || (allowedTo('delete_replies') && $context['user']['started']) || (allowedTo('delete_own') && $row['ID_MEMBER'] == $ID_MEMBER && (empty($modSettings['edit_disable_time']) || $row['posterTime'] + $modSettings['edit_disable_time'] * 60 > time())),
'can_see_ip' => allowedTo('moderate_forum') || ($row['ID_MEMBER'] == $ID_MEMBER && !empty($ID_MEMBER)),
'is_last' => false,
);

$counter++;
}

mysql_free_result($result);

// The last post
$return['replies'][$lastpostid]['is_last'] = true;

return $return;
}
}
// --- End modification ---


FWIW, here's an update to my "minitopic.php" that now allows post editing:

<?php

// Minitopic.php v1.1 by Kristopher Windsor
// show the topic, allow new posts, allow post edits
// good for embedding in an iframe

require(dirname(__FILE__) . '/SSI.php');
require(
dirname(__FILE__) . '/Sources/Post.php');
require(
dirname(__FILE__) . '/Sources/Subs-Post.php');

if (!isset(
$_GET['topic'])) die('In Soviet Russia, forums own you!');

$thisguy = ssi_welcome('array');
$topic = ssi_topic(null, null, null,  'array');
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Forums</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style type="text/css">
a {color: black; outline: none;}
a:hover {color: black; text-decoration: none;}
a img {border: none;}
body {margin: 0; padding: 0;}
textarea {background-color: transparent;}
.postinfo {font-size: smaller; text-align: right; width: 100%;}
.section {padding: 1%; width: 98%;}
</style>
</head>
<body>

<?php

echo '<div class="section" style="background-color: #EEE">';
echo
'<a href="index.php?topic=' . $topic . '" target="_top">[-]</a> ';
echo
'Hey, ';
if (
$thisguy['is_guest'] == 1)
echo 'Guest. Please <a href="index.php?action=login" target="_top">login</a> or <a href="index.php?action=register" target="_top">register</a>';
else if (
$thisguy['unread_messages'] > 0)
echo htmlspecialchars($thisguy['username']) .', you have <a href="index.php?action=pm" target="_top">' . $thisguy['unread_messages'] . ' unread messages</a>.';
else
echo htmlspecialchars($thisguy['username']) . '!';
echo
'</div>';

// maybe post a new message
if ($thisguy['id'] > 0 && isset($_POST['message']) && strlen(trim($_POST['message'])) > 0)
{
if (isset($_POST['edit']) && $_POST['edit'] != 0)
{
// edit message (make sure user made the post, then edit it
$editid = intval($_POST['edit']);
if (!isset($topic['replies'][$editid]))
die('error');
if ($topic['replies'][$editid]['poster']['id'] != $thisguy['id'])
die('error');
$messageOptions = array('id' => $editid, 'body' => htmlspecialchars(trim($_POST['message'])));
$topicOptions = array('id' => $topic['id'], 'mark_as_read' => true);
$posterOptions = array();
modifyPost($messageOptions, $topicOptions, $posterOptions);
}
else
{
// new message
$messageOptions = array('body' => htmlspecialchars(trim($_POST['message'])), 'id' => '', 'subject' => 'Re:', 'smileys_enabled' => true);
$topicOptions = array('board' => 0, 'id' => $topic['id'], 'mark_as_read' => true);
$posterOptions = array('email' => '', 'ip' => '', 'name' => '', 'update_post_count' => true, 'id' => $thisguy['id']);
createPost($messageOptions, $topicOptions, $posterOptions);
}
$topic = ssi_topic(null, null, null,  'array');
}

$toggle = false;
foreach (
$topic['replies'] as $c => $apost)
{
echo '<div class="section" style="background-color: #';
if ($toggle = !$toggle)
echo 'FFF';
else
echo 'EEE';
echo '">';

// edit button
if ($apost['poster']['username'] == $thisguy['username'])
echo '<div><a href="index.php?action=post;msg=' . $apost['id'] . ';topic=' . $topic['id'] . ';sesc=' . $sc . '" target="_blank"' .
' onClick="document.forms.theform.message.value=\'' . addcslashes($apost['bbcode'], "'\"\\\0\n\r") .
'\'; document.forms.theform.edit.value=\'' . $apost['id'] . '\'; return false">[E]</a></div>';

// message
echo $apost['body'];

// author
echo '<div class="postinfo">&mdash; ' . htmlspecialchars($apost['poster']['username']) . ' (' . $apost['time'] . ')</div>';

echo '</div>';
}

// logged in -> form to edit message
if ($thisguy['id'] > 0)
echo '<fieldset><legend>New Post</legend><form name="theform" action="?topic=' . $topic['id'] . '" method="post"><div class="formcontent">
<textarea name="message" row="8" cols="40"></textarea><br /><input type="submit" value="Post" /></div>
<input name="edit" type="hidden" value="0" /></form></fieldset>'
;

?>

</body>
</html>

Advertisement: