Completed
Push — master ( 9b85c2...3f27cb )
by Mario
02:45
created

main_controller::paypal_hidden_fields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 16
rs 9.4285
cc 1
eloc 13
nc 1
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
use Symfony\Component\DependencyInjection\ContainerInterface;
14
15
class main_controller
16
{
17
	protected $auth;
18
	protected $config;
19
	protected $container;
20
	protected $extension_manager;
21
	protected $helper;
22
	protected $ppde_entity_currency;
23
	protected $ppde_entity_donation_pages;
24
	protected $ppde_entity_transactions;
25
	protected $ppde_operator_currency;
26
	protected $ppde_operator_donation_pages;
27
	protected $ppde_operator_transactions;
28
	protected $request;
29
	protected $template;
30
	protected $user;
31
	protected $root_path;
32
	protected $php_ext;
33
	/** @var array */
34
	protected $ext_meta = array();
35
	/** @var string */
36
	protected $ext_name;
37
	/** @var string */
38
	private $donation_body;
39
	/** @var string */
40
	private $return_args_url;
41
	/** @var string */
42
	private $u_action;
43
44
	/**
45
	 * Constructor
46
	 *
47
	 * @param \phpbb\auth\auth                      $auth                         Auth object
48
	 * @param \phpbb\config\config                  $config                       Config object
49
	 * @param ContainerInterface                    $container                    Service container interface
50
	 * @param \phpbb\extension\manager              $extension_manager            An instance of the phpBB extension manager
51
	 * @param \phpbb\controller\helper              $helper                       Controller helper object
52
	 * @param \skouat\ppde\entity\currency          $ppde_entity_currency         Currency entity object
53
	 * @param \skouat\ppde\entity\donation_pages    $ppde_entity_donation_pages   Donation pages entity object
54
	 * @param \skouat\ppde\entity\transactions      $ppde_entity_transactions     Transactions log entity object
55
	 * @param \skouat\ppde\operators\currency       $ppde_operator_currency       Currency operator object
56
	 * @param \skouat\ppde\operators\donation_pages $ppde_operator_donation_pages Donation pages operator object
57
	 * @param \skouat\ppde\operators\transactions   $ppde_operator_transactions   Transactions log operator object
58
	 * @param \phpbb\request\request                $request                      Request object
59
	 * @param \phpbb\template\template              $template                     Template object
60
	 * @param \phpbb\user                           $user                         User object
61
	 * @param string                                $root_path                    phpBB root path
62
	 * @param string                                $php_ext                      phpEx
63
	 *
64
	 * @return \skouat\ppde\controller\main_controller
65
	 * @access public
66
	 */
67
	public function __construct(\phpbb\auth\auth $auth, \phpbb\config\config $config, ContainerInterface $container, \phpbb\extension\manager $extension_manager, \phpbb\controller\helper $helper, \skouat\ppde\entity\currency $ppde_entity_currency, \skouat\ppde\entity\donation_pages $ppde_entity_donation_pages, \skouat\ppde\entity\transactions $ppde_entity_transactions, \skouat\ppde\operators\currency $ppde_operator_currency, \skouat\ppde\operators\donation_pages $ppde_operator_donation_pages, \skouat\ppde\operators\transactions $ppde_operator_transactions, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user, $root_path, $php_ext)
68
	{
69
		$this->auth = $auth;
70
		$this->config = $config;
71
		$this->container = $container;
72
		$this->extension_manager = $extension_manager;
73
		$this->helper = $helper;
74
		$this->ppde_entity_currency = $ppde_entity_currency;
75
		$this->ppde_entity_donation_pages = $ppde_entity_donation_pages;
76
		$this->ppde_entity_transactions = $ppde_entity_transactions;
77
		$this->ppde_operator_currency = $ppde_operator_currency;
78
		$this->ppde_operator_donation_pages = $ppde_operator_donation_pages;
79
		$this->ppde_operator_transactions = $ppde_operator_transactions;
80
		$this->request = $request;
81
		$this->template = $template;
82
		$this->user = $user;
83
		$this->root_path = $root_path;
84
		$this->php_ext = $php_ext;
85
	}
86
87
	public function handle()
88
	{
89
		// When this extension is disabled, redirect users back to the forum index
90
		// Else if user is not allowed to use it, disallow access to the extension main page
91
		if (empty($this->config['ppde_enable']))
92
		{
93
			redirect(append_sid("{$this->root_path}index.{$this->php_ext}"));
94
		}
95
		else if (!$this->can_use_ppde())
96
		{
97
			trigger_error('NOT_AUTHORISED');
98
		}
99
100
		$this->set_return_args_url($this->request->variable('return', 'body'));
101
102
		// Prepare message for display
103
		if ($this->get_donation_content_data($this->return_args_url))
104
		{
105
			$this->ppde_entity_donation_pages->get_vars();
106
			$this->donation_body = $this->ppde_entity_donation_pages->replace_template_vars($this->ppde_entity_donation_pages->get_message_for_display());
107
		}
108
109
		$this->template->assign_vars(array(
110
			'DEFAULT_CURRENCY'   => $this->build_currency_select_menu($this->config['ppde_default_currency']),
111
			'DONATION_BODY'      => $this->donation_body,
112
			'PPDE_DEFAULT_VALUE' => $this->config['ppde_default_value'] ? $this->config['ppde_default_value'] : 0,
113
			'PPDE_LIST_VALUE'    => $this->build_currency_value_select_menu(),
114
115
			'S_HIDDEN_FIELDS'    => $this->paypal_hidden_fields(),
116
			'S_PPDE_FORM_ACTION' => $this->get_paypal_url(),
117
			'S_RETURN_ARGS'      => $this->return_args_url,
118
			'S_SANDBOX'          => $this->use_sandbox(),
119
		));
120
121
		$this->display_stats();
122
123
		// Send all data to the template file
124
		return $this->send_data_to_template();
125
	}
126
127
	public function donorlist_handle()
128
	{
129
		// If the donorlist is not enabled, redirect users back to the forum index
130
		// Else if user is not allowed to view the donors list, disallow access to the extension page
131
		if (!$this->donorlist_is_enabled())
132
		{
133
			redirect(append_sid($this->root_path . 'index.' . $this->php_ext));
134
		}
135
		else if (!$this->can_view_ppde_donorlist())
136
		{
137
			trigger_error('NOT_AUTHORISED');
138
		}
139
140
		// Get needed container
141
		/** @type \phpbb\pagination $pagination */
142
		$pagination = $this->container->get('pagination');
143
		/** @type \phpbb\path_helper $path_helper */
144
		$path_helper = $this->container->get('path_helper');
145
146
		// Set up general vars
147
		$default_key = 'd';
148
		$sort_key = $this->request->variable('sk', $default_key);
149
		$sort_dir = $this->request->variable('sd', 'd');
150
		$start = $this->request->variable('start', 0);
151
152
		// Sorting and order
153
		$sort_key_sql = array('a' => 'amount', 'd' => 'txn.payment_date', 'u' => 'u.username_clean');
154
155
		if (!isset($sort_key_sql[$sort_key]))
156
		{
157
			$sort_key = $default_key;
158
		}
159
160
		$order_by = $sort_key_sql[$sort_key] . ' ' . (($sort_dir == 'a') ? 'ASC' : 'DESC');
161
162
		// Build a relevant pagination_url and sort_url
163
		// We do not use request_var() here directly to save some calls (not all variables are set)
164
		$check_params = array(
165
			'sk'    => array('sk', $default_key),
166
			'sd'    => array('sd', 'a'),
167
			'start' => array('start', 0),
168
		);
169
170
		$params = $this->check_params($check_params, array('start'));
171
		$sort_params = $this->check_params($check_params, array('sk', 'sd'));
172
173
		// Set '$this->u_action'
174
		$use_page = ($this->u_action) ? $this->u_action : $this->user->page['page_name'];
175
		$this->u_action = reapply_sid($path_helper->get_valid_page($use_page, $this->config['enable_mod_rewrite']));
176
177
		$pagination_url = append_sid($this->u_action, implode('&amp;', $params), true, false, true);
178
		$sort_url = $this->set_url_delim(append_sid($this->u_action, implode('&amp;', $sort_params), true, false, true), $sort_params);
179
180
		$get_donorlist_sql_ary = $this->ppde_operator_transactions->get_sql_donorlist_ary(false, $order_by);
181
		$total_donors = $this->ppde_operator_transactions->query_sql_count($get_donorlist_sql_ary, 'txn.user_id');
182
		$start = $pagination->validate_start($start, $this->config['topics_per_page'], $total_donors);
183
184
		$pagination->generate_template_pagination($pagination_url, 'pagination', 'start', $total_donors, $this->config['topics_per_page'], $start);
185
186
		// adds fields to the table schema needed by entity->import()
187
		$additional_table_schema = array(
188
			'item_username'    => array('name' => 'username', 'type' => 'string'),
189
			'item_user_colour' => array('name' => 'user_colour', 'type' => 'string'),
190
			'item_amount'      => array('name' => 'amount', 'type' => 'float'),
191
			'item_max_txn_id'  => array('name' => 'max_txn_id', 'type' => 'integer'),
192
		);
193
194
		$data_ary = $this->ppde_entity_transactions->get_data($this->ppde_operator_transactions->build_sql_donorlist_data($get_donorlist_sql_ary), $additional_table_schema, $this->config['topics_per_page'], $start);
195
196
		// Get default currency data from the database
197
		$default_currency_data = $this->get_default_currency_data($this->config['ppde_default_currency']);
198
		$this->template->assign_vars(array(
199
			'TOTAL_DONORS'    => $this->user->lang('PPDE_DONORS', $total_donors),
200
			'U_SORT_AMOUNT'   => $sort_url . 'sk=a&amp;sd=' . $this->set_sort_key($sort_key, 'a', $sort_dir),
201
			'U_SORT_DONATED'  => $sort_url . 'sk=d&amp;sd=' . $this->set_sort_key($sort_key, 'd', $sort_dir),
202
			'U_SORT_USERNAME' => $sort_url . 'sk=u&amp;sd=' . $this->set_sort_key($sort_key, 'u', $sort_dir),
203
		));
204
205
		foreach ($data_ary as $data)
206
		{
207
			$get_last_transaction_sql_ary = $this->ppde_operator_transactions->get_sql_donorlist_ary($data['max_txn_id']);
208
			$last_donation_data = $this->ppde_entity_transactions->get_data($this->ppde_operator_transactions->build_sql_donorlist_data($get_last_transaction_sql_ary));
209
			$this->template->assign_block_vars('donorrow', array(
210
				'PPDE_DONOR_USERNAME'       => get_username_string('full', $data['user_id'], $data['username'], $data['user_colour']),
211
				'PPDE_LAST_DONATED_AMOUNT'  => $this->currency_on_left(number_format($last_donation_data[0]['mc_gross'], 2), $default_currency_data[0]['currency_symbol'], (bool) $default_currency_data[0]['currency_on_left']),
212
				'PPDE_LAST_PAYMENT_DATE'    => $this->user->format_date($last_donation_data[0]['payment_date']),
213
				'PPDE_TOTAL_DONATED_AMOUNT' => $this->currency_on_left(number_format($data['amount'], 2), $default_currency_data[0]['currency_symbol'], (bool) $default_currency_data[0]['currency_on_left']),
214
			));
215
		}
216
217
		// Set "return_args_url" object before sending data to template
218
		$this->set_return_args_url('donorlist');
219
220
		// Send all data to the template file
221
		return $this->send_data_to_template();
222
	}
223
224
	/**
225
	 * Set the sort key value
226
	 *
227
	 * @param string $sk
228
	 * @param string $sk_comp
229
	 * @param string $sd
230
	 *
231
	 * @return string
232
	 * @access private
233
	 */
234
	private function set_sort_key($sk, $sk_comp, $sd)
235
	{
236
		return ($sk == $sk_comp && $sd == 'a') ? 'd' : 'a';
237
	}
238
239
	/**
240
	 * Simply adds an url or &amp; delimiter to the url when params is empty
241
	 *
242
	 * @param $url
243
	 * @param $params
244
	 *
245
	 * @return string
246
	 * @access private
247
	 */
248
	private function set_url_delim($url, $params)
249
	{
250
		return (empty($params)) ? $url . '?' : $url . '&amp;';
251
	}
252
253
	/**
254
	 * @param array    $params_ary
255
	 * @param string[] $excluded_keys
256
	 *
257
	 * @return array
258
	 * @access private
259
	 */
260
	private function check_params($params_ary, $excluded_keys)
261
	{
262
		$params = array();
263
264
		foreach ($params_ary as $key => $call)
265
		{
266
			if (!isset($_REQUEST[$key]))
267
			{
268
				continue;
269
			}
270
271
			$param = call_user_func_array('request_var', $call);
272
			$param = urlencode($key) . '=' . ((is_string($param)) ? urlencode($param) : $param);
273
274
			if (!in_array($key, $excluded_keys))
275
			{
276
				$params[] = $param;
277
			}
278
		}
279
280
		return $params;
281
	}
282
283
	/**
284
	 * @return bool
285
	 * @access public
286
	 */
287
	public function can_use_ppde()
288
	{
289
		return $this->auth->acl_get('u_ppde_use');
290
	}
291
292
	/**
293
	 * @return bool
294
	 * @access public
295
	 */
296
	public function can_view_ppde_donorlist()
297
	{
298
		return $this->auth->acl_get('u_ppde_view_donorlist');
299
	}
300
301
	/**
302
	 * @return bool
303
	 * @access private
304
	 */
305
	private function donorlist_is_enabled()
306
	{
307
		return $this->use_ipn() && $this->config['ppde_ipn_donorlist_enable'];
308
	}
309
310
	/**
311
	 * @param string $set_return_args_url
312
	 *
313
	 * @return null
314
	 * @access private
315
	 */
316
	private function set_return_args_url($set_return_args_url)
317
	{
318
		switch ($set_return_args_url)
319
		{
320
			case 'cancel':
321
			case 'success':
322
				$this->template->assign_vars(array(
323
					'L_PPDE_DONATION_TITLE' => $this->user->lang['PPDE_' . strtoupper($set_return_args_url) . '_TITLE'],
324
				));
325
				$this->return_args_url = $set_return_args_url;
326
				break;
327
			case 'donorlist':
328
				$this->template->assign_vars(array(
329
					'L_PPDE_DONORLIST_TITLE' => $this->user->lang['PPDE_DONORLIST_TITLE'],
330
				));
331
				$this->return_args_url = $set_return_args_url;
332
				break;
333
			default:
334
				$this->return_args_url = 'body';
335
		}
336
337
	}
338
339
	/**
340
	 * Get content of current donation pages
341
	 *
342
	 * @param string $return_args_url
343
	 *
344
	 * @return array
345
	 * @access private
346
	 */
347
	private function get_donation_content_data($return_args_url)
348
	{
349
		return $this->ppde_entity_donation_pages->get_data(
350
				$this->ppde_operator_donation_pages->build_sql_data($this->user->get_iso_lang_id(), $return_args_url)
351
		);
352
	}
353
354
	/**
355
	 * Build pull down menu options of available currency
356
	 *
357
	 * @param int $config_value Currency identifier; default: 0
358
	 *
359
	 * @return null
360
	 * @access public
361
	 */
362
	public function build_currency_select_menu($config_value = 0)
363
	{
364
		// Grab the list of all enabled currencies; 0 is for all data
365
		$currency_items = $this->ppde_entity_currency->get_data($this->ppde_operator_currency->build_sql_data(0, true));
366
367
		// Process each rule menu item for pull-down
368
		foreach ($currency_items as $currency_item)
369
		{
370
			// Set output block vars for display in the template
371
			$this->template->assign_block_vars('options', array(
372
				'CURRENCY_ID'        => (int) $currency_item['currency_id'],
373
				'CURRENCY_ISO_CODE'  => $currency_item['currency_iso_code'],
374
				'CURRENCY_NAME'      => $currency_item['currency_name'],
375
				'CURRENCY_SYMBOL'    => $currency_item['currency_symbol'],
376
				'S_CURRENCY_DEFAULT' => $config_value == $currency_item['currency_id'],
377
			));
378
		}
379
		unset ($currency_items, $currency_item);
380
	}
381
382
	/**
383
	 * Build pull down menu options of available currency value
384
	 *
385
	 * @return string List of currency value set in ACP for dropdown menu
386
	 * @access private
387
	 */
388
	private function build_currency_value_select_menu()
389
	{
390
		$list_donation_value = '';
391
392
		if ($this->get_dropbox_status())
393
		{
394
			$donation_ary_value = explode(',', $this->config['ppde_dropbox_value']);
395
396
			foreach ($donation_ary_value as $value)
397
			{
398
				$int_value = $this->settype_dropbox_int_value($value);
399
				$list_donation_value .= !empty($int_value) ? '<option value="' . $int_value . '">' . $int_value . '</option>' : '';
400
			}
401
			unset($value);
402
		}
403
404
		return $list_donation_value;
405
	}
406
407
	/**
408
	 * Get dropbox config value
409
	 *
410
	 * @return bool
411
	 * @access private
412
	 */
413
	private function get_dropbox_status()
414
	{
415
		return $this->config['ppde_dropbox_enable'] && $this->config['ppde_dropbox_value'];
416
	}
417
418
	/**
419
	 * Force dropbox value to integer
420
	 *
421
	 * @param int $value
422
	 *
423
	 * @return int
424
	 * @access private
425
	 */
426
	private function settype_dropbox_int_value($value = 0)
427
	{
428
		if (settype($value, 'integer') && $value != 0)
429
		{
430
			return $value;
431
		}
432
433
		return 0;
434
	}
435
436
	/**
437
	 * Build PayPal hidden fields
438
	 *
439
	 * @return string PayPal hidden field needed to fill PayPal forms
440
	 * @access private
441
	 */
442
	private function paypal_hidden_fields()
443
	{
444
		return build_hidden_fields(array(
445
			'cmd'           => '_donations',
446
			'business'      => $this->get_account_id(),
447
			'item_name'     => $this->user->lang['PPDE_DONATION_TITLE_HEAD'] . ' ' . $this->config['sitename'],
448
			'no_shipping'   => 1,
449
			'return'        => $this->generate_paypal_return_url('success'),
450
			'notify_url'    => $this->generate_paypal_notify_return_url(),
451
			'cancel_return' => $this->generate_paypal_return_url('cancel'),
452
			'item_number'   => 'uid_' . $this->user->data['user_id'] . '_' . time(),
453
			'tax'           => 0,
454
			'bn'            => 'Board_Donate_WPS',
455
			'charset'       => 'utf-8',
456
		));
457
	}
458
459
	/**
460
	 * Get PayPal account id
461
	 *
462
	 * @return string $this Paypal account Identifier
463
	 * @access private
464
	 */
465
	private function get_account_id()
466
	{
467
		return $this->use_sandbox() ? $this->config['ppde_sandbox_address'] : $this->config['ppde_account_id'];
468
	}
469
470
	/**
471
	 * Check if Sandbox is enabled based on config value
472
	 *
473
	 * @return bool
474
	 * @access public
475
	 */
476
	public function use_sandbox()
477
	{
478
		return $this->use_ipn() && !empty($this->config['ppde_sandbox_enable']) && $this->is_sandbox_founder_enable();
479
	}
480
481
	/**
482
	 * Check if Sandbox could be use by founders based on config value
483
	 *
484
	 * @return bool
485
	 * @access public
486
	 */
487
	public function is_sandbox_founder_enable()
488
	{
489
		return (!empty($this->config['ppde_sandbox_founder_enable']) && ($this->user->data['user_type'] == USER_FOUNDER)) || empty($this->config['ppde_sandbox_founder_enable']);
490
	}
491
492
	/**
493
	 * Check if IPN is enabled based on config value
494
	 *
495
	 * @return bool
496
	 * @access public
497
	 */
498
	public function use_ipn()
499
	{
500
		return !empty($this->config['ppde_enable']) && !empty($this->config['ppde_ipn_enable']) && $this->is_remote_detected();
501
	}
502
	/**
503
	 * Check if remote is detected based on config value
504
	 *
505
	 * @return bool
506
	 * @access public
507
	 */
508
	public function is_remote_detected()
509
	{
510
		return !empty($this->config['ppde_curl_detected']) || !empty($this->config['ppde_fsockopen_detected']);
511
	}
512
513
	/**
514
	 * Generate PayPal return URL
515
	 *
516
	 * @param string $arg
517
	 *
518
	 * @return string
519
	 * @access private
520
	 */
521
	private function generate_paypal_return_url($arg)
522
	{
523
		return generate_board_url(true) . $this->helper->route('skouat_ppde_donate', array('return' => $arg));
524
	}
525
526
	/**
527
	 * Generate PayPal return notify URL
528
	 *
529
	 * @return string
530
	 * @access private
531
	 */
532
	private function generate_paypal_notify_return_url()
533
	{
534
		return generate_board_url(true) . $this->helper->route('skouat_ppde_ipn_listener');
535
	}
536
537
	/**
538
	 * Get PayPal URL
539
	 * Used in form and in IPN process
540
	 *
541
	 * @param bool $is_test_ipn
542
	 *
543
	 * @return string
544
	 * @access public
545
	 */
546
	public function get_paypal_url($is_test_ipn = false)
547
	{
548
		return ($is_test_ipn || $this->use_sandbox()) ? 'https://www.sandbox.paypal.com/cgi-bin/webscr' : 'https://www.paypal.com/cgi-bin/webscr';
549
	}
550
551
	/**
552
	 * Assign statistics vars to the template
553
	 *
554
	 * @return null
555
	 * @access public
556
	 */
557
	public function display_stats()
558
	{
559
		if ($this->config['ppde_goal_enable'] || $this->config['ppde_raised_enable'] || $this->config['ppde_used_enable'])
560
		{
561
			// Get data from the database
562
			$default_currency_data = $this->get_default_currency_data($this->config['ppde_default_currency']);
563
564
			$this->template->assign_vars(array(
565
				'PPDE_GOAL_ENABLE'   => $this->config['ppde_goal_enable'],
566
				'PPDE_RAISED_ENABLE' => $this->config['ppde_raised_enable'],
567
				'PPDE_USED_ENABLE'   => $this->config['ppde_used_enable'],
568
569
				'L_PPDE_GOAL'        => $this->get_ppde_goal_langkey($default_currency_data[0]['currency_symbol'], (bool) $default_currency_data[0]['currency_on_left']),
570
				'L_PPDE_RAISED'      => $this->get_ppde_raised_langkey($default_currency_data[0]['currency_symbol'], (bool) $default_currency_data[0]['currency_on_left']),
571
				'L_PPDE_USED'        => $this->get_ppde_used_langkey($default_currency_data[0]['currency_symbol'], (bool) $default_currency_data[0]['currency_on_left']),
572
			));
573
574
			// Generate statistics percent for display
575
			$this->generate_stats_percent();
576
		}
577
	}
578
579
	/**
580
	 * Get default currency symbol
581
	 *
582
	 * @param int $id
583
	 *
584
	 * @return array
585
	 * @access public
586
	 */
587
	public function get_default_currency_data($id = 0)
588
	{
589
		return $this->ppde_entity_currency->get_data($this->ppde_operator_currency->build_sql_data($id, true));
590
	}
591
592
	/**
593
	 * Retrieve the language key for donation goal
594
	 *
595
	 * @param string $currency_symbol Currency symbol
596
	 * @param bool   $on_left         Symbol position
597
	 *
598
	 * @return string
599
	 * @access public
600
	 */
601
	public function get_ppde_goal_langkey($currency_symbol, $on_left = true)
602
	{
603
		if ((int) $this->config['ppde_goal'] <= 0)
604
		{
605
			$l_ppde_goal = $this->user->lang['PPDE_DONATE_NO_GOAL'];
606
		}
607 View Code Duplication
		else if ((int) $this->config['ppde_goal'] < (int) $this->config['ppde_raised'])
608
		{
609
			$l_ppde_goal = $this->user->lang['PPDE_DONATE_GOAL_REACHED'];
610
		}
611
		else
612
		{
613
			$l_ppde_goal = $this->user->lang('PPDE_DONATE_GOAL_RAISE', $this->currency_on_left((int) $this->config['ppde_goal'], $currency_symbol, $on_left));
614
		}
615
616
		return $l_ppde_goal;
617
	}
618
619
	/**
620
	 * Put the currency on the left or on the right of the amount
621
	 *
622
	 * @param int    $value
623
	 * @param string $currency
624
	 * @param bool   $on_left
625
	 *
626
	 * @return string
627
	 * @access public
628
	 */
629
	public function currency_on_left($value, $currency, $on_left = true)
630
	{
631
		return $on_left ? $currency . $value : $value . $currency;
632
	}
633
634
	/**
635
	 * Retrieve the language key for donation raised
636
	 *
637
	 * @param string $currency_symbol Currency symbol
638
	 * @param bool   $on_left         Symbol position
639
	 *
640
	 * @return string
641
	 * @access public
642
	 */
643
	public function get_ppde_raised_langkey($currency_symbol, $on_left = true)
644
	{
645 View Code Duplication
		if ((int) $this->config['ppde_raised'] <= 0)
646
		{
647
			$l_ppde_raised = $this->user->lang['PPDE_DONATE_NOT_RECEIVED'];
648
		}
649
		else
650
		{
651
			$l_ppde_raised = $this->user->lang('PPDE_DONATE_RECEIVED', $this->currency_on_left((int) $this->config['ppde_raised'], $currency_symbol, $on_left));
652
		}
653
654
		return $l_ppde_raised;
655
	}
656
657
	/**
658
	 * Retrieve the language key for donation used
659
	 *
660
	 * @param string $currency_symbol Currency symbol
661
	 * @param bool   $on_left         Symbol position
662
	 *
663
	 * @return string
664
	 * @access public
665
	 */
666
	public function get_ppde_used_langkey($currency_symbol, $on_left = true)
667
	{
668
		if ((int) $this->config['ppde_used'] <= 0)
669
		{
670
			$l_ppde_used = $this->user->lang['PPDE_DONATE_NOT_USED'];
671
		}
672
		else if ((int) $this->config['ppde_used'] < (int) $this->config['ppde_raised'])
673
		{
674
			$l_ppde_used = $this->user->lang('PPDE_DONATE_USED', $this->currency_on_left((int) $this->config['ppde_used'], $currency_symbol, $on_left), $this->currency_on_left((int) $this->config['ppde_raised'], $currency_symbol, $on_left));
675
		}
676
		else
677
		{
678
			$l_ppde_used = $this->user->lang('PPDE_DONATE_USED_EXCEEDED', $this->currency_on_left((int) $this->config['ppde_used'], $currency_symbol, $on_left));
679
		}
680
681
		return $l_ppde_used;
682
	}
683
684
	/**
685
	 * Generate statistics percent for display
686
	 *
687
	 * @return null
688
	 * @access private
689
	 */
690
	private function generate_stats_percent()
691
	{
692 View Code Duplication
		if ($this->is_ppde_goal_stats())
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
693
		{
694
			$percent = $this->percent_value((int) $this->config['ppde_raised'], (int) $this->config['ppde_goal']);
695
			$this->assign_vars_stats_percent($percent, 'GOAL_NUMBER');
696
			$this->template->assign_var('PPDE_CSS_GOAL_NUMBER', $this->ppde_css_classname($percent));
697
		}
698
699 View Code Duplication
		if ($this->is_ppde_used_stats())
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
700
		{
701
			$percent = $this->percent_value((int) $this->config['ppde_used'], (int) $this->config['ppde_raised']);
702
			$this->assign_vars_stats_percent($percent, 'USED_NUMBER');
703
			$this->template->assign_var('PPDE_CSS_USED_NUMBER', $this->ppde_css_classname($percent, true));
704
		}
705
	}
706
707
	/**
708
	 * Checks if stats can be displayed
709
	 *
710
	 * @return bool
711
	 * @access private
712
	 */
713
	private function is_ppde_goal_stats()
714
	{
715
		return $this->config['ppde_goal_enable'] && (int) $this->config['ppde_goal'] > 0;
716
	}
717
718
	/**
719
	 * Checks if stats can be displayed
720
	 *
721
	 * @return bool
722
	 * @access private
723
	 */
724
	private function is_ppde_used_stats()
725
	{
726
		return $this->config['ppde_used_enable'] && (int) $this->config['ppde_raised'] > 0 && (int) $this->config['ppde_used'] > 0;
727
	}
728
729
730
	/**
731
	 * Returns percent value
732
	 *
733
	 * @param integer $multiplicand
734
	 * @param integer $dividend
735
	 *
736
	 * @return float
737
	 * @access private
738
	 */
739
	private function percent_value($multiplicand, $dividend)
740
	{
741
		return round(($multiplicand * 100) / $dividend, 2);
742
	}
743
744
	/**
745
	 * Assign statistics percent vars to template
746
	 *
747
	 * @param float  $percent
748
	 * @param string $type
749
	 *
750
	 * @return null
751
	 * @access private
752
	 */
753
	private function assign_vars_stats_percent($percent, $type)
754
	{
755
		$this->template->assign_vars(array(
756
			'PPDE_' . $type => $percent,
757
			'S_' . $type    => !empty($type) ? true : false,
758
		));
759
	}
760
761
	/**
762
	 * Returns the CSS class name based on the percent of stats
763
	 *
764
	 * @param float $value
765
	 * @param bool  $reverse
766
	 *
767
	 * @return string
768
	 * @access private
769
	 */
770
	private function ppde_css_classname($value, $reverse = false)
771
	{
772
		$css_reverse = '';
773
774
		if ($reverse && $value < 100)
775
		{
776
			$value = 100 - $value;
777
			$css_reverse = '-reverse';
778
		}
779
780
		switch ($value)
781
		{
782
			case ($value <= 10):
783
				return 'ten' . $css_reverse;
784
			case ($value <= 20):
785
				return 'twenty' . $css_reverse;
786
			case ($value <= 30):
787
				return 'thirty' . $css_reverse;
788
			case ($value <= 40):
789
				return 'forty' . $css_reverse;
790
			case ($value <= 50):
791
				return 'fifty' . $css_reverse;
792
			case ($value <= 60):
793
				return 'sixty' . $css_reverse;
794
			case ($value <= 70):
795
				return 'seventy' . $css_reverse;
796
			case ($value <= 80):
797
				return 'eighty' . $css_reverse;
798
			case ($value <= 90):
799
				return 'ninety' . $css_reverse;
800
			case ($value < 100):
801
				return 'hundred' . $css_reverse;
802
			default:
803
				return $reverse ? 'red' : 'green';
804
		}
805
	}
806
807
	/**
808
	 * Send data to the template file
809
	 *
810
	 * @return \Symfony\Component\HttpFoundation\Response
811
	 * @access private
812
	 */
813
	private function send_data_to_template()
814
	{
815
		switch ($this->return_args_url)
816
		{
817
			case 'cancel':
818
			case 'success':
819
				return $this->helper->render('donate_body.html', $this->user->lang('PPDE_' . strtoupper($this->return_args_url) . '_TITLE'));
820
			case 'donorlist':
821
				return $this->helper->render('donorlist_body.html', $this->user->lang('PPDE_DONORLIST_TITLE'));
822
			default:
823
				return $this->helper->render('donate_body.html', $this->user->lang('PPDE_DONATION_TITLE'));
824
		}
825
	}
826
827
	/**
828
	 * Do action if it's the first time the extension is accessed
829
	 *
830
	 * @return null
831
	 * @access public
832
	 */
833
	public function first_start()
834
	{
835
		if ($this->config['ppde_first_start'])
836
		{
837
			$this->set_curl_info();
838
			$this->set_remote_detected();
839
			$this->config['ppde_first_start'] = false;
840
		}
841
	}
842
843
	/**
844
	 * Check if cURL is available
845
	 *
846
	 * @param bool $check_version
847
	 *
848
	 * @return array|bool
849
	 * @access public
850
	 */
851
	public function check_curl($check_version = false)
852
	{
853
		if (function_exists('curl_version') && $check_version)
854
		{
855
			return curl_version();
856
		}
857
858
		if (function_exists('curl_init') && function_exists('curl_exec'))
859
		{
860
			$this->get_ext_meta();
861
862
			$ch = curl_init($this->ext_meta['extra']['version-check']['host']);
863
864
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
865
866
			$response = curl_exec($ch);
867
			$response_status = strval(curl_getinfo($ch, CURLINFO_HTTP_CODE));
868
869
			curl_close($ch);
870
871
			return ($response !== false || $response_status !== '0') ? true : false;
872
		}
873
874
		return false;
875
	}
876
877
	/**
878
	 * Set config value for cURL version
879
	 *
880
	 * @return null
881
	 * @access public
882
	 */
883
	public function set_curl_info()
884
	{
885
		// Get cURL version informations
886
		if ($curl_info = $this->check_curl(true))
887
		{
888
			$this->config->set('ppde_curl_version', $curl_info['version']);
889
			$this->config->set('ppde_curl_ssl_version', $curl_info['ssl_version']);
890
		}
891
	}
892
893
	/**
894
	 * Set config value for cURL and fsockopen
895
	 *
896
	 * @return null
897
	 * @access public
898
	 */
899
	public function set_remote_detected()
900
	{
901
		$this->config->set('ppde_curl_detected', $this->check_curl());
902
		$this->config->set('ppde_fsock_detected', $this->check_fsockopen());
903
	}
904
905
	/**
906
	 * Get extension metadata
907
	 *
908
	 * @return null
909
	 * @access protected
910
	 */
911
	protected function get_ext_meta()
912
	{
913
		if (empty($this->ext_meta))
914
		{
915
			$this->load_metadata();
916
		}
917
	}
918
919
	/**
920
	 * Load metadata for this extension
921
	 *
922
	 * @return array
923
	 * @access public
924
	 */
925
	public function load_metadata()
926
	{
927
		// Retrieve the extension name based on the namespace of this file
928
		$this->retrieve_ext_name();
929
930
		// If they've specified an extension, let's load the metadata manager and validate it.
931
		if ($this->ext_name)
932
		{
933
			$md_manager = new \phpbb\extension\metadata_manager($this->ext_name, $this->config, $this->extension_manager, $this->template, $this->user, $this->root_path);
934
935
			try
936
			{
937
				$this->ext_meta = $md_manager->get_metadata('all');
938
			}
939
			catch (\phpbb\extension\exception $e)
940
			{
941
				trigger_error($e, E_USER_WARNING);
942
			}
943
		}
944
945
		return $this->ext_meta;
946
	}
947
948
	/**
949
	 * Retrieve the extension name
950
	 *
951
	 * @return null
952
	 * @access protected
953
	 */
954
	protected function retrieve_ext_name()
955
	{
956
		$namespace_ary = explode('\\', __NAMESPACE__);
957
		$this->ext_name = $namespace_ary[0] . '/' . $namespace_ary[1];
958
	}
959
960
	/**
961
	 * Check if fsockopen is available
962
	 *
963
	 * @return bool
964
	 * @access public
965
	 */
966
	public function check_fsockopen()
967
	{
968
		if (function_exists('fsockopen'))
969
		{
970
			$this->get_ext_meta();
971
972
			$url = parse_url($this->ext_meta['extra']['version-check']['host']);
973
974
			$fp = @fsockopen($url['path'], 80);
975
976
			return ($fp !== false) ? true : false;
977
		}
978
979
		return false;
980
	}
981
}
982