Completed
Branch master (915db5)
by Mario
03:03
created

admin_overview_controller::obtain_last_version()   B

Complexity

Conditions 5
Paths 10

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 33
rs 8.439
cc 5
eloc 19
nc 10
nop 0
1
<?php
2
/**
3
 *
4
 * PayPal Donation extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2015 Skouat
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace skouat\ppde\controller;
12
13
/**
14
 * @property string                   id_prefix_name     Prefix name for identifier in the URL
15
 * @property string                   lang_key_prefix    Prefix for the messages thrown by exceptions
16
 * @property \phpbb\log\log           log                The phpBB log system
17
 * @property string                   module_name        Name of the module currently used
18
 * @property \phpbb\request\request   request            Request object
19
 * @property \phpbb\template\template template           Template object
20
 * @property string                   u_action           Action URL
21
 * @property \phpbb\user              user               User object
22
 */
23
24
class admin_overview_controller extends admin_main
25
{
26
	protected $auth;
27
	protected $cache;
28
	protected $config;
29
	protected $ppde_controller_main;
30
	protected $ppde_controller_transactions;
31
	protected $php_ext;
32
33
	protected $ext_name;
34
	protected $ext_meta = array();
35
36
	/**
37
	 * Constructor
38
	 *
39
	 * @param \phpbb\auth\auth                                      $auth                         Authentication object
40
	 * @param \phpbb\cache\service                                  $cache                        Cache object
41
	 * @param \phpbb\config\config                                  $config                       Config object
42
	 * @param \phpbb\log\log                                        $log                          The phpBB log system
43
	 * @param \skouat\ppde\controller\main_controller               $ppde_controller_main         Main controller object
44
	 * @param \skouat\ppde\controller\admin_transactions_controller $ppde_controller_transactions Admin transactions controller object
45
	 * @param \phpbb\request\request                                $request                      Request object
46
	 * @param \phpbb\template\template                              $template                     Template object
47
	 * @param \phpbb\user                                           $user                         User object
48
	 * @param string                                                $php_ext                      phpEx
49
	 *
50
	 * @access public
51
	 */
52
	public function __construct(\phpbb\auth\auth $auth, \phpbb\cache\service $cache, \phpbb\config\config $config, \phpbb\log\log $log, \skouat\ppde\controller\main_controller $ppde_controller_main, \skouat\ppde\controller\admin_transactions_controller $ppde_controller_transactions, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user, $php_ext)
53
	{
54
		$this->auth = $auth;
55
		$this->cache = $cache;
56
		$this->config = $config;
57
		$this->log = $log;
58
		$this->ppde_controller_main = $ppde_controller_main;
59
		$this->ppde_controller_transactions = $ppde_controller_transactions;
60
		$this->request = $request;
61
		$this->template = $template;
62
		$this->user = $user;
63
		$this->php_ext = $php_ext;
64
		parent::__construct(
65
			'overview',
66
			'PPDE_',
67
			''
68
		);
69
	}
70
71
	/**
72
	 * Display the overview page
73
	 *
74
	 * @param string $action Action name
75
	 *
76
	 * @return null
77
	 * @access public
78
	 */
79
	public function display_overview($action)
80
	{
81
		$this->ppde_controller_main->first_start();
82
83
		$this->do_action($action);
84
85
		//Load metadata for this extension
86
		$this->ext_meta = $this->ppde_controller_main->load_metadata();
87
88
		// Check if a new version is available
89
		$this->obtain_last_version();
90
91
		// Set output block vars for display in the template
92
		$this->template->assign_vars(array(
93
			'ANONYMOUS_DONORS_COUNT'    => $this->config['ppde_anonymous_donors_count'],
94
			'ANONYMOUS_DONORS_PER_DAY'  => $this->per_day_stats('ppde_anonymous_donors_count'),
95
			'INFO_CURL'                 => $this->config['ppde_curl_detected'] ? $this->user->lang('INFO_CURL_VERSION', $this->config['ppde_curl_version'], $this->config['ppde_curl_ssl_version']) : $this->user->lang['INFO_NOT_DETECTED'],
96
			'INFO_FSOCKOPEN'            => $this->config['ppde_fsock_detected'] ? $this->user->lang['INFO_DETECTED'] : $this->user->lang['INFO_NOT_DETECTED'],
97
			'KNOWN_DONORS_COUNT'        => $this->config['ppde_known_donors_count'],
98
			'KNOWN_DONORS_PER_DAY'      => $this->per_day_stats('ppde_known_donors_count'),
99
			'L_PPDE_INSTALL_DATE'       => $this->user->lang('PPDE_INSTALL_DATE', $this->ext_meta['extra']['display-name']),
100
			'L_PPDE_VERSION'            => $this->user->lang('PPDE_VERSION', $this->ext_meta['extra']['display-name']),
101
			'PPDE_INSTALL_DATE'         => $this->user->format_date($this->config['ppde_install_date']),
102
			'PPDE_VERSION'              => $this->ext_meta['version'],
103
			'S_ACTION_OPTIONS'          => ($this->auth->acl_get('a_ppde_manage')) ? true : false,
104
			'S_CURL'                    => $this->config['ppde_curl_detected'],
105
			'S_FSOCKOPEN'               => $this->config['ppde_fsock_detected'],
106
			'TRANSACTIONS_COUNT'        => $this->config['ppde_transactions_count'],
107
			'TRANSACTIONS_PER_DAY'      => $this->per_day_stats('ppde_transactions_count'),
108
			'U_PPDE_MORE_INFORMATION'   => append_sid("index.$this->php_ext", 'i=acp_extensions&amp;mode=main&amp;action=details&amp;ext_name=' . urlencode($this->ext_meta['name'])),
109
			'U_PPDE_VERSIONCHECK_FORCE' => $this->u_action . '&amp;versioncheck_force=1',
110
			'U_ACTION'                  => $this->u_action,
111
		));
112
113
		if ($this->ppde_controller_main->use_sandbox())
114
		{
115
			// Set output block vars for display in the template
116
			$this->template->assign_vars(array(
117
				'S_IPN_TEST'                       => true,
118
				'SANDBOX_ANONYMOUS_DONORS_COUNT'   => $this->config['ppde_anonymous_donors_count_ipn'],
119
				'SANDBOX_ANONYMOUS_DONORS_PER_DAY' => $this->per_day_stats('ppde_anonymous_donors_count_ipn'),
120
				'SANDBOX_KNOWN_DONORS_COUNT'       => $this->config['ppde_known_donors_count_ipn'],
121
				'SANDBOX_KNOWN_DONORS_PER_DAY'     => $this->per_day_stats('ppde_known_donors_count_ipn'),
122
				'SANDBOX_TRANSACTIONS_COUNT'       => $this->config['ppde_transactions_count_ipn'],
123
				'SANDBOX_TRANSACTIONS_PER_DAY'     => $this->per_day_stats('ppde_transactions_count_ipn'),
124
			));
125
		}
126
	}
127
128
	/**
129
	 * Do action regarding the value of $action
130
	 *
131
	 * @param string $action Requested action
132
	 *
133
	 * @return null
134
	 * @access private
135
	 */
136
	private function do_action($action)
137
	{
138
		if ($action)
139
		{
140
			if (!confirm_box(true))
141
			{
142
				$this->display_confirm($action);
143
			}
144
			else
145
			{
146
				$this->exec_action($action);
147
			}
148
		}
149
	}
150
151
	/**
152
	 * Display confirm box
153
	 *
154
	 * @param string $action Requested action
155
	 *
156
	 * @return null
157
	 * @access private
158
	 */
159
	private function display_confirm($action)
160
	{
161
		switch ($action)
162
		{
163
			case 'date':
164
				$confirm = true;
165
				$confirm_lang = 'STAT_RESET_DATE_CONFIRM';
166
				break;
167
			case 'remote':
168
				$confirm = true;
169
				$confirm_lang = 'STAT_RETEST_CURL_FSOCK_CONFIRM';
170
				break;
171
			case 'sandbox':
172
				$confirm = true;
173
				$confirm_lang = 'STAT_RESYNC_SANDBOX_STATS_CONFIRM';
174
				break;
175
			case 'stats':
176
				$confirm = true;
177
				$confirm_lang = 'STAT_RESYNC_STATS_CONFIRM';
178
				break;
179
			default:
180
				$confirm = true;
181
				$confirm_lang = 'CONFIRM_OPERATION';
182
		}
183
184
		if ($confirm)
185
		{
186
			confirm_box(false, $this->user->lang[$confirm_lang], build_hidden_fields(array(
187
				'action' => $action,
188
			)));
189
		}
190
	}
191
192
	/**
193
	 * @param string $action Requested action
194
	 *
195
	 * @return null
196
	 * @access private
197
	 */
198
	private function exec_action($action)
199
	{
200 View Code Duplication
		if (!$this->auth->acl_get('a_ppde_manage'))
201
		{
202
			trigger_error($this->user->lang['NO_AUTH_OPERATION'] . adm_back_link($this->u_action), E_USER_WARNING);
203
		}
204
205
		switch ($action)
206
		{
207
			case 'date':
208
				$this->config->set('ppde_install_date', time() - 1);
209
				$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PPDE_STAT_RESET_DATE');
210
				break;
211
			case 'sandbox':
212
				$this->ppde_controller_transactions->update_stats(true);
213
				$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PPDE_STAT_SANDBOX_RESYNC');
214
				break;
215
			case 'stats':
216
				$this->ppde_controller_transactions->update_stats();
217
				$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PPDE_STAT_RESYNC');
218
				break;
219
			case 'remote':
220
				$this->ppde_controller_main->set_curl_info();
221
				$this->ppde_controller_main->set_remote_detected();
222
				$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PPDE_STAT_RETEST_REMOTE');
223
				break;
224
		}
225
	}
226
227
	/**
228
	 * Obtains the last version for this extension
229
	 *
230
	 * @return null
231
	 * @access private
232
	 */
233
	private function obtain_last_version()
234
	{
235
		try
236
		{
237
			if (!isset($this->ext_meta['extra']['version-check']))
238
			{
239
				throw new \RuntimeException($this->user->lang('PPDE_NO_VERSIONCHECK'), 1);
240
			}
241
242
			$version_check = $this->ext_meta['extra']['version-check'];
243
244
			$version_helper = new \phpbb\version_helper($this->cache, $this->config, new \phpbb\file_downloader(), $this->user);
245
			$version_helper->set_current_version($this->ext_meta['version']);
246
			$version_helper->set_file_location($version_check['host'], $version_check['directory'], $version_check['filename']);
247
			$version_helper->force_stability($this->config['extension_force_unstable'] ? 'unstable' : null);
248
249
			$recheck = $this->request->variable('versioncheck_force', false);
250
			$s_up_to_date = $version_helper->get_suggested_updates($recheck);
251
252
			$this->template->assign_vars(array(
253
				'S_UP_TO_DATE'   => empty($s_up_to_date),
254
				'S_VERSIONCHECK' => true,
255
				'UP_TO_DATE_MSG' => $this->user->lang('PPDE_NOT_UP_TO_DATE', $this->ext_meta['extra']['display-name']),
256
			));
257
		}
258
		catch (\RuntimeException $e)
259
		{
260
			$this->template->assign_vars(array(
261
				'S_VERSIONCHECK_STATUS'    => $e->getCode(),
262
				'VERSIONCHECK_FAIL_REASON' => ($e->getMessage() !== $this->user->lang('VERSIONCHECK_FAIL')) ? $e->getMessage() : '',
263
			));
264
		}
265
	}
266
267
	/**
268
	 * Returns the percent of items (transactions, donors) per day
269
	 *
270
	 * @param string $config_name
271
	 *
272
	 * @return string
273
	 * @access private
274
	 */
275
	private function per_day_stats($config_name)
276
	{
277
		return sprintf('%.2f', (float) $this->config[$config_name] / $this->get_install_days());
278
	}
279
280
	/**
281
	 * Returns the number of days from the date of installation of the extension.
282
	 *
283
	 * @return float
284
	 * @access private
285
	 */
286
	private function get_install_days()
287
	{
288
		return (float) (time() - $this->config['ppde_install_date']) / 86400;
289
	}
290
}
291