Completed
Push — master ( 95843a...6bd958 )
by Matt
10s
created

main_listener::set_permissions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 *
4
 * Advertisement management. An extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2017 phpBB Limited <https://www.phpbb.com>
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace phpbb\ads\event;
12
13
/**
14
 * @ignore
15
 */
16
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17
18
/**
19
 * Advertisement management Event listener.
20
 */
21
class main_listener implements EventSubscriberInterface
22
{
23
	/** @var \phpbb\template\template */
24
	protected $template;
25
26
	/** @var \phpbb\user */
27
	protected $user;
28
29
	/** @var \phpbb\config\db_text */
30
	protected $config_text;
31
32
	/** @var \phpbb\config\config */
33
	protected $config;
34
35
	/** @var \phpbb\ads\ad\manager */
36
	protected $manager;
37
38
	/** @var \phpbb\ads\location\manager */
39
	protected $location_manager;
40
41
	/** @var \phpbb\controller\helper */
42
	protected $controller_helper;
43
44
	/** @var bool Can the current user view ads? */
45
	protected $can_view_ads;
46
47
	/**
48
	 * {@inheritdoc}
49
	 */
50 1
	public static function getSubscribedEvents()
51
	{
52
		return array(
53 1
			'core.permissions'				=> 'set_permissions',
54 1
			'core.user_setup'				=> 'load_language_on_setup',
55 1
			'core.page_header_after'		=> array(array('setup_ads'), array('adblocker'), array('clicks')),
56 1
			'core.delete_user_after'		=> 'remove_ad_owner',
57 1
			'core.adm_page_header_after'	=> 'disable_xss_protection',
58 1
		);
59
	}
60
61
	/**
62
	 * Constructor
63
	 *
64
	 * @param \phpbb\template\template				$template			Template object
65
	 * @param \phpbb\user							$user				User object
66
	 * @param \phpbb\config\db_text					$config_text		Config text object
67
	 * @param \phpbb\config\config					$config				Config object
68
	 * @param \phpbb\ads\ad\manager					$manager			Advertisement manager object
69
	 * @param \phpbb\ads\location\manager			$location_manager	Template location manager object
70
	 * @param \phpbb\controller\helper				$controller_helper	Controller helper object
71
	 */
72 24 View Code Duplication
	public function __construct(\phpbb\template\template $template, \phpbb\user $user, \phpbb\config\db_text $config_text, \phpbb\config\config $config, \phpbb\ads\ad\manager $manager, \phpbb\ads\location\manager $location_manager, \phpbb\controller\helper $controller_helper)
73
	{
74 24
		$this->template = $template;
75 24
		$this->user = $user;
76 24
		$this->config_text = $config_text;
77 24
		$this->config = $config;
78 24
		$this->manager = $manager;
79 24
		$this->location_manager = $location_manager;
80 24
		$this->controller_helper = $controller_helper;
81 24
	}
82
83
	/**
84
	 * Wire up u_phpbb_ads permission
85
	 *
86
	 * @param	\phpbb\event\data	$event	The event object
87
	 * @return	void
88
	 */
89 1
	public function set_permissions($event)
90
	{
91 1
		$event->update_subarray('permissions', 'u_phpbb_ads', ['lang' => 'ACL_U_PHPBB_ADS', 'cat' => 'misc']);
92 1
	}
93
94
	/**
95
	 * Load common language file during user setup
96
	 *
97
	 * @param	\phpbb\event\data	$event	The event object
98
	 * @return	void
99
	 */
100 1
	public function load_language_on_setup($event)
101
	{
102 1
		$lang_set_ext = $event['lang_set_ext'];
103 1
		$lang_set_ext[] = array(
104 1
			'ext_name' => 'phpbb/ads',
105 1
			'lang_set' => 'common',
106
		);
107 1
		$event['lang_set_ext'] = $lang_set_ext;
108 1
	}
109
110
	/**
111
	 * Displays advertisements
112
	 *
113
	 * @return	void
114
	 */
115 6
	public function setup_ads()
116
	{
117 6
		if ($this->can_view_ads())
118 6
		{
119 3
			$location_ids = $this->location_manager->get_all_location_ids();
120 3
			$ad_ids = array();
121
122 3
			foreach ($this->manager->get_ads($location_ids) as $row)
123
			{
124 3
				$ad_ids[] = $row['ad_id'];
125
126 3
				$this->template->assign_vars(array(
127 3
					'AD_' . strtoupper($row['location_id']) . '_ID'	=> $row['ad_id'],
128 3
					'AD_' . strtoupper($row['location_id'])			=> htmlspecialchars_decode($row['ad_code']),
129 3
				));
130 3
			}
131
132 3
			$this->views($ad_ids);
133 3
		}
134 6
	}
135
136
	/**
137
	 * Display Ad blocker friendly message if allowed
138
	 *
139
	 * @return	void
140
	 */
141 6
	public function adblocker()
142
	{
143 6
		$this->template->assign_var(
144 6
			'S_DISPLAY_ADBLOCKER',
145 6
			($this->config['phpbb_ads_adblocker_message'] && $this->can_view_ads())
146 6
		);
147 6
	}
148
149
	/**
150
	 * Add click tracking template variables
151
	 *
152
	 * @return	void
153
	 */
154 2
	public function clicks()
155
	{
156 2
		if ($this->config['phpbb_ads_enable_clicks'])
157 2
		{
158 1
			$this->template->assign_vars(array(
159 1
				'U_PHPBB_ADS_CLICK'		=> $this->controller_helper->route('phpbb_ads_click', array('data' => 0)),
160 1
				'S_PHPBB_ADS_ENABLE_CLICKS'	=> true,
161 1
			));
162 1
		}
163 2
	}
164
165
	/**
166
	 * Prepare views counter template
167
	 *
168
	 * @param	array	$ad_ids	List of ads that will be displayed on current request's page
169
	 * @return	void
170
	 */
171 3
	protected function views($ad_ids)
172
	{
173 3
		if ($this->config['phpbb_ads_enable_views'] && !$this->user->data['is_bot'] && count($ad_ids))
174 3
		{
175 1
			$this->template->assign_vars(array(
176 1
				'S_INCREMENT_VIEWS'		=> true,
177 1
				'U_PHPBB_ADS_VIEWS'	=> $this->controller_helper->route('phpbb_ads_view', array('data' => implode('-', $ad_ids))),
178 1
			));
179 1
		}
180 3
	}
181
182
	/**
183
	 * Disable XSS Protection
184
	 * In Chrome browsers, previewing an Ad Code with javascript can
185
	 * be blocked, due to a false positive where Chrome thinks the
186
	 * javascript is an XSS injection. This will temporarily disable
187
	 * XSS protection in chrome while managing ads in the ACP.
188
	 *
189
	 * @param	\phpbb\event\data	$event	The event object
190
	 */
191 6
	public function disable_xss_protection($event)
192
	{
193 6
		if (stripos($this->user->browser, 'chrome') !== false &&
194 4
			stripos($this->user->page['page'], 'phpbb-ads') !== false)
195 6
		{
196 2
			$event['http_headers'] = array_merge($event['http_headers'], ['X-XSS-Protection' => '0']);
197 2
		}
198 6
	}
199
200
	/**
201
	 * Remove ad owner when deleting user(s)
202
	 *
203
	 * @param	\phpbb\event\data	$event	The event object
204
	 * @return	void
205
	 */
206 1
	public function remove_ad_owner($event)
207
	{
208 1
		$this->manager->remove_ad_owner($event['user_ids']);
209 1
	}
210
211
	/**
212
	 * User can view ads only if they are not in a group that has ads hidden
213
	 *
214
	 * @return	bool	true if the user is not in a group with ads hidden, false if they are
215
	 */
216 9
	protected function can_view_ads()
217
	{
218 9
		if ($this->can_view_ads === null)
219 9
		{
220 9
			$user_groups = $this->manager->load_memberships($this->user->data['user_id']);
221 9
			$hide_groups = json_decode($this->config_text->get('phpbb_ads_hide_groups'), true);
222
223 9
			$this->can_view_ads = !array_intersect($user_groups, $hide_groups);
224 9
		}
225
226 9
		return $this->can_view_ads;
227
	}
228
}
229