Viewing Attachments on External Page

All web design discussion, including Ultimate Quiz MOD support.

Moderator: CricketMX Forum Moderators

Post Reply
SMCaD
Greenhorn
Greenhorn
Posts: 9
Joined: Fri Feb 17, 2012 10:39 pm

Thanks to TheGiftedApe's page and to battye for getting my external page going without them knowing :D

I have another question that I figured I'd start up a new thread on...
I am unable to view the attachments that are on the "original posts" that the new page pulls from.

I'm sure it's just a matter of plugging functions in the code to the News.php page, but I've tried doing this by copying the viewtopic.php into News.php and either it errors with sql or just simply doesn't show. You can see the image listed on this page, " dead.png " and to view the actual topic within the forum not on the external page, just click on the title.

http://www.dotrf.net will forward you automatically to the News.php page in question.

And here's the code for the News.php page:

Code: Select all

<?php
/*
 * News.php
 * Description: News File for displaying News topics for guild.
 * Created on: February 17, 2012
 * @package phpBB3
 * @version $Id$
 * @copyright (c) 2005 phpBB Group
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 */

define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'includes/bbcode.' . $phpEx);
include($phpbb_root_path . 'includes/functions_display.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup('viewforum');

/* create_where_clauses( int[] gen_id, String type )
* This function outputs an SQL WHERE statement for use when grabbing 
* posts and topics */

function create_where_clauses($gen_id, $type)
{
global $db, $auth;

    $size_gen_id = sizeof($gen_id);

        switch($type)
        {
            case 'forum':
                $type = 'forum_id';
                break;
            case 'topic':
                $type = 'topic_id';
                break;
            default:
                trigger_error('No type defined');
        }

    // Set $out_where to nothing, this will be used of the gen_id
    // size is empty, in other words "grab from anywhere" with
    // no restrictions
    $out_where = '';

    if( $size_gen_id > 0 )
    {
    // Get a list of all forums the user has permissions to read
    $auth_f_read = array_keys($auth->acl_getf('f_read', true));

        if( $type == 'topic_id' )
        {
            $sql     = 'SELECT topic_id FROM ' . TOPICS_TABLE . '
                        WHERE ' .  $db->sql_in_set('topic_id', $gen_id) . '
                        AND ' .  $db->sql_in_set('forum_id', $auth_f_read);

            $result     = $db->sql_query($sql);

                while( $row = $db->sql_fetchrow($result) )
                {
                        // Create an array with all acceptable topic ids
                        $topic_id_list[] = $row['topic_id'];
                }

            unset($gen_id);

            $gen_id = $topic_id_list;
            $size_gen_id = sizeof($gen_id);
        }

    $j = 0;    

        for( $i = 0; $i < $size_gen_id; $i++ )
        {
        $id_check = (int) $gen_id[$i];

            // If the type is topic, all checks have been made and the query can start to be built
            if( $type == 'topic_id' )
            {
                $out_where .= ($j == 0) ? 'WHERE ' . $type . ' = ' . $id_check . ' ' : 'OR ' . $type . ' = ' . $id_check . ' ';
            }

            // If the type is forum, do the check to make sure the user has read permissions
            else if( $type == 'forum_id' && $auth->acl_get('f_read', $id_check) )
            {
                $out_where .= ($j == 0) ? 'WHERE ' . $type . ' = ' . $id_check . ' ' : 'OR ' . $type . ' = ' . $id_check . ' ';
            }    

        $j++;
        }
    }

    if( $out_where == '' && $size_gen_id > 0 )
    {
        trigger_error('A list of topics/forums has not been created');
    }

    return $out_where;
}

$search_limit = 5;

    $forum_id = array(37);
    $forum_id_where = create_where_clauses($forum_id, 'forum');

    $topic_id = array(619);
    $topic_id_where = create_where_clauses($topic_id, 'topic');

// Example 3: Display posts from specified topics  
$posts_ary = array(
        'SELECT'    => 'p.*, t.*, u.username, u.user_colour',
    
        'FROM'      => array(
            POSTS_TABLE     => 'p',
        ),
    
        'LEFT_JOIN' => array(
            array(
                'FROM'  => array(USERS_TABLE => 'u'),
                'ON'    => 'u.user_id = p.poster_id'
            ),
            array(
                'FROM'  => array(TOPICS_TABLE => 't'),
                'ON'    => 'p.topic_id = t.topic_id'
            ),
        ),
    
        'WHERE'     =>  str_replace( array('WHERE ', 'topic_id'), array('', 't.topic_id'), $topic_id_where) . '
                        AND t.topic_status <> ' . ITEM_MOVED . '
                         AND t.topic_approved = 1',
    
        'ORDER_BY'  => 'p.post_id DESC',
    );
    
    $posts = $db->sql_build_query('SELECT', $posts_ary);

   $posts_result = $db->sql_query_limit($posts, $search_limit);

      while( $posts_row = $db->sql_fetchrow($posts_result) )
      {
         $topic_title       = $posts_row['topic_title'];
         $post_author       = get_username_string('full', $posts_row['poster_id'], $posts_row['username'], $posts_row['user_colour']);
         $post_date          = $user->format_date($posts_row['post_time']);
         $post_link       = append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'f=' . $posts_row['forum_id'] . '&t=' . $posts_row['topic_id'] . '&p=' . $posts_row['post_id']) . '#p' . $posts_row['post_id'];

         $post_text = nl2br($posts_row['post_text']);

         $bbcode = new bbcode(base64_encode($bbcode_bitfield));         
         $bbcode->bbcode_second_pass($post_text, $posts_row['bbcode_uid'], $posts_row['bbcode_bitfield']);

         $post_text = smiley_text($post_text);

         $template->assign_block_vars('announcements', array(
         'TOPIC_TITLE'       => censor_text($topic_title),
         'POST_AUTHOR'       => $post_author,
         'POST_DATE'       => $post_date,
         'POST_LINK'       => $post_link,
         'POST_TEXT'         => censor_text($post_text),
         ));
      }
	
