FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
exportLicenseRefUsingSPDX.php
Go to the documentation of this file.
1 <?php
2 /***********************************************************
3  Copyright (C) 2019 Siemens AG
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU General Public License
7  version 2 as published by the Free Software Foundation.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License along
15  with this program; if not, write to the Free Software Foundation, Inc.,
16  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  ***********************************************************/
18 
27 {
32  private $mapArrayData = array(
33  'licenses' => array('licenseId', 'licenseText', 'name'),
34  'exceptions' => array('licenseExceptionId', 'licenseExceptionText', 'name')
35  );
36 
37 
38  function startProcessingLicenseData()
39  {
40  global $argv;
41  $updateWithNew = '';
42  $updateExisting = '';
43  $addNewLicense = '';
44  $newLicenseRefData = array();
45  $usage = "Usage: " . basename($argv[0]) . " [options]
46 
47  Create new licenseref.json file. Options are:
48  -E Update all existing licenses and also add new licenses.
49  (NOTE: there may be failure of test cases)
50 
51  -e Only update existing licenses.
52  (NOTE: there may be failure of test cases)
53 
54  -n Only add new licenses.
55 
56  --type Usually licenses/exceptions (optional)
57  (ex: --type 'licenses')
58 
59  --url From where you want to download (optional)
60  (ex: --url 'https://spdx.org/licenses/licenses.json')
61 
62  Additional note:
63  (if --type and --url is empty then the script will automatically download the from below)
64  For type 'licenses' URL is : $scanList[licenses]
65 
66  For type 'exceptions' URL is : $scanList[exceptions]";
67  $scanList = array(
68  'licenses' => 'https://spdx.org/licenses/licenses.json',
69  'exceptions' => 'https://spdx.org/licenses/exceptions.json'
70  );
71 
72  $options = getopt("hcEen", array("type:","url:"));
73  /* get type and url if exists if not set them to empty */
74  $type = array_key_exists("type", $options) ? $options["type"] : '';
75  $URL = array_key_exists("url", $options) ? $options["url"] : '';
76  foreach ($options as $option => $optVal) {
77  switch ($option) {
78  case 'c': /* used by fo_wrapper */
79  break;
80  case 'E': /* Update all existing licenses and also add new licenses */
81  $updateWithNew = $option;
82  break;
83  case 'e': /* Only update existing licenses */
84  $updateExisting = $option;
85  break;
86  case 'n': /* only add new licenses */
87  $addNewLicense = $option;
88  break;
89  case 'h': /* help */
90  $showUsage = true;
91  break;
92  }
93  }
94 
95  if ($showUsage) {
96  print "$usage\n";
97  exit;
98  }
99 
100  if (!empty($updateWithNew) || !empty($updateExisting) || !empty($addNewLicense)) {
101  if (!empty($type) && !empty($URL)) {
102  $newLicenseRefData = $this->getListSPDX($type, $URL, $updateWithNew, $updateExisting, $addNewLicense, $newLicenseRefData);
103  } else if (!empty($type) && empty($URL)) {
104  echo "Notice: --url cannot be empty if --type is provided \n";
105  } else if (empty($type) && !empty($URL)) {
106  echo "Notice: --type cannot be empty if --url is provided \n";
107  } else {
108  foreach ($scanList as $type => $URL) {
109  $newLicenseRefData = $this->getListSPDX($type, $URL, $updateWithNew, $updateExisting, $addNewLicense, $newLicenseRefData);
110  }
111  }
112  $newFileName = "licenseRefNew.json";
113  if (file_exists($newFileName)) {
114  unlink($newFileName);
115  }
116  $file = fopen($newFileName,'w+');
117  file_put_contents($newFileName, json_encode($newLicenseRefData, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES));
118  fclose($file);
119  echo "\n\n INFO: new $newFileName file created \n\n";
120  } else {
121  echo "\nINVALID OPTION PROVIDED\n\n";
122  print "$usage\n";
123  exit;
124  }
125  }
126 
133  function getLicenseNameWithOutSuffix($RFShortName)
134  {
135  if (strpos($RFShortName, "-only") !== false) {
136  return strstr($RFShortName, "-only", true);
137  } else if (strpos($RFShortName, "-or-later") !== false) {
138  $licenseShortname = strstr($RFShortName, "-or-later", true);
139  return $licenseShortname."+";
140  } else {
141  return $RFShortName;
142  }
143  }
144 
155  function getListSPDX($type, $URL, $updateWithNew, $updateExisting, $addNewLicense, $existingLicenseRefData)
156  {
157  global $LIBEXECDIR;
158 
159  if (!is_dir($LIBEXECDIR)) {
160  print "FATAL: Directory '$LIBEXECDIR' does not exist.\n";
161  return (1);
162  }
163 
164  $dir = opendir($LIBEXECDIR);
165  if (!$dir) {
166  print "FATAL: Unable to access '$LIBEXECDIR'.\n";
167  return (1);
168  }
169  /* check if licenseref.json exists */
170  $fileName = "$LIBEXECDIR/licenseRef.json";
171  if (!file_exists($fileName)) {
172  print "FATAL: File '$fileName' does not exist.\n";
173  return (1);
174  }
175 
176  if (empty($existingLicenseRefData)) {
177  echo "INFO: get existing licenseRef.json from $LIBEXECDIR\n";
178  $getExistingLicenseRefData = file_get_contents("$fileName");
179  /* dump all the data from licenseRef.json file to a array */
180  $existingLicenseRefData = (array) json_decode($getExistingLicenseRefData, true);
181  }
182  /* get max rf_pk from existing licenseref.json file */
183  $maxkey = array_search(max($existingLicenseRefData), $existingLicenseRefData);
184  $newRfPk = $existingLicenseRefData[$maxkey]['rf_pk'] + 1;
185  /* get license list and each license's URL */
186  $getList = json_decode(file_get_contents($URL));
187  foreach ($getList->$type as $listValue) {
188  /* get current license data from given URL */
189  $getCurrentData = file_get_contents($listValue->detailsUrl);
190  $getCurrentData = (array) json_decode($getCurrentData, true);
191  echo "INFO: search for license ".$getCurrentData[$this->mapArrayData[$type][0]]."\n";
192  /* check if the licenseid of the current license exists in old license data */
193  $licenseIdCheck = array_search($getCurrentData[$this->mapArrayData[$type][0]], array_column($existingLicenseRefData, 'rf_shortname'));
194  $currentMD5 = md5($getCurrentData[$this->mapArrayData[$type][1]]);
195  $MD5Check = array_search($currentMD5, array_column($existingLicenseRefData, 'rf_md5'));
196  if (!is_numeric($licenseIdCheck)) {
197  /* if licenseid does'nt exists then remove the suffix if any and search again */
198  $getCurrentData[$this->mapArrayData[$type][0]] = $this->getLicenseNameWithOutSuffix($getCurrentData[$this->mapArrayData[$type][0]]);
199  $getCurrentData[$this->mapArrayData[$type][0]];
200  $licenseIdCheck = array_search($getCurrentData[$this->mapArrayData[$type][0]], array_column($existingLicenseRefData, 'rf_shortname'));
201  }
202  if (is_numeric($licenseIdCheck) &&
203  !is_numeric($MD5Check) &&
204  (
205  !empty($updateWithNew) ||
206  !empty($updateExisting)
207  )
208  ) {
209  $existingLicenseRefData[$licenseIdCheck]['rf_fullname'] = $getCurrentData[$this->mapArrayData[$type][2]];
210  $existingLicenseRefData[$licenseIdCheck]['rf_text'] = $getCurrentData[$this->mapArrayData[$type][1]];
211  $existingLicenseRefData[$licenseIdCheck]['rf_url'] = $getCurrentData['seeAlso'][0];
212  $existingLicenseRefData[$licenseIdCheck]['rf_md5'] = $currentMD5;
213  $existingLicenseRefData[$licenseIdCheck]['rf_notes'] = (array_key_exists("licenseComments", $getCurrentData) ? $getCurrentData['licenseComments'] : $existingLicenseRefData[$licenseIdCheck]['rf_notes']);
214  echo "INFO: license ".$getCurrentData[$this->mapArrayData[$type][0]]." updated\n\n";
215  }
216  if (!is_numeric($licenseIdCheck) &&
217  !is_numeric($MD5Check) &&
218  (
219  !empty($updateWithNew) ||
220  !empty($addNewLicense)
221  )
222  ) {
223  $existingLicenseRefData[] = array(
224  'rf_pk' => "$newRfPk",
225  'rf_shortname' => $getCurrentData[$this->mapArrayData[$type][0]],
226  'rf_text' => $getCurrentData[$this->mapArrayData[$type][1]],
227  'rf_url' => $getCurrentData['seeAlso'][0],
228  'rf_add_date' => null,
229  'rf_copyleft' => null,
230  'rf_OSIapproved' => null,
231  'rf_fullname' => $getCurrentData[$this->mapArrayData[$type][2]],
232  'rf_FSFfree' => null,
233  'rf_GPLv2compatible' => null,
234  'rf_GPLv3compatible' => null,
235  'rf_notes' => (array_key_exists("licenseComments", $getCurrentData) ? $getCurrentData['licenseComments'] : null),
236  'rf_Fedora' => null,
237  'marydone' => "f",
238  'rf_active' => "t",
239  'rf_text_updatable' => "f",
240  'rf_md5' => $currentMD5,
241  'rf_detector_type' => 1,
242  'rf_source' => null,
243  'rf_risk' => null,
244  'rf_spdx_compatible' => "t",
245  'rf_flag' => "1"
246  );
247  echo "INFO: new license ".$getCurrentData[$this->mapArrayData[$type][0]]." added\n\n";
248  $newRfPk++;
249  }
250  }
251  return $existingLicenseRefData;
252  }
253 }
254 $obj = new exportLicenseRef();
255 echo $obj->startProcessingLicenseData();
getLicenseNameWithOutSuffix($RFShortName)
check if -only or -or-later exists.
FUNCTION int max(int permGroup, int permPublic)
Get the maximum group privilege.
Definition: libfossagent.c:309
getListSPDX($type, $URL, $updateWithNew, $updateExisting, $addNewLicense, $existingLicenseRefData)
get SPDX license or exception list and update licenseref.json