overview_controller::display_confirm()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 16
c 2
b 0
f 0
nc 5
nop 1
dl 0
loc 21
rs 9.4222
1
<?php
2
/**
3
 *
4
 * PayPal Donation extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2015-2024 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\ext_manager;
23
use skouat\ppde\controller\esi_controller;
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 esi_controller esi_controller      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 ext_manager             $ppde_ext_manager             Extension manager object
64
	 * @param esi_controller          $esi_controller              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
	public function __construct(
73
		auth $auth,
74
		config $config,
75
		language $language,
76
		log $log,
77
		core $ppde_actions,
78
		locale_icu $ppde_actions_locale,
79
		main_controller $ppde_controller_main,
80
		transactions_controller $ppde_controller_transactions,
81
		ext_manager $ppde_ext_manager,
82
		esi_controller $esi_controller,
83
		request $request,
84
		template $template,
85
		user $user,
86
		$adm_relative_path,
87
		$phpbb_root_path,
88
		$php_ext
89
	)
90
	{
91
		$this->auth = $auth;
92
		$this->config = $config;
93
		$this->language = $language;
94
		$this->log = $log;
95
		$this->ppde_actions = $ppde_actions;
96
		$this->ppde_actions_locale = $ppde_actions_locale;
97
		$this->ppde_controller_main = $ppde_controller_main;
98
		$this->ppde_controller_transactions = $ppde_controller_transactions;
99
		$this->ppde_ext_manager = $ppde_ext_manager;
100
		$this->esi_controller = $esi_controller;
101
		$this->request = $request;
102
		$this->template = $template;
103
		$this->user = $user;
104
		$this->php_ext = $php_ext;
105
		$this->adm_relative_path = $adm_relative_path;
106
		$this->phpbb_admin_path = $phpbb_root_path . $adm_relative_path;
107
		$this->phpbb_root_path = $phpbb_root_path;
108
	}
109
110
	/**
111
	 * Display the overview page
112
	 *
113
	 * @param string $action Action name
114
	 *
115
	 * @throws \ReflectionException
116
	 */
117
	public function display_overview(string $action): void
118
	{
119
		$this->ppde_first_start();
120
121
		$this->do_action($action);
122
123
		//Load metadata for this extension
124
		$ext_meta = $this->ppde_ext_manager->get_ext_meta();
125
126
		// Set output block vars for display in the template
127
		$template_vars = $this->prepare_template_vars($ext_meta);
128
		$this->template->assign_vars($template_vars);
129
130
		if ($this->ppde_controller_main->use_sandbox())
131
		{
132
			$sandbox_template_vars = $this->prepare_sandbox_template_vars();
133
			$this->template->assign_vars($sandbox_template_vars);
134
		}
135
	}
136
137
	/**
138
	 * Executes the specified action.
139
	 *
140
	 * @param string $action The action to be executed.
141
	 *
142
	 * @throws \ReflectionException
143
	 */
144
	private function do_action(string $action): void
145
	{
146
		if (!$action)
147
		{
148
			return;
149
		}
150
151
		if (!confirm_box(true))
152
		{
153
			$this->display_confirm($action);
154
			return;
155
		}
156
157
		$this->exec_action($action);
158
	}
159
160
	/**
161
	 * Display confirm box
162
	 *
163
	 * @param string $action Requested action
164
	 */
165
	private function display_confirm(string $action): void
166
	{
167
		switch ($action)
168
		{
169
			case 'date':
170
				$confirm_lang = 'STAT_RESET_DATE_CONFIRM';
171
			break;
172
			case 'esi':
173
				$confirm_lang = 'STAT_RETEST_ESI_CONFIRM';
174
			break;
175
			case 'sandbox':
176
				$confirm_lang = 'STAT_RESYNC_SANDBOX_STATS_CONFIRM';
177
			break;
178
			case 'stats':
179
				$confirm_lang = 'STAT_RESYNC_STATS_CONFIRM';
180
			break;
181
			default:
182
				$confirm_lang = 'CONFIRM_OPERATION';
183
		}
184
185
		confirm_box(false, $this->language->lang($confirm_lang), build_hidden_fields(['action' => $action]));
186
	}
187
188
	/**
189
	 * @param string $action Requested action
190
	 *
191
	 * @throws \ReflectionException
192
	 */
193
	private function exec_action(string $action): void
194
	{
195
		if (!$this->auth->acl_get('a_ppde_manage'))
196
		{
197
			trigger_error($this->language->lang('NO_AUTH_OPERATION') . adm_back_link($this->u_action), E_USER_WARNING);
198
		}
199
200
		$action_handlers = [
201
			'date'    => [$this, 'handle_date_action'],
202
			'esi'     => [$this, 'handle_esi_action'],
203
			'sandbox' => [$this, 'handle_sandbox_action'],
204
			'stats'   => [$this, 'handle_stats_action'],
205
		];
206
207
		if (!isset($action_handlers[$action]))
208
		{
209
			trigger_error($this->language->lang('NO_MODE') . adm_back_link($this->u_action), E_USER_WARNING);
210
		}
211
212
		call_user_func($action_handlers[$action]);
213
	}
214
215
	private function prepare_template_vars($ext_meta): array
