Completed
Push — develop ( 660ae6...f77be0 )
by Daniel
08:50
created

attachments::get_attachments()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 12
cts 12
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 2
nop 6
crap 3
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2016 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
class attachments
13
{
14
	/** @var \phpbb\auth\auth */
15
	protected $auth;
16
17
	/** @var \phpbb\db\driver\driver_interface */
18
	protected $db;
19
20
	/**
21
	 * Constructor
22
	 *
23
	 * @param \phpbb\auth\auth						$auth		Auth object
24
	 * @param \phpbb\db\driver\driver_interface		$db     	Database connection
25
	 */
26 7
	public function __construct(\phpbb\auth\auth $auth, \phpbb\db\driver\driver_interface $db)
27
	{
28 7
		$this->auth = $auth;
29 7
		$this->db = $db;
30 7
	}
31
32
	/**
33
	 * Get attachments...
34
	 *
35
	 * @param int $forum_id
36
	 * @param array $attach_ids
37
	 * @param array $allowed_extensions
38
	 * @param int|bool $limit
39
	 * @param bool $exclude_in_message
40
	 * @param string $order_by
41
	 * @return array
42
	 */
43 7
	public function get_attachments($forum_id = 0, array $attach_ids = array(), $allowed_extensions = array(), $limit = false, $exclude_in_message = true, $order_by = 'filetime DESC')
44
	{
45 7
		$attachments = array();
46 7
		if ($this->user_can_download_attachments($forum_id))
47 7
		{
48 7
			$sql = $this->get_attachment_sql($attach_ids, $allowed_extensions, $exclude_in_message, $order_by);
49 7
			$result = $this->db->sql_query_limit($sql, $limit);
50
51 7
			while ($row = $this->db->sql_fetchrow($result))
52
			{
53 7
				$attachments[$row['post_msg_id']][] = $row;
54 7
			}
55 7
			$this->db->sql_freeresult($result);
56 7
		}
57
58 7
		return $attachments;
59
	}
60
61
	/**
62
	 * @param int $forum_id
63
	 * @return bool
64
	 */
65 7
	protected function user_can_download_attachments($forum_id)
66
	{
67 7
		return ($this->auth->acl_get('u_download') && (!$forum_id || $this->auth->acl_get('f_download', $forum_id))) ? true : false;
68
	}
69
70
	/**
71
	 * @param array $attach_ids
72
	 * @param array $allowed_extensions
73
	 * @param bool $exclude_in_message
74
	 * @param string $order_by
75
	 * @return string
76
	 */
77 7
	protected function get_attachment_sql(array $attach_ids, array $allowed_extensions, $exclude_in_message, $order_by)
78
	{
79
		return 'SELECT *
80 7
			FROM ' . ATTACHMENTS_TABLE . '
81 7
			WHERE ' . $this->db->sql_in_set('post_msg_id', array_map('intval', $attach_ids)) .
82 7
				(($exclude_in_message) ? ' AND in_message = 0' : '') .
83 7
				(sizeof($allowed_extensions) ? ' AND ' . $this->db->sql_in_set('extension', $allowed_extensions) : '') . '
84 7
			ORDER BY ' . $order_by;
85
	}
86
}
87