FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
spdx2.php
Go to the documentation of this file.
1 <?php
2 /*
3  * Copyright (C) 2015-2016, Siemens AG
4  * Copyright (C) 2017 TNG Technology Consulting GmbH
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * version 2 as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
54 namespace Fossology\SpdxTwo;
55 
73 
74 include_once(__DIR__ . "/spdx2utils.php");
75 
76 include_once(__DIR__ . "/version.php");
77 include_once(__DIR__ . "/services.php");
78 
83 class SpdxTwoAgent extends Agent
84 {
85 
86  const OUTPUT_FORMAT_KEY = "outputFormat";
87  const DEFAULT_OUTPUT_FORMAT = "spdx2";
88  const AVAILABLE_OUTPUT_FORMATS = "spdx2,spdx2tv,dep5";
89  const UPLOAD_ADDS = "uploadsAdd";
90 
94  private $uploadDao;
98  private $clearingDao;
102  private $licenseDao;
106  protected $dbManager;
110  protected $renderer;
114  private $licenseMap;
118  protected $agentNames = AgentRef::AGENT_LIST;
122  protected $includedLicenseIds = array();
126  protected $filebasename = null;
130  protected $uri;
134  protected $filename;
138  protected $outputFormat = self::DEFAULT_OUTPUT_FORMAT;
139 
143  protected $spdxValidityChecker = null;
144 
145  function __construct()
146  {
147  // deduce the agent name from the command line arguments
148  $args = getopt("", array(self::OUTPUT_FORMAT_KEY.'::'));
149  $agentName = trim($args[self::OUTPUT_FORMAT_KEY]);
150  if (empty($agentName)) {
151  $agentName = self::DEFAULT_OUTPUT_FORMAT;
152  }
153 
154  parent::__construct($agentName, AGENT_VERSION, AGENT_REV);
155 
156  $this->uploadDao = $this->container->get('dao.upload');
157  $this->clearingDao = $this->container->get('dao.clearing');
158  $this->licenseDao = $this->container->get('dao.license');
159  $this->dbManager = $this->container->get('db.manager');
160  $this->renderer = $this->container->get('twig.environment');
161  $this->renderer->setCache(false);
162 
163  $this->agentSpecifLongOptions[] = self::UPLOAD_ADDS.':';
164  $this->agentSpecifLongOptions[] = self::OUTPUT_FORMAT_KEY.':';
165 
169  $this->spdxValidityChecker = function ($licenseShortname) use ($dbManager, $licenseDao, $groupId)
170  {
171  $lic = $licenseDao->getLicenseByShortName($licenseShortname, $groupId);
172  if ($lic === null) {
173  return false;
174  }
175  return $dbManager->booleanFromDb($lic->getSpdxCompatible());
176  };
177  }
178 
184  protected function preWorkOnArgs($args)
185  {
186  if ((!array_key_exists(self::OUTPUT_FORMAT_KEY,$args)
187  || $args[self::OUTPUT_FORMAT_KEY] === "")
188  && array_key_exists(self::UPLOAD_ADDS,$args)) {
189  $args = SpdxTwoUtils::preWorkOnArgsFlp($args,self::UPLOAD_ADDS,self::OUTPUT_FORMAT_KEY);
190  } else {
191  if (!array_key_exists(self::UPLOAD_ADDS,$args) || $args[self::UPLOAD_ADDS] === "") {
192  $args = SpdxTwoUtils::preWorkOnArgsFlp($args,self::UPLOAD_ADDS,self::OUTPUT_FORMAT_KEY);
193  }
194  }
195  return $args;
196  }
197 
202  function processUploadId($uploadId)
203  {
204  $args = $this->preWorkOnArgs($this->args);
205 
206  if (array_key_exists(self::OUTPUT_FORMAT_KEY,$args)) {
207  $possibleOutputFormat = trim($args[self::OUTPUT_FORMAT_KEY]);
208  if (in_array($possibleOutputFormat, explode(',',self::AVAILABLE_OUTPUT_FORMATS))) {
209  $this->outputFormat = $possibleOutputFormat;
210  }
211  }
212  $this->licenseMap = new LicenseMap($this->dbManager, $this->groupId, LicenseMap::REPORT, true);
213  $this->computeUri($uploadId);
214 
215  $packageNodes = $this->renderPackage($uploadId);
216  $additionalUploadIds = array_key_exists(self::UPLOAD_ADDS,$args) ? explode(',',$args[self::UPLOAD_ADDS]) : array();
217  $packageIds = array($uploadId);
218  foreach ($additionalUploadIds as $additionalId) {
219  $packageNodes .= $this->renderPackage($additionalId);
220  $packageIds[] = $additionalId;
221  }
222 
223  $this->writeReport($packageNodes, $packageIds, $uploadId);
224  return true;
225  }
226 
232  protected function getTemplateFile($partname)
233  {
234  $prefix = $this->outputFormat . "-";
235  $postfix = ".twig";
236  switch ($this->outputFormat) {
237  case "spdx2":
238  $postfix = ".xml" . $postfix;
239  break;
240  case "spdx2tv":
241  break;
242  case "dep5":
243  $prefix = $prefix . "copyright-";
244  break;
245  }
246  return $prefix . $partname . $postfix;
247  }
248 
256  protected function getFileBasename($packageName)
257  {
258  if ($this->filebasename == null) {
259  $fileName = strtoupper($this->outputFormat)."_".$packageName.'_'.time();
260  switch ($this->outputFormat) {
261  case "spdx2":
262  $fileName = $fileName ."-spdx.rdf";
263  break;
264  case "spdx2tv":
265  $fileName = $fileName .".spdx";
266  break;
267  case "dep5":
268  $fileName = $fileName .".txt";
269  break;
270  }
271  $this->filebasename = $fileName;
272  }
273  return $this->filebasename;
274  }
275 
281  protected function getFileName($packageName)
282  {
283  global $SysConf;
284  $fileBase = $SysConf['FOSSOLOGY']['path']."/report/";
285  return $fileBase. $this->getFileBasename($packageName);
286  }
287 
293  protected function getUri($packageName)
294  {
295  global $SysConf;
296  $url=$SysConf['SYSCONFIG']['FOSSologyURL'];
297  if (substr( $url, 0, 4 ) !== "http") {
298  $url="http://".$url;
299  }
300 
301  return $url . $this->getFileBasename($packageName);
302  }
303 
309  protected function renderPackage($uploadId)
310  {
311  $uploadTreeTableName = $this->uploadDao->getUploadtreeTableName($uploadId);
312  $itemTreeBounds = $this->uploadDao->getParentItemBounds($uploadId,$uploadTreeTableName);
313  $this->heartbeat(0);
314 
315  $filesWithLicenses = $this->getFilesWithLicensesFromClearings($itemTreeBounds);
316  $this->heartbeat(0);
317 
318  $this->addClearingStatus($filesWithLicenses,$itemTreeBounds);
319  $this->heartbeat(0);
320 
321  $licenseComment = $this->addScannerResults($filesWithLicenses, $itemTreeBounds);
322  $this->heartbeat(0);
323 
324  $this->addCopyrightResults($filesWithLicenses, $uploadId);
325  $this->heartbeat(0);
326 
327  $upload = $this->uploadDao->getUpload($uploadId);
328  $fileNodes = $this->generateFileNodes($filesWithLicenses, $upload->getTreeTableName(), $uploadId);
329 
330  $mainLicenseIds = $this->clearingDao->getMainLicenseIds($uploadId, $this->groupId);
331  $mainLicenses = array();
332  foreach ($mainLicenseIds as $licId) {
333  $reportedLicenseId = $this->licenseMap->getProjectedId($licId);
334  $this->includedLicenseIds[$reportedLicenseId] = true;
335  $mainLicenses[] = $this->licenseMap->getProjectedShortname($reportedLicenseId);
336  }
337 
338  if (strcmp($this->outputFormat, "dep5")!==0) {
339  $mainLicenses = SpdxTwoUtils::addPrefixOnDemandList($mainLicenses, $this->spdxValidityChecker);
340  }
341 
342  $hashes = $this->uploadDao->getUploadHashes($uploadId);
343  return $this->renderString($this->getTemplateFile('package'),array(
344  'packageId' => $uploadId,
345  'uri' => $this->uri,
346  'packageName' => $upload->getFilename(),
347  'uploadName' => $upload->getFilename(),
348  'sha1' => $hashes['sha1'],
349  'md5' => $hashes['md5'],
350  'sha256' => $hashes['sha256'],
351  'verificationCode' => $this->getVerificationCode($upload),
352  'mainLicenses' => $mainLicenses,
353  'mainLicense' => SpdxTwoUtils::implodeLicenses($mainLicenses, $this->spdxValidityChecker),
354  'licenseComments' => $licenseComment,
355  'fileNodes' => $fileNodes)
356  );
357  }
358 
364  protected function getFilesWithLicensesFromClearings(ItemTreeBounds $itemTreeBounds)
365  {
366  $clearingDecisions = $this->clearingDao->getFileClearingsFolder($itemTreeBounds, $this->groupId);
367 
368  $filesWithLicenses = array();
369  $clearingsProceeded = 0;
370  foreach ($clearingDecisions as $clearingDecision) {
371  $clearingsProceeded += 1;
372  if (($clearingsProceeded&2047)==0) {
373  $this->heartbeat(0);
374  }
375  if ($clearingDecision->getType() == DecisionTypes::IRRELEVANT) {
376  continue;
377  }
378 
379  foreach ($clearingDecision->getClearingEvents() as $clearingEvent) {
380  $clearingLicense = $clearingEvent->getClearingLicense();
381  if ($clearingLicense->isRemoved()) {
382  continue;
383  }
384 
385  /* ADD COMMENT */
386  $filesWithLicenses[$clearingDecision->getUploadTreeId()]['comment'][] = $clearingLicense->getComment();
387  if ($clearingEvent->getReportinfo()) {
388  $customLicenseText = $clearingEvent->getReportinfo();
389  $reportedLicenseShortname = $this->licenseMap->getProjectedShortname($this->licenseMap->getProjectedId($clearingLicense->getLicenseId())) .
390  '-' . md5($customLicenseText);
391  $this->includedLicenseIds[$reportedLicenseShortname] = $customLicenseText;
392  $filesWithLicenses[$clearingDecision->getUploadTreeId()]['concluded'][] = $reportedLicenseShortname;
393  } else {
394  $reportedLicenseId = $this->licenseMap->getProjectedId($clearingLicense->getLicenseId());
395  $this->includedLicenseIds[$reportedLicenseId] = true;
396  $filesWithLicenses[$clearingDecision->getUploadTreeId()]['concluded'][] = $this->licenseMap->getProjectedShortname($reportedLicenseId);
397  }
398  }
399  }
400  return $filesWithLicenses;
401  }
402 
411  protected function toLicensesWithFilesAdder(&$filesWithLicenses, $licenses, $copyrights, $file, $fullPath)
412  {
413  $key = SpdxTwoUtils::implodeLicenses($licenses);
414 
415  if (!array_key_exists($key, $filesWithLicenses)) {
416  $filesWithLicenses[$key]['files']=array();
417  $filesWithLicenses[$key]['copyrights']=array();
418  }
419  if (empty($copyrights)) {
420  $copyrights = array();
421  }
422  $filesWithLicenses[$key]['files'][$file] = $fullPath;
423  foreach ($copyrights as $copyright) {
424  if (!in_array($copyright, $filesWithLicenses[$key]['copyrights'])) {
425  $filesWithLicenses[$key]['copyrights'][] = $copyright;
426  }
427  }
428  }
429 
436  protected function toLicensesWithFiles(&$filesWithLicenses, $treeTableName)
437  {
438  $licensesWithFiles = array();
439  $treeDao = $this->container->get('dao.tree');
440  $filesProceeded = 0;
441  foreach ($filesWithLicenses as $fileId=>$licenses) {
442  $filesProceeded += 1;
443  if (($filesProceeded&2047)==0) {
444  $this->heartbeat(0);
445  }
446  $fullPath = $treeDao->getFullPath($fileId,$treeTableName,0);
447  if (!empty($licenses['concluded']) && count($licenses['concluded'])>0) {
448  $this->toLicensesWithFilesAdder($licensesWithFiles,$licenses['concluded'],$licenses['copyrights'],$fileId,$fullPath);
449  } else {
450  if (!empty($licenses['scanner']) && count($licenses['scanner']) > 0) {
451  $implodedLicenses = SpdxTwoUtils::implodeLicenses($licenses['scanner']);
452  if ($licenses['isCleared']) {
453  $msgLicense = "None (scanners found: " . $implodedLicenses . ")";
454  } else {
455  $msgLicense = "NoLicenseConcluded (scanners found: " . $implodedLicenses . ")";
456  }
457  } else {
458  if ($licenses['isCleared']) {
459  $msgLicense = "None";
460  } else {
461  $msgLicense = "NoLicenseConcluded";
462  }
463  }
464  $this->toLicensesWithFilesAdder($licensesWithFiles,array($msgLicense),$licenses['copyrights'],$fileId,$fullPath);
465  }
466  }
467  return $licensesWithFiles;
468  }
469 
476  protected function addScannerResults(&$filesWithLicenses, ItemTreeBounds $itemTreeBounds)
477  {
478  $uploadId = $itemTreeBounds->getUploadId();
479  $scannerAgents = array_keys($this->agentNames);
480  $scanJobProxy = new ScanJobProxy($this->container->get('dao.agent'), $uploadId);
481  $scanJobProxy->createAgentStatus($scannerAgents);
482  $scannerIds = $scanJobProxy->getLatestSuccessfulAgentIds();
483  if (empty($scannerIds)) {
484  return "";
485  }
486  $tableName = $itemTreeBounds->getUploadTreeTableName();
487  $stmt = __METHOD__ .'.scanner_findings';
488  $sql = "SELECT DISTINCT uploadtree_pk,rf_fk FROM $tableName ut, license_file
489  WHERE ut.pfile_fk=license_file.pfile_fk AND rf_fk IS NOT NULL AND agent_fk=any($1)";
490  $param = array('{'.implode(',',$scannerIds).'}');
491  if ($tableName == 'uploadtree_a') {
492  $param[] = $uploadId;
493  $sql .= " AND upload_fk=$".count($param);
494  $stmt .= $tableName;
495  }
496  $sql .= " GROUP BY uploadtree_pk,rf_fk";
497  $this->dbManager->prepare($stmt, $sql);
498  $res = $this->dbManager->execute($stmt,$param);
499  while ($row=$this->dbManager->fetchArray($res)) {
500  $reportedLicenseId = $this->licenseMap->getProjectedId($row['rf_fk']);
501  $shortName = $this->licenseMap->getProjectedShortname($reportedLicenseId);
502  if ($shortName != 'Void') {
503  if ($shortName != 'No_license_found') {
504  $filesWithLicenses[$row['uploadtree_pk']]['scanner'][] = $shortName;
505  } else {
506  $filesWithLicenses[$row['uploadtree_pk']]['scanner'][] = "";
507  }
508  $this->includedLicenseIds[$reportedLicenseId] = true;
509  }
510  }
511  $this->dbManager->freeResult($res);
512 
514  $func = function($scannerId) use ($agentDao)
515  {
516  return $agentDao->getAgentName($scannerId)." (".$agentDao->getAgentRev($scannerId).")";
517  };
518  $scannerNames = array_map($func, $scannerIds);
519  return "licenseInfoInFile determined by Scanners:\n - ".implode("\n - ",$scannerNames);
520  }
521 
527  protected function addCopyrightResults(&$filesWithLicenses, $uploadId)
528  {
529  $agentName = 'copyright';
531  $copyrightDao = $this->container->get('dao.copyright');
533  $scanJobProxy = new ScanJobProxy($this->container->get('dao.agent'),
534  $uploadId);
535 
536  $scanJobProxy->createAgentStatus(array($agentName));
537  $selectedScanners = $scanJobProxy->getLatestSuccessfulAgentIds();
538  if (!array_key_exists($agentName, $selectedScanners)) {
539  return;
540  }
541  $latestAgentId = $selectedScanners[$agentName];
542  $extrawhere = ' agent_fk='.$latestAgentId;
543 
544  $uploadtreeTable = $this->uploadDao->getUploadtreeTableName($uploadId);
545  $allScannerEntries = $copyrightDao->getScannerEntries('copyright', $uploadtreeTable, $uploadId, $type='statement', $extrawhere);
546  $allEditedEntries = $copyrightDao->getEditedEntries('copyright_decision', $uploadtreeTable, $uploadId, $decisionType=null);
547  foreach ($allScannerEntries as $finding) {
548  $filesWithLicenses[$finding['uploadtree_pk']]['copyrights'][] = \convertToUTF8($finding['content'],false);
549  }
550  foreach ($allEditedEntries as $finding) {
551  $filesWithLicenses[$finding['uploadtree_pk']]['copyrights'][] = \convertToUTF8($finding['textfinding'],false);
552  }
553  }
554 
560  protected function addClearingStatus(&$filesWithLicenses,ItemTreeBounds $itemTreeBounds)
561  {
562  $alreadyClearedUploadTreeView = new UploadTreeProxy($itemTreeBounds->getUploadId(),
563  array(UploadTreeProxy::OPT_SKIP_THESE => UploadTreeProxy::OPT_SKIP_ALREADY_CLEARED,
564  UploadTreeProxy::OPT_ITEM_FILTER => "AND (lft BETWEEN ".$itemTreeBounds->getLeft()." AND ".$itemTreeBounds->getRight().")",
565  UploadTreeProxy::OPT_GROUP_ID => $this->groupId),
566  $itemTreeBounds->getUploadTreeTableName(),
567  'already_cleared_uploadtree' . $itemTreeBounds->getUploadId());
568 
569  $alreadyClearedUploadTreeView->materialize();
570  $filesThatShouldStillBeCleared = $alreadyClearedUploadTreeView->getNonArtifactDescendants($itemTreeBounds);
571  $alreadyClearedUploadTreeView->unmaterialize();
572 
573  $uploadTreeIds = array_keys($filesWithLicenses);
574  foreach ($uploadTreeIds as $uploadTreeId) {
575  $filesWithLicenses[$uploadTreeId]['isCleared'] = false == array_key_exists($uploadTreeId,$filesThatShouldStillBeCleared);
576  }
577  }
578 
583  protected function computeUri($uploadId)
584  {
585  $upload = $this->uploadDao->getUpload($uploadId);
586  $packageName = $upload->getFilename();
587 
588  $this->uri = $this->getUri($packageName);
589  $this->filename = $this->getFileName($packageName);
590  }
591 
598  protected function writeReport(&$packageNodes, $packageIds, $uploadId)
599  {
600  $fileBase = dirname($this->filename);
601 
602  if (!is_dir($fileBase)) {
603  mkdir($fileBase, 0777, true);
604  }
605  umask(0133);
606 
607  $licenseTexts=$this->getLicenseTexts();
608  if (strcmp($this->outputFormat, "dep5")!==0) {
609  $licenseTexts = SpdxTwoUtils::addPrefixOnDemandKeys($licenseTexts, $this->spdxValidityChecker);
610  }
611 
612  $message = $this->renderString($this->getTemplateFile('document'),array(
613  'documentName' => $fileBase,
614  'uri' => $this->uri,
615  'userName' => $this->container->get('dao.user')->getUserName($this->userId) . " (" . $this->container->get('dao.user')->getUserEmail($this->userId) . ")",
616  'organisation' => '',
617  'packageNodes' => $packageNodes,
618  'packageIds' => $packageIds,
619  'licenseTexts' => $licenseTexts)
620  );
621 
622  // To ensure the file is valid, replace any non-printable characters with a question mark.
623  // 'Non-printable' is ASCII < 0x20 (excluding \r, \n and tab) and 0x7F (delete).
624  $message = preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/','?',$message);
625 
626  file_put_contents($this->filename, $message);
627  $this->updateReportTable($uploadId, $this->jobId, $this->filename);
628  }
629 
636  protected function updateReportTable($uploadId, $jobId, $fileName)
637  {
638  $this->dbManager->insertTableRow('reportgen',
639  array('upload_fk'=>$uploadId, 'job_fk'=>$jobId, 'filepath'=>$fileName),
640  __METHOD__);
641  }
642 
649  protected function renderString($templateName, $vars)
650  {
651  return $this->renderer->loadTemplate($templateName)->render($vars);
652  }
653 
661  protected function generateFileNodes($filesWithLicenses, $treeTableName, $uploadId)
662  {
663  if (strcmp($this->outputFormat, "dep5") !== 0) {
664  return $this->generateFileNodesByFiles($filesWithLicenses, $treeTableName, $uploadId);
665  } else {
666  return $this->generateFileNodesByLicenses($filesWithLicenses, $treeTableName);
667  }
668  }
669 
677  protected function generateFileNodesByFiles($filesWithLicenses, $treeTableName, $uploadId)
678  {
679  /* @var $treeDao TreeDao */
680  $treeDao = $this->container->get('dao.tree');
681 
682  $filesProceeded = 0;
683  $lastValue = 0;
684  $content = '';
685  foreach ($filesWithLicenses as $fileId=>$licenses) {
686  $filesProceeded += 1;
687  if (($filesProceeded & 2047) == 0) {
688  $this->heartbeat($filesProceeded - $lastValue);
689  $lastValue = $filesProceeded;
690  }
691  $hashes = $treeDao->getItemHashes($fileId);
692  $fileName = $treeDao->getFullPath($fileId, $treeTableName, 0);
693  if (!is_array($licenses['concluded'])) {
694  $licenses['concluded'] = array();
695  }
696  if (!is_array($licenses['scanner'])) {
697  $licenses['scanner'] = array();
698  }
699  $stateComment = $this->getSPDXReportConf($uploadId, 0);
700  $stateWoInfos = $this->getSPDXReportConf($uploadId, 1);
701  if (!$stateWoInfos ||
702  ($stateWoInfos && (!empty($licenses['concluded']) || (!empty($licenses['scanner']) && !empty($licenses['scanner'][0])) || !empty($licenses['copyrights'])))) {
703  $dataTemplate = array(
704  'fileId' => $fileId,
705  'sha1' => $hashes['sha1'],
706  'md5' => $hashes['md5'],
707  'sha256' => $hashes['sha256'],
708  'uri' => $this->uri,
709  'fileName' => $fileName,
710  'fileDirName' => dirname($fileName),
711  'fileBaseName' => basename($fileName),
712  'isCleared' => $licenses['isCleared'],
713  'concludedLicense' => SpdxTwoUtils::implodeLicenses($licenses['concluded'], $this->spdxValidityChecker),
714  'concludedLicenses' => SpdxTwoUtils::addPrefixOnDemandList($licenses['concluded'], $this->spdxValidityChecker),
715  'scannerLicenses' => SpdxTwoUtils::addPrefixOnDemandList($licenses['scanner'], $this->spdxValidityChecker),
716  'copyrights' => $licenses['copyrights'],
717  'licenseCommentState' => $stateComment
718  );
719  if ($stateComment) {
720  $dataTemplate['licenseComment'] = SpdxTwoUtils::implodeLicenses($licenses['comment']);
721  }
722  $content .= $this->renderString($this->getTemplateFile('file'),$dataTemplate);
723  }
724  }
725  $this->heartbeat($filesProceeded - $lastValue);
726  return $content;
727  }
728 
735  protected function generateFileNodesByLicenses($filesWithLicenses, $treeTableName)
736  {
737  $licensesWithFiles = $this->toLicensesWithFiles($filesWithLicenses, $treeTableName);
738 
739  $content = '';
740  $filesProceeded = 0;
741  $lastStep = 0;
742  $lastValue = 0;
743  foreach ($licensesWithFiles as $licenseId=>$entry) {
744  $filesProceeded += count($entry['files']);
745  if ($filesProceeded&(~2047) > $lastStep) {
746  $this->heartbeat($filesProceeded - $lastValue);
747  $lastStep = $filesProceeded&(~2047) + 2048;
748  $lastValue = $filesProceeded;
749  }
750 
751  $comment = "";
752  if (strrpos($licenseId, "NoLicenseConcluded (scanners found: ", -strlen($licenseId)) !== false) {
753  $comment = substr($licenseId,20,strlen($licenseId)-21);
754  $licenseId = "NoLicenseConcluded";
755  } elseif (strrpos($licenseId, "None (scanners found: ", -strlen($licenseId)) !== false) {
756  $comment = substr($licenseId,6,strlen($licenseId)-7);
757  $licenseId = "None";
758  }
759 
760  $content .= $this->renderString($this->getTemplateFile('file'),array(
761  'fileNames'=>$entry['files'],
762  'license'=>$licenseId,
763  'copyrights'=>$entry['copyrights'],
764  'comment'=>$comment));
765  }
766  $this->heartbeat($filesProceeded - $lastValue);
767  return $content;
768  }
769 
774  protected function getLicenseTexts()
775  {
776  $licenseTexts = array();
777  $licenseViewProxy = new LicenseViewProxy($this->groupId,array(LicenseViewProxy::OPT_COLUMNS=>array('rf_pk','rf_shortname','rf_fullname','rf_text')));
778  $this->dbManager->prepare($stmt=__METHOD__, $licenseViewProxy->getDbViewQuery());
779  $res = $this->dbManager->execute($stmt);
780 
781  while ($row=$this->dbManager->fetchArray($res)) {
782  if (array_key_exists($row['rf_pk'], $this->includedLicenseIds)) {
783  $licenseTexts[$row['rf_shortname']] = array(
784  'text' => $row['rf_text'],
785  'name' => $row['rf_fullname'] ?: $row['rf_shortname']);
786  }
787  }
788  foreach ($this->includedLicenseIds as $license => $customText) {
789  if (true !== $customText) {
790  $licenseTexts[$license] = array(
791  'text' => $customText,
792  'name' => $license);
793  }
794  }
795  $this->dbManager->freeResult($res);
796  return $licenseTexts;
797  }
798 
807  protected function getVerificationCode(Upload $upload)
808  {
809  $stmt = __METHOD__;
810  $param = array();
811  if ($upload->getTreeTableName()=='uploadtree_a') {
812  $sql = $upload->getTreeTableName().' WHERE upload_fk=$1 AND';
813  $param[] = $upload->getId();
814  } else {
815  $sql = $upload->getTreeTableName().' WHERE';
816  $stmt .= '.'.$upload->getTreeTableName();
817  }
818 
819  $sql = "SELECT STRING_AGG(lower_sha1,'') concat_sha1 FROM
820  (SELECT LOWER(pfile_sha1) lower_sha1 FROM pfile, $sql pfile_fk=pfile_pk ORDER BY pfile_sha1) templist";
821  $filelistPack = $this->dbManager->getSingleRow($sql,$param,$stmt);
822 
823  return sha1($filelistPack['concat_sha1']);
824  }
825 
832  protected function getSPDXReportConf($uploadId, $key)
833  {
834  $sql = "SELECT ri_spdx_selection FROM report_info WHERE upload_fk = $1";
835  $getCommentState = $this->dbManager->getSingleRow($sql, array($uploadId), __METHOD__.'.SPDX_license_comment');
836  if (!empty($getCommentState['ri_spdx_selection'])) {
837  $getCommentStateSingle = explode(',', $getCommentState['ri_spdx_selection']);
838  if ($getCommentStateSingle[$key] === "checked") {
839  return true;
840  }
841  }
842  return false;
843  }
844 }
845 $agent = new SpdxTwoAgent();
846 $agent->scheduler_connect();
847 $agent->run_scheduler_event_loop();
848 $agent->scheduler_disconnect(0);
generateFileNodesByFiles($filesWithLicenses, $treeTableName, $uploadId)
For each file, generate the nodes by files.
Definition: spdx2.php:677
heartbeat($newProcessed)
Send hear beat to the scheduler.
Definition: Agent.php:214
static preWorkOnArgsFlp($args, $key1, $key2)
For a given set of arguments assign $args[$key1] and $args[$key2].
Definition: spdx2utils.php:39
addScannerResults(&$filesWithLicenses, ItemTreeBounds $itemTreeBounds)
Attach finding agents to the files and return names of scanners.
Definition: spdx2.php:476
getVerificationCode(Upload $upload)
Get a unique identifier for a given upload.
Definition: spdx2.php:807
generateFileNodes($filesWithLicenses, $treeTableName, $uploadId)
Generate report nodes for files.
Definition: spdx2.php:661
Structure of an Agent with all required parameters.
Definition: Agent.php:51
int jobId
The id of the job.
getLicenseTexts()
Get the license texts from fossology.
Definition: spdx2.php:774
getSPDXReportConf($uploadId, $key)
Get spdx license comment state for a given upload.
Definition: spdx2.php:832
Namespace used by SPDX2 agent.
updateReportTable($uploadId, $jobId, $fileName)
Update the reportgen table with new report path.
Definition: spdx2.php:636
processUploadId($uploadId)
Given an upload ID, process the items in it.
Definition: spdx2.php:202
getTemplateFile($partname)
Get TWIG template file based on output format.
Definition: spdx2.php:232
Wrapper class for license map.
Definition: LicenseMap.php:29
const DEFAULT_OUTPUT_FORMAT
Default output format.
Definition: spdx2.php:87
getFileBasename($packageName)
Generate report basename based on upload name.
Definition: spdx2.php:256
getUri($packageName)
Get the URI for the given package.
Definition: spdx2.php:293
getFileName($packageName)
Get absolute path for report.
Definition: spdx2.php:281
renderString($templateName, $vars)
Render a twig template.
Definition: spdx2.php:649
const UPLOAD_ADDS
Argument for additional uploads.
Definition: spdx2.php:89
static addPrefixOnDemandKeys($licenses, $spdxValidityChecker=null)
Add prefix to license keys.
Definition: spdx2utils.php:87
getFilesWithLicensesFromClearings(ItemTreeBounds $itemTreeBounds)
Given an ItemTreeBounds, get the files with clearings.
Definition: spdx2.php:364
renderPackage($uploadId)
Given an upload id, render the report string.
Definition: spdx2.php:309
static addPrefixOnDemandList($licenses, $spdxValidityChecker=null)
Add prefix to license list.
Definition: spdx2utils.php:102
generateFileNodesByLicenses($filesWithLicenses, $treeTableName)
For each file, generate the nodes by licenses.
Definition: spdx2.php:735
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28
preWorkOnArgs($args)
Parse arguments.
Definition: spdx2.php:184
toLicensesWithFiles(&$filesWithLicenses, $treeTableName)
Map findings to the files.
Definition: spdx2.php:436
const AVAILABLE_OUTPUT_FORMATS
Output formats available.
Definition: spdx2.php:88
writeReport(&$packageNodes, $packageIds, $uploadId)
Write the report the file and update report table.
Definition: spdx2.php:598
if(!defined('ENT_SUBSTITUTE')) convertToUTF8($content, $toHTML=true)
const OUTPUT_FORMAT_KEY
Argument key for output format.
Definition: spdx2.php:86
addClearingStatus(&$filesWithLicenses, ItemTreeBounds $itemTreeBounds)
Add clearing status to the files.
Definition: spdx2.php:560
computeUri($uploadId)
For a given upload, compute the URI and filename for the report.
Definition: spdx2.php:583
static implodeLicenses($licenses, $spdxValidityChecker=null)
Implode licenses with "AND" or "OR".
Definition: spdx2utils.php:116
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:695
materialize()
create temp table
Definition: DbViewProxy.php:51
toLicensesWithFilesAdder(&$filesWithLicenses, $licenses, $copyrights, $file, $fullPath)
Map licenses, copyrights, files and full path to filesWithLicenses array.
Definition: spdx2.php:411