7 namespace Fossology\CliXml;
23 include_once(__DIR__ .
"/version.php");
24 include_once(__DIR__ .
"/services.php");
29 const OUTPUT_FORMAT_KEY =
"outputFormat";
30 const DEFAULT_OUTPUT_FORMAT =
"clixml";
31 const AVAILABLE_OUTPUT_FORMATS =
"xml";
32 const UPLOAD_ADDS =
"uploadsAdd";
41 protected $licenseDao;
50 protected $packageName;
72 protected $outputFormat = self::DEFAULT_OUTPUT_FORMAT;
74 function __construct()
76 parent::__construct(
'clixml', AGENT_VERSION, AGENT_REV);
78 $this->uploadDao = $this->container->get(
'dao.upload');
79 $this->
dbManager = $this->container->get(
'db.manager');
80 $this->licenseDao = $this->container->get(
'dao.license');
81 $this->renderer = $this->container->get(
'twig.environment');
82 $this->renderer->setCache(
false);
95 $this->agentSpecifLongOptions[] = self::UPLOAD_ADDS.
':';
96 $this->agentSpecifLongOptions[] = self::OUTPUT_FORMAT_KEY.
':';
108 $needle =
' --'.$key2.
'=';
109 if (array_key_exists($key1,
$args) && strpos(
$args[$key1],$needle) !==
false) {
110 $exploded = explode($needle,
$args[$key1]);
124 if ((!array_key_exists(self::OUTPUT_FORMAT_KEY,
$args)
125 ||
$args[self::OUTPUT_FORMAT_KEY] ===
"")
126 && array_key_exists(self::UPLOAD_ADDS,
$args)) {
130 if (!array_key_exists(self::UPLOAD_ADDS,
$args) ||
$args[self::UPLOAD_ADDS] ===
"") {
143 if (array_key_exists(self::OUTPUT_FORMAT_KEY,
$args)) {
144 $possibleOutputFormat =
trim(
$args[self::OUTPUT_FORMAT_KEY]);
145 if (in_array($possibleOutputFormat, explode(
',',self::AVAILABLE_OUTPUT_FORMATS))) {
146 $this->outputFormat = $possibleOutputFormat;
149 $this->computeUri($uploadId);
151 $contents = $this->renderPackage($uploadId,
$groupId);
153 $additionalUploadIds = array_key_exists(self::UPLOAD_ADDS,
$args) ? explode(
',',
$args[self::UPLOAD_ADDS]) : array();
154 $packageIds = array($uploadId);
155 foreach ($additionalUploadIds as $additionalId) {
156 $contents .= $this->renderPackage($additionalId,
$groupId);
157 $packageIds[] = $additionalId;
160 $this->writeReport($contents, $packageIds, $uploadId);
164 protected function getTemplateFile($partname)
166 $prefix = $this->outputFormat .
"-";
168 $postfix =
".xml" . $postfix;
169 return $prefix . $partname . $postfix;
172 protected function getUri($fileBase)
174 $fileName = $fileBase.
strtoupper($this->outputFormat).
"_".$this->packageName.
'_'.date(
"Y-m-d_H:i:s");
175 return $fileName .
".xml";
178 protected function renderPackage($uploadId,
$groupId)
182 $otherStatement = $this->otherGetter->getReportData($uploadId);
183 $this->
heartbeat(empty($otherStatement) ? 0 : count($otherStatement));
185 if (!empty($otherStatement[
'ri_clixmlcolumns'])) {
186 $clixmlColumns = json_decode($otherStatement[
'ri_clixmlcolumns'],
true);
188 $clixmlColumns = UploadDao::CLIXML_REPORT_HEADINGS;
191 $licenses = $this->licenseClearedGetter->getCleared($uploadId, $this,
$groupId,
true,
"license",
false);
192 $this->
heartbeat(empty($licenses) ? 0 : count($licenses[
"statements"]));
194 $licensesMain = $this->licenseMainGetter->getCleared($uploadId, $this,
$groupId,
true,
null,
false);
195 $this->
heartbeat(empty($licensesMain) ? 0 : count($licensesMain[
"statements"]));
197 if (array_values($clixmlColumns[
'irrelevantfilesclixml'])[0]) {
198 $licensesIrre = $this->licenseIrrelevantGetter->getCleared($uploadId, $this,
$groupId,
true,
null,
false);
199 $irreComments = $this->licenseIrrelevantGetterComments->getCleared($uploadId, $this,
$groupId,
true,
null,
false);
201 $licensesIrre = array(
"statements" => array());
202 $irreComments = array(
"statements" => array());
204 $this->
heartbeat(empty($licensesIrre) ? 0 : count($licensesIrre[
"statements"]));
205 $this->
heartbeat(empty($irreComments) ? 0 : count($irreComments[
"statements"]));
207 if (array_values($clixmlColumns[
'dnufilesclixml'])[0]) {
208 $licensesDNU = $this->licenseDNUGetter->getCleared($uploadId, $this,
$groupId,
true,
null,
false);
209 $licensesDNUComment = $this->licenseDNUCommentGetter->getCleared($uploadId, $this,
$groupId,
true,
null,
false);
211 $licensesDNU = array(
"statements" => array());
212 $licensesDNUComment = array(
"statements" => array());
214 $this->
heartbeat(empty($licensesDNU) ? 0 : count($licensesDNU[
"statements"]));
215 $this->
heartbeat(empty($licensesDNUComment) ? 0 : count($licensesDNUComment[
"statements"]));
217 if (array_values($clixmlColumns[
'copyrightsclixml'])[0]) {
218 $copyrights = $this->cpClearedGetter->getCleared($uploadId, $this,
$groupId,
true,
"copyright",
false);
220 $copyrights = array(
"statements" => array());
222 $this->
heartbeat(empty($copyrights[
"statements"]) ? 0 : count($copyrights[
"statements"]));
224 if (array_values($clixmlColumns[
'exportrestrictionsclixml'])[0]) {
225 $ecc = $this->eccClearedGetter->getCleared($uploadId, $this,
$groupId,
true,
"ecc",
false);
227 $ecc = array(
"statements" => array());
229 $this->
heartbeat(empty($ecc) ? 0 : count($ecc[
"statements"]));
231 if (array_values($clixmlColumns[
'intellectualPropertyclixml'])[0]) {
232 $ipra = $this->ipraClearedGetter->getCleared($uploadId, $this,
$groupId,
true,
"ipra",
false);
234 $ipra = array(
"statements" => array());
236 $this->
heartbeat(empty($ipra) ? 0 : count($ipra[
"statements"]));
238 if (array_values($clixmlColumns[
'notesclixml'])[0]) {
239 $notes = htmlspecialchars($otherStatement[
'ri_ga_additional'], ENT_DISALLOWED);
244 $countAcknowledgement = 0;
245 $includeAcknowledgements = array_values($clixmlColumns[
'acknowledgementsclixml'])[0];
246 $licenses[
"statements"] = $this->
addLicenseNames($licenses[
"statements"]);
247 $licensesWithAcknowledgement = $this->removeDuplicateAcknowledgements(
248 $licenses[
"statements"], $countAcknowledgement, $includeAcknowledgements);
250 if (array_values($clixmlColumns[
'allobligations'])[0]) {
251 $obligations = $this->obligationsGetter->getObligations(
252 $licenses[
'statements'], $licensesMain[
'statements'], $uploadId,
$groupId)[0];
253 $obligations = array_values($obligations);
255 $obligations = array();
258 if (array_values($clixmlColumns[
'mainlicensesclixml'])[0]) {
259 $mainLicenses = $licensesMain[
"statements"];
261 $mainLicenses = array();
263 $componentHash = $this->uploadDao->getUploadHashes($uploadId);
265 "licensesMain" => $mainLicenses,
266 "licenses" => $licensesWithAcknowledgement,
267 "obligations" => $obligations,
268 "copyrights" => $copyrights[
"statements"],
269 "ecc" => $ecc[
"statements"],
270 "ipra" => $ipra[
"statements"],
271 "licensesIrre" => $licensesIrre[
"statements"],
272 "irreComments" => $irreComments[
"statements"],
273 "licensesDNU" => $licensesDNU[
"statements"],
274 "licensesDNUComment" => $licensesDNUComment[
"statements"],
275 "countAcknowledgement" => $countAcknowledgement
277 $contents = $this->reArrangeMainLic($contents, $includeAcknowledgements);
278 $contents = $this->reArrangeContent($contents);
279 $fileOperations = array(
280 "licensepath" => array_values($clixmlColumns[
'licensepath'])[0],
281 "licensehash" => array_values($clixmlColumns[
'licensehash'])[0],
282 "copyrightpath" => array_values($clixmlColumns[
'copyrightpath'])[0],
283 "copyrighthash" => array_values($clixmlColumns[
'copyrighthash'])[0],
284 "eccpath" => array_values($clixmlColumns[
'eccpath'])[0],
285 "ecchash" => array_values($clixmlColumns[
'ecchash'])[0],
286 "iprapath" => array_values($clixmlColumns[
'iprapath'])[0],
287 "iprahash" => array_values($clixmlColumns[
'iprahash'])[0]
289 list($generalInformation, $assessmentSummary) = $this->
getReportSummary($uploadId);
290 $generalInformation[
'componentHash'] = $componentHash[
'sha1'];
291 return $this->
renderString($this->getTemplateFile(
'file'),array(
292 'documentName' => $this->packageName,
295 'userName' => $this->container->get(
'dao.user')->getUserName($this->userId),
296 'organisation' =>
'',
297 'componentHash' => strtolower($componentHash[
'sha1']),
298 'contents' => $contents,
299 'commentAdditionalNotes' => $notes,
300 'externalIdLink' => htmlspecialchars($otherStatement[
'ri_sw360_link']),
301 'generalInformation' => $generalInformation,
302 'assessmentSummary' => $assessmentSummary,
303 'fileOperations' => $fileOperations
307 protected function removeDuplicateAcknowledgements($licenses, &$countAcknowledgement, $includeAcknowledgements)
309 if (empty($licenses)) {
313 foreach ($licenses as $ackKey => $ackValue) {
314 if (!$includeAcknowledgements) {
315 $licenses[$ackKey][
'acknowledgement'] =
null;
316 }
else if (isset($ackValue[
'acknowledgement'])) {
317 $licenses[$ackKey][
'acknowledgement'] = array_unique(array_filter($ackValue[
'acknowledgement']));
318 $countAcknowledgement += count($licenses[$ackKey][
'acknowledgement']);
324 protected function riskMapping($licenseContent)
326 foreach ($licenseContent as $riskKey => $riskValue) {
327 if (!array_key_exists(
'risk', $riskValue)) {
328 $riskValue[
'risk'] = 0;
330 if ($riskValue[
'risk'] ==
'2' || $riskValue[
'risk'] ==
'3') {
331 $licenseContent[$riskKey][
'risk'] =
'otheryellow';
332 }
else if ($riskValue[
'risk'] ==
'4' || $riskValue[
'risk'] ==
'5') {
333 $licenseContent[$riskKey][
'risk'] =
'otherred';
335 $licenseContent[$riskKey][
'risk'] =
'otherwhite';
338 return $licenseContent;
341 protected function reArrangeMainLic($contents, $includeAcknowledgements)
344 $lenTotalLics = count($contents[
"licenses"]);
346 $lenMainLics = count($contents[
"licensesMain"]);
347 for ($i=0; $i<$lenMainLics; $i++) {
349 for ($j=0; $j<$lenTotalLics; $j++) {
350 if (!strcmp($contents[
"licenses"][$j][
"content"], $contents[
"licensesMain"][$i][
"content"])) {
352 $mainlic[] = $contents[
"licenses"][$j];
353 unset($contents[
"licenses"][$j]);
357 $mainlic[] = $contents[
"licensesMain"][$i];
359 unset($contents[
"licensesMain"][$i]);
361 $contents[
"licensesMain"] = $mainlic;
363 $lenMainLicenses=count($contents[
"licensesMain"]);
364 for ($i=0; $i<$lenMainLicenses; $i++) {
365 $contents[
"licensesMain"][$i][
"contentMain"] = $contents[
"licensesMain"][$i][
"content"];
366 $contents[
"licensesMain"][$i][
"nameMain"] = $contents[
"licensesMain"][$i][
"name"];
367 $contents[
"licensesMain"][$i][
"textMain"] = $contents[
"licensesMain"][$i][
"text"];
368 $contents[
"licensesMain"][$i][
"riskMain"] = $contents[
"licensesMain"][$i][
"risk"];
369 if (array_key_exists(
'acknowledgement', $contents[
"licensesMain"][$i])) {
370 if ($includeAcknowledgements) {
371 $contents[
"licensesMain"][$i][
"acknowledgementMain"] = $contents[
"licensesMain"][$i][
"acknowledgement"];
373 unset($contents[
"licensesMain"][$i][
"acknowledgement"]);
375 unset($contents[
"licensesMain"][$i][
"content"]);
376 unset($contents[
"licensesMain"][$i][
"text"]);
377 unset($contents[
"licensesMain"][$i][
"risk"]);
382 protected function reArrangeContent($contents)
384 $contents[
'licensesMain'] = $this->riskMapping($contents[
'licensesMain']);
385 $contents[
'licenses'] = $this->riskMapping($contents[
'licenses']);
387 $contents[
"obligations"] = array_map(
function($changeKey) {
389 'obliText' => $changeKey[
'text'],
390 'topic' => $changeKey[
'topic'],
391 'license' => $changeKey[
'license']
393 }, $contents[
"obligations"]);
395 $contents[
"copyrights"] = array_map(
function($changeKey) {
396 $content = htmlspecialchars_decode($changeKey[
'content']);
397 $content = str_replace(
"]]>",
"]]>", $content);
398 $comments = htmlspecialchars_decode($changeKey[
'comments']);
399 $comments = str_replace(
"]]>",
"]]>", $comments);
401 'contentCopy' => $content,
402 'comments' => $comments,
403 'files' => $changeKey[
'files'],
404 'hash' => $changeKey[
'hash']
406 }, $contents[
"copyrights"]);
408 $contents[
"ecc"] = array_map(
function($changeKey) {
409 $content = htmlspecialchars_decode($changeKey[
'content']);
410 $content = str_replace(
"]]>",
"]]>", $content);
411 $comments = htmlspecialchars_decode($changeKey[
'comments']);
412 $comments = str_replace(
"]]>",
"]]>", $comments);
414 'contentEcc' => $content,
415 'commentsEcc' => $comments,
416 'files' => $changeKey[
'files'],
417 'hash' => $changeKey[
'hash']
419 }, $contents[
"ecc"]);
421 $contents[
"ipra"] = array_map(
function($changeKey) {
422 $content = htmlspecialchars_decode($changeKey[
'content']);
423 $content = str_replace(
"]]>",
"]]>", $content);
424 $comments = htmlspecialchars_decode($changeKey[
'comments']);
425 $comments = str_replace(
"]]>",
"]]>", $comments);
427 'contentIpra' => $content,
428 'commentsIpra' => $comments,
429 'files' => $changeKey[
'files'],
430 'hash' => $changeKey[
'hash']
432 }, $contents[
"ipra"]);
434 $contents[
"irreComments"] = array_map(
function($changeKey) {
436 'contentIrre' => $changeKey[
'content'],
437 'textIrre' => $changeKey[
'text']
439 }, $contents[
"irreComments"]);
441 $contents[
"licensesIrre"] = array_map(
function($changeKey) {
443 'filesIrre' => $changeKey[
'fullPath']
445 }, $contents[
"licensesIrre"]);
447 $contents[
"licensesDNUComment"] = array_map(
function($changeKey) {
449 'contentDNU' => $changeKey[
'content'],
450 'textDNU' => $changeKey[
'text']
452 }, $contents[
"licensesDNUComment"]);
454 $contents[
"licensesDNU"] = array_map(
function($changeKey) {
456 'filesDNU' => $changeKey[
'fullPath']
458 }, $contents[
"licensesDNU"]);
463 protected function computeUri($uploadId)
466 $upload = $this->uploadDao->getUpload($uploadId);
467 $this->packageName = $upload->getFilename();
469 $fileBase = $SysConf[
'FOSSOLOGY'][
'path'].
"/report/";
471 $this->uri = $this->getUri($fileBase);
474 protected function writeReport($contents, $packageIds, $uploadId)
476 $fileBase = dirname($this->uri);
478 if (!is_dir($fileBase)) {
479 mkdir($fileBase, 0777,
true);
483 $message = $this->
renderString($this->getTemplateFile(
'document'),
484 array(
'content' => $contents));
488 $message = preg_replace(
'/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x9F]/u',
'?',$message);
489 file_put_contents($this->uri, $message);
490 $this->updateReportTable($uploadId, $this->
jobId, $this->uri);
493 protected function updateReportTable($uploadId,
$jobId, $fileName)
495 $this->
dbManager->insertTableRow(
'reportgen',
496 array(
'upload_fk'=>$uploadId,
'job_fk'=>
$jobId,
'filepath'=>$fileName),
507 return $this->renderer->load($templateName)->render($vars);
520 $row = $this->uploadDao->getReportInfo($uploadId);
522 $review = htmlspecialchars($row[
'ri_reviewed']);
523 if ($review ==
'NA') {
527 $dependency =
'None';
530 if (!empty($row[
'ri_ga_checkbox_selection'])) {
531 $listURCheckbox = explode(
',', $row[
'ri_ga_checkbox_selection']);
532 if ($listURCheckbox[0] ==
'checked') {
535 if ($listURCheckbox[1] ==
'checked') {
538 if ($listURCheckbox[2] ==
'checked') {
539 $dependency =
'None';
541 if ($listURCheckbox[3] ==
'checked') {
542 $dependency =
'SourceDependenciesFound';
544 if ($listURCheckbox[4] ==
'checked') {
545 $dependency =
'BinaryDependenciesFound';
547 if ($listURCheckbox[5] ==
'checked') {
550 if ($listURCheckbox[6] ==
'checked') {
553 if ($listURCheckbox[7] ==
'checked') {
556 if ($listURCheckbox[8] ==
'checked') {
560 $componentType = $row[
'ri_component_type'];
561 if (!empty($componentType)) {
566 $componentId = $row[
'ri_component_id'];
567 if (empty($componentId) || $componentId ==
"NA") {
571 $parentItem = $this->uploadDao->getUploadParent($uploadId);
573 $uploadLink = $SysConf[
'SYSCONFIG'][
'FOSSologyURL'];
574 if (substr($uploadLink, 0, 4) !==
"http") {
575 $uploadLink =
"http://" . $uploadLink;
577 $uploadLink .=
"?mod=browse&upload=$uploadId&item=$parentItem";
580 'reportId' => uuid_create(UUID_TYPE_TIME),
581 'reviewedBy' => $review,
582 'componentName' => htmlspecialchars($row[
'ri_component']),
583 'community' => htmlspecialchars($row[
'ri_community']),
584 'version' => htmlspecialchars($row[
'ri_version']),
585 'componentHash' =>
'',
586 'componentReleaseDate' => htmlspecialchars($row[
'ri_release_date']),
587 'linkComponentManagement' => htmlspecialchars($row[
'ri_sw360_link']),
588 'linkScanTool' => $uploadLink,
589 'componentType' => htmlspecialchars($componentType),
590 'componentId' => htmlspecialchars($componentId)
592 'generalAssessment' => $row[
'ri_general_assesment'],
593 'criticalFilesFound' => $critical,
594 'dependencyNotes' => $dependency,
595 'exportRestrictionsFound' => $ecc,
596 'usageRestrictionsFound' => $usage,
597 'additionalNotes' => $row[
'ri_ga_additional']
608 $statementsWithNames = [];
609 foreach ($licenses as $license) {
610 $allLicenseCols = $this->licenseDao->getLicenseById($license[
"licenseId"],
612 $license[
"name"] = $allLicenseCols->getShortName();
613 $statementsWithNames[] = $license;
615 return $statementsWithNames;
619 $agent =
new CliXml();
620 $agent->scheduler_connect();
621 $agent->run_scheduler_event_loop();
622 $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.