FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
list.php
Go to the documentation of this file.
1 <?php
2 /***********************************************************
3  Copyright (C) 2010-2012 Hewlett-Packard Development Company, L.P.
4  Copyright (C) 2013-2016, 2018 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 
23 
31 define("TITLE_COPYRIGHT_LIST", _("List Files for Copyright/Email/URL"));
32 
34 {
38  private $dbManager;
39 
43  private $uploadDao;
44 
45  function __construct()
46  {
47  $this->Name = "copyright-list";
48  $this->Title = TITLE_COPYRIGHT_LIST;
49  $this->Version = "1.0";
50  $this->Dependency = array("copyright-hist", "ecc-hist");
51  $this->DBaccess = PLUGIN_DB_READ;
52  $this->LoginFlag = 0;
53  $this->NoMenu = 0;
54 
55  parent::__construct();
56  global $container;
57  $this->dbManager = $container->get('db.manager');
58  $this->uploadDao = $container->get('dao.upload');
59  }
60 
65  function RegisterMenus()
66  {
67  if ($this->State != PLUGIN_STATE_READY) {
68  return(0);
69  }
70 
71  // micro-menu
72  $agent_pk = GetParm("agent",PARM_INTEGER);
73  $uploadtree_pk = GetParm("item",PARM_INTEGER);
74  $hash = GetParm("hash",PARM_RAW);
75  $type = GetParm("type",PARM_RAW);
76  $Excl = GetParm("excl",PARM_RAW);
77 
78  $URL = $this->Name . "&agent=$agent_pk&item=$uploadtree_pk&hash=$hash&type=$type&page=-1";
79  if (!empty($Excl)) {
80  $URL .= "&excl=$Excl";
81  }
82  $text = _("Show All Files");
83  menu_insert($this->Name."::Show All",0, $URL, $text);
84  } // RegisterMenus()
85 
97  function GetRows($Uploadtree_pk, $Agent_pk, &$upload_pk, $hash, $type, $tableName)
98  {
99  /******* Get license names and counts ******/
100  $row = $this->uploadDao->getUploadEntry($Uploadtree_pk);
101  $lft = $row["lft"];
102  $rgt = $row["rgt"];
103  $upload_pk = $row["upload_fk"];
104  $params = [];
105 
106  if ($type == "copyFindings") {
107  $sql = "SELECT textfinding AS content, '$type' AS type, uploadtree_pk, ufile_name, PF
108  FROM $tableName,
109  (SELECT uploadtree_pk, pfile_fk AS PF, ufile_name FROM uploadtree
110  WHERE upload_fk=$1
111  AND uploadtree.lft BETWEEN $2 AND $3) AS SS
112  WHERE PF=pfile_fk AND hash=$4 ORDER BY uploadtree_pk";
113  $params = [
114  $upload_pk, $lft, $rgt, $hash
115  ];
116  } else {
117  /* get all the copyright records for this uploadtree. */
118  $sql = "SELECT content, type, uploadtree_pk, ufile_name, PF
119  FROM $tableName,
120  (SELECT uploadtree_pk, pfile_fk AS PF, ufile_name FROM uploadtree
121  WHERE upload_fk=$1
122  AND uploadtree.lft BETWEEN $2 AND $3) AS SS
123  WHERE PF=pfile_fk AND agent_fk=$4 AND hash=$5 AND type=$6 ORDER BY uploadtree_pk";
124  $params = [
125  $upload_pk, $lft, $rgt, $Agent_pk, $hash, $type
126  ];
127  }
128  $statement = __METHOD__.$tableName;
129  $this->dbManager->prepare($statement, $sql);
130  $result = $this->dbManager->execute($statement,$params);
131 
132  $rows = $this->dbManager->fetchAll($result);
133  $this->dbManager->freeResult($result);
134 
135  return $rows;
136  }
137 
147  function GetRequestedRows($rows, $excl, &$NumRows, $filter)
148  {
149  $NumRows = count($rows);
150  $prev = 0;
151  $ExclArray = explode(":", $excl);
152 
153  /* filter will need to know the rf_pk of "No_license_found" or "Void" */
154  if (!empty($filter))
155  {
156  $NoLicStr = "No_license_found";
157  $VoidLicStr = "Void";
158  $rf_clause = "";
159 
160  $sql = "select rf_pk from license_ref where rf_shortname IN ($1, $2)";
161  $statement = __METHOD__."NoLicenseFoundORVoid";
162  $this->dbManager->prepare($statement, $sql);
163  $result = $this->dbManager->execute($statement,array("$NoLicStr", "$VoidLicStr"));
164  $rf_rows = $this->dbManager->fetchAll($result);
165  if(!empty($rf_rows)){
166  foreach($rf_rows as $row)
167  {
168  if (!empty($rf_clause)) { $rf_clause .= " or ";
169  }
170  $rf_clause .= " rf_fk=$row[rf_pk]";
171  }
172  }
173  $this->dbManager->freeResult($result);
174  }
175 
176  for($RowIdx = 0; $RowIdx < $NumRows; $RowIdx++)
177  {
178  $row = $rows[$RowIdx];
179  /* remove excluded files */
180  if ($excl)
181  {
182  $FileExt = GetFileExt($rows[$RowIdx]['ufile_name']);
183  if (in_array($FileExt, $ExclArray))
184  {
185  unset($rows[$RowIdx]);
186  continue;
187  }
188  }
189 
190  /* apply filters */
191  if (($filter == "nolic") and ($rf_clause))
192  {
193  /* discard file unless it has no license */
194  $sql = "select rf_fk from license_file where ($rf_clause) and pfile_fk=$1";
195  $statement = __METHOD__."CheckForNoLicenseFound";
196  $this->dbManager->prepare($statement, $sql);
197  $result = $this->dbManager->execute($statement,array("{$row['pf']}"));
198  $FoundRows = $this->dbManager->fetchAll($result);
199  if (empty($FoundRows))
200  {
201  unset($rows[$RowIdx]);
202  continue;
203  }
204  }
205  }
206 
207  /* reset array keys, keep order (uploadtree_pk) */
208  $rows2 = array();
209  foreach ($rows as $row) {
210  $rows2[] = $row;
211  }
212  unset($rows);
213 
214  /* remove duplicate files */
215  $NumRows = count($rows2);
216  $prev = 0;
217  for($RowIdx = 0; $RowIdx < $NumRows; $RowIdx++)
218  {
219  if ($RowIdx > 0)
220  {
221  /* Since rows are ordered by uploadtree_pk,
222  * remove duplicate uploadtree_pk's. This can happen if there
223  * are multiple same copyrights in one file.
224  */
225  if ($rows2[$RowIdx-1]['uploadtree_pk'] == $rows2[$RowIdx]['uploadtree_pk']) {
226  unset($rows2[$RowIdx-1]);
227  }
228  }
229  }
230 
231  /* sort by name so output has some order */
232  usort($rows2, 'copyright_namecmp');
233 
234  return $rows2;
235  }
236 
241  function OutputOpen()
242  {
243 
244  if ($this->State != PLUGIN_STATE_READY) {
245  return(0);
246  }
247 
248  return parent::OutputOpen();
249  }
250 
255  function Output()
256  {
257  if ($this->State != PLUGIN_STATE_READY) {
258  return;
259  }
260 
261  $OutBuf = "";
262  $Time = microtime(true);
263  $Max = 50;
264 
265  /* Input parameters */
266  $agent_pk = GetParm("agent",PARM_INTEGER);
267  $uploadtree_pk = GetParm("item",PARM_INTEGER);
268  $hash = GetParm("hash",PARM_RAW);
269  $type = GetParm("type",PARM_RAW);
270  $excl = GetParm("excl",PARM_RAW);
271  $filter = GetParm("filter",PARM_RAW);
272  if (empty($uploadtree_pk) || empty($hash) || empty($type) || empty($agent_pk))
273  {
274  $this->vars['pageContent'] = $this->Name . _(" is missing required parameters");
275  return;
276  }
277 
278  /* Check item1 and item2 upload permissions */
279  $Row = $this->uploadDao->getUploadEntry($uploadtree_pk);
280  if (!$this->uploadDao->isAccessible($Row['upload_fk'], Auth::getGroupId()))
281  {
282  $this->vars['pageContent'] = "<h2>" . _("Permission Denied") . "</h2>";
283  return;
284  }
285 
286  $Page = GetParm("page",PARM_INTEGER);
287  if (empty($Page) || $Page == -1) {
288  $Page=0;
289  }
290 
291  list($tableName,$modBack,$viewName) = $this->getTableName($type);
292 
293  /* get all rows */
294  $upload_pk = -1;
295  $allRows = $this->GetRows($uploadtree_pk, $agent_pk, $upload_pk, $hash, $type, $tableName);
296  $uploadtree_tablename = $this->uploadDao->getUploadtreeTableName($upload_pk);
297 
298  /* slim down to all rows with this hash and type, and filter */
299  $NumInstances = 0;
300  $rows = $this->GetRequestedRows($allRows, $excl, $NumInstances, $filter);
301 
302  // micro menus
303  $OutBuf .= menu_to_1html(menu_find($this->Name, $MenuDepth),0);
304 
305  $RowCount = count($rows);
306  if ($RowCount)
307  {
308  $TypeStr = "";
309  $Content = htmlentities($rows[0]['content']);
310  $Offset = ($Page < 0) ? 0 : $Page*$Max;
311  $PkgsOnly = false;
312  $text = _("files");
313  $text1 = _("unique");
314  $text3 = _("copyright");
315  $text4 = _("email");
316  $text5 = _("url");
317  switch ($type)
318  {
319  case "statement":
320  $TypeStr = "$text3";
321  break;
322  case "email":
323  $TypeStr = "$text4";
324  break;
325  case "url":
326  $TypeStr = "$text5";
327  break;
328  case "ecc":
329  $TypeStr = _("export restriction");
330  break;
331  case "keyword":
332  $TypeStr = _("Keyword Analysis");
333  break;
334  case "copyFindings":
335  $TypeStr = _("User Findings");
336  }
337  $OutBuf .= "$NumInstances $TypeStr instances found in $RowCount $text";
338 
339  $OutBuf .= ": <b>$Content</b>";
340 
341  $text = _("Display excludes files with these extensions");
342  if (!empty($excl)) { $OutBuf .= "<br>$text: $excl";
343  }
344 
345  /* Get the page menu */
346  if (($RowCount >= $Max) && ($Page >= 0))
347  {
348  $PagingMenu = "<P />\n" . MenuPage($Page, intval($RowCount / $Max)) . "<P />\n";
349  $OutBuf .= $PagingMenu;
350  }
351  else
352  {
353  $PagingMenu = "";
354  }
355 
356  /* Offset is +1 to start numbering from 1 instead of zero */
357  $LinkLast = "$viewName&agent=$agent_pk";
358  $ShowBox = 1;
359  $ShowMicro=NULL;
360 
361  $baseURL = "?mod=" . $this->Name . "&agent=$agent_pk&item=$uploadtree_pk&hash=$hash&type=$type&page=-1";
362 
363  // display rows
364  $RowNum = 0;
365  foreach($rows as $row)
366  {
367  ++$RowNum;
368  if ($RowNum < $Offset) {
369  continue;
370  }
371  if ($RowNum > $Offset + $Max) {
372  break;
373  }
374 
375  // Allow user to exclude files with this extension
376  $FileExt = GetFileExt($row['ufile_name']);
377  if (empty($excl)) {
378  $URL = $baseURL . "&excl=$FileExt";
379  } else {
380  $URL = $baseURL . "&excl=$excl:$FileExt";
381  }
382 
383  $text = _("Exclude this file type");
384  $Header = "<a href=$URL>$text.</a>";
385 
386  $ok = true;
387  if ($excl)
388  {
389  $ExclArray = explode(":", $excl);
390  if (in_array($FileExt, $ExclArray)) {
391  $ok = false;
392  }
393  }
394 
395  if ($ok)
396  {
397  $OutBuf .= Dir2Browse($modBack, $row['uploadtree_pk'], $LinkLast,
398  $ShowBox, $ShowMicro, $RowNum, $Header, '', $uploadtree_tablename);
399  }
400  }
401  }
402  else
403  {
404  $OutBuf .= _("No files found");
405  }
406 
407  if (!empty($PagingMenu)) {
408  $OutBuf .= $PagingMenu . "\n";
409  }
410  $OutBuf .= "<hr>\n";
411  $Time = microtime(true) - $Time;
412  $text = _("Elapsed time");
413  $text1 = _("seconds");
414  $OutBuf .= sprintf("<small>$text: %.2f $text1</small>\n", $Time);
415 
416  $this->vars['pageContent'] = $OutBuf;
417  return;
418  }
419 
424  function getTemplateName()
425  {
426  return 'copyrightlist.html.twig';
427  }
428 
434  private function getTableName($type)
435  {
436 
437  switch ($type) {
438  case "ecc" :
439  $tableName = "ecc";
440  $modBack = "ecc-hist";
441  $viewName = "ecc-view";
442  break;
443  case "keyword" :
444  $tableName = "keyword";
445  $modBack = "keyword-hist";
446  $viewName = "keyword-view";
447  break;
448  case "statement" :
449  $tableName = "copyright";
450  $modBack = "copyright-hist";
451  $viewName = "copyright-view";
452  break;
453  case "copyFindings" :
454  $tableName = "copyright_decision";
455  $modBack = "copyright-hist";
456  $viewName = "copyright-view";
457  break;
458  default:
459  $tableName = "author";
460  $modBack = "email-hist";
461  $viewName = "copyright-view";
462  }
463  return array($tableName, $modBack,$viewName);
464  }
465 }
466 
467 $NewPlugin = new copyright_list;
468 $NewPlugin->Initialize();
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
const PARM_RAW
Definition: common-parm.php:33
getTemplateName()
Definition: list.php:424
menu_find($Name, &$MaxDepth, $Menu=NULL)
Given a top-level menu name, find the list of sub-menus below it and max depth of menu...
MenuPage($Page, $TotalPage, $Uri= '')
Create a "First Prev 1 2 ... Next Last" page links for paged output.
Definition: common-menu.php:71
OutputOpen()
This function is called when user output is requested. This function is responsible for assigning hea...
Definition: list.php:241
#define PLUGIN_DB_READ
Plugin requires read permission on DB.
Definition: libfossology.h:49
Definition: state.hpp:26
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:57
menu_to_1html($Menu, $ShowRefresh=1, $ShowTraceback=0, $ShowAll=1)
Take a menu and render it as one HTML line.
GetRows($Uploadtree_pk, $Agent_pk, &$upload_pk, $hash, $type, $tableName)
Get statement rows for a specified set.
Definition: list.php:97
GetFileExt($fname)
Get File Extension (text after last period)
Definition: common-ui.php:127
GetRequestedRows($rows, $excl, &$NumRows, $filter)
Remove unwanted rows by hash and type and exclusions and filter.
Definition: list.php:147
const PARM_INTEGER
Definition: common-parm.php:25
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28
This is the Plugin class. All plugins should:
Definition: FO_Plugin.php:67
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...
getTableName($type)
Get the table name, mod, and view based on type.
Definition: list.php:434
RegisterMenus()
While menus can be added to any time at or after the PostInitialize phase, this is the standard locat...
Definition: list.php:65
Output()
This function is called when user output is requested. This function is responsible for content...
Definition: list.php:255