Passed
Push — develop-3.3.x ( ff97a1...be8ddf )
by Mario
03:24 queued 42s
created

overview_controller::handle_esi_action()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
/**
3
 *
4
 * PayPal Donation extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2015-2020 Skouat
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace skouat\ppde\controller\admin;
12
13
use phpbb\auth\auth;
14
use phpbb\config\config;
15
use phpbb\language\language;
16
use phpbb\log\log;
17
use phpbb\request\request;
18
use phpbb\template\template;
19
use phpbb\user;
20
use skouat\ppde\actions\core;
21
use skouat\ppde\actions\locale_icu;
22
use skouat\ppde\controller\extension_manager;
23
use skouat\ppde\controller\ipn_paypal;
24
use skouat\ppde\controller\main_controller;
25
26
/**
27
 * @property config     config              Config object
28
 * @property string     id_prefix_name      Prefix name for identifier in the URL
29
 * @property string     lang_key_prefix     Prefix for the messages thrown by exceptions
30
 * @property language   language            Language user object
31
 * @property log        log                 The phpBB log system
32
 * @property string     module_name         Name of the module currently used
33
 * @property locale_icu ppde_actions_locale PPDE Locale actions object
34
 * @property ipn_paypal ppde_ipn_paypal     IPN PayPal object
35
 * @property request    request             Request object
36
 * @property template   template            Template object
37
 * @property string     u_action            Action URL
38
 * @property user       user                User object
39
 */
