FOSSology  4.7.0-rc1
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  if (empty($parent)) {
88  $parent = $Item;
89  }
90  $viewLicenseURI = $this->NAME . Traceback_parm_keep(array("show",
91  "format", "page", "upload")) . "&item=$parent";
92  }
93  if (GetParm("mod", PARM_STRING) == self::NAME) {
94  menu_insert("Browse::$menuName", 98);
95  menu_insert("View::$menuName", 98);
96  menu_insert("View-Meta::$menuName", 98);
97  } else {
98  $text = _("File Browser");
99  menu_insert("Browse::$menuName", 98, $URI, $text);
100  menu_insert("View::$menuName", 98, $viewLicenseURI, $text);
101  menu_insert("View-Meta::$menuName", 98, $viewLicenseURI, $text);
102  }
103  }
104 
109  protected function handle(Request $request)
110  {
111  $upload = intval($request->get("upload"));
112  $groupId = Auth::getGroupId();
113  if (!$this->uploadDao->isAccessible($upload, $groupId)) {
114  return $this->flushContent(_("Permission Denied"));
115  }
116 
117  $item = intval($request->get("item"));
118 
119  $vars['baseuri'] = Traceback_uri();
120  $vars['uploadId'] = $upload;
121  $this->uploadtree_tablename = $this->uploadDao->getUploadtreeTableName($upload);
122  if ($request->get('show')=='quick') {
123  $item = $this->uploadDao->getFatItemId($item,$upload,$this->uploadtree_tablename);
124  }
125  $vars['itemId'] = $item;
126 
127  $itemTreeBounds = $this->uploadDao->getItemTreeBounds($item, $this->uploadtree_tablename);
128  $left = $itemTreeBounds->getLeft();
129  if (empty($left)) {
130  return $this->flushContent(_("Job unpack/adj2nest hasn't completed."));
131  }
132  $histVars = $this->showUploadHist($itemTreeBounds);
133  if (is_a($histVars, 'Symfony\\Component\\HttpFoundation\\RedirectResponse')) {
134  return $histVars;
135  }
136  $vars = array_merge($vars, $histVars);
137 
138  $vars['micromenu'] = Dir2Browse($this->Name, $item, NULL, $showBox = 0, "Browse", -1, '', '', $this->uploadtree_tablename);
139 
140  $allLicensesPre = $this->licenseDao->getLicenseArray();
141  $allLicenses = array();
142  foreach ($allLicensesPre as $value) {
143  $allLicenses[$value['shortname']] = array('rf_pk' => $value['id']);
144  }
145  $vars['scannerLicenses'] = $allLicenses;
146 
147  $vars['content'] = js_url();
148 
149  return $this->render("file-browse.html.twig",$this->mergeWithDefault($vars));
150  }
151 
152 
158  private function showUploadHist(ItemTreeBounds $itemTreeBounds)
159  {
160  $groupId = Auth::getGroupId();
161  $selectedAgentId = GetParm('agentId', PARM_INTEGER);
162  $tag_pk = GetParm("tag", PARM_INTEGER);
163 
164  $uploadId = $itemTreeBounds->getUploadId();
165  $scannerAgents = array_keys($this->agentNames);
166  $scanJobProxy = new ScanJobProxy($this->agentDao, $uploadId);
167  $scannerVars = $scanJobProxy->createAgentStatus($scannerAgents);
168  $agentMap = $scanJobProxy->getAgentMap();
169 
170  $vars = array('agentId' => $selectedAgentId,
171  'agentMap' => $agentMap,
172  'scanners'=>$scannerVars);
173 
174  $selectedAgentIds = empty($selectedAgentId) ? $scanJobProxy->getLatestSuccessfulAgentIds() : $selectedAgentId;
175 
176  $this->licenseProjector = new LicenseMap($this->getObject('db.manager'),$groupId,LicenseMap::CONCLUSION,true);
177  $dirVars = $this->countFileListing($itemTreeBounds);
178  $childCount = $dirVars['iTotalRecords'];
179  /***************************************
180  * Problem: $ChildCount can be zero if you have a container that does not
181  * unpack to a directory. For example:
182  * file.gz extracts to archive.txt that contains a license.
183  * Same problem seen with .pdf and .Z files.
184  * Solution: if $ChildCount == 0, then just view the license!
185  *
186  * $ChildCount can also be zero if the directory is empty.
187  * **************************************/
188  if ($childCount == 0) {
189  return new RedirectResponse("?mod=view-license" . Traceback_parm_keep(array("upload", "item")));
190  }
191 
192  $vars['licenseUri'] = Traceback_uri() . "?mod=popup-license&rf=";
193  $vars['bulkUri'] = Traceback_uri() . "?mod=popup-license";
194 
195  return array_merge($vars, $dirVars);
196  }
197 
202  private function countFileListing(ItemTreeBounds $itemTreeBounds)
203  {
204  $isFlat = isset($_GET['flatten']);
205  $vars['isFlat'] = $isFlat;
206  $vars['iTotalRecords'] = $this->uploadDao->countNonArtifactDescendants($itemTreeBounds, $isFlat);
207  $uri = Traceback_uri().'?mod='.$this->Name.Traceback_parm_keep(array('upload','folder','show','item'));
208  $vars['fileSwitch'] = $isFlat ? $uri : $uri."&flatten=yes";
209  return $vars;
210  }
211 
212 
218  public function renderString($templateName, $vars)
219  {
220  return $this->renderer->load($templateName)->render($vars);
221  }
222 }
223 
224 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