// Output the page - Footer and Conclusion
page_header($user->lang['TITLE_NEWS']);

    $template->set_filenames(array(
        'body' => 'news_body.html'
    ));

    page_footer();
?>
SMCaD
Greenhorn
Greenhorn
Posts: 9
Joined: Fri Feb 17, 2012 10:39 pm

Ok, so I fixed a permission issue, BUT the External page still doesn't show the image that's on the post.
The permission I fixed just now was the ability for a Guest to download files, which allowed the Guest to see the image on the main post but still cannot see it on the External page...

Still need help please :mrgreen:
SMCaD
Greenhorn
Greenhorn
Posts: 9
Joined: Fri Feb 17, 2012 10:39 pm

I'm wondering if adding the following code to the News.php page will help and if so...

Code: Select all

// Does post have an attachment? If so, add it to the list
	if ($row['post_attachment'] && $config['allow_attachments'])
	{
		$attach_list[] = (int) $row['post_id'];

		if ($row['post_approved'])
		{
			$has_attachments = true;
		}
	}
..if I need to have something prior to it to add it into. As normally it is called in this from the viewtopic.php page:

Code: Select all

while ($row = $db->sql_fetchrow($result))
{
But then as I continue to read through the viewtopic.php page I find this code and am also wondering if combining all three into the News.php page would help:

Code: Select all

// Pull attachment data
if (sizeof($attach_list))
{
	if ($auth->acl_get('u_download') && $auth->acl_get('f_download', $forum_id))
	{
		$sql = 'SELECT *
			FROM ' . ATTACHMENTS_TABLE . '
			WHERE ' . $db->sql_in_set('post_msg_id', $attach_list) . '
				AND in_message = 0
			ORDER BY filetime DESC, post_msg_id ASC';
		$result = $db->sql_query($sql);

		while ($row = $db->sql_fetchrow($result))
		{
			$attachments[$row['post_msg_id']][] = $row;
		}
		$db->sql_freeresult($result);

		// No attachments exist, but post table thinks they do so go ahead and reset post_attach flags
		if (!sizeof($attachments))
		{
			$sql = 'UPDATE ' . POSTS_TABLE . '
				SET post_attachment = 0
				WHERE ' . $db->sql_in_set('post_id', $attach_list);
			$db->sql_query($sql);

			// We need to update the topic indicator too if the complete topic is now without an attachment
			if (sizeof($rowset) != $total_posts)
			{
				// Not all posts are displayed so we query the db to find if there's any attachment for this topic
				$sql = 'SELECT a.post_msg_id as post_id
					FROM ' . ATTACHMENTS_TABLE . ' a, ' . POSTS_TABLE . " p
					WHERE p.topic_id = $topic_id
						AND p.post_approved = 1
						AND p.topic_id = a.topic_id";
				$result = $db->sql_query_limit($sql, 1);
				$row = $db->sql_fetchrow($result);
				$db->sql_freeresult($result);

				if (!$row)
				{
					$sql = 'UPDATE ' . TOPICS_TABLE . "
						SET topic_attachment = 0
						WHERE topic_id = $topic_id";
					$db->sql_query($sql);
				}
			}
			else
			{
				$sql = 'UPDATE ' . TOPICS_TABLE . "
					SET topic_attachment = 0
					WHERE topic_id = $topic_id";
				$db->sql_query($sql);
			}
		}
		else if ($has_attachments && !$topic_data['topic_attachment'])
		{
			// Topic has approved attachments but its flag is wrong
			$sql = 'UPDATE ' . TOPICS_TABLE . "
				SET topic_attachment = 1
				WHERE topic_id = $topic_id";
			$db->sql_query($sql);

			$topic_data['topic_attachment'] = 1;
		}
	}
	else
	{
		$display_notice = true;
	}
}
SMCaD
Greenhorn
Greenhorn
Posts: 9
Joined: Fri Feb 17, 2012 10:39 pm

Yea..., that didn't do anything... haha

Code: Select all

<?php
/*
 * News.php
 * Description: News File for displaying News topics for guild.
 * Created on: February 17, 2012
 * @package phpBB3
 * @version $Id$
 * @copyright (c) 2005 phpBB Group
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 */

define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'includes/bbcode.' . $phpEx);
include($phpbb_root_path . 'includes/functions_display.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup('viewforum');

/* create_where_clauses( int[] gen_id, String type )
* This function outputs an SQL WHERE statement for use when grabbing 
* posts and topics */

function create_where_clauses($gen_id, $type)
{
global $db, $auth;

    $size_gen_id = sizeof($gen_id);

        switch($type)
        {
            case 'forum':
                $type = 'forum_id';
                break;
            case 'topic':
                $type = 'topic_id';
                break;
            default:
                trigger_error('No type defined');
        }

    // Set $out_where to nothing, this will be used of the gen_id
    // size is empty, in other words "grab from anywhere" with
    // no restrictions
    $out_where = '';

    if( $size_gen_id > 0 )
    {
    // Get a list of all forums the user has permissions to read
    $auth_f_read = array_keys($auth->acl_getf('f_read', true));

        if( $type == 'topic_id' )
        {
            $sql     = 'SELECT topic_id FROM ' . TOPICS_TABLE . '
                        WHERE ' .  $db->sql_in_set('topic_id', $gen_id) . '
                        AND ' .  $db->sql_in_set('forum_id', $auth_f_read);

            $result     = $db->sql_query($sql);

                while( $row = $db->sql_fetchrow($result) )
                {
                        // Create an array with all acceptable topic ids
                        $topic_id_list[] = $row['topic_id'];
                }

            unset($gen_id);

            $gen_id = $topic_id_list;
            $size_gen_id = sizeof($gen_id);
        }

    $j = 0;    

        for( $i = 0; $i < $size_gen_id; $i++ )
        {
        $id_check = (int) $gen_id[$i];

            // If the type is topic, all checks have been made and the query can start to be built
            if( $type == 'topic_id' )
            {
                $out_where .= ($j == 0) ? 'WHERE ' . $type . ' = ' . $id_check . ' ' : 'OR ' . $type . ' = ' . $id_check . ' ';
            }

            // If the type is forum, do the check to make sure the user has read permissions
            else if( $type == 'forum_id' && $auth->acl_get('f_read', $id_check) )
            {
                $out_where .= ($j == 0) ? 'WHERE ' . $type . ' = ' . $id_check . ' ' : 'OR ' . $type . ' = ' . $id_check . ' ';
            }    

        $j++;
        }
    }

    if( $out_where == '' && $size_gen_id > 0 )
    {
        trigger_error('A list of topics/forums has not been created');
    }

    return $out_where;
}

$search_limit = 5;

    $forum_id = array(37);
    $forum_id_where = create_where_clauses($forum_id, 'forum');

    $topic_id = array(619);
    $topic_id_where = create_where_clauses($topic_id, 'topic');
	
// Lines 115 thru 200 = Attachment addition

while ($row = $db->sql_fetchrow($result))
{
// Does post have an attachment? If so, add it to the list
   if ($row['post_attachment'] && $config['allow_attachments'])
   {
      $attach_list[] = (int) $row['post_id'];

      if ($row['post_approved'])
      {
         $has_attachments = true;
      }
   }
}

    // Pull attachment data
    if (sizeof($attach_list))
    {
       if ($auth->acl_get('u_download') && $auth->acl_get('f_download', $forum_id))
       {
          $sql = 'SELECT *
             FROM ' . ATTACHMENTS_TABLE . '
             WHERE ' . $db->sql_in_set('post_msg_id', $attach_list) . '
                AND in_message = 0
             ORDER BY filetime DESC, post_msg_id ASC';
          $result = $db->sql_query($sql);

          while ($row = $db->sql_fetchrow($result))
          {
             $attachments[$row['post_msg_id']][] = $row;
          }
          $db->sql_freeresult($result);

          // No attachments exist, but post table thinks they do so go ahead and reset post_attach flags
          if (!sizeof($attachments))
          {
             $sql = 'UPDATE ' . POSTS_TABLE . '
                SET post_attachment = 0
                WHERE ' . $db->sql_in_set('post_id', $attach_list);
             $db->sql_query($sql);

             // We need to update the topic indicator too if the complete topic is now without an attachment
             if (sizeof($rowset) != $total_posts)
             {
                // Not all posts are displayed so we query the db to find if there's any attachment for this topic
                $sql = 'SELECT a.post_msg_id as post_id
                   FROM ' . ATTACHMENTS_TABLE . ' a, ' . POSTS_TABLE . " p
                   WHERE p.topic_id = $topic_id
                      AND p.post_approved = 1
                      AND p.topic_id = a.topic_id";
                $result = $db->sql_query_limit($sql, 1);
                $row = $db->sql_fetchrow($result);
                $db->sql_freeresult($result);

                if (!$row)
                {
                   $sql = 'UPDATE ' . TOPICS_TABLE . "
                      SET topic_attachment = 0
                      WHERE topic_id = $topic_id";
                   $db->sql_query($sql);
                }
             }
             else
             {
                $sql = 'UPDATE ' . TOPICS_TABLE . "
                   SET topic_attachment = 0
                   WHERE topic_id = $topic_id";
                $db->sql_query($sql);
             }
          }
          else if ($has_attachments && !$topic_data['topic_attachment'])
          {
             // Topic has approved attachments but its flag is wrong
             $sql = 'UPDATE ' . TOPICS_TABLE . "
                SET topic_attachment = 1
                WHERE topic_id = $topic_id";
             $db->sql_query($sql);

             $topic_data['topic_attachment'] = 1;
          }
       }
       else
       {
          $display_notice = true;
       }
    }

// Example 3: Display posts from specified topics  
$posts_ary = array(
        'SELECT'    => 'p.*, t.*, u.username, u.user_colour',
    
        'FROM'      => array(
            POSTS_TABLE     => 'p',
        ),
    
        'LEFT_JOIN' => array(
            array(
                'FROM'  => array(USERS_TABLE => 'u'),
                'ON'    => 'u.user_id = p.poster_id'
            ),
            array(
                'FROM'  => array(TOPICS_TABLE => 't'),
                'ON'    => 'p.topic_id = t.topic_id'
            ),
        ),
    
        'WHERE'     =>  str_replace( array('WHERE ', 'topic_id'), array('', 't.topic_id'), $topic_id_where) . '
                        AND t.topic_status <> ' . ITEM_MOVED . '
                         AND t.topic_approved = 1',
    
        'ORDER_BY'  => 'p.post_id DESC',
    );
    
    $posts = $db->sql_build_query('SELECT', $posts_ary);

   $posts_result = $db->sql_query_limit($posts, $search_limit);

      while( $posts_row = $db->sql_fetchrow($posts_result) )
      {
         $topic_title       = $posts_row['topic_title'];
         $post_author       = get_username_string('full', $posts_row['poster_id'], $posts_row['username'], $posts_row['user_colour']);
         $post_date          = $user->format_date($posts_row['post_time']);
         $post_link       = append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'f=' . $posts_row['forum_id'] . '&t=' . $posts_row['topic_id'] . '&p=' . $posts_row['post_id']) . '#p' . $posts_row['post_id'];

         $post_text = nl2br($posts_row['post_text']);

         $bbcode = new bbcode(base64_encode($bbcode_bitfield));         
         $bbcode->bbcode_second_pass($post_text, $posts_row['bbcode_uid'], $posts_row['bbcode_bitfield']);

         $post_text = smiley_text($post_text);

         $template->assign_block_vars('announcements', array(
         'TOPIC_TITLE'       => censor_text($topic_title),
         'POST_AUTHOR'       => $post_author,
         'POST_DATE'       => $post_date,
         'POST_LINK'       => $post_link,
         'POST_TEXT'         => censor_text($post_text),
         ));
      }
	
// Output the page - Footer and Conclusion
page_header($user->lang['TITLE_NEWS']);

    $template->set_filenames(array(
        'body' => 'news_body.html'
    ));

    page_footer();
?>
Here's my news_body.html file:

Code: Select all

<!-- INCLUDE overall_header.html -->

	<!-- BEGIN announcements -->
    <div align="center"><h2><a href="{U_GUILDNEWS}" target="_self">{announcements.TOPIC_TITLE}</a></h2></div>
    <div align="center"><table width="800" border="0" cellspacing="0" cellpadding="0" algin="center">
  		<tr>
    		<td><div class="entete"></div></td>
  		</tr>
  		<tr>
    		<td class="bg1">Post author: {announcements.POST_AUTHOR}<br />
		Post date: {announcements.POST_DATE}<br />
		<br />
        <div class="content">{announcements.POST_TEXT}</div>
        
        <!-- IF postrow.S_HAS_ATTACHMENTS -->
				<dl class="attachbox">
					<dt>{L_ATTACHMENTS}</dt>
					<!-- BEGIN attachment -->
						<dd>{postrow.attachment.DISPLAY_ATTACHMENT}</dd>
					<!-- END attachment -->
				</dl>
			<!-- ENDIF -->
            </td>
  		</tr>
  		<tr>
    		<td height="10"><hr class="divider" /></td>
  		</tr>
	</table></div>	
	<!-- END announcements -->
    <br />
	<br />
	<br />
	<br />
	<br />

<!-- INCLUDE overall_footer_news.html -->
User avatar
battye
Site Admin
Site Admin
Posts: 14395
Joined: Sun Jan 11, 2004 8:26 am
Location: Australia
Contact:

Hi SMCaD, I just wanted to let you know I've seen the topic and I'll have a greater look at the code tomorrow. At a glance, it looks like you are on the right track. Your idea of checking to see how it's done in viewtopic.php is a good idea and what I would do too. I think the only thing you are missing is a call to parse_attachment; it's explained a little bit at http://www.phpbb.com/community/viewtopi ... 5#p6846785 :)
CricketMX.com in 2022: Still the home of bat's, rat's and other farmyard animals!

"OK, life [as you chose to define it] repeats until there are no more lessons to be learned." - nrnoble (June 12, 2005)
"the new forum looks awesome, it's getting bigger & better" - p2p-sharing-rules (11 Jan, 2008)
"Looks like CMX is not only getting bigger...but, also getting better!!" - moongirl (14 Dec, 2007)
SMCaD
Greenhorn
Greenhorn
Posts: 9
Joined: Fri Feb 17, 2012 10:39 pm

On that section of the page you pointed me to they say to find the following code:

Code: Select all

                $message = bbcode_nl2br($message);
                $message = smiley_text($message);
