Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 61
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
forum
0.00% covered (danger)
0.00%
0 / 61
0.00% covered (danger)
0.00%
0 / 5
342
0.00% covered (danger)
0.00%
0 / 1
 set_forum_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 / 26
0.00% covered (danger)
0.00%
0 / 1
90
 get_sql
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 1
12
 adjust_item
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
 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_forum_exception;
18use phpbb\feed\exception\unauthorized_forum_exception;
19
20/**
21 * Forum feed
22 *
23 * This will give you the last {$this->num_items} posts made
24 * within a specific forum.
25 */
26class forum extends post_base
27{
28    protected $forum_id        = 0;
29    protected $forum_data    = array();
30
31    /**
32     * Set the Forum ID
33     *
34     * @param int    $forum_id            Forum ID
35     * @return    \phpbb\feed\forum
36     */
37    public function set_forum_id($forum_id)
38    {
39        $this->forum_id = (int) $forum_id;
40
41        return $this;
42    }
43
44    /**
45     * {@inheritdoc}
46     */
47    public function open()
48    {
49        // Check if forum exists
50        $sql = 'SELECT forum_id, forum_name, forum_password, forum_type, forum_options
51            FROM ' . FORUMS_TABLE . '
52            WHERE forum_id = ' . $this->forum_id;
53        $result = $this->db->sql_query($sql);
54        $this->forum_data = $this->db->sql_fetchrow($result);
55        $this->db->sql_freeresult($result);
56
57        if (empty($this->forum_data))
58        {
59            throw new no_forum_exception($this->forum_id);
60        }
61
62        // Forum needs to be postable
63        if ($this->forum_data['forum_type'] != FORUM_POST)
64        {
65            throw new no_feed_exception();
66        }
67
68        // Make sure forum is not excluded from feed
69        if (phpbb_optionget(FORUM_OPTION_FEED_EXCLUDE, $this->forum_data['forum_options']))
70        {
71            throw new no_feed_exception();
72        }
73
74        // Make sure we can read this forum
75        if (!$this->auth->acl_get('f_read', $this->forum_id))
76        {
77            if ($this->user->data['user_id'] != ANONYMOUS)
78            {
79                send_status_line(403, 'Forbidden');
80            }
81            else
82            {
83                send_status_line(401, 'Unauthorized');
84            }
85            throw new unauthorized_forum_exception($this->forum_id);
86        }
87
88        // Make sure forum is not passworded or user is authed
89        if ($this->forum_data['forum_password'])
90        {
91            $forum_ids_passworded = $this->get_passworded_forums();
92
93            if (isset($forum_ids_passworded[$this->forum_id]))
94            {
95                if ($this->user->data['user_id'] != ANONYMOUS)
96                {
97                    send_status_line(403, 'Forbidden');
98                }
99                else
100                {
101                    send_status_line(401, 'Unauthorized');
102                }
103                throw new unauthorized_forum_exception($this->forum_id);
104            }
105
106            unset($forum_ids_passworded);
107        }
108
109        parent::open();
110    }
111
112    /**
113     * {@inheritdoc}
114     */
115    protected function get_sql()
116    {
117        // Determine topics with recent activity
118        $sql = 'SELECT topic_id, topic_last_post_time
119            FROM ' . TOPICS_TABLE . '
120            WHERE forum_id = ' . $this->forum_id . '
121                AND topic_moved_id = 0
122                AND ' . $this->content_visibility->get_visibility_sql('topic', $this->forum_id) . '
123            ORDER BY topic_last_post_time DESC, topic_last_post_id DESC';
124        $result = $this->db->sql_query_limit($sql, $this->num_items);
125
126        $topic_ids = array();
127        $min_post_time = 0;
128        while ($row = $this->db->sql_fetchrow())
129        {
130            $topic_ids[] = (int) $row['topic_id'];
131
132            $min_post_time = (int) $row['topic_last_post_time'];
133        }
134        $this->db->sql_freeresult($result);
135
136        if (empty($topic_ids))
137        {
138            return false;
139        }
140
141        parent::fetch_attachments(array(), $topic_ids);
142
143        $this->sql = array(
144            'SELECT'    =>    'p.post_id, p.topic_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, ' .
145                'u.username, u.user_id',
146            'FROM'        => array(
147                POSTS_TABLE        => 'p',
148                USERS_TABLE        => 'u',
149            ),
150            'WHERE'        => $this->db->sql_in_set('p.topic_id', $topic_ids) . '
151                            AND ' . $this->content_visibility->get_visibility_sql('post', $this->forum_id, 'p.') . '
152                            AND p.post_time >= ' . $min_post_time . '
153                            AND p.poster_id = u.user_id',
154            'ORDER_BY'    => 'p.post_time DESC, p.post_id DESC',
155        );
156
157        return true;
158    }
159
160    /**
161     * {@inheritdoc}
162     */
163    public function adjust_item(&$item_row, &$row)
164    {
165        parent::adjust_item($item_row, $row);
166
167        $item_row['title'] = (isset($row['forum_name']) && $row['forum_name'] !== '') ? $row['forum_name'] . ' ' . $this->separator . ' ' . $item_row['title'] : $item_row['title'];
168        $item_row['forum_id'] = $this->forum_id;
169    }
170
171    /**
172     * {@inheritdoc}
173     */
174    public function get_item()
175    {
176        return ($row = parent::get_item()) ? array_merge($this->forum_data, $row) : $row;
177    }
178}