216
	{
217
		return [
218
			'L_PPDE_ESI_INSTALL_DATE'        => $this->language->lang('PPDE_ESI_INSTALL_DATE', $ext_meta['extra']['display-name']),
219
			'L_PPDE_ESI_VERSION'             => $this->language->lang('PPDE_ESI_VERSION', $ext_meta['extra']['display-name']),
220
			'PPDE_ESI_INSTALL_DATE'          => $this->user->format_date($this->config['ppde_install_date']),
221
			'PPDE_ESI_VERSION'               => $ext_meta['version'],
222
			'PPDE_ESI_VERSION_CURL'          => !empty($this->config['ppde_curl_version']) ? $this->config['ppde_curl_version'] : $this->language->lang('PPDE_ESI_NOT_DETECTED'),
223
			'PPDE_ESI_VERSION_INTL'          => $this->config['ppde_intl_detected'] ? $this->config['ppde_intl_version'] : $this->language->lang('PPDE_ESI_INTL_NOT_DETECTED'),
224
			'PPDE_ESI_VERSION_SSL'           => !empty($this->config['ppde_curl_ssl_version']) ? $this->config['ppde_curl_ssl_version'] : $this->language->lang('PPDE_ESI_NOT_DETECTED'),
225
			'PPDE_ESI_VERSION_TLS'           => $this->config['ppde_tls_detected'] ?: $this->language->lang('PPDE_ESI_NOT_DETECTED'),
226
			'S_ACTION_OPTIONS'               => $this->auth->acl_get('a_ppde_manage'),
227
			'S_CURL'                         => $this->config['ppde_curl_detected'],
228
			'S_INTL'                         => $this->config['ppde_intl_detected'] && $this->config['ppde_intl_version_valid'],
229
			'S_SSL'                          => $this->config['ppde_curl_detected'],
230
			'S_TLS'                          => $this->config['ppde_tls_detected'],
231
			'STATS_ANONYMOUS_DONORS_COUNT'   => $this->config['ppde_anonymous_donors_count'],
232
			'STATS_ANONYMOUS_DONORS_PER_DAY' => $this->per_day_stats('ppde_anonymous_donors_count'),
233
			'STATS_KNOWN_DONORS_COUNT'       => $this->config['ppde_known_donors_count'],
234
			'STATS_KNOWN_DONORS_PER_DAY'     => $this->per_day_stats('ppde_known_donors_count'),
235
			'STATS_TRANSACTIONS_COUNT'       => $this->config['ppde_transactions_count'],
236
			'STATS_TRANSACTIONS_PER_DAY'     => $this->per_day_stats('ppde_transactions_count'),
237
			'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'])),
238
			'U_ACTION'                       => $this->u_action,
239
		];
240
	}
241
242
	/**
243
	 * Returns the percent of items (transactions, donors) per day
244
	 *
245
	 * @param string $config_name
246
	 *
247
	 * @return string
248
	 */
249
	private function per_day_stats(string $config_name): string
250
	{
251
		return sprintf('%.2f', (float) $this->config[$config_name] / $this->get_install_days());
252
	}
253
254
	/**
255
	 * Returns the number of days from the date of installation of the extension.
256
	 *
257
	 * @return float
258
	 */
259
	private function get_install_days(): float
260
	{
261
		return (time() - $this->config['ppde_install_date']) / self::SECONDS_IN_A_DAY;
262
	}
263
264
	/**
265
	 * Prepares the template variables for the PayPal sandbox environment.
266
	 *
267
	 * @return array An array of template variables for the PayPal sandbox environment.
268
	 */
269
	private function prepare_sandbox_template_vars(): array
270
	{
271
		return [
272
			'S_IPN_TEST'                       => true,
273
			'SANDBOX_ANONYMOUS_DONORS_COUNT'   => $this->config['ppde_anonymous_donors_count_ipn'],
274
			'SANDBOX_ANONYMOUS_DONORS_PER_DAY' => $this->per_day_stats('ppde_anonymous_donors_count_ipn'),
275
			'SANDBOX_KNOWN_DONORS_COUNT'       => $this->config['ppde_known_donors_count_ipn'],
276
			'SANDBOX_KNOWN_DONORS_PER_DAY'     => $this->per_day_stats('ppde_known_donors_count_ipn'),
277
			'SANDBOX_TRANSACTIONS_COUNT'       => $this->config['ppde_transactions_count_ipn'],
278
			'SANDBOX_TRANSACTIONS_PER_DAY'     => $this->per_day_stats('ppde_transactions_count_ipn'),
279
		];
280
	}
281
282
	/**
283
	 * Handles the 'date' action.
284
	 * Resets the installation date to yesterday and logs the action.
285
	 */
286
	private function handle_date_action(): void
287
	{
288
		$this->config->set('ppde_install_date', time() - 1);
289
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PPDE_STAT_RESET_DATE');
290
	}
291
292
	/**
293
	 * Handles the 'esi' (Extension System Information) action.
294
	 * Triggers a retest of the extension's system information and logs the action.
295
	 * @throws \ReflectionException
296
	 */
297
	private function handle_esi_action(): void
298
	{
299
		$this->config->set('ppde_first_start', 1);
300
		$this->ppde_first_start();
301
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PPDE_STAT_RETEST_ESI');
302
	}
303
304
	/**
305
	 * Handles the 'sandbox' action.
306
	 * Updates the overview statistics for the sandbox environment and logs the action.
307
	 */
308
	private function handle_sandbox_action(): void
309
	{
310
		$this->ppde_actions->set_ipn_test_properties(true);
311
		$this->ppde_actions->update_overview_stats();
312
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PPDE_STAT_SANDBOX_RESYNC');
313
	}
314
315
	/**
316
	 * Handles the 'stats' action.
317
	 * Updates the overview statistics for the live environment and logs the action.
318
	 */
319
	private function handle_stats_action(): void
320
	{
321
		$this->ppde_actions->set_ipn_test_properties(false);
322
		$this->ppde_actions->update_overview_stats();
323
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PPDE_STAT_RESYNC');
324
	}
325
}
326