FOSSology  4.4.0
Open Source License Compliance by Open Source Software
ui-file-browse.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2008-2015 Hewlett-Packard Development Company, L.P.
4  SPDX-FileCopyrightText: © 2014-2015 Siemens AG
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
18 use Symfony\Component\HttpFoundation\RedirectResponse;
19 use Symfony\Component\HttpFoundation\Request;
20 use Symfony\Component\HttpFoundation\Response;
22 
29 {
30  const NAME = "fileBrowse";
31 
32  private $uploadtree_tablename = "";
34  private $uploadDao;
36  private $licenseDao;
38  private $agentDao;
40  private $licenseProjector;
42  protected $agentNames = AgentRef::AGENT_LIST;
43 
44  public function __construct()
45  {
46  parent::__construct(self::NAME, array(
47  self::TITLE => _("File Browser"),
48  self::DEPENDENCIES => array("browse", "view"),
49  self::PERMISSION => Auth::PERM_READ,
50  self::REQUIRES_LOGIN => false
51  ));
52 
53  global $container;
54  $this->uploadDao = $container->get('dao.upload');
55  $this->licenseDao = $container->get('dao.license');
56  $this->agentDao = $container->get('dao.agent');
57  }
58 
62  function RegisterMenus()
63  {
64  $text = _("File Browser");
65  menu_insert("Browse-Pfile::File Browser", 20, 'fileBrowse', $text);
66 
67  // For all other menus, permit coming back here.
68  $URI = $this->Name . Traceback_parm_keep(array("upload", "item", "show"));
69 
70  $Item = GetParm("item", PARM_INTEGER);
71  $Upload = GetParm("upload", PARM_INTEGER);
72  if (empty($Item) || empty($Upload)) {
73  return;
74  }
75  $viewLicenseURI = $this->Name . Traceback_parm_keep(array("show", "format", "page", "upload", "item"));
76  $menuName = $this->Title;
77 
78  $uploadTreeTable = $this->uploadDao->getUploadtreeTableName($Upload);
79  $itemBounds = $this->uploadDao->getItemTreeBounds($Item, $uploadTreeTable);
80  if (! $itemBounds->containsFiles()) {
81  global $container;
85  $treeDao = $container->get('dao.tree');
86  $parent = $treeDao->getParentOfItem($itemBounds);
87  $viewLicenseURI = $this->NAME . Traceback_parm_keep(array("show",
88  "format", "page", "upload")) . "&item=$parent";
89  }
90  if (GetParm("mod", PARM_STRING) == self::NAME) {
91  menu_insert("Browse::$menuName", 98);
92  menu_insert("View::$menuName", 98);
93  menu_insert("View-Meta::$menuName", 98);
94  } else {
95  $text = _("File Browser");
96  menu_insert("Browse::$menuName", 98, $URI, $text);
97  menu_insert("View::$menuName", 98, $viewLicenseURI, $text);
98  menu_insert("View-Meta::$menuName", 98, $viewLicenseURI, $text);
99  }
100  }
101 
106  protected function handle(Request $request)
107  {
108  $upload = intval($request->get("upload"));
109  $groupId = Auth::getGroupId();
110  if (!$this->uploadDao->isAccessible($upload, $groupId)) {
111  return $this->flushContent(_("Permission Denied"));
112  }
113 
114  $item = intval($request->get("item"));
115 
116  $vars['baseuri'] = Traceback_uri();
117  $vars['uploadId'] = $upload;
118  $this->uploadtree_tablename = $this->uploadDao->getUploadtreeTableName($upload);
119  if ($request->get('show')=='quick') {
120  $item = $this->uploadDao->getFatItemId($item,$upload,$this->uploadtree_tablename);
121  }
122  $vars['itemId'] = $item;
123 
124  $itemTreeBounds = $this->uploadDao->getItemTreeBounds($item, $this->uploadtree_tablename);
125  $left = $itemTreeBounds->getLeft();
126  if (empty($left)) {
127  return $this->flushContent(_("Job unpack/adj2nest hasn't completed."));
128  }
129  $histVars = $this->showUploadHist($itemTreeBounds);
130  if (is_a($histVars, 'Symfony\\Component\\HttpFoundation\\RedirectResponse')) {
131  return $histVars;
132  }
133  $vars = array_merge($vars, $histVars);
134 
135  $vars['micromenu'] = Dir2Browse($this->Name, $item, NULL, $showBox = 0, "Browse", -1, '', '', $this->uploadtree_tablename);
136 
137  $allLicensesPre = $this->licenseDao->getLicenseArray();
138  $allLicenses = array();
139  foreach ($allLicensesPre as $value) {
140  $allLicenses[$value['shortname']] = array('rf_pk' => $value['id']);
141  }
142  $vars['scannerLicenses'] = $allLicenses;
143 
144  $vars['content'] = js_url();
145 
146  return $this->render("file-browse.html.twig",$this->mergeWithDefault($vars));
147  }
148 
149 
155  private function showUploadHist(ItemTreeBounds $itemTreeBounds)
156  {
157  $groupId = Auth::getGroupId();
158  $selectedAgentId = GetParm('agentId', PARM_INTEGER);
159  $tag_pk = GetParm("tag", PARM_INTEGER);
160 
161  $uploadId = $itemTreeBounds->getUploadId();
162  $scannerAgents = array_keys($this->agentNames);
163  $scanJobProxy = new ScanJobProxy($this->agentDao, $uploadId);
164  $scannerVars = $scanJobProxy->createAgentStatus($scannerAgents);
165  $agentMap = $scanJobProxy->getAgentMap();
166 
167  $vars = array('agentId' => $selectedAgentId,
168  'agentMap' => $agentMap,
169  'scanners'=>$scannerVars);
170 
171  $selectedAgentIds = empty($selectedAgentId) ? $scanJobProxy->getLatestSuccessfulAgentIds() : $selectedAgentId;
172 
173  $this->licenseProjector = new LicenseMap($this->getObject('db.manager'),$groupId,LicenseMap::CONCLUSION,true);
174  $dirVars = $this->countFileListing($itemTreeBounds);
175  $childCount = $dirVars['iTotalRecords'];
176  /***************************************
177  * Problem: $ChildCount can be zero if you have a container that does not
178  * unpack to a directory. For example:
179  * file.gz extracts to archive.txt that contains a license.
180  * Same problem seen with .pdf and .Z files.
181  * Solution: if $ChildCount == 0, then just view the license!
182  *
183  * $ChildCount can also be zero if the directory is empty.
184  * **************************************/
185  if ($childCount == 0) {
186  return new RedirectResponse("?mod=view-license" . Traceback_parm_keep(array("upload", "item")));
187  }
188 
189  $vars['licenseUri'] = Traceback_uri() . "?mod=popup-license&rf=";
190  $vars['bulkUri'] = Traceback_uri() . "?mod=popup-license";
191 
192  return array_merge($vars, $dirVars);
193  }
194 
199  private function countFileListing(ItemTreeBounds $itemTreeBounds)
200  {
201  $isFlat = isset($_GET['flatten']);
202  $vars['isFlat'] = $isFlat;
203  $vars['iTotalRecords'] = $this->uploadDao->countNonArtifactDescendants($itemTreeBounds, $isFlat);
204  $uri = Traceback_uri().'?mod='.$this->Name.Traceback_parm_keep(array('upload','folder','show','item'));
205  $vars['fileSwitch'] = $isFlat ? $uri : $uri."&flatten=yes";
206  return $vars;
207  }
208 
209 
215  public function renderString($templateName, $vars)
216  {
217  return $this->renderer->load($templateName)->render($vars);
218  }
219 }
220 
221 register_plugin(new ui_file_browse());
char * uploadtree_tablename
upload.uploadtree_tablename
Definition: adj2nest.c:100
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
Wrapper class for license map.
Definition: LicenseMap.php:19
RegisterMenus()
Customize submenus.
render($templateName, $vars=null, $headers=null)
handle(Request $request)
renderString($templateName, $vars)
countFileListing(ItemTreeBounds $itemTreeBounds)
showUploadHist(ItemTreeBounds $itemTreeBounds)
Given an $Uploadtree_pk, display:
js_url()
Load a new url.
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.
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 PERM_READ
Read-only permission.
Definition: libfossology.h:32