40
class overview_controller extends admin_main
41
{
42
	protected $adm_relative_path;
43
	protected $auth;
44
	protected $ppde_actions;
45
	protected $ppde_controller_main;
46
	protected $ppde_controller_transactions;
47
	protected $ppde_ext_manager;
48
	protected $php_ext;
49
	protected $phpbb_admin_path;
50
	protected $phpbb_root_path;
51
52
	/**
53
	 * Constructor
54
	 *
55
	 * @param auth                    $auth                         Authentication object
56
	 * @param config                  $config                       Config object
57
	 * @param language                $language                     Language user object
58
	 * @param log                     $log                          The phpBB log system
59
	 * @param core                    $ppde_actions                 PPDE core actions object
60
	 * @param locale_icu              $ppde_actions_locale          PPDE Locale actions object
61
	 * @param main_controller         $ppde_controller_main         Main controller object
62
	 * @param transactions_controller $ppde_controller_transactions Admin transactions controller object
63
	 * @param extension_manager       $ppde_ext_manager             Extension manager object
64
	 * @param ipn_paypal              $ppde_ipn_paypal              IPN PayPal object
65
	 * @param request                 $request                      Request object
66
	 * @param template                $template                     Template object
67
	 * @param user                    $user                         User object
68
	 * @param string                  $adm_relative_path            phpBB admin relative path
69
	 * @param string                  $phpbb_root_path              phpBB root path
70
	 * @param string                  $php_ext                      phpEx
71
	 *
72
	 * @access public
73
	 */
74
	public function __construct(
75
		auth $auth,
76
		config $config,
77
		language $language,
78
		log $log,
79
		core $ppde_actions,
80
		locale_icu $ppde_actions_locale,
81
		main_controller $ppde_controller_main,
82
		transactions_controller $ppde_controller_transactions,
83
		extension_manager $ppde_ext_manager,
84
		ipn_paypal $ppde_ipn_paypal,
85
		request $request,
86
		template $template,
87
		user $user,
88
		$adm_relative_path,
89
		$phpbb_root_path,
90
		$php_ext
91
	)
92
	{
93
		$this->auth = $auth;
94
		$this->config = $config;
95
		$this->language = $language;
96
		$this->log = $log;
97
		$this->ppde_actions = $ppde_actions;
98
		$this->ppde_actions_locale = $ppde_actions_locale;
99
		$this->ppde_controller_main = $ppde_controller_main;
100
		$this->ppde_controller_transactions = $ppde_controller_transactions;
101
		$this->ppde_ext_manager = $ppde_ext_manager;
102
		$this->ppde_ipn_paypal = $ppde_ipn_paypal;
103
		$this->request = $request;
104
		$this->template = $template;
105
		$this->user = $user;
106
		$this->php_ext = $php_ext;
107
		$this->adm_relative_path = $adm_relative_path;
108
		$this->phpbb_admin_path = $phpbb_root_path . $adm_relative_path;
109
		$this->phpbb_root_path = $phpbb_root_path;
110
		parent::__construct(
111
			'overview',
112
			'PPDE_',
113
			''
114
		);
115
	}
116
117
	/**
118
	 * Display the overview page
119
	 *
120
	 * @param string $action Action name
121
	 *
122
	 * @return void
123
	 * @throws \ReflectionException
124
	 * @access public
125
	 */
126
	public function display_overview(string $action): void
127
	{
128
		$this->ppde_first_start();
129
130
		$this->do_action($action);
131
132
		//Load metadata for this extension
133
		$ext_meta = $this->ppde_ext_manager->get_ext_meta();
134
135
		// Set output block vars for display in the template
136
		$template_vars = $this->prepare_template_vars($ext_meta);
137
		$this->template->assign_vars($template_vars);
138
139
		if ($this->ppde_controller_main->use_sandbox())
140
		{
141
			$sandbox_template_vars = $this->prepare_sandbox_template_vars();
142
			$this->template->assign_vars($sandbox_template_vars);
143
		}
144
	}
145
146
	/**
147
	 * Executes the specified action.
148
	 *
149
	 * @param string $action The action to be executed.
150
	 *
151
	 * @return void
152
	 * @throws \ReflectionException
153
	 * @access private
154
	 */
155
	private function do_action(string $action): void
156
	{
157
		if (!$action)
158
		{
159
			return;
160
		}
161
162
		if (!confirm_box(true))
163
		{
164
			$this->display_confirm($action);
165
			return;
166
		}
167
168
		$this->exec_action($action);
169
	}
170
171
	/**
172
	 * Display confirm box
173
	 *
174
	 * @param string $action Requested action
175
	 *
176
	 * @return void
177
	 * @access private
178
	 */
179
	private function display_confirm(string $action): void
180
	{
181
		switch ($action)
182
		{
183
			case 'date':
184
				$confirm_lang = 'STAT_RESET_DATE_CONFIRM';
185
			break;
186
			case 'esi':
187
				$confirm_lang = 'STAT_RETEST_ESI_CONFIRM';
188
			break;
189
			case 'sandbox':
190
				$confirm_lang = 'STAT_RESYNC_SANDBOX_STATS_CONFIRM';
191
			break;
192
			case 'stats':
193
				$confirm_lang = 'STAT_RESYNC_STATS_CONFIRM';
194
			break;
195
			default:
196
				$confirm_lang = 'CONFIRM_OPERATION';
197
		}
198
199
		confirm_box(false, $this->language->lang($confirm_lang), build_hidden_fields(['action' => $action]));
200
	}
201
202
	/**
203
	 * @param string $action Requested action
204
	 *
205
	 * @return void
206
	 * @throws \ReflectionException
207
	 * @access private
208
	 */
209
	private function exec_action(string $action): void
210
	{
211
		if (!$this->auth->acl_get('a_ppde_manage'))
212
		{
213
			trigger_error($this->language->lang('NO_AUTH_OPERATION') . adm_back_link($this->u_action), E_USER_WARNING);
214
		}
215
216
		$action_handlers = [
217
			'date'    => [$this, 'handle_date_action'],
218
			'esi'     => [$this, 'handle_esi_action'],
219
			'sandbox' => [$this, 'handle_sandbox_action'],
220
			'stats'   => [$this, 'handle_stats_action'],
221
		];
222
223
		if (!isset($action_handlers[$action]))
224
		{
225
			trigger_error($this->language->lang('NO_MODE') . adm_back_link($this->u_action), E_USER_WARNING);
226
		}
227
228
		call_user_func($action_handlers[$action]);
229
	}
230
231
	private function prepare_template_vars($ext_meta): array
232
	{
233
		return array(
234
			'L_PPDE_ESI_INSTALL_DATE'        => $this->language->lang('PPDE_ESI_INSTALL_DATE', $ext_meta['extra']['display-name']),
235
			'L_PPDE_ESI_VERSION'             => $this->language->lang('PPDE_ESI_VERSION', $ext_meta['extra']['display-name']),
236
			'PPDE_ESI_INSTALL_DATE'          => $this->user->format_date($this->config['ppde_install_date']),
237
			'PPDE_ESI_TLS'                   => $this->config['ppde_tls_detected'] ? $this->language->lang('PPDE_ESI_DETECTED') : $this->language->lang('PPDE_ESI_NOT_DETECTED'),
238
			'PPDE_ESI_VERSION'               => $ext_meta['version'],
239
			'PPDE_ESI_VERSION_CURL'          => !empty($this->config['ppde_curl_version']) ? $this->config['ppde_curl_version'] : $this->language->lang('PPDE_ESI_NOT_DETECTED'),
240
			'PPDE_ESI_VERSION_INTL'          => $this->config['ppde_intl_detected'] ? $this->config['ppde_intl_version'] : $this->language->lang('PPDE_ESI_INTL_NOT_DETECTED'),
241
			'PPDE_ESI_VERSION_SSL'           => !empty($this->config['ppde_curl_ssl_version']) ? $this->config['ppde_curl_ssl_version'] : $this->language->lang('PPDE_ESI_NOT_DETECTED'),
242
			'S_ACTION_OPTIONS'               => $this->auth->acl_get('a_ppde_manage'),
243
			'S_CURL'                         => $this->config['ppde_curl_detected'],
244
			'S_INTL'                         => $this->config['ppde_intl_detected'] && $this->config['ppde_intl_version_valid'],
245
			'S_SSL'                          => $this->config['ppde_curl_detected'],
246
			'S_TLS'                          => $this->config['ppde_tls_detected'],
247
			'STATS_ANONYMOUS_DONORS_COUNT'   => $this->config['ppde_anonymous_donors_count'],
248
			'STATS_ANONYMOUS_DONORS_PER_DAY' => $this->per_day_stats('ppde_anonymous_donors_count'),
249
			'STATS_KNOWN_DONORS_COUNT'       => $this->config['ppde_known_donors_count'],
250
			'STATS_KNOWN_DONORS_PER_DAY'     => $this->per_day_stats('ppde_known_donors_count'),
251
			'STATS_TRANSACTIONS_COUNT'       => $this->config['ppde_transactions_count'],
252
			'STATS_TRANSACTIONS_PER_DAY'     => $this->per_day_stats('ppde_transactions_count'),
253
			'U_PPDE_MORE_INFORMATION'        => append_sid($this->phpbb_admin_path . 'index.' . $this->php_ext, 'i=acp_extensions&amp;mode=main&amp;action=details&amp;ext_name=' . urlencode($ext_meta['name'])),
254
			'U_ACTION'                       => $this->u_action,
255
		);
256
	}
257
258
	/**
259
	 * Returns the percent of items (transactions, donors) per day
260
	 *
261
	 * @param string $config_name
262
	 *
263
	 * @return string
264
	 * @access private
265
	 */
266
	private function per_day_stats(string $config_name): string
267
	{
268
		return sprintf('%.2f', (float) $this->config[$config_name] / $this->get_install_days());
269
	}
270
271
	/**
272
	 * Returns the number of days from the date of installation of the extension.
273
	 *
274
	 * @return float
275
	 * @access private
276
	 */
277
	private function get_install_days(): float
278
	{
279
		return (time() - $this->config['ppde_install_date']) / self::SECONDS_IN_A_DAY;
280
	}
281
282
	/**
283
	 * Prepares the template variables for the PayPal sandbox environment.
284
	 *
285
	 * @return array An array of template variables for the PayPal sandbox environment.
286
	 */
287
	private function prepare_sandbox_template_vars(): array
288
	{
289
		return [
290
			'S_IPN_TEST'                       => true,
291
			'SANDBOX_ANONYMOUS_DONORS_COUNT'   => $this->config['ppde_anonymous_donors_count_ipn'],
292
			'SANDBOX_ANONYMOUS_DONORS_PER_DAY' => $this->per_day_stats('ppde_anonymous_donors_count_ipn'),
293
			'SANDBOX_KNOWN_DONORS_COUNT'       => $this->config['ppde_known_donors_count_ipn'],
294
			'SANDBOX_KNOWN_DONORS_PER_DAY'     => $this->per_day_stats('ppde_known_donors_count_ipn'),
295
			'SANDBOX_TRANSACTIONS_COUNT'       => $this->config['ppde_transactions_count_ipn'],
296
			'SANDBOX_TRANSACTIONS_PER_DAY'     => $this->per_day_stats('ppde_transactions_count_ipn'),
297
		];
298
	}
299
300
	/**
301
	 * Handles the 'date' action.
302
	 * Resets the installation date to yesterday and logs the action.
303
	 *
304
	 * @return void
305
	 */
306
	private function handle_date_action(): void
307
	{
308
		$this->config->set('ppde_install_date', time() - 1);
309
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PPDE_STAT_RESET_DATE');
310
	}
311
312
	/**
313
	 * Handles the 'esi' (Extension System Information) action.
314
	 * Triggers a retest of the extension's system information and logs the action.
315
	 *
316
	 * @return void
317
	 */
318
	private function handle_esi_action(): void
319
	{
320
		$this->config->set('ppde_first_start', 1);
321
		$this->ppde_first_start();
322
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PPDE_STAT_RETEST_ESI');
323
	}
324
325
	/**
326
	 * Handles the 'sandbox' action.
327
	 * Updates the overview statistics for the sandbox environment and logs the action.
328
	 *
329
	 * @return void
330
	 */
331
	private function handle_sandbox_action(): void
332
	{
333
		$this->ppde_actions->set_ipn_test_properties(true);
334
		$this->ppde_actions->update_overview_stats();
335
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PPDE_STAT_SANDBOX_RESYNC');
336
	}
337
338
	/**
339
	 * Handles the 'stats' action.
340
	 * Updates the overview statistics for the live environment and logs the action.
341
	 *
342
	 * @return void
343
	 */
344
	private function handle_stats_action(): void
345
	{
346
		$this->ppde_actions->set_ipn_test_properties(false);
347
		$this->ppde_actions->update_overview_stats();
348
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PPDE_STAT_RESYNC');
349
	}
350
}
351