FOSSology  4.4.0
Open Source License Compliance by Open Source Software
ui-list-bucket-files.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2010-2015 Hewlett-Packard Development Company, L.P.
4  SPDX-FileCopyrightText: © 2015 Siemens AG
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
11 
36 {
37  function __construct()
38  {
39  $this->Name = "list_bucket_files";
40  $this->Title = _("List Files for Bucket");
41  $this->Dependency = array("nomoslicense");
42  $this->DBaccess = PLUGIN_DB_READ;
43  $this->LoginFlag = 0;
44  parent::__construct();
45  }
46 
51  function RegisterMenus()
52  {
53  // micro-menu
54  $bucketagent_pk = GetParm("bapk",PARM_INTEGER);
55  $uploadtree_pk = GetParm("item",PARM_INTEGER);
56  $bucket_pk = GetParm("bpk",PARM_INTEGER);
57  $bucketpool_pk = GetParm("bp",PARM_INTEGER);
58  $nomosagent_pk = GetParm("napk",PARM_INTEGER);
59 
60  $URL = $this->Name . "&bapk=$bucketagent_pk&item=$uploadtree_pk&bpk=$bucket_pk&bp=$bucketpool_pk&napk=$nomosagent_pk&page=-1";
61  $text = _("Show All Files");
62  menu_insert($this->Name."::Show All",0, $URL, $text);
63  } // RegisterMenus()
64 
77  function Initialize()
78  {
79  $this->State=PLUGIN_STATE_READY;
80  return true;
81  } // Initialize()
82 
83 
88  function Output()
89  {
90  if ($this->State != PLUGIN_STATE_READY) {
91  return;
92  }
93  global $PG_CONN;
94 
95  /* Input parameters */
96  $bucketagent_pk = GetParm("bapk",PARM_INTEGER);
97  $uploadtree_pk = GetParm("item",PARM_INTEGER);
98  $bucket_pk = GetParm("bpk",PARM_INTEGER);
99  $bucketpool_pk = GetParm("bp",PARM_INTEGER);
100  $nomosagent_pk = GetParm("napk",PARM_INTEGER);
101  $BinNoSrc = GetParm("bns",PARM_INTEGER); // 1 if requesting binary with no src
102  $Excl = GetParm("excl",PARM_RAW);
103 
104  if (empty($uploadtree_pk) || empty($bucket_pk) || empty($bucketpool_pk))
105  {
106  $text = _("is missing required parameters.");
107  echo $this->Name . " $text";
108  return;
109  }
110 
111  /* Check upload permission */
112  $Row = GetSingleRec("uploadtree", "WHERE uploadtree_pk = $uploadtree_pk");
114  $uploadDao = $GLOBALS['container']->get('dao.upload');
115  if ( !$uploadDao->isAccessible($Row['upload_fk'], Auth::getGroupId()) )
116  {
117  $text = _("Permission Denied");
118  echo "<h2>$text item 1</h2>";
119  return;
120  }
121 
122  $Page = GetParm("page",PARM_INTEGER);
123  if (empty($Page)) {
124  $Page=0;
125  }
126 
127  $V="";
128  $Time = time();
129  $Max = 200;
130 
131  // Create cache of bucket_pk => bucket_name
132  // Since we are going to do a lot of lookups
133  $sql = "select bucket_pk, bucket_name from bucket_def where bucketpool_fk=$bucketpool_pk";
134  $result_name = pg_query($PG_CONN, $sql);
135  DBCheckResult($result_name, $sql, __FILE__, __LINE__);
136  $bucketNameCache = array();
137  while ($name_row = pg_fetch_assoc($result_name)) {
138  $bucketNameCache[$name_row['bucket_pk']] = $name_row['bucket_name'];
139  }
140  pg_free_result($result_name);
141 
142  switch($this->OutputType)
143  {
144  case "XML":
145  break;
146  case "HTML":
147  // micro menus
148  $V .= menu_to_1html(menu_find($this->Name, $MenuDepth),0);
149 
150  /* Get all the files under this uploadtree_pk with this bucket */
151  $V .= _("The following files are in bucket: '<b>");
152  $V .= $bucketNameCache[$bucket_pk];
153  $V .= "</b>'.\n";
154  $text = _("Display");
155  $text1 = _("excludes");
156  $text2 = _("files with these licenses");
157  if (!empty($Excl)) $V .= "<br>$text <b>$text1</b> $text2: $Excl";
158 
159  $Offset = ($Page <= 0) ? 0 : $Page*$Max;
160  $PkgsOnly = false;
161 
162  // Get bounds of subtree (lft, rgt) for this uploadtree_pk
163  $sql = "SELECT lft,rgt,upload_fk FROM uploadtree
164  WHERE uploadtree_pk = $uploadtree_pk";
165  $result = pg_query($PG_CONN, $sql);
166  DBCheckResult($result, $sql, __FILE__, __LINE__);
167  $row = pg_fetch_assoc($result);
168  $lft = $row["lft"];
169  $rgt = $row["rgt"];
170  $upload_pk = $row["upload_fk"];
171  pg_free_result($result);
172 
173  /* Get uploadtree table */
174  $uploadtree_tablename = GetUploadtreeTableName($upload_pk);
175 
176  /* If $BinNoSrc, then only list binary packages in this subtree
177  * that do not have Source packages.
178  * Else list files in the asked for bucket.
179  */
180  if ($BinNoSrc)
181  {
182  }
183  else
184  {
185  $Offset= ($Page < 0) ? 0 : $Page*$Max;
186  $limit = ($Page < 0) ? "ALL":$Max;
187  // Get all the uploadtree_pk's with this bucket (for this agent and bucketpool)
188  // in this subtree.
189  // It would be best to sort by pfile_pk, so that the duplicate pfiles are
190  // correctly indented, but pfile_pk has no meaning to the user. So a compromise,
191  // sorting by ufile_name is used.
192  $sql = "select uploadtree.*, bucket_file.nomosagent_fk as nomosagent_fk
193  from uploadtree, bucket_file, bucket_def
194  where upload_fk=$upload_pk and uploadtree.lft between $lft and $rgt
195  and ((ufile_mode & (3<<28)) = 0)
196  and uploadtree.pfile_fk=bucket_file.pfile_fk
197  and agent_fk=$bucketagent_pk
198  and bucket_fk=$bucket_pk
199  and bucketpool_fk=$bucketpool_pk
200  and bucket_pk=bucket_fk
201  and nomosagent_fk=$nomosagent_pk
202  order by uploadtree.ufile_name
203  limit $limit offset $Offset";
204  $fileresult = pg_query($PG_CONN, $sql);
205  DBCheckResult($fileresult, $sql, __FILE__, __LINE__);
206  $Count = pg_num_rows($fileresult);
207  }
208  $file_result_temp = pg_fetch_all($fileresult);
209  $sourted_file_result = array(); // the final file list will display
210  $max_num = $Count;
212  for($i = 0; $i < $max_num; $i++)
213  {
214  $row = $file_result_temp[$i];
215  if (empty($row)) continue;
216  $sourted_file_result[] = $row;
217  for($j = $i + 1; $j < $max_num; $j++)
218  {
219  $row_next = $file_result_temp[$j];
220  if (!empty($row_next) && ($row['pfile_fk'] == $row_next['pfile_fk']))
221  {
222  $sourted_file_result[] = $row_next;
223  $file_result_temp[$j] = null;
224  }
225  }
226  }
227 
228  if ($Count < (1.25 * $Max)) $Max = $Count;
229  if ($Max < 1) $Max = 1; // prevent div by zero in corner case of no files
230 
231  /* Get the page menu */
232  if (($Count >= $Max) && ($Page >= 0))
233  {
234  $VM = "<P />\n" . MenuEndlessPage($Page,intval((($Count+$Offset)/$Max))) . "<P />\n";
235  $V .= $VM;
236  }
237  else
238  {
239  $VM = "";
240  }
241 
242  // base url
243  $baseURL = "?mod=" . $this->Name . "&bapk=$bucketagent_pk&item=$uploadtree_pk&bpk=$bucket_pk&bp=$bucketpool_pk&napk=$nomosagent_pk&page=-1";
244 
245  // for each uploadtree rec ($fileresult), find all the licenses in it and it's children
246  $ShowBox = 1;
247  $ShowMicro=NULL;
248  $RowNum = $Offset;
249  $Header = "";
250  $LinkLast = "list_bucket_files&bapk=$bucketagent_pk";
251 
252  /* file display loop/table */
253  $V .= "<table>";
254  $text = _("File");
255  $V .= "<tr><th>$text</th><th>&nbsp";
256  $ExclArray = explode(":", $Excl);
257  $ItemNumb = 0;
258  $PrevPfile_pk = 0;
259 
260  if ($Count > 0)
261  foreach ($sourted_file_result as $row)
262  {
263  // get all the licenses in this subtree (bucket uploadtree_pk)
264  $pfile_pk = $row['pfile_fk'];
265  $licstring = GetFileLicenses_string($nomosagent_pk, $row['pfile_fk'], $row['uploadtree_pk'], $uploadtree_tablename);
266  if (empty($licstring)) $licstring = '-';
267  $URLlicstring = urlencode($licstring);
268 
269  /* Allow user to exclude files with this exact license list */
270  if (!empty($Excl))
271  $URL = $baseURL ."&excl=".urlencode($Excl).":".$URLlicstring;
272  else
273  $URL = $baseURL ."&excl=$URLlicstring";
274  $text = _("Exclude files with license");
275  $Header = "<a href=$URL>$text: $licstring.</a>";
276 
277  $ok = !($Excl && in_array($licstring, $ExclArray));
278 
279  if ($ok)
280  {
281  $nomosagent_pk = $row['nomosagent_fk'];
282  $LinkLast = "view-license&bapk=$bucketagent_pk&napk=$nomosagent_pk";
283  $V .= "<tr><td>";
284  if ($PrevPfile_pk == $pfile_pk)
285  {
286  $V .= "<div style='margin-left:2em;'>";
287  }
288  else
289  {
290  $V .= "<div>";
291  }
292  $V .= Dir2Browse("browse", $row['uploadtree_pk'], $LinkLast, $ShowBox, $ShowMicro, ++$RowNum, $Header, '', $uploadtree_tablename);
293  $V .= "</div>";
294 
295  $V .= "</td>";
296  $V .= "<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>"; // spaces to seperate licenses
297 
298  // show the entire license list as a single string with links to the files
299  // in this container with that license.
300  $V .= "<td>$licstring</td></tr>";
301  $V .= "<tr><td colspan=3><hr></td></tr>"; // separate files
302  }
303  $PrevPfile_pk = $pfile_pk;
304  }
305  pg_free_result($fileresult);
306  $V .= "</table>";
307  if (!empty($VM)) {
308  $V .= $VM . "\n";
309  }
310  $V .= "<hr>\n";
311  $Time = time() - $Time;
312  $text = _("Elapsed time");
313  $text1 = _("seconds");
314  $V .= "<small>$text: $Time $text1</small>\n";
315  break;
316  case "Text":
317  break;
318  default:
319  break;
320  }
321  if (!$this->OutputToStdout) {
322  return($V);
323  }
324  print($V);
325  return;
326  } // Output()
327 
328 }
329 
330 $NewPlugin = new list_bucket_files;
331 $NewPlugin->Initialize();
This is the Plugin class. All plugins should:
Definition: FO_Plugin.php:57
Output()
This function is called when user output is requested. This function is responsible for content....
Definition: FO_Plugin.php:399
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
Definition: state.hpp:16
__construct()
base constructor. Most plugins will just use this
RegisterMenus()
Customize submenus.
Initialize()
This is called before the plugin is used.
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:187
GetSingleRec($Table, $Where="")
Retrieve a single database record.
Definition: common-db.php:91
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
GetFileLicenses_string($agent_pk, $pfile_pk, $uploadtree_pk, $uploadtree_tablename='uploadtree')
Same as GetFileLicenses() but returns license list as a single string.
MenuEndlessPage($Page, $Next=1, $Uri='')
Create a "First Prev 1 2 ... Next" page links for paged output.
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.
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
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:46
FUNCTION char * GetUploadtreeTableName(PGconn *pgConn, int upload_pk)
Get the uploadtree table name for this upload_pk If upload_pk does not exist, return "uploadtree".
Definition: libfossagent.c:414
#define PLUGIN_DB_READ
Plugin requires read permission on DB.
Definition: libfossology.h:37
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN