Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 50
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
topic
0.00% covered (danger)
0.00%
0 / 50
0.00% covered (danger)
0.00%
0 / 5
272
0.00% covered (danger)
0.00%
0 / 1
 set_topic_id
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 open
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 1
132
 get_sql
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
2
 adjust_item
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 get_item
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 *
4 * This file is part of the phpBB Forum Software package.
5 *
6 * @copyright (c) phpBB Limited <https://www.phpbb.com>
7 * @license GNU General Public License, version 2 (GPL-2.0)
8 *
9 * For full copyright and license information, please see
10 * the docs/CREDITS.txt file.
11 *
12 */
13
14namespace phpbb\feed;
15
16use phpbb\feed\exception\no_feed_exception;
17use phpbb\feed\exception\no_topic_exception;
18use phpbb\feed\exception\unauthorized_forum_exception;
19use phpbb\feed\exception\unauthorized_topic_exception;
20
21/**
22 * Topic feed for a specific topic
23 *
24 * This will give you the last {$this->num_items} posts made within this topic.
25 */
26class topic extends post_base
27{
28    protected $topic_id        = 0;
29    protected $forum_id        = 0;
30    protected $topic_data    = array();
31
32    /**
33     * Set the Topic ID
34     *
35     * @param int    $topic_id            Topic ID
36     * @return    \phpbb\feed\topic
37     */
38    public function set_topic_id($topic_id)
39    {
40        $this->topic_id = (int) $topic_id;
41
42        return $this;
43    }
44
45    /**
46     * {@inheritdoc}
47     */
48    public function open()
49    {
50        $sql = 'SELECT f.forum_options, f.forum_password, t.topic_id, t.forum_id, t.topic_visibility, t.topic_title, t.topic_time, t.topic_views, t.topic_posts_approved, t.topic_type
51            FROM ' . TOPICS_TABLE . ' t
52            LEFT JOIN ' . FORUMS_TABLE . ' f
53                ON (f.forum_id = t.forum_id)
54            WHERE t.topic_id = ' . $this->topic_id;
55        $result = $this->db->sql_query($sql);
56        $this->topic_data = $this->db->sql_fetchrow($result);
57        $this->db->sql_freeresult($result);
58
59        if (empty($this->topic_data))
60        {
61            throw new no_topic_exception($this->topic_id);
62        }
63
64        $this->forum_id = (int) $this->topic_data['forum_id'];
65
66        // Make sure topic is either approved or user authed
67        if ($this->topic_data['topic_visibility'] != ITEM_APPROVED && !$this->auth->acl_get('m_approve', $this->forum_id))
68        {
69            if ($this->user->data['user_id'] != ANONYMOUS)
70            {
71                send_status_line(403, 'Forbidden');
72            }
73            else
74            {
75                send_status_line(401, 'Unauthorized');
76            }
77            throw new unauthorized_topic_exception($this->topic_id);
78        }
79
80        // Make sure forum is not excluded from feed
81        if (phpbb_optionget(FORUM_OPTION_FEED_EXCLUDE, $this->topic_data['forum_options']))
82        {
83            throw new no_feed_exception();
84        }
85
86        // Make sure we can read this forum
87        if (!$this->auth->acl_get('f_read', $this->forum_id))
88        {
89            if ($this->user->data['user_id'] != ANONYMOUS)
90            {
91                send_status_line(403, 'Forbidden');
92            }
93            else
94            {
95                send_status_line(401, 'Unauthorized');
96            }
97            throw new unauthorized_forum_exception($this->forum_id);
98        }
99
100        // Make sure forum is not passworded or user is authed
101        if ($this->topic_data['forum_password'])
102        {
103            $forum_ids_passworded = $this->get_passworded_forums();
104
105            if (isset($forum_ids_passworded[$this->forum_id]))
106            {
107                if ($this->user->data['user_id'] != ANONYMOUS)
108                {
109                    send_status_line(403, 'Forbidden');
110                }
111                else
112                {
113                    send_status_line(401, 'Unauthorized');
114                }
115                throw new unauthorized_forum_exception($this->forum_id);
116            }
117
118            unset($forum_ids_passworded);
119        }
120
121        parent::open();
122    }
123
124    /**
125     * {@inheritdoc}
126     */
127    protected function get_sql()
128    {
129        parent::fetch_attachments();
130
131        $this->sql = array(
132            'SELECT'    =>    'p.post_id, p.post_time, p.post_edit_time, p.post_visibility, p.post_subject, p.post_text, p.bbcode_bitfield, p.bbcode_uid, p.enable_bbcode, p.enable_smilies, p.enable_magic_url, p.post_attachment, ' .
133                'u.username, u.user_id',
134            'FROM'        => array(
135                POSTS_TABLE        => 'p',
136                USERS_TABLE        => 'u',
137            ),
138            'WHERE'        => 'p.topic_id = ' . $this->topic_id . '
139                                AND ' . $this->content_visibility->get_visibility_sql('post', $this->forum_id, 'p.') . '
140                                AND p.poster_id = u.user_id',
141            'ORDER_BY'    => 'p.post_time DESC, p.post_id DESC',
142        );
143
144        return true;
145    }
146
147    /**
148     * {@inheritdoc}
149     */
150    public function adjust_item(&$item_row, &$row)
151    {
152        parent::adjust_item($item_row, $row);
153
154        $item_row['forum_id'] = $this->forum_id;
155    }
156
157    /**
158     * {@inheritdoc}
159     */
160    public function get_item()
161    {
162        return ($row = parent::get_item()) ? array_merge($this->topic_data, $row) : $row;
163    }
164}