Completed
Push — master ( d86878...3d7558 )
by Daniel
11:14
created

data::_user_can_download_attachments()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 4

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 9.2
cc 4
eloc 2
nc 6
nop 1
crap 4
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2013 Daniel A. (blitze)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace blitze\sitemaker\services\forum;
11
12
use blitze\sitemaker\services\forum\query_builder;
13
14
class data extends query_builder
15
{
16
	/** @var \phpbb\auth\auth */
17
	protected $auth;
18
19
	/** @var \phpbb\config\config */
20
	protected $config;
21
22
	/** @var \phpbb\content_visibility */
23
	protected $content_visibility;
24
25
	/** @var \phpbb\db\driver\driver_interface */
26
	protected $db;
27
28
	/** @var \phpbb\user */
29
	protected $user;
30
31
	/**
32
	 * Constructor
33
	 *
34
	 * @param \phpbb\auth\auth					$auth					Auth object
35
	 * @param \phpbb\config\config				$config					Config object
36
	 * @param \phpbb\content_visibility			$content_visibility		Content visibility
37
	 * @param \phpbb\db\driver\driver_interface	$db     				Database connection
38
	 * @param \phpbb\user						$user					User object
39
	 * @param integer							$cache_time				Cache results for 3 hours by default
40
	 */
41 24
	public function __construct(\phpbb\auth\auth $auth, \phpbb\config\config $config, \phpbb\content_visibility $content_visibility, \phpbb\db\driver\driver_interface $db, \phpbb\user $user, $cache_time = 10800)
42
	{
43 24
		parent::__construct($auth, $config, $content_visibility, $db, $user, $cache_time);
44
45 24
		$this->auth = $auth;
46 24
		$this->config = $config;
47 24
		$this->content_visibility = $content_visibility;
48 24
		$this->db = $db;
49 24
		$this->user = $user;
50 24
	}
51
52
	/**
53
	 * Get topic data
54
	 *
55
	 * @param mixed|false $limit
56
	 * @param int $start
57
	 * @return array
58
	 */
59 11
	public function get_topic_data($limit = false, $start = 0)
60
	{
61 11
		$sql = $this->db->sql_build_query('SELECT', $this->store['sql_array']);
62 11
		$result = $this->db->sql_query_limit($sql, $limit, $start, $this->cache_time);
63
64 11
		while ($row = $this->db->sql_fetchrow($result))
65
		{
66 9
			$this->store['topic'][$row['topic_id']] = $row;
67
68 9
			$this->store['tracking'][$row['forum_id']]['topic_list'][] = $row['topic_id'];
69 9
			$this->store['tracking'][$row['forum_id']]['mark_time'] =& $row['forum_mark_time'];
70 9
			$this->store['post_ids']['first'][] = $row['topic_first_post_id'];
71 9
			$this->store['post_ids']['last'][] = $row['topic_last_post_id'];
72 9
		}
73 11
		$this->db->sql_freeresult($result);
74
75 11
		return $this->store['topic'];
76
	}
77
78
	/**
79
	 * Get post data
80
	 *
81
	 * @param mixed|false $topic_first_or_last_post (first|last)
82
	 * @param array $post_ids
83
	 * @param bool|false $limit
84
	 * @param int $start
85
	 * @param array $sql_array
86
	 * @return array
87
	 */
88 9
	public function get_post_data($topic_first_or_last_post = false, $post_ids = array(), $limit = false, $start = 0, $sql_array = array())
89
	{
90 9
		$sql = $this->db->sql_build_query('SELECT_DISTINCT', $this->_get_posts_sql_array($topic_first_or_last_post, $post_ids, $sql_array));
91 9
		$result = $this->db->sql_query_limit($sql, $limit, $start, $this->cache_time);
92
93 9
		$post_data = array();
94 9
		while ($row = $this->db->sql_fetchrow($result))
95
		{
96 8
			$parse_flags = ($row['bbcode_bitfield'] ? OPTION_FLAG_BBCODE : 0) | OPTION_FLAG_SMILIES;
97 8
			$row['post_text'] = generate_text_for_display($row['post_text'], $row['bbcode_uid'], $row['bbcode_bitfield'], $parse_flags, true);
98
99 8
			$post_data[$row['topic_id']][$row['post_id']] = $row;
100 8
			$this->store['poster_ids'][] = $row['poster_id'];
101 8
			$this->store['poster_ids'][] = $row['post_edit_user'];
102 8
			$this->store['poster_ids'][] = $row['post_delete_user'];
103 8
			$this->store['attachments'][] = $row['post_id'];
104 8
		}
105 9
		$this->db->sql_freeresult($result);
106
107 9
		return $post_data;
108
	}
109
110
	/**
111
	 * Get attachments...
112
	 *
113
	 * @param int $forum_id
114
	 * @param bool $exclude_in_message
115
	 * @param string $order_by
116
	 * @return array
117
	 */
118 6
	public function get_attachments($forum_id = 0, $allowed_extensions, $exclude_in_message = true, $order_by = 'filetime DESC, post_msg_id ASC')
119
	{
120 6
		$this->store['attachments'] = array_filter($this->store['attachments']);
121
122 6
		$attachments = array();
123 6
		if ($this->_attachments_allowed($forum_id))
124 6
		{
125 5
			$sql = $this->_get_attachment_sql($allowed_extensions, $exclude_in_message, $order_by);
126 5
			$result = $this->db->sql_query($sql, $this->cache_time);
127
128 5
			while ($row = $this->db->sql_fetchrow($result))
129
			{
130 5
				$attachments[$row['post_msg_id']][] = $row;
131 5
			}
132 5
			$this->db->sql_freeresult($result);
133 5
		}
134 6
		$this->store['attachments'] = array();
135
136 6
		return $attachments;
137
	}
138
139
	/**
140
	 * Get topic tracking info
141
	 *
142
	 * @param int $forum_id
143
	 * @return array
144
	 */
145 4
	public function get_topic_tracking_info($forum_id = 0)
146
	{
147 4
		if (!sizeof($this->store['tracking']))
148 4
		{
149
			return array();
150
		}
151
152 4
		$tracking_info = $this->_get_tracking_info();
153
154 4
		return ($forum_id) ? (isset($tracking_info[$forum_id]) ? $tracking_info[$forum_id] : array()) : $tracking_info;
155
	}
156
157
	/**
158
	 * Returns an array of topic first post or last post ids
159
	 *
160
	 * @return array
161
	 */
162
	public function get_posters_info()
163
	{
164
		$this->store['poster_ids'] = array_filter(array_unique($this->store['poster_ids']));
165
166
		$sql = 'SELECT *
167
			FROM ' . USERS_TABLE . '
168
			WHERE ' . $this->db->sql_in_set('user_id', $this->store['poster_ids']);
169
		$result = $this->db->sql_query($sql);
170
171
		$poster = array();
172
		while ($row = $this->db->sql_fetchrow($result))
173
		{
174
			$poster[$row['user_id']] = $row;
175
		}
176
		$this->db->sql_freeresult($result);
177
178
		return $poster;
179
	}
180
181
	/**
182
	 * Returns an array of topic first post or last post ids
183
	 *
184
	 * @param string $first_or_last_post
185
	 * @return array
186
	 */
187 3
	public function get_topic_post_ids($first_or_last_post = 'first')
188
	{
189 3
		return (isset($this->store['post_ids'][$first_or_last_post])) ? $this->store['post_ids'][$first_or_last_post] : array();
190
	}
191
192
	/**
193
	 * @param mixed $topic_first_or_last_post
194
	 * @param array $post_ids
195
	 * @param array $sql_array
196
	 * @return array
197
	 */
198 9
	private function _get_posts_sql_array($topic_first_or_last_post, array $post_ids, array $sql_array)
199
	{
200 9
		$sql_array = array_merge_recursive(
201
			array(
202 9
				'SELECT'	=> array('p.*'),
203 9
				'FROM'		=> array(POSTS_TABLE => 'p'),
204 9
				'WHERE'		=> $this->_get_post_data_where($post_ids, $topic_first_or_last_post),
205 9
				'ORDER_BY'	=> 'p.topic_id, p.post_time ASC',
206 9
			),
207
			$sql_array
208 9
		);
209
210 9
		$sql_array['SELECT'] = join(', ', array_filter($sql_array['SELECT']));
211 9
		$sql_array['WHERE'] = join(' AND ', array_filter($sql_array['WHERE']));
212
213 9
		return $sql_array;
214
	}
215
216
	/**
217
	 * @param array $post_ids
218
	 * @param string $topic_first_or_last_post
219
	 * @return array
220
	 */
221 9
	private function _get_post_data_where(array $post_ids, $topic_first_or_last_post)
222
	{
223 9
		$sql_where = array();
224
225 9
		if (sizeof($post_ids))
226 9
		{
227 2
			$sql_where[] = $this->db->sql_in_set('p.post_id', $post_ids);
228 2
		}
229 7
		else if (sizeof($this->store['topic']))
230 7
		{
231 3
			$sql_where[] = $this->db->sql_in_set('p.topic_id', array_keys($this->store['topic']));
232
233
			if ($topic_first_or_last_post)
234 3
			{
235 3
				$sql_where[] = $this->db->sql_in_set('p.post_id', $this->get_topic_post_ids($topic_first_or_last_post));
236 3
			}
237 3
		}
238
239 9
		$sql_where[] = $this->content_visibility->get_global_visibility_sql('post', $this->ex_fid_ary, 'p.');
240
241 9
		return $sql_where;
242
	}
243
244
	/**
245
	 * @return array
246
	 */
247 4
	private function _get_tracking_info()
248
	{
249 4
		$info = array();
250 4
		if ($this->_can_track_by_lastread())
251 4
		{
252 4
			$info = $this->_build_tracking_info('get_topic_tracking');
253 4
		}
254
		else if ($this->_can_track_anonymous())
255
		{
256
			$info = $this->_build_tracking_info('get_complete_topic_tracking');
257
		}
258
259 4
		return $info;
260
	}
261
262
	/**
263
	 * @param string $function
264
	 * @return array
265
	 */
266 4
	private function _build_tracking_info($function)
267
	{
268 4
		$tracking_info = array();
269 4
		foreach ($this->store['tracking'] as $fid => $forum)
270
		{
271 4
			$tracking_info[$fid] = call_user_func_array($function, array($fid, $forum['topic_list'], &$this->store['topic'], array($fid => $forum['mark_time'])));
272 4
		}
273
274 4
		return $tracking_info;
275
	}
276
277
	/**
278
	 * @return bool
279
	 */
280 4
	private function _can_track_by_lastread()
281
	{
282 4
		return ($this->config['load_db_lastread'] && $this->user->data['is_registered']) ? true : false;
283
	}
284
285
	/**
286
	 * @return bool
287
	 */
288
	private function _can_track_anonymous()
289
	{
290
		return ($this->config['load_anon_lastread'] || $this->user->data['is_registered']) ? true : false;
291
	}
292
293
	/**
294
	 * @param int $forum_id
295
	 * @return bool
296
	 */
297 6
	private function _attachments_allowed($forum_id)
298
	{
299 6
		return ($this->store['attachments'] && $this->_user_can_download_attachments($forum_id)) ? true : false;
300
	}
301
302
	/**
303
	 * @param int $forum_id
304
	 * @return bool
305
	 */
306 5
	private function _user_can_download_attachments($forum_id)
307
	{
308 5
		return ($this->auth->acl_get('u_download') && (!$forum_id || $this->auth->acl_get('f_download', $forum_id))) ? true : false;
309
	}
310
311
	/**
312
	 * @param array $allowed_extensions
313
	 * @param bool $exclude_in_message
314
	 * @param string $order_by
315
	 * @return string
316
	 */
317 5
	private function _get_attachment_sql($allowed_extensions, $exclude_in_message, $order_by)
318
	{
319
		return 'SELECT *
320 5
			FROM ' . ATTACHMENTS_TABLE . '
321 5
			WHERE ' . $this->db->sql_in_set('post_msg_id', $this->store['attachments']) .
322 5
				(($exclude_in_message) ? ' AND in_message = 0' : '') .
323 5
				(sizeof($allowed_extensions) ? ' AND ' . $this->db->sql_in_set('extension', $allowed_extensions) : '') . '
324 5
			ORDER BY ' . $order_by;
325
	}
326
}
327