FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
BrowseLicense.php
1 <?php
2 /***********************************************************
3  * Copyright (C) 2008-2015 Hewlett-Packard Development Company, L.P.
4  * 2014-2015 Siemens AG
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * version 2 as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  ***********************************************************/
19 
20 namespace Fossology\UI\Page;
21 
37 
44 {
45  const NAME = "license";
46 
47  private $uploadtree_tablename = "";
49  private $uploadDao;
51  private $licenseDao;
53  private $clearingDao;
55  private $agentDao;
57  private $clearingFilter;
59  private $licenseProjector;
61  protected $agentNames = AgentRef::AGENT_LIST;
62 
63  public function __construct()
64  {
65  parent::__construct(self::NAME, array(
66  self::TITLE => _("License Browser"),
67  self::DEPENDENCIES => array("browse", "view"),
68  self::PERMISSION => Auth::PERM_READ,
69  self::REQUIRES_LOGIN => false
70  ));
71 
72  global $container;
73  $this->uploadDao = $container->get('dao.upload');
74  $this->licenseDao = $container->get('dao.license');
75  $this->clearingDao = $container->get('dao.clearing');
76  $this->agentDao = $container->get('dao.agent');
77  $this->clearingFilter = $container->get('businessrules.clearing_decision_filter');
78  }
79 
83  function RegisterMenus()
84  {
85  // For all other menus, permit coming back here.
86  $URI = $this->Name . Traceback_parm_keep(array("upload", "item"));
87 
88  $Item = GetParm("item", PARM_INTEGER);
89  $Upload = GetParm("upload", PARM_INTEGER);
90  if (empty($Item) || empty($Upload)) {
91  return;
92  }
93  $viewLicenseURI = "view-license" . Traceback_parm_keep(array("show", "format", "page", "upload", "item"));
94  $menuName = $this->Title;
95  if (GetParm("mod", PARM_STRING) == self::NAME) {
96  menu_insert("Browse::$menuName", 99);
97  menu_insert("View::$menuName", 99);
98  menu_insert("View-Meta::$menuName", 99);
99  } else {
100  $text = _("license histogram");
101  menu_insert("Browse::$menuName", 99, $URI, $text);
102  menu_insert("View::$menuName", 99, $viewLicenseURI, $text);
103  menu_insert("View-Meta::$menuName", 99, $viewLicenseURI, $text);
104  }
105  }
106 
111  protected function handle(Request $request)
112  {
113  $upload = intval($request->get("upload"));
114  $groupId = Auth::getGroupId();
115  if (!$this->uploadDao->isAccessible($upload, $groupId)) {
116  return $this->flushContent(_("Permission Denied"));
117  }
118 
119  $item = intval($request->get("item"));
120 
121  $vars['baseuri'] = Traceback_uri();
122  $vars['uploadId'] = $upload;
123  $this->uploadtree_tablename = $this->uploadDao->getUploadtreeTableName($upload);
124  if ($request->get('show')=='quick') {
125  $item = $this->uploadDao->getFatItemId($item,$upload,$this->uploadtree_tablename);
126  }
127  $vars['itemId'] = $item;
128 
129  $vars['micromenu'] = Dir2Browse($this->Name, $item, NULL, $showBox = 0, "Browse",
130  -1, '', '', $this->uploadtree_tablename);
131  $vars['licenseArray'] = $this->licenseDao->getLicenseArray();
132 
133  $itemTreeBounds = $this->uploadDao->getItemTreeBounds($item, $this->uploadtree_tablename);
134  $left = $itemTreeBounds->getLeft();
135  if (empty($left)) {
136  return $this->flushContent(_("Job unpack/adj2nest hasn't completed."));
137  }
138  $histVars = $this->showUploadHist($itemTreeBounds);
139  if (is_a($histVars, 'Symfony\\Component\\HttpFoundation\\RedirectResponse')) {
140  return $histVars;
141  }
142  $vars = array_merge($vars, $histVars);
143 
144  $vars['content'] = js_url();
145 
146  return $this->render("browse.html.twig",$this->mergeWithDefault($vars));
147  }
148 
149 
155  private function showUploadHist(ItemTreeBounds $itemTreeBounds)
156  {
157  $groupId = Auth::getGroupId();
158  $selectedAgentId = GetParm('agentId', PARM_INTEGER);
159  $tag_pk = GetParm("tag", PARM_INTEGER);
160 
161  $uploadId = $itemTreeBounds->getUploadId();
162  $scannerAgents = array_keys($this->agentNames);
163  $scanJobProxy = new ScanJobProxy($this->agentDao, $uploadId);
164  $scannerVars = $scanJobProxy->createAgentStatus($scannerAgents);
165  $agentMap = $scanJobProxy->getAgentMap();
166 
167  $vars = array('agentId' => GetParm('agentId', PARM_INTEGER),
168  'agentShowURI' => Traceback_uri() . '?mod=' . Traceback_parm(),
169  'agentMap' => $agentMap,
170  'scanners'=>$scannerVars);
171 
172  $selectedAgentIds = empty($selectedAgentId) ? $scanJobProxy->getLatestSuccessfulAgentIds() : $selectedAgentId;
173 
174  if (!empty($agentMap)) {
175  $licVars = $this->createLicenseHistogram($itemTreeBounds->getItemId(),
176  $tag_pk, $itemTreeBounds, $selectedAgentIds, $groupId);
177  $vars = array_merge($vars, $licVars);
178  }
179 
180  $this->licenseProjector = new LicenseMap($this->getObject('db.manager'),$groupId,LicenseMap::CONCLUSION,true);
181  $dirVars = $this->countFileListing($itemTreeBounds);
182  $childCount = $dirVars['iTotalRecords'];
183  /***************************************
184  * Problem: $ChildCount can be zero if you have a container that does not
185  * unpack to a directory. For example:
186  * file.gz extracts to archive.txt that contains a license.
187  * Same problem seen with .pdf and .Z files.
188  * Solution: if $ChildCount == 0, then just view the license!
189  *
190  * $ChildCount can also be zero if the directory is empty.
191  * **************************************/
192  if ($childCount == 0) {
193  return new RedirectResponse("?mod=view-license" . Traceback_parm_keep(array("upload", "item")));
194  }
195 
196  $vars['licenseUri'] = Traceback_uri() . "?mod=popup-license&rf=";
197  $vars['bulkUri'] = Traceback_uri() . "?mod=popup-license";
198 
199  return array_merge($vars, $dirVars);
200  }
201 
206  private function countFileListing(ItemTreeBounds $itemTreeBounds)
207  {
208  $isFlat = isset($_GET['flatten']);
209  $vars['isFlat'] = $isFlat;
210  $vars['iTotalRecords'] = $this->uploadDao->countNonArtifactDescendants($itemTreeBounds, $isFlat);
211  $uri = Traceback_uri().'?mod='.$this->Name.Traceback_parm_keep(array('upload','folder','show','item'));
212  $vars['fileSwitch'] = $isFlat ? $uri : $uri."&flatten=yes";
213  return $vars;
214  }
215 
216 
225  private function createLicenseHistogram($uploadTreeId, $tagId, ItemTreeBounds $itemTreeBounds, $agentIds, $groupId)
226  {
227  $fileCount = $this->uploadDao->countPlainFiles($itemTreeBounds);
228  $licenseHistogram = $this->licenseDao->getLicenseHistogram($itemTreeBounds, $agentIds);
229  $editedLicensesHist = $this->clearingDao->getClearedLicenseIdAndMultiplicities($itemTreeBounds, $groupId);
230 
231  $agentId = GetParm('agentId', PARM_INTEGER);
232  $licListUri = Traceback_uri()."?mod=license_list_files&item=$uploadTreeId";
233  if ($tagId) {
234  $licListUri .= "&tag=$tagId";
235  }
236  if ($agentId) {
237  $licListUri .= "&agentId=$agentId";
238  }
239 
240  /* Write license histogram to $VLic */
241  list($tableData, $totalScannerLicenseCount, $editedTotalLicenseCount)
242  = $this->createLicenseHistogramJSarray($licenseHistogram, $editedLicensesHist, $licListUri);
243 
244  $uniqueLicenseCount = count($tableData);
245  $scannerUniqueLicenseCount = count( $licenseHistogram );
246  $editedUniqueLicenseCount = count($editedLicensesHist);
247  $noScannerLicenseFoundCount = array_key_exists(LicenseDao::NO_LICENSE_FOUND, $licenseHistogram)
248  ? $licenseHistogram[LicenseDao::NO_LICENSE_FOUND]['count'] : 0;
249  $editedNoLicenseFoundCount = array_key_exists(LicenseDao::NO_LICENSE_FOUND, $editedLicensesHist)
250  ? $editedLicensesHist[LicenseDao::NO_LICENSE_FOUND]['count'] : 0;
251 
252  $vars = array('tableDataJson'=>json_encode($tableData),
253  'uniqueLicenseCount'=>$uniqueLicenseCount,
254  'fileCount'=>$fileCount,
255  'scannerUniqueLicenseCount'=>$scannerUniqueLicenseCount,
256  'editedUniqueLicenseCount'=>$editedUniqueLicenseCount,
257  'scannerLicenseCount'=> $totalScannerLicenseCount,
258  'editedLicenseCount'=> $editedTotalLicenseCount-$editedNoLicenseFoundCount,
259  'noScannerLicenseFoundCount'=>$noScannerLicenseFoundCount,
260  'editedNoLicenseFoundCount'=>$editedNoLicenseFoundCount,
261  'scannerLicenses'=>$licenseHistogram,
262  'editedLicenses'=>$editedLicensesHist
263  );
264 
265  return $vars;
266  }
267 
275  protected function createLicenseHistogramJSarray($scannerLics, $editedLics, $licListUri)
276  {
277  $allScannerLicenseNames = array_keys($scannerLics);
278  $allEditedLicenseNames = array_keys($editedLics);
279 
280  $allLicNames = array_unique(array_merge($allScannerLicenseNames, $allEditedLicenseNames));
281  $realLicNames = array_diff($allLicNames, array(LicenseDao::NO_LICENSE_FOUND));
282 
283  $totalScannerLicenseCount = 0;
284  $editedTotalLicenseCount = 0;
285 
286  $tableData = array();
287  foreach ($realLicNames as $licenseShortName) {
288  $count = 0;
289  if (array_key_exists($licenseShortName, $scannerLics)) {
290  $count = $scannerLics[$licenseShortName]['unique'];
291  $rfId = $scannerLics[$licenseShortName]['rf_pk'];
292  } else {
293  $rfId = $editedLics[$licenseShortName]['rf_pk'];
294  }
295  $editedCount = array_key_exists($licenseShortName, $editedLics) ? $editedLics[$licenseShortName]['count'] : 0;
296 
297  $totalScannerLicenseCount += $count;
298  $editedTotalLicenseCount += $editedCount;
299 
300  $scannerCountLink = ($count > 0) ? "<a href='$licListUri&lic=" .
301  urlencode($licenseShortName) . "'>$count</a>": "0";
302  $editedLink = ($editedCount > 0) ? $editedCount : "0";
303 
304  $tableData[] = array($scannerCountLink, $editedLink, array($licenseShortName,$rfId));
305  }
306 
307  return array($tableData, $totalScannerLicenseCount, $editedTotalLicenseCount);
308  }
309 
315  public function renderString($templateName, $vars)
316  {
317  return $this->renderer->loadTemplate($templateName)->render($vars);
318  }
319 
325  public function getUploadHist($itemTreeBounds)
326  {
327  return $this->showUploadHist($itemTreeBounds);
328  }
329 }
330 
331 register_plugin(new BrowseLicense());
countFileListing(ItemTreeBounds $itemTreeBounds)
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:274
Traceback_uri()
Get the URI without query to this location.
createLicenseHistogram($uploadTreeId, $tagId, ItemTreeBounds $itemTreeBounds, $agentIds, $groupId)
Traceback_parm($ShowMod=1)
Get the URI query to this location.
render($templateName, $vars=null, $headers=null)
Wrapper class for license map.
Definition: LicenseMap.php:29
createLicenseHistogramJSarray($scannerLics, $editedLics, $licListUri)
RegisterMenus()
Customize submenus.
renderString($templateName, $vars)
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:57
const PARM_STRING
Definition: common-parm.php:29
const PARM_INTEGER
Definition: common-parm.php:25
char * uploadtree_tablename
upload.uploadtree_tablename
Definition: adj2nest.c:112
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...
list_t type structure used to keep various lists. (e.g. there are multiple lists).
Definition: nomos.h:321
js_url()
Load a new url.
showUploadHist(ItemTreeBounds $itemTreeBounds)
Given an $Uploadtree_pk, display:
static getGroupId()
Get the current user&#39;s group id.
Definition: Auth.php:78
Traceback_parm_keep($List)
Create a new URI, keeping only these items.