No images in posts using ssi_boardNews()

Started by robembra, November 13, 2009, 07:55:13 PM

Previous topic - Next topic

robembra

Hi,

I have read the acticle http://www.simplemachines.org/community/index.php?topic=13016.0 and have added the code to my page and it works fine, but it doesn't show images that are in the body text.


require('../forum/SSI.php');

$newsForum = ssi_boardNews(6.0, null, null, null, 'array');
foreach ($newsForum as $news){
echo '<div id="newsBox" class="border">
      <div id="box_header">
    <a name="', $news['subject'], '" id="', $news['subject'], '"><strong>', $news['subject'], '</strong></a>
    </div>
      <div class="news">
        <div class="news_text"><br /><table border="0" width="100%" align="center" class="ssi_table"><tr><td><b></b></td></tr><tr><td>' , $news['body'], '<br /><br />', $news['time'], ' ', $txt[525], ' ', $news['poster']['name'], '</td></tr></table></div>
      </div>
      <!-- end #mainContent -->
      </div>';
if (!$news['is_last'])echo '</br>'; }



Any help greatful...Thanks

Arantor

Are these images added with [img], attachments or some other method?
Holder of controversial views, all of which my own.


robembra

Hi,

Thanks for quick reply. I believe its th attaced method!


Arantor

Attachments are not shown via ssi_boardNews. Several people have documented how you would modify the code to make it display them though.
Holder of controversial views, all of which my own.


robembra

Does anyone whee i could find this information?

Thanks

Arantor

Try searching for ssi_boardNews and attachments; I'm sure it's been coded more than once before.
Holder of controversial views, all of which my own.


robembra

I have search around and found this http://www.simplemachines.org/community/index.php?topic=20394.0

I have made altereations to file SSI2.php like is says.

But is still can see images. This time is shows the image tags in html, image name but with no image and only shows 1 and i know there is 3.

The url for the image is http://www.example.org/forum/index.php?action=dlattach;topic=172.0;id=31;image

Is this correct looks a bit odd to me...

