-
Notifications
You must be signed in to change notification settings - Fork 1
/
OrcidProfilePlugin.inc.php
475 lines (423 loc) · 14.8 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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
<?php
/**
* @file plugins/generic/orcidProfile/OrcidProfilePlugin.inc.php
*
* Copyright (c) 2015-2016 University of Pittsburgh
* Copyright (c) 2014-2016 Simon Fraser University Library
* Copyright (c) 2003-2016 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', 'v1.2/');
define('ORCID_PROFILE_URL', 'orcid-profile');
class OrcidProfilePlugin extends GenericPlugin {
/**
* Called as a plugin is registered to the registry
* @param $category String Name of category plugin was registered to
* @return boolean True iff plugin initialized successfully; if false,
* the plugin will not be registered.
*/
function register($category, $path) {
$success = parent::register($category, $path);
if (!Config::getVar('general', 'installed') || defined('RUNNING_UPGRADE')) return true;
if ($success && $this->getEnabled()) {
// Register callback for Smarty filters; add CSS
HookRegistry::register('TemplateManager::display', array(&$this, 'handleTemplateDisplay'));
// Insert ORCID callback
HookRegistry::register('LoadHandler', array(&$this, 'setupCallbackHandler'));
// 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.
$templateMgr->addStylesheet($request->getBaseUrl() . '/' . $this->getStyleSheet());
switch ($template) {
case 'user/register.tpl':
$templateMgr->register_outputfilter(array(&$this, 'registrationFilter'));
break;
case 'user/profile.tpl':
$templateMgr->register_outputfilter(array(&$this, 'profileFilter'));
break;
case 'author/submit/step3.tpl':
$templateMgr->register_outputfilter(array(&$this, 'submitFilter'));
break;
case 'user/login.tpl':
$templateMgr->register_outputfilter(array(&$this, 'loginFilter'));
break;
}
return false;
}
/**
* Return the OAUTH path (prod or sandbox) based on the current API configuration
* @return $string
*/
function getOauthPath() {
$journal = Request::getJournal();
$apiPath = $this->getSetting($journal->getId(), '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 login form.
* @param $output string
* @param $templateMgr TemplateManager
* @return $string
*/
function loginFilter($output, &$templateMgr) {
$sessionManager = SessionManager::getManager();
$userSession = $sessionManager->getUserSession();
if (preg_match('/<form id="signinForm"[^>]+>/', $output, $matches, PREG_OFFSET_CAPTURE)) {
$match = $matches[0][0];
$offset = $matches[0][1];
$journal = Request::getJournal();
$templateMgr->assign(array(
'targetOp' => 'login',
'orcidProfileOauthPath' => $this->getOauthPath(),
'orcidClientId' => $this->getSetting($journal->getId(), 'orcidClientId'),
));
$newOutput = substr($output, 0, $offset);
$newOutput .= $templateMgr->fetch($this->getTemplatePath() . 'orcidLogin.tpl');
$newOutput .= substr($output, $offset);
$output = $newOutput;
}
$templateMgr->unregister_outputfilter('loginFilter');
return $output;
}
/**
* 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="registerForm"[^>]+>/', $output, $matches, PREG_OFFSET_CAPTURE)) {
$match = $matches[0][0];
$offset = $matches[0][1];
$journal = Request::getJournal();
$templateMgr->assign(array(
'targetOp' => 'register',
'orcidProfileOauthPath' => $this->getOauthPath(),
'orcidClientId' => $this->getSetting($journal->getId(), 'orcidClientId'),
));
$newOutput = substr($output, 0, $offset);
$newOutput .= $templateMgr->fetch($this->getTemplatePath() . 'orcidProfile.tpl');
$newOutput .= substr($output, $offset);
$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('/<input[^>]+id="orcid"[^>]+>/', $output, $matches, PREG_OFFSET_CAPTURE)) {
$match = $matches[0][0];
$offset = $matches[0][1];
$journal = Request::getJournal();
// Entering the registration without ORCiD; present the button.
$templateMgr->assign(array(
'targetOp' => 'profile',
'orcidProfileOauthPath' => $this->getOauthPath(),
'orcidClientId' => $this->getSetting($journal->getId(), 'orcidClientId'),
));
$newOutput = substr($output, 0, $offset+strlen($match));
$newOutput .= $templateMgr->fetch($this->getTemplatePath() . 'orcidProfile.tpl');
$newOutput .= '<script type="text/javascript">
$(document).ready(function() {
$(\'#orcid\').attr(\'readonly\', "true");
});
</script>';
$newOutput .= substr($output, $offset+strlen($match));
$output = $newOutput;
}
$templateMgr->unregister_outputfilter('profileFilter');
return $output;
}
/**
* Output filter adds ORCiD interaction to the 3rd step submission form.
* @param $output string
* @param $templateMgr TemplateManager
* @return $string
*/
function submitFilter($output, &$templateMgr) {
if (preg_match('/<input type="text" class="textField" name="authors\[0\]\[orcid\][^>]+>/', $output, $matches, PREG_OFFSET_CAPTURE)) {
$match = $matches[0][0];
$offset = $matches[0][1];
$journal = Request::getJournal();
// Entering the registration without ORCiD; present the button.
$templateMgr->assign(array(
'targetOp' => 'submit',
'orcidProfileOauthPath' => $this->getOauthPath(),
'orcidClientId' => $this->getSetting($journal->getId(), 'orcidClientId'),
'params' => array('articleId' => Request::getUserVar('articleId')),
));
$newOutput = substr($output, 0, $offset + strlen($match) - 1);
$newOutput .= ' readonly=\'readonly\'><br />';
$newOutput .= $templateMgr->fetch($this->getTemplatePath() . 'orcidProfile.tpl');
$newOutput .= '<button id="remove-orcid-button">Remove ORCID ID</button>
<script>$("#remove-orcid-button").click(function(event) {
event.preventDefault();
$("#authors-0-orcid").val("");
$("#connect-orcid-button").show();
});</script>';
$newOutput .= substr($output, $offset + strlen($match));
$output = $newOutput;
}
$templateMgr->unregister_outputfilter('submitFilter');
return $output;
}
/**
* Output filter adds ORCiD interaction to the 3rd step submission form.
* @param $output string
* @param $templateMgr TemplateManager
* @return $string
*/
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();
$articleDao =& DAORegistry::getDAO('ArticleDAO');
$article =& $articleDao->getArticle($author->getSubmissionId());
$mail->assignParams(array(
'authorOrcidUrl' => $this->getOauthPath() . 'authorize?' . http_build_query(array(
'client_id' => $this->getSetting($context->getId(), '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);
}
/**
* Set the page's breadcrumbs, given the plugin's tree of items
* to append.
* @param $subclass boolean
*/
function setBreadcrumbs($isSubclass = false) {
$templateMgr =& TemplateManager::getManager();
$pageCrumbs = array(
array(
Request::url(null, 'user'),
'navigation.user'
),
array(
Request::url(null, 'manager'),
'user.role.manager'
)
);
if ($isSubclass) {
$pageCrumbs[] = array(
Request::url(null, 'manager', 'plugins'),
'manager.plugins'
);
$pageCrumbs[] = array(
Request::url(null, 'manager', 'plugins', 'generic'),
'plugins.categories.generic'
);
}
$templateMgr->assign('pageHierarchy', $pageCrumbs);
}
/**
* Display verbs for the management interface.
*/
function getManagementVerbs() {
$verbs = array();
if ($this->getEnabled()) {
$verbs[] = array('settings', __('manager.plugins.settings'));
}
return parent::getManagementVerbs($verbs);
}
/**
* Execute a management verb on this plugin
* @param $verb string
* @param $args array
* @param $message string Result status message
* @param $messageParams array Parameters for the message key
* @return boolean
*/
function manage($verb, $args, &$message, &$messageParams) {
$journal =& Request::getJournal();
if (!parent::manage($verb, $args, $message, $messageParams)) {
if ($verb == 'enable' && !$this->getSetting($journal->getId(), 'orcidProfileAPIPath')) {
// default the 1.2 public API if no setting is present
$this->updateSetting($journal->getId(), 'orcidProfileAPIPath', ORCID_API_URL_PUBLIC, 'string');
} else {
return false;
}
}
switch ($verb) {
case 'settings':
$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_by_ref('orcidApiUrls', $apiOptions);
$this->import('OrcidProfileSettingsForm');
$form = new OrcidProfileSettingsForm($this, $journal->getId());
if (Request::getUserVar('save')) {
$form->readInputData();
if ($form->validate()) {
$form->execute();
Request::redirect(null, 'manager', 'plugin');
return false;
} else {
$this->setBreadcrumbs(true);
$form->display();
}
} else {
$this->setBreadcrumbs(true);
$form->initData();
$form->display();
}
return true;
default:
// Unknown management verb
assert(false);
return false;
}
}
/**
* Return the location of the plugin's CSS file
* @return string
*/
function getStyleSheet() {
return $this->getPluginPath() . DIRECTORY_SEPARATOR . 'css' . DIRECTORY_SEPARATOR . 'orcidProfile.css';
}
/**
* Instantiate a MailTemplate
*
* @param $emailKey string
* @param $journal Journal
*/
function &getMailTemplate($emailKey, $journal = null) {
if (!isset($this->_mailTemplates[$emailKey])) {
import('classes.mail.MailTemplate');
$mailTemplate = new MailTemplate($emailKey, null, null, $journal, true, true);
$this->_mailTemplates[$emailKey] =& $mailTemplate;
}
return $this->_mailTemplates[$emailKey];
}
}
?>