forked from pkp/orcidProfile
-
Notifications
You must be signed in to change notification settings - Fork 1
/
OrcidProfilePlugin.inc.php
418 lines (369 loc) · 12.5 KB
/
OrcidProfilePlugin.inc.php
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
<?php
/**
* @file plugins/generic/orcidProfile/OrcidProfilePlugin.inc.php
*
* Copyright (c) 2015-2018 University of Pittsburgh
* Copyright (c) 2014-2018 Simon Fraser University
* Copyright (c) 2003-2018 John Willinsky
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
*
* @class OrcidProfilePlugin
* @ingroup plugins_generic_orcidProfile
*
* @brief ORCID Profile plugin class
*/
import('lib.pkp.classes.plugins.GenericPlugin');
define('ORCID_OAUTH_URL', 'https://orcid.org/oauth/');
define('ORCID_OAUTH_URL_SANDBOX', 'https://sandbox.orcid.org/oauth/');
define('ORCID_API_URL_PUBLIC', 'https://pub.orcid.org/');
define('ORCID_API_URL_PUBLIC_SANDBOX', 'https://pub.sandbox.orcid.org/');
define('ORCID_API_URL_MEMBER', 'https://api.orcid.org/');
define('ORCID_API_URL_MEMBER_SANDBOX', 'https://api.sandbox.orcid.org/');
define('OAUTH_TOKEN_URL', 'oauth/token');
define('ORCID_API_VERSION_URL', 'v2.0/');
define('ORCID_PROFILE_URL', 'person');
define('ORCID_EMAIL_URL', 'email');
class OrcidProfilePlugin extends GenericPlugin {
/**
* @copydoc Plugin::register()
*/
function register($category, $path, $mainContextId = null) {
$success = parent::register($category, $path, $mainContextId);
if (!Config::getVar('general', 'installed') || defined('RUNNING_UPGRADE')) return true;
if ($success && $this->getEnabled($mainContextId)) {
// Register callback for Smarty filters; add CSS
HookRegistry::register('TemplateManager::display', array($this, 'handleTemplateDisplay'));
// Insert ORCID callback
HookRegistry::register('LoadHandler', array($this, 'setupCallbackHandler'));
// Handle ORCID on user registration
HookRegistry::register('registrationform::execute', array($this, 'collectUserOrcidId'));
// Send emails to authors without ORCID id upon submission
HookRegistry::register('Author::Form::Submit::AuthorSubmitStep3Form::Execute', array($this, 'collectAuthorOrcidId'));
// Add ORCiD hash to author DAO
HookRegistry::register('authordao::getAdditionalFieldNames', array($this, 'authorSubmitGetFieldNames'));
}
return $success;
}
/**
* Get page handler path for this plugin.
* @return string Path to plugin's page handler
*/
function getHandlerPath() {
return $this->getPluginPath() . DIRECTORY_SEPARATOR . 'pages';
}
/**
* Hook callback: register pages for each sushi-lite method
* This URL is of the form: orcidapi/{$orcidrequest}
* @see PKPPageRouter::route()
*/
function setupCallbackHandler($hookName, $params) {
$page = $params[0];
if ($this->getEnabled() && $page == 'orcidapi') {
$this->import('pages/OrcidHandler');
define('HANDLER_CLASS', 'OrcidHandler');
return true;
}
return false;
}
/**
* Hook callback: register output filter to add data citation to submission
* summaries; add data citation to reading tools' suppfiles and metadata views.
* @see TemplateManager::display()
*/
function handleTemplateDisplay($hookName, $args) {
$templateMgr =& $args[0];
$template =& $args[1];
$request = PKPApplication::getRequest();
// Assign our private stylesheet, for front and back ends.
$templateMgr->addStyleSheet(
'orcidProfile',
$request->getBaseUrl() . '/' . $this->getStyleSheet(),
array(
'contexts' => array('frontend', 'backend')
)
);
switch ($template) {
case 'frontend/pages/userRegister.tpl':
$templateMgr->register_outputfilter(array($this, 'registrationFilter'));
break;
case 'frontend/pages/article.tpl':
$templateMgr->assign('orcidIcon', $this->getIcon());
break;
case 'user/publicProfileForm.tpl':
$templateMgr->register_outputfilter(array($this, 'profileFilter'));
break;
}
return false;
}
/**
* Return the OAUTH path (prod or sandbox) based on the current API configuration
* @return $string
*/
function getOauthPath() {
$context = Request::getContext();
$contextId = ($context == null) ? 0 : $context->getId();
$apiPath = $this->getSetting($contextId, 'orcidProfileAPIPath');
if ($apiPath == ORCID_API_URL_PUBLIC || $apiPath == ORCID_API_URL_MEMBER) {
return ORCID_OAUTH_URL;
} else {
return ORCID_OAUTH_URL_SANDBOX;
}
}
/**
* Output filter adds ORCiD interaction to registration form.
* @param $output string
* @param $templateMgr TemplateManager
* @return $string
*/
function registrationFilter($output, &$templateMgr) {
if (preg_match('/<form[^>]+id="register"[^>]+>/', $output, $matches, PREG_OFFSET_CAPTURE)) {
$match = $matches[0][0];
$offset = $matches[0][1];
$context = Request::getContext();
$contextId = ($context == null) ? 0 : $context->getId();
$templateMgr->assign(array(
'targetOp' => 'register',
'orcidProfileOauthPath' => $this->getOauthPath(),
'orcidClientId' => $this->getSetting($contextId, 'orcidClientId'),
'orcidIcon' => $this->getIcon(),
));
$newOutput = substr($output, 0, $offset+strlen($match));
$newOutput .= $templateMgr->fetch($this->getTemplatePath() . 'orcidProfile.tpl');
$newOutput .= substr($output, $offset+strlen($match));
$output = $newOutput;
}
$templateMgr->unregister_outputfilter('registrationFilter');
return $output;
}
/**
* Output filter adds ORCiD interaction to user profile form.
* @param $output string
* @param $templateMgr TemplateManager
* @return $string
*/
function profileFilter($output, &$templateMgr) {
if (preg_match('/<label[^>]+for="orcid[^"]*"[^>]*>[^<]+<\/label>/', $output, $matches, PREG_OFFSET_CAPTURE) &&
!(preg_match('/\$\(\'input\[name=orcid\]\'\)/', $output))) {
$match = $matches[0][0];
$offset = $matches[0][1];
$context = Request::getContext();
$contextId = ($context == null) ? 0 : $context->getId();
// Entering the registration without ORCiD; present the button.
$templateMgr->assign(array(
'targetOp' => 'profile',
'orcidProfileOauthPath' => $this->getOauthPath(),
'orcidClientId' => $this->getSetting($contextId, 'orcidClientId'),
'orcidIcon' => $this->getIcon(),
));
$newOutput = substr($output, 0, $offset+strlen($match));
$newOutput .= $templateMgr->fetch($this->getTemplatePath() . 'orcidProfile.tpl');
$newOutput .= '<script type="text/javascript">
$(document).ready(function() {
$(\'input[name=orcid]\').attr(\'readonly\', "true");
});
</script>';
$newOutput .= substr($output, $offset+strlen($match));
$output = $newOutput;
}
$templateMgr->unregister_outputfilter('profileFilter');
return $output;
}
/**
* Collect the ORCID when registering a user.
* @param $hookName string
* @param $params array
* @return bool
*/
function collectUserOrcidId($hookName, $params) {
$form = $params[0];
$user =& $params[1];
$form->readUserVars(array('orcid'));
$user->setOrcid($form->getData('orcid'));
return false;
}
/**
* Output filter adds ORCiD interaction to the 3rd step submission form.
* @param $output string
* @param $templateMgr TemplateManager
* @return bool
*/
function collectAuthorOrcidId($hookName, $params) {
$author =& $params[0];
$formAuthor =& $params[1];
// if author has no orcid id
if (!$author->getData('orcid')){
$mail = $this->getMailTemplate('ORCID_COLLECT_AUTHOR_ID');
$orcidToken = md5(time());
$author->setData('orcidToken', $orcidToken);
$request = PKPApplication::getRequest();
$context = $request->getContext();
// This should only ever happen within a context, never site-wide.
assert($context != null);
$contextId = $context->getId();
$articleDao = DAORegistry::getDAO('ArticleDAO');
$article = $articleDao->getArticle($author->getSubmissionId());
$mail->assignParams(array(
'authorOrcidUrl' => $this->getOauthPath() . 'authorize?' . http_build_query(array(
'client_id' => $this->getSetting($contextId, 'orcidClientId'),
'response_type' => 'code',
'scope' => '/authenticate',
'redirect_uri' => Request::url(null, 'orcidapi', 'orcidVerify', null, array('orcidToken' => $orcidToken, 'articleId' => $author->getSubmissionId()))
)),
'authorName' => $author->getFullName(),
'editorialContactSignature' => $context->getSetting('contactName'),
'articleTitle' => $article->getLocalizedTitle(),
));
// Send to author
$mail->addRecipient($author->getEmail(), $author->getFullName());
// Send the mail.
$mail->send($request);
}
return false;
}
/**
* Add the author hash storage to the author record
* @param $hookName string
* @param $params array
*/
function authorSubmitGetFieldNames($hookName, $params) {
$fields =& $params[1];
$fields[] = 'orcidToken';
return false;
}
/**
* @copydoc Plugin::getDisplayName()
*/
function getDisplayName() {
return __('plugins.generic.orcidProfile.displayName');
}
/**
* @copydoc Plugin::getDescription()
*/
function getDescription() {
return __('plugins.generic.orcidProfile.description');
}
/**
* @see PKPPlugin::getInstallEmailTemplatesFile()
*/
function getInstallEmailTemplatesFile() {
return ($this->getPluginPath() . '/emailTemplates.xml');
}
/**
* @see PKPPlugin::getInstallEmailTemplateDataFile()
*/
function getInstallEmailTemplateDataFile() {
return ($this->getPluginPath() . '/locale/{$installedLocale}/emailTemplates.xml');
}
/**
* Extend the {url ...} smarty to support this plugin.
*/
function smartyPluginUrl($params, &$smarty) {
$path = array($this->getCategory(), $this->getName());
if (is_array($params['path'])) {
$params['path'] = array_merge($path, $params['path']);
} elseif (!empty($params['path'])) {
$params['path'] = array_merge($path, array($params['path']));
} else {
$params['path'] = $path;
}
if (!empty($params['id'])) {
$params['path'] = array_merge($params['path'], array($params['id']));
unset($params['id']);
}
return $smarty->smartyUrl($params, $smarty);
}
/**
* @see Plugin::getActions()
*/
function getActions($request, $actionArgs) {
$router = $request->getRouter();
import('lib.pkp.classes.linkAction.request.AjaxModal');
return array_merge(
$this->getEnabled()?array(
new LinkAction(
'settings',
new AjaxModal(
$router->url(
$request,
null,
null,
'manage',
null,
array(
'verb' => 'settings',
'plugin' => $this->getName(),
'category' => 'generic'
)
),
$this->getDisplayName()
),
__('manager.plugins.settings'),
null
),
):array(),
parent::getActions($request, $actionArgs)
);
}
/**
* @see Plugin::manage()
*/
function manage($args, $request) {
switch ($request->getUserVar('verb')) {
case 'settings':
$context = $request->getContext();
$contextId = ($context == null) ? 0 : $context->getId();
$templateMgr = TemplateManager::getManager();
$templateMgr->register_function('plugin_url', array($this, 'smartyPluginUrl'));
$apiOptions = array(
ORCID_API_URL_PUBLIC => 'plugins.generic.orcidProfile.manager.settings.orcidProfileAPIPath.public',
ORCID_API_URL_PUBLIC_SANDBOX => 'plugins.generic.orcidProfile.manager.settings.orcidProfileAPIPath.publicSandbox',
ORCID_API_URL_MEMBER => 'plugins.generic.orcidProfile.manager.settings.orcidProfileAPIPath.member',
ORCID_API_URL_MEMBER_SANDBOX => 'plugins.generic.orcidProfile.manager.settings.orcidProfileAPIPath.memberSandbox'
);
$templateMgr->assign('orcidApiUrls', $apiOptions);
$this->import('OrcidProfileSettingsForm');
$form = new OrcidProfileSettingsForm($this, $contextId);
if ($request->getUserVar('save')) {
$form->readInputData();
if ($form->validate()) {
$form->execute();
return new JSONMessage(true);
}
} else {
$form->initData();
}
return new JSONMessage(true, $form->fetch($request));
}
return parent::manage($args, $request);
}
/**
* Return the location of the plugin's CSS file
* @return string
*/
function getStyleSheet() {
return $this->getPluginPath() . '/css/orcidProfile.css';
}
/**
* Return a string of the ORCiD SVG icon
* @return string
*/
function getIcon() {
$path = Core::getBaseDir() . '/' . $this->getPluginPath() . '/templates/images/orcid.svg';
return file_exists($path) ? file_get_contents($path) : '';
}
/**
* Instantiate a MailTemplate
*
* @param $emailKey string
* @param $context Context
*/
function &getMailTemplate($emailKey, $context = null) {
if (!isset($this->_mailTemplates[$emailKey])) {
import('classes.mail.MailTemplate');
$mailTemplate = new MailTemplate($emailKey, null, null, $context, true, true);
$this->_mailTemplates[$emailKey] = $mailTemplate;
}
return $this->_mailTemplates[$emailKey];
}
}
?>