FOSSology  4.4.0
Open Source License Compliance by Open Source Software
ui-browse.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2010-2013 Hewlett-Packard Development Company, L.P.
4  SPDX-FileCopyrightText: © 2014-2015 Siemens AG
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
16 use Symfony\Component\HttpFoundation\RedirectResponse;
17 use Symfony\Component\HttpFoundation\Response;
18 
19 define("TITLE_UI_BROWSE", _("Browse"));
20 
21 class ui_browse extends FO_Plugin
22 {
24  private $uploadDao;
26  private $folderDao;
27 
28  function __construct()
29  {
30  $this->Name = "browse";
31  $this->Title = TITLE_UI_BROWSE;
32  $this->MenuList = "Browse";
33  $this->MenuOrder = 80; // just to right of Home(100)
34  $this->MenuTarget = "";
35  $this->DBaccess = PLUGIN_DB_READ;
36  $this->LoginFlag = 0;
37 
38  global $container;
39  $this->uploadDao = $container->get('dao.upload');
40  $this->folderDao = $container->get('dao.folder');
41 
42  parent::__construct();
43  }
44 
48  function RegisterMenus()
49  {
50  menu_insert("Main::" . $this->MenuList, $this->MenuOrder, $this->Name, $this->Name);
51 
52  $Upload = GetParm("upload", PARM_INTEGER);
53  if (empty($Upload)) {
54  return;
55  }
56  // For the Browse menu, permit switching between detail and simple.
57  $URI = $this->Name . Traceback_parm_keep(array("upload","item"));
58  if (GetParm("mod", PARM_STRING) == $this->Name) {
59  menu_insert("Browse::Browse", 1);
60  } else {
61  menu_insert("Browse::Browse", 1, $URI);
62  }
63  return ($this->State == PLUGIN_STATE_READY);
64  }
65 
70  function ShowItem($Upload, $Item, $Show, $Folder, $uploadtree_tablename)
71  {
72  global $container;
74  $dbManager = $container->get('db.manager');
75  $RowStyle1 = "style='background-color:#ecfaff'"; // pale blue
76  $RowStyle2 = "style='background-color:#ffffe3'"; // pale yellow
77  $ColorSpanRows = 3; // Alternate background color every $ColorSpanRows
78  $RowNum = 0;
79 
80  $V = "";
81  /* Use plugin "view" and "download" if they exist. */
82  $Uri = Traceback_uri() . "?mod=" . $this->Name . "&folder=$Folder";
83 
84  /* there are three types of Browse-Pfile menus */
85  /* menu as defined by their plugins */
86  $MenuPfile = menu_find("Browse-Pfile", $MenuDepth);
87  /* menu but without Compare */
88  $MenuPfileNoCompare = menu_remove($MenuPfile, "Compare");
89  /* menu with only Tag and Compare */
90  $MenuTag = array();
91  foreach ($MenuPfile as $value) {
92  if (($value->Name == 'Tag') || ($value->Name == 'Compare')) {
93  $MenuTag[] = $value;
94  }
95  }
96 
97  $Results = GetNonArtifactChildren($Item, $uploadtree_tablename);
98  $ShowSomething = 0;
99  $V .= "<table class='text' style='border-collapse: collapse' border=0 padding=0>\n";
100  $stmtGetFirstChild = __METHOD__.'.getFirstChild';
101  $dbManager->prepare($stmtGetFirstChild,"SELECT uploadtree_pk FROM $uploadtree_tablename WHERE parent=$1 limit 1");
102  foreach ($Results as $Row) {
103  if (empty($Row['uploadtree_pk'])) {
104  continue;
105  }
106  $ShowSomething = 1;
107  $Name = $Row['ufile_name'];
108 
109  /* Set alternating row background color - repeats every $ColorSpanRows rows */
110  $RowStyle = (($RowNum++ % (2 * $ColorSpanRows)) < $ColorSpanRows) ? $RowStyle1 : $RowStyle2;
111  $V .= "<tr $RowStyle>";
112 
113  /* Check for children so we know if the file should by hyperlinked */
114  $result = $dbManager->execute($stmtGetFirstChild,array($Row['uploadtree_pk']));
115  $HasChildren = $dbManager->fetchArray($result);
116  $dbManager->freeResult($result);
117 
118  $Parm = "upload=$Upload&show=$Show&item=" . $Row['uploadtree_pk'];
119  $Link = $HasChildren ? "$Uri&show=$Show&upload=$Upload&item=$Row[uploadtree_pk]" : NULL;
120 
121  if ($Show == 'detail') {
122  $V .= "<td class='mono'>" . DirMode2String($Row['ufile_mode']) . "</td>";
123  if (!Isdir($Row['ufile_mode'])) {
124  $V .= "<td align='right'>&nbsp;&nbsp;" . number_format($Row['pfile_size'], 0, "", ",") . "&nbsp;&nbsp;</td>";
125  } else {
126  $V .= "<td>&nbsp;</td>";
127  }
128  }
129 
130  $displayItem = Isdir($Row['ufile_mode']) ? "$Name/" : $Name;
131  if (!empty($Link)) {
132  $displayItem = "<a href=\"$Link\">$displayItem</a>";
133  }
134  if (Iscontainer($Row['ufile_mode'])) {
135  $displayItem = "<b>$displayItem</b>";
136  }
137  $V .= "<td>$displayItem</td>\n";
138 
139  if (!Iscontainer($Row['ufile_mode'])) {
140  $V .= menu_to_1list($MenuPfileNoCompare, $Parm, "<td>", "</td>\n", 1, $Upload);
141  } else if (!Isdir($Row['ufile_mode'])) {
142  $V .= menu_to_1list($MenuPfile, $Parm, "<td>", "</td>\n", 1, $Upload);
143  } else {
144  $V .= menu_to_1list($MenuTag, $Parm, "<td>", "</td>\n", 1, $Upload);
145  }
146  } /* foreach($Results as $Row) */
147  $V .= "</table>\n";
148  if (! $ShowSomething) {
149  $text = _("No files");
150  $V .= "<b>$text</b>\n";
151  } else {
152  $V .= "<hr>\n";
153  if (count($Results) == 1) {
154  $text = _("1 item");
155  $V .= "$text\n";
156  } else {
157  $text = _("items");
158  $V .= count($Results) . " $text\n";
159  }
160  }
161  return ($V);
162  }
163 
168  private function ShowFolder($folderId)
169  {
170  $rootFolder = $this->folderDao->getDefaultFolder(Auth::getUserId());
171  if ($rootFolder == NULL) {
172  $rootFolder = $this->folderDao->getRootFolder(Auth::getUserId());
173  }
174  /* @var $uiFolderNav FolderNav */
175  $uiFolderNav = $GLOBALS['container']->get('ui.folder.nav');
176 
177  $folderNav = '<div id="sidetree" class="container justify-content-center" style="min-width: 234px;">';
178  if ($folderId != $rootFolder->getId()) {
179  $folderNav .= '<div class="treeheader" style="display:inline;"><a class="btn btn-outline-success btn-sm" href="' .
180  Traceback_uri() . '?mod=' . $this->Name . '">Top folder</a> | </div>';
181  }
182  $folderNav .= '<div id="sidetreecontrol" class="treeheader" style="display:inline;">
183  <a class="btn btn-outline-success btn-sm" href="?#">Collapse All</a> |
184  <a class="btn btn-outline-success btn-sm" href="?#">Expand All</a>
185  </div><br/><br/>';
186  $folderNav .= '
187  <div class="col-sm-20" style="margin-top:-10px;">
188  <input id="searchFolderTree" type="text" class="form-control" name="searchFolderTree" placeholder="Search folder" autofocus="autofocus"">
189  </div>';
190  $folderNav .= $uiFolderNav->showFolderTree($folderId).'</div>';
191 
192  $this->vars['folderNav'] = $folderNav;
193 
194  $assigneeArray = $this->getAssigneeArray();
195  $this->vars['assigneeOptions'] = $assigneeArray;
196  $this->vars['statusOptions'] = $this->uploadDao->getStatusTypeMap();
197  $this->vars['folder'] = $folderId;
198  $this->vars['folderName'] = $this->folderDao->getFolder($folderId)->getName();
199  }
200 
204  function Output()
205  {
206  if ($this->State != PLUGIN_STATE_READY) {
207  return 0;
208  }
209  $this->folderDao->ensureTopLevelFolder();
210 
211  $folder_pk = GetParm("folder", PARM_INTEGER);
212  $Upload = GetParm("upload", PARM_INTEGER); // upload_pk to browse
213  $Item = GetParm("item", PARM_INTEGER); // uploadtree_pk to browse
214 
215  /* check if $folder_pk is accessible to logged in user */
216  if (!empty($folder_pk) && !$this->folderDao->isFolderAccessible($folder_pk)) {
217  $this->vars['message'] = _("Permission Denied");
218  return $this->render('include/base.html.twig');
219  }
220 
221  /* check permission if $Upload is given */
222  if (!empty($Upload) && !$this->uploadDao->isAccessible($Upload, Auth::getGroupId())) {
223  $this->vars['message'] = _("Permission Denied");
224  return $this->render('include/base.html.twig');
225  }
226 
227  if (empty($folder_pk)) {
228  try {
229  $folder_pk = $this->getFolderId($Upload);
230  } catch (Exception $exc) {
231  return $exc->getMessage();
232  }
233  }
234 
235  $output = $this->outputItemHtml($Item, $folder_pk, $Upload);
236  if ($output instanceof Response) {
237  return $output;
238  }
239 
240  $this->vars['content'] = $output;
241  $modsUploadMulti = MenuHook::getAgentPluginNames('UploadMulti');
242  if (!empty($modsUploadMulti)) {
243  $multiUploadAgents = array();
244  foreach ($modsUploadMulti as $mod) {
245  $multiUploadAgents[$mod] = $GLOBALS['Plugins'][$mod]->title;
246  }
247  $this->vars['multiUploadAgents'] = $multiUploadAgents;
248  }
249  $this->vars['folderId'] = $folder_pk;
250 
251  return $this->render('ui-browse.html.twig');
252  }
253 
258  private function getFolderId($uploadId)
259  {
260  $rootFolder = $this->folderDao->getDefaultFolder(Auth::getUserId());
261  if ($rootFolder == NULL) {
262  $rootFolder = $this->folderDao->getRootFolder(Auth::getUserId());
263  }
264  if (empty($uploadId)) {
265  return $rootFolder->getId();
266  }
267 
268  global $container;
269  /* @var $dbManager DbManager */
270  $dbManager = $container->get('db.manager');
271  $uploadExists = $dbManager->getSingleRow(
272  "SELECT count(*) cnt FROM upload WHERE upload_pk=$1 " .
273  "AND (expire_action IS NULL OR expire_action!='d') AND pfile_fk IS NOT NULL", array($uploadId));
274  if ($uploadExists['cnt']< 1) {
275  throw new Exception("This upload no longer exists on this system.");
276  }
277 
278  $folderTreeCte = $this->folderDao->getFolderTreeCte($rootFolder);
279 
280  $parent = $dbManager->getSingleRow(
281  $folderTreeCte .
282  " SELECT ft.folder_pk FROM foldercontents fc LEFT JOIN folder_tree ft ON fc.parent_fk=ft.folder_pk "
283  . "WHERE child_id=$2 AND foldercontents_mode=$3 ORDER BY depth LIMIT 1",
284  array($rootFolder->getId(), $uploadId, FolderDao::MODE_UPLOAD),
285  __METHOD__.'.parent');
286  if (!$parent) {
287  throw new Exception("Upload $uploadId missing from foldercontents in your foldertree.");
288  }
289  return $parent['folder_pk'];
290  }
291 
298  function outputItemHtml($uploadTreeId, $Folder, $Upload)
299  {
300  global $container;
301  $dbManager = $container->get('db.manager');
302  $show = 'quick';
303  $html = '';
304  $uploadtree_tablename = "";
305  if (! empty($uploadTreeId)) {
306  $sql = "SELECT ufile_mode, upload_fk FROM uploadtree WHERE uploadtree_pk = $1";
307  $row = $dbManager->getSingleRow($sql, array($uploadTreeId));
308  $Upload = $row['upload_fk'];
309  if (! $this->uploadDao->isAccessible($Upload, Auth::getGroupId())) {
310  $this->vars['message'] = _("Permission Denied");
311  return $this->render('include/base.html.twig');
312  }
313 
314  if (! Iscontainer($row['ufile_mode'])) {
315  $parentItemBounds = $this->uploadDao->getParentItemBounds($Upload);
316  if (! $parentItemBounds->containsFiles()) {
317  // Upload with a single file, open license view
318  return new RedirectResponse(Traceback_uri() . '?mod=view-license'
319  . Traceback_parm_keep(array("upload", "item")));
320  }
321  global $Plugins;
322  $View = &$Plugins[plugin_find_id("view")];
323  if (! empty($View)) {
324  $this->vars['content'] = $View->ShowView(NULL, "browse");
325  return $this->render('include/base.html.twig');
326  }
327  }
328  $uploadtree_tablename = $this->uploadDao->getUploadtreeTableName($row['upload_fk']);
329  $html .= Dir2Browse($this->Name, $uploadTreeId, NULL, 1, "Browse", -1, '', '', $uploadtree_tablename) . "\n";
330  } else if (!empty($Upload)) {
331  $uploadtree_tablename = $this->uploadDao->getUploadtreeTableName($Upload);
332  $html .= Dir2BrowseUpload($this->Name, $Upload, NULL, 1, "Browse",
333  $uploadtree_tablename) . "\n";
334  }
335 
336  if (empty($Upload)) {
337  $this->vars['show'] = $show;
338  $this->ShowFolder($Folder);
339  return $html;
340  }
341 
342  if (empty($uploadTreeId)) {
343  try {
344  $uploadTreeId = $this->uploadDao->getUploadParent($Upload);
345  } catch(Exception $e) {
346  $this->vars['message'] = $e->getMessage();
347  return $this->render('include/base.html.twig');
348  }
349  }
350  $html .= $this->ShowItem($Upload, $uploadTreeId, $show, $Folder, $uploadtree_tablename);
351  $this->vars['content'] = $html;
352  return $this->render('include/base.html.twig');
353  }
354 
358  private function getAssigneeArray()
359  {
360  global $container;
362  $userDao = $container->get('dao.user');
363  $assigneeArray = $userDao->getUserChoices();
364  $assigneeArray[Auth::getUserId()] = _('-- Me --');
365  $assigneeArray[1] = _('Unassigned');
366  $assigneeArray[0] = '';
367  return $assigneeArray;
368  }
369 }
370 
371 $NewPlugin = new ui_browse();
372 $NewPlugin->Install();
This is the Plugin class. All plugins should:
Definition: FO_Plugin.php:57
render($templateName, $vars=null)
Definition: FO_Plugin.php:434
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
Definition: state.hpp:16
ShowFolder($folderId)
Given a folderId, list every item in it. If it is an individual file, then list the file contents.
Definition: ui-browse.php:168
Output()
This function returns the output html.
Definition: ui-browse.php:204
getFolderId($uploadId)
kludge for plugins not supplying a folder parameter. Find what folder this upload is in.
Definition: ui-browse.php:258
RegisterMenus()
Customize submenus.
Definition: ui-browse.php:48
__construct()
base constructor. Most plugins will just use this
Definition: ui-browse.php:28
outputItemHtml($uploadTreeId, $Folder, $Upload)
Definition: ui-browse.php:298
Dir2BrowseUpload($Mod, $UploadPk, $LinkLast=NULL, $ShowBox=1, $ShowMicro=NULL, $uploadtree_tablename='uploadtree')
Get an html links string of a file browse path.
Definition: common-dir.php:374
Isdir($mode)
Definition: common-dir.php:20
DirMode2String($Mode)
Convert a file mode to string values.
Definition: common-dir.php:50
Iscontainer($mode)
Definition: common-dir.php:38
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.
menu_to_1list($Menu, &$Parm, $Pre="", $Post="", $ShowAll=1, $upload_id="")
Take a menu and render it as one HTML line with items in a "[name]" list.
menu_remove($Menu, $RmName)
Remove a menu object (based on an object name) from a menu list.
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.
Traceback_uri()
Get the URI without query to this location.
Definition: common-parm.php:97
const PARM_INTEGER
Definition: common-parm.php:14
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
Traceback_parm_keep($List)
Create a new URI, keeping only these items.
#define PLUGIN_DB_READ
Plugin requires read permission on DB.
Definition: libfossology.h:37