24 use PhpOffice\PhpSpreadsheet\Spreadsheet;
25 use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
26 use Symfony\Component\HttpFoundation\Response;
68 $this->Name =
"export-list";
69 $this->Title = _(
"Export Lists");
70 $this->Dependency = array(
"browse");
74 parent::__construct();
75 $this->uploadDao = $GLOBALS[
'container']->get(
'dao.upload');
76 $this->licenseDao = $GLOBALS[
'container']->get(
'dao.license');
77 $this->clearingDao = $GLOBALS[
'container']->get(
'dao.clearing');
78 $this->copyrightDao = $GLOBALS[
'container']->get(
'dao.copyright');
79 $this->treeDao = $GLOBALS[
'container']->get(
'dao.tree');
80 $this->clearingFilter = $GLOBALS[
'container']->get(
'businessrules.clearing_decision_filter');
114 $MenuDisplayString = _(
"Export List");
117 if (empty($Item) || empty($Upload)) {
123 menu_insert(
"Browse::$MenuDisplayString", 1, $URI, $MenuDisplayString);
141 $agents = array_keys(AgentRef::AGENT_LIST);
142 $agent_pks = array();
144 foreach ($agents as $agent) {
147 $AgentRec = AgentARSList($agent.
"_ars", $upload_pk, 1);
148 if (!empty($AgentRec)) {
149 $agent_pks[$agent] = $AgentRec[0][
"agent_fk"];
151 $agent_pks[$agent] =
false;
170 public function createListOfLines($uploadtreeTablename, $uploadtree_pk,
171 $agent_pks, $NomostListNum, $includeSubfolder, $exclude, $ignore)
173 $licensesPerFileName = array();
175 $itemTreeBounds = $this->uploadDao->getItemTreeBounds($uploadtree_pk,
176 $uploadtreeTablename);
177 $allDecisions = $this->clearingDao->getFileClearingsFolder($itemTreeBounds,
179 $editedMappedLicenses = $this->clearingFilter->filterCurrentClearingDecisionsForLicenseList($allDecisions);
180 $licensesPerFileName = $this->licenseDao->getLicensesPerFileNameForAgentId($itemTreeBounds,
181 $agent_pks, $includeSubfolder, $exclude, $ignore, $editedMappedLicenses,
186 foreach ($licensesPerFileName as $fileName => $licenseNames) {
187 if ($licenseNames !==
false && count($licenseNames) > 0) {
188 if ($NomostListNum > -1 && ++$currentNum > $NomostListNum) {
189 $lines[
"warn"] = _(
"<br><b>Warning: Only the first $NomostListNum " .
190 "lines are displayed. To see the whole list, run " .
191 "fo_nomos_license_list from the command line.</b><br>");
197 $row[
'filePath'] = $fileName;
198 if (array_key_exists(
'scanResults', $licenseNames)) {
199 $row[
'agentFindings'] = $licenseNames[
'scanResults'];
201 $row[
'agentFindings'] =
null;
203 $row[
'conclusions'] =
null;
204 $row[
'uploadtree_pk'] = $licenseNames[
'uploadtree_pk'][0];
205 if (array_key_exists(
'concludedResults', $licenseNames) && !empty($licenseNames[
'concludedResults'])) {
210 if (!$ignore && $licenseNames ===
false) {
212 $row[
'filePath'] = $fileName;
213 $row[
'agentFindings'] =
null;
214 $row[
'conclusions'] =
null;
227 return "ui-export-list.html.twig";
239 echo _(
"NO DB connection");
242 if ($this->
State != PLUGIN_STATE_READY) {
246 if (empty($uploadtree_pk)) {
251 if (empty($upload_pk)) {
254 if (!$this->uploadDao->isAccessible($upload_pk, Auth::getGroupId())) {
255 $text = _(
"Permission Denied");
256 return "<h2>$text</h2>";
262 if (!empty($exportCopyright) && $exportCopyright ==
"yes") {
263 $exportCopyright =
true;
265 $formVars[
"export_copy"] =
"1";
266 if ($copyrightType ==
"all") {
267 $formVars[
"copy_type_all"] = 1;
269 $formVars[
"copy_type_nolic"] = 1;
272 $exportCopyright =
false;
274 $agent_pks = array();
275 foreach ($agent_pks_dict as $agent_name => $agent_pk) {
276 if ($agent_pk ===
false) {
277 $warnings[] = _(
"No information for agent: $agent_name");
279 $agent_pks[] = $agent_pk;
280 $formVars[
"agentToInclude_".$agent_name] =
"1";
286 if (!(array_key_exists(
'copy_type_all', $formVars) ||
287 array_key_exists(
'copy_type_nolic', $formVars))) {
288 $formVars[
"copy_type_all"] = 1;
292 $formVars[
"dltext"] = $dltext;
294 $formVars[
"dlspreadsheet"] = $dlspreadsheet;
295 if (!$dltext&&!$dlspreadsheet) {
296 $formVars[
"noDownload"] =
true;
298 $formVars[
"noDownload"] =
false;
301 $NomostListNum = @$SysConf[
'SYSCONFIG'][
'NomostListNum'];
302 $formVars[
"NomostListNum"] = $NomostListNum;
305 $formVars[
"includeSubfolder"] = $includeSubfolder;
308 $formVars[
"showContainers"] = !$ignore;
310 $formVars[
"exclude"] = $exclude;
313 $formVars[
"perFile"] = $consolidateLicenses;
315 $formVars[
"perDirectory"] = $consolidatePerDirectory;
316 if (!$consolidateLicenses&&!$consolidatePerDirectory) {
317 $formVars[
"rawResult"] =
true;
319 $formVars[
"rawResult"] =
false;
322 $this->vars = array_merge($this->vars, $formVars);
324 if ($exportCopyright) {
326 $uploadtreeTablename, $NomostListNum, $exclude, $copyrightType);
328 $lines = $this->createListOfLines($uploadtreeTablename, $uploadtree_pk,
329 $agent_pks, $NomostListNum, $includeSubfolder, $exclude, $ignore);
332 $this->vars[
'warnings'] = array();
333 if (array_key_exists(
"warn",$lines)) {
334 $warnings[] = $lines[
"warn"];
335 unset($lines[
"warn"]);
337 foreach ($warnings as $warning) {
338 $this->vars[
'warnings'][] =
"<br><b>$warning</b><br>";
341 $this->vars[
'warnings'][] =
"<br /><b>Result empty</b><br />";
344 if ($consolidateLicenses||$consolidatePerDirectory) {
347 if ($consolidatePerDirectory) {
352 return $this->
printCSV($lines, $uploadtreeTablename, $exportCopyright);
353 } elseif ($dlspreadsheet) {
356 $this->vars[
'listoutput'] = $this->
printLines($lines, $exportCopyright);
374 public function getCopyrights($uploadId, $uploadtree_pk, $uploadTreeTableName,
375 $NomostListNum, $exclude, $copyrightType =
"all")
377 $agentName = array(
'copyright',
'reso');
378 $scanJobProxy =
new ScanJobProxy($GLOBALS[
'container']->
get(
'dao.agent'),
380 $scanJobProxy->createAgentStatus($agentName);
381 $selectedScanners = $scanJobProxy->getLatestSuccessfulAgentIds();
382 if (!array_key_exists($agentName[0], $selectedScanners)) {
385 $latestAgentIds[] = $selectedScanners[$agentName[0]];
386 if (array_key_exists($agentName[1], $selectedScanners)) {
387 $latestAgentIds[] = $selectedScanners[$agentName[1]];
389 $ids = implode(
',', $latestAgentIds);
390 $agentFilter =
' AND C.agent_fk IN ('.$ids.
')';
392 $itemTreeBounds = $this->uploadDao->getItemTreeBounds($uploadtree_pk,
393 $uploadTreeTableName);
394 $extrawhere =
"UT.lft BETWEEN " . $itemTreeBounds->getLeft() .
" AND " .
395 $itemTreeBounds->getRight();
396 if (! empty($exclude)) {
397 $extrawhere .=
" AND UT.ufile_name NOT LIKE '%$exclude%'";
400 $uploadtreePkToFilePath = [];
402 $copyrights = $this->copyrightDao->getScannerEntries($agentName[0],
403 $uploadTreeTableName, $uploadId,
null, $extrawhere . $agentFilter);
405 $NomostListNum, $uploadTreeTableName,
"content");
407 $copyrights = $this->copyrightDao->getEditedEntries(
'copyright_decision',
408 $uploadTreeTableName, $uploadId, [], $extrawhere);
410 $NomostListNum, $uploadTreeTableName,
"textfinding");
412 if ($copyrightType !=
"all") {
414 foreach (AgentRef::AGENT_LIST as $agentname => $value) {
415 $AgentRec = AgentARSList($agentname.
"_ars", $uploadId, 1);
416 if (!empty($AgentRec)) {
417 $agentList[] = $AgentRec[0][
"agent_fk"];
421 $itemTreeBounds, $agentList, $exclude);
436 $newCopyrights, $NomostListNum, $uploadTreeTableName, $key)
438 foreach ($newCopyrights as $copyright) {
439 if ($NomostListNum > -1 && count($list) >= $NomostListNum) {
440 $list[
"warn"] = _(
"<br><b>Warning: Only the first $NomostListNum lines
441 are displayed. To see the whole list, run fo_nomos_license_list from the
442 command line.</b><br>");
446 $row[
"content"] = $copyright[$key];
447 $row[
"filePath"] = $this->treeDao->getFullPath($copyright[
"uploadtree_pk"],
448 $uploadTreeTableName);
449 $uploadtreePkToFilePath[$copyright[
"uploadtree_pk"]] = $row[
"filePath"];
450 $list[$row[
"filePath"]][] = $row;
464 $uploadtreePkToFilePath, $itemTreeBounds, $agentList, $exclude)
466 $licensesPerFileName = array();
467 $allDecisions = $this->clearingDao->getFileClearingsFolder($itemTreeBounds,
469 $editedMappedLicenses = $this->clearingFilter->filterCurrentClearingDecisionsForCopyrightList(
471 $licensesPerFileName = $this->licenseDao->getLicensesPerFileNameForAgentId(
472 $itemTreeBounds, $agentList,
true, $exclude,
true, $editedMappedLicenses,
474 foreach ($licensesPerFileName as $licenseNames) {
475 if ($licenseNames !==
false && count($licenseNames) > 0) {
476 $shouldRemove =
false;
477 if (array_key_exists(
'concludedResults', $licenseNames)) {
479 if (in_array(
"Void", $conclusions)) {
484 $shouldRemove =
true;
486 if ((! empty($licenseNames[
'scanResults'])) &&
487 ! (in_array(
"No_license_found", $licenseNames[
'scanResults']) ||
488 in_array(
"Void", $licenseNames[
'scanResults']))) {
489 $shouldRemove =
true;
493 $licenseNames[
'uploadtree_pk']);
506 $consolidatedConclusions = array();
507 foreach ($conclusions as $conclusion) {
508 $consolidatedConclusions = array_merge($consolidatedConclusions,
511 return array_unique($consolidatedConclusions);
522 $uploadtreePkToFilePath, $uploadtreePks)
524 foreach ($uploadtreePks as $uploadtreePk) {
525 if (array_key_exists($uploadtreePk, $uploadtreePkToFilePath)) {
526 unset($lines[$uploadtreePkToFilePath[$uploadtreePk]]);
541 foreach ($lines as $row) {
542 $V .= $row[
'filePath'] .
": " . htmlentities($row[
'content']) .
"\n";
545 foreach ($lines as $row) {
546 $V .= $row[
'filePath'];
547 if ($row[
'agentFindings'] !==
null) {
548 $V .=
": " . implode(
' ', $row[
'agentFindings']);
549 if ($row[
'conclusions'] !==
null) {
550 $V .=
", " . implode(
' ', $row[
'conclusions']);
567 foreach ($lines as $row) {
568 $consolidatedLicenses = array();
569 if ($row[
'agentFindings'] ==
null) {
572 if ($row[
'conclusions'] !==
null) {
573 $row[
'agentFindings'] = array();
574 foreach ($row[
'conclusions'] as $key => $value) {
575 $row[
'agentFindings'][$key] = $row[
'conclusions'][$key];
577 $row[
'conclusions'] =
null;
578 } elseif ($row[
'agentFindings'] ==
null) {
581 foreach ($row[
'agentFindings'] as $key => $value) {
582 if ($value ==
"No_license_found") {
583 unset($row[
'agentFindings'][$key]);
585 $consolidatedLicenses[] = $row[
'agentFindings'][$key];
588 $consolidatedLicenses = array_unique($consolidatedLicenses);
589 foreach ($row[
'agentFindings'] as $key => $value) {
590 if (array_key_exists($key, $consolidatedLicenses) && $consolidatedLicenses[$key] !==
null) {
591 $row[
'agentFindings'][$key] = $consolidatedLicenses[$key];
593 unset($row[
'agentFindings'][$key]);
612 $consolidatedByDirectory = [];
613 foreach ($lines as $row) {
614 $path_parts = pathinfo($row[
'filePath']);
615 sort($row[
'agentFindings']);
616 if (array_key_exists($path_parts[
'dirname'], $consolidatedByDirectory)) {
617 if (in_array($row[
'agentFindings'], $consolidatedByDirectory[$path_parts[
'dirname']])) {
620 $consolidatedByDirectory[$path_parts[
'dirname']][] = $row[
'agentFindings'];
623 $consolidatedByDirectory[$path_parts[
'dirname']][] = $row[
'agentFindings'];
626 foreach ($consolidatedByDirectory as $key => $value) {
627 foreach ($consolidatedByDirectory[$key] as $newKey => $newValue) {
629 $newRow[
'filePath'] = $key .
"/";
630 $newRow[
'agentFindings'] = $newValue;
631 $newRow[
'conclusions'] =
null;
632 $newLines[] = $newRow;
646 private function printCSV($lines, $uploadtreeTablename, $copyright =
false)
649 $itemId = intval($request->get(
'item'));
650 $path =
Dir2Path($itemId, $uploadtreeTablename);
651 $fileName = $path[count($path) - 1][
'ufile_name'].
"-".date(
"Ymd");
653 $fileName .=
"-copyrights";
655 $fileName .=
"-licenses";
658 $out = fopen(
'php://output',
'w');
661 $head = array(
'file path',
'scan results',
'concluded results');
663 $head = array(
'file path',
'copyright');
665 fputcsv($out, $head, $this->delimiter, $this->enclosure);
666 foreach ($lines as $row) {
668 $newRow[] = $row[
'filePath'];
670 $newRow[] = $row[
'content'];
672 if ($row[
'agentFindings'] !==
null) {
673 $newRow[] = implode(
' ', $row[
'agentFindings']);
677 if ($row[
'conclusions'] !==
null) {
678 $newRow[] = implode(
' ', $row[
'conclusions']);
683 fputcsv($out, $newRow, $this->delimiter, $this->enclosure);
685 $content = ob_get_contents();
689 'Content-type' =>
'text/csv, charset=UTF-8',
690 'Content-Disposition' =>
'attachment; filename='.$fileName.
'.csv',
691 'Pragma' =>
'no-cache',
692 'Cache-Control' =>
'no-cache, must-revalidate, maxage=1, post-check=0, pre-check=0',
693 'Expires' =>
'Expires: Thu, 19 Nov 1981 08:52:00 GMT'
696 return new Response($content, Response::HTTP_OK, $headers);
708 $itemId = intval($request->get(
'item'));
709 $path =
Dir2Path($itemId, $uploadtreeTablename);
710 $fileName = $path[count($path) - 1][
'ufile_name'].
"-".date(
"Ymd");
712 $spreadsheet =
new Spreadsheet();
713 $sheet = $spreadsheet->getActiveSheet();
716 'A' => array(5,
'id',
'ID'),
717 'B' => array(30,
'path',
'Source Name or Path'),
718 'C' => array(20,
'name',
'OSS Name'),
719 'D' => array(10,
'version',
'OSS Version'),
720 'E' => array(20,
'scan results',
'Scan Results'),
721 'F' => array(20,
'concluded results',
'Concluded Results'),
722 'G' => array(20,
'download',
'Download Location'),
723 'H' => array(20,
'homepage',
'Homepage'),
724 'I' => array(30,
'copyright',
'Copyright Text'),
725 'J' => array(10,
'exclude',
'Exclude'),
726 'K' => array(30,
'comment',
'Comment')
731 'color' => [
'argb' => \PhpOffice\PhpSpreadsheet\Style\Color::COLOR_WHITE],
734 'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,
738 'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
739 'color' => [
'argb' => \PhpOffice\PhpSpreadsheet\Style\Color::COLOR_BLACK],
743 'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
744 'startColor' => [
'argb' =>
'002060'],
747 $sheet->getStyle(
'A1:J1')->applyFromArray($styleArray);
748 foreach ($headRow as $key => $val) {
749 $cellName = $key.
'1';
751 $sheet->getColumnDimension($key)->setWidth($val[0]);
753 $sheet->setCellValue($cellName, $val[2]);
758 'color' => [
'argb' =>
'808080'],
762 'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
763 'color' => [
'argb' =>
'B2B2B2'],
767 'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
768 'startColor' => [
'argb' =>
'FFFFCC'],
771 $sheet->getStyle(
'A2:J2')->applyFromArray($styleArray);
772 $annotationRow = array(
773 'A' => array(
'id',
'-'),
774 'B' => array(
'path',
'[Name of the Source File or Path]'),
775 'C' => array(
'name',
'[Name of the OSS used]'),
776 'D' => array(
'version',
'[Version Number of the OSS]'),
777 'E' => array(
'scanResults',
'[Scan results. Use SPDX Identifier : https://spdx.org/licenses/]'),
778 'F' => array(
'concludedResults',
'[Concluded results. Use SPDX Identifier : https://spdx.org/licenses/]'),
779 'G' => array(
'download',
'[Download URL or a specific location within a VCS for the OSS]'),
780 'H' => array(
'homepage',
'[Web site that serves as the OSSs home page]'),
781 'I' => array(
'copyright',
'[The copyright holders of the OSS. E.g. Copyright (c) 201X Copyright Holder]'),
782 'J' => array(
'exclude',
'[If this OSS is not included in the final version, Check exclude]'),
783 'K' => array(
'comment',
'')
785 foreach ($annotationRow as $key => $val) {
786 $cellName = $key.
'2';
788 $sheet->setCellValue($cellName, $val[1]);
794 'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
795 'color' => [
'argb' => \PhpOffice\PhpSpreadsheet\Style\Color::COLOR_BLACK],
801 foreach ($lines as $row) {
802 $rowNumber = $id + 2;
803 $range =
'A'.$rowNumber.
':K'.$rowNumber;
804 $sheet->getStyle($range)->applyFromArray($styleArray);
805 $sheet->setCellValue(
'A'.$rowNumber,
"$id");
806 $sheet->setCellValue(
'B'.$rowNumber, $row[
'filePath']);
808 if ($row[
'agentFindings'] !==
null) {
809 $scannedLicenses = implode(
',', $row[
'agentFindings']);
810 $sheet->setCellValue(
'E'.$rowNumber, $scannedLicenses);
812 if ($row[
'conclusions'] !==
null) {
813 $concludedLicenses = implode(
',', $row[
'conclusions']);
814 $sheet->setCellValue(
'F'.$rowNumber, $concludedLicenses);
819 header(
'Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
820 header(
'Content-Disposition: attachment; filename="'.$fileName.
'.xlsx"');
821 header(
'Cache-Control: max-age=0');
823 $writer =
new Xlsx($spreadsheet);
825 $writer->save(
'php://output');
836 $reducedLines = array();
837 foreach ($lines as $line) {
838 foreach ($line as $copyright) {
839 $reducedLines[] = $copyright;
842 return $reducedLines;
847 $NewPlugin->Initialize();
This is the Plugin class. All plugins should:
Contains the constants and helpers for authentication of user.
Various utility functions to filter ClearingDecision.
setEnclosure($enclosure='"')
printLines($lines, $copyright=false)
getCopyrights($uploadId, $uploadtree_pk, $uploadTreeTableName, $NomostListNum, $exclude, $copyrightType="all")
setDelimiter($delimiter=',')
getAgentPksFromRequest($upload_pk)
consolidateConclusions($conclusions)
Output()
This function returns the scheduler status.
removeCopyrightByUploadtreePk(&$lines, $uploadtreePkToFilePath, $uploadtreePks)
updateCopyrightList(&$list, &$uploadtreePkToFilePath, $newCopyrights, $NomostListNum, $uploadTreeTableName, $key)
consolidateFindingsPerDirectory($lines)
removeCopyrightWithLicense(&$lines, $uploadtreePkToFilePath, $itemTreeBounds, $agentList, $exclude)
printCSV($lines, $uploadtreeTablename, $copyright=false)
RegisterMenus()
Customize submenus.
reduceCopyrightLines($lines)
consolidateResult($lines)
printSpreadsheet($lines, $uploadtreeTablename)
__construct()
base constructor. Most plugins will just use this
Dir2Path($uploadtree_pk, $uploadtree_tablename='uploadtree')
Return the path (without artifacts) of an uploadtree_pk.
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Traceback_parm_keep($List)
Create a new URI, keeping only these items.
FUNCTION char * GetUploadtreeTableName(PGconn *pgConn, int upload_pk)
Get the uploadtree table name for this upload_pk If upload_pk does not exist, return "uploadtree".
#define PLUGIN_DB_READ
Plugin requires read permission on DB.
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN