|
|
@ -192,69 +192,115 @@ class SpdxLicense
|
|
|
|
* @param string $license
|
|
|
|
* @param string $license
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
* @return bool
|
|
|
|
*
|
|
|
|
|
|
|
|
* @throws \RuntimeException
|
|
|
|
* @throws \RuntimeException
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private function isValidLicenseString($license)
|
|
|
|
private function isValidLicenseString($license)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$licenses = array_map('preg_quote', array_keys($this->licenses));
|
|
|
|
$tokens = array(
|
|
|
|
sort($licenses);
|
|
|
|
'po' => '\(',
|
|
|
|
$licenses = array_reverse($licenses);
|
|
|
|
'pc' => '\)',
|
|
|
|
$licenses = implode('|', $licenses);
|
|
|
|
'op' => '(?:or|OR|and|AND)',
|
|
|
|
|
|
|
|
'wi' => '(?:with|WITH)',
|
|
|
|
$exceptions = array_map('preg_quote', array_keys($this->exceptions));
|
|
|
|
'lix' => '(?:NONE|NOASSERTION)',
|
|
|
|
sort($exceptions);
|
|
|
|
'lir' => 'LicenseRef-\d+',
|
|
|
|
$exceptions = array_reverse($exceptions);
|
|
|
|
'lic' => '[-_.a-zA-Z0-9]{3,}\+?',
|
|
|
|
$exceptions = implode('|', $exceptions);
|
|
|
|
'ws' => '\s+',
|
|
|
|
|
|
|
|
'_' => '.',
|
|
|
|
$regex = "{
|
|
|
|
);
|
|
|
|
(?(DEFINE)
|
|
|
|
|
|
|
|
# idstring: 1*( ALPHA / DIGIT / - / . )
|
|
|
|
$next = function () use ($license, $tokens) {
|
|
|
|
(?<idstring>[\pL\pN\-\.]{1,})
|
|
|
|
static $offset = 0;
|
|
|
|
|
|
|
|
|
|
|
|
# license-id: taken from list
|
|
|
|
if ($offset >= strlen($license)) {
|
|
|
|
(?<licenseid>${licenses})
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
# license-exception-id: taken from list
|
|
|
|
|
|
|
|
(?<licenseexceptionid>${exceptions})
|
|
|
|
foreach ($tokens as $name => $token) {
|
|
|
|
|
|
|
|
if (false === $r = preg_match('{' . $token . '}', $license, $matches, PREG_OFFSET_CAPTURE, $offset)) {
|
|
|
|
# license-ref: [DocumentRef-1*(idstring):]LicenseRef-1*(idstring)
|
|
|
|
throw new \RuntimeException('Pattern for token %s failed (regex error).', $name);
|
|
|
|
(?<licenseref>(?:DocumentRef-(?&idstring):)?LicenseRef-(?&idstring))
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($r === 0) {
|
|
|
|
# simple-expresssion: license-id / license-id+ / license-ref
|
|
|
|
continue;
|
|
|
|
(?<simple_expression>(?&licenseid)\+? | (?&licenseid) | (?&licenseref))
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($matches[0][1] !== $offset) {
|
|
|
|
# compound expression: 1*(
|
|
|
|
continue;
|
|
|
|
# simple-expression /
|
|
|
|
}
|
|
|
|
# simple-expression WITH license-exception-id /
|
|
|
|
$offset += strlen($matches[0][0]);
|
|
|
|
# compound-expression AND compound-expression /
|
|
|
|
|
|
|
|
# compound-expression OR compound-expression
|
|
|
|
return array($name, $matches[0][0]);
|
|
|
|
# ) / ( compound-expression ) )
|
|
|
|
|
|
|
|
(?<compound_head>
|
|
|
|
|
|
|
|
(?&simple_expression) ( \s+ (?:with|WITH) \s+ (?&licenseexceptionid))?
|
|
|
|
|
|
|
|
| \( \s* (?&compound_expression) \s*\)
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
(?<compound_expression>
|
|
|
|
|
|
|
|
(?&compound_head) (?: \s+ (?:and|AND|or|OR) \s+ (?&compound_expression))?
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# license-expression: 1*1(simple-expression / compound-expression)
|
|
|
|
|
|
|
|
(?<license_expression>NONE | NOASSERTION | (?&compound_expression) | (?&simple_expression))
|
|
|
|
|
|
|
|
) # end of define
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
^(?&license_expression)$
|
|
|
|
|
|
|
|
}x";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$match = preg_match($regex, $license);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (0 === $match) {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (false === $match) {
|
|
|
|
throw new \RuntimeException(
|
|
|
|
throw new \RuntimeException('Regex failed to compile/run.');
|
|
|
|
'At least the last pattern needs to match, but it did not (dot-match-all is missing?).'
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$open = 0;
|
|
|
|
|
|
|
|
$with = false;
|
|
|
|
|
|
|
|
$require = true;
|
|
|
|
|
|
|
|
$lastop = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (list($token, $string) = $next()) {
|
|
|
|
|
|
|
|
switch ($token) {
|
|
|
|
|
|
|
|
case 'po':
|
|
|
|
|
|
|
|
if ($open || !$require || $with) {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$open = 1;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'pc':
|
|
|
|
|
|
|
|
if ($open !== 1 || $require || !$lastop || $with) {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$open = 2;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'op':
|
|
|
|
|
|
|
|
if ($require || !$open || $with) {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$lastop || $lastop = $string;
|
|
|
|
|
|
|
|
if ($lastop !== $string) {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$require = true;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'wi':
|
|
|
|
|
|
|
|
$with = true;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'lix':
|
|
|
|
|
|
|
|
if ($open || $with) {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
goto lir;
|
|
|
|
|
|
|
|
case 'lic':
|
|
|
|
|
|
|
|
if ($with && $this->isValidExceptionIdentifier($string)) {
|
|
|
|
|
|
|
|
$require = true;
|
|
|
|
|
|
|
|
$with = false;
|
|
|
|
|
|
|
|
goto lir;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($with) {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->isValidLicenseIdentifier(rtrim($string, '+'))) {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fall-through intended
|
|
|
|
|
|
|
|
case 'lir':
|
|
|
|
|
|
|
|
lir:
|
|
|
|
|
|
|
|
if (!$require) {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$require = false;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'ws':
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '_':
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
throw new \RuntimeException(sprintf('Unparsed token: %s.', print_r($token, true)));
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
return !($open % 2 || $require || $with);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|