Code I added in SSI2.php is...
function ssi_boardNews2($board = null, $limit = null, $start = null, $length = null, $output_method = 'echo')
{

global $scripturl, $db_prefix, $txt;
global $settings, $modSettings, $context;

loadLanguage('Stats');

// Must be integers....

if ($limit === null)
$limit = isset($_GET['limit']) ? (int) $_GET['limit'] : 5;
else
$limit = (int) $limit;

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

if ($board !== null)$board = (int) $board;
elseif (isset($_GET['board']))
$board = (int) $_GET['board'];

if ($length === null)
$length = isset($_GET['length']) ? (int) $_GET['length'] : 0;
else
$length = (int) $length;

// Make sure guests can see this board.
$request = db_query("
SELECT ID_BOARD
FROM {$db_prefix}boards
WHERE " . ($board === null ? '' : "ID_BOARD = $board
AND ") . "FIND_IN_SET(-1, memberGroups)
LIMIT 1", __FILE__, __LINE__);
if (mysql_num_rows($request) == 0)
{
if ($output_method == 'echo')
die($txt['smf_news_error2']);
else
return array();
}
list ($board) = mysql_fetch_row($request);
mysql_free_result($request);

// Find the posts.
$request = db_query("
SELECT
m.icon, m.subject, m.body, IFNULL(mem.realName, m.posterName) AS posterName, m.posterTime,
t.numReplies, t.ID_TOPIC, m.ID_MEMBER, m.smileysEnabled, m.ID_MSG
FROM {$db_prefix}topics AS t, {$db_prefix}messages AS m
LEFT JOIN {$db_prefix}members AS mem ON (mem.ID_MEMBER = m.ID_MEMBER)
WHERE t.ID_BOARD = $board
AND m.ID_MSG = t.ID_FIRST_MSG
ORDER BY m.ID_MSG DESC
LIMIT $start, $limit", __FILE__, __LINE__);
$return = array();
$message_ids = array();
while ($row = mysql_fetch_assoc($request))
{
$message_ids[] = $row['ID_MSG'];
// If we want to limit the length of the post.
if (!empty($length) && strlen($row['body']) > $length)
{
$row['body'] = substr($row['body'], 0, $length);

// The first space or line break. (<br />, etc.)
$cutoff = max(strrpos($row['body'], ' '), strrpos($row['body'], '<'));

if ($cutoff !== false)
$row['body'] = substr($row['body'], 0, $cutoff);
$row['body'] .= '...';
}

$row['body'] = doUBBC($row['body'], $row['smileysEnabled']);

censorText($row['subject']);
censorText($row['body']);

$return[$row['ID_MSG']] = array(
'attachments' => array(),
'id' => $row['ID_TOPIC'],
'icon' => '<img src="' . $settings['images_url'] . '/post/' . $row['icon'] . '.gif" align="middle" alt="' . $row['icon'] . '" />',
'subject' => $row['subject'],
'time' => timeformat($row['posterTime']),
'timestamp' => $row['posterTime'],
'body' => $row['body'],
'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>','replies' => $row['numReplies'],'new_comment' => '<a href="' . $scripturl . '?action=post;topic=' . $row['ID_TOPIC'] . '.' . $row['numReplies'] . '">' . $txt['smf_news_3'] . '</a>','poster' => array('id' => $row['ID_MEMBER'],'name' => $row['posterName'],'href' => !empty($row['ID_MEMBER']) ? $scripturl . '?action=profile;u=' . $row['ID_MEMBER'] : '','link' => !empty($row['ID_MEMBER']) ? '<a href="' . $scripturl . '?action=profile;u=' . $row['ID_MEMBER'] . '">' . $row['posterName'] . '</a>' : $row['posterName'] ),
'is_last' => false
);
}
mysql_free_result($request);


if (empty($return))
return $return;

// Find their attachments.

$request = db_query("
SELECT ID_ATTACH, ID_MSG, filename, size, downloads
FROM {$db_prefix}attachments
WHERE ID_MSG IN (" . implode(', ', $message_ids) . ")", __FILE__, __LINE__);
while ($row = mysql_fetch_assoc($request))
{

$return[$row['ID_MSG']]['attachments'][] = array(
'name' => $row['filename'],
'downloads' => $row['downloads'],
'size' => round($row['size'] / 1024, 2) . ' ' . $txt['smf211'],
'byte_size' => $row['size'],
'href' => $scripturl . '?action=dlattach;topic=' . $return[$row['ID_MSG']]['id'] . '.0;id=' . $row['ID_ATTACH'],
'link' => '<a href="' . $scripturl . '?action=dlattach;topic=' . $return[$row['ID_MSG']]['id'] . '.0;id=' . $row['ID_ATTACH'] . '">' . $row['filename'] . '</a>'
);
}
mysql_free_result($request);
return $return;
}


Code is added to show news...


$newsForum = ssi_boardNews2(6.0, 1, null, 300, 'array');
foreach ($newsForum as $news){
$newsImage = '' ;
if( isset( $news[ 'attachments' ][ 0 ] ) ){
$newsImage = '<img class="news" src="' . $news[ 'attachments' ][ 0 ][ 'href' ] . ';image" alt="' . $news[ 'attachments' ][ 0 ][ 'name' ] . '" title="' . $news[ 'attachments' ][ 0 ][ 'name' ] . '" width="120" height="90"/>' ;}

echo '<div class="teaser"><div class="teasercontent">' . $news['subject'] . '
' . $newsImage . $news['body'] . '</div><div class="teaserfooter"><p>Posted b: ' . $news['poster']['link'] . ' on ' . $news['time'] . ' | ' . $news['link'] . ' | ' . $news['new_comment'] . '</p></div> </div>';}


Thanks

Arantor

Hmmm. Not sure off hand whether that's right.

I'm guessing you're using SMF 1.1.10 ?

Seems to me you have two issues here. Can you provide a link to the page where this is supposed to be displayed please?
Holder of controversial views, all of which my own.


robembra

#8
Hi again,

I am using 1.1.10 and i cant provide a link as its running on a intranet. Please tell me what information you would like and I will provide.

Basically what i want to do is to see posts from a certain forum and show the images whether they be [img], [url], or attached image. Also all users including anonymous should be able to see the images.

Thanks again

Arantor

A screenshot of the result, plus the code from the function you're calling (ssi_boardNews) would be handy.

Attachments have to be done separately, I'm currently working on a mod that greatly expands ssi_boardNews with a new function, ssi_multiBoardNews, which allows getting from multiple boards, overriding the permissions check (so that it can be a private board that only admins can normally see in the forum, but that you want to pull out of the forum), plus attachments and avatars (optionally)
Holder of controversial views, all of which my own.


robembra

Hi again...

I have attached a screenshot. And the code in SSI.php is
function ssi_boardNews2($board = null, $limit = null, $start = null, $length = null, $output_method = 'echo')
{

global $scripturl, $db_prefix, $txt;
global $settings, $modSettings, $context;

loadLanguage('Stats');

// Must be integers....

if ($limit === null)
$limit = isset($_GET['limit']) ? (int) $_GET['limit'] : 5;
else
$limit = (int) $limit;

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

if ($board !== null)$board = (int) $board;
elseif (isset($_GET['board']))
$board = (int) $_GET['board'];

if ($length === null)
$length = isset($_GET['length']) ? (int) $_GET['length'] : 0;
else
$length = (int) $length;

// Make sure guests can see this board.
$request = db_query("
SELECT ID_BOARD
FROM {$db_prefix}boards
WHERE " . ($board === null ? '' : "ID_BOARD = $board
AND ") . "FIND_IN_SET(-1, memberGroups)
LIMIT 1", __FILE__, __LINE__);
if (mysql_num_rows($request) == 0)
{
if ($output_method == 'echo')
die($txt['smf_news_error2']);
else
return array();
}
list ($board) = mysql_fetch_row($request);
mysql_free_result($request);

// Find the posts.
$request = db_query("
SELECT
m.icon, m.subject, m.body, IFNULL(mem.realName, m.posterName) AS posterName, m.posterTime,
t.numReplies, t.ID_TOPIC, m.ID_MEMBER, m.smileysEnabled, m.ID_MSG
FROM {$db_prefix}topics AS t, {$db_prefix}messages AS m
LEFT JOIN {$db_prefix}members AS mem ON (mem.ID_MEMBER = m.ID_MEMBER)
WHERE t.ID_BOARD = $board
AND m.ID_MSG = t.ID_FIRST_MSG
ORDER BY m.ID_MSG DESC
LIMIT $start, $limit", __FILE__, __LINE__);
$return = array();
$message_ids = array();
while ($row = mysql_fetch_assoc($request))
{
$message_ids[] = $row['ID_MSG'];
// If we want to limit the length of the post.
if (!empty($length) && strlen($row['body']) > $length)
{
$row['body'] = substr($row['body'], 0, $length);

// The first space or line break. (<br />, etc.)
$cutoff = max(strrpos($row['body'], ' '), strrpos($row['body'], '<'));

if ($cutoff !== false)
$row['body'] = substr($row['body'], 0, $cutoff);
$row['body'] .= '...';
}

$row['body'] = doUBBC($row['body'], $row['smileysEnabled']);

censorText($row['subject']);
censorText($row['body']);

$return[$row['ID_MSG']] = array(
'attachments' => array(),
'id' => $row['ID_TOPIC'],
'icon' => '<img src="' . $settings['images_url'] . '/post/' . $row['icon'] . '.gif" align="middle" alt="' . $row['icon'] . '" />',
'subject' => $row['subject'],
'time' => timeformat($row['posterTime']),
'timestamp' => $row['posterTime'],
'body' => $row['body'],
'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>','replies' => $row['numReplies'],'new_comment' => '<a href="' . $scripturl . '?action=post;topic=' . $row['ID_TOPIC'] . '.' . $row['numReplies'] . '">' . $txt['smf_news_3'] . '</a>','poster' => array('id' => $row['ID_MEMBER'],'name' => $row['posterName'],'href' => !empty($row['ID_MEMBER']) ? $scripturl . '?action=profile;u=' . $row['ID_MEMBER'] : '','link' => !empty($row['ID_MEMBER']) ? '<a href="' . $scripturl . '?action=profile;u=' . $row['ID_MEMBER'] . '">' . $row['posterName'] . '</a>' : $row['posterName'] ),
'is_last' => false
);
}
mysql_free_result($request);


if (empty($return))
return $return;

// Find their attachments.

$request = db_query("
SELECT ID_ATTACH, ID_MSG, filename, size, downloads
FROM {$db_prefix}attachments
WHERE ID_MSG IN (" . implode(', ', $message_ids) . ")", __FILE__, __LINE__);
while ($row = mysql_fetch_assoc($request))
{

$return[$row['ID_MSG']]['attachments'][] = array(
'name' => $row['filename'],
'downloads' => $row['downloads'],
'size' => round($row['size'] / 1024, 2) . ' ' . $txt['smf211'],
'byte_size' => $row['size'],
'href' => $scripturl . '?action=dlattach;topic=' . $return[$row['ID_MSG']]['id'] . '.0;id=' . $row['ID_ATTACH'],
'link' => '<a href="' . $scripturl . '?action=dlattach;topic=' . $return[$row['ID_MSG']]['id'] . '.0;id=' . $row['ID_ATTACH'] . '">' . $row['filename'] . '</a>'
);
}
mysql_free_result($request);
return $return;
}


And the code used to call the funtion...
$newsForum = ssi_boardNews2(6.0, 1, null, 300, 'array');
foreach ($newsForum as $news){
$newsImage = '' ;foreach ($news['attachments'] as $file)$newsImage = '<a href="' . $file['href'] . '">' . $file['name'] . '</a>';echo '<div class="teaser"><div class="teasercontent"><h1 class="news">' . $news['subject'] . '</h1>' . $newsImage . $news['body'] . '</div><div class="teaserfooter"><p>Posted b: ' . $news['poster']['link'] . ' on ' . $news['time'] . ' | ' . $news['link'] . ' | ' . $news['new_comment'] . '</p></div> </div>';}


Thanks again

Arantor

Yeah, that code gets attachments, but it still relies on the guest membergroup being granted permission to view attachments, which by default it isn't.

You'd have to enable that from Permissions, as that's why it's not showing them.
Holder of controversial views, all of which my own.


robembra

Thanks got help Arantor...but if could could help me little more please  :D

How would I go about changing the permissions in the script to allow it to fetch images??? I dont want to change the permissions for the forum itself just for the ssi_boardNews() function.

Thanks again

Quote from: Arantor on November 14, 2009, 12:19:31 PM
Yeah, that code gets attachments, but it still relies on the guest membergroup being granted permission to view attachments, which by default it isn't.

You'd have to enable that from Permissions, as that's why it's not showing them.

Arantor

The link it generates calls upon the internal handling to display the attachments, just as it would in the forum itself. (In fact, if that weren't the case, this code would have broken and not been fixed since 1.1.9 / 2.0 RC1-1)

It will end up being a rewrite to make it support attachments from the forum without making them open to guests normally in the forum, though viewing permissions is a board level permission, so you could turn on per-board permissions and grant viewing of attachments to guests only in that board.
Holder of controversial views, all of which my own.


robembra

Hi again sorry to be a pain.

Ok, i have played around with the Permissions and I have allowed guests to view attachments, but that didn't fix it. So i turned ON "Allow guests to browse the forum" in Features and Options and that made it work but I must have "Allow guests to browse the forum" to be tured OFF.

Is there anyway to do this or should I just add a redirect to login page within the index.php for guests, will that work?

Thanks

Arantor

No, you'll have to have guest browsing of the forum turned on, and the board needs to be visible to guests too.

But you can hide all the other boards from guests by marking them not visible from the board configuration.
Holder of controversial views, all of which my own.


robembra

Thanks for the help Arantor.

I solved this problem by adding these lines to my index.php file

require_once(dirname(__FILE__) . '/SSI.php');
if ($context['user']['is_guest']) {

/* Links Guest are not allowed to see */
    if(!isset($_GET['action'])){
$redirect = "?action=login";
Header('Location: '.$redirect);
}
if(empty($_GET['action'])){
$redirect = "?action=login";
Header('Location: '.$redirect);
}
/* Search */
if($_GET['action'] === "search"){
$redirect = "?action=login";
Header('Location: '.$redirect);
}

}


Its very important that our guest cant see any boards so had to do it this way as "Allow guests to browse the forum" has to be turned ON in oreder to get attachments.

Thanks again.

Arantor

Ouch, I'd hate to see the performance on that. You should NEVER need to include SSI.php from within SMF itself. Just put that check a little further into index.php and it'll still work.

Also, don't use the raw header function (even in SSI). Use redirectexit instead.
Holder of controversial views, all of which my own.


Advertisement: