Location: PHPKode > projects > Gnew > Gnew-2013.1/posts/read.php
<?php
// -----------------------------------------------------------------------------
// $Id: read.php 682 2013-03-28 14:17:41Z raoul $
//
// Copyright (C) 2013 Raoul Proença
// License: GNU GPL version 3 (see copying.txt file)
// Website: http://www.gnew.fr/
// -----------------------------------------------------------------------------
// This program is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation, either version 3 of the License, or (at your option) any later
// version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along with
// this program. If not, see <http://www.gnu.org/licenses/>.
// -----------------------------------------------------------------------------

include('./../includes/common.php');

page_header($lang['POSTS_READ']);

// Check query
if ((!empty($_GET['category_id']) && string_is_int($_GET['category_id'])) && (!empty($_GET['thread_id']) && string_is_int($_GET['thread_id'])))
{
    // Create pages list
    if (empty($_GET['page']) || !string_is_int($_GET['page']))
    {
        $_GET['page'] = 1;
    }
    $posts_per_page = $settings['posts_per_page'];
    $posts_offset = ($_GET['page'] - 1) * $posts_per_page;
    $sql->query('SELECT post_id
                 FROM ' . TABLE_POSTS . '
                 WHERE thread_id = \'' . $_GET['thread_id'] . '\'');
    $num_posts = $sql->num_rows();
    $num_pages = ceil($num_posts / $posts_per_page);
    $pages_list = get_pages_list('./../posts/read.php?category_id=' . $_GET['category_id'] . '&amp;thread_id=' . $_GET['thread_id'] . '&amp;', $num_pages);

    // Only administrators can read private threads
    if ($users['user_level'] < 4)
    {
        $clause = ' AND t2.category_level IN (\'3\', \'4\')';
    }
    else
    {
        $clause = ' AND t2.category_level IN (\'0\', \'3\', \'4\')';
    }
    $template->set_file('read', 'posts/read.htpl');
    $template->set_block('read', 'POSTS_BLOCK', 'posts');
    $sql->query('SELECT t1.post_id, t1.user_id, t1.post_subject, t1.post_text, t1.post_creation, t1.post_edition, t1.edition_id, t1.edition_name, t3.user_avatar, t3.user_name, t3.user_posts, t3.user_signature, t3.user_creation
                 FROM ' . TABLE_POSTS . ' AS t1
                 INNER JOIN ' . TABLE_CATEGORIES . ' AS t2
                             ON (t2.category_id = t1.category_id' . $clause . ')
                 INNER JOIN ' . TABLE_USERS . ' AS t3
                             ON (t3.user_id = t1.user_id)
                 WHERE t1.category_id = \'' . $_GET['category_id'] . '\'
                 AND t1.thread_id = \'' . $_GET['thread_id'] . '\'
                 ORDER BY t1.' . $settings['posts_sorting'] . ' ' . $settings['posts_order'] . '
                 LIMIT ' . $posts_per_page . ' OFFSET ' . $posts_offset);
    while ($table_posts = $sql->fetch())
    {
        if ($table_posts['post_edition'])
        {
            $post_edited = sprintf($lang['EDITED_BY'], $table_posts['edition_id'], $table_posts['edition_name'], format_date($table_posts['post_edition']));
        }
        else
        {
            $post_edited = '';
        }
        if (!empty($table_posts['user_signature']))
        {
            $user_signature = '<br />&#45;&#45;<br />' . $table_posts['user_signature'];
        }
        else
        {
            $user_signature = '';
        }
        // User logged?
        if ($_SESSION['user_id'] > 0)
        {
            // Administrator
            if ($users['user_level'] == 4)
            {
                $edit_link = '<a href="./../admin/posts.php?post_id=' . $table_posts['post_id'] . '" title="' . $lang['EDIT'] . '">' . $lang['EDIT'] . '</a>';
            }
            // Normal user
            elseif ($table_posts['user_id'] == $_SESSION['user_id'])
            {
                $edit_link = '<a href="./../posts/edit.php?post_id=' . $table_posts['post_id'] . '" title="' . $lang['EDIT'] . '">' . $lang['EDIT'] . '</a>';
            }
            else
            {
                $edit_link = '';
            }
            // New posts since last visit
            if ($table_posts['post_creation'] > $users['user_last_visit'])
            {
                $subject_class = 'new';
            }
            else
            {
                $subject_class = 'old';
            }
        }
        else
        {
            $edit_link = '';
            $subject_class = 'old';
        }
        $post_text = undo_escape_sequences($table_posts['post_text']);
        $template->set_var(array('EDIT_LINK' => $edit_link,
                                 'POST_EDITED' => $post_edited,
                                 'POST_ID' => $table_posts['post_id'],
                                 'POST_POSTED' => format_date($table_posts['post_creation']),
                                 'POST_SUBJECT' => $table_posts['post_subject'],
                                 'POST_TEXT' => $post_text,
                                 'SUBJECT_CLASS' => $subject_class,
                                 'USER_AVATAR' => $table_posts['user_avatar'],
                                 'USER_CREATION' => format_date($table_posts['user_creation']),
                                 'USER_ID' => $table_posts['user_id'],
                                 'USER_NAME' => $table_posts['user_name'],
                                 'USER_POSTS' => $table_posts['user_posts'],
                                 'USER_SIGNATURE' => $user_signature));
        $template->parse('POSTS_BLOCK', 'posts', true);
        $posts_exist = true;
    }
    if (isset($posts_exist))
    {
        if ($settings['allow_html'])
        {
            $html_support = $lang['HTML_ENABLED'];
        }
        else
        {
            $html_support = $lang['HTML_DISABLED'];
        }
        $template->set_var(array('CATEGORY_ID' => $_GET['category_id'],
                                 'EMOTICONS_LIST' => get_emoticons_list(0),
                                 'HTML_SUPPORT' => $html_support,
                                 'PAGES' => sprintf($lang['PAGES'], $pages_list),
                                 'THREAD_ID' => $_GET['thread_id']));
        $template->parse('read', null, false, array(TABLE_CATEGORIES, TABLE_EMOTICONS, TABLE_POSTS, TABLE_USERS));
    }
    else
    {
        error_template($lang['ERROR_NO_DATA']);
    }
}
else
{
    error_template($lang['ERROR_NO_DATA']);
}

page_footer();

?>
Return current item: Gnew