Repository navigation
Expand file tree
/
Copy pathScieloScreeningSettingsForm.php
More file actions
165 lines (142 loc) · 5.98 KB
/
Copy pathScieloScreeningSettingsForm.php
File metadata and controls
165 lines (142 loc) · 5.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/**
* @file ScieloScreeningSettingsForm.inc.php
*
* Copyright (c) 2024 SciELO
* Copyright (c) 2024 Lepidus Tecnologia
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class ScieloScreeningSettingsForm
* @ingroup plugins_generic_scieloScreening
*
* @brief Form for site admins to modify SciELO Screening plugin settings
*/
namespace APP\plugins\generic\scieloScreening;
use PKP\form\Form;
use APP\template\TemplateManager;
use APP\core\Application;
use PKP\config\Config;
use PKP\form\validation\FormValidatorPost;
use PKP\form\validation\FormValidatorCSRF;
use PKP\form\validation\FormValidatorCustom;
use APP\plugins\generic\scieloScreening\classes\APIKeyEncryption;
use APP\plugins\generic\scieloScreening\classes\OrcidClient;
use APP\plugins\generic\scieloScreening\classes\DocumentChecker;
class ScieloScreeningSettingsForm extends Form
{
public const CONFIG_VARS = array(
'orcidAPIPath' => 'string',
'orcidClientId' => 'string',
'orcidClientSecret' => 'string',
);
public $contextId;
public $plugin;
private $encrypter;
public function __construct($plugin, $contextId)
{
$this->contextId = $contextId;
$this->plugin = $plugin;
$this->encrypter = new APIKeyEncryption();
$template = $this->encrypter->secretConfigExists() ? 'settingsForm.tpl' : 'settingsFormEmptySecret.tpl';
parent::__construct($plugin->getTemplateResource($template));
$this->addCheck(new FormValidatorPost($this));
$this->addCheck(new FormValidatorCSRF($this));
if (!$this->orcidIsGloballyConfigured()) {
$this->addCheck(new FormValidatorCustom($this, 'orcidAPIPath', 'required', 'plugins.generic.scieloScreening.settings.orcidAPIPathRequired', [OrcidClient::class, 'isAllowedApiUrl']));
$this->addCheck(new FormValidatorCustom($this, 'orcidClientId', 'required', 'plugins.generic.scieloScreening.settings.orcidClientIdError', function ($clientId) {
return $this->validateClientId($clientId);
}));
$this->addCheck(new FormValidatorCustom($this, 'orcidClientSecret', 'required', 'plugins.generic.scieloScreening.settings.orcidClientSecretError', function ($clientSecret) {
return $this->validateClientSecret($clientSecret);
}));
}
}
public function initData()
{
$contextId = $this->contextId;
$plugin = &$this->plugin;
$this->_data = array();
foreach (self::CONFIG_VARS as $configVar => $type) {
$settingValue = $plugin->getSetting($contextId, $configVar);
if ($configVar == 'orcidClientId' || $configVar == 'orcidClientSecret') {
if (!empty($settingValue) && $this->encrypter->textIsEncrypted($settingValue)) {
$settingValue = $this->encrypter->decryptString($settingValue);
}
}
$this->_data[$configVar] = $settingValue;
}
}
public function readInputData()
{
$this->readUserVars(array_keys(self::CONFIG_VARS));
}
public function fetch($request, $template = null, $display = false)
{
$templateMgr = TemplateManager::getManager($request);
$templateMgr->assign('missingDocumentScreeningRequirements', implode(', ', DocumentChecker::getMissingRequirements()));
$templateMgr->assign('globallyConfigured', $this->orcidIsGloballyConfigured());
$templateMgr->assign('pluginName', $this->plugin->getName());
$templateMgr->assign('applicationName', Application::get()->getName());
return parent::fetch($request, $template, $display);
}
public function execute(...$functionArgs)
{
if ($this->orcidIsGloballyConfigured()) {
return parent::execute(...$functionArgs);
}
if (!OrcidClient::isAllowedApiUrl($this->getData('orcidAPIPath'))) {
throw new \InvalidArgumentException('Invalid ORCID API endpoint');
}
$plugin = &$this->plugin;
$contextId = $this->contextId;
foreach (self::CONFIG_VARS as $configVar => $type) {
if ($configVar === 'orcidAPIPath') {
$plugin->updateSetting($contextId, $configVar, $this->getData($configVar), $type);
} else {
$encryptedValue = $this->encrypter->encryptString($this->getData($configVar));
$plugin->updateSetting($contextId, $configVar, $encryptedValue, $type);
}
}
parent::execute(...$functionArgs);
}
public function _checkPrerequisites()
{
$messages = array();
$clientId = $this->getData('orcidClientId');
if (!$this->validateClientId($clientId)) {
$messages[] = __('plugins.generic.scieloScreening.settings.orcidClientIdError');
}
$clientSecret = $this->getData('orcidClientSecret');
if (!$this->validateClientSecret($clientSecret)) {
$messages[] = __('plugins.generic.scieloScreening.settings.orcidClientSecretError');
}
if (strlen($clientId) == 0 or strlen($clientSecret) == 0) {
$this->plugin->setEnabled(false);
}
return $messages;
}
public function orcidIsGloballyConfigured(): bool
{
$apiUrl = Config::getVar('orcid', 'api_url');
$clientId = Config::getVar('orcid', 'client_id');
$clientSecret = Config::getVar('orcid', 'client_secret');
return isset($apiUrl) && trim($apiUrl) && isset($clientId) && trim($clientId) &&
isset($clientSecret) && trim($clientSecret);
}
private function validateClientId($str): bool
{
$valid = false;
if (preg_match('/^APP-[\da-zA-Z]{16}|(\d{4}-){3,}\d{3}[\dX]/', $str) == 1) {
$valid = true;
}
return $valid;
}
private function validateClientSecret($str): bool
{
$valid = false;
if (preg_match('/^(\d|-|[a-f]){36,64}/', $str) == 1) {
$valid = true;
}
return $valid;
}
}