aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/feed/base.php
blob: d4be0dc5929b06d95c05494cbe6c660ce34db503 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
<?php
/**
 *
 * This file is part of the phpBB Forum Software package.
 *
 * @copyright (c) phpBB Limited <https://www.phpbb.com>
 * @license GNU General Public License, version 2 (GPL-2.0)
 *
 * For full copyright and license information, please see
 * the docs/CREDITS.txt file.
 *
 */

namespace phpbb\feed;

/**
 * Base class with some generic functions and settings.
 */
abstract class base implements feed_interface
{
	/**
	 * Feed helper object
	 * @var \phpbb\feed\helper
	 */
	protected $helper;

	/** @var \phpbb\config\config */
	protected $config;

	/** @var \phpbb\db\driver\driver_interface */
	protected $db;

	/** @var \phpbb\cache\driver\driver_interface */
	protected $cache;

	/** @var \phpbb\user */
	protected $user;

	/** @var \phpbb\auth\auth */
	protected $auth;

	/** @var \phpbb\content_visibility */
	protected $content_visibility;

	/** @var \phpbb\event\dispatcher_interface */
	protected $phpbb_dispatcher;

	/** @var string */
	protected $phpEx;

	/**
	 * SQL Query to be executed to get feed items
	 */
	protected $sql = array();

	/**
	 * Keys specified for retrieval of title, content, etc.
	 */
	protected $keys = array();

	/**
	 * Number of items to fetch. Usually overwritten by $config['feed_something']
	 */
	protected $num_items = 15;

	/**
	 * Separator for title elements to separate items (for example forum / topic)
	 */
	protected $separator = "\xE2\x80\xA2"; // &bull;

	/**
	 * Separator for the statistics row (Posted by, post date, replies, etc.)
	 */
	protected $separator_stats = "\xE2\x80\x94"; // &mdash;

	/** @var mixed Query result handle */
	protected $result;

	/**
	 * Constructor
	 *
	 * @param \phpbb\feed\helper					$helper		Feed helper
	 * @param \phpbb\config\config				$config		Config object
	 * @param \phpbb\db\driver\driver_interface	$db			Database connection
	 * @param \phpbb\cache\driver\driver_interface	$cache	Cache object
	 * @param \phpbb\user						$user		User object
	 * @param \phpbb\auth\auth					$auth		Auth object
	 * @param \phpbb\content_visibility			$content_visibility		Auth object
	 * @param \phpbb\event\dispatcher_interface	$phpbb_dispatcher		Event dispatcher object
	 * @param string								$phpEx		php file extension
	 */
	public function __construct(
		\phpbb\feed\helper $helper,
		\phpbb\config\config $config,
		\phpbb\db\driver\driver_interface $db,
		\phpbb\cache\driver\driver_interface $cache,
		\phpbb\user $user,
		\phpbb\auth\auth $auth,
		\phpbb\content_visibility $content_visibility,
		\phpbb\event\dispatcher_interface $phpbb_dispatcher,
		$phpEx
	)
	{
		$this->config = $config;
		$this->helper = $helper;
		$this->db = $db;
		$this->cache = $cache;
		$this->user = $user;
		$this->auth = $auth;
		$this->content_visibility = $content_visibility;
		$this->phpbb_dispatcher = $phpbb_dispatcher;
		$this->phpEx = $phpEx;

		$this->set_keys();

		// Allow num_items to be string
		if (is_string($this->num_items))
		{
			$this->num_items = (int) $this->config[$this->num_items];

			// A precaution
			if (!$this->num_items)
			{
				$this->num_items = 10;
			}
		}
	}

	/**
	 * {@inheritdoc}
	 */
	public function set_keys()
	{
	}

	/**
	 * {@inheritdoc}
	 */
	public function open()
	{
	}

	/**
	 * {@inheritdoc}
	 */
	public function close()
	{
		if (!empty($this->result))
		{
			$this->db->sql_freeresult($this->result);
		}
	}

	/**
	 * {@inheritdoc}
	 */
	public function set($key, $value)
	{
		$this->keys[$key] = $value;
	}

	/**
	 * {@inheritdoc}
	 */
	public function get($key)
	{
		return (isset($this->keys[$key])) ? $this->keys[$key] : null;
	}

