FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
ui-view-info.php
1 <?php
2 /***********************************************************
3  Copyright (C) 2008-2014 Hewlett-Packard Development Company, L.P.
4  Copyright (C) 2015,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 
24 
25 class ui_view_info extends FO_Plugin
26 {
28  private $uploadDao;
30  private $dbManager;
33  private $userDao;
34 
35  function __construct()
36  {
37  $this->Name = "view_info";
38  $this->Title = _("View File Information");
39  $this->Dependency = array("browse");
40  $this->DBaccess = PLUGIN_DB_READ;
41  $this->LoginFlag = 0;
42  parent::__construct();
43  $this->uploadDao = $GLOBALS['container']->get('dao.upload');
44  $this->dbManager = $GLOBALS['container']->get('db.manager');
45  $this->userDao = $GLOBALS['container']->get('dao.user');
46  }
47 
51  function RegisterMenus()
52  {
53  $tooltipText = _("View file information");
54  menu_insert("Browse-Pfile::Info",5,$this->Name,$tooltipText);
55  // For the Browse menu, permit switching between detail and summary.
56  $Parm = Traceback_parm_keep(array("upload","item","format"));
57  $URI = $this->Name . $Parm;
58 
59  $menuPosition = 60;
60  $menuText = "Info";
61  if (GetParm("mod", PARM_STRING) == $this->Name) {
62  menu_insert("View::[BREAK]", 61);
63  menu_insert("View::[BREAK]", 50);
64  menu_insert("View::{$menuText}", $menuPosition);
65  menu_insert("View-Meta::[BREAK]", 61);
66  menu_insert("View-Meta::[BREAK]", 50);
67  menu_insert("View-Meta::{$menuText}", $menuPosition);
68 
69  menu_insert("Browse::Info",-3);
70  } else {
71  $tooltipText = _("View information about this file");
72  menu_insert("View::[BREAK]", 61);
73  menu_insert("View::[BREAK]", 50);
74  menu_insert("View::{$menuText}", $menuPosition, $URI, $tooltipText);
75  menu_insert("View-Meta::[BREAK]", 61);
76  menu_insert("View-Meta::[BREAK]", 50);
77  menu_insert("View-Meta::{$menuText}", $menuPosition, $URI, $tooltipText);
78 
79  menu_insert("Browse::Info", -3, $URI, $tooltipText);
80  }
81  } // RegisterMenus()
82 
86  function ShowView($Upload, $Item, $ShowMenu=0)
87  {
88  $vars = [];
89  if (empty($Upload) || empty($Item)) {
90  return;
91  }
92 
93  $Page = GetParm("page",PARM_INTEGER);
94  if (empty($Page)) {
95  $Page = 0;
96  }
97  $vars['repoLocPage'] = $Page;
98 
104  if ($Page == 0) {
105  $sql = "SELECT * FROM uploadtree
106  INNER JOIN pfile ON uploadtree_pk = $1
107  AND pfile_fk = pfile_pk
108  LIMIT 1;";
109  $row = $this->dbManager->getSingleRow($sql, array($Item),
110  __METHOD__ . "GetFileDescribingRow");
111  $bytes = $row['pfile_size'];
112  $bytesH = HumanSize($bytes);
113  $bytes = number_format($bytes, 0, "", ",") . ' B';
114  if ($bytesH == $bytes) {
115  $bytesH = "";
116  } else {
117  $bytesH = '(' . $bytesH . ')';
118  }
119  $vars['sizeInBytes'] = $bytes;
120  $vars['sizeInMB'] = $bytesH;
121  $vars['fileSha1'] = $row['pfile_sha1'];
122  $vars['fileMd5'] = $row['pfile_md5'];
123  $vars['fileSha256'] = $row['pfile_sha256'];
124  $vars['fileSize'] = $row['pfile_size'];
125  $vars['filePfileId'] = $row['pfile_fk'];
126  }
127  return $vars;
128  } // ShowView()
129 
133  function ShowSightings($Upload, $Item)
134  {
135  $v = "";
136  if (empty($Upload) || empty($Item)) {
137  return $vars;
138  }
139 
140  $page = GetParm("page",PARM_INTEGER);
141  if (empty($page)) {
142  $page = 0;
143  }
144  $MAX = 50;
145  $offset = $page * $MAX;
146  /**********************************
147  List the directory locations where this pfile is found
148  **********************************/
149  $sql = "SELECT * FROM pfile,uploadtree
150  WHERE pfile_pk=pfile_fk
151  AND pfile_pk IN
152  (SELECT pfile_fk FROM uploadtree WHERE uploadtree_pk = $1)
153  LIMIT $2 OFFSET $3";
154  $this->dbManager->prepare(__METHOD__."getListOfFiles",$sql);
155  $result = $this->dbManager->execute(__METHOD__."getListOfFiles",array($Item,$MAX,$offset));
156  $count = pg_num_rows($result);
157  if (($page > 0) || ($count >= $MAX)) {
158  $vMenu = "<p>\n" . MenuEndlessPage($page, ($count >= $MAX)) . "</p>\n";
159  } else {
160  $vMenu = "";
161  }
162  if ($count > 0) {
163  $v .= _("This exact file appears in the following locations:\n");
164  $v .= $vMenu;
165  $offset++;
166  $v .= Dir2FileList($result,"browse","view",$offset);
167  $v .= $vMenu;
168  } else if ($page > 0) {
169  $v .= _("End of listing.\n");
170  } else {
171  $v .= _("This file does not appear in any other known location.\n");
172  }
173  pg_free_result($result);
174 
175  $vars = [];
176  $vars['sightingsContent'] = $v;
177  return $vars;
178  } //ShowSightings()
179 
183  function ShowMetaView($Upload, $Item)
184  {
185  $vars = [];
186  if (empty($Item) || empty($Upload)) {
187  return $vars;
188  }
189 
190  /* display mimetype */
191  $sql = "SELECT * FROM uploadtree where uploadtree_pk = $1";
192  $this->dbManager->prepare(__METHOD__."DisplayMimetype",$sql);
193  $result = $this->dbManager->execute(__METHOD__."DisplayMimetype",array($Item));
194  if (pg_num_rows($result)) {
195  $vars['fileInfo'] = 1;
196  $row = pg_fetch_assoc($result);
197 
198  if (! empty($row['mimetype_pk'])) {
199  $vars['displayMimeTypeName'] = $row['mimetype_name'];
200  }
201  } else {
202  // bad uploadtree_pk
203  $vars['fileInfo'] = 0;
204  return $vars;
205  }
206  $this->dbManager->freeResult($result);
207 
208  /* get mimetype */
209  if (! empty($row['pfile_fk'])) {
210  $sql = "select mimetype_name from pfile, mimetype where pfile_pk = $1 and pfile_mimetypefk=mimetype_pk";
211  $this->dbManager->prepare(__METHOD__ . "GetMimetype", $sql);
212  $result = $this->dbManager->execute(__METHOD__ . "GetMimetype",
213  array($row['pfile_fk']));
214  if (pg_num_rows($result)) {
215  $pmRow = pg_fetch_assoc($result);
216  $vars['getMimeTypeName'] = $pmRow['mimetype_name'];
217  }
218  $this->dbManager->freeResult($result);
219  }
220 
221  /* display upload origin */
222  $sql = "select * from upload where upload_pk=$1";
223  $row = $this->dbManager->getSingleRow($sql, array($row['upload_fk']),
224  __METHOD__ . "getUploadOrigin");
225  if ($row) {
226 
227  /* upload source */
228  if ($row['upload_mode'] & 1 << 2) {
229  $text = _("Added by URL");
230  } else if ($row['upload_mode'] & 1 << 3) {
231  $text = _("Added by file upload");
232  } else if ($row['upload_mode'] & 1 << 4) {
233  $text = _("Added from filesystem");
234  }
235  $vars['fileUploadOriginInfo'] = $text;
236  $vars['fileUploadOrigin'] = $row['upload_origin'];
237 
238  /* upload time */
239  $ts = $row['upload_ts'];
240  $vars['fileUploadDate'] = substr($ts, 0, strrpos($ts, '.'));
241  }
242  /* display where it was uploaded from */
243 
244  /* display upload owner*/
245  $sql = "SELECT user_name from users, upload where user_pk = user_fk and upload_pk = $1";
246  $row = $this->dbManager->getSingleRow($sql,array($Upload),__METHOD__."getUploadOwner");
247 
248  $vars['fileUploadUser'] = $row['user_name'];
249 
250  return $vars;
251  } // ShowMetaView()
252 
257  function ShowPackageInfo($Upload, $Item, $ShowMenu=0)
258  {
259  $vars = [];
260  $Require = "";
261  $MIMETYPE = "";
262  $Count = 0;
263 
264  $rpm_info = array("Package"=>"pkg_name",
265  "Alias"=>"pkg_alias",
266  "Architecture"=>"pkg_arch",
267  "Version"=>"version",
268  "License"=>"license",
269  "Group"=>"pkg_group",
270  "Packager"=>"packager",
271  "Release"=>"release",
272  "BuildDate"=>"build_date",
273  "Vendor"=>"vendor",
274  "URL"=>"url",
275  "Summary"=>"summary",
276  "Description"=>"description",
277  "Source"=>"source_rpm");
278 
279  $deb_binary_info = array("Package"=>"pkg_name",
280  "Architecture"=>"pkg_arch",
281  "Version"=>"version",
282  "Section"=>"section",
283  "Priority"=>"priority",
284  "Installed Size"=>"installed_size",
285  "Maintainer"=>"maintainer",
286  "Homepage"=>"homepage",
287  "Source"=>"source",
288  "Summary"=>"summary",
289  "Description"=>"description");
290 
291  $deb_source_info = array("Format"=>"format",
292  "Source"=>"source",
293  "Binary"=>"pkg_name",
294  "Architecture"=>"pkg_arch",
295  "Version"=>"version",
296  "Maintainer"=>"maintainer",
297  "Uploaders"=>"uploaders",
298  "Standards-Version"=>"standards_version");
299 
300  if (empty($Item) || empty($Upload)) {
301  return $vars;
302  }
303 
304  /**********************************
305  Check if pkgagent disabled
306  ***********************************/
307  $sql = "SELECT agent_enabled FROM agent WHERE agent_name ='pkgagent' order by agent_ts LIMIT 1;";
308  $row = $this->dbManager->getSingleRow($sql,array(),__METHOD__."checkPkgagentDisabled");
309  if (isset($row) && ($row['agent_enabled'] == 'f')) {
310  return $vars;
311  }
312 
313  /* If pkgagent_ars table didn't exists, don't show the result. */
314  $sql = "SELECT typlen FROM pg_type where typname='pkgagent_ars' limit 1;";
315  $this->dbManager->prepare(__METHOD__."displayPackageInfo",$sql);
316  $result = $this->dbManager->execute(__METHOD__."displayPackageInfo",array());
317  $numrows = pg_num_rows($result);
318  $this->dbManager->freeResult($result);
319  if ($numrows <= 0) {
320  $vars['packageAgentNA'] = 1;
321  return $vars;
322  }
323 
324  /* If pkgagent_ars table didn't have record for this upload, don't show the result. */
325  $agent_status = AgentARSList('pkgagent_ars', $Upload);
326  if (empty($agent_status)) {
327  $vars['packageAgentStatus'] = 1;
328  $vars['trackback_uri'] = Traceback_uri() .
329  "?mod=schedule_agent&upload=$Upload&agent=agent_pkgagent";
330  return ($vars);
331  }
332  $sql = "SELECT mimetype_name
333  FROM uploadtree
334  INNER JOIN pfile ON uploadtree_pk = $1
335  AND pfile_fk = pfile_pk
336  INNER JOIN mimetype ON pfile_mimetypefk = mimetype_pk;";
337  $this->dbManager->prepare(__METHOD__."getMimetypeName",$sql);
338  $result = $this->dbManager->execute(__METHOD__."getMimetypeName",array($Item));
339  while ($row = pg_fetch_assoc($result)) {
340  if (! empty($row['mimetype_name'])) {
341  $MIMETYPE = $row['mimetype_name'];
342  }
343  }
344  $this->dbManager->freeResult($result);
345 
347  if ($MIMETYPE == "application/x-rpm") {
348  $sql = "SELECT *
349  FROM pkg_rpm
350  INNER JOIN uploadtree ON uploadtree_pk = $1
351  AND uploadtree.pfile_fk = pkg_rpm.pfile_fk;";
352  $R = $this->dbManager->getSingleRow($sql,array($Item),__METHOD__."getRPMPackageInfo");
353  if ((! empty($R['source_rpm'])) and (trim($R['source_rpm']) != "(none)")) {
354  $vars['packageType'] = _("RPM Binary Package");
355  } else {
356  $vars['packageType'] = _("RPM Source Package");
357  }
358  $Count=1;
359 
360  if (! empty($R['pkg_pk'])) {
361  $Require = $R['pkg_pk'];
362  foreach ($rpm_info as $key => $value) {
363  $entry = [];
364  $entry['count'] = $Count;
365  $entry['type'] = _($key);
366  $entry['value'] = htmlentities($R["$value"]);
367  $Count ++;
368  $vars['packageEntries'][] = $entry;
369  }
370 
371  $sql = "SELECT * FROM pkg_rpm_req WHERE pkg_fk = $1;";
372  $this->dbManager->prepare(__METHOD__."getPkg_rpm_req",$sql);
373  $result = $this->dbManager->execute(__METHOD__."getPkg_rpm_req",array($Require));
374 
375  while ($R = pg_fetch_assoc($result) and ! empty($R['req_pk'])) {
376  $entry = [];
377  $entry['count'] = $Count;
378  $entry['type'] = _("Requires");
379  $entry['value'] = htmlentities($R['req_value']);
380  $Count ++;
381  $vars['packageRequires'][] = $entry;
382  }
383  $this->dbManager->freeResult($result);
384  }
385  } else if ($MIMETYPE == "application/x-debian-package") {
386  $vars['packageType'] = _("Debian Binary Package\n");
387 
388  $sql = "SELECT *
389  FROM pkg_deb
390  INNER JOIN uploadtree ON uploadtree_pk = $1
391  AND uploadtree.pfile_fk = pkg_deb.pfile_fk;";
392  $R = $this->dbManager->getSingleRow($sql,array($Item),__METHOD__."debianBinaryPackageInfo");
393  $Count=1;
394 
395  if ($R) {
396  $Require = $R['pkg_pk'];
397  foreach ($deb_binary_info as $key => $value) {
398  $entry = [];
399  $entry['count'] = $Count;
400  $entry['type'] = _($key);
401  $entry['value'] = htmlentities($R["$value"]);
402  $Count ++;
403  $vars['packageEntries'][] = $entry;
404  }
405  pg_free_result($result);
406 
407  $sql = "SELECT * FROM pkg_deb_req WHERE pkg_fk = $1;";
408  $this->dbManager->prepare(__METHOD__."getPkg_rpm_req",$sql);
409  $result = $this->dbManager->execute(__METHOD__."getPkg_rpm_req",array($Require));
410 
411  while ($R = pg_fetch_assoc($result) and ! empty($R['req_pk'])) {
412  $entry = [];
413  $entry['count'] = $Count;
414  $entry['type'] = _("Depends");
415  $entry['value'] = htmlentities($R['req_value']);
416  $Count ++;
417  $vars['packageRequires'][] = $entry;
418  }
419  $this->dbManager->freeResult($result);
420  }
421  $V .= "</table>\n";
422  } else if ($MIMETYPE == "application/x-debian-source") {
423  $vars['packageType'] = _("Debian Source Package\n");
424 
425  $sql = "SELECT *
426  FROM pkg_deb
427  INNER JOIN uploadtree ON uploadtree_pk = $1
428  AND uploadtree.pfile_fk = pkg_deb.pfile_fk;";
429  $R = $this->dbManager->getSingleRow($sql,array($Item),__METHOD__."debianSourcePakcageInfo");
430  $Count=1;
431 
432  if ($R) {
433  $Require = $R['pkg_pk'];
434  foreach ($deb_source_info as $key => $value) {
435  $entry = [];
436  $entry['count'] = $Count;
437  $entry['type'] = _($key);
438  $entry['value'] = htmlentities($R["$value"]);
439  $Count ++;
440  $vars['packageEntries'][] = $entry;
441  }
442  pg_free_result($result);
443 
444  $sql = "SELECT * FROM pkg_deb_req WHERE pkg_fk = $1;";
445  $this->dbManager->prepare(__METHOD__."getPkg_rpm_req",$sql);
446  $result = $this->dbManager->execute(__METHOD__."getPkg_rpm_req",array($Require));
447 
448  while ($R = pg_fetch_assoc($result) and ! empty($R['req_pk'])) {
449  $entry = [];
450  $entry['count'] = $Count;
451  $entry['type'] = _("Build-Depends");
452  $entry['value'] = htmlentities($R['req_value']);
453  $Count ++;
454  $vars['packageRequires'][] = $entry;
455  }
456  $this->dbManager->freeResult($result);
457  }
458  } else {
459  /* Not a package */
460  $vars['packageType'] = "";
461  }
462  return $vars;
463  } // ShowPackageInfo()
464 
465 
469  function ShowTagInfo($Upload, $Item)
470  {
471  $vars = [];
472  $groupId = Auth::getGroupId();
473  $row = $this->uploadDao->getUploadEntry($Item);
474  if (empty($row)) {
475  $vars['tagInvalid'] = 1;
476  return $vars;
477  }
478  $lft = $row["lft"];
479  $rgt = $row["rgt"];
480  $upload_pk = $row["upload_fk"];
481 
482  if (empty($lft)) {
483  $vars['tagInvalid'] = 2;
484  return $vars;
485  }
486  $sql = "SELECT * FROM uploadtree INNER JOIN (SELECT * FROM tag_file,tag WHERE tag_pk = tag_fk) T
487  ON uploadtree.pfile_fk = T.pfile_fk WHERE uploadtree.upload_fk = $1
488  AND uploadtree.lft >= $2 AND uploadtree.rgt <= $3 UNION SELECT * FROM uploadtree INNER JOIN
489  (SELECT * FROM tag_uploadtree,tag WHERE tag_pk = tag_fk) T ON uploadtree.uploadtree_pk = T.uploadtree_fk
490  WHERE uploadtree.upload_fk = $1 AND uploadtree.lft >= $2 AND uploadtree.rgt <= $3 ORDER BY ufile_name";
491  $this->dbManager->prepare(__METHOD__,$sql);
492  $result = $this->dbManager->execute(__METHOD__,array($upload_pk, $lft,$rgt));
493  if (pg_num_rows($result) > 0) {
494  while ($row = pg_fetch_assoc($result)) {
495  $entry = [];
496  $entry['ufile_name'] = $row['ufile_name'];
497  $entry['tag'] = $row['tag'];
498  if ($this->uploadDao->isAccessible($upload_pk, $groupId)) {
499  $entry['url'] = Traceback_uri() .
500  "?mod=tag&action=edit&upload=$Upload&item=" . $row['uploadtree_pk'] .
501  "&tag_file_pk=" . $row['tag_file_pk'];
502  } else {
503  $entry['url'] = "";
504  }
505  $vars['tagsEntries'][] = $entry;
506  }
507  }
508  $this->dbManager->freeResult($result);
509 
510  return $vars;
511  }
512 
518  function showReuseInfo($uploadId)
519  {
520  $vars = [];
521  $reusedInfo = $this->uploadDao->getReusedUpload($uploadId,
522  Auth::getGroupId());
523  foreach ($reusedInfo as $row) {
524  $entry = [];
525  $reuseUploadFk = $row['reused_upload_fk'];
526  $reuseGroupFk = $row['reused_group_fk'];
527  $reusedUpload = $this->uploadDao->getUpload($reuseUploadFk);
528  $reuseMode = array();
529  if ($row['reuse_mode'] & UploadDao::REUSE_ENHANCED) {
530  $reuseMode[] = "Enhanced";
531  } else {
532  $reuseMode[] = "Normal";
533  }
534  if ($row['reuse_mode'] & UploadDao::REUSE_MAIN) {
535  $reuseMode[] = "Main license";
536  }
537  if ($row['reuse_mode'] & UploadDao::REUSE_CONF) {
538  $reuseMode[] = "Report configuration settings";
539  }
540  $entry['name'] = $reusedUpload->getFilename();
541  $entry['url'] = Traceback_uri() .
542  "?mod=license&upload=$reuseUploadFk&item=" .
543  $this->uploadDao->getUploadParent($reuseUploadFk);
544  $entry['group'] = $this->userDao->getGroupNameById($reuseGroupFk) .
545  " ($reuseGroupFk)";
546  $entry['sha1'] = $this->uploadDao->getUploadHashes($reuseUploadFk)['sha1'];
547  $entry['mode'] = implode(", ", $reuseMode)." reuse";
548 
549  $vars['reusedPackageList'][] = $entry;
550  }
551  return $vars;
552  }
553 
554  public function Output()
555  {
556  $uploadId = GetParm("upload", PARM_INTEGER);
557  if (!$this->uploadDao->isAccessible($uploadId, Auth::getGroupId())) {
558  return;
559  }
560 
561  $itemId = GetParm("item",PARM_INTEGER);
562  $this->vars['micromenu'] = Dir2Browse("browse", $itemId, NULL, $showBox=0, "View-Meta");
563 
564  $this->vars += $this->ShowTagInfo($uploadId, $itemId);
565  $this->vars += $this->ShowPackageinfo($uploadId, $itemId, 1);
566  $this->vars += $this->ShowMetaView($uploadId, $itemId);
567  $this->vars += $this->ShowSightings($uploadId, $itemId);
568  $this->vars += $this->ShowView($uploadId, $itemId);
569  $this->vars += $this->showReuseInfo($uploadId);
570  }
571 
572  public function getTemplateName()
573  {
574  return "ui-view-info.html.twig";
575  }
576 }
577 
578 $NewPlugin = new ui_view_info();
579 $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
Traceback_uri()
Get the URI without query to this location.
Dir2FileList(&$Listing, $IfDirPlugin, $IfFilePlugin, $Count=-1, $ShowPhrase=0)
Given an array of pfiles/uploadtree, sorted by pfile, list all of the breadcrumbs for each file...
Definition: common-dir.php:417
RegisterMenus()
Customize submenus.
showReuseInfo($uploadId)
Get the info regarding reused package.
#define PLUGIN_DB_READ
Plugin requires read permission on DB.
Definition: libfossology.h:49
MenuEndlessPage($Page, $Next=1, $Uri= '')
Create a "First Prev 1 2 ... Next" page links for paged output.
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:57
ShowSightings($Upload, $Item)
Show Sightings, List the directory locations where this pfile is found.
const PARM_STRING
Definition: common-parm.php:29
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...
HumanSize($bytes)
Translate a byte number to a proper type, xxx bytes to xxx B/KB/MB/GB/TB/PB.
Definition: common-ui.php:109
Traceback_parm_keep($List)
Create a new URI, keeping only these items.
ShowView($Upload, $Item, $ShowMenu=0)
Display the info data associated with the file.
ShowMetaView($Upload, $Item)
Display the meta data associated with the file.
ShowTagInfo($Upload, $Item)
Display the tag info data associated with the file.
ShowPackageInfo($Upload, $Item, $ShowMenu=0)
Display the package info associated with the rpm/debian package.
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:695