FOSSology  4.4.0
Open Source License Compliance by Open Source Software
AjaxFileBrowser.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 
9 namespace Fossology\UI\Ajax;
10 
20 use Symfony\Component\HttpFoundation\JsonResponse;
21 use Symfony\Component\HttpFoundation\Request;
22 use Symfony\Component\HttpFoundation\Response;
24 
31 {
32  const NAME = "ajax_file_browser";
33 
34  private $uploadtree_tablename = "";
36  private $uploadDao;
38  private $licenseDao;
40  private $agentDao;
42  private $licenseProjector;
44  protected $agentNames = AgentRef::AGENT_LIST;
45 
46  public function __construct()
47  {
48  parent::__construct(self::NAME, array(
49  self::TITLE => _("Ajax: File Browser"),
50  self::DEPENDENCIES => array("fileBrowse"),
51  self::PERMISSION => Auth::PERM_READ,
52  self::REQUIRES_LOGIN => false
53  ));
54 
55  $this->uploadDao = $this->getObject('dao.upload');
56  $this->licenseDao = $this->getObject('dao.license');
57  $this->agentDao = $this->getObject('dao.agent');
58  }
59 
64  protected function handle(Request $request)
65  {
66  $upload = intval($request->get("upload"));
67  $groupId = Auth::getGroupId();
68  if (!$this->uploadDao->isAccessible($upload, $groupId)) {
69  throw new \Exception("Permission Denied");
70  }
71 
72  $item = intval($request->get("item"));
73  $this->uploadtree_tablename = $this->uploadDao->getUploadtreeTableName($upload);
74  $itemTreeBounds = $this->uploadDao->getItemTreeBounds($item, $this->uploadtree_tablename);
75  $left = $itemTreeBounds->getLeft();
76  if (empty($left)) {
77  throw new \Exception("Job unpack/adj2nest hasn't completed.");
78  }
79 
80  $scannerAgents = array_keys($this->agentNames);
81  $scanJobProxy = new ScanJobProxy($this->agentDao, $upload);
82  $scanJobProxy->createAgentStatus($scannerAgents);
83  $selectedAgentId = intval($request->get('agentId'));
84  $tag_pk = intval($request->get('tag'));
85 
86  $UniqueTagArray = array();
87  $this->licenseProjector = new LicenseMap($this->getObject('db.manager'),$groupId,LicenseMap::CONCLUSION,true);
88  $vars = $this->createFileListing($tag_pk, $itemTreeBounds, $UniqueTagArray, $selectedAgentId, $groupId, $scanJobProxy, $request);
89 
90  return new JsonResponse(array(
91  'sEcho' => intval($request->get('sEcho')),
92  'aaData' => $vars['fileData'],
93  'iTotalRecords' => intval($request->get('totalRecords')),
94  'iTotalDisplayRecords' => $vars['iTotalDisplayRecords']
95  ) );
96  }
97 
98 
108  private function createFileListing($tagId, ItemTreeBounds $itemTreeBounds, &$UniqueTagArray, $selectedAgentId, $groupId, $scanJobProxy, $request)
109  {
110  if (!empty($selectedAgentId)) {
111  $agentName = $this->agentDao->getAgentName($selectedAgentId);
112  $selectedScanners = array($agentName=>$selectedAgentId);
113  } else {
114  $selectedScanners = $scanJobProxy->getLatestSuccessfulAgentIds();
115  }
116 
118  $uploadId = $itemTreeBounds->getUploadId();
119  $isFlat = isset($_GET['flatten']);
120 
121  if ($isFlat) {
122  $options = array(UploadTreeProxy::OPT_RANGE => $itemTreeBounds);
123  } else {
124  $options = array(UploadTreeProxy::OPT_REALPARENT => $itemTreeBounds->getItemId());
125  }
126 
127  $searchMap = array();
128  foreach (explode(' ',$request->get('sSearch')) as $pair) {
129  $a = explode(':',$pair);
130  if (count($a) == 1) {
131  $searchMap['head'] = $pair;
132  } else {
133  $searchMap[$a[0]] = $a[1];
134  }
135  }
136 
137  if (array_key_exists('ext', $searchMap) && strlen($searchMap['ext'])>=1) {
138  $options[UploadTreeProxy::OPT_EXT] = $searchMap['ext'];
139  }
140  if (array_key_exists('head', $searchMap) && strlen($searchMap['head'])>=1) {
141  $options[UploadTreeProxy::OPT_HEAD] = $searchMap['head'];
142  }
143  if (($rfId=$request->get('scanFilter'))>0) {
144  $options[UploadTreeProxy::OPT_AGENT_SET] = $selectedScanners;
145  $options[UploadTreeProxy::OPT_SCAN_REF] = $rfId;
146  }
147 
148  $descendantView = new UploadTreeProxy($uploadId, $options, $itemTreeBounds->getUploadTreeTableName(), 'uberItems');
149 
150  $vars['iTotalDisplayRecords'] = $descendantView->count();
151 
152  $columnNamesInDatabase = array($isFlat?'ufile_name':'lft');
153  $defaultOrder = array(array(0, "asc"));
154  $orderString = $this->getObject('utils.data_tables_utility')->getSortingString($_GET, $columnNamesInDatabase, $defaultOrder);
155 
156  $offset = GetParm('iDisplayStart', PARM_INTEGER);
157  $limit = GetParm('iDisplayLength', PARM_INTEGER);
158  if ($offset) {
159  $orderString .= " OFFSET $offset";
160  }
161  if ($limit) {
162  $orderString .= " LIMIT $limit";
163  }
164 
165  /* Get ALL the items under this Uploadtree_pk */
166  $sql = $descendantView->getDbViewQuery()." $orderString";
167  $dbManager = $this->getObject('db.manager');
168 
169  $dbManager->prepare($stmt=__METHOD__.$orderString,$sql);
170  $res = $dbManager->execute($stmt,$descendantView->getParams());
171  $descendants = $dbManager->fetchAll($res);
172  $dbManager->freeResult($res);
173 
174  /* Filter out Children that don't have tag */
175  if (!empty($tagId)) {
176  TagFilter($descendants, $tagId, $itemTreeBounds->getUploadTreeTableName());
177  }
178  if (empty($descendants)) {
179  $vars['fileData'] = array();
180  return $vars;
181  }
182 
183  if ($isFlat) {
184  $firstChild = reset($descendants);
185  $lastChild = end($descendants);
186  $nameRange = array($firstChild['ufile_name'],$lastChild['ufile_name']);
187  } else {
188  $nameRange = array();
189  }
190 
191  /******* File Listing ************/
192  $pfileLicenses = array();
193  foreach ($selectedScanners as $agentName=>$agentId) {
194  $licensePerPfile = $this->licenseDao->getLicenseIdPerPfileForAgentId($itemTreeBounds, $agentId, $isFlat, $nameRange);
195  foreach ($licensePerPfile as $pfile => $licenseRow) {
196  foreach ($licenseRow as $licId => $row) {
197  $lic = $this->licenseProjector->getProjectedShortname($licId);
198  $pfileLicenses[$pfile][$lic][$agentName] = $row;
199  }
200  }
201  }
202 
203  $baseUri = Traceback_uri().'?mod=fileBrowse'.Traceback_parm_keep(array('upload','folder','show'));
204 
205  $tableData = array();
206  $latestSuccessfulAgentIds = $scanJobProxy->getLatestSuccessfulAgentIds();
207  foreach ($descendants as $child) {
208  if (empty($child)) {
209  continue;
210  }
211  $tableData[] = $this->createFileDataRow($child, $uploadId, $selectedAgentId,
212  $pfileLicenses, $groupId, $baseUri, $UniqueTagArray, $isFlat, $latestSuccessfulAgentIds);
213  }
214 
215  $vars['fileData'] = $tableData;
216  return $vars;
217  }
218 
219 
232  private function createFileDataRow($child, $uploadId, $selectedAgentId, $pfileLicenses, $groupId, $uri, &$UniqueTagArray, $isFlat, $latestSuccessfulAgentIds)
233  {
234  $fileId = $child['pfile_fk'];
235  $childUploadTreeId = $child['uploadtree_pk'];
236  $linkUri = '';
237  if (!empty($fileId)) {
238  $linkUri = Traceback_uri();
239  $linkUri .= "?mod=view-license&upload=$uploadId&item=$childUploadTreeId";
240  if ($selectedAgentId) {
241  $linkUri .= "&agentId=$selectedAgentId";
242  }
243  }
244 
245  /* Determine link for containers */
246  $isContainer = Iscontainer($child['ufile_mode']);
247  if ($isContainer && !$isFlat) {
248  $uploadtree_pk = $child['uploadtree_pk'];
249  $linkUri = "$uri&item=" . $uploadtree_pk;
250  if ($selectedAgentId) {
251  $linkUri .= "&agentId=$selectedAgentId";
252  }
253  } else if ($isContainer) {
254  $uploadtree_pk = Isartifact($child['ufile_mode']) ? DirGetNonArtifact($childUploadTreeId, $this->uploadtree_tablename) : $childUploadTreeId;
255  $linkUri = "$uri&item=" . $uploadtree_pk;
256  if ($selectedAgentId) {
257  $linkUri .= "&agentId=$selectedAgentId";
258  }
259  }
260 
261  /* Populate the output ($VF) - file list */
262  /* id of each element is its uploadtree_pk */
263  $fileName = htmlspecialchars($child['ufile_name']);
264  if ($isContainer) {
265  $fileName = "<a href='$linkUri'><span style='color: darkblue'> <b>$fileName</b> </span></a>";
266  } else if (!empty($linkUri)) {
267  $fileName = "<a href='$linkUri'>$fileName</a>";
268  }
269  /* show licenses under file name */
270  $childItemTreeBounds =
271  new ItemTreeBounds($childUploadTreeId, $this->uploadtree_tablename, $child['upload_fk'], $child['lft'], $child['rgt']);
272  $licenseEntries = array();
273  if ($isContainer) {
274  $agentFilter = $selectedAgentId ? array($selectedAgentId) : $latestSuccessfulAgentIds;
275  $licenseEntries = $this->licenseDao->getLicenseShortnamesContained($childItemTreeBounds, $agentFilter, array());
276  } else {
277  if (array_key_exists($fileId, $pfileLicenses)) {
278  foreach ($pfileLicenses[$fileId] as $shortName => $rfInfo) {
279  $agentEntries = array();
280  foreach ($rfInfo as $agent => $match) {
281  $agentName = $this->agentNames[$agent];
282  $agentEntry = "<a href='?mod=view-license&upload=$child[upload_fk]&item=$childUploadTreeId&format=text&agentId=$match[agent_id]&licenseId=$match[license_id]#highlight'>" . $agentName . "</a>";
283 
284  if ($match['match_percentage'] > 0) {
285  $agentEntry .= ": $match[match_percentage]%";
286  }
287  $agentEntries[] = $agentEntry;
288  }
289  $licenseEntries[] = $shortName . " [" . implode("][", $agentEntries) . "]";
290  }
291  }
292  }
293 
294  $licenseList = implode(', ', $licenseEntries);
295 
296  $fileListLinks = FileListLinks($uploadId, $childUploadTreeId, 0, $fileId, true, $UniqueTagArray, $this->uploadtree_tablename, !$isFlat);
297 
298  if (! $isContainer) {
299  $text = _("Copyright/Email/Url");
300  $fileListLinks .= "[<a href='" . Traceback_uri() . "?mod=copyright-view&upload=$uploadId&item=$childUploadTreeId' >$text</a>]";
301  $text = _("ReadMe_OSS");
302  $fileListLinks .= "[<a href='" . Traceback_uri() . "?mod=ui_readmeoss&upload=$uploadId&item=$childUploadTreeId' >$text</a>]";
303  $text = _("SPDX");
304  $fileListLinks .= "[<a href='" . Traceback_uri() . "?mod=ui_spdx2&upload=$uploadId&item=$childUploadTreeId' >$text</a>]";
305  }
306 
307  return array($fileName, $licenseList, $fileListLinks);
308  }
309 }
310 
311 register_plugin(new AjaxFileBrowser());
char * uploadtree_tablename
upload.uploadtree_tablename
Definition: adj2nest.c:100
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
static getGroupId()
Get the current user's group id.
Definition: Auth.php:80
Wrapper class for license map.
Definition: LicenseMap.php:19
createFileListing($tagId, ItemTreeBounds $itemTreeBounds, &$UniqueTagArray, $selectedAgentId, $groupId, $scanJobProxy, $request)
createFileDataRow($child, $uploadId, $selectedAgentId, $pfileLicenses, $groupId, $uri, &$UniqueTagArray, $isFlat, $latestSuccessfulAgentIds)
Isartifact($mode)
Definition: common-dir.php:29
DirGetNonArtifact($UploadtreePk, $uploadtree_tablename='uploadtree')
Given an artifact directory (uploadtree_pk), return the first non-artifact directory (uploadtree_pk).
Definition: common-dir.php:158
Iscontainer($mode)
Definition: common-dir.php:38
FileListLinks($upload_fk, $uploadtree_pk, $napk, $pfile_pk, $Recurse=True, &$UniqueTagArray=array(), $uploadtree_tablename="uploadtree", $wantTags=true)
Get list of links: [View][Info][Download]
Traceback_uri()
Get the URI without query to this location.
Definition: common-parm.php:97
const PARM_INTEGER
Definition: common-parm.php:14
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.
TagFilter(&$UploadtreeRows, $tag_pk, $uploadtree_tablename)
Given a list of uploadtree recs, remove recs that do not have $tag_pk.