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%'";
401 $copyrights = $this->copyrightDao->getScannerEntries($agentName[0],
402 $uploadTreeTableName, $uploadId,
null, $extrawhere . $agentFilter);
404 $uploadTreeTableName,
"content");
406 $copyrights = $this->copyrightDao->getEditedEntries(
'copyright_decision',
407 $uploadTreeTableName, $uploadId, [], $extrawhere);
409 $uploadTreeTableName,
"textfinding");
411 if ($copyrightType !=
"all") {
413 foreach (AgentRef::AGENT_LIST as $agentname => $value) {
414 $AgentRec = AgentARSList($agentname.
"_ars", $uploadId, 1);
415 if (!empty($AgentRec)) {
416 $agentList[] = $AgentRec[0][
"agent_fk"];
434 $uploadTreeTableName, $key)
436 foreach ($newCopyrights as $copyright) {
437 if ($NomostListNum > -1 && count($list) >= $NomostListNum) {
438 $lines[
"warn"] = _(
"<br><b>Warning: Only the first $NomostListNum lines
439 are displayed. To see the whole list, run fo_nomos_license_list from the
440 command line.</b><br>");
444 $row[
"content"] = $copyright[$key];
445 $row[
"filePath"] = $this->treeDao->getFullPath($copyright[
"uploadtree_pk"],
446 $uploadTreeTableName);
447 $list[$row[
"filePath"]][] = $row;
460 $agentList, $exclude)
462 $licensesPerFileName = array();
463 $allDecisions = $this->clearingDao->getFileClearingsFolder($itemTreeBounds,
465 $editedMappedLicenses = $this->clearingFilter->filterCurrentClearingDecisionsForCopyrightList(
467 $licensesPerFileName = $this->licenseDao->getLicensesPerFileNameForAgentId(
468 $itemTreeBounds, $agentList,
true, $exclude,
true, $editedMappedLicenses);
469 foreach ($licensesPerFileName as $fileName => $licenseNames) {
470 if ($licenseNames !==
false && count($licenseNames) > 0) {
471 if (array_key_exists(
'concludedResults', $licenseNames)) {
473 if (in_array(
"Void", $conclusions)) {
480 if ((! empty($licenseNames[
'scanResults'])) &&
481 ! (in_array(
"No_license_found", $licenseNames[
'scanResults']) ||
482 in_array(
"Void", $licenseNames[
'scanResults']))) {
496 $consolidatedConclusions = array();
497 foreach ($conclusions as $conclusion) {
498 $consolidatedConclusions = array_merge($consolidatedConclusions,
501 return array_unique($consolidatedConclusions);
513 foreach (array_keys($lines) as $file) {
514 if (strpos($file, $key) !==
false) {
515 unset($lines[$file]);
531 foreach ($lines as $row) {
532 $V .= $row[
'filePath'] .
": " . htmlentities($row[
'content']) .
"\n";
535 foreach ($lines as $row) {
536 $V .= $row[
'filePath'];
537 if ($row[
'agentFindings'] !==
null) {
538 $V .=
": " . implode(
' ', $row[
'agentFindings']);
539 if ($row[
'conclusions'] !==
null) {
540 $V .=
", " . implode(
' ', $row[
'conclusions']);
557 foreach ($lines as $row) {
558 $consolidatedLicenses = array();
559 if ($row[
'agentFindings'] ==
null) {
562 if ($row[
'conclusions'] !==
null) {
563 $row[
'agentFindings'] = array();
564 foreach ($row[
'conclusions'] as $key => $value) {
565 $row[
'agentFindings'][$key] = $row[
'conclusions'][$key];
567 $row[
'conclusions'] =
null;
568 } elseif ($row[
'agentFindings'] ==
null) {
571 foreach ($row[
'agentFindings'] as $key => $value) {
572 if ($value ==
"No_license_found") {
573 unset($row[
'agentFindings'][$key]);
575 $consolidatedLicenses[] = $row[
'agentFindings'][$key];
578 $consolidatedLicenses = array_unique($consolidatedLicenses);
579 foreach ($row[
'agentFindings'] as $key => $value) {
580 if (array_key_exists($key, $consolidatedLicenses) && $consolidatedLicenses[$key] !==
null) {
581 $row[
'agentFindings'][$key] = $consolidatedLicenses[$key];
583 unset($row[
'agentFindings'][$key]);
602 $consolidatedByDirectory = [];
603 foreach ($lines as $row) {
604 $path_parts = pathinfo($row[
'filePath']);
605 sort($row[
'agentFindings']);
606 if (array_key_exists($path_parts[
'dirname'], $consolidatedByDirectory)) {
607 if (in_array($row[
'agentFindings'], $consolidatedByDirectory[$path_parts[
'dirname']])) {
610 $consolidatedByDirectory[$path_parts[
'dirname']][] = $row[
'agentFindings'];
613 $consolidatedByDirectory[$path_parts[
'dirname']][] = $row[
'agentFindings'];
616 foreach ($consolidatedByDirectory as $key => $value) {
617 foreach ($consolidatedByDirectory[$key] as $newKey => $newValue) {
619 $newRow[
'filePath'] = $key .
"/";
620 $newRow[
'agentFindings'] = $newValue;
621 $newRow[
'conclusions'] =
null;
622 $newLines[] = $newRow;
636 private function printCSV($lines, $uploadtreeTablename, $copyright =
false)
639 $itemId = intval($request->get(
'item'));
640 $path =
Dir2Path($itemId, $uploadtreeTablename);
641 $fileName = $path[count($path) - 1][
'ufile_name'].
"-".date(
"Ymd");
643 $fileName .=
"-copyrights";
645 $fileName .=
"-licenses";
648 $out = fopen(
'php://output',
'w');
651 $head = array(
'file path',
'scan results',
'concluded results');
653 $head = array(
'file path',
'copyright');
655 fputcsv($out, $head, $this->delimiter, $this->enclosure);
656 foreach ($lines as $row) {
658 $newRow[] = $row[
'filePath'];
660 $newRow[] = $row[
'content'];
662 if ($row[
'agentFindings'] !==
null) {
663 $newRow[] = implode(
' ', $row[
'agentFindings']);
667 if ($row[
'conclusions'] !==
null) {
668 $newRow[] = implode(
' ', $row[
'conclusions']);
673 fputcsv($out, $newRow, $this->delimiter, $this->enclosure);
675 $content = ob_get_contents();
679 'Content-type' =>
'text/csv, charset=UTF-8',
680 'Content-Disposition' =>
'attachment; filename='.$fileName.
'.csv',
681 'Pragma' =>
'no-cache',
682 'Cache-Control' =>
'no-cache, must-revalidate, maxage=1, post-check=0, pre-check=0',
683 'Expires' =>
'Expires: Thu, 19 Nov 1981 08:52:00 GMT'
686 return new Response($content, Response::HTTP_OK, $headers);
698 $itemId = intval($request->get(
'item'));
699 $path =
Dir2Path($itemId, $uploadtreeTablename);
700 $fileName = $path[count($path) - 1][
'ufile_name'].
"-".date(
"Ymd");
702 $spreadsheet =
new Spreadsheet();
703 $sheet = $spreadsheet->getActiveSheet();
706 'A' => array(5,
'id',
'ID'),
707 'B' => array(30,
'path',
'Source Name or Path'),
708 'C' => array(20,
'name',
'OSS Name'),
709 'D' => array(10,
'version',
'OSS Version'),
710 'E' => array(20,
'scan results',
'Scan Results'),
711 'F' => array(20,
'concluded results',
'Concluded Results'),
712 'G' => array(20,
'download',
'Download Location'),
713 'H' => array(20,
'homepage',
'Homepage'),
714 'I' => array(30,
'copyright',
'Copyright Text'),
715 'J' => array(10,
'exclude',
'Exclude'),
716 'K' => array(30,
'comment',
'Comment')
721 'color' => [
'argb' => \PhpOffice\PhpSpreadsheet\Style\Color::COLOR_WHITE],
724 'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,
728 'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
729 'color' => [
'argb' => \PhpOffice\PhpSpreadsheet\Style\Color::COLOR_BLACK],
733 'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
734 'startColor' => [
'argb' =>
'002060'],
737 $sheet->getStyle(
'A1:J1')->applyFromArray($styleArray);
738 foreach ($headRow as $key => $val) {
739 $cellName = $key.
'1';
741 $sheet->getColumnDimension($key)->setWidth($val[0]);
743 $sheet->setCellValue($cellName, $val[2]);
748 'color' => [
'argb' =>
'808080'],
752 'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
753 'color' => [
'argb' =>
'B2B2B2'],
757 'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
758 'startColor' => [
'argb' =>
'FFFFCC'],
761 $sheet->getStyle(
'A2:J2')->applyFromArray($styleArray);
762 $annotationRow = array(
763 'A' => array(
'id',
'-'),
764 'B' => array(
'path',
'[Name of the Source File or Path]'),
765 'C' => array(
'name',
'[Name of the OSS used]'),
766 'D' => array(
'version',
'[Version Number of the OSS]'),
767 'E' => array(
'scanResults',
'[Scan results. Use SPDX Identifier : https://spdx.org/licenses/]'),
768 'F' => array(
'concludedResults',
'[Concluded results. Use SPDX Identifier : https://spdx.org/licenses/]'),
769 'G' => array(
'download',
'[Download URL or a specific location within a VCS for the OSS]'),
770 'H' => array(
'homepage',
'[Web site that serves as the OSSs home page]'),
771 'I' => array(
'copyright',
'[The copyright holders of the OSS. E.g. Copyright (c) 201X Copyright Holder]'),
772 'J' => array(
'exclude',
'[If this OSS is not included in the final version, Check exclude]'),
773 'K' => array(
'comment',
'')
775 foreach ($annotationRow as $key => $val) {
776 $cellName = $key.
'2';
778 $sheet->setCellValue($cellName, $val[1]);
784 'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
785 'color' => [
'argb' => \PhpOffice\PhpSpreadsheet\Style\Color::COLOR_BLACK],
791 foreach ($lines as $row) {
792 $rowNumber = $id + 2;
793 $range =
'A'.$rowNumber.
':K'.$rowNumber;
794 $sheet->getStyle($range)->applyFromArray($styleArray);
795 $sheet->setCellValue(
'A'.$rowNumber,
"$id");
796 $sheet->setCellValue(
'B'.$rowNumber, $row[
'filePath']);
798 if ($row[
'agentFindings'] !==
null) {
799 $scannedLicenses = implode(
',', $row[
'agentFindings']);
800 $sheet->setCellValue(
'E'.$rowNumber, $scannedLicenses);
802 if ($row[
'conclusions'] !==
null) {
803 $concludedLicenses = implode(
',', $row[
'conclusions']);
804 $sheet->setCellValue(
'F'.$rowNumber, $concludedLicenses);
809 header(
'Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
810 header(
'Content-Disposition: attachment; filename="'.$fileName.
'.xlsx"');
811 header(
'Cache-Control: max-age=0');
813 $writer =
new Xlsx($spreadsheet);
815 $writer->save(
'php://output');
826 $reducedLines = array();
827 foreach ($lines as $line) {
828 foreach ($line as $copyright) {
829 $reducedLines[] = $copyright;
832 return $reducedLines;
837 $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")
updateCopyrightList(&$list, $newCopyrights, $NomostListNum, $uploadTreeTableName, $key)
setDelimiter($delimiter=',')
getAgentPksFromRequest($upload_pk)
consolidateConclusions($conclusions)
Output()
This function returns the scheduler status.
consolidateFindingsPerDirectory($lines)
printCSV($lines, $uploadtreeTablename, $copyright=false)
RegisterMenus()
Customize submenus.
reduceCopyrightLines($lines)
removeIfKeyExists(&$lines, $key)
removeCopyrightWithLicense(&$lines, $itemTreeBounds, $agentList, $exclude)
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