main_listener   A
last analyzed

Complexity

Total Complexity 28

Size/Duplication

Total Lines 277
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 11
Bugs 0 Features 0
Metric Value
eloc 86
dl 0
loc 277
ccs 98
cts 98
cp 1
rs 10
c 11
b 0
f 0
wmc 28

14 Methods

Rating   Name   Duplication   Size   Complexity  
A load_language_on_setup() 0 8 1
A __construct() 0 12 1
A getSubscribedEvents() 0 11 1
A set_permissions() 0 5 1
A remove_ad_owner() 0 3 1
A destroy_user_group_cache() 0 3 1
A disable_xss_protection() 0 6 3
A clicks() 0 7 2
A is_non_content_page() 0 10 1
A views() 0 7 4
A visual_demo() 0 16 3
A setup_ads() 0 20 3
A adblocker() 0 3 1
A append_agreement() 0 13 5
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;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\EventD...ventSubscriberInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
18
/**
19
 * Advertisement management Event listener.
20
 */
21
class main_listener implements EventSubscriberInterface
22
{
23
	/** @var \phpbb\language\language */
0 ignored issues
show
Bug introduced by
The type phpbb\language\language was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
	protected $language;
25
26
	/** @var \phpbb\template\template */
0 ignored issues
show
Bug introduced by
The type phpbb\template\template was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
	protected $template;
28
29
	/** @var \phpbb\user */
0 ignored issues
show
Bug introduced by
The type phpbb\user was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30
	protected $user;
31
32
	/** @var \phpbb\config\config */
0 ignored issues
show
Bug introduced by
The type phpbb\config\config was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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 */
0 ignored issues
show
Bug introduced by
The type phpbb\controller\helper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
42
	protected $controller_helper;
43
44
	/** @var \phpbb\request\request */
0 ignored issues
show
Bug introduced by
The type phpbb\request\request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
45
	protected $request;
46
47
	/** @var \phpbb\cache\driver\driver_interface */
0 ignored issues
show
Bug introduced by
The type phpbb\cache\driver\driver_interface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
48
	protected $cache;
49
50
	/** @var string */
51
	protected $php_ext;
52
53
	/**
54
	 * {@inheritdoc}
55
	 */
56 1
	public static function getSubscribedEvents()
57
	{
58
		return array(
59 1
			'core.permissions'				=> 'set_permissions',
60 1
			'core.user_setup'				=> 'load_language_on_setup',
61 1
			'core.page_footer_after'		=> array(array('setup_ads'), array('visual_demo'), array('append_agreement')),
62 1
			'core.page_header_after'		=> array(array('adblocker'), array('clicks')),
63 1
			'core.delete_user_after'		=> 'remove_ad_owner',
64 1
			'core.adm_page_header_after'	=> 'disable_xss_protection',
65 1
			'core.group_add_user_after'		=> 'destroy_user_group_cache',
66 1
			'core.group_delete_user_after'	=> 'destroy_user_group_cache',
67 1
		);
68
	}
69
70
	/**
71
	 * Constructor
72
	 *
73
	 * @param \phpbb\language\language				$language			Language object
74
	 * @param \phpbb\template\template				$template			Template object
75
	 * @param \phpbb\user							$user				User object
76
	 * @param \phpbb\config\config					$config				Config object
77
	 * @param \phpbb\ads\ad\manager					$manager			Advertisement manager object
78
	 * @param \phpbb\ads\location\manager			$location_manager	Template location manager object
79
	 * @param \phpbb\controller\helper				$controller_helper	Controller helper object
80
	 * @param \phpbb\request\request				$request			Request object
81
	 * @param \phpbb\cache\driver\driver_interface	$cache				Cache driver object
82
	 * @param string								$php_ext			PHP extension
83
	 */
84 20
	public function __construct(\phpbb\language\language $language, \phpbb\template\template $template, \phpbb\user $user, \phpbb\config\config $config, \phpbb\ads\ad\manager $manager, \phpbb\ads\location\manager $location_manager, \phpbb\controller\helper $controller_helper, \phpbb\request\request $request, \phpbb\cache\driver\driver_interface $cache, $php_ext)
85
	{
86 20
		$this->language = $language;
87 20
		$this->template = $template;
88 20
		$this->user = $user;
89 20
		$this->config = $config;
90 20
		$this->manager = $manager;
91 20
		$this->location_manager = $location_manager;
92 20
		$this->controller_helper = $controller_helper;
93 20
		$this->request = $request;
94 20
		$this->cache = $cache;
95 20
		$this->php_ext = $php_ext;
96 20
	}
97
98
	/**
99
	 * Wire up u_phpbb_ads permission
100
	 *
101
	 * @param	\phpbb\event\data	$event	The event object
0 ignored issues
show
Bug introduced by
The type phpbb\event\data was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
102
	 * @return	void
103
	 */
104 1
	public function set_permissions($event)
105
	{
106 1
		$event->update_subarray('permissions', 'u_phpbb_ads', ['lang' => 'ACL_U_PHPBB_ADS', 'cat' => 'misc']);
107 1
		$event->update_subarray('permissions', 'a_phpbb_ads_m', ['lang' => 'ACL_A_PHPBB_ADS_M', 'cat' => 'misc']);
108
		$event->update_subarray('permissions', 'a_phpbb_ads_s', ['lang' => 'ACL_A_PHPBB_ADS_S', 'cat' => 'misc']);
109
	}
110
111
	/**
112
	 * Load common language file during user setup
113
	 *
114
	 * @param	\phpbb\event\data	$event	The event object
115 1
	 * @return	void
116
	 */
117 1
	public function load_language_on_setup($event)
118 1
	{
119 1
		$lang_set_ext = $event['lang_set_ext'];
120 1
		$lang_set_ext[] = array(
121
			'ext_name' => 'phpbb/ads',
122 1
			'lang_set' => 'common',
123 1
		);
124
		$event['lang_set_ext'] = $lang_set_ext;
125
	}
126
127
	/**
128
	 * Displays advertisements
129
	 *
130 3
	 * @return	void
131
	 */
132
	public function setup_ads()
133
	{
134 3
		// check for the existence of 'MESSAGE_TEXT', which signals it's an error page.
135 3
		$non_content_page = $this->template->retrieve_var('MESSAGE_TEXT') || $this->is_non_content_page();
136 3
		$location_ids = $this->location_manager->get_all_location_ids();
137 3
		$user_groups = $this->manager->load_memberships($this->user->data['user_id']);
138 3
		$ad_ids = array();
139
140 3
		foreach ($this->manager->get_ads($location_ids, $user_groups, $non_content_page) as $row)
141
		{
142 3
			$ad_ids[] = $row['ad_id'];
143
144 3
			$this->template->assign_vars(array(
145 3
				'AD_' . strtoupper($row['location_id']) => htmlspecialchars_decode($row['ad_code'], ENT_COMPAT),
146 3
				'AD_' . strtoupper($row['location_id']) . '_ID' => (int) $row['ad_id'],
147 3
				'AD_' . strtoupper($row['location_id']) . '_CENTER' => (bool) $row['ad_centering'],
148 3
			));
149 3
		}
150
151 3
		$this->views($ad_ids);
152 3
	}
153
154
	/**
155
	 * Display Ad blocker friendly message if allowed
156
	 *
157
	 * @return	void
158
	 */
159 2
	public function adblocker()
160
	{
161 2
		$this->template->assign_var('S_DISPLAY_ADBLOCKER', (int) $this->config['phpbb_ads_adblocker_message']);
162 2
	}
163
164
	/**
165
	 * Add click tracking template variables
166
	 *
167
	 * @return	void
168
	 */
169 2
	public function clicks()
170
	{
171 2
		if ($this->config['phpbb_ads_enable_clicks'])
172 2
		{
173 1
			$this->template->assign_vars(array(
174 1
				'U_PHPBB_ADS_CLICK'		=> $this->controller_helper->route('phpbb_ads_click', array('data' => 0), true, ''),
175 1
				'S_PHPBB_ADS_ENABLE_CLICKS'	=> true,
176 1
			));
177 1
		}
178 2
	}
179
180
	/**
181
	 * Generate visual demo templates
182
	 *
183
	 * @return	void
184
	 */
185 2
	public function visual_demo()
186
	{
187 2
		if ($this->request->is_set($this->config['cookie_name'] . '_phpbb_ads_visual_demo', \phpbb\request\request_interface::COOKIE))
0 ignored issues
show
Bug introduced by
The type phpbb\request\request_interface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
188 2
		{
189 1
			$all_locations = $this->location_manager->get_all_locations(false);
190 1
			foreach ($this->location_manager->get_all_location_ids() as $location_id)
191
			{
192 1
				$this->template->assign_vars(array(
193 1
					'AD_' . strtoupper($location_id) . '_ID'	=> $location_id,
194 1
					'AD_' . strtoupper($location_id)			=> '<div class="phpbb-ads-visual-demo" title="' . $all_locations[$location_id]['desc'] . '">' . $all_locations[$location_id]['name'] . '</div>',
195 1
				));
196 1
			}
197
198 1
			$this->template->assign_vars(array(
199 1
				'S_PHPBB_ADS_VISUAL_DEMO'	=> true,
200 1
				'U_DISABLE_VISUAL_DEMO'		=> $this->controller_helper->route('phpbb_ads_visual_demo', array('action' => 'disable')),
201 1
			));
202 1
		}
203 2
	}
204
205
	/**
206
	 * Prepare views counter template
207
	 *
208
	 * @param	array	$ad_ids	List of ads that will be displayed on current request's page
209
	 * @return	void
210
	 */
211 3
	protected function views($ad_ids)
212
	{
213 3
		if ($this->config['phpbb_ads_enable_views'] && empty($this->user->data['is_bot']) && count($ad_ids))
214 3
		{
215 1
			$this->template->assign_vars(array(
216 1
				'S_INCREMENT_VIEWS'	=> true,
217 1
				'U_PHPBB_ADS_VIEWS'	=> $this->controller_helper->route('phpbb_ads_view', array('data' => implode('-', $ad_ids)), true, ''),
218 1
			));
219 1
		}
220 3
	}
221
222
	/**
223
	 * Disable XSS Protection
224
	 * In Chrome browsers, previewing an Ad Code with javascript can
225
	 * be blocked, due to a false positive where Chrome thinks the
226
	 * javascript is an XSS injection. This will temporarily disable
227
	 * XSS protection in chrome while managing ads in the ACP.
228
	 *
229
	 * @param	\phpbb\event\data	$event	The event object
230
	 */
231 6
	public function disable_xss_protection($event)
232
	{
233 6
		if (stripos($this->user->browser, 'chrome') !== false &&
234 4
			stripos($this->user->page['page'], 'phpbb-ads') !== false)
235 6
		{
236 2
			$event['http_headers'] = array_merge($event['http_headers'], ['X-XSS-Protection' => '0']);
237 2
		}
238 6
	}
239
240
	/**
241
	 * Remove ad owner when deleting user(s)
242
	 *
243
	 * @param	\phpbb\event\data	$event	The event object
244
	 * @return	void
245
	 */
246 1
	public function remove_ad_owner($event)
247
	{
248 1
		$this->manager->remove_ad_owner($event['user_ids']);
249 1
	}
250
251
	/**
252
	 * Destroy user_group cache after user was removed from the group.
253
	 *
254
	 * @return	void
255
	 */
256 1
	public function destroy_user_group_cache()
257
	{
258 1
		$this->cache->destroy('sql', USER_GROUP_TABLE);
0 ignored issues
show
Bug introduced by
The constant phpbb\ads\event\USER_GROUP_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
259 1
	}
260
261
	/**
262
	 * Check if the page user is currently on is a non-content page.
263
	 * This should include member list and details pages, posting and
264
	 * replying pages, anything inside the UCP, MCP and ACP.
265
	 *
266
	 * @return bool True or false
267
	 */
268
	protected function is_non_content_page()
269
	{
270
		return count(array_intersect([$this->user->page['page_name'], $this->user->page['page_dir']], [
271
			'memberlist.' . $this->php_ext,
272
			'viewonline.' . $this->php_ext,
273
			'posting.' . $this->php_ext,
274
			'ucp.' . $this->php_ext,
275
			'mcp.' . $this->php_ext,
276
			'adm',
277
		])) > 0;
278
	}
279
280
	/**
281
	 * Append additional agreement details to the privacy agreement.
282
	 *
283
	 * @return void
284
	 */
285
	public function append_agreement()
286
	{
287
		if (!$this->config['phpbb_ads_show_agreement']
288
			|| (strpos($this->user->page['page_name'], 'ucp') !== 0)
289
			|| !$this->template->retrieve_var('S_AGREEMENT')
290
			|| ($this->template->retrieve_var('AGREEMENT_TITLE') !== $this->language->lang('PRIVACY')))
291
		{
292
			return;
293
		}
294
295
		$this->language->add_lang('ucp', 'phpbb/ads');
296
297
		$this->template->append_var('AGREEMENT_TEXT', $this->language->lang('PHPBB_ADS_PRIVACY_POLICY', $this->config['sitename']));
298
	}
299
}
300