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