listener::getSubscribedEvents()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 *
4
 * Google Analytics extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2014 phpBB Limited <https://www.phpbb.com>
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace phpbb\googleanalytics\event;
12
13
use 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...
14
use 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...
15
use 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...
16
use 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...
17
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...
18
19
/**
20
 * Event listener
21
 */
22
class listener implements EventSubscriberInterface
23
{
24
	/** @var config */
25
	protected $config;
26
27
	/** @var language */
28
	protected $language;
29
30
	/** @var template */
31
	protected $template;
32
33
	/** @var user */
34
	protected $user;
35
36
	/**
37
	 * Constructor
38
	 *
39
	 * @param config   $config   Config object
40
	 * @param language $language Language object
41
	 * @param template $template Template object
42
	 * @param user     $user     User object
43
	 * @access public
44
	 */
45
	public function __construct(config $config, language $language, template $template, user $user)
46
	{
47
		$this->config = $config;
48
		$this->language = $language;
49
		$this->template = $template;
50
		$this->user = $user;
51
	}
52
53
	/**
54
	 * Assign functions defined in this class to event listeners in the core
55
	 *
56
	 * @return array
57
	 * @static
58
	 * @access public
59
	 */
60
	public static function getSubscribedEvents()
61
	{
62
		return [
63
			'core.page_header'				=> 'load_google_analytics',
64
			'core.acp_board_config_edit_add'	=> 'add_googleanalytics_configs',
65
			'core.validate_config_variable'	=> 'validate_googleanalytics_id',
66
			'core.page_footer_after'		=> 'append_agreement',
67
		];
68
	}
69
70
	/**
71
	 * Load Google Analytics js code
72
	 *
73
	 * @return void
74
	 * @access public
75
	 */
76
	public function load_google_analytics()
77
	{
78
		$this->template->assign_vars([
79
			'GOOGLEANALYTICS_ID'		=> $this->config['googleanalytics_id'],
80
			'GOOGLEANALYTICS_TAG'		=> $this->config['googleanalytics_tag'],
81
			'GOOGLEANALYTICS_USER_ID'	=> $this->user->data['user_id'],
82
			'S_ANONYMIZE_IP'			=> $this->config['ga_anonymize_ip'],
83
		]);
84
	}
85
86
	/**
87
	 * Add config vars to ACP Board Settings
88
	 *
89
	 * @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...
90
	 * @return void
91
	 * @access public
92
	 */
93
	public function add_googleanalytics_configs($event)
94
	{
95
		// Add a config to the settings mode, after warnings_expire_days
96
		if ($event['mode'] === 'settings' && isset($event['display_vars']['vars']['warnings_expire_days']))
97
		{
98
			// Load language file
99
			$this->language->add_lang('googleanalytics_acp', 'phpbb/googleanalytics');
100
101
			// Store display_vars event in a local variable
102
			$display_vars = $event['display_vars'];
103
104
			// Define the new config vars
105
			$ga_config_vars = [
106
				'legend_googleanalytics' => 'ACP_GOOGLEANALYTICS',
107
				'googleanalytics_id' => [
108
					'lang'		=> 'ACP_GOOGLEANALYTICS_ID',
109
					'validate'	=> 'googleanalytics_id',
110
					'type'		=> 'text:40:20',
111
					'explain'	=> true,
112
				],
113
				'ga_anonymize_ip' => [
114
					'lang'		=> 'ACP_GA_ANONYMIZE_IP',
115
					'validate'	=> 'bool',
116
					'type'		=> 'radio:yes_no',
117
					'explain'	=> true,
118
				],
119
				'googleanalytics_tag' => [
120
					'lang'		=> 'ACP_GOOGLEANALYTICS_TAG',
121
					'validate'	=> 'int',
122
					'type'		=> 'select',
123
					'function'	=> 'build_select',
124
					'params'	=> [[
125
						0	=> 'ACP_GA_ANALYTICS_TAG',
126
						1	=> 'ACP_GA_GTAGS_TAG',
127
					], '{CONFIG_VALUE}'],
128
					'explain'	=> true,
129
				],
130
			];
131
132
			// Add the new config vars after warnings_expire_days in the display_vars config array
133
			$insert_after = ['after' => 'warnings_expire_days'];
134
			$display_vars['vars'] = phpbb_insert_config_array($display_vars['vars'], $ga_config_vars, $insert_after);
0 ignored issues
show
Bug introduced by
The function phpbb_insert_config_array was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

134
			$display_vars['vars'] = /** @scrutinizer ignore-call */ phpbb_insert_config_array($display_vars['vars'], $ga_config_vars, $insert_after);
Loading history...
135
136
			// Update the display_vars event with the new array
137
			$event['display_vars'] = $display_vars;
138
		}
139
	}
140
141
	/**
142
	 * Validate the Google Analytics ID
143
	 *
144
	 * @param \phpbb\event\data $event The event object
145
	 * @return void
146
	 * @access public
147
	 */
148
	public function validate_googleanalytics_id($event)
149
	{
150
		// Check if the validate test is for google_analytics
151
		if ($event['config_definition']['validate'] !== 'googleanalytics_id' || empty($event['cfg_array']['googleanalytics_id']))
152
		{
153
			return;
154
		}
155
156
		// Store the input and error event data
157
		$input = $event['cfg_array']['googleanalytics_id'];
158
		$error = $event['error'];
159
160
		// Add error message if the input is not a valid Google Analytics ID
161
		if (!preg_match('/^UA-\d{4,9}-\d{1,4}$|^G-[A-Z0-9]{10}$/', $input))
162
		{
163
			$error[] = $this->language->lang('ACP_GOOGLEANALYTICS_ID_INVALID', $input);
164
		}
165
166
		// Add error message if GTAG is not selected for use with a Measurement ID
167
		if ((int) $event['cfg_array']['googleanalytics_tag'] === 0 && preg_match('/^G-[A-Z0-9]{10}$/', $input))
168
		{
169
			$error[] = $this->language->lang('ACP_GOOGLEANALYTICS_TAG_INVALID', $input);
170
		}
171
172
		// Update error event data
173
		$event['error'] = $error;
174
	}
175
176
	/**
177
	 * Append additional agreement details to the privacy agreement.
178
	 *
179
	 * @return void
180
	 */
181
	public function append_agreement()
182
	{
183
		if (!$this->config['googleanalytics_id']
184
			|| (strpos($this->user->page['page_name'], 'ucp') !== 0)
185
			|| !$this->template->retrieve_var('S_AGREEMENT')
186
			|| ($this->template->retrieve_var('AGREEMENT_TITLE') !== $this->language->lang('PRIVACY')))
187
		{
188
			return;
189
		}
190
191
		$this->language->add_lang('googleanalytics_ucp', 'phpbb/googleanalytics');
192
193
		$this->template->append_var('AGREEMENT_TEXT', $this->language->lang('PHPBB_ANALYTICS_PRIVACY_POLICY', $this->config['sitename']));
194
	}
195
}
196