On my page I found this message which appears to be approx... the same'ish... but is the only section that has it.

Code: Select all

         $post_text = nl2br($posts_row['post_text']);

         $bbcode = new bbcode(base64_encode($bbcode_bitfield));         
         $bbcode->bbcode_second_pass($post_text, $posts_row['bbcode_uid'], $posts_row['bbcode_bitfield']);

         $post_text = smiley_text($post_text);
So I'm gonna post the next section that they said to directly after that to see if it works.

Code: Select all

     // Parse inline attachments
                                $sql_attach = 'SELECT *
                                        FROM ' . ATTACHMENTS_TABLE . '
                                        WHERE ' . $db->sql_in_set('post_msg_id', $row['topic_first_post_id']) . '
                                                AND in_message = 0
                                        ORDER BY filetime DESC, post_msg_id ASC';

                                $result_attach = $db->sql_query($sql_attach);

                                while ($row_attach = $db->sql_fetchrow($result_attach))
                                {
                                        $attachments[$row_attach['post_msg_id']][] = $row_attach;
                                }
                                $db->sql_freeresult($result_attach);

                                if (!empty($attachments[$row['topic_first_post_id']]))
                                {
                                        parse_attachments($forum_id, $message, $attachments[$row['topic_first_post_id']], $update_count);
                                }
