FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
ui-browse.php
1 <?php
2 /***********************************************************
3  * Copyright (C) 2010-2013 Hewlett-Packard Development Company, L.P.
4  * Copyright (C) 2014-2015 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 
28 
29 define("TITLE_UI_BROWSE", _("Browse"));
30 
31 class ui_browse extends FO_Plugin
32 {
34  private $uploadDao;
36  private $folderDao;
37 
38  function __construct()
39  {
40  $this->Name = "browse";
41  $this->Title = TITLE_UI_BROWSE;
42  $this->MenuList = "Browse";
43  $this->MenuOrder = 80; // just to right of Home(100)
44  $this->MenuTarget = "";
45  $this->DBaccess = PLUGIN_DB_READ;
46  $this->LoginFlag = 0;
47 
48  global $container;
49  $this->uploadDao = $container->get('dao.upload');
50  $this->folderDao = $container->get('dao.folder');
51 
52  parent::__construct();
53  }
54 
58  function RegisterMenus()
59  {
60  menu_insert("Main::" . $this->MenuList, $this->MenuOrder, $this->Name, $this->Name);
61 
62  $Upload = GetParm("upload", PARM_INTEGER);
63  if (empty($Upload)) {
64  return;
65  }
66  // For the Browse menu, permit switching between detail and simple.
67  $URI = $this->Name . Traceback_parm_keep(array("upload","item"));
68  if (GetParm("mod", PARM_STRING) == $this->Name) {
69  menu_insert("Browse::Browse", 1);
70  } else {
71  menu_insert("Browse::Browse", 1, $URI);
72  }
73  return ($this->State == PLUGIN_STATE_READY);
74  }
75 
80  function ShowItem($Upload, $Item, $Show, $Folder, $uploadtree_tablename)
81  {
82  global $container;
84  $dbManager = $container->get('db.manager');
85  $RowStyle1 = "style='background-color:#ecfaff'"; // pale blue
86  $RowStyle2 = "style='background-color:#ffffe3'"; // pale yellow
87  $ColorSpanRows = 3; // Alternate background color every $ColorSpanRows
88  $RowNum = 0;
89 
90  $V = "";
91  /* Use plugin "view" and "download" if they exist. */
92  $Uri = Traceback_uri() . "?mod=" . $this->Name . "&folder=$Folder";
93 
94  /* there are three types of Browse-Pfile menus */
95  /* menu as defined by their plugins */
96  $MenuPfile = menu_find("Browse-Pfile", $MenuDepth);
97  /* menu but without Compare */
98  $MenuPfileNoCompare = menu_remove($MenuPfile, "Compare");
99  /* menu with only Tag and Compare */
100  $MenuTag = array();
101  foreach ($MenuPfile as $value) {
102  if (($value->Name == 'Tag') || ($value->Name == 'Compare')) {
103  $MenuTag[] = $value;
104  }
105  }
106 
107  $Results = GetNonArtifactChildren($Item, $uploadtree_tablename);
108  $ShowSomething = 0;
109  $V .= "<table class='text' style='border-collapse: collapse' border=0 padding=0>\n";
110  $stmtGetFirstChild = __METHOD__.'.getFirstChild';
111  $dbManager->prepare($stmtGetFirstChild,"SELECT uploadtree_pk FROM $uploadtree_tablename WHERE parent=$1 limit 1");
112  foreach ($Results as $Row) {
113  if (empty($Row['uploadtree_pk'])) {
114  continue;
115  }
116  $ShowSomething = 1;
117  $Name = $Row['ufile_name'];
118 
119  /* Set alternating row background color - repeats every $ColorSpanRows rows */
120  $RowStyle = (($RowNum++ % (2 * $ColorSpanRows)) < $ColorSpanRows) ? $RowStyle1 : $RowStyle2;
121  $V .= "<tr $RowStyle>";
122 
123  /* Check for children so we know if the file should by hyperlinked */
124  $result = $dbManager->execute($stmtGetFirstChild,array($Row['uploadtree_pk']));
125  $HasChildren = $dbManager->fetchArray($result);
126  $dbManager->freeResult($result);
127 
128  $Parm = "upload=$Upload&show=$Show&item=" . $Row['uploadtree_pk'];
129  $Link = $HasChildren ? "$Uri&show=$Show&upload=$Upload&item=$Row[uploadtree_pk]" : NULL;
130 
131  if ($Show == 'detail') {
132  $V .= "<td class='mono'>" . DirMode2String($Row['ufile_mode']) . "</td>";
133  if (!Isdir($Row['ufile_mode'])) {
134  $V .= "<td align='right'>&nbsp;&nbsp;" . number_format($Row['pfile_size'], 0, "", ",") . "&nbsp;&nbsp;</td>";
135  } else {
136  $V .= "<td>&nbsp;</td>";
137  }
138  }
139 
140  $displayItem = Isdir($Row['ufile_mode']) ? "$Name/" : $Name;
141  if (!empty($Link)) {
142  $displayItem = "<a href=\"$Link\">$displayItem</a>";
143  }
144  if (Iscontainer($Row['ufile_mode'])) {
145  $displayItem = "<b>$displayItem</b>";
146  }
147  $V .= "<td>$displayItem</td>\n";
148 
149  if (!Iscontainer($Row['ufile_mode'])) {
150  $V .= menu_to_1list($MenuPfileNoCompare, $Parm, "<td>", "</td>\n", 1, $Upload);
151  } else if (!Isdir($Row['ufile_mode'])) {
152  $V .= menu_to_1list($MenuPfile, $Parm, "<td>", "</td>\n", 1, $Upload);
153  } else {
154  $V .= menu_to_1list($MenuTag, $Parm, "<td>", "</td>\n", 1, $Upload);
155  }
156  } /* foreach($Results as $Row) */
157  $V .= "</table>\n";
158  if (! $ShowSomething) {
159  $text = _("No files");
160  $V .= "<b>$text</b>\n";
161  } else {
162  $V .= "<hr>\n";
163  if (count($Results) == 1) {
164  $text = _("1 item");
165  $V .= "$text\n";
166  } else {
167  $text = _("items");
168  $V .= count($Results) . " $text\n";
169  }
170  }
171  return ($V);
172  }
173 
178  private function ShowFolder($folderId)
179  {
180  $rootFolder = $this->folderDao->getRootFolder(Auth::getUserId());
181  /* @var $uiFolderNav FolderNav */
182  $uiFolderNav = $GLOBALS['container']->get('ui.folder.nav');
183 
184  $folderNav = '<div id="sidetree">';
185  if ($folderId != $rootFolder->getId()) {
186  $folderNav .= '<div class="treeheader" style="display:inline;"><a href="' .
187  Traceback_uri() . '?mod=' . $this->Name . '">Top</a> | </div>';
188  }
189  $folderNav .= '<div id="sidetreecontrol" class="treeheader" style="display:inline;"><a href="?#">Collapse All</a> | <a href="?#">Expand All</a></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'] = $rootFolder->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->getRootFolder(Auth::getUserId());
261  if (empty($uploadId)) {
262  return $rootFolder->getId();
263  }
264 
265  global $container;
266  /* @var $dbManager DbManager */
267  $dbManager = $container->get('db.manager');
268  $uploadExists = $dbManager->getSingleRow(
269  "SELECT count(*) cnt FROM upload WHERE upload_pk=$1 " .
270  "AND (expire_action IS NULL OR expire_action!='d') AND pfile_fk IS NOT NULL", array($uploadId));
271  if ($uploadExists['cnt']< 1) {
272  throw new Exception("This upload no longer exists on this system.");
273  }
274 
275  $folderTreeCte = $this->folderDao->getFolderTreeCte($rootFolder);
276 
277  $parent = $dbManager->getSingleRow(
278  $folderTreeCte .
279  " SELECT ft.folder_pk FROM foldercontents fc LEFT JOIN folder_tree ft ON fc.parent_fk=ft.folder_pk "
280  . "WHERE child_id=$2 AND foldercontents_mode=$3 ORDER BY depth LIMIT 1",
281  array($rootFolder->getId(), $uploadId, FolderDao::MODE_UPLOAD),
282  __METHOD__.'.parent');
283  if (!$parent) {
284  throw new Exception("Upload $uploadId missing from foldercontents in your foldertree.");
285  }
286  return $parent['folder_pk'];
287  }
288 
295  function outputItemHtml($uploadTreeId, $Folder, $Upload)
296  {
297  global $container;
298  $dbManager = $container->get('db.manager');
299  $show = 'quick';
300  $html = '';
301  $uploadtree_tablename = "";
302  if (! empty($uploadTreeId)) {
303  $sql = "SELECT ufile_mode, upload_fk FROM uploadtree WHERE uploadtree_pk = $1";
304  $row = $dbManager->getSingleRow($sql, array($uploadTreeId));
305  $Upload = $row['upload_fk'];
306  if (! $this->uploadDao->isAccessible($Upload, Auth::getGroupId())) {
307  $this->vars['message'] = _("Permission Denied");
308  return $this->render('include/base.html.twig');
309  }
310 
311  if (! Iscontainer($row['ufile_mode'])) {
312  global $Plugins;
313  $View = &$Plugins[plugin_find_id("view")];
314  if (! empty($View)) {
315  $this->vars['content'] = $View->ShowView(NULL, "browse");
316  return $this->render('include/base.html.twig');
317  }
318  }
319  $uploadtree_tablename = $this->uploadDao->getUploadtreeTableName($row['upload_fk']);
320  $html .= Dir2Browse($this->Name, $uploadTreeId, NULL, 1, "Browse", -1, '', '', $uploadtree_tablename) . "\n";
321  } else if (!empty($Upload)) {
322  $uploadtree_tablename = $this->uploadDao->getUploadtreeTableName($Upload);
323  $html .= Dir2BrowseUpload($this->Name, $Upload, NULL, 1, "Browse",
324  $uploadtree_tablename) . "\n";
325  }
326 
327  if (empty($Upload)) {
328  $this->vars['show'] = $show;
329  $this->ShowFolder($Folder);
330  return $html;
331  }
332 
333  if (empty($uploadTreeId)) {
334  try {
335  $uploadTreeId = $this->uploadDao->getUploadParent($Upload);
336  } catch(Exception $e) {
337  $this->vars['message'] = $e->getMessage();
338  return $this->render('include/base.html.twig');
339  }
340  }
341  $html .= $this->ShowItem($Upload, $uploadTreeId, $show, $Folder, $uploadtree_tablename);
342  $this->vars['content'] = $html;
343  return $this->render('include/base.html.twig');
344  }
345 
349  private function getAssigneeArray()
350  {
351  global $container;
353  $userDao = $container->get('dao.user');
354  $assigneeArray = $userDao->getUserChoices();
355  $assigneeArray[Auth::getUserId()] = _('-- Me --');
356  $assigneeArray[1] = _('Unassigned');
357  $assigneeArray[0] = '';
358  return $assigneeArray;
359  }
360 }
361 
362 $NewPlugin = new ui_browse();
363 $NewPlugin->Install();
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.
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...
DirMode2String($Mode)
Convert a file mode to string values.
Definition: common-dir.php:61
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:178
Iscontainer($mode)
Definition: common-dir.php:49
#define PLUGIN_DB_READ
Plugin requires read permission on DB.
Definition: libfossology.h:49
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.
Definition: state.hpp:26
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:57
const PARM_STRING
Definition: common-parm.php:29
const PARM_INTEGER
Definition: common-parm.php:25
Isdir($mode)
Definition: common-dir.php:31
This is the Plugin class. All plugins should:
Definition: FO_Plugin.php:67
RegisterMenus()
Customize submenus.
Definition: ui-browse.php:58
menu_remove($Menu, $RmName)
Remove a menu object (based on an object name) from a menu list.
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...
Output()
This function returns the output html.
Definition: ui-browse.php:204
Traceback_parm_keep($List)
Create a new URI, keeping only these items.
outputItemHtml($uploadTreeId, $Folder, $Upload)
Definition: ui-browse.php:295
getFolderId($uploadId)
kludge for plugins not supplying a folder parameter. Find what folder this upload is in...
Definition: ui-browse.php:258
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:388
render($templateName, $vars=null)
Definition: FO_Plugin.php:442