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 .= "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;
274  switch ($this->outputFormat) {
275  case "spdx2":
276  $fileName .= ".spdx.rdf";
277  break;
278  case "spdx2tv":
279  $fileName .= ".spdx";
280  break;
281  case "spdx2csv":
282  $fileName .= ".csv";
283  break;
284  case "dep5":
285  $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, $this->groupId);
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->reportutils->updateOrInsertReportgenEntry($uploadId, $jobId, $fileName);
608  }
609 
616  protected function renderString($templateName, $vars)
617  {
618  return $this->renderer->load($templateName)->render($vars);
619  }
620 
628  protected function generateFileNodes($filesWithLicenses, $treeTableName, $uploadId)
629  {
630  $this->deduplicateLicenseList();
631  if (strcmp($this->outputFormat, "dep5") !== 0) {
632  return $this->generateFileNodesByFiles($filesWithLicenses, $treeTableName, $uploadId);
633  } else {
634  return $this->generateFileNodesByLicenses($filesWithLicenses, $treeTableName);
635  }
636  }
637 
645  protected function generateFileNodesByFiles($filesWithLicenses, $treeTableName, $uploadId)
646  {
647  /* @var $treeDao TreeDao */
648  $treeDao = $this->container->get('dao.tree');
649 
650  $filesProceeded = 0;
651  $lastValue = 0;
652  $content = '';
653  $textToBePrinted = [];
654  foreach ($filesWithLicenses as $fileId => $fileData) {
655  $filesProceeded += 1;
656  if (($filesProceeded & 2047) == 0) {
657  $this->heartbeat($filesProceeded - $lastValue);
658  $lastValue = $filesProceeded;
659  }
660  $hashes = $treeDao->getItemHashes($fileId);
661  $fileName = $treeDao->getFullPath($fileId, $treeTableName, 0);
662  $stateComment = $this->getSPDXReportConf($uploadId, 0);
663  $stateWoInfos = $this->getSPDXReportConf($uploadId, 1);
664  foreach ($fileData->getConcludedLicenses() as $license) {
665  if (! $this->licensesInDocument[$license]->isTextPrinted()) {
666  $textToBePrinted[] = $license;
667  }
668  }
669  foreach ($fileData->getScanners() as $license) {
670  if (! $this->licensesInDocument[$license]->isTextPrinted()) {
671  $textToBePrinted[] = $license;
672  }
673  }
674  $concludedLicensesString = [];
675  if ($this->outputFormat == "spdx2tv" ||
676  $this->outputFormat == "spdx2csv") {
677  foreach ($fileData->getConcludedLicenses() as $license) {
678  $shortName = $this->licensesInDocument[$license]
679  ->getLicenseObj()->getShortName();
680  if (StringOperation::stringStartsWith($shortName,
681  LicenseRef::SPDXREF_PREFIX)) {
682  $concludedLicensesString[] = $shortName;
683  } else {
684  $concludedLicensesString[] = $this->licensesInDocument[$license]
685  ->getLicenseObj()->getSpdxId();
686  }
687  }
688  $concludedLicensesString = SpdxTwoUtils::implodeLicenses(
689  SpdxTwoUtils::removeEmptyLicenses($concludedLicensesString));
690  }
691  if (!$stateWoInfos ||
692  ($stateWoInfos && (!empty($fileData->getConcludedLicenses()) ||
693  !empty($fileData->getScanners()) || !empty($fileData->getCopyrights())))) {
694  $fileData->setAcknowledgements(
695  SpdxTwoUtils::cleanTextArray($fileData->getAcknowledgements()));
696  $fileData->setComments(
697  SpdxTwoUtils::cleanTextArray($fileData->getComments()));
698  $dataTemplate = array(
699  'fileId' => $fileId,
700  'sha1' => $hashes['sha1'],
701  'md5' => $hashes['md5'],
702  'sha256' => $hashes['sha256'],
703  'uri' => $this->uri,
704  'fileName' => $fileName,
705  'fileDirName' => dirname($fileName),
706  'fileBaseName' => basename($fileName),
707  'fileData' => $fileData,
708  'licenseList' => $this->licensesInDocument,
709  'concludedLicensesString' => $concludedLicensesString,
710  'licenseCommentState' => $stateComment
711  );
712  $content .= $this->renderString($this->getTemplateFile('file'),
713  $dataTemplate);
714  }
715  foreach ($textToBePrinted as $license) {
716  $this->licensesInDocument[$license]->setTextPrinted(true);
717  }
718  }
719  $this->heartbeat($filesProceeded - $lastValue);
720  return $content;
721  }
722 
729  protected function generateFileNodesByLicenses($filesWithLicenses, $treeTableName)
730  {
731  $licensesWithFiles = $this->toLicensesWithFiles($filesWithLicenses, $treeTableName);
732 
733  $content = '';
734  $filesProceeded = 0;
735  $lastStep = 0;
736  $lastValue = 0;
737  foreach ($licensesWithFiles as $licenseId=>$entry) {
738  $filesProceeded += count($entry['files']);
739  if ($filesProceeded&(~2047) > $lastStep) {
740  $this->heartbeat($filesProceeded - $lastValue);
741  $lastStep = $filesProceeded&(~2047) + 2048;
742  $lastValue = $filesProceeded;
743  }
744 
745  $comment = "";
746  if (strrpos($licenseId, "NoLicenseConcluded (scanners found: ", -strlen($licenseId)) !== false) {
747  $comment = substr($licenseId,20,strlen($licenseId)-21);
748  $licenseId = "NoLicenseConcluded";
749  } elseif (strrpos($licenseId, "None (scanners found: ", -strlen($licenseId)) !== false) {
750  $comment = substr($licenseId,6,strlen($licenseId)-7);
751  $licenseId = "None";
752  }
753 
754  $content .= $this->renderString($this->getTemplateFile('file'),array(
755  'fileNames'=>$entry['files'],
756  'license'=>$licenseId,
757  'copyrights'=>$entry['copyrights'],
758  'comment'=>$comment));
759  }
760  $this->heartbeat($filesProceeded - $lastValue);
761  return $content;
762  }
763 
772  protected function getVerificationCode(Upload $upload)
773  {
774  $stmt = __METHOD__;
775  $param = array();
776  if ($upload->getTreeTableName()=='uploadtree_a') {
777  $sql = $upload->getTreeTableName().' WHERE upload_fk=$1 AND';
778  $param[] = $upload->getId();
779  } else {
780  $sql = $upload->getTreeTableName().' WHERE';
781  $stmt .= '.'.$upload->getTreeTableName();
782  }
783 
784  $sql = "SELECT STRING_AGG(lower_sha1,'') concat_sha1 FROM
785  (SELECT LOWER(pfile_sha1) lower_sha1 FROM pfile, $sql pfile_fk=pfile_pk AND parent IS NOT NULL ORDER BY pfile_sha1) templist";
786  $filelistPack = $this->dbManager->getSingleRow($sql,$param,$stmt);
787 
788  return sha1($filelistPack['concat_sha1']);
789  }
790 
797  protected function getSPDXReportConf($uploadId, $key)
798  {
799  $sql = "SELECT ri_spdx_selection FROM report_info WHERE upload_fk = $1";
800  $getCommentState = $this->dbManager->getSingleRow($sql, array($uploadId), __METHOD__.'.SPDX_license_comment');
801  if (!empty($getCommentState['ri_spdx_selection'])) {
802  $getCommentStateSingle = explode(',', $getCommentState['ri_spdx_selection']);
803  if ($getCommentStateSingle[$key] === "checked") {
804  return true;
805  }
806  }
807  return false;
808  }
809 
816  private function getObligations(int $uploadId, int $groupId): array
817  {
818  $licenses = $this->licenseClearedGetter->getCleared($uploadId, $this,
819  $groupId, true, "license", false);
820  $this->heartbeat(0);
821  $licensesMain = $this->licenseMainGetter->getCleared($uploadId, $this,
822  $groupId, true, null, false);
823  $this->heartbeat(0);
824  list($obligations, $_) = $this->obligationsGetter->getObligations(
825  $licenses['statements'], $licensesMain['statements'], $uploadId,
826  $groupId);
827  if (empty($obligations)) {
828  return [];
829  } else {
830  return array_column($obligations, "text");
831  }
832  }
833 
838  protected function getSPDXDataLicense()
839  {
840  $dataLic = $this->licenseDao->getLicenseByShortName(self::DATA_LICENSE);
841  return $dataLic->getId() . "-" . md5($dataLic->getText());
842  }
843 
855  private function deduplicateLicenseList()
856  {
857  $localList = array_values($this->licensesInDocument);
858  usort($localList,
859  function(SpdxLicenseInfo $a, SpdxLicenseInfo $b) {
860  return strcmp(
861  $a->getLicenseObj()->getSpdxId() . $a->getLicenseObj()->getShortName(),
862  $b->getLicenseObj()->getSpdxId() . $b->getLicenseObj()->getShortName());
863  }
864  );
865  for ($i = 0; $i < count($localList) - 1; $i++) {
866  if ((! $localList[$i]->isCustomText() && ! $localList[$i + 1]->isCustomText()) &&
867  $localList[$i]->getLicenseObj()->getSpdxId() ===
868  $localList[$i + 1]->getLicenseObj()->getSpdxId()) {
869  $newShortName = $localList[$i + 1]->getLicenseObj()->getShortName();
871  $localList[$i + 1]->getLicenseObj()->getShortName(),
872  LicenseRef::SPDXREF_PREFIX)) {
873  $newShortName = LicenseRef::SPDXREF_PREFIX .
874  $localList[$i + 1]->getLicenseObj()->getShortName();
875  $newShortName = preg_replace('/\+$/', '-or-later', $newShortName);
876  }
877  $md5 = md5($localList[$i + 1]->getLicenseObj()->getText());
878  $reportedLicenseShortname = "$newShortName-$md5";
879  $licIndex = $localList[$i + 1]->getLicenseObj()->getId() . "-$md5";
880  $oldLicObj = $this->licensesInDocument[$licIndex]->getLicenseObj();
881  $this->licensesInDocument[$licIndex]->setLicenseObj(
882  new License($oldLicObj->getId(), $reportedLicenseShortname,
883  $oldLicObj->getFullName(), $oldLicObj->getRisk(),
884  $oldLicObj->getText(), $oldLicObj->getUrl(),
885  $oldLicObj->getDetectorType(), $oldLicObj->getSpdxId()));
886  }
887  }
888  }
889 }
890 
891 $agent = new SpdxTwoAgent();
892 $agent->scheduler_connect();
893 $agent->run_scheduler_event_loop();
894 $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:772
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:616
generateFileNodesByFiles($filesWithLicenses, $treeTableName, $uploadId)
For each file, generate the nodes by files.
Definition: spdx2.php:645
getObligations(int $uploadId, int $groupId)
Definition: spdx2.php:816
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:797
generateFileNodes($filesWithLicenses, $treeTableName, $uploadId)
Generate report nodes for files.
Definition: spdx2.php:628
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:855
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:729
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