diff --git a/src/lib/php/BusinessRules/ClearingDecisionFilter.php b/src/lib/php/BusinessRules/ClearingDecisionFilter.php index 509f3f8892..193e5013c4 100644 --- a/src/lib/php/BusinessRules/ClearingDecisionFilter.php +++ b/src/lib/php/BusinessRules/ClearingDecisionFilter.php @@ -1,6 +1,6 @@ => ` + * for copyright list + * + * Irrelevant decisions and removed licenses are marked as `"Void"`. + * @param ClearingDecision[] $clearingDecisions Clearing decisions to be + * filtered. + * @return ClearingDecision[] + */ + public function filterCurrentClearingDecisionsForCopyrightList($clearingDecisions) + { + $clearingDecisionsForCopyList = array(); + + foreach ($clearingDecisions as $clearingDecision) { + $itemId = $clearingDecision->getUploadTreeId(); + $clearingDecisionsForCopyList[$itemId] = array(); + if ($clearingDecision->getType() == DecisionTypes::IRRELEVANT) { + $clearingDecisionsForCopyList[$itemId][] = "Void"; + continue; + } + + foreach ($clearingDecision->getClearingLicenses() as $clearingLicense) { + if ($clearingLicense->isRemoved()) { + continue; + } + $clearingDecisionsForCopyList[$itemId][] = $clearingLicense->getShortName(); + } + if (empty($clearingDecisionsForCopyList[$itemId])) { + $clearingDecisionsForCopyList[$itemId][] = "Void"; + } + } + return $clearingDecisionsForCopyList; + } +} diff --git a/src/lib/php/Dao/CopyrightDao.php b/src/lib/php/Dao/CopyrightDao.php index b911a0627e..8cc27e736f 100644 --- a/src/lib/php/Dao/CopyrightDao.php +++ b/src/lib/php/Dao/CopyrightDao.php @@ -215,13 +215,15 @@ public function getScannerEntries($tableName, $uploadTreeTableName, $uploadId, $ } /** - * @param $tableName - * @param $uploadTreeTableName - * @param $uploadId - * @param $decisionType + * @param string $tableName + * @param string $uploadTreeTableName + * @param integer $uploadId + * @param integer $decisionType + * @param string $extrawhere * @return array $result */ - public function getEditedEntries($tableName, $uploadTreeTableName, $uploadId, $decisionType) + public function getEditedEntries($tableName, $uploadTreeTableName, $uploadId, + $decisionType, $extrawhere="") { $statementName = __METHOD__.$tableName.$uploadTreeTableName; $params = array(); @@ -238,6 +240,12 @@ public function getEditedEntries($tableName, $uploadTreeTableName, $uploadId, $d $extendWClause .= " AND clearing_decision_type_fk = $".count($params); $statementName .= ".withDecisionType"; } + + if (!empty($extrawhere)) { + $extendWClause .= " AND ". $extrawhere; + $statementName .= "._".$extrawhere."_"; + } + $columns = "CD.description as description, CD.textfinding as textfinding, CD.comment as comments, UT.uploadtree_pk as uploadtree_pk"; $primaryColumn = $tableName . '_pk'; diff --git a/src/www/ui/Makefile b/src/www/ui/Makefile index 690eb618eb..682d28bce0 100644 --- a/src/www/ui/Makefile +++ b/src/www/ui/Makefile @@ -27,7 +27,8 @@ UIFILES = `find . -type f | grep -v svn |grep -v tests | grep -E "(php|dat|dtd|j OBSOLETEFILES = admin-tag-ns.php admin-change-owner.php admin-tag-ns-perm.php admin-folder-delete.php \ admin-dashboard.php ajax-showjobs.php group-manage-self.php group-manage.php ajax-perms.php \ upload_permissions.php upload-srv-files.php upload-vcs.php template/components \ - template/include ui-browse-license.php + template/include ui-browse-license.php ui-license-list.php \ + template/ui-license-list-form.html.twig OTHERFILES = `find . -type f | grep -v svn |grep -v tests | grep -E "(css|htc|gif|png|ico|htm|openapi.yaml)$$"` diff --git a/src/www/ui/api/Helper/UploadHelper.php b/src/www/ui/api/Helper/UploadHelper.php index bcfa275cd8..2e99c09bba 100644 --- a/src/www/ui/api/Helper/UploadHelper.php +++ b/src/www/ui/api/Helper/UploadHelper.php @@ -584,10 +584,10 @@ public function getUploadLicenseList($uploadId, $agents, $printContainers) $scanProx->createAgentStatus($agents); $agent_ids = $scanProx->getLatestSuccessfulAgentIds(); - /** @var ui_license_list $licenseListObj - * ui_license_list object to get licenses + /** @var UIExportList $licenseListObj + * UIExportList object to get licenses */ - $licenseListObj = $restHelper->getPlugin('license-list'); + $licenseListObj = $restHelper->getPlugin('export-list'); $licenseList = $licenseListObj->createListOfLines($uploadTreeTableName, $parent->getItemId(), $agent_ids, -1, true, '', !$printContainers); if (array_key_exists("warn", $licenseList)) { diff --git a/src/www/ui/template/ui-export-list.html.twig b/src/www/ui/template/ui-export-list.html.twig new file mode 100644 index 0000000000..d161adc1bd --- /dev/null +++ b/src/www/ui/template/ui-export-list.html.twig @@ -0,0 +1,160 @@ +{# Copyright 2016-2017,2020 Siemens AG + + Copying and distribution of this file, with or without modification, + are permitted in any medium without royalty provided the copyright notice and this notice are preserved. + This file is offered as-is, without any warranty. +#} +{% extends "include/base.html.twig" %} + +{% block content %} +{{ parent() }} +
+ +
+
+
    +
  1. + + {{ "Which agents do you want to include?"|trans }} + +
      +
    • + +
    • +
    • + +
    • +
    • + +
    • +
    • + +
    • +
    • + +
    • +
    +
  2. +
  3. + +
  4. +
  5. + +
  6. +
  7. + +
  8. +
  9. + +
  10. +
