FOSSology  4.4.0
Open Source License Compliance by Open Source Software
spdx2.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2015-2016,2023 Siemens AG
4  SPDX-FileCopyrightText: © 2017 TNG Technology Consulting GmbH
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
43 namespace Fossology\SpdxTwo;
44 
64 use Twig\Environment;
65 
66 include_once(__DIR__ . "/spdx2utils.php");
67 
68 include_once(__DIR__ . "/version.php");
69 include_once(__DIR__ . "/services.php");
70 
75 class SpdxTwoAgent extends Agent
76 {
77 
78  const OUTPUT_FORMAT_KEY = "outputFormat";
79  const DEFAULT_OUTPUT_FORMAT = "spdx2";
80  const AVAILABLE_OUTPUT_FORMATS = "spdx2,spdx2tv,dep5,spdx2csv";
81  const UPLOAD_ADDS = "uploadsAdd";
82  const DATA_LICENSE = "CC0-1.0";
83 
87  private $uploadDao;
91  private $clearingDao;
95  private $licenseDao;
99  protected $dbManager;
103  protected $renderer;
107  private $licenseMap;
127  private $reportutils;
131  protected $agentNames = AgentRef::AGENT_LIST;
135  protected $filebasename = null;
139  protected $uri;
143  protected $filename;
148  private $licensesInDocument = [];
153 
154  function __construct()
155  {
156  // deduce the agent name from the command line arguments
157  $args = getopt("", array(self::OUTPUT_FORMAT_KEY.'::'));
158  $agentName = "";
159  if (array_key_exists(self::OUTPUT_FORMAT_KEY, $args)) {
160  $agentName = trim($args[self::OUTPUT_FORMAT_KEY]);
161  }
162  if (empty($agentName)) {
164  }
165 
166  parent::__construct($agentName, AGENT_VERSION, AGENT_REV);
167 
168  $this->uploadDao = $this->container->get('dao.upload');
169  $this->clearingDao = $this->container->get('dao.clearing');
170  $this->licenseDao = $this->container->get('dao.license');
171  $this->dbManager = $this->container->get('db.manager');
172  $this->renderer = $this->container->get('twig.environment');
173  $this->renderer->setCache(false);
174 
175  $this->agentSpecifLongOptions[] = self::UPLOAD_ADDS.':';
176  $this->agentSpecifLongOptions[] = self::OUTPUT_FORMAT_KEY.':';
177 
178  $this->licenseClearedGetter = new LicenseClearedGetter();
179  $this->licenseMainGetter = new LicenseMainGetter();
180  $this->obligationsGetter = new ObligationsGetter();
181  $this->reportutils = new ReportUtils();
182  }
183 
189  protected function preWorkOnArgs($args)
190  {
191  if ((!array_key_exists(self::OUTPUT_FORMAT_KEY, $args)
192  || $args[self::OUTPUT_FORMAT_KEY] === "")
193  && array_key_exists(self::UPLOAD_ADDS,$args)) {
194  $args = SpdxTwoUtils::preWorkOnArgsFlp($args,self::UPLOAD_ADDS,self::OUTPUT_FORMAT_KEY);
195  } else {
196  if (!array_key_exists(self::UPLOAD_ADDS,$args) || $args[self::UPLOAD_ADDS] === "") {
197  $args = SpdxTwoUtils::preWorkOnArgsFlp($args,self::UPLOAD_ADDS,self::OUTPUT_FORMAT_KEY);
198  }
199  }
200  return $args;
201  }
202 
207  function processUploadId($uploadId)
208  {
209  $args = $this->preWorkOnArgs($this->args);
210 
211  if (array_key_exists(self::OUTPUT_FORMAT_KEY,$args)) {
212  $possibleOutputFormat = trim($args[self::OUTPUT_FORMAT_KEY]);
213  if (in_array($possibleOutputFormat, explode(',',self::AVAILABLE_OUTPUT_FORMATS))) {
214  $this->outputFormat = $possibleOutputFormat;
215  }
216  }
217  $this->licenseMap = new LicenseMap($this->dbManager, $this->groupId, LicenseMap::REPORT, true);
218  $this->computeUri($uploadId);
219 
220  $docLicense = $this->licenseDao->getLicenseByShortName(self::DATA_LICENSE);
221  $docLicenseId = $docLicense->getId() . "-" . md5($docLicense->getText());
222  $this->licensesInDocument[$docLicenseId] = (new SpdxLicenseInfo())
223  ->setLicenseObj($docLicense)
224  ->setListedLicense(true)
225  ->setCustomText(false)
226  ->setTextPrinted(true);
227 
228  $packageNodes = $this->renderPackage($uploadId);
229  $additionalUploadIds = array_key_exists(self::UPLOAD_ADDS,$args) ? explode(',',$args[self::UPLOAD_ADDS]) : array();
230  $packageIds = array($uploadId);
231  foreach ($additionalUploadIds as $additionalId) {
232  $packageNodes .= $this->renderPackage($additionalId);
233  $packageIds[] = $additionalId;
234  }
235 
236  $this->writeReport($packageNodes, $packageIds, $uploadId);
237  return true;
238  }
239 
245  protected function getTemplateFile($partname)
246  {
247  $prefix = $this->outputFormat . "-";
248  $postfix = ".twig";
249  switch ($this->outputFormat) {
250  case "spdx2":
251  $postfix = ".xml" . $postfix;
252  break;
253  case "spdx2csv":
254  case "spdx2tv":
255  break;
256  case "dep5":
257  $prefix = $prefix . "copyright-";
258  break;
259  }
260  return $prefix . $partname . $postfix;
261  }
262 
270  protected function getFileBasename($packageName)
271  {
272  if ($this->filebasename == null) {
273  $fileName = strtoupper($this->outputFormat)."_".$packageName.'_'.time();
274  switch ($this->outputFormat) {
275  case "spdx2":
276  $fileName = $fileName .".spdx.rdf";
277  break;
278  case "spdx2tv":
279  $fileName = $fileName .".spdx";
280  break;
281  case "spdx2csv":
282  $fileName = $fileName .".csv";
283  break;
284  case "dep5":
285  $fileName = $fileName .".txt";
286  break;
287  }
288  $this->filebasename = $fileName;
289  }
290  return $this->filebasename;
291  }
292 
298  protected function getFileName($packageName)
299  {
300  global $SysConf;
301  $fileBase = $SysConf['FOSSOLOGY']['path']."/report/";
302  return $fileBase. $this->getFileBasename($packageName);
303  }
304 
310  protected function getUri($packageName)
311  {
312  global $SysConf;
313  $url=$SysConf['SYSCONFIG']['FOSSologyURL'];
314  if (substr( $url, 0, 4 ) !== "http") {
315  $url="http://".$url;
316  }
317 
318  return rtrim($url, '/') . '/' . $this->getFileBasename($packageName);
319  }
320 
326  protected function renderPackage($uploadId)
327  {
328  $uploadTreeTableName = $this->uploadDao->getUploadtreeTableName($uploadId);
329  $itemTreeBounds = $this->uploadDao->getParentItemBounds($uploadId,$uploadTreeTableName);
330  $this->heartbeat(0);
331 
332  $filesWithLicenses = $this->reportutils
333  ->getFilesWithLicensesFromClearings($itemTreeBounds, $this->groupId,
334  $this, $this->licensesInDocument);
335  $this->heartbeat(0);
336 
337  $this->reportutils->addClearingStatus($filesWithLicenses,$itemTreeBounds, $this->groupId);
338  $this->heartbeat(0);
339 
340  $scannerIDs = $this->reportutils->addScannerResults($filesWithLicenses, $itemTreeBounds, $this->groupId, $this->licensesInDocument);
341  $licenseComment = "";
342  if (!empty($scannerIDs)) {
343  $licenseComment = $this->getLicenseComment($scannerIDs);
344  }
345  $this->heartbeat(0);
346 
347  $this->reportutils->addCopyrightResults($filesWithLicenses, $uploadId);
348  $this->heartbeat(0);
349 
350  $upload = $this->uploadDao->getUpload($uploadId);
351  $fileNodes = $this->generateFileNodes($filesWithLicenses, $upload->getTreeTableName(), $uploadId);
352 
353  $mainLicenseIds = $this->clearingDao->getMainLicenseIds($uploadId, $this->groupId);
354  $mainLicenses = array();
355  foreach ($mainLicenseIds as $licId) {
356  $reportedLicenseId = $this->licenseMap->getProjectedId($licId);
357  $mainLicense = $this->licenseDao->getLicenseById($reportedLicenseId);
358  $reportLicId = $mainLicense->getId() . "-" . md5($mainLicense->getText());
359  $mainLicenses[] = $reportLicId;
360  if (!array_key_exists($reportLicId, $this->licensesInDocument)) {
361  $listedLicense = stripos($mainLicense->getSpdxId(),
362  LicenseRef::SPDXREF_PREFIX) !== 0;
363  $this->licensesInDocument[$reportLicId] = (new SpdxLicenseInfo())
364  ->setLicenseObj($mainLicense)
365  ->setCustomText(false)
366  ->setListedLicense($listedLicense);
367  }
368  }
369  $mainLicenseString = [];
370  if ($this->outputFormat == "spdx2tv" ||
371  $this->outputFormat == "spdx2csv") {
372  foreach ($mainLicenses as $mainLicense) {
373  $shortName = $this->licensesInDocument[$mainLicense]
374  ->getLicenseObj()->getShortName();
375  if (StringOperation::stringStartsWith($shortName,
376  LicenseRef::SPDXREF_PREFIX)) {
377  $mainLicenseString[] = $shortName;
378  } else {
379  $mainLicenseString[] = $this->licensesInDocument[$mainLicense]
380  ->getLicenseObj()->getSpdxId();
381  }
382  }
383  $mainLicenseString = SpdxTwoUtils::implodeLicenses(
384  SpdxTwoUtils::removeEmptyLicenses($mainLicenseString));
385  }
386 
387  $hashes = $this->uploadDao->getUploadHashes($uploadId);
388 
389  $reportInfo = $this->uploadDao->getReportInfo($uploadId);
390  $componentId = $reportInfo['ri_component_id'];
391  $componentType = $reportInfo['ri_component_type'];
392  $componentVersion = $reportInfo['ri_version'];
393  $generalAssessment = $reportInfo['ri_general_assesment'];
394  $releaseDate = $reportInfo['ri_release_date'];
395  if ($componentId == "NA") {
396  $componentId = "";
397  }
398  if ($componentVersion == "NA") {
399  $componentVersion = "";
400  }
401  if ($generalAssessment == "NA") {
402  $generalAssessment = "";
403  }
404  if ($releaseDate == "NA") {
405  $releaseDate = "";
406  } else {
407  $timeStamp = strtotime($releaseDate);
408  if ($timeStamp != -1) {
409  $releaseDate = date("Y-m-d\\T00:00:00\\Z", $timeStamp);
410  } else {
411  $releaseDate = "";
412  }
413  }
414  if ($componentType == ComponentType::MAVEN) {
415  $componentType = "maven-central";
416  } elseif ($componentType == ComponentType::PACKAGEURL) {
417  $componentType = "purl";
418  } else {
419  if (!empty($componentType)) {
420  $componentType = ComponentType::TYPE_MAP[$componentType];
421  } else {
423  }
424  }
425  $obligations = $this->getObligations($uploadId, $this->groupId);
426 
427  return $this->renderString($this->getTemplateFile('package'), [
428  'packageId' => $uploadId,
429  'uri' => $this->uri,
430  'packageName' => $upload->getFilename(),
431  'packageVersion' => $componentVersion,
432  'releaseDate' => $releaseDate,
433  'generalAssessment' => $generalAssessment,
434  'uploadName' => $upload->getFilename(),
435  'componentType' => $componentType,
436  'componentId' => htmlspecialchars($componentId),
437  'sha1' => $hashes['sha1'],
438  'md5' => $hashes['md5'],
439  'sha256' => $hashes['sha256'],
440  'verificationCode' => $this->getVerificationCode($upload),
441  'mainLicenses' => $mainLicenses,
442  'mainLicenseString' => $mainLicenseString,
443  'licenseComments' => $licenseComment,
444  'fileNodes' => $fileNodes,
445  'obligations' => $obligations,
446  'licenseList' => $this->licensesInDocument
447  ]);
448  }
449 
454  protected function getLicenseComment($scannerIds)
455  {
457  $func = function($scannerId) use ($agentDao)
458  {
459  return $agentDao->getAgentName($scannerId)." (".$agentDao->getAgentRev($scannerId).")";
460  };
461  $scannerNames = array_map($func, $scannerIds);
462  return "licenseInfoInFile determined by Scanners:\n - ".implode("\n - ",$scannerNames);
463  }
464 
473  protected function toLicensesWithFilesAdder(&$filesWithLicenses, $licenses, $copyrights, $file, $fullPath)
474  {
475  if (!array_key_exists($licenses, $filesWithLicenses)) {
476  $filesWithLicenses[$licenses]['files']=array();
477  $filesWithLicenses[$licenses]['copyrights']=array();
478  }
479  if (empty($copyrights)) {
480  $copyrights = array();
481  }
482  $filesWithLicenses[$licenses]['files'][$file] = $fullPath;
483  foreach ($copyrights as $copyright) {
484  if (!in_array($copyright, $filesWithLicenses[$licenses]['copyrights'])) {
485  $filesWithLicenses[$licenses]['copyrights'][] = $copyright;
486  }
487  }
488  }
489 
496  protected function toLicensesWithFiles(&$filesWithLicenses, $treeTableName)
497  {
498  $licensesWithFiles = array();
499  $treeDao = $this->container->get('dao.tree');
500  $filesProceeded = 0;
501  foreach ($filesWithLicenses as $fileId => $fileNode) {
502  $filesProceeded += 1;
503  if (($filesProceeded&2047)==0) {
504  $this->heartbeat(0);
505  }
506  $fullPath = $treeDao->getFullPath($fileId, $treeTableName, 0);
507  if (! empty($fileNode->getConcludedLicenses())) {
508  $licenses = [];
509  foreach ($fileNode->getConcludedLicenses() as $license) {
510  $licenses[] = $this->licensesInDocument[$license]
511  ->getLicenseObj()->getSpdxId();
512  }
513  $licenses = SpdxTwoUtils::implodeLicenses(
514  SpdxTwoUtils::removeEmptyLicenses(array_unique($licenses)));
515  $this->toLicensesWithFilesAdder($licensesWithFiles,
516  $licenses, $fileNode->getCopyrights(), $fileId, $fullPath);
517  } else {
518  if (! empty($fileNode->getScanners())) {
519  $implodedLicenses = [];
520  foreach ($fileNode->getScanners() as $license) {
521  $implodedLicenses[] = $this->licensesInDocument[$license]
522  ->getLicenseObj()->getSpdxId();
523  }
524  $implodedLicenses = SpdxTwoUtils::implodeLicenses(
525  SpdxTwoUtils::removeEmptyLicenses(array_unique($implodedLicenses)));
526  if ($fileNode->isCleared()) {
527  $msgLicense = "None (scanners found: " . $implodedLicenses . ")";
528  } else {
529  $msgLicense = "NoLicenseConcluded (scanners found: " . $implodedLicenses . ")";
530  }
531  } else {
532  if ($fileNode->isCleared()) {
533  $msgLicense = "None";
534  } else {
535  $msgLicense = "NoLicenseConcluded";
536  }
537  }
538  $this->toLicensesWithFilesAdder($licensesWithFiles, $msgLicense,
539  $fileNode->getCopyrights(), $fileId, $fullPath);
540  }
541  }
542  return $licensesWithFiles;
543  }
544 
549  protected function computeUri($uploadId)
550  {
551  $upload = $this->uploadDao->getUpload($uploadId);
552  $packageName = $upload->getFilename();
553 
554  $this->uri = $this->getUri($packageName);
555  $this->filename = $this->getFileName($packageName);
556  }
557 
564  protected function writeReport(&$packageNodes, $packageIds, $uploadId)
565  {
566  global $SysConf;
567 
568  $fileBase = dirname($this->filename);
569 
570  if (!is_dir($fileBase)) {
571  mkdir($fileBase, 0777, true);
572  }
573  umask(0133);
574 
575  $organizationName = $SysConf['SYSCONFIG']["ReportHeaderText"];
576  $version = $SysConf['BUILD']['VERSION'];
577 
578  $message = $this->renderString($this->getTemplateFile('document'),array(
579  'documentName' => $fileBase,
580  'uri' => $this->uri,
581  'userName' => $this->container->get('dao.user')->getUserName($this->userId) . " (" . $this->container->get('dao.user')->getUserEmail($this->userId) . ")",
582  'organisation' => $organizationName,
583  'toolVersion' => 'fossology-' . $version,
584  'packageNodes' => $packageNodes,
585  'packageIds' => $packageIds,
586  'dataLicense' => $this->getSPDXDataLicense(),
587  'licenseList' => $this->licensesInDocument
588  )
589  );
590 
591  // To ensure the file is valid, replace any non-printable characters with a question mark.
592  // 'Non-printable' is ASCII < 0x20 (excluding \r, \n and tab) and 0x7F (delete).
593  $message = preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/','?',$message);
594 
595  file_put_contents($this->filename, $message);
596  $this->updateReportTable($uploadId, $this->jobId, $this->filename);
597  }
598 
605  protected function updateReportTable($uploadId, $jobId, $fileName)
606  {
607  $this->dbManager->insertTableRow('reportgen',
608  array('upload_fk'=>$uploadId, 'job_fk'=>$jobId, 'filepath'=>$fileName),
609  __METHOD__);
610  }
611 
618  protected function renderString($templateName, $vars)
619  {
620  return $this->renderer->load($templateName)->render($vars);
621  }
622 
630  protected function generateFileNodes($filesWithLicenses, $treeTableName, $uploadId)
631  {
632  $this->deduplicateLicenseList();
633  if (strcmp($this->outputFormat, "dep5") !== 0) {
634  return $this->generateFileNodesByFiles($filesWithLicenses, $treeTableName, $uploadId);
635  } else {
636  return $this->generateFileNodesByLicenses($filesWithLicenses, $treeTableName);
637  }
638  }
639 
647  protected function generateFileNodesByFiles($filesWithLicenses, $treeTableName, $uploadId)
648  {
649  /* @var $treeDao TreeDao */
650  $treeDao = $this->container->get('dao.tree');
651 
652  $filesProceeded = 0;
653  $lastValue = 0;
654  $content = '';
655  $textToBePrinted = [];
656  foreach ($filesWithLicenses as $fileId => $fileData) {
657  $filesProceeded += 1;
658  if (($filesProceeded & 2047) == 0) {
659  $this->heartbeat($filesProceeded - $lastValue);
660  $lastValue = $filesProceeded;
661  }
662  $hashes = $treeDao->getItemHashes($fileId);
663  $fileName = $treeDao->getFullPath($fileId, $treeTableName, 0);
664  $stateComment = $this->getSPDXReportConf($uploadId, 0);
665  $stateWoInfos = $this->getSPDXReportConf($uploadId, 1);
666  foreach ($fileData->getConcludedLicenses() as $license) {
667  if (! $this->licensesInDocument[$license]->isTextPrinted()) {
668  $textToBePrinted[] = $license;
669  }
670  }
671  foreach ($fileData->getScanners() as $license) {
672  if (! $this->licensesInDocument[$license]->isTextPrinted()) {
673  $textToBePrinted[] = $license;
674  }
675  }
676  $concludedLicensesString = [];
677  if ($this->outputFormat == "spdx2tv" ||
678  $this->outputFormat == "spdx2csv") {
679  foreach ($fileData->getConcludedLicenses() as $license) {
680  $shortName = $this->licensesInDocument[$license]
681  ->getLicenseObj()->getShortName();
682  if (StringOperation::stringStartsWith($shortName,
683  LicenseRef::SPDXREF_PREFIX)) {
684  $concludedLicensesString[] = $shortName;
685  } else {
686  $concludedLicensesString[] = $this->licensesInDocument[$license]
687  ->getLicenseObj()->getSpdxId();
688  }
689  }
690  $concludedLicensesString = SpdxTwoUtils::implodeLicenses(
691  SpdxTwoUtils::removeEmptyLicenses($concludedLicensesString));
692  }
693  if (!$stateWoInfos ||
694  ($stateWoInfos && (!empty($fileData->getConcludedLicenses()) ||
695  !empty($fileData->getScanners()) || !empty($fileData->getCopyrights())))) {
696  $fileData->setAcknowledgements(
697  SpdxTwoUtils::cleanTextArray($fileData->getAcknowledgements()));
698  $fileData->setComments(
699  SpdxTwoUtils::cleanTextArray($fileData->getComments()));
700  $dataTemplate = array(
701  'fileId' => $fileId,
702  'sha1' => $hashes['sha1'],
703  'md5' => $hashes['md5'],
704  'sha256' => $hashes['sha256'],
705  'uri' => $this->uri,
706  'fileName' => $fileName,
707  'fileDirName' => dirname($fileName),
708  'fileBaseName' => basename($fileName),
709  'fileData' => $fileData,
710  'licenseList' => $this->licensesInDocument,
711  'concludedLicensesString' => $concludedLicensesString,
712  'licenseCommentState' => $stateComment
713  );
714  $content .= $this->renderString($this->getTemplateFile('file'),
715  $dataTemplate);
716  }
717  foreach ($textToBePrinted as $license) {
718  $this->licensesInDocument[$license]->setTextPrinted(true);
719  }
720  }
721  $this->heartbeat($filesProceeded - $lastValue);
722  return $content;
723  }
724 
731  protected function generateFileNodesByLicenses($filesWithLicenses, $treeTableName)
732  {
733  $licensesWithFiles = $this->toLicensesWithFiles($filesWithLicenses, $treeTableName);
734 
735  $content = '';
736  $filesProceeded = 0;
737  $lastStep = 0;
738  $lastValue = 0;
739  foreach ($licensesWithFiles as $licenseId=>$entry) {
740  $filesProceeded += count($entry['files']);
741  if ($filesProceeded&(~2047) > $lastStep) {
742  $this->heartbeat($filesProceeded - $lastValue);
743  $lastStep = $filesProceeded&(~2047) + 2048;
744  $lastValue = $filesProceeded;
745  }
746 
747  $comment = "";
748  if (strrpos($licenseId, "NoLicenseConcluded (scanners found: ", -strlen($licenseId)) !== false) {
749  $comment = substr($licenseId,20,strlen($licenseId)-21);
750  $licenseId = "NoLicenseConcluded";
751  } elseif (strrpos($licenseId, "None (scanners found: ", -strlen($licenseId)) !== false) {
752  $comment = substr($licenseId,6,strlen($licenseId)-7);
753  $licenseId = "None";
754  }
755 
756  $content .= $this->renderString($this->getTemplateFile('file'),array(
757  'fileNames'=>$entry['files'],
758  'license'=>$licenseId,
759  'copyrights'=>$entry['copyrights'],
760  'comment'=>$comment));
761  }
762  $this->heartbeat($filesProceeded - $lastValue);
763  return $content;
764  }
765 
774  protected function getVerificationCode(Upload $upload)
775  {
776  $stmt = __METHOD__;
777  $param = array();
778  if ($upload->getTreeTableName()=='uploadtree_a') {
779  $sql = $upload->getTreeTableName().' WHERE upload_fk=$1 AND';
780  $param[] = $upload->getId();
781  } else {
782  $sql = $upload->getTreeTableName().' WHERE';
783  $stmt .= '.'.$upload->getTreeTableName();
784  }
785 
786  $sql = "SELECT STRING_AGG(lower_sha1,'') concat_sha1 FROM
787  (SELECT LOWER(pfile_sha1) lower_sha1 FROM pfile, $sql pfile_fk=pfile_pk AND parent IS NOT NULL ORDER BY pfile_sha1) templist";
788  $filelistPack = $this->dbManager->getSingleRow($sql,$param,$stmt);
789 
790  return sha1($filelistPack['concat_sha1']);
791  }
792 
799  protected function getSPDXReportConf($uploadId, $key)
800  {
801  $sql = "SELECT ri_spdx_selection FROM report_info WHERE upload_fk = $1";
802  $getCommentState = $this->dbManager->getSingleRow($sql, array($uploadId), __METHOD__.'.SPDX_license_comment');
803  if (!empty($getCommentState['ri_spdx_selection'])) {
804  $getCommentStateSingle = explode(',', $getCommentState['ri_spdx_selection']);
805  if ($getCommentStateSingle[$key] === "checked") {
806  return true;
807  }
808  }
809  return false;
810  }
811 
818  private function getObligations(int $uploadId, int $groupId): array
819  {
820  $licenses = $this->licenseClearedGetter->getCleared($uploadId, $this,
821  $groupId, true, "license", false);
822  $this->heartbeat(0);
823  $licensesMain = $this->licenseMainGetter->getCleared($uploadId, $this,
824  $groupId, true, null, false);
825  $this->heartbeat(0);
826  list($obligations, $_) = $this->obligationsGetter->getObligations(
827  $licenses['statements'], $licensesMain['statements'], $uploadId,
828  $groupId);
829  if (empty($obligations)) {
830  return [];
831  } else {
832  return array_column($obligations, "text");
833  }
834  }
835 
840  protected function getSPDXDataLicense()
841  {
842  $dataLic = $this->licenseDao->getLicenseByShortName(self::DATA_LICENSE);
843  return $dataLic->getId() . "-" . md5($dataLic->getText());
844  }
845 
857  private function deduplicateLicenseList()
858  {
859  $localList = array_values($this->licensesInDocument);
860  usort($localList,
861  function(SpdxLicenseInfo $a, SpdxLicenseInfo $b) {
862  return strcmp(
863  $a->getLicenseObj()->getSpdxId() . $a->getLicenseObj()->getShortName(),
864  $b->getLicenseObj()->getSpdxId() . $b->getLicenseObj()->getShortName());
865  }
866  );
867  for ($i = 0; $i < count($localList) - 1; $i++) {
868  if ((! $localList[$i]->isCustomText() && ! $localList[$i + 1]->isCustomText()) &&
869  $localList[$i]->getLicenseObj()->getSpdxId() ===
870  $localList[$i + 1]->getLicenseObj()->getSpdxId()) {
871  $newShortName = $localList[$i + 1]->getLicenseObj()->getShortName();
873  $localList[$i + 1]->getLicenseObj()->getSpdxId(),
874  LicenseRef::SPDXREF_PREFIX)) {
875  $newShortName = LicenseRef::SPDXREF_PREFIX .
876  $localList[$i + 1]->getLicenseObj()->getShortName();
877  $newShortName = preg_replace('/\+$/', '-or-later', $newShortName);
878  }
879  $md5 = md5($localList[$i + 1]->getLicenseObj()->getText());
880  $reportedLicenseShortname = "$newShortName-$md5";
881  $licIndex = $localList[$i + 1]->getLicenseObj()->getId() . "-$md5";
882  $oldLicObj = $this->licensesInDocument[$licIndex]->getLicenseObj();
883  $this->licensesInDocument[$licIndex]->setLicenseObj(
884  new License($oldLicObj->getId(), $reportedLicenseShortname,
885  $oldLicObj->getFullName(), $oldLicObj->getRisk(),
886  $oldLicObj->getText(), $oldLicObj->getUrl(),
887  $oldLicObj->getDetectorType(), $oldLicObj->getSpdxId()));
888  }
889  }
890  }
891 }
892 
893 $agent = new SpdxTwoAgent();
894 $agent->scheduler_connect();
895 $agent->run_scheduler_event_loop();
896 $agent->scheduler_disconnect(0);
Structure of an Agent with all required parameters.
Definition: Agent.php:41
heartbeat($newProcessed)
Send hear beat to the scheduler.
Definition: Agent.php:203
Wrapper class for license map.
Definition: LicenseMap.php:19
static stringStartsWith($haystack, $needle)
getVerificationCode(Upload $upload)
Get a unique identifier for a given upload.
Definition: spdx2.php:774
getFileName($packageName)
Get absolute path for report.
Definition: spdx2.php:298
getTemplateFile($partname)
Get TWIG template file based on output format.
Definition: spdx2.php:245
updateReportTable($uploadId, $jobId, $fileName)
Update the reportgen table with new report path.
Definition: spdx2.php:605
const DATA_LICENSE
Data license for SPDX reports.
Definition: spdx2.php:82
computeUri($uploadId)
For a given upload, compute the URI and filename for the report.
Definition: spdx2.php:549
const UPLOAD_ADDS
Argument for additional uploads.
Definition: spdx2.php:81
getFileBasename($packageName)
Generate report basename based on upload name.
Definition: spdx2.php:270
getLicenseComment($scannerIds)
Definition: spdx2.php:454
processUploadId($uploadId)
Given an upload ID, process the items in it.
Definition: spdx2.php:207
const DEFAULT_OUTPUT_FORMAT
Default output format.
Definition: spdx2.php:79
preWorkOnArgs($args)
Parse arguments.
Definition: spdx2.php:189
renderString($templateName, $vars)
Render a twig template.
Definition: spdx2.php:618
generateFileNodesByFiles($filesWithLicenses, $treeTableName, $uploadId)
For each file, generate the nodes by files.
Definition: spdx2.php:647
getObligations(int $uploadId, int $groupId)
Definition: spdx2.php:818
renderPackage($uploadId)
Given an upload id, render the report string.
Definition: spdx2.php:326
writeReport(&$packageNodes, $packageIds, $uploadId)
Write the report the file and update report table.
Definition: spdx2.php:564
toLicensesWithFilesAdder(&$filesWithLicenses, $licenses, $copyrights, $file, $fullPath)
Map licenses, copyrights, files and full path to filesWithLicenses array.
Definition: spdx2.php:473
getUri($packageName)
Get the URI for the given package.
Definition: spdx2.php:310
getSPDXReportConf($uploadId, $key)
Get spdx license comment state for a given upload.
Definition: spdx2.php:799
generateFileNodes($filesWithLicenses, $treeTableName, $uploadId)
Generate report nodes for files.
Definition: spdx2.php:630
const AVAILABLE_OUTPUT_FORMATS
Output formats available.
Definition: spdx2.php:80
deduplicateLicenseList()
De-duplicate license list by comparing licenses with the same SPDX ID.
Definition: spdx2.php:857
const OUTPUT_FORMAT_KEY
Argument key for output format.
Definition: spdx2.php:78
generateFileNodesByLicenses($filesWithLicenses, $treeTableName)
For each file, generate the nodes by licenses.
Definition: spdx2.php:731
toLicensesWithFiles(&$filesWithLicenses, $treeTableName)
Map findings to the files.
Definition: spdx2.php:496
static implodeLicenses($licenses)
Implode licenses with "AND" or "OR".
Definition: spdx2utils.php:96
static preWorkOnArgsFlp($args, $key1, $key2)
For a given set of arguments assign $args[$key1] and $args[$key2].
Definition: spdx2utils.php:28
static removeEmptyLicenses($licenses)
Definition: spdx2utils.php:147
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:690
int jobId
The id of the job.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16
FUNCTION char * strtoupper(char *s)
Helper function to upper case a string.
Definition: utils.c:39
Namespace used by SPDX2 agent.
list_t type structure used to keep various lists. (e.g. there are multiple lists).
Definition: nomos.h:308