FOSSology  4.4.0
Open Source License Compliance by Open Source Software
BrowseLicense.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2008-2015 Hewlett-Packard Development Company, L.P.
4  SPDX-FileCopyrightText: © 2014-2015 Siemens AG
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
9 namespace Fossology\UI\Page;
10 
24 use Symfony\Component\HttpFoundation\RedirectResponse;
25 use Symfony\Component\HttpFoundation\Request;
26 use Symfony\Component\HttpFoundation\Response;
27 
34 {
35  const NAME = "license";
36 
37  private $uploadtree_tablename = "";
39  private $uploadDao;
41  private $licenseDao;
43  private $clearingDao;
45  private $agentDao;
47  private $clearingFilter;
49  private $licenseProjector;
51  protected $agentNames = AgentRef::AGENT_LIST;
52 
53  public function __construct()
54  {
55  parent::__construct(self::NAME, array(
56  self::TITLE => _("License Browser"),
57  self::DEPENDENCIES => array("browse", "view"),
58  self::PERMISSION => Auth::PERM_READ,
59  self::REQUIRES_LOGIN => false
60  ));
61 
62  global $container;
63  $this->uploadDao = $container->get('dao.upload');
64  $this->licenseDao = $container->get('dao.license');
65  $this->clearingDao = $container->get('dao.clearing');
66  $this->agentDao = $container->get('dao.agent');
67  $this->clearingFilter = $container->get('businessrules.clearing_decision_filter');
68  }
69 
73  function RegisterMenus()
74  {
75  // For all other menus, permit coming back here.
76  $URI = $this->Name . Traceback_parm_keep(array("upload", "item"));
77 
78  $Item = GetParm("item", PARM_INTEGER);
79  $Upload = GetParm("upload", PARM_INTEGER);
80  if (empty($Item) || empty($Upload)) {
81  return;
82  }
83  $viewLicenseURI = $this->NAME . Traceback_parm_keep(array("show", "format", "page", "upload", "item"));
84  $menuName = $this->Title;
85 
86  $uploadTreeTable = $this->uploadDao->getUploadtreeTableName($Upload);
87  $itemBounds = $this->uploadDao->getItemTreeBounds($Item, $uploadTreeTable);
88  if (! $itemBounds->containsFiles()) {
89  global $container;
93  $treeDao = $container->get('dao.tree');
94  $parent = $treeDao->getParentOfItem($itemBounds);
95  $viewLicenseURI = $this->NAME . Traceback_parm_keep(array("show",
96  "format", "page", "upload")) . "&item=$parent";
97  }
98  if (GetParm("mod", PARM_STRING) == $this->NAME) {
99  menu_insert("Browse::$menuName", 99);
100  menu_insert("View::$menuName", 99);
101  menu_insert("View-Meta::$menuName", 99);
102  } else {
103  $text = _("license histogram");
104  menu_insert("Browse::$menuName", 99, $URI, $text);
105  menu_insert("View::$menuName", 99, $viewLicenseURI, $text);
106  menu_insert("View-Meta::$menuName", 99, $viewLicenseURI, $text);
107  }
108  }
109 
114  protected function handle(Request $request)
115  {
116  $upload = intval($request->get("upload"));
117  $groupId = Auth::getGroupId();
118  if (!$this->uploadDao->isAccessible($upload, $groupId)) {
119  return $this->flushContent(_("Permission Denied"));
120  }
121 
122  $item = intval($request->get("item"));
123  $this->uploadtree_tablename = $this->uploadDao->getUploadtreeTableName($upload);
124  $itemTreeBounds = $this->uploadDao->getItemTreeBounds($item, $this->uploadtree_tablename);
125  $left = $itemTreeBounds->getLeft();
126  if (empty($left)) {
127  return $this->flushContent(_("Job unpack/adj2nest hasn't completed."));
128  }
129 
130  $vars['baseuri'] = Traceback_uri();
131  $vars['uploadId'] = $upload;
132  if ($request->get('show')=='quick') {
133  $item = $this->uploadDao->getFatItemId($item,$upload,$this->uploadtree_tablename);
134  }
135  $vars['itemId'] = $item;
136 
137  $vars['micromenu'] = Dir2Browse($this->Name, $item, NULL, $showBox = 0, "Browse",
138  -1, '', '', $this->uploadtree_tablename);
139  $vars['licenseArray'] = $this->licenseDao->getLicenseArray();
140 
141  $histVars = $this->showUploadHist($itemTreeBounds);
142  if (is_a($histVars, 'Symfony\\Component\\HttpFoundation\\RedirectResponse')) {
143  return $histVars;
144  }
145  $vars = array_merge($vars, $histVars);
146 
147  $vars['content'] = js_url();
148 
149  return $this->render("browse.html.twig",$this->mergeWithDefault($vars));
150  }
151 
152 
158  private function showUploadHist(ItemTreeBounds $itemTreeBounds)
159  {
160  $groupId = Auth::getGroupId();
161  $selectedAgentId = GetParm('agentId', PARM_INTEGER);
162  $tag_pk = GetParm("tag", PARM_INTEGER);
163 
164  $uploadId = $itemTreeBounds->getUploadId();
165  $scannerAgents = array_keys($this->agentNames);
166  $scanJobProxy = new ScanJobProxy($this->agentDao, $uploadId);
167  $scannerVars = $scanJobProxy->createAgentStatus($scannerAgents);
168  $agentMap = $scanJobProxy->getAgentMap();
169 
170  $vars = array('agentId' => GetParm('agentId', PARM_INTEGER),
171  'agentShowURI' => Traceback_uri() . '?mod=' . Traceback_parm(),
172  'agentMap' => $agentMap,
173  'scanners'=>$scannerVars);
174 
175  $selectedAgentIds = empty($selectedAgentId) ? $scanJobProxy->getLatestSuccessfulAgentIds() : $selectedAgentId;
176 
177  if (!empty($agentMap)) {
178  $licVars = $this->createLicenseHistogram($itemTreeBounds->getItemId(),
179  $tag_pk, $itemTreeBounds, $selectedAgentIds, $groupId);
180  $vars = array_merge($vars, $licVars);
181  }
182 
183  $this->licenseProjector = new LicenseMap($this->getObject('db.manager'),$groupId,LicenseMap::CONCLUSION,true);
184  $dirVars = $this->countFileListing($itemTreeBounds);
185  $childCount = $dirVars['iTotalRecords'];
186  /***************************************
187  * Problem: $ChildCount can be zero if you have a container that does not
188  * unpack to a directory. For example:
189  * file.gz extracts to archive.txt that contains a license.
190  * Same problem seen with .pdf and .Z files.
191  * Solution: if $ChildCount == 0, then just view the license!
192  *
193  * $ChildCount can also be zero if the directory is empty.
194  * **************************************/
195  if ($childCount == 0) {
196  return new RedirectResponse("?mod=view-license" . Traceback_parm_keep(array("upload", "item")));
197  }
198 
199  $vars['licenseUri'] = Traceback_uri() . "?mod=popup-license&rf=";
200  $vars['bulkUri'] = Traceback_uri() . "?mod=popup-license";
201 
202  return array_merge($vars, $dirVars);
203  }
204 
209  private function countFileListing(ItemTreeBounds $itemTreeBounds)
210  {
211  $isFlat = isset($_GET['flatten']);
212  $vars['isFlat'] = $isFlat;
213  $vars['iTotalRecords'] = $this->uploadDao->countNonArtifactDescendants($itemTreeBounds, $isFlat);
214  $uri = Traceback_uri().'?mod='.$this->Name.Traceback_parm_keep(array('upload','folder','show','item'));
215  $vars['fileSwitch'] = $isFlat ? $uri : $uri."&flatten=yes";
216  return $vars;
217  }
218 
219 
228  public function createLicenseHistogram($uploadTreeId, $tagId, ItemTreeBounds $itemTreeBounds, $agentIds, $groupId)
229  {
230  $fileCount = $this->uploadDao->countPlainFiles($itemTreeBounds);
231  $licenseHistogram = $this->licenseDao->getLicenseHistogram($itemTreeBounds, $agentIds);
232  $editedLicensesHist = $this->clearingDao->getClearedLicenseIdAndMultiplicities($itemTreeBounds, $groupId);
233 
234  $agentId = GetParm('agentId', PARM_INTEGER);
235  $licListUri = Traceback_uri()."?mod=license_list_files&item=$uploadTreeId";
236  if ($tagId) {
237  $licListUri .= "&tag=$tagId";
238  }
239  if ($agentId) {
240  $licListUri .= "&agentId=$agentId";
241  }
242 
243  /* Write license histogram to $VLic */
244  list($tableData, $totalScannerLicenseCount, $editedTotalLicenseCount)
245  = $this->createLicenseHistogramJSarray($licenseHistogram, $editedLicensesHist, $licListUri);
246 
247  $uniqueLicenseCount = count($tableData);
248  $scannerUniqueLicenseCount = count( $licenseHistogram );
249  $editedUniqueLicenseCount = count($editedLicensesHist);
250  $noScannerLicenseFoundCount = array_key_exists(LicenseDao::NO_LICENSE_FOUND, $licenseHistogram)
251  ? $licenseHistogram[LicenseDao::NO_LICENSE_FOUND]['count'] : 0;
252  $editedNoLicenseFoundCount = array_key_exists(LicenseDao::NO_LICENSE_FOUND, $editedLicensesHist)
253  ? $editedLicensesHist[LicenseDao::NO_LICENSE_FOUND]['count'] : 0;
254 
255  return array('tableDataJson'=>json_encode($tableData),
256  'uniqueLicenseCount'=>$uniqueLicenseCount,
257  'fileCount'=>$fileCount,
258  'scannerUniqueLicenseCount'=>$scannerUniqueLicenseCount,
259  'editedUniqueLicenseCount'=>$editedUniqueLicenseCount,
260  'scannerLicenseCount'=> $totalScannerLicenseCount,
261  'editedLicenseCount'=> $editedTotalLicenseCount-$editedNoLicenseFoundCount,
262  'noScannerLicenseFoundCount'=>$noScannerLicenseFoundCount,
263  'editedNoLicenseFoundCount'=>$editedNoLicenseFoundCount,
264  'scannerLicenses'=>$licenseHistogram,
265  'editedLicenses'=>$editedLicensesHist
266  );
267  }
268 
276  protected function createLicenseHistogramJSarray($scannerLics, $editedLics, $licListUri)
277  {
278  $allScannerLicenseNames = array_keys($scannerLics);
279  $allEditedLicenseNames = array_keys($editedLics);
280 
281  $allLicNames = array_unique(array_merge($allScannerLicenseNames, $allEditedLicenseNames));
282  $realLicNames = array_diff($allLicNames, array(LicenseDao::NO_LICENSE_FOUND));
283 
284  $totalScannerLicenseCount = 0;
285  $editedTotalLicenseCount = 0;
286 
287  $tableData = array();
288  foreach ($realLicNames as $licenseShortName) {
289  $count = 0;
290  if (array_key_exists($licenseShortName, $scannerLics)) {
291  $count = $scannerLics[$licenseShortName]['unique'];
292  $rfId = $scannerLics[$licenseShortName]['rf_pk'];
293  } else {
294  $rfId = $editedLics[$licenseShortName]['rf_pk'];
295  }
296  $editedCount = array_key_exists($licenseShortName, $editedLics) ? $editedLics[$licenseShortName]['count'] : 0;
297 
298  $totalScannerLicenseCount += $count;
299  $editedTotalLicenseCount += $editedCount;
300 
301  $scannerCountLink = ($count > 0) ? "<a href='$licListUri&lic=" .
302  urlencode($licenseShortName) . "'>$count</a>": "0";
303  $editedLink = ($editedCount > 0) ? $editedCount : "0";
304 
305  $tableData[] = array($scannerCountLink, $editedLink, array($licenseShortName,$rfId));
306  }
307 
308  return array($tableData, $totalScannerLicenseCount, $editedTotalLicenseCount);
309  }
310 
316  public function renderString($templateName, $vars)
317  {
318  return $this->renderer->load($templateName)->render($vars);
319  }
320 
326  public function getUploadHist($itemTreeBounds)
327  {
328  return $this->showUploadHist($itemTreeBounds);
329  }
330 }
331 
332 register_plugin(new BrowseLicense());
char * uploadtree_tablename
upload.uploadtree_tablename
Definition: adj2nest.c:100
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
static getGroupId()
Get the current user's group id.
Definition: Auth.php:80
Various utility functions to filter ClearingDecision.
Wrapper class for license map.
Definition: LicenseMap.php:19
RegisterMenus()
Customize submenus.
render($templateName, $vars=null, $headers=null)
countFileListing(ItemTreeBounds $itemTreeBounds)
createLicenseHistogram($uploadTreeId, $tagId, ItemTreeBounds $itemTreeBounds, $agentIds, $groupId)
showUploadHist(ItemTreeBounds $itemTreeBounds)
Given an $Uploadtree_pk, display:
createLicenseHistogramJSarray($scannerLics, $editedLics, $licListUri)
renderString($templateName, $vars)
js_url()
Load a new url.
Dir2Browse($Mod, $UploadtreePk, $LinkLast=NULL, $ShowBox=1, $ShowMicro=NULL, $Enumerate=-1, $PreText='', $PostText='', $uploadtree_tablename="uploadtree")
Get an html linked string of a file browse path.
Definition: common-dir.php:263
menu_insert($Path, $LastOrder=0, $URI=NULL, $Title=NULL, $Target=NULL, $HTML=NULL)
Given a Path, order level for the last item, and optional plugin name, insert the menu item.
Traceback_uri()
Get the URI without query to this location.
Definition: common-parm.php:97
Traceback_parm($ShowMod=1)
Get the URI query to this location.
const PARM_INTEGER
Definition: common-parm.php:14
const PARM_STRING
Definition: common-parm.php:18
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:46
Traceback_parm_keep($List)
Create a new URI, keeping only these items.
list_t type structure used to keep various lists. (e.g. there are multiple lists).
Definition: nomos.h:308