17 import(
'classes.handler.Handler');
25 function authorize($request, &$args, $roleAssignments) {
27 import(
'lib.pkp.classes.security.authorization.PKPSiteAccessPolicy');
30 array(
'orcidVerify',
'orcidAuthorize',
'about'),
34 $op = $request->getRequestedOp();
35 $targetOp = $request->getUserVar(
'targetOp');
36 if (
$op ===
'orcidAuthorize' && in_array($targetOp, [
'profile',
'submit'])) {
38 import(
'lib.pkp.classes.security.authorization.UserRequiredPolicy');
42 if (!
Config::getVar(
'general',
'installed')) define(
'SESSION_DISABLE_INIT',
true);
45 return parent::authorize($request, $args, $roleAssignments);
55 $context = $request->getContext();
56 $op = $request->getRequestedOp();
58 $contextId = ($context ==
null) ? CONTEXT_ID_NONE : $context->getId();
64 curl_setopt($curl, CURLOPT_PROXY, $httpProxyHost);
65 curl_setopt($curl, CURLOPT_PROXYPORT,
Config::getVar(
'proxy',
'http_port',
'80'));
67 curl_setopt($curl, CURLOPT_PROXYUSERPWD, $username .
':' .
Config::getVar(
'proxy',
'password'));
72 curl_setopt_array($curl, array(
73 CURLOPT_URL => $plugin->getSetting($contextId,
'orcidProfileAPIPath') . OAUTH_TOKEN_URL,
74 CURLOPT_RETURNTRANSFER =>
true,
75 CURLOPT_HTTPHEADER => array(
'Accept: application/json'),
77 CURLOPT_POSTFIELDS => http_build_query(array(
78 'code' => $request->getUserVar(
'code'),
79 'grant_type' =>
'authorization_code',
80 'client_id' => $plugin->getSetting($contextId,
'orcidClientId'),
81 'client_secret' => $plugin->getSetting($contextId,
'orcidClientSecret')
84 if (!($result = curl_exec($curl))) {
85 error_log(
'ORCID CURL error: ' . curl_error($curl) .
' (' . __FILE__ .
' line ' . __LINE__ .
', URL ' . $url .
')');
86 $orcidUri = $orcid = $accessToken =
null;
88 $response = json_decode($result,
true);
89 $orcid = $response[
'orcid'];
90 $accessToken = $response[
'access_token'];
91 $orcidUri = ($plugin->getSetting($contextId,
"isSandBox") ==
true ? ORCID_URL_SANDBOX : ORCID_URL) . $orcid;
94 switch ($request->getUserVar(
'targetOp')) {
97 curl_setopt_array($curl, array(
98 CURLOPT_RETURNTRANSFER => 1,
99 CURLOPT_URL => $url = $plugin->getSetting($contextId,
'orcidProfileAPIPath') . ORCID_API_VERSION_URL . urlencode($orcid) .
'/' . ORCID_PROFILE_URL,
100 CURLOPT_POST =>
false,
101 CURLOPT_HTTPHEADER => array(
102 'Accept: application/json',
103 'Authorization: Bearer ' . $accessToken,
106 if (!($result = curl_exec($curl))) error_log(
'ORCID CURL error: ' . curl_error($curl) .
' (' . __FILE__ .
' line ' . __LINE__ .
', URL ' . $url .
')');
107 $info = curl_getinfo($curl);
108 if ($info[
'http_code'] == 200) {
109 $profileJson = json_decode($result,
true);
111 error_log(
'Unexpected ORCID API response: ' . $info[
'http_code'] .
' (' . __FILE__ .
' line ' . __LINE__ .
', URL ' . $url .
')');
116 curl_setopt_array($curl, array(
117 CURLOPT_RETURNTRANSFER => 1,
118 CURLOPT_URL => $url = $plugin->getSetting($contextId,
'orcidProfileAPIPath') . ORCID_API_VERSION_URL . urlencode($orcid) .
'/' . ORCID_EMPLOYMENTS_URL,
119 CURLOPT_POST =>
false,
120 CURLOPT_HTTPHEADER => array(
121 'Accept: application/json',
122 'Authorization: Bearer ' . $accessToken,
125 if (!($result = curl_exec($curl))) error_log(
'ORCID CURL error: ' . curl_error($curl) .
' (' . __FILE__ .
' line ' . __LINE__ .
', URL ' . $url .
')');
126 $info = curl_getinfo($curl);
127 if ($info[
'http_code'] == 200) {
128 $employmentJson = json_decode($result,
true);
130 error_log(
'Unexpected ORCID API response: ' . $info[
'http_code'] .
' (' . __FILE__ .
' line ' . __LINE__ .
', URL ' . $url .
')');
131 $employmentJson =
null;
136 <html><body><script type="text/javascript">
137 opener.document.getElementById("givenName").value = ' . json_encode(@$profileJson[
'name'][
'given-names'][
'value']) .
';
138 opener.document.getElementById("familyName").value = ' . json_encode(@$profileJson[
'name'][
'family-name'][
'value']) .
';
139 opener.document.getElementById("email").value = ' . json_encode(@$profileJson[
'emails'][
'email'][0][
'email']) .
';
140 opener.document.getElementById("country").value = ' . json_encode(@$profileJson[
'addresses'][
'address'][0][
'country'][
'value']) .
';
141 opener.document.getElementById("affiliation").value = ' . json_encode(@$employmentJson[
'employment-summary'][0][
'organization'][
'name']) .
';
142 opener.document.getElementById("orcid").value = ' . json_encode($orcidUri).
';
143 opener.document.getElementById("connect-orcid-button").style.display = "none";
145 </script></body></html>
149 $user = $request->getUser();
153 $userDao->updateLocaleFields($user);
157 <html><body><script type="text/javascript">
158 opener.$("#profileTabs").tabs("load", 3);
160 </script></body></html>
163 default: assert(
false);
176 $context = $request->getContext();
177 $contextId = ($context ==
null) ? CONTEXT_ID_NONE : $context->getId();
180 $templatePath = $plugin->getTemplateResource(self::TEMPLATE);
183 $publicationId = $request->getUserVar(
'publicationId');
185 $authors = $authorDao->getByPublicationId($publicationId);
187 $publication =
Services::get(
'publication')->get($publicationId);
189 $authorToVerify =
null;
191 if ($request->getUserVar(
'token')) {
192 foreach ($authors as $author) {
193 if ($author->getData(
'orcidEmailToken') == $request->getUserVar(
'token')) {
194 $authorToVerify = $author;
200 $templateMgr->assign(array(
201 'currentUrl' => $request->url(
null,
'index'),
202 'verifySuccess' =>
false,
203 'authFailure' =>
false,
204 'notPublished' =>
false,
205 'sendSubmission' =>
false,
206 'sendSubmissionSuccess' =>
false,
210 if ($authorToVerify ==
null) {
212 $plugin->logError(
'OrcidHandler::orcidverify - No author found with supplied token');
213 $templateMgr->assign(
'verifySuccess',
false);
214 $templateMgr->display($templatePath);
218 if ($request->getUserVar(
'error') ===
'access_denied') {
223 $authorToVerify->setData(
'orcidAccessToken',
null);
224 $authorToVerify->setData(
'orcidAccessScope',
null);
225 $authorToVerify->setData(
'orcidRefreshToken',
null);
226 $authorToVerify->setData(
'orcidAccessExpiresOn',
null);
227 $authorToVerify->setData(
'orcidEmailToken',
null);
228 $authorDao->updateLocaleFields($authorToVerify);
229 $plugin->logError(
'OrcidHandler::orcidverify - ORCID access denied. Error description: ' . $request->getUserVar(
'error_description'));
230 $templateMgr->assign(
'denied',
true);
231 $templateMgr->display($templatePath);
236 $url = $plugin->getSetting($contextId,
'orcidProfileAPIPath').OAUTH_TOKEN_URL;
238 $ch = curl_init($url);
240 $header = array(
'Accept: application/json');
241 $postData = http_build_query(array(
242 'code' => $request->getUserVar(
'code'),
243 'grant_type' =>
'authorization_code',
244 'client_id' => $plugin->getSetting($contextId,
'orcidClientId'),
245 'client_secret' => $plugin->getSetting($contextId,
'orcidClientSecret')
248 $plugin->logInfo(
'POST ' . $url);
249 $plugin->logInfo(
'Request header: ' . var_export($header,
true));
250 $plugin->logInfo(
'Request body: ' . $postData);
254 curl_setopt($ch, CURLOPT_PROXY, $httpProxyHost);
255 curl_setopt($ch, CURLOPT_PROXYPORT,
Config::getVar(
'proxy',
'http_port',
'80'));
257 curl_setopt($ch, CURLOPT_PROXYUSERPWD, $username .
':' .
Config::getVar(
'proxy',
'password'));
261 curl_setopt_array($ch, array(
262 CURLOPT_RETURNTRANSFER =>
true,
263 CURLOPT_HTTPHEADER => $header,
264 CURLOPT_POST =>
true,
265 CURLOPT_POSTFIELDS => $postData
268 if (!($result = curl_exec($ch))) {
269 $plugin->logError(
'OrcidHandler::orcidverify - CURL error: ' . curl_error($ch));
270 $templateMgr->assign(
'authFailure',
true);
271 $templateMgr->display($templatePath);
275 $httpstatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
278 $plugin->logInfo(
'Response body: ' . $result);
279 $response = json_decode($result,
true);
280 if (isset($response[
'error']) && $response[
'error'] ===
'invalid_grant') {
281 $plugin->logError(
"Response status: $httpstatus . Authroization code invalid, maybe already used");
282 $templateMgr->assign(
'authFailure',
true);
283 $templateMgr->display($templatePath);
285 } elseif (isset($response[
'error'])) {
286 $plugin->logError(
"Response status: $httpstatus . Invalid ORCID response: $result");
287 $templateMgr->assign(
'authFailure',
true);
288 $templateMgr->display($templatePath);
291 $orcidUri = ($plugin->getSetting($contextId,
"isSandBox") ==
true ? ORCID_URL_SANDBOX : ORCID_URL) . $response[
'orcid'];
292 if (!empty($authorToVerify->getOrcid()) && $orcidUri != $authorToVerify->getOrcid()) {
294 $templateMgr->assign(
'duplicateOrcid',
true);
295 $templateMgr->display($templatePath);
298 $authorToVerify->setOrcid($orcidUri);
299 if ($plugin->getSetting($contextId,
'orcidProfileAPIPath') == ORCID_API_URL_MEMBER_SANDBOX ||
300 $plugin->getSetting($contextId,
'orcidProfileAPIPath') == ORCID_API_URL_PUBLIC_SANDBOX) {
302 $authorToVerify->setData(
'orcidSandbox',
true);
303 $templateMgr->assign(
'orcid', ORCID_URL_SANDBOX . $response[
'orcid']);
305 $templateMgr->assign(
'orcid', $orcidUri);
309 $authorToVerify->setData(
'orcidEmailToken',
null);
311 $authorDao->updateObject($authorToVerify);
312 if($plugin->isMemberApiEnabled($contextId) ) {
313 if ($publication->getData(
'status') == STATUS_PUBLISHED) {
314 $templateMgr->assign(
'sendSubmission',
true);
315 $sendResult = $plugin->sendSubmissionToOrcid($publication, $request);
316 if ($sendResult ===
true || (is_array($sendResult) && $sendResult[$response[
'orcid']])) {
317 $templateMgr->assign(
'sendSubmissionSuccess',
true);
320 $templateMgr->assign(
'submissionNotPublished',
true);
324 $templateMgr->assign(array(
325 'verifySuccess' =>
true,
326 'orcidIcon' => $plugin->getIcon()
329 $templateMgr->display($templatePath);
334 $orcidAccessExpiresOn = Carbon\Carbon::now();
337 $orcidAccessExpiresOn->addSeconds($orcidResponse[
'expires_in']);
338 $userOrAuthor->setOrcid($orcidUri);
340 $userOrAuthor->setData(
'orcidAccessDenied',
null);
341 $userOrAuthor->setData(
'orcidAccessToken', $orcidResponse[
'access_token']);
342 $userOrAuthor->setData(
'orcidAccessScope', $orcidResponse[
'scope']);
343 $userOrAuthor->setData(
'orcidRefreshToken', $orcidResponse[
'refresh_token']);
344 $userOrAuthor->setData(
'orcidAccessExpiresOn', $orcidAccessExpiresOn->toDateTimeString());
352 $context = $request->getContext();
353 $contextId = ($context ==
null) ? CONTEXT_ID_NONE : $context->getId();
356 $templateMgr->assign(
'orcidIcon', $plugin->getIcon());
357 $templateMgr->assign(
'isMemberApi', $plugin->isMemberApiEnabled($contextId));
358 $templateMgr->display($plugin->getTemplateResource(
'orcidAbout.tpl'));