SMCaD
Greenhorn
Greenhorn
Posts: 9
Joined: Fri Feb 17, 2012 10:39 pm

Nope, caused an SQL Error
[0]

...and so the hunt continues...
User avatar
battye
Site Admin
Site Admin
Posts: 14395
Joined: Sun Jan 11, 2004 8:26 am
Location: Australia
Contact:

It was the "Parse inline attachments" query that returned an SQL error? Let's see if we can narrow down where the code isn't working.

Before $result_attach = $db->sql_query($sql_attach); add this:

Code: Select all

echo $sql_attach;
That way we can find out what is actually in the query and if something in the where clause is causing it to fail.

If that looks okay, also try adding a new line directly before the line parse_attachments( ... with an echo statement (ie. echo $row['topic_first_post_id'];) to see if it is entering that if statement.
CricketMX.com in 2022: Still the home of bat's, rat's and other farmyard animals!

"OK, life [as you chose to define it] repeats until there are no more lessons to be learned." - nrnoble (June 12, 2005)
"the new forum looks awesome, it's getting bigger & better" - p2p-sharing-rules (11 Jan, 2008)
"Looks like CMX is not only getting bigger...but, also getting better!!" - moongirl (14 Dec, 2007)
DunnElden
Greenhorn
Greenhorn
Posts: 2
Joined: Mon Oct 14, 2013 4:39 am

I got a little further with my effort to display a full size attachment in an external page. If I use this code...
DunnElden
Post Reply