FOSSology  4.6.0
Open Source License Compliance by Open Source Software
SpdxLicenseValidator.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2025 Sandip Mandal
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 namespace Fossology\Lib\Data;
9 
15 {
16  const VALID_IDSTRING_PATTERN = '/^[a-zA-Z0-9.\-]+$/';
17 
18  public static function isValidLicenseRef(string $licenseId): bool
19  {
20  if (empty($licenseId) || !self::isLicenseRef($licenseId)) {
21  return false;
22  }
23 
24  $idstring = self::extractIdstring($licenseId);
25  return !empty($idstring) && preg_match(self::VALID_IDSTRING_PATTERN, $idstring);
26  }
27 
28  public static function isLicenseRef(string $licenseId): bool
29  {
30  return strpos($licenseId, LicenseRef::SPDXREF_PREFIX) === 0;
31  }
32 
33  public static function extractIdstring(string $licenseId): string
34  {
35  if (!self::isLicenseRef($licenseId)) {
36  return '';
37  }
38  return substr($licenseId, strlen(LicenseRef::SPDXREF_PREFIX));
39  }
40 
41  public static function getValidationErrors(string $licenseId): array
42  {
43  if (empty($licenseId)) {
44  return ["License identifier is empty"];
45  }
46 
47  if (!self::isLicenseRef($licenseId)) {
48  return [];
49  }
50 
51  $idstring = self::extractIdstring($licenseId);
52  if (empty($idstring)) {
53  return ["LicenseRef- prefix found but no idstring follows"];
54  }
55 
56  $invalidChars = [];
57  for ($i = 0; $i < strlen($idstring); $i++) {
58  $char = $idstring[$i];
59  if (!preg_match('/[a-zA-Z0-9.\-]/', $char) && !in_array($char, $invalidChars)) {
60  $invalidChars[] = $char;
61  }
62  }
63 
64  if (!empty($invalidChars)) {
65  return ["Contains invalid characters: " . implode(', ', array_map(fn($c) => "'$c'", $invalidChars))];
66  }
67 
68  return [];
69  }
70 
71  public static function sanitizeLicenseRef(string $licenseId): string
72  {
73  if (empty($licenseId) || !self::isLicenseRef($licenseId)) {
74  return $licenseId;
75  }
76 
77  $idstring = self::extractIdstring($licenseId);
78  if (empty($idstring)) {
79  return $licenseId;
80  }
81 
82  $sanitized = preg_replace('/[^a-zA-Z0-9.\-]/', '-', $idstring);
83  $sanitized = preg_replace('/-+/', '-', $sanitized);
84  $sanitized = trim($sanitized, '-');
85 
86  return empty($sanitized) ? $licenseId : LicenseRef::SPDXREF_PREFIX . $sanitized;
87  }
88 }
Validate and sanitize SPDX LicenseRef identifiers.
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:690