34 include_once(__DIR__ .
"/version.php");
35 include_once(__DIR__ .
"/reportgenerator.php");
45 const UPLOADS_ADD_KEY =
"uploadsAdd";
50 private $additionalUploads = [];
102 private $packageName;
104 function __construct()
107 $args = getopt(
"", array(
108 self::OUTPUT_FORMAT_KEY.
'::',
109 self::UPLOADS_ADD_KEY.
'::'
112 if (array_key_exists(self::OUTPUT_FORMAT_KEY,
$args)) {
118 if (array_key_exists(self::UPLOADS_ADD_KEY,
$args)) {
119 $uploadsString =
$args[self::UPLOADS_ADD_KEY];
120 if (!empty($uploadsString)) {
121 $this->additionalUploads = explode(
',', $uploadsString);
125 parent::__construct(
$agentName, AGENT_VERSION, AGENT_REV);
127 $this->uploadDao = $this->container->get(
'dao.upload');
128 $this->clearingDao = $this->container->get(
'dao.clearing');
129 $this->licenseDao = $this->container->get(
'dao.license');
130 $this->
dbManager = $this->container->get(
'db.manager');
142 $this->licenseMap =
new LicenseMap($this->
dbManager, $this->groupId, LicenseMap::REPORT,
true);
159 if (count($this->additionalUploads) > 0) {
160 $fileName = $fileBase .
"multifile" .
"_" .
strtoupper($this->outputFormat);
162 $fileName = $fileBase.
strtoupper($this->outputFormat).
"_".$this->packageName;
165 return $fileName .
".json" ;
175 $upload = $this->uploadDao->getUpload($uploadId);
176 $this->packageName = $upload->getFilename();
178 $fileBase = $SysConf[
'FOSSOLOGY'][
'path'].
"/report/";
180 $this->uri = $this->
getUri($fileBase);
191 $uploadTreeTableName = $this->uploadDao->getUploadtreeTableName($uploadId);
192 $itemTreeBounds = $this->uploadDao->getParentItemBounds($uploadId, $uploadTreeTableName);
195 $filesWithLicenses = $this->reportutils
196 ->getFilesWithLicensesFromClearings($itemTreeBounds, $this->groupId,
197 $this, $this->licensesInDocument);
200 $this->reportutils->addClearingStatus($filesWithLicenses, $itemTreeBounds, $this->groupId);
203 $this->reportutils->addScannerResults($filesWithLicenses, $itemTreeBounds, $this->groupId, $this->licensesInDocument);
206 $this->reportutils->addCopyrightResults($filesWithLicenses, $uploadId);
209 $upload = $this->uploadDao->getUpload($uploadId);
210 $components = $this->
generateFileComponents($filesWithLicenses, $upload->getTreeTableName(), $uploadId, $itemTreeBounds);
212 $mainLicenseIds = $this->clearingDao->getMainLicenseIds($uploadId, $this->groupId);
213 $mainLicenses = array();
214 foreach ($mainLicenseIds as $licId) {
215 $reportedLicenseId = $this->licenseMap->getProjectedId($licId);
216 $mainLicObj = $this->licenseDao->getLicenseById($reportedLicenseId, $this->groupId);
217 $licId = $mainLicObj->getId() .
"-" . md5($mainLicObj->getText());
218 if (!array_key_exists($licId, $this->licensesInDocument)) {
220 ->setLicenseObj($mainLicObj)
221 ->setCustomText(
false)
222 ->setTextPrinted(
true)
223 ->setListedLicense(
true);
225 $licensedata[
'id'] = $mainLicObj->getSpdxId();
226 $licensedata[
'url'] = $mainLicObj->getUrl();
227 $mainLicenses[] = $this->reportGenerator->createLicense($licensedata);
230 $hashes = $this->uploadDao->getUploadHashes($uploadId);
231 $serializedhash = array();
232 $serializedhash[] = $this->reportGenerator->createHash(
'SHA-1', $hashes[
'sha1']);
233 $serializedhash[] = $this->reportGenerator->createHash(
'MD5', $hashes[
'md5']);
235 if (array_key_exists(
'sha256', $hashes) && !empty($hashes[
'sha256'])) {
236 $serializedhash[] = $this->reportGenerator->createHash(
'SHA-256', $hashes[
'sha256']);
239 $maincomponentData = array (
240 'bomref' => strval($uploadId),
242 'name' => $upload->getFilename(),
243 'hashes' => $serializedhash,
244 'scope' =>
'required',
245 'mimeType' => $this->getMimeType($uploadId),
246 'licenses' => $mainLicenses
248 $maincomponent = $this->reportGenerator->createComponent($maincomponentData);
251 'tool-version' => $SysConf[
'BUILD'][
'VERSION'],
252 'maincomponent' => $maincomponent,
253 'components' => $components
256 return $this->reportGenerator->generateReport($bomdata);
269 $treeDao = $this->container->get(
'dao.tree');
273 $components = array();
274 foreach ($filesWithLicenses as $fileId => $licenses) {
275 $filesProceeded += 1;
276 if (($filesProceeded & 2047) == 0) {
277 $this->
heartbeat($filesProceeded - $lastValue);
278 $lastValue = $filesProceeded;
281 $hashes = $treeDao->getItemHashes($fileId);
282 $serializedhash = array();
283 $serializedhash[] = $this->reportGenerator->createHash(
'SHA-1', $hashes[
'sha1']);
284 $serializedhash[] = $this->reportGenerator->createHash(
'MD5', $hashes[
'md5']);
286 if (array_key_exists(
'sha256', $hashes) && !empty($hashes[
'sha256'])) {
287 $serializedhash[] = $this->reportGenerator->createHash(
'SHA-256', $hashes[
'sha256']);
290 $fileName = $treeDao->getFullPath($fileId, $treeTableName, 0);
293 if (!empty($licenses->getConcludedLicenses())) {
294 foreach ($licenses->getConcludedLicenses() as $licenseId) {
295 if (array_key_exists($licenseId, $this->licensesInDocument)) {
296 $licensedata = array(
297 "id" => $this->licensesInDocument[$licenseId]->getLicenseObj()->getSpdxId(),
298 "name" => $this->licensesInDocument[$licenseId]->getLicenseObj()->getFullName(),
299 "url" => $this->licensesInDocument[$licenseId]->getLicenseObj()->getUrl()
301 $licensesfound[] = $this->reportGenerator->createLicense($licensedata);
305 foreach ($licenses->getScanners() as $licenseId) {
306 if (array_key_exists($licenseId, $this->licensesInDocument)) {
307 $licensedata = array(
308 "id" => $this->licensesInDocument[$licenseId]->getLicenseObj()->getSpdxId(),
309 "name" => $this->licensesInDocument[$licenseId]->getLicenseObj()->getFullName(),
310 "url" => $this->licensesInDocument[$licenseId]->getLicenseObj()->getUrl()
312 $licensesfound[] = $this->reportGenerator->createLicense($licensedata);
316 if (!empty($fileName)) {
317 $componentdata = array(
318 'bomref' => $uploadId .
'-'. $fileId,
321 'hashes' => $serializedhash,
322 'mimeType' =>
'text/plain',
323 'copyright' => implode(
"\n", $licenses->getCopyrights()),
324 'licenses' => $licensesfound
326 $components[] = $this->reportGenerator->createComponent($componentdata);
329 $this->
heartbeat($filesProceeded - $lastValue);
340 $fileBase = dirname($this->uri);
342 if (!is_dir($fileBase)) {
343 mkdir($fileBase, 0777,
true);
347 $contents = json_encode($packageNodes, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
350 $contents = preg_replace(
'/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x9F]/u',
'?',$contents);
351 file_put_contents($this->uri, $contents);
363 $this->reportutils->updateOrInsertReportgenEntry($uploadId,
$jobId, $fileName);
372 $sql =
"SELECT mimetype_name
374 JOIN pfile pf ON u.pfile_fk = pf.pfile_pk
375 JOIN mimetype m ON pf.pfile_mimetypefk = m.mimetype_pk
376 WHERE u.upload_pk = $1";
378 $row = $this->
dbManager->getSingleRow($sql, [$uploadId], __METHOD__);
379 return $row[
'mimetype_name'];
383 $agent =
new CycloneDXAgent();
384 $agent->scheduler_connect();
385 $agent->run_scheduler_event_loop();
386 $agent->scheduler_disconnect(0);
const OUTPUT_FORMAT_KEY
Argument key for output format.
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.
generateFileComponents($filesWithLicenses, $treeTableName, $uploadId, $itemTreeBounds)
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.
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.