Verified Commit 3cf05bd7 authored by Côme Chilliet's avatar Côme Chilliet
Browse files

:sparkles: feat(personal) Fix ORCID value check

Fixed ORCID regexp to allow X final character.
Added a checksum check on the value.

issue #5991
Showing with 17 additions and 1 deletion
+17 -1
......@@ -141,9 +141,25 @@ class socialHandler_orcid extends socialHandler
function validate ($value)
{
if (!preg_match('/^[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{4}$/', $value)) {
if (!preg_match('/^[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{3}[0-9X]$/', $value)) {
throw new socialHandlerInvalidValueException(_('ORCID account IDs must look like XXXX-XXXX-XXXX-XXXX where X are digits'));
}
if (static::orcidCheckSum($value) != substr($value, -1)) {
throw new socialHandlerInvalidValueException(_('Incorrect ORCID value, the checksum does not match'));
}
return $value;
}
public static function orcidCheckSum (string $orcid): string
{
/* Remove hyphens, remove last digit, convert to array */
$baseDigits = str_split(str_replace('-', '', substr($orcid, 0, -1)));
$sum = 0;
foreach ($baseDigits as $baseDigit) {
$sum = ($sum + $baseDigit) * 2;
}
$remainder = $sum % 11;
$result = (12 - $remainder) % 11;
return (($result == 10) ? "X" : (string)$result);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment