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