+ +
+
+
+
+ +
    +
  1. + {{ "Which copyrights to export?"|trans }} +
      +
    • + +
    • +
    • + +
    +
  2. +
  3. + +
  4. +
  5. + +
  6. +
+ +
+
+
+

+ With this tool you will only be able to obtain a list with at maximum + {{ NomostListNum }} entries. + For a full list run fo_nomos_license_list from the command line. + This Limit can be modified by the administrator. +

+
+{% for warn in warnings %} + {{ warn }} +{% endfor %} +
+{{ listoutput }}
+
+{% endblock %} +{% block foot %} + {{ parent() }} + + + +{% endblock %} diff --git a/src/www/ui/template/ui-license-list-form.html.twig b/src/www/ui/template/ui-license-list-form.html.twig deleted file mode 100644 index 5a62d79d29..0000000000 --- a/src/www/ui/template/ui-license-list-form.html.twig +++ /dev/null @@ -1,67 +0,0 @@ -{# Copyright 2016-2017 Siemens AG - - Copying and distribution of this file, with or without modification, - are permitted in any medium without royalty provided the copyright notice and this notice are preserved. - This file is offered as-is, without any warranty. -#} -
-
    -
  1. - - {{ "Which agents do you want to include?"|trans }} - -
      -
    • - -
    • -
    • - -
    • -
    • - -
    • -
    -
  2. -
  3. - -
  4. -
  5. - -
  6. -
  7. - -
  8. -
  9. - -
  10. -
- With this tool you will only be able to obtain a list with at maximum {{ NomostListNum }} entries. - For a full list run fo_nomos_license_list from the command line. - This Limit can be modified by the administrator. -
- -
diff --git a/src/www/ui/ui-export-list.php b/src/www/ui/ui-export-list.php new file mode 100644 index 0000000000..f716dab805 --- /dev/null +++ b/src/www/ui/ui-export-list.php @@ -0,0 +1,603 @@ +Name = "export-list"; + $this->Title = _("Export Lists"); + $this->Dependency = array("browse"); + $this->DBaccess = PLUGIN_DB_READ; + $this->LoginFlag = 0; + $this->NoHeader = 0; + parent::__construct(); + $this->uploadDao = $GLOBALS['container']->get('dao.upload'); + $this->licenseDao = $GLOBALS['container']->get('dao.license'); + $this->clearingDao = $GLOBALS['container']->get('dao.clearing'); + $this->copyrightDao = $GLOBALS['container']->get('dao.copyright'); + $this->treeDao = $GLOBALS['container']->get('dao.tree'); + $this->clearingFilter = $GLOBALS['container']->get('businessrules.clearing_decision_filter'); + } + + /** + * Set the delimiter for CSV + * @param string $delimiter The delimiter to be used (max len 1) + */ + public function setDelimiter($delimiter=',') + { + $this->delimiter = substr($delimiter, 0, 1); + } + + /** + * Set the enclosure for CSV + * @param string $enclosure The enclosure to be used (max len 1) + */ + public function setEnclosure($enclosure='"') + { + $this->enclosure = substr($enclosure, 0, 1); + } + + /** + * \brief Customize submenus. + */ + function RegisterMenus() + { + // For all other menus, permit coming back here. + $URI = $this->Name . Traceback_parm_keep(array( + "show", + "format", + "page", + "upload", + "item", + )); + $MenuDisplayString = _("Export List"); + $Item = GetParm("item", PARM_INTEGER); + $Upload = GetParm("upload", PARM_INTEGER); + if (empty($Item) || empty($Upload)) { + return; + } + if (GetParm("mod", PARM_STRING) == $this->Name) { + menu_insert("Browse::$MenuDisplayString", 1); + } else { + menu_insert("Browse::$MenuDisplayString", 1, $URI, $MenuDisplayString); + /* bobg - This is to use a select list in the micro menu to replace the above List + and Download, but put this select list in a form + $LicChoices = array("Lic Download" => "Download", "Lic display" => "Display"); + $LicChoice = Array2SingleSelect($LicChoices, $SLName="LicDL"); + menu_insert("Browse::Nomos License List Download2", 1, $URI . "&output=dltext", NULL,NULL, $LicChoice); + */ + } + } + + /** + * Get the agent IDs for requested agents. + * @param integer $upload_pk Current upload id + * @return array Array with agent name as key and agent id if found or false + * as value. + */ + function getAgentPksFromRequest($upload_pk) + { + $agents = array_keys(AgentRef::AGENT_LIST); + $agent_pks = array(); + + foreach ($agents as $agent) { + if (GetParm("agentToInclude_".$agent, PARM_STRING)) { + /* get last nomos agent_pk that has data for this upload */ + $AgentRec = AgentARSList($agent."_ars", $upload_pk, 1); + if ($AgentRec !== false) { + $agent_pks[$agent] = $AgentRec[0]["agent_fk"]; + } else { + $agent_pks[$agent] = false; + } + } + } + return $agent_pks; + } + + /** + * Get the list of lines for the given item. + * @param string $uploadtreeTablename Upload tree table for upload + * @param integer $uploadtree_pk Item ID + * @param array $agent_pks Agents to be fetched + * @param integer $NomostListNum Max limit of items (-1 for unlimited) + * @param boolean $includeSubfolder True to include subfolders + * @param string $exclude Files to be excluded + * @param boolean $ignore Ignore empty folders + * @return array Array with each element containing `filePath`, list of + * `agentFindings` and list of `conclusions`. + */ + public function createListOfLines($uploadtreeTablename, $uploadtree_pk, + $agent_pks, $NomostListNum, $includeSubfolder, $exclude, $ignore) + { + $licensesPerFileName = array(); + /** @var ItemTreeBounds */ + $itemTreeBounds = $this->uploadDao->getItemTreeBounds($uploadtree_pk, + $uploadtreeTablename); + $allDecisions = $this->clearingDao->getFileClearingsFolder($itemTreeBounds, + Auth::getGroupId()); + $editedMappedLicenses = $this->clearingFilter->filterCurrentClearingDecisionsForLicenseList($allDecisions); + $licensesPerFileName = $this->licenseDao->getLicensesPerFileNameForAgentId($itemTreeBounds, + $agent_pks, $includeSubfolder, $exclude, $ignore, $editedMappedLicenses); + /* how many lines of data do you want to display */ + $currentNum = 0; + $lines = []; + foreach ($licensesPerFileName as $fileName => $licenseNames) { + if ($licenseNames !== false && count($licenseNames) > 0) { + if ($NomostListNum > -1 && ++$currentNum > $NomostListNum) { + $lines["warn"] = _("
Warning: Only the first $NomostListNum " . + "lines are displayed. To see the whole list, run " . + "fo_nomos_license_list from the command line.
"); + // TODO: the following should be done using a "LIMIT" statement in the sql query + break; + } + + $row = array(); + $row['filePath'] = $fileName; + $row['agentFindings'] = $licenseNames['scanResults']; + $row['conclusions'] = null; + if (array_key_exists('concludedResults', $licenseNames) && !empty($licenseNames['concludedResults'])) { + $row['conclusions'] = $this->consolidateConclusions($licenseNames['concludedResults']); + $lines[] = $row; + } else { + $lines[] = $row; + } + } + if (!$ignore && $licenseNames === false) { + $row = array(); + $row['filePath'] = $fileName; + $row['agentFindings'] = null; + $row['conclusions'] = null; + $lines[] = $row; + } + } + return $lines; + } + + /** + * @copydoc FO_Plugin::getTemplateName() + * @see FO_Plugin::getTemplateName() + */ + public function getTemplateName() + { + return "ui-export-list.html.twig"; + } + + /** + * \brief This function returns the scheduler status. + */ + function Output() + { + global $PG_CONN; + global $SysConf; + $formVars = array(); + if (!$PG_CONN) { + echo _("NO DB connection"); + } + + if ($this->State != PLUGIN_STATE_READY) { + return (0); + } + $uploadtree_pk = GetParm("item", PARM_INTEGER); + if (empty($uploadtree_pk)) { + return; + } + + $upload_pk = GetParm("upload", PARM_INTEGER); + if (empty($upload_pk)) { + return; + } + if (!$this->uploadDao->isAccessible($upload_pk, Auth::getGroupId())) { + $text = _("Permission Denied"); + return "

$text

"; + } + $uploadtreeTablename = GetUploadtreeTableName($upload_pk); + + $warnings = array(); + $exportCopyright = GetParm('export_copy', PARM_STRING); + if (!empty($exportCopyright) && $exportCopyright == "yes") { + $exportCopyright = true; + $copyrightType = GetParm('copyright_type', PARM_STRING); + $formVars["export_copy"] = "1"; + if ($copyrightType == "all") { + $formVars["copy_type_all"] = 1; + } else { + $formVars["copy_type_nolic"] = 1; + } + } else { + $exportCopyright = false; + $agent_pks_dict = $this->getAgentPksFromRequest($upload_pk); + $agent_pks = array(); + foreach ($agent_pks_dict as $agent_name => $agent_pk) { + if ($agent_pk === false) { + $warnings[] = _("No information for agent: $agent_name"); + } else { + $agent_pks[] = $agent_pk; + $formVars["agentToInclude_".$agent_name] = "1"; + } + } + } + + // Make sure all copyrights is selected in the form be default + if (!(array_key_exists('copy_type_all', $formVars) || + array_key_exists('copy_type_nolic', $formVars))) { + $formVars["copy_type_all"] = 1; + } + + $dltext = (GetParm("output", PARM_STRING) == 'dltext'); + $formVars["dltext"] = $dltext; + + $NomostListNum = @$SysConf['SYSCONFIG']['NomostListNum']; + $formVars["NomostListNum"] = $NomostListNum; + + $includeSubfolder = (GetParm("doNotIncludeSubfolder", PARM_STRING) !== "yes"); + $formVars["includeSubfolder"] = $includeSubfolder; + + $ignore = (GetParm("showContainers", PARM_STRING) !== "yes"); + $formVars["showContainers"] = !$ignore; + $exclude = GetParm("exclude", PARM_STRING); + $formVars["exclude"] = $exclude; + + $this->vars = array_merge($this->vars, $formVars); + + if ($exportCopyright) { + $lines = $this->getCopyrights($upload_pk, $uploadtree_pk, + $uploadtreeTablename, $NomostListNum, $exclude, $copyrightType); + } else { + $lines = $this->createListOfLines($uploadtreeTablename, $uploadtree_pk, + $agent_pks, $NomostListNum, $includeSubfolder, $exclude, $ignore); + } + + $this->vars['warnings'] = array(); + if (array_key_exists("warn",$lines)) { + $warnings[] = $lines["warn"]; + unset($lines["warn"]); + } + foreach ($warnings as $warning) { + $this->vars['warnings'][] = "
$warning
"; + } + if (empty($lines)) { + $this->vars['warnings'][] = "
Result empty
"; + } + + if ($dltext) { + return $this->printCSV($lines, $uploadtreeTablename, $exportCopyright); + } else { + $this->vars['listoutput'] = $this->printLines($lines, $exportCopyright); + return; + } + } + + /** + * Get the list of copyrights + * @param integer $uploadId Upload ID + * @param integer $uploadtree_pk Item ID + * @param integer $uploadTreeTableName Upload tree table name + * @param integer $NomostListNum Limit of lines to print + * @param integer $exclude Files to be excluded + * @param string $copyrightType Which copyrights to print (`"all"` to + * print everything, `"nolic"` to print + * only files with no scanner findings and + * no license as conclusion) + * @return array List of copyrights with `filePath` and `content` + */ + public function getCopyrights($uploadId, $uploadtree_pk, $uploadTreeTableName, + $NomostListNum, $exclude, $copyrightType = "all") + { + $agentName = "copyright"; + $scanJobProxy = new ScanJobProxy($GLOBALS['container']->get('dao.agent'), + $uploadId); + $scanJobProxy->createAgentStatus([$agentName]); + $selectedScanners = $scanJobProxy->getLatestSuccessfulAgentIds(); + if (!array_key_exists($agentName, $selectedScanners)) { + return array(); + } + $latestAgentId = $selectedScanners[$agentName]; + $agentFilter = ' AND C.agent_fk='.$latestAgentId; + + $itemTreeBounds = $this->uploadDao->getItemTreeBounds($uploadtree_pk, + $uploadTreeTableName); + $extrawhere = "UT.lft BETWEEN " . $itemTreeBounds->getLeft() . " AND " . + $itemTreeBounds->getRight(); + if (! empty($exclude)) { + $extrawhere .= " AND UT.ufile_name NOT LIKE '%$exclude%'"; + } + $lines = []; + + $copyrights = $this->copyrightDao->getScannerEntries($agentName, + $uploadTreeTableName, $uploadId, null, $extrawhere . $agentFilter); + $this->updateCopyrightList($lines, $copyrights, $NomostListNum, + $uploadTreeTableName, "content"); + + $copyrights = $this->copyrightDao->getEditedEntries('copyright_decision', + $uploadTreeTableName, $uploadId, [], $extrawhere); + $this->updateCopyrightList($lines, $copyrights, $NomostListNum, + $uploadTreeTableName, "textfinding"); + + if ($copyrightType != "all") { + $agentList = []; + foreach (AgentRef::AGENT_LIST as $agentname => $value) { + $AgentRec = AgentARSList($agentname."_ars", $uploadId, 1); + if (!empty($AgentRec)) { + $agentList[] = $AgentRec[0]["agent_fk"]; + } + } + $this->removeCopyrightWithLicense($lines, $itemTreeBounds, $agentList, + $exclude); + } + return $this->reduceCopyrightLines($lines); + } + + /** + * Update the list of copyrights with new list + * @param array[in,out] $list List of copyrights + * @param array $newCopyrights List of copyrights to be included + * @param integer $NomostListNum Limit of copyrights + * @param string $uploadTreeTableName Upload tree table name + * @param string $key Key of the array holding copyright + */ + private function updateCopyrightList(&$list, $newCopyrights, $NomostListNum, + $uploadTreeTableName, $key) + { + foreach ($newCopyrights as $copyright) { + if ($NomostListNum > -1 && count($list) >= $NomostListNum) { + $lines["warn"] = _("
Warning: Only the first $NomostListNum lines + are displayed. To see the whole list, run fo_nomos_license_list from the + command line.
"); + break; + } + $row = []; + $row["content"] = $copyright[$key]; + $row["filePath"] = $this->treeDao->getFullPath($copyright["uploadtree_pk"], + $uploadTreeTableName); + $list[$row["filePath"]][] = $row; + } + } + + /** + * Remove all files which either have license findings and not remove, or + * have at least one license as conclusion + * @param array[in,out] $lines Lines to be filtered + * @param ItemTreeBounds $itemTreeBounds Item bounds + * @param array $agentList List of agent IDs + * @param string $exclude Files to be excluded + */ + private function removeCopyrightWithLicense(&$lines, $itemTreeBounds, + $agentList, $exclude) + { + $licensesPerFileName = array(); + $allDecisions = $this->clearingDao->getFileClearingsFolder($itemTreeBounds, + Auth::getGroupId()); + $editedMappedLicenses = $this->clearingFilter->filterCurrentClearingDecisionsForCopyrightList( + $allDecisions); + $licensesPerFileName = $this->licenseDao->getLicensesPerFileNameForAgentId( + $itemTreeBounds, $agentList, true, $exclude, true, $editedMappedLicenses); + foreach ($licensesPerFileName as $fileName => $licenseNames) { + if ($licenseNames !== false && count($licenseNames) > 0) { + if (array_key_exists('concludedResults', $licenseNames)) { + $conclusions = $this->consolidateConclusions($licenseNames['concludedResults']); + if (in_array("Void", $conclusions)) { + // File has all licenses removed or irrelevant decision + continue; + } + // File has license conclusions + $this->removeIfKeyExists($lines, $fileName); + } + if ((! empty($licenseNames['scanResults'])) && + ! (in_array("No_license_found", $licenseNames['scanResults']) || + in_array("Void", $licenseNames['scanResults']))) { + $this->removeIfKeyExists($lines, $fileName); + } + } + } + } + + /** + * Reduce the 2D list of conclusions on a file to a linear array + * @param array $conclusions 2D array of conclusions + * @return array List of unique conclusions on the file + */ + private function consolidateConclusions($conclusions) + { + $consolidatedConclusions = array(); + foreach ($conclusions as $conclusion) { + $consolidatedConclusions = array_merge($consolidatedConclusions, + $conclusion); + } + return array_unique($consolidatedConclusions); + } + + /** + * Remove key from a list if it exists + * + * @note Uses strpos to find the key + * @param array[in,out] $lines Array + * @param string $key Key to be removed + */ + private function removeIfKeyExists(&$lines, $key) + { + foreach (array_keys($lines) as $file) { + if (strpos($file, $key) !== false) { + unset($lines[$file]); + break; + } + } + } + + /** + * Print the lines for browser + * @param array $lines Lines to be printed + * @param boolean $copyright Results are copyright? + * @return string + */ + private function printLines($lines, $copyright=false) + { + $V = ''; + if ($copyright) { + foreach ($lines as $row) { + $V .= $row['filePath'] . ": " . htmlentities($row['content']) . "\n"; + } + } else { + foreach ($lines as $row) { + $V .= $row['filePath']; + if ($row['agentFindings'] !== null) { + $V .= ": " . implode(' ', $row['agentFindings']); + if ($row['conclusions'] !== null) { + $V .= ", " . implode(' ', $row['conclusions']); + } + } + $V .= "\n"; + } + } + return $V; + } + + /** + * Print the lines as CSV + * @param array $lines Lines to be printed + * @param string $uploadtreeTablename Upload tree table name + * @param boolean $copyright Results are copyright? + * @return Response CSV file as a response + */ + private function printCSV($lines, $uploadtreeTablename, $copyright = false) + { + $request = $this->getRequest(); + $itemId = intval($request->get('item')); + $path = Dir2Path($itemId, $uploadtreeTablename); + $fileName = $path[count($path) - 1]['ufile_name']."-".date("Ymd"); + if ($copyright) { + $fileName .= "-copyrights"; + } else { + $fileName .= "-licenses"; + } + + $out = fopen('php://output', 'w'); + ob_start(); + if (!$copyright) { + $head = array('file path', 'scan results', 'concluded results'); + } else { + $head = array('file path', 'copyright'); + } + fputcsv($out, $head, $this->delimiter, $this->enclosure); + foreach ($lines as $row) { + $newRow = array(); + $newRow[] = $row['filePath']; + if ($copyright) { + $newRow[] = $row['content']; + } else { + if ($row['agentFindings'] !== null) { + $newRow[] = implode(' ', $row['agentFindings']); + } else { + $newRow[] = ""; + } + if ($row['conclusions'] !== null) { + $newRow[] = implode(' ', $row['conclusions']); + } else { + $newRow[] = ""; + } + } + fputcsv($out, $newRow, $this->delimiter, $this->enclosure); + } + $content = ob_get_contents(); + ob_end_clean(); + + $headers = array( + 'Content-type' => 'text/csv, charset=UTF-8', + 'Content-Disposition' => 'attachment; filename='.$fileName.'.csv', + 'Pragma' => 'no-cache', + 'Cache-Control' => 'no-cache, must-revalidate, maxage=1, post-check=0, pre-check=0', + 'Expires' => 'Expires: Thu, 19 Nov 1981 08:52:00 GMT' + ); + + return new Response($content, Response::HTTP_OK, $headers); + } + + /** + * Reduce multidimentional copyright list to simple 2D array + * @param array $lines Copyright list + * @return array Simple 2D array + */ + private function reduceCopyrightLines($lines) + { + $reducedLines = array(); + foreach ($lines as $line) { + foreach ($line as $copyright) { + $reducedLines[] = $copyright; + } + } + return $reducedLines; + } +} + +$NewPlugin = new UIExportList(); +$NewPlugin->Initialize(); diff --git a/src/www/ui/ui-license-list.php b/src/www/ui/ui-license-list.php deleted file mode 100644 index 7ae7e6a131..0000000000 --- a/src/www/ui/ui-license-list.php +++ /dev/null @@ -1,297 +0,0 @@ -Name = "license-list"; - $this->Title = _("License List"); - $this->Dependency = array("browse"); - $this->DBaccess = PLUGIN_DB_READ; - $this->LoginFlag = 0; - $this->NoHeader = 0; - parent::__construct(); - $this->uploadDao = $GLOBALS['container']->get('dao.upload'); - $this->licenseDao = $GLOBALS['container']->get('dao.license'); - $this->clearingDao = $GLOBALS['container']->get('dao.clearing'); - $this->clearingFilter = $GLOBALS['container']->get('businessrules.clearing_decision_filter'); - } - - public function setDelimiter($delimiter=',') - { - $this->delimiter = substr($delimiter,0,1); - } - - public function setEnclosure($enclosure='"') - { - $this->enclosure = substr($enclosure,0,1); - } - - /** - * \brief Customize submenus. - */ - function RegisterMenus() - { - // For all other menus, permit coming back here. - $URI = $this->Name . Traceback_parm_keep(array( - "show", - "format", - "page", - "upload", - "item", - )); - $MenuDisplayString = _("License List"); - $Item = GetParm("item", PARM_INTEGER); - $Upload = GetParm("upload", PARM_INTEGER); - if (empty($Item) || empty($Upload)) { - return; - } - if (GetParm("mod", PARM_STRING) == $this->Name) { - menu_insert("Browse::$MenuDisplayString", 1); - } else { - menu_insert("Browse::$MenuDisplayString", 1, $URI, $MenuDisplayString); - /* bobg - This is to use a select list in the micro menu to replace the above List - and Download, but put this select list in a form - $LicChoices = array("Lic Download" => "Download", "Lic display" => "Display"); - $LicChoice = Array2SingleSelect($LicChoices, $SLName="LicDL"); - menu_insert("Browse::Nomos License List Download2", 1, $URI . "&output=dltext", NULL,NULL, $LicChoice); - */ - } - } - - function getAgentPksFromRequest($upload_pk) - { - $agents = array("monk","nomos","ninka","reportImport"); - $agent_pks = array(); - - foreach ($agents as $agent) { - if (GetParm("agentToInclude_".$agent, PARM_STRING)) { - /* get last nomos agent_pk that has data for this upload */ - $AgentRec = AgentARSList($agent."_ars", $upload_pk, 1); - if ($AgentRec !== false) { - $agent_pks[$agent] = $AgentRec[0]["agent_fk"]; - } else { - $agent_pks[$agent] = false; - } - } - } - return $agent_pks; - } - - public function createListOfLines($uploadtreeTablename, $uploadtree_pk, $agent_pks, $NomostListNum, $includeSubfolder, $exclude, $ignore) - { - $licensesPerFileName = array(); - /** @var ItemTreeBounds */ - $itemTreeBounds = $this->uploadDao->getItemTreeBounds($uploadtree_pk, $uploadtreeTablename); - $allDecisions = $this->clearingDao->getFileClearingsFolder($itemTreeBounds, Auth::getGroupId()); - $editedMappedLicenses = $this->clearingFilter->filterCurrentClearingDecisionsForLicenseList($allDecisions); - $licensesPerFileName = $this->licenseDao->getLicensesPerFileNameForAgentId($itemTreeBounds, - $agent_pks, $includeSubfolder, $exclude, $ignore, $editedMappedLicenses); - /* how many lines of data do you want to display */ - $currentNum = 0; - $lines = []; - foreach ($licensesPerFileName as $fileName => $licenseNames) { - if ($licenseNames !== false && count($licenseNames) > 0) { - if ($NomostListNum > -1 && ++$currentNum > $NomostListNum) { - $lines["warn"] = _("
Warning: Only the first $NomostListNum lines are displayed. To see the whole list, run fo_nomos_license_list from the command line.
"); - // TODO: the following should be done using a "LIMIT" statement in the sql query - break; - } - - $row = array(); - $row['filePath'] = $fileName; - $row['agentFindings'] = $licenseNames['scanResults']; - $row['conclusions'] = null; - if (array_key_exists('concludedResults', $licenseNames) && !empty($licenseNames['concludedResults'])) { - $conclusions = array(); - foreach ($licenseNames['concludedResults'] as $value) { - $conclusions = array_merge($conclusions, $value); - } - $conclusions = array_unique ($conclusions); - $row['conclusions'] = $conclusions; - $lines[] = $row; - } else { - $lines[] = $row; - } - } - if (!$ignore && $licenseNames === false) { - $row = array(); - $row['filePath'] = $fileName; - $row['agentFindings'] = null; - $row['conclusions'] = null; - $lines[] = $row; - } - } - return $lines; - } - - /** - * \brief This function returns the scheduler status. - */ - function Output() - { - global $PG_CONN; - global $SysConf; - $V = ""; - $formVars = array(); - if (!$PG_CONN) { - echo _("NO DB connection"); - } - - if ($this->State != PLUGIN_STATE_READY) { - return (0); - } - $uploadtree_pk = GetParm("item", PARM_INTEGER); - if (empty($uploadtree_pk)) { - return; - } - - $upload_pk = GetParm("upload", PARM_INTEGER); - if (empty($upload_pk)) { - return; - } - if (!$this->uploadDao->isAccessible($upload_pk, Auth::getGroupId())) { - $text = _("Permission Denied"); - return "

$text

"; - } - $uploadtreeTablename = GetUploadtreeTableName($upload_pk); - - $warnings = array(); - $agent_pks_dict = $this->getAgentPksFromRequest($upload_pk); - $agent_pks = array(); - foreach ($agent_pks_dict as $agent_name => $agent_pk) { - if ($agent_pk === false) { - $warnings[] = _("No information for agent: $agent_name"); - } else { - $agent_pks[] = $agent_pk; - $formVars["agentToInclude_".$agent_name] = "1"; - } - } - - $dltext = (GetParm("output", PARM_STRING) == 'dltext'); - $formVars["dltext"] = $dltext; - - $NomostListNum = @$SysConf['SYSCONFIG']['NomostListNum']; - $formVars["NomostListNum"] = $NomostListNum; - - $includeSubfolder = (GetParm("doNotIncludeSubfolder", PARM_STRING) !== "yes"); - $formVars["includeSubfolder"] = $includeSubfolder; - - $ignore = (GetParm("showContainers", PARM_STRING) !== "yes"); - $formVars["showContainers"] = !$ignore; - $exclude = GetParm("exclude", PARM_STRING); - $formVars["exclude"] = $exclude; - - $V .= $this->renderString("ui-license-list-form.html.twig",$formVars); - - $V .= "
"; - $lines = $this->createListOfLines($uploadtreeTablename, $uploadtree_pk, $agent_pks, $NomostListNum, $includeSubfolder, $exclude, $ignore); - - if (array_key_exists("warn",$lines)) { - $warnings[] = $lines["warn"]; - unset($lines["warn"]); - } - foreach ($warnings as $warning) { - $V .= "
$warning
"; - } - - if ($dltext) { - $request = $this->getRequest(); - $itemId = intval($request->get('item')); - $path = Dir2Path($itemId, $uploadtreeTablename); - $fileName = $path[count($path) - 1]['ufile_name']."-".date("Ymd"); - - $out = fopen('php://output', 'w'); - ob_start(); - $head = array('file path', 'scan results', 'concluded results'); - fputcsv($out, $head, $this->delimiter, $this->enclosure); - foreach ($lines as $row) { - $newRow = array(); - $newRow[] = $row['filePath']; - if ($row['agentFindings'] !== null) { - $newRow[] = implode(' ', $row['agentFindings']); - } else { - $newRow[] = ""; - } - if ($row['conclusions'] !== null) { - $newRow[] = implode(' ', $row['conclusions']); - } else { - $newRow[] = ""; - } - fputcsv($out, $newRow, $this->delimiter, $this->enclosure); - } - $content = ob_get_contents(); - ob_end_clean(); - - $headers = array( - 'Content-type' => 'text/csv, charset=UTF-8', - 'Content-Disposition' => 'attachment; filename='.$fileName.'.csv', - 'Pragma' => 'no-cache', - 'Cache-Control' => 'no-cache, must-revalidate, maxage=1, post-check=0, pre-check=0', - 'Expires' => 'Expires: Thu, 19 Nov 1981 08:52:00 GMT' - ); - - return new Response($content, Response::HTTP_OK, $headers); - } else { - $V .= '
';
-      foreach ($lines as $row) {
-        $V .= $row['filePath'];
-        if ($row['agentFindings'] !== null) {
-          $V .= ": " . implode(' ', $row['agentFindings']);
-          if ($row['conclusions'] !== null) {
-            $V .= ", " . implode(' ', $row['conclusions']);
-          }
-        }
-        $V .= "\n";
-      }
-      $V .= "
"; - return $V .= ""; - } - } -} - -$NewPlugin = new ui_license_list(); -$NewPlugin->Initialize();