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 skouat\ppde\controller\ipn_paypal; |
14
|
|
|
|
15
|
|
|
abstract class admin_main |
16
|
|
|
{ |
17
|
|
|
/** @var array */ |
18
|
|
|
protected $args = []; |
19
|
|
|
/** @var object \phpbb\config\config */ |
20
|
|
|
protected $config; |
21
|
|
|
/** @var object Symfony\Component\DependencyInjection\ContainerInterface */ |
22
|
|
|
protected $container; |
23
|
|
|
/** @var string */ |
24
|
|
|
protected $id_prefix_name; |
25
|
|
|
/** @var string */ |
26
|
|
|
protected $lang_key_prefix; |
27
|
|
|
/** @var \phpbb\language\language */ |
28
|
|
|
protected $language; |
29
|
|
|
/** @var \phpbb\log\log */ |
30
|
|
|
protected $log; |
31
|
|
|
/** @var string */ |
32
|
|
|
protected $module_name; |
33
|
|
|
/** @var \skouat\ppde\actions\locale_icu */ |
34
|
|
|
protected $ppde_actions_locale; |
35
|
|
|
/** @var ipn_paypal */ |
36
|
|
|
protected $ppde_ipn_paypal; |
37
|
|
|
/** @var bool */ |
38
|
|
|
protected $preview; |
39
|
|
|
/** @var \phpbb\request\request */ |
40
|
|
|
protected $request; |
41
|
|
|
/** @var bool */ |
42
|
|
|
protected $submit; |
43
|
|
|
/** @var \phpbb\template\template */ |
44
|
|
|
protected $template; |
45
|
|
|
/** @var \phpbb\user */ |
46
|
|
|
protected $user; |
47
|
|
|
|
48
|
|
|
/** @var string */ |
49
|
|
|
public $u_action; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Constructor |
53
|
|
|
* |
54
|
|
|
* @param string $lang_key_prefix Prefix for the messages thrown by exceptions |
55
|
|
|
* @param string $id_prefix_name Prefix name for identifier in the URL |
56
|
|
|
* @param string $module_name Name of the module currently used |
57
|
|
|
* |
58
|
|
|
* @access public |
59
|
|
|
*/ |
60
|
|
|
public function __construct($module_name, $lang_key_prefix, $id_prefix_name) |
61
|
|
|
{ |
62
|
|
|
$this->module_name = $module_name; |
63
|
|
|
$this->lang_key_prefix = $lang_key_prefix; |
64
|
|
|
$this->id_prefix_name = $id_prefix_name; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Set page url |
69
|
|
|
* |
70
|
|
|
* @param string $u_action Custom form action |
71
|
|
|
* |
72
|
|
|
* @return void |
73
|
|
|
* @access public |
74
|
|
|
*/ |
75
|
|
|
public function set_page_url($u_action): void |
76
|
|
|
{ |
77
|
|
|
$this->u_action = $u_action; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Gets vars from POST then build a array of them |
82
|
|
|
* |
83
|
|
|
* @param string $id Module id |
84
|
|
|
* @param string $mode Module categorie |
85
|
|
|
* @param string $action Action name |
86
|
|
|
* |
87
|
|
|
* @return void |
88
|
|
|
* @access private |
89
|
|
|
*/ |
90
|
|
|
public function set_hidden_fields($id, $mode, $action): void |
91
|
|
|
{ |
92
|
|
|
$this->args = array_merge($this->args, [ |
93
|
|
|
'i' => $id, |
94
|
|
|
'mode' => $mode, |
95
|
|
|
'action' => $action, |
96
|
|
|
'hidden_fields' => [], |
97
|
|
|
]); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function get_hidden_fields(): array |
101
|
|
|
{ |
102
|
|
|
return count($this->args) ? array_merge( |
103
|
|
|
['i' => $this->args['i'], |
104
|
|
|
'mode' => $this->args['mode'], |
105
|
|
|
'action' => $this->args['action'], |
106
|
|
|
$this->id_prefix_name . '_id' => $this->args[$this->id_prefix_name . '_id']], |
107
|
|
|
$this->args['hidden_fields']) : ['id' => '', 'mode' => '', 'action' => '']; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function set_action($action): void |
111
|
|
|
{ |
112
|
|
|
$this->args['action'] = $action; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function get_action(): string |
116
|
|
|
{ |
117
|
|
|
return (string) ($this->args['action'] ?? ''); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function set_item_id($item_id): void |
121
|
|
|
{ |
122
|
|
|
$this->args[$this->id_prefix_name . '_id'] = (int) $item_id; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Display items of the called controller |
127
|
|
|
* |
128
|
|
|
* @return void |
129
|
|
|
* @access public |
130
|
|
|
*/ |
131
|
|
|
public function display(): void |
132
|
|
|
{ |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Add item for the called controller |
137
|
|
|
* |
138
|
|
|
* @return void |
139
|
|
|
* @access public |
140
|
|
|
*/ |
141
|
|
|
public function add(): void |
142
|
|
|
{ |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function approve(): void |
146
|
|
|
{ |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Change item details for the called controller |
151
|
|
|
* |
152
|
|
|
* @return void |
153
|
|
|
* @access public |
154
|
|
|
*/ |
155
|
|
|
public function change(): void |
156
|
|
|
{ |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Delete item for the called controller |
161
|
|
|
* |
162
|
|
|
* @return void |
163
|
|
|
* @access public |
164
|
|
|
*/ |
165
|
|
|
public function delete(): void |
166
|
|
|
{ |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Edit item on the called controller |
171
|
|
|
* |
172
|
|
|
* @return void |
173
|
|
|
* @access public |
174
|
|
|
*/ |
175
|
|
|
public function edit(): void |
176
|
|
|
{ |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Enable/disable item on the called controller |
181
|
|
|
* |
182
|
|
|
* @return void |
183
|
|
|
* @access public |
184
|
|
|
*/ |
185
|
|
|
public function enable(): void |
186
|
|
|
{ |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Move up/down an item on the called controller |
191
|
|
|
* |
192
|
|
|
* @return void |
193
|
|
|
* @access public |
194
|
|
|
*/ |
195
|
|
|
public function move(): void |
196
|
|
|
{ |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* View a selected item on the called controller |
201
|
|
|
* |
202
|
|
|
* @return void |
203
|
|
|
* @access public |
204
|
|
|
*/ |
205
|
|
|
public function view(): void |
206
|
|
|
{ |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Build pull down menu options of available remote URI |
211
|
|
|
* |
212
|
|
|
* @param int $default ID of the selected value. |
213
|
|
|
* @param string $type Can be 'live' or 'sandbox' |
214
|
|
|
* |
215
|
|
|
* @return void |
216
|
|
|
* @access public |
217
|
|
|
*/ |
218
|
|
|
public function build_remote_uri_select_menu($default, $type): void |
219
|
|
|
{ |
220
|
|
|
$type = $this->force_type($type); |
221
|
|
|
|
222
|
|
|
// Grab the list of remote uri for selected type |
223
|
|
|
$remote_list = ipn_paypal::get_remote_uri(); |
224
|
|
|
|
225
|
|
|
// Process each menu item for pull-down |
226
|
|
|
foreach ($remote_list as $id => $remote) |
227
|
|
|
{ |
228
|
|
|
if ($remote['type'] !== $type) |
229
|
|
|
{ |
230
|
|
|
continue; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
// Set output block vars for display in the template |
234
|
|
|
$this->template->assign_block_vars('remote_options', [ |
235
|
|
|
'REMOTE_ID' => (int) $id, |
236
|
|
|
'REMOTE_NAME' => $remote['hostname'], |
237
|
|
|
'S_DEFAULT' => (int) $default === (int) $id, |
238
|
|
|
]); |
239
|
|
|
} |
240
|
|
|
unset ($remote_list, $id); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Enforce the type of remote provided |
245
|
|
|
* |
246
|
|
|
* @param string $type |
247
|
|
|
* |
248
|
|
|
* @return string |
249
|
|
|
* @access private |
250
|
|
|
*/ |
251
|
|
|
private function force_type($type): string |
252
|
|
|
{ |
253
|
|
|
return $type === 'live' || $type === 'sandbox' ? (string) $type : 'live'; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* The form submitting if 'submit' is true |
258
|
|
|
* |
259
|
|
|
* @return void |
260
|
|
|
* @access protected |
261
|
|
|
*/ |
262
|
|
|
protected function submit_settings(): void |
263
|
|
|
{ |
264
|
|
|
$this->submit = $this->request->is_set_post('submit'); |
265
|
|
|
|
266
|
|
|
// Test if the submitted form is valid |
267
|
|
|
$errors = $this->is_invalid_form('ppde_' . $this->module_name, $this->submit); |
268
|
|
|
|
269
|
|
|
if ($this->can_submit_data($errors)) |
270
|
|
|
{ |
271
|
|
|
// Set the options the user configured |
272
|
|
|
$this->set_settings(); |
273
|
|
|
|
274
|
|
|
// Add option settings change action to the admin log |
275
|
|
|
$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_' . $this->lang_key_prefix . '_UPDATED'); |
276
|
|
|
|
277
|
|
|
// Option settings have been updated and logged |
278
|
|
|
// Confirm this to the user and provide link back to previous page |
279
|
|
|
trigger_error($this->language->lang($this->lang_key_prefix . '_SAVED') . adm_back_link($this->u_action)); |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Check if form is valid or not |
285
|
|
|
* |
286
|
|
|
* @param string $form_name |
287
|
|
|
* @param bool $submit_or_preview |
288
|
|
|
* |
289
|
|
|
* @return array |
290
|
|
|
* @access protected |
291
|
|
|
*/ |
292
|
|
|
protected function is_invalid_form($form_name, $submit_or_preview = false): array |
293
|
|
|
{ |
294
|
|
|
if ($submit_or_preview && !check_form_key($form_name)) |
295
|
|
|
{ |
296
|
|
|
return [$this->language->lang('FORM_INVALID')]; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
return []; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* @param array $errors |
304
|
|
|
* |
305
|
|
|
* @return bool |
306
|
|
|
* @access protected |
307
|
|
|
*/ |
308
|
|
|
protected function can_submit_data(array $errors): bool |
309
|
|
|
{ |
310
|
|
|
return $this->submit && empty($errors) && !$this->preview; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* Set the options for called controller |
315
|
|
|
* |
316
|
|
|
* @return void |
317
|
|
|
* @access protected |
318
|
|
|
*/ |
319
|
|
|
protected function set_settings(): void |
320
|
|
|
{ |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* Trigger error message if data already exists |
325
|
|
|
* |
326
|
|
|
* @param \skouat\ppde\entity\main $entity The entity object |
327
|
|
|
* |
328
|
|
|
* @return void |
329
|
|
|
* @access protected |
330
|
|
|
*/ |
331
|
|
|
protected function trigger_error_data_already_exists(\skouat\ppde\entity\main $entity): void |
332
|
|
|
{ |
333
|
|
|
if ($this->is_added_data_exists($entity)) |
334
|
|
|
{ |
335
|
|
|
// Show user warning for an already exist page and provide link back to the edit page |
336
|
|
|
$message = $this->language->lang($this->lang_key_prefix . '_EXISTS'); |
337
|
|
|
$message .= '<br><br>'; |
338
|
|
|
$message .= $this->language->lang($this->lang_key_prefix . '_GO_TO_PAGE', '<a href="' . $this->u_action . '&action=edit&' . $this->id_prefix_name . '_id=' . $entity->get_id() . '">» ', '</a>'); |
339
|
|
|
trigger_error($message . adm_back_link($this->u_action), E_USER_WARNING); |
340
|
|
|
} |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* @param \skouat\ppde\entity\main $entity The entity object |
345
|
|
|
* |
346
|
|
|
* @return bool |
347
|
|
|
* @access protected |
348
|
|
|
*/ |
349
|
|
|
protected function is_added_data_exists(\skouat\ppde\entity\main $entity): bool |
350
|
|
|
{ |
351
|
|
|
return $entity->data_exists($entity->build_sql_data_exists()) && $this->request->variable('action', '') === 'add'; |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* Check some settings before submitting data |
356
|
|
|
* |
357
|
|
|
* @param \skouat\ppde\entity\main $entity The entity object |
358
|
|
|
* @param string $field_name Name of the entity function to call |
359
|
|
|
* @param string|int $value_cmp Default value to compare with the return value of the called function |
360
|
|
|
* @param bool $submit_or_preview Form submit or preview status |
361
|
|
|
* |
362
|
|
|
* @return array $errors |
363
|
|
|
* @access protected |
364
|
|
|
*/ |
365
|
|
|
protected function is_empty_data(\skouat\ppde\entity\main $entity, $field_name, $value_cmp, $submit_or_preview = false): array |
366
|
|
|
{ |
367
|
|
|
$errors = []; |
368
|
|
|
|
369
|
|
|
if ($submit_or_preview && $entity->{'get_' . $field_name}() == $value_cmp) |
370
|
|
|
{ |
371
|
|
|
$errors[] = $this->language->lang($this->lang_key_prefix . '_EMPTY_' . strtoupper($field_name)); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
return $errors; |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* Get result of submit and preview expression |
379
|
|
|
* |
380
|
|
|
* @param bool $submit |
381
|
|
|
* @param bool $preview |
382
|
|
|
* |
383
|
|
|
* @return bool |
384
|
|
|
* @access protected |
385
|
|
|
*/ |
386
|
|
|
protected function submit_or_preview($submit = false, $preview = false): bool |
387
|
|
|
{ |
388
|
|
|
return $submit || $preview; |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* Show user a result message if AJAX was used |
393
|
|
|
* |
394
|
|
|
* @param string $message Text message to show to the user |
395
|
|
|
* |
396
|
|
|
* @return void |
397
|
|
|
* @access protected |
398
|
|
|
*/ |
399
|
|
|
protected function ajax_delete_result_message($message = ''): void |
400
|
|
|
{ |
401
|
|
|
if ($this->request->is_ajax()) |
402
|
|
|
{ |
403
|
|
|
$json_response = new \phpbb\json_response; |
404
|
|
|
$json_response->send([ |
405
|
|
|
'MESSAGE_TITLE' => $this->language->lang('INFORMATION'), |
406
|
|
|
'MESSAGE_TEXT' => $message, |
407
|
|
|
'REFRESH_DATA' => [ |
408
|
|
|
'time' => 3, |
409
|
|
|
], |
410
|
|
|
]); |
411
|
|
|
} |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
/** |
415
|
|
|
* Set u_action output vars for display in the template |
416
|
|
|
* |
417
|
|
|
* @return void |
418
|
|
|
* @access protected |
419
|
|
|
*/ |
420
|
|
|
protected function u_action_assign_template_vars(): void |
421
|
|
|
{ |
422
|
|
|
$this->template->assign_vars([ |
423
|
|
|
'U_ACTION' => $this->u_action, |
424
|
|
|
]); |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
/** |
428
|
|
|
* Set add/edit action output vars for display in the template |
429
|
|
|
* |
430
|
|
|
* @param string $type Action type: 'add' or 'edit' |
431
|
|
|
* @param integer $id Identifier to Edit. If action = add, then let to '0'. |
432
|
|
|
* |
433
|
|
|
* @return void |
434
|
|
|
* @access protected |
435
|
|
|
*/ |
436
|
|
|
protected function add_edit_action_assign_template_vars($type, $id = 0): void |
437
|
|
|
{ |
438
|
|
|
$id_action = !empty($id) ? '&' . $this->id_prefix_name . '_id=' . (int) $id : ''; |
439
|
|
|
|
440
|
|
|
$this->template->assign_vars([ |
441
|
|
|
'S_ADD_EDIT' => true, |
442
|
|
|
'U_ACTION' => $this->u_action . '&action=' . $type . $id_action, |
443
|
|
|
'U_BACK' => $this->u_action, |
444
|
|
|
]); |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
/** |
448
|
|
|
* Set error output vars for display in the template |
449
|
|
|
* |
450
|
|
|
* @param array $errors |
451
|
|
|
* |
452
|
|
|
* @return void |
453
|
|
|
* @access protected |
454
|
|
|
*/ |
455
|
|
|
protected function s_error_assign_template_vars($errors): void |
456
|
|
|
{ |
457
|
|
|
$this->template->assign_vars([ |
458
|
|
|
'S_ERROR' => (bool) count($errors), |
459
|
|
|
'ERROR_MSG' => (count($errors)) ? implode('<br>', $errors) : '', |
460
|
|
|
]); |
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
/** |
464
|
|
|
* Check if a config value is true |
465
|
|
|
* |
466
|
|
|
* @param mixed $config Config value |
467
|
|
|
* @param string $type (see settype()) |
468
|
|
|
* @param mixed $default |
469
|
|
|
* |
470
|
|
|
* @return mixed |
471
|
|
|
* @access protected |
472
|
|
|
*/ |
473
|
|
|
protected function check_config($config, $type = 'boolean', $default = '') |
474
|
|
|
{ |
475
|
|
|
// We're using settype to enforce data types |
476
|
|
|
settype($config, $type); |
477
|
|
|
settype($default, $type); |
478
|
|
|
|
479
|
|
|
return $config ?: $default; |
480
|
|
|
} |
481
|
|
|
|
482
|
|
|
/** |
483
|
|
|
* Check if settings is required |
484
|
|
|
* |
485
|
|
|
* @param $settings |
486
|
|
|
* @param $depend_on |
487
|
|
|
* |
488
|
|
|
* @return mixed |
489
|
|
|
* @access protected |
490
|
|
|
*/ |
491
|
|
|
protected function required_settings($settings, $depend_on) |
492
|
|
|
{ |
493
|
|
|
if (empty($settings) && (bool) $depend_on === true) |
494
|
|
|
{ |
495
|
|
|
trigger_error($this->language->lang($this->lang_key_prefix . '_MISSING') . adm_back_link($this->u_action), E_USER_WARNING); |
496
|
|
|
} |
497
|
|
|
|
498
|
|
|
return $settings; |
499
|
|
|
} |
500
|
|
|
|
501
|
|
|
/** |
502
|
|
|
* Run system checks if config 'ppde_first_start' is true |
503
|
|
|
* |
504
|
|
|
* @return void |
505
|
|
|
* @throws \ReflectionException |
506
|
|
|
* @access protected |
507
|
|
|
*/ |
508
|
|
|
protected function ppde_first_start(): void |
509
|
|
|
{ |
510
|
|
|
if ($this->config['ppde_first_start']) |
511
|
|
|
{ |
512
|
|
|
$this->ppde_ipn_paypal->set_curl_info(); |
513
|
|
|
$this->ppde_ipn_paypal->set_remote_detected(); |
514
|
|
|
$this->ppde_ipn_paypal->check_tls(); |
515
|
|
|
$this->ppde_actions_locale->set_intl_info(); |
516
|
|
|
$this->ppde_actions_locale->set_intl_detected(); |
517
|
|
|
$this->config->set('ppde_first_start', '0'); |
518
|
|
|
} |
519
|
|
|
} |
520
|
|
|
} |
521
|
|
|
|