36 include_once(__DIR__ .
"/version.php");
37 include_once(__DIR__ .
"/reportgenerator.php");
47 const UPLOADS_ADD_KEY =
"uploadsAdd";
52 private $additionalUploads = [];
61 protected $citations = [];
108 private $packageName;
115 function __construct()
118 $args = getopt(
"", array(
119 self::OUTPUT_FORMAT_KEY.
'::',
120 self::UPLOADS_ADD_KEY.
'::'
123 if (array_key_exists(self::OUTPUT_FORMAT_KEY,
$args)) {
129 if (array_key_exists(self::UPLOADS_ADD_KEY,
$args)) {
130 $uploadsString =
$args[self::UPLOADS_ADD_KEY];
131 if (!empty($uploadsString)) {
132 $this->additionalUploads = explode(
',', $uploadsString);
136 parent::__construct(
$agentName, AGENT_VERSION, AGENT_REV);
138 $this->uploadDao = $this->container->get(
'dao.upload');
139 $this->clearingDao = $this->container->get(
'dao.clearing');
140 $this->licenseDao = $this->container->get(
'dao.license');
141 $this->
dbManager = $this->container->get(
'db.manager');
153 $this->licenseMap =
new LicenseMap($this->
dbManager, $this->groupId, LicenseMap::REPORT,
true);
155 $this->settings = $this->uploadDao->getCyclonedxSettings($uploadId);
156 $this->reportGenerator->applySettings($this->settings);
173 if (count($this->additionalUploads) > 0) {
174 $fileName = $fileBase .
"multifile" .
"_" .
strtoupper($this->outputFormat);
176 $fileName = $fileBase.
strtoupper($this->outputFormat).
"_".$this->packageName;
179 return $fileName .
".json" ;
189 $upload = $this->uploadDao->getUpload($uploadId);
190 $this->packageName = $upload->getFilename();
192 $fileBase = $SysConf[
'FOSSOLOGY'][
'path'].
"/report/";
194 $this->uri = $this->
getUri($fileBase);
205 $uploadTreeTableName = $this->uploadDao->getUploadtreeTableName($uploadId);
206 $itemTreeBounds = $this->uploadDao->getParentItemBounds($uploadId, $uploadTreeTableName);
209 $filesWithLicenses = $this->reportutils
210 ->getFilesWithLicensesFromClearings($itemTreeBounds, $this->groupId,
211 $this, $this->licensesInDocument);
214 $this->reportutils->addClearingStatus($filesWithLicenses, $itemTreeBounds, $this->groupId);
217 $this->reportutils->addScannerResults($filesWithLicenses, $itemTreeBounds, $this->groupId, $this->licensesInDocument);
220 $this->reportutils->addCopyrightResults($filesWithLicenses, $uploadId);
225 'timestamp' => date(
'c'),
226 'attributedTo' =>
'tool-fossology-scanners',
230 'timestamp' => date(
'c'),
231 'attributedTo' =>
'person-fossology-analyst',
236 $customLicenseTexts = $this->clearingDao->getMainLicenseReportInfos($uploadId, $this->groupId);
238 $upload = $this->uploadDao->getUpload($uploadId);
239 $components = $this->
generateFileComponents($filesWithLicenses, $upload->getTreeTableName(), $uploadId, $itemTreeBounds, $customLicenseTexts);
241 $mainLicenseIds = $this->clearingDao->getMainLicenseIds($uploadId, $this->groupId);
242 $mainLicenses = array();
243 $seenLicenseIds = array();
244 foreach ($mainLicenseIds as $licId) {
245 $reportedLicenseId = $this->licenseMap->getProjectedId($licId);
246 $mainLicObj = $this->licenseDao->getLicenseById($reportedLicenseId, $this->groupId);
247 if ($mainLicObj ===
null) {
252 $licensedata[
'bom-ref'] =
'lic-clearing-main-' . $licId;
253 $licensedata[
'acknowledgement'] =
'concluded';
254 $this->citations[
'cite-analyst'][
'expressions'][] =
'$..[?(@.bom-ref=="lic-clearing-main-' . $licId .
'")]';
255 $mainLicenses[] = $this->reportGenerator->createLicense($licensedata);
257 $customText = array_key_exists($licId, $customLicenseTexts) ? $customLicenseTexts[$licId] :
null;
258 $licText = !empty($customText) ? $customText : $mainLicObj->getText();
259 $reportLicId = $mainLicObj->getId() .
"-" . md5($licText);
260 $seenLicenseIds[$reportLicId] =
true;
263 foreach ($filesWithLicenses as $fileNode) {
264 $isConcluded = !empty($fileNode->getConcludedLicenses());
265 $licenseIds = $isConcluded
266 ? $fileNode->getConcludedLicenses()
267 : $fileNode->getScanners();
268 foreach ($licenseIds as $licenseId) {
269 if (array_key_exists($licenseId, $this->licensesInDocument) && !array_key_exists($licenseId, $seenLicenseIds)) {
270 $seenLicenseIds[$licenseId] =
true;
271 $licObj = $this->licensesInDocument[$licenseId]->getLicenseObj();
272 $isCustomText = $this->licensesInDocument[$licenseId]->isCustomText();
274 $refPrefix = $isConcluded ?
'clearing' :
'scanner';
275 $licensedata[
'bom-ref'] =
'lic-' . $refPrefix .
'-main-' . $licenseId;
276 $licensedata[
'acknowledgement'] = $isConcluded ?
'concluded' :
'declared';
277 $this->citations[
'cite-' . ($isConcluded ?
'analyst' :
'scanner')][
'expressions'][] =
'$..[?(@.bom-ref=="lic-' . $refPrefix .
'-main-' . $licenseId .
'")]';
278 $mainLicenses[] = $this->reportGenerator->createLicense($licensedata);
283 $hashes = $this->uploadDao->getUploadHashes($uploadId);
284 $serializedhash = array();
285 $serializedhash[] = $this->reportGenerator->createHash(
'SHA-1', $hashes[
'sha1']);
286 $serializedhash[] = $this->reportGenerator->createHash(
'MD5', $hashes[
'md5']);
288 if (array_key_exists(
'sha256', $hashes) && !empty($hashes[
'sha256'])) {
289 $serializedhash[] = $this->reportGenerator->createHash(
'SHA-256', $hashes[
'sha256']);
292 $allCopyrights = array();
293 foreach ($filesWithLicenses as $fileNode) {
294 $fileCopyrights = $fileNode->getCopyrights();
295 if (!empty($fileCopyrights)) {
296 $allCopyrights = array_merge($allCopyrights, $fileCopyrights);
299 $allCopyrights = array_unique($allCopyrights);
301 $reportInfo = $this->uploadDao->getReportInfo($uploadId);
302 $componentVersion = ($reportInfo[
'ri_version'] ??
'');
303 if ($componentVersion ==
'NA') {
304 $componentVersion =
'';
306 $componentId = ($reportInfo[
'ri_component_id'] ??
'');
307 if ($componentId ==
'NA') {
310 $componentType = intval($reportInfo[
'ri_component_type'] ?? 0);
311 $generalAssessment = ($reportInfo[
'ri_general_assesment'] ??
'');
312 if ($generalAssessment ==
'NA') {
313 $generalAssessment =
'';
317 $externalReferences = [];
318 if (!empty($componentId)) {
320 $purl = $componentId;
322 $externalReferences[] = [
323 'type' =>
'distribution',
324 'url' => $componentId
329 $maincomponentData = array (
330 'bomref' => strval($uploadId),
332 'name' => $upload->getFilename(),
333 'version' => $componentVersion,
334 'hashes' => $serializedhash,
335 'scope' =>
'required',
336 'mimeType' => $this->getMimeType($uploadId),
337 'copyright' => implode(
"\n", $allCopyrights),
338 'description' => $generalAssessment,
340 'externalReferences' => $externalReferences,
341 'licenses' => $mainLicenses
343 $maincomponent = $this->reportGenerator->createComponent($maincomponentData);
345 $formattedDate = date(
'Y-m-d\TH:i:s\Z');
347 $finalCitations = [];
348 if (!empty($this->citations[
'cite-scanner'][
'expressions'])) {
349 $finalCitations[] = $this->citations[
'cite-scanner'];
351 if (!empty($this->citations[
'cite-analyst'][
'expressions'])) {
352 $finalCitations[] = $this->citations[
'cite-analyst'];
356 'timestamp' => $formattedDate,
357 'tool-version' => $SysConf[
'BUILD'][
'VERSION'],
358 'maincomponent' => $maincomponent,
359 'components' => $components,
360 'externalReferences' => $externalReferences,
361 'citations' => $finalCitations
364 return $this->reportGenerator->generateReport($bomdata);
374 protected function generateFileComponents($filesWithLicenses, $treeTableName, $uploadId, $itemTreeBounds, $customLicenseTexts = array())
377 $treeDao = $this->container->get(
'dao.tree');
379 $stateLicenseComment = $this->settings->isLicenseCommentEnabled();
380 $stateWoInfos = $this->settings->isIgnoreFilesWithoutInfo();
381 $stateOsselot = $this->settings->isOsselotExport();
385 $components = array();
386 foreach ($filesWithLicenses as $fileId => $licenses) {
387 $filesProceeded += 1;
388 if (($filesProceeded & 2047) == 0) {
389 $this->
heartbeat($filesProceeded - $lastValue);
390 $lastValue = $filesProceeded;
393 if ($stateWoInfos && empty($licenses->getConcludedLicenses()) &&
394 empty($licenses->getScanners()) && empty($licenses->getCopyrights())) {
398 $hashes = $treeDao->getItemHashes($fileId);
399 $serializedhash = array();
400 $serializedhash[] = $this->reportGenerator->createHash(
'SHA-1', $hashes[
'sha1']);
401 $serializedhash[] = $this->reportGenerator->createHash(
'MD5', $hashes[
'md5']);
403 if (array_key_exists(
'sha256', $hashes) && !empty($hashes[
'sha256'])) {
404 $serializedhash[] = $this->reportGenerator->createHash(
'SHA-256', $hashes[
'sha256']);
407 $fileName = $treeDao->getFullPath($fileId, $treeTableName, 0);
410 if (!empty($licenses->getConcludedLicenses())) {
411 foreach ($licenses->getConcludedLicenses() as $licenseId) {
412 if (array_key_exists($licenseId, $this->licensesInDocument)) {
413 $licObj = $this->licensesInDocument[$licenseId]->getLicenseObj();
414 $isCustomText = $this->licensesInDocument[$licenseId]->isCustomText();
416 $licensedata[
'bom-ref'] =
'lic-clearing-' . $licenseId .
'-' . $fileId;
417 $licensedata[
'acknowledgement'] =
'concluded';
418 $licensesfound[] = $this->reportGenerator->createLicense($licensedata,
false);
422 foreach ($licenses->getScanners() as $licenseId) {
423 if (array_key_exists($licenseId, $this->licensesInDocument)) {
424 $licObj = $this->licensesInDocument[$licenseId]->getLicenseObj();
425 $isCustomText = $this->licensesInDocument[$licenseId]->isCustomText();
427 $licensedata[
'bom-ref'] =
'lic-scanner-' . $licenseId .
'-' . $fileId;
428 $licensedata[
'acknowledgement'] =
'declared';
429 $licensesfound[] = $this->reportGenerator->createLicense($licensedata,
false);
433 if (!empty($fileName)) {
435 $componentdata = array(
436 'bomref' => $uploadId .
'-'. $fileId,
439 'hashes' => $serializedhash,
440 'mimeType' => $mimeType,
441 'copyright' => implode(
"\n", $licenses->getCopyrights()),
442 'licenses' => $licensesfound,
443 'acknowledgements' => implode(
"\n", $licenses->getAcknowledgements()),
444 'comments' => $stateLicenseComment ? implode(
"\n", $licenses->getComments()) :
''
446 $components[] = $this->reportGenerator->createComponent($componentdata);
449 $this->
heartbeat($filesProceeded - $lastValue);
460 $fileBase = dirname($this->uri);
462 if (!is_dir($fileBase)) {
463 mkdir($fileBase, 0777,
true);
467 $contents = json_encode($packageNodes, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
470 $contents = preg_replace(
'/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x9F]/u',
'?',$contents);
471 file_put_contents($this->uri, $contents);
483 $this->reportutils->updateOrInsertReportgenEntry($uploadId,
$jobId, $fileName);
497 $customText = array_key_exists($licenseId, $customLicenseTexts) ? $customLicenseTexts[$licenseId] :
null;
498 $licText = !empty($customText) ? $customText : $licObj->getText();
500 $licensedata = array(
501 'url' => $licObj->getUrl()
504 if (!empty($customText) || $isCustomText) {
505 if (!empty($customText)) {
506 $prefix = \Fossology\Lib\Data\LicenseRef::SPDXREF_PREFIX;
507 $licensedata[
'name'] = $prefix . $licObj->getShortName() .
'-' . md5($customText);
509 $licensedata[
'name'] = $licObj->getShortName();
512 $spdxId = $licObj->getSpdxId();
513 if (!empty($spdxId)) {
514 $licensedata[
'id'] = $spdxId;
516 $licensedata[
'name'] = $licObj->getFullName();
520 if ($includeText && !empty($licText)) {
521 $licensedata[
'textContent'] = base64_encode($licText);
522 $licensedata[
'textContentType'] =
'text/plain';
534 $sql =
"SELECT mimetype_name
536 JOIN pfile pf ON u.pfile_fk = pf.pfile_pk
537 JOIN mimetype m ON pf.pfile_mimetypefk = m.mimetype_pk
538 WHERE u.upload_pk = $1";
540 $row = $this->
dbManager->getSingleRow($sql, [$uploadId], __METHOD__);
541 return $row[
'mimetype_name'];
552 $sql =
"SELECT m.mimetype_name
553 FROM $treeTableName ut
554 JOIN pfile pf ON ut.pfile_fk = pf.pfile_pk
555 LEFT JOIN mimetype m ON pf.pfile_mimetypefk = m.mimetype_pk
556 WHERE ut.uploadtree_pk = $1";
558 $row = $this->
dbManager->getSingleRow($sql, [$fileId], __METHOD__);
559 return $row[
'mimetype_name'] ??
'application/octet-stream';
563 $agent =
new CycloneDXAgent();
564 $agent->scheduler_connect();
565 $agent->run_scheduler_event_loop();
566 $agent->scheduler_disconnect(0);
const OUTPUT_FORMAT_KEY
Argument key for output format.
getFileMimeType($fileId, $treeTableName)
Get the mime type of a file.
writeReport($packageNodes, $uploadId)
Write the report the file and update report table.
updateReportTable($uploadId, $jobId, $fileName)
Update the reportgen table with new report path.
renderPackage($uploadId)
Given an upload id, render the report string.
getMimeType($uploadId)
Get the mime type of the upload.
processUploadId($uploadId)
Given an upload ID, process the items in it.
getUri($fileBase)
Get the URI for the given package.
const DEFAULT_OUTPUT_FORMAT
Default output format.
computeUri($uploadId)
For a given upload, compute the URI.
getLicenseDataForCycloneDX($licObj, $licenseId, $customLicenseTexts, $isCustomText=false, $includeText=true)
Helper to create license data array taking custom text into account.
generateFileComponents($filesWithLicenses, $treeTableName, $uploadId, $itemTreeBounds, $customLicenseTexts=array())
Generate the components by files.
Structure of an Agent with all required parameters.
heartbeat($newProcessed)
Send hear beat to the scheduler.
Wrapper class for license map.
CycloneDX export settings of a user (defaults) or of an upload.
char * trim(char *ptext)
Trimming whitespace.
int jobId
The id of the job.
fo_dbManager * dbManager
fo_dbManager object
FUNCTION char * strtoupper(char *s)
Helper function to upper case a string.
Namespace used by CycloneDX agent.