	/**
	 * {@inheritdoc}
	 */
	public function get_item()
	{
		if (!isset($this->result))
		{
			if (!$this->get_sql())
			{
				return false;
			}

			$sql_ary = $this->sql;

			/**
			 * Event to modify the feed item sql
			 *
			 * @event core.feed_base_modify_item_sql
			 * @var	array	sql_ary		The SQL array to get the feed item data
			 *
			 * @since 3.1.10-RC1
			 */
			$vars = array('sql_ary');
			extract($this->phpbb_dispatcher->trigger_event('core.feed_base_modify_item_sql', compact($vars)));
			$this->sql = $sql_ary;
			unset($sql_ary);

			// Query database
			$sql = $this->db->sql_build_query('SELECT', $this->sql);
			$this->result = $this->db->sql_query_limit($sql, $this->num_items);
		}

		return $this->db->sql_fetchrow($this->result);
	}

	/**
	 * Returns the ids of the forums readable by the current user.
	 *
	 * @return int[]
	 */
	protected function get_readable_forums()
	{
		static $forum_ids;

		if (!isset($forum_ids))
		{
			$forum_ids = array_keys($this->auth->acl_getf('f_read', true));
		}

		return $forum_ids;
	}

	/**
	 * Returns the ids of the forum for which the current user can approve the post in the moderation queue.
	 *
	 * @return int[]
	 */
	protected function get_moderator_approve_forums()
	{
		static $forum_ids;

		if (!isset($forum_ids))
		{
			$forum_ids = array_keys($this->auth->acl_getf('m_approve', true));
		}

		return $forum_ids;
	}

	/**
	 * Returns true if the current user can approve the post of the given forum
	 *
	 * @param int $forum_id Forum id to check
	 * @return bool
	 */
	protected function is_moderator_approve_forum($forum_id)
	{
		static $forum_ids;

		if (!isset($forum_ids))
		{
			$forum_ids = array_flip($this->get_moderator_approve_forums());
		}

		return (isset($forum_ids[$forum_id])) ? true : false;
	}

	/**
	 * Returns the ids of the forum excluded from the feeds
	 *
	 * @return int[]
	 */
	protected function get_excluded_forums()
	{
		static $forum_ids;

		// Matches acp/acp_board.php
		$cache_name	= 'feed_excluded_forum_ids';

		if (!isset($forum_ids) && ($forum_ids = $this->cache->get('_' . $cache_name)) === false)
		{
			$sql = 'SELECT forum_id
				FROM ' . FORUMS_TABLE . '
				WHERE ' . $this->db->sql_bit_and('forum_options', FORUM_OPTION_FEED_EXCLUDE, '<> 0');
			$result = $this->db->sql_query($sql);

			$forum_ids = array();
			while ($forum_id = (int) $this->db->sql_fetchfield('forum_id'))
			{
				$forum_ids[$forum_id] = $forum_id;
			}
			$this->db->sql_freeresult($result);

			$this->cache->put('_' . $cache_name, $forum_ids);
		}

		return $forum_ids;
	}

	/**
	 * Returns true if the given id is in the excluded forums list.
	 *
	 * @param int $forum_id Id to check
	 * @return bool
	 */
	protected function is_excluded_forum($forum_id)
	{
		$forum_ids = $this->get_excluded_forums();

		return isset($forum_ids[$forum_id]) ? true : false;
	}

	/**
	 * Returns all password protected forum ids the current user is currently NOT authenticated for.
	 *
	 * @return array     Array of forum ids
	 */
	protected function get_passworded_forums()
	{
		return $this->user->get_passworded_forums();
	}

	/**
	 * Returns the link to the user profile.
	 *
	 * @return string
	 */
	protected function user_viewprofile($row)
	{
		$author_id = (int) $row[$this->get('author_id')];

		if ($author_id == ANONYMOUS)
		{
			// Since we cannot link to a profile, we just return GUEST
			// instead of $row['username']
			return $this->user->lang['GUEST'];
		}

		return '<a href="' . $this->helper->append_sid('memberlist.' . $this->phpEx, 'mode=viewprofile&amp;u=' . $author_id) . '">' . $row[$this->get('creator')] . '</a>';
	}

	/**
	 * Returns the SQL query used to retrieve the posts of the feed.
	 *
	 * @return string SQL SELECT query
	 */
	protected abstract function get_sql();
}