34 include_once(__DIR__ .
"/version.php");
35 include_once(__DIR__ .
"/reportgenerator.php");
98 function __construct()
101 $args = getopt(
"", array(self::OUTPUT_FORMAT_KEY.
'::'));
103 if (array_key_exists(self::OUTPUT_FORMAT_KEY,
$args)) {
110 parent::__construct(
$agentName, AGENT_VERSION, AGENT_REV);
112 $this->uploadDao = $this->container->get(
'dao.upload');
113 $this->clearingDao = $this->container->get(
'dao.clearing');
114 $this->licenseDao = $this->container->get(
'dao.license');
115 $this->
dbManager = $this->container->get(
'db.manager');
127 $this->licenseMap =
new LicenseMap($this->
dbManager, $this->groupId, LicenseMap::REPORT,
true);
144 $fileName = $fileBase.
strtoupper($this->outputFormat).
"_".$this->packageName;
145 return $fileName .
".json" ;
155 $upload = $this->uploadDao->getUpload($uploadId);
156 $this->packageName = $upload->getFilename();
158 $fileBase = $SysConf[
'FOSSOLOGY'][
'path'].
"/report/";
160 $this->uri = $this->
getUri($fileBase);
171 $uploadTreeTableName = $this->uploadDao->getUploadtreeTableName($uploadId);
172 $itemTreeBounds = $this->uploadDao->getParentItemBounds($uploadId, $uploadTreeTableName);
175 $filesWithLicenses = $this->reportutils
176 ->getFilesWithLicensesFromClearings($itemTreeBounds, $this->groupId,
177 $this, $this->licensesInDocument);
180 $this->reportutils->addClearingStatus($filesWithLicenses, $itemTreeBounds, $this->groupId);
183 $this->reportutils->addScannerResults($filesWithLicenses, $itemTreeBounds, $this->groupId, $this->licensesInDocument);
186 $this->reportutils->addCopyrightResults($filesWithLicenses, $uploadId);
189 $upload = $this->uploadDao->getUpload($uploadId);
190 $components = $this->
generateFileComponents($filesWithLicenses, $upload->getTreeTableName(), $uploadId, $itemTreeBounds);
192 $mainLicenseIds = $this->clearingDao->getMainLicenseIds($uploadId, $this->groupId);
193 $mainLicenses = array();
194 foreach ($mainLicenseIds as $licId) {
195 $reportedLicenseId = $this->licenseMap->getProjectedId($licId);
196 $mainLicObj = $this->licenseDao->getLicenseById($reportedLicenseId, $this->groupId);
197 $licId = $mainLicObj->getId() .
"-" . md5($mainLicObj->getText());
198 if (!array_key_exists($licId, $this->licensesInDocument)) {
200 ->setLicenseObj($mainLicObj)
201 ->setCustomText(
false)
202 ->setTextPrinted(
true)
203 ->setListedLicense(
true);
205 $licensedata[
'id'] = $mainLicObj->getSpdxId();
206 $licensedata[
'url'] = $mainLicObj->getUrl();
207 $mainLicenses[] = $this->reportGenerator->createLicense($licensedata);
210 $hashes = $this->uploadDao->getUploadHashes($uploadId);
211 $serializedhash = array();
212 $serializedhash[] = $this->reportGenerator->createHash(
'SHA-1', $hashes[
'sha1']);
213 $serializedhash[] = $this->reportGenerator->createHash(
'MD5', $hashes[
'md5']);
215 if (array_key_exists(
'sha256', $hashes) && !empty($hashes[
'sha256'])) {
216 $serializedhash[] = $this->reportGenerator->createHash(
'SHA-256', $hashes[
'sha256']);
219 $maincomponentData = array (
220 'bomref' => strval($uploadId),
222 'name' => $upload->getFilename(),
223 'hashes' => $serializedhash,
224 'scope' =>
'required',
225 'mimeType' => $this->getMimeType($uploadId),
226 'licenses' => $mainLicenses
228 $maincomponent = $this->reportGenerator->createComponent($maincomponentData);
231 'tool-version' => $SysConf[
'BUILD'][
'VERSION'],
232 'maincomponent' => $maincomponent,
233 'components' => $components
236 return $this->reportGenerator->generateReport($bomdata);
249 $treeDao = $this->container->get(
'dao.tree');
253 $components = array();
254 foreach ($filesWithLicenses as $fileId => $licenses) {
255 $filesProceeded += 1;
256 if (($filesProceeded & 2047) == 0) {
257 $this->
heartbeat($filesProceeded - $lastValue);
258 $lastValue = $filesProceeded;
261 $hashes = $treeDao->getItemHashes($fileId);
262 $serializedhash = array();
263 $serializedhash[] = $this->reportGenerator->createHash(
'SHA-1', $hashes[
'sha1']);
264 $serializedhash[] = $this->reportGenerator->createHash(
'MD5', $hashes[
'md5']);
266 if (array_key_exists(
'sha256', $hashes) && !empty($hashes[
'sha256'])) {
267 $serializedhash[] = $this->reportGenerator->createHash(
'SHA-256', $hashes[
'sha256']);
270 $fileName = $treeDao->getFullPath($fileId, $treeTableName, 0);
273 if (!empty($licenses->getConcludedLicenses())) {
274 foreach ($licenses->getConcludedLicenses() as $licenseId) {
275 if (array_key_exists($licenseId, $this->licensesInDocument)) {
276 $licensedata = array(
277 "id" => $this->licensesInDocument[$licenseId]->getLicenseObj()->getSpdxId(),
278 "name" => $this->licensesInDocument[$licenseId]->getLicenseObj()->getFullName(),
279 "url" => $this->licensesInDocument[$licenseId]->getLicenseObj()->getUrl()
281 $licensesfound[] = $this->reportGenerator->createLicense($licensedata);
285 foreach ($licenses->getScanners() as $licenseId) {
286 if (array_key_exists($licenseId, $this->licensesInDocument)) {
287 $licensedata = array(
288 "id" => $this->licensesInDocument[$licenseId]->getLicenseObj()->getSpdxId(),
289 "name" => $this->licensesInDocument[$licenseId]->getLicenseObj()->getFullName(),
290 "url" => $this->licensesInDocument[$licenseId]->getLicenseObj()->getUrl()
292 $licensesfound[] = $this->reportGenerator->createLicense($licensedata);
296 if (!empty($fileName)) {
297 $componentdata = array(
298 'bomref' => $uploadId .
'-'. $fileId,
301 'hashes' => $serializedhash,
302 'mimeType' =>
'text/plain',
303 'copyright' => implode(
"\n", $licenses->getCopyrights()),
304 'licenses' => $licensesfound
306 $components[] = $this->reportGenerator->createComponent($componentdata);
309 $this->
heartbeat($filesProceeded - $lastValue);
320 $fileBase = dirname($this->uri);
322 if (!is_dir($fileBase)) {
323 mkdir($fileBase, 0777,
true);
327 $contents = json_encode($packageNodes, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
330 $contents = preg_replace(
'/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x9F]/u',
'?',$contents);
331 file_put_contents($this->uri, $contents);
343 $this->reportutils->updateOrInsertReportgenEntry($uploadId,
$jobId, $fileName);
352 $sql =
"SELECT mimetype_name
354 JOIN pfile pf ON u.pfile_fk = pf.pfile_pk
355 JOIN mimetype m ON pf.pfile_mimetypefk = m.mimetype_pk
356 WHERE u.upload_pk = $1";
358 $row = $this->
dbManager->getSingleRow($sql, [$uploadId], __METHOD__);
359 return $row[
'mimetype_name'];
363 $agent =
new CycloneDXAgent();
364 $agent->scheduler_connect();
365 $agent->run_scheduler_event_loop();
366 $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.