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