7 namespace Fossology\CliXml;
24 include_once(__DIR__ .
"/version.php");
25 include_once(__DIR__ .
"/services.php");
30 const OUTPUT_FORMAT_KEY =
"outputFormat";
31 const DEFAULT_OUTPUT_FORMAT =
"clixml";
32 const AVAILABLE_OUTPUT_FORMATS =
"xml";
33 const UPLOAD_ADDS =
"uploadsAdd";
42 protected $licenseDao;
51 protected $packageName;
78 protected $outputFormat = self::DEFAULT_OUTPUT_FORMAT;
80 function __construct()
82 parent::__construct(
'clixml', AGENT_VERSION, AGENT_REV);
84 $this->uploadDao = $this->container->get(
'dao.upload');
85 $this->
dbManager = $this->container->get(
'db.manager');
86 $this->licenseDao = $this->container->get(
'dao.license');
87 $this->renderer = $this->container->get(
'twig.environment');
88 $this->renderer->setCache(
false);
102 $this->agentSpecifLongOptions[] = self::UPLOAD_ADDS.
':';
103 $this->agentSpecifLongOptions[] = self::OUTPUT_FORMAT_KEY.
':';
115 $needle =
' --'.$key2.
'=';
116 if (array_key_exists($key1,
$args) && strpos(
$args[$key1],$needle) !==
false) {
117 $exploded = explode($needle,
$args[$key1]);
131 if ((!array_key_exists(self::OUTPUT_FORMAT_KEY,
$args)
132 ||
$args[self::OUTPUT_FORMAT_KEY] ===
"")
133 && array_key_exists(self::UPLOAD_ADDS,
$args)) {
137 if (!array_key_exists(self::UPLOAD_ADDS,
$args) ||
$args[self::UPLOAD_ADDS] ===
"") {
150 if (array_key_exists(self::OUTPUT_FORMAT_KEY,
$args)) {
151 $possibleOutputFormat =
trim(
$args[self::OUTPUT_FORMAT_KEY]);
152 if (in_array($possibleOutputFormat, explode(
',',self::AVAILABLE_OUTPUT_FORMATS))) {
153 $this->outputFormat = $possibleOutputFormat;
156 $this->computeUri($uploadId);
158 $contents = $this->renderPackage($uploadId,
$groupId);
160 $additionalUploadIds = array_key_exists(self::UPLOAD_ADDS,
$args) ? explode(
',',
$args[self::UPLOAD_ADDS]) : array();
161 $packageIds = array($uploadId);
162 foreach ($additionalUploadIds as $additionalId) {
163 $contents .= $this->renderPackage($additionalId,
$groupId);
164 $packageIds[] = $additionalId;
167 $this->writeReport($contents, $packageIds, $uploadId);
171 protected function getTemplateFile($partname)
173 $prefix = $this->outputFormat .
"-";
175 $postfix =
".xml" . $postfix;
176 return $prefix . $partname . $postfix;
179 protected function getUri($fileBase)
181 $fileName = $fileBase.
strtoupper($this->outputFormat).
"_".$this->packageName;
182 return $fileName .
".xml";
185 protected function renderPackage($uploadId,
$groupId)
189 $otherStatement = $this->otherGetter->getReportData($uploadId);
190 $this->
heartbeat(empty($otherStatement) ? 0 : count($otherStatement));
192 if (!empty($otherStatement[
'ri_clixmlcolumns'])) {
193 $clixmlColumns = json_decode($otherStatement[
'ri_clixmlcolumns'],
true);
195 $clixmlColumns = UploadDao::CLIXML_REPORT_HEADINGS;
198 $licenses = $this->licenseClearedGetter->getCleared($uploadId, $this,
$groupId,
true,
"license",
false);
199 $this->
heartbeat(empty($licenses) ? 0 : count($licenses[
"statements"]));
201 $licensesMain = $this->licenseMainGetter->getCleared($uploadId, $this,
$groupId,
true,
null,
false);
202 $this->
heartbeat(empty($licensesMain) ? 0 : count($licensesMain[
"statements"]));
204 if (array_values($clixmlColumns[
'irrelevantfilesclixml'])[0]) {
205 $licensesIrre = $this->licenseIrrelevantGetter->getCleared($uploadId, $this,
$groupId,
true,
null,
false);
206 $irreComments = $this->licenseIrrelevantGetterComments->getCleared($uploadId, $this,
$groupId,
true,
null,
false);
208 $licensesIrre = array(
"statements" => array());
209 $irreComments = array(
"statements" => array());
211 $this->
heartbeat(empty($licensesIrre) ? 0 : count($licensesIrre[
"statements"]));
212 $this->
heartbeat(empty($irreComments) ? 0 : count($irreComments[
"statements"]));
214 if (array_values($clixmlColumns[
'dnufilesclixml'])[0]) {
215 $licensesDNU = $this->licenseDNUGetter->getCleared($uploadId, $this,
$groupId,
true,
null,
false);
216 $licensesDNUComment = $this->licenseDNUCommentGetter->getCleared($uploadId, $this,
$groupId,
true,
null,
false);
218 $licensesDNU = array(
"statements" => array());
219 $licensesDNUComment = array(
"statements" => array());
221 $this->
heartbeat(empty($licensesDNU) ? 0 : count($licensesDNU[
"statements"]));
222 $this->
heartbeat(empty($licensesDNUComment) ? 0 : count($licensesDNUComment[
"statements"]));
224 if (array_values($clixmlColumns[
'copyrightsclixml'])[0]) {
225 $copyrights = $this->cpClearedGetter->getCleared($uploadId, $this,
$groupId,
true,
"copyright",
false);
227 $copyrights = array(
"statements" => array());
229 $this->
heartbeat(empty($copyrights[
"statements"]) ? 0 : count($copyrights[
"statements"]));
231 if (array_values($clixmlColumns[
'exportrestrictionsclixml'])[0]) {
232 $ecc = $this->eccClearedGetter->getCleared($uploadId, $this,
$groupId,
true,
"ecc",
false);
234 $ecc = array(
"statements" => array());
236 $this->
heartbeat(empty($ecc) ? 0 : count($ecc[
"statements"]));
238 if (array_values($clixmlColumns[
'intellectualPropertyclixml'])[0]) {
239 $ipra = $this->ipraClearedGetter->getCleared($uploadId, $this,
$groupId,
true,
"ipra",
false);
241 $ipra = array(
"statements" => array());
243 $this->
heartbeat(empty($ipra) ? 0 : count($ipra[
"statements"]));
245 if (array_values($clixmlColumns[
'notesclixml'])[0]) {
246 $notes = htmlspecialchars($otherStatement[
'ri_ga_additional'], ENT_DISALLOWED);
251 $countAcknowledgement = 0;
252 $includeAcknowledgements = array_values($clixmlColumns[
'acknowledgementsclixml'])[0];
253 $licenses[
"statements"] = $this->
addLicenseNames($licenses[
"statements"]);
254 $licensesWithAcknowledgement = $this->removeDuplicateAcknowledgements(
255 $licenses[
"statements"], $countAcknowledgement, $includeAcknowledgements);
257 if (array_values($clixmlColumns[
'allobligations'])[0]) {
258 $obligations = $this->obligationsGetter->getObligations(
259 $licenses[
'statements'], $licensesMain[
'statements'], $uploadId,
$groupId)[0];
260 $obligations = array_values($obligations);
262 $obligations = array();
265 if (array_values($clixmlColumns[
'mainlicensesclixml'])[0]) {
266 $mainLicenses = $licensesMain[
"statements"];
268 $mainLicenses = array();
270 $componentHash = $this->uploadDao->getUploadHashes($uploadId);
272 "licensesMain" => $mainLicenses,
273 "licenses" => $licensesWithAcknowledgement,
274 "obligations" => $obligations,
275 "copyrights" => $copyrights[
"statements"],
276 "ecc" => $ecc[
"statements"],
277 "ipra" => $ipra[
"statements"],
278 "licensesIrre" => $licensesIrre[
"statements"],
279 "irreComments" => $irreComments[
"statements"],
280 "licensesDNU" => $licensesDNU[
"statements"],
281 "licensesDNUComment" => $licensesDNUComment[
"statements"],
282 "countAcknowledgement" => $countAcknowledgement
284 $contents = $this->reArrangeMainLic($contents, $includeAcknowledgements);
285 $contents = $this->reArrangeContent($contents);
286 $fileOperations = array(
287 "licensepath" => array_values($clixmlColumns[
'licensepath'])[0],
288 "licensehash" => array_values($clixmlColumns[
'licensehash'])[0],
289 "copyrightpath" => array_values($clixmlColumns[
'copyrightpath'])[0],
290 "copyrighthash" => array_values($clixmlColumns[
'copyrighthash'])[0],
291 "eccpath" => array_values($clixmlColumns[
'eccpath'])[0],
292 "ecchash" => array_values($clixmlColumns[
'ecchash'])[0],
293 "iprapath" => array_values($clixmlColumns[
'iprapath'])[0],
294 "iprahash" => array_values($clixmlColumns[
'iprahash'])[0]
296 list($generalInformation, $assessmentSummary) = $this->
getReportSummary($uploadId);
297 $generalInformation[
'componentHash'] = $componentHash[
'sha1'];
298 return $this->
renderString($this->getTemplateFile(
'file'),array(
299 'documentName' => $this->packageName,
302 'userName' => $this->container->get(
'dao.user')->getUserName($this->userId),
303 'organisation' =>
'',
304 'componentHash' => strtolower($componentHash[
'sha1']),
305 'contents' => $contents,
306 'commentAdditionalNotes' => $notes,
307 'externalIdLink' => htmlspecialchars($otherStatement[
'ri_sw360_link']),
308 'generalInformation' => $generalInformation,
309 'assessmentSummary' => $assessmentSummary,
310 'fileOperations' => $fileOperations
314 protected function removeDuplicateAcknowledgements($licenses, &$countAcknowledgement, $includeAcknowledgements)
316 if (empty($licenses)) {
320 foreach ($licenses as $ackKey => $ackValue) {
321 if (!$includeAcknowledgements) {
322 $licenses[$ackKey][
'acknowledgement'] =
null;
323 }
else if (isset($ackValue[
'acknowledgement'])) {
324 $licenses[$ackKey][
'acknowledgement'] = array_unique(array_filter($ackValue[
'acknowledgement']));
325 $countAcknowledgement += count($licenses[$ackKey][
'acknowledgement']);
331 protected function riskMapping($licenseContent)
333 foreach ($licenseContent as $riskKey => $riskValue) {
334 if (!array_key_exists(
'risk', $riskValue)) {
335 $riskValue[
'risk'] = 0;
337 if ($riskValue[
'risk'] ==
'2' || $riskValue[
'risk'] ==
'3') {
338 $licenseContent[$riskKey][
'risk'] =
'otheryellow';
339 }
else if ($riskValue[
'risk'] ==
'4' || $riskValue[
'risk'] ==
'5') {
340 $licenseContent[$riskKey][
'risk'] =
'otherred';
342 $licenseContent[$riskKey][
'risk'] =
'otherwhite';
345 return $licenseContent;
348 protected function reArrangeMainLic($contents, $includeAcknowledgements)
351 $lenTotalLics = count($contents[
"licenses"]);
353 $lenMainLics = count($contents[
"licensesMain"]);
354 for ($i=0; $i<$lenMainLics; $i++) {
356 for ($j=0; $j<$lenTotalLics; $j++) {
357 if (!strcmp($contents[
"licenses"][$j][
"content"], $contents[
"licensesMain"][$i][
"content"])) {
359 $mainlic[] = $contents[
"licenses"][$j];
360 unset($contents[
"licenses"][$j]);
364 $mainlic[] = $contents[
"licensesMain"][$i];
366 unset($contents[
"licensesMain"][$i]);
368 $contents[
"licensesMain"] = $mainlic;
370 $lenMainLicenses=count($contents[
"licensesMain"]);
371 for ($i=0; $i<$lenMainLicenses; $i++) {
372 $contents[
"licensesMain"][$i][
"contentMain"] = $contents[
"licensesMain"][$i][
"content"];
373 $contents[
"licensesMain"][$i][
"nameMain"] = $contents[
"licensesMain"][$i][
"name"];
374 $contents[
"licensesMain"][$i][
"textMain"] = $contents[
"licensesMain"][$i][
"text"];
375 $contents[
"licensesMain"][$i][
"riskMain"] = $contents[
"licensesMain"][$i][
"risk"];
376 if (array_key_exists(
'acknowledgement', $contents[
"licensesMain"][$i])) {
377 if ($includeAcknowledgements) {
378 $contents[
"licensesMain"][$i][
"acknowledgementMain"] = $contents[
"licensesMain"][$i][
"acknowledgement"];
380 unset($contents[
"licensesMain"][$i][
"acknowledgement"]);
382 unset($contents[
"licensesMain"][$i][
"content"]);
383 unset($contents[
"licensesMain"][$i][
"text"]);
384 unset($contents[
"licensesMain"][$i][
"risk"]);
389 protected function reArrangeContent($contents)
391 $contents[
'licensesMain'] = $this->riskMapping($contents[
'licensesMain']);
392 $contents[
'licenses'] = $this->riskMapping($contents[
'licenses']);
394 $contents[
"obligations"] = array_map(
function($changeKey) {
396 'obliText' => $changeKey[
'text'],
397 'topic' => $changeKey[
'topic'],
398 'license' => $changeKey[
'license']
400 }, $contents[
"obligations"]);
402 $contents[
"copyrights"] = array_map(
function($changeKey) {
403 $content = htmlspecialchars_decode($changeKey[
'content']);
404 $content = str_replace(
"]]>",
"]]>", $content);
405 $comments = htmlspecialchars_decode($changeKey[
'comments']);
406 $comments = str_replace(
"]]>",
"]]>", $comments);
408 'contentCopy' => $content,
409 'comments' => $comments,
410 'files' => $changeKey[
'files'],
411 'hash' => $changeKey[
'hash']
413 }, $contents[
"copyrights"]);
415 $contents[
"ecc"] = array_map(
function($changeKey) {
416 $content = htmlspecialchars_decode($changeKey[
'content']);
417 $content = str_replace(
"]]>",
"]]>", $content);
418 $comments = htmlspecialchars_decode($changeKey[
'comments']);
419 $comments = str_replace(
"]]>",
"]]>", $comments);
421 'contentEcc' => $content,
422 'commentsEcc' => $comments,
423 'files' => $changeKey[
'files'],
424 'hash' => $changeKey[
'hash']
426 }, $contents[
"ecc"]);
428 $contents[
"ipra"] = array_map(
function($changeKey) {
429 $content = htmlspecialchars_decode($changeKey[
'content']);
430 $content = str_replace(
"]]>",
"]]>", $content);
431 $comments = htmlspecialchars_decode($changeKey[
'comments']);
432 $comments = str_replace(
"]]>",
"]]>", $comments);
434 'contentIpra' => $content,
435 'commentsIpra' => $comments,
436 'files' => $changeKey[
'files'],
437 'hash' => $changeKey[
'hash']
439 }, $contents[
"ipra"]);
441 $contents[
"irreComments"] = array_map(
function($changeKey) {
443 'contentIrre' => $changeKey[
'content'],
444 'textIrre' => $changeKey[
'text']
446 }, $contents[
"irreComments"]);
448 $contents[
"licensesIrre"] = array_map(
function($changeKey) {
450 'filesIrre' => $changeKey[
'fullPath']
452 }, $contents[
"licensesIrre"]);
454 $contents[
"licensesDNUComment"] = array_map(
function($changeKey) {
456 'contentDNU' => $changeKey[
'content'],
457 'textDNU' => $changeKey[
'text']
459 }, $contents[
"licensesDNUComment"]);
461 $contents[
"licensesDNU"] = array_map(
function($changeKey) {
463 'filesDNU' => $changeKey[
'fullPath']
465 }, $contents[
"licensesDNU"]);
470 protected function computeUri($uploadId)
473 $upload = $this->uploadDao->getUpload($uploadId);
474 $this->packageName = $upload->getFilename();
476 $fileBase = $SysConf[
'FOSSOLOGY'][
'path'].
"/report/";
478 $this->uri = $this->getUri($fileBase);
481 protected function writeReport($contents, $packageIds, $uploadId)
483 $fileBase = dirname($this->uri);
485 if (!is_dir($fileBase)) {
486 mkdir($fileBase, 0777,
true);
490 $message = $this->
renderString($this->getTemplateFile(
'document'),
491 array(
'content' => $contents));
495 $message = preg_replace(
'/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x9F]/u',
'?',$message);
496 file_put_contents($this->uri, $message);
497 $this->updateReportTable($uploadId, $this->
jobId, $this->uri);
500 protected function updateReportTable($uploadId,
$jobId, $fileName)
502 $this->reportutils->updateOrInsertReportgenEntry($uploadId,
$jobId, $fileName);
512 return $this->renderer->load($templateName)->render($vars);
525 $row = $this->uploadDao->getReportInfo($uploadId);
527 $review = htmlspecialchars($row[
'ri_reviewed']);
528 if ($review ==
'NA') {
532 $dependency =
'None';
535 if (!empty($row[
'ri_ga_checkbox_selection'])) {
536 $listURCheckbox = explode(
',', $row[
'ri_ga_checkbox_selection']);
537 if ($listURCheckbox[0] ==
'checked') {
540 if ($listURCheckbox[1] ==
'checked') {
543 if ($listURCheckbox[2] ==
'checked') {
544 $dependency =
'None';
546 if ($listURCheckbox[3] ==
'checked') {
547 $dependency =
'SourceDependenciesFound';
549 if ($listURCheckbox[4] ==
'checked') {
550 $dependency =
'BinaryDependenciesFound';
552 if ($listURCheckbox[5] ==
'checked') {
555 if ($listURCheckbox[6] ==
'checked') {
558 if ($listURCheckbox[7] ==
'checked') {
561 if ($listURCheckbox[8] ==
'checked') {
565 $componentType = $row[
'ri_component_type'];
566 if (!empty($componentType)) {
571 $componentId = $row[
'ri_component_id'];
572 if (empty($componentId) || $componentId ==
"NA") {
576 $parentItem = $this->uploadDao->getUploadParent($uploadId);
578 $uploadLink = $SysConf[
'SYSCONFIG'][
'FOSSologyURL'];
579 if (substr($uploadLink, 0, 4) !==
"http") {
580 $uploadLink =
"http://" . $uploadLink;
582 $uploadLink .=
"?mod=browse&upload=$uploadId&item=$parentItem";
585 'reportId' => uuid_create(UUID_TYPE_TIME),
586 'reviewedBy' => $review,
587 'componentName' => htmlspecialchars($row[
'ri_component']),
588 'community' => htmlspecialchars($row[
'ri_community']),
589 'version' => htmlspecialchars($row[
'ri_version']),
590 'componentHash' =>
'',
591 'componentReleaseDate' => htmlspecialchars($row[
'ri_release_date']),
592 'linkComponentManagement' => htmlspecialchars($row[
'ri_sw360_link']),
593 'linkScanTool' => $uploadLink,
594 'componentType' => htmlspecialchars($componentType),
595 'componentId' => htmlspecialchars($componentId)
597 'generalAssessment' => $row[
'ri_general_assesment'],
598 'criticalFilesFound' => $critical,
599 'dependencyNotes' => $dependency,
600 'exportRestrictionsFound' => $ecc,
601 'usageRestrictionsFound' => $usage,
602 'additionalNotes' => $row[
'ri_ga_additional']
613 $statementsWithNames = [];
614 foreach ($licenses as $license) {
615 $allLicenseCols = $this->licenseDao->getLicenseById($license[
"licenseId"],
617 $license[
"name"] = $allLicenseCols->getShortName();
618 $statementsWithNames[] = $license;
620 return $statementsWithNames;
624 $agent =
new CliXml();
625 $agent->scheduler_connect();
626 $agent->run_scheduler_event_loop();
627 $agent->scheduler_disconnect(0);
addLicenseNames($licenses)
preWorkOnArgsFlp($args, $key1, $key2)
getReportSummary($uploadId)
renderString($templateName, $vars)
processUploadId($uploadId)
Given an upload ID, process the items in it.
Structure of an Agent with all required parameters.
heartbeat($newProcessed)
Send hear beat to the scheduler.
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.