Completed
Push — develop ( 524863...c767cb )
by Daniel
10:30
created

contacts::can_receive_pm()   B

Complexity

Conditions 8
Paths 16

Size

Total Lines 19
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
ccs 7
cts 7
cp 1
rs 7.7777
cc 8
eloc 7
nc 16
nop 3
crap 8
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\users;
11
12
abstract class contacts
13
{
14
	/** @var \phpbb\auth\auth */
15
	protected $auth;
16
17
	/** @var \phpbb\config\config */
18
	protected $config;
19
20
	/** @var \phpbb\language\language */
21
	protected $translator;
22
23
	/** @var \phpbb\user */
24
	protected $user;
25
26
	/** @var string */
27
	protected $phpbb_root_path;
28
29
	/** @var string */
30
	protected $php_ext;
31
32
	/** @var bool */
33
	protected $mailto_allowed;
34
35
	/** @var bool */
36
	protected $email_form_allowed;
37
38
	/** @var bool */
39
	protected $jabber_allowed;
40
41
	/**
42
	 * Constructor
43
	 *
44
	 * @param \phpbb\auth\auth					$auth					Auth object
45
	 * @param \phpbb\config\config				$config					Config object
46
	 * @param \phpbb\language\language			$translator				Language object
47
	 * @param \phpbb\user						$user					User Object
48
	 * @param string							$phpbb_root_path		Path to the phpbb includes directory.
49
	 * @param string							$php_ext				php file extension
50
	 */
51 89
	public function __construct(\phpbb\auth\auth $auth, \phpbb\config\config $config, \phpbb\language\language $translator, \phpbb\user $user, $phpbb_root_path, $php_ext)
52
	{
53 89
		$this->auth = $auth;
54 89
		$this->config = $config;
55 89
		$this->translator = $translator;
56 89
		$this->user = $user;
57 89
		$this->phpbb_root_path = $phpbb_root_path;
58 89
		$this->php_ext = $php_ext;
59
60 89
		$this->mailto_allowed = $this->get_mailto_allowed();
61 89
		$this->email_form_allowed = $this->get_email_form_allowed();
62 89
		$this->jabber_allowed = $this->get_jabber_allowed();
63 89
	}
64
65
	/**
66
	 * @param array $row
67
	 * @return array
68
	 */
69 8
	protected function get_email_contact(array $row)
70
	{
71 8
		$email = array();
72 8
		if ($this->user_email_allowed($row))
73 8
		{
74
			$email = array(
75 8
				'ID'		=> 'email',
76 8
				'NAME'		=> $this->translator->lang('SEND_EMAIL'),
77 8
				'U_CONTACT'	=> $this->get_email_url($row),
78 8
			);
79 8
		}
80
81 8
		return $email;
82
	}
83
84
	/**
85
	 * @param array $row
86
	 * @return bool
87
	 */
88 8
	protected function user_email_allowed(array $row)
89
	{
90 8
		return ((!empty($row['user_allow_viewemail']) && $this->auth->acl_get('u_sendemail')) || $this->auth->acl_get('a_email')) ? true : false;
91
	}
92
93
	/**
94
	 * @param array $row
95
	 * @return string
96
	 */
97 8
	protected function get_email_url(array $row)
98
	{
99 8
		return ($this->email_form_allowed) ? append_sid("{$this->phpbb_root_path}memberlist.{$this->php_ext}", 'mode=email&amp;u=' . $row['user_id']) : (($this->mailto_allowed) ? 'mailto:' . $row['user_email'] : '');
100
	}
101
102
	/**
103
	 * @return bool
104
	 */
105 89
	protected function get_email_form_allowed()
106
	{
107 89
		return ($this->config['board_email_form'] && $this->config['email_enable']) ? true : false;
108
	}
109
110
	/**
111
	 * @return bool
112
	 */
113 89
	protected function get_mailto_allowed()
114
	{
115 89
		return ($this->config['board_hide_emails'] && !$this->auth->acl_get('a_email')) ? false : true;
116
	}
117
118
	/**
119
	 * @param array $row
120
	 * @return array
121
	 */
122 8
	protected function get_jabber_contact(array $row)
123
	{
124 8
		$jabber = array();
125 8
		if ($this->jabber_allowed && $row['user_jabber'])
126 8
		{
127
			$jabber = array(
128 4
				'ID'		=> 'jabber',
129 4
				'NAME' 		=> $this->translator->lang('JABBER'),
130 4
				'U_CONTACT'	=> append_sid("{$this->phpbb_root_path}memberlist.{$this->php_ext}", 'mode=contact&amp;action=jabber&amp;u=' . $row['user_id']),
131 4
			);
132 4
		}
133
134 8
		return $jabber;
135
	}
136
137
	/**
138
	 * @return bool
139
	 */
140 89
	protected function get_jabber_allowed()
141
	{
142 89
		return ($this->config['jab_enable'] && $this->auth->acl_get('u_sendim')) ? true : false;
143
	}
144
145
	/**
146
	 * @param array $row
147
	 * @param array $can_receive_pm_list
148
	 * @param array $permanently_banned_users
149
	 * @return array
150
	 */
151 8
	protected function get_pm_contact(array $row, array $can_receive_pm_list, array $permanently_banned_users)
152
	{
153 8
		$pm = array();
154 8
		if ($this->user_can_pm() && $this->can_receive_pm($row, $can_receive_pm_list, $permanently_banned_users))
155 8
		{
156
			$pm = array(
157 4
				'ID'		=> 'pm',
158 4
				'NAME' 		=> $this->translator->lang('SEND_PRIVATE_MESSAGE'),
159 4
				'U_CONTACT'	=> append_sid("{$this->phpbb_root_path}ucp.{$this->php_ext}", 'i=pm&amp;mode=compose&amp;u=' . $row['user_id']),
160 4
			);
161 4
		}
162
163 8
		return $pm;
164
	}
165
166
	/**
167
	 * @return bool
168
	 */
169 8
	protected function user_can_pm()
170
	{
171 8
		return ($this->config['allow_privmsg'] && $this->auth->acl_get('u_sendpm')) ? true : false;
172
	}
173
174
	/**
175
	 * @param array $row
176
	 * @param array $can_receive_pm_list
177
	 * @param array $permanently_banned_users
178
	 * @return bool
179
	 */
180 8
	protected function can_receive_pm(array $row, array $can_receive_pm_list, array $permanently_banned_users)
181
	{
182
		return (
183
			// They must be a "normal" user
184 8
			$row['user_type'] != USER_IGNORE &&
185
186
			// They must not be deactivated by the administrator
187 8
			($row['user_type'] != USER_INACTIVE || $row['user_inactive_reason'] != INACTIVE_MANUAL) &&
188
189
			// They must be able to read PMs
190 8
			in_array($row['user_id'], $can_receive_pm_list) &&
191
192
			// They must not be permanently banned
193 8
			!in_array($row['user_id'], $permanently_banned_users) &&
194
195
			// They must allow users to contact via PM
196 4
			(($this->auth->acl_gets('a_', 'm_') || $this->auth->acl_getf_global('m_')) || $row['user_allow_pm'])
197 8
		);
198
	}
199
200
	/**
201
	 * Get the list of users who can receive private messages
202
	 *
203
	 * @param array $user_ids
204
	 * @return array
205
	 */
206 8
	protected function get_can_receive_pm_list(array $user_ids)
207
	{
208 8
		$can_receive_pm_list = $this->auth->acl_get_list($user_ids, 'u_readpm');
209 8
		return (empty($can_receive_pm_list) || !isset($can_receive_pm_list[0]['u_readpm'])) ? array() : $can_receive_pm_list[0]['u_readpm'];
210
	}
211
212
	/**
213
	 * Get the list of permanently banned users
214
	 *
215
	 * @param array $user_ids
216
	 * @return array
217
	 */
218 8
	protected function get_banned_users_list(array $user_ids)
219
	{
220 8
		if (!function_exists('phpbb_get_banned_user_ids'))
221 8
		{
222 1
			include($this->phpbb_root_path . 'includes/functions_user.' . $this->php_ext);
223 1
		}
224
225 8
		return phpbb_get_banned_user_ids($user_ids, false);
226
	}
227
}
228