Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
19.05% covered (danger)
19.05%
12 / 63
7.14% covered (danger)
7.14%
1 / 14
CRAP
0.00% covered (danger)
0.00%
0 / 1
base
19.05% covered (danger)
19.05%
12 / 63
7.14% covered (danger)
7.14%
1 / 14
475.15
0.00% covered (danger)
0.00%
0 / 1
 __construct
78.57% covered (warning)
78.57%
11 / 14
0.00% covered (danger)
0.00%
0 / 1
3.09
 set_keys
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 open
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 close
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 set
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 get_item
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
12
 get_readable_forums
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 get_moderator_approve_forums
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 is_moderator_approve_forum
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 get_excluded_forums
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
20
 is_excluded_forum
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 get_passworded_forums
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 user_viewprofile
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 get_sql
n/a
0 / 0
n/a
0 / 0
0
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
16/**
17 * Base class with some generic functions and settings.
18 */
19abstract class base implements feed_interface
20{
21    /**
22     * Feed helper object
23     * @var \phpbb\feed\helper
24     */
25    protected $helper;
26
27    /** @var \phpbb\config\config */
28    protected $config;
29
30    /** @var \phpbb\db\driver\driver_interface */
31    protected $db;
32
33    /** @var \phpbb\cache\driver\driver_interface */
34    protected $cache;
35
36    /** @var \phpbb\user */
37    protected $user;
38
39    /** @var \phpbb\auth\auth */
40    protected $auth;
41
42    /** @var \phpbb\content_visibility */
43    protected $content_visibility;
44
45    /** @var \phpbb\event\dispatcher_interface */
46    protected $phpbb_dispatcher;
47
48    /** @var string */
49    protected $phpEx;
50
51    /**
52     * SQL Query to be executed to get feed items
53     */
54    protected $sql = array();
55
56    /**
57     * Keys specified for retrieval of title, content, etc.
58     */
59    protected $keys = array();
60
61    /**
62     * Number of items to fetch. Usually overwritten by $config['feed_something']
63     */
64    protected $num_items = 15;
65
66    /**
67     * Separator for title elements to separate items (for example forum / topic)
68     */
69    protected $separator = "\xE2\x80\xA2"; // &bull;
70
71    /**
72     * Separator for the statistics row (Posted by, post date, replies, etc.)
73     */
74    protected $separator_stats = "\xE2\x80\x94"; // &mdash;
75
76    /** @var mixed Query result handle */
77    protected $result;
78
79    /**
80     * Constructor
81     *
82     * @param \phpbb\feed\helper                    $helper        Feed helper
83     * @param \phpbb\config\config                $config        Config object
84     * @param \phpbb\db\driver\driver_interface    $db            Database connection
85     * @param \phpbb\cache\driver\driver_interface    $cache    Cache object
86     * @param \phpbb\user                        $user        User object
87     * @param \phpbb\auth\auth                    $auth        Auth object
88     * @param \phpbb\content_visibility            $content_visibility        Auth object
89     * @param \phpbb\event\dispatcher_interface    $phpbb_dispatcher        Event dispatcher object
90     * @param string                                $phpEx        php file extension
91     */
92    public function __construct(
93        \phpbb\feed\helper $helper,
94        \phpbb\config\config $config,
95        \phpbb\db\driver\driver_interface $db,
96        \phpbb\cache\driver\driver_interface $cache,
97        \phpbb\user $user,
98        \phpbb\auth\auth $auth,
99        \phpbb\content_visibility $content_visibility,
100        \phpbb\event\dispatcher_interface $phpbb_dispatcher,
101        $phpEx
102    )
103    {
104        $this->config = $config;
105        $this->helper = $helper;
106        $this->db = $db;
107        $this->cache = $cache;
108        $this->user = $user;
109        $this->auth = $auth;
110        $this->content_visibility = $content_visibility;
111        $this->phpbb_dispatcher = $phpbb_dispatcher;
112        $this->phpEx = $phpEx;
113
114        $this->set_keys();
115
116        // Allow num_items to be string
117        if (is_string($this->num_items))
118        {
119            $this->num_items = (int) $this->config[$this->num_items];
120
121            // A precaution
122            if (!$this->num_items)
123            {
124                $this->num_items = 10;
125            }
126        }
127    }
128
129    /**
130     * {@inheritdoc}
131     */
132    public function set_keys()
133    {
134    }
135
136    /**
137     * {@inheritdoc}
138     */
139    public function open()
140    {
141    }
142
143    /**
144     * {@inheritdoc}
145     */
146    public function close()
147    {
148        if (!empty($this->result))
149        {
150            $this->db->sql_freeresult($this->result);
151        }
152    }
153
154    /**
155     * {@inheritdoc}
156     */
157    public function set($key, $value)
158    {
159        $this->keys[$key] = $value;
160    }
161
162    /**
163     * {@inheritdoc}
164     */
165    public function get($key)
166    {
167        return (isset($this->keys[$key])) ? $this->keys[$key] : null;
168    }
169
170    /**
171     * {@inheritdoc}
172     */
173    public function get_item()
174    {
175        if (!isset($this->result))
176        {
177            if (!$this->get_sql())
178            {
179                return false;
180            }
181
182            $sql_ary = $this->sql;
183
184            /**
185             * Event to modify the feed item sql
186             *
187             * @event core.feed_base_modify_item_sql
188             * @var    array    sql_ary        The SQL array to get the feed item data
189             *
190             * @since 3.1.10-RC1
191             */
192            $vars = array('sql_ary');
193            extract($this->phpbb_dispatcher->trigger_event('core.feed_base_modify_item_sql', compact($vars)));
194            $this->sql = $sql_ary;
195            unset($sql_ary);
196
197            // Query database
198            $sql = $this->db->sql_build_query('SELECT', $this->sql);
199            $this->result = $this->db->sql_query_limit($sql, $this->num_items);
200        }
201
202        return $this->db->sql_fetchrow($this->result);
203    }
204
205    /**
206     * Returns the ids of the forums readable by the current user.
207     *
208     * @return int[]
209     */
210    protected function get_readable_forums()
211    {
212        static $forum_ids;
213
214        if (!isset($forum_ids))
215        {
216            $forum_ids = array_keys($this->auth->acl_getf('f_read', true));
217        }
218
219        return $forum_ids;
220    }
221
222    /**
223     * Returns the ids of the forum for which the current user can approve the post in the moderation queue.
224     *
225     * @return int[]
226     */
227    protected function get_moderator_approve_forums()
228    {
229        static $forum_ids;
230
231        if (!isset($forum_ids))
232        {
233            $forum_ids = array_keys($this->auth->acl_getf('m_approve', true));
234        }
235
236        return $forum_ids;
237    }
238
239    /**
240     * Returns true if the current user can approve the post of the given forum
241     *
242     * @param int $forum_id Forum id to check
243     * @return bool
244     */
245    protected function is_moderator_approve_forum($forum_id)
246    {
247        static $forum_ids;
248
249        if (!isset($forum_ids))
250        {
251            $forum_ids = array_flip($this->get_moderator_approve_forums());
252        }
253
254        return (isset($forum_ids[$forum_id])) ? true : false;
255    }
256
257    /**
258     * Returns the ids of the forum excluded from the feeds
259     *
260     * @return int[]
261     */
262    protected function get_excluded_forums()
263    {
264        static $forum_ids;
265
266        // Matches acp/acp_board.php
267        $cache_name    = 'feed_excluded_forum_ids';
268
269        if (!isset($forum_ids) && ($forum_ids = $this->cache->get('_' . $cache_name)) === false)
270        {
271            $sql = 'SELECT forum_id
272                FROM ' . FORUMS_TABLE . '
273                WHERE ' . $this->db->sql_bit_and('forum_options', FORUM_OPTION_FEED_EXCLUDE, '<> 0');
274            $result = $this->db->sql_query($sql);
275
276            $forum_ids = array();
277            while ($forum_id = (int) $this->db->sql_fetchfield('forum_id'))
278            {
279                $forum_ids[$forum_id] = $forum_id;
280            }
281            $this->db->sql_freeresult($result);
282
283            $this->cache->put('_' . $cache_name, $forum_ids);
284        }
285
286        return $forum_ids;
287    }
288
289    /**
290     * Returns true if the given id is in the excluded forums list.
291     *
292     * @param int $forum_id Id to check
293     * @return bool
294     */
295    protected function is_excluded_forum($forum_id)
296    {
297        $forum_ids = $this->get_excluded_forums();
298
299        return isset($forum_ids[$forum_id]) ? true : false;
300    }
301
302    /**
303     * Returns all password protected forum ids the current user is currently NOT authenticated for.
304     *
305     * @return array     Array of forum ids
306     */
307    protected function get_passworded_forums()
308    {
309        return $this->user->get_passworded_forums();
310    }
311
312    /**
313     * Returns the link to the user profile.
314     *
315     * @return string
316     */
317    protected function user_viewprofile($row)
318    {
319        $author_id = (int) $row[$this->get('author_id')];
320
321        if ($author_id == ANONYMOUS)
322        {
323            // Since we cannot link to a profile, we just return GUEST
324            // instead of $row['username']
325            return $this->user->lang['GUEST'];
326        }
327
328        return '<a href="' . $this->helper->append_sid('memberlist.' . $this->phpEx, 'mode=viewprofile&amp;u=' . $author_id) . '">' . $row[$this->get('creator')] . '</a>';
329    }
330
331    /**
332     * Sets the SQL query used to retrieve the posts of the feed and returns success state
333     *
334     * @return bool True of SQL query was set, false if not
335     */
336    protected abstract function get_sql();
337}