main_listener   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 259
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 11
Bugs 0 Features 0
Metric Value
eloc 80
c 11
b 0
f 0
dl 0
loc 259
rs 10
ccs 98
cts 98
cp 1
wmc 23

13 Methods

Rating   Name   Duplication   Size   Complexity  
A remove_ad_owner() 0 3 1
A destroy_user_group_cache() 0 3 1
A load_language_on_setup() 0 8 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 __construct() 0 12 1
A getSubscribedEvents() 0 11 1
A set_permissions() 0 5 1
A visual_demo() 0 16 3
A setup_ads() 0 22 3
A adblocker() 0 3 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;
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\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...
24
	protected $template;
25
26
	/** @var \phpbb\template\context */
0 ignored issues
show
Bug introduced by
The type phpbb\template\context 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_context;
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')),
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\template\template				$template			Template object
74
	 * @param \phpbb\template\context				$template_context	Template context 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\template\template $template, \phpbb\template\context $template_context, \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->template = $template;
87 20
		$this->template_context = $template_context;
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
		// Reason we access template's root ref is to check for existence
135 3
		// of 'MESSAGE_TEXT', which signals error page.
136 3
		$rootref = $this->template_context->get_root_ref();
137 3
		$non_content_page = !empty($rootref['MESSAGE_TEXT']) || $this->is_non_content_page();
138 3
		$location_ids = $this->location_manager->get_all_location_ids();
139
		$user_groups = $this->manager->load_memberships($this->user->data['user_id']);
140 3
		$ad_ids = array();
141
142 3
		foreach ($this->manager->get_ads($location_ids, $user_groups, $non_content_page) as $row)
143
		{
144 3
			$ad_ids[] = $row['ad_id'];
145 3
146 3
			$this->template->assign_vars(array(
147 3
				'AD_' . strtoupper($row['location_id']) => htmlspecialchars_decode($row['ad_code'], ENT_COMPAT),
148 3
				'AD_' . strtoupper($row['location_id']) . '_ID' => (int) $row['ad_id'],
149 3
				'AD_' . strtoupper($row['location_id']) . '_CENTER' => (bool) $row['ad_centering'],
150
			));
151 3
		}
152 3
153
		$this->views($ad_ids);
154
	}
155
156
	/**
157
	 * Display Ad blocker friendly message if allowed
158
	 *
159 2
	 * @return	void
160
	 */
161 2
	public function adblocker()
162 2
	{
163
		$this->template->assign_var('S_DISPLAY_ADBLOCKER', (int) $this->config['phpbb_ads_adblocker_message']);
164
	}
165
166
	/**
167
	 * Add click tracking template variables
168
	 *
169 2
	 * @return	void
170
	 */
171 2
	public function clicks()
172 2
	{
173 1
		if ($this->config['phpbb_ads_enable_clicks'])
174 1
		{
175 1
			$this->template->assign_vars(array(
176 1
				'U_PHPBB_ADS_CLICK'		=> $this->controller_helper->route('phpbb_ads_click', array('data' => 0), true, ''),
177 1
				'S_PHPBB_ADS_ENABLE_CLICKS'	=> true,
178 2
			));
179
		}
180
	}
181
182
	/**
183
	 * Generate visual demo templates
184
	 *
185 2
	 * @return	void
186
	 */
187 2
	public function visual_demo()
188 2
	{
189 1
		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...
190 1
		{
191
			$all_locations = $this->location_manager->get_all_locations(false);
192 1
			foreach ($this->location_manager->get_all_location_ids() as $location_id)
193 1
			{
194 1
				$this->template->assign_vars(array(
195 1
					'AD_' . strtoupper($location_id) . '_ID'	=> $location_id,
196 1
					'AD_' . strtoupper($location_id)			=> '<div class="phpbb-ads-visual-demo" title="' . $all_locations[$location_id]['desc'] . '">' . $all_locations[$location_id]['name'] . '</div>',
197
				));
198 1
			}
199 1
200 1
			$this->template->assign_vars(array(
201 1
				'S_PHPBB_ADS_VISUAL_DEMO'	=> true,
202 1
				'U_DISABLE_VISUAL_DEMO'		=> $this->controller_helper->route('phpbb_ads_visual_demo', array('action' => 'disable')),
203 2
			));
204
		}
205
	}
206
207
	/**
208
	 * Prepare views counter template
209
	 *
210
	 * @param	array	$ad_ids	List of ads that will be displayed on current request's page
211 3
	 * @return	void
212
	 */
213 3
	protected function views($ad_ids)
214 3
	{
215 1
		if ($this->config['phpbb_ads_enable_views'] && empty($this->user->data['is_bot']) && count($ad_ids))
216 1
		{
217 1
			$this->template->assign_vars(array(
218 1
				'S_INCREMENT_VIEWS'	=> true,
219 1
				'U_PHPBB_ADS_VIEWS'	=> $this->controller_helper->route('phpbb_ads_view', array('data' => implode('-', $ad_ids)), true, ''),
220 3
			));
221
		}
222
	}
223
224
	/**
225
	 * Disable XSS Protection
226
	 * In Chrome browsers, previewing an Ad Code with javascript can
227
	 * be blocked, due to a false positive where Chrome thinks the
228
	 * javascript is an XSS injection. This will temporarily disable
229
	 * XSS protection in chrome while managing ads in the ACP.
230
	 *
231 6
	 * @param	\phpbb\event\data	$event	The event object
232
	 */
233 6
	public function disable_xss_protection($event)
234 4
	{
235 6
		if (stripos($this->user->browser, 'chrome') !== false &&
236 2
			stripos($this->user->page['page'], 'phpbb-ads') !== false)
237 2
		{
238 6
			$event['http_headers'] = array_merge($event['http_headers'], ['X-XSS-Protection' => '0']);
239
		}
240
	}
241
242
	/**
243
	 * Remove ad owner when deleting user(s)
244
	 *
245
	 * @param	\phpbb\event\data	$event	The event object
246 1
	 * @return	void
247
	 */
248 1
	public function remove_ad_owner($event)
249 1
	{
250
		$this->manager->remove_ad_owner($event['user_ids']);
251
	}
252
253
	/**
254
	 * Destroy user_group cache after user was removed from the group.
255
	 *
256 1
	 * @return	void
257
	 */
258 1
	public function destroy_user_group_cache()
259 1
	{
260
		$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...
261
	}
262
263
	/**
264
	 * Check if the page user is currently on is a non-content page.
265
	 * This should include member list and details pages, posting and
266
	 * replying pages, anything inside the UCP, MCP and ACP.
267
	 *
268
	 * @return bool True or false
269
	 */
270
	protected function is_non_content_page()
271
	{
272
		return count(array_intersect([$this->user->page['page_name'], $this->user->page['page_dir']], [
273
			'memberlist.' . $this->php_ext,
274
			'viewonline.' . $this->php_ext,
275
			'posting.' . $this->php_ext,
276
			'ucp.' . $this->php_ext,
277
			'mcp.' . $this->php_ext,
278
			'adm',
279
		])) > 0;
280
	}
281
}
282