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);
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)
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  $descendantView = new UploadTreeProxy($uploadId, $options, $itemTreeBounds->getUploadTreeTableName(), 'uberItems');
128 
129  $vars['iTotalDisplayRecords'] = $descendantView->count();
130 
131  $columnNamesInDatabase = array($isFlat?'ufile_name':'lft');
132  $defaultOrder = array(array(0, "asc"));
133  $orderString = $this->getObject('utils.data_tables_utility')->getSortingString($_GET, $columnNamesInDatabase, $defaultOrder);
134 
135  $offset = GetParm('iDisplayStart', PARM_INTEGER);
136  $limit = GetParm('iDisplayLength', PARM_INTEGER);
137  if ($offset) {
138  $orderString .= " OFFSET $offset";
139  }
140  if ($limit) {
141  $orderString .= " LIMIT $limit";
142  }
143 
144  /* Get ALL the items under this Uploadtree_pk */
145  $sql = $descendantView->getDbViewQuery()." $orderString";
146  $dbManager = $this->getObject('db.manager');
147 
148  $dbManager->prepare($stmt=__METHOD__.$orderString,$sql);
149  $res = $dbManager->execute($stmt,$descendantView->getParams());
150  $descendants = $dbManager->fetchAll($res);
151  $dbManager->freeResult($res);
152 
153  /* Filter out Children that don't have tag */
154  if (!empty($tagId)) {
155  TagFilter($descendants, $tagId, $itemTreeBounds->getUploadTreeTableName());
156  }
157  if (empty($descendants)) {
158  $vars['fileData'] = array();
159  return $vars;
160  }
161 
162  if ($isFlat) {
163  $firstChild = reset($descendants);
164  $lastChild = end($descendants);
165  $nameRange = array($firstChild['ufile_name'],$lastChild['ufile_name']);
166  } else {
167  $nameRange = array();
168  }
169 
170  /******* File Listing ************/
171  $pfileLicenses = array();
172  foreach ($selectedScanners as $agentName=>$agentId) {
173  $licensePerPfile = $this->licenseDao->getLicenseIdPerPfileForAgentId($itemTreeBounds, $agentId, $isFlat, $nameRange);
174  foreach ($licensePerPfile as $pfile => $licenseRow) {
175  foreach ($licenseRow as $licId => $row) {
176  $lic = $this->licenseProjector->getProjectedShortname($licId);
177  $pfileLicenses[$pfile][$lic][$agentName] = $row;
178  }
179  }
180  }
181 
182  $baseUri = Traceback_uri().'?mod=fileBrowse'.Traceback_parm_keep(array('upload','folder','show'));
183 
184  $tableData = array();
185  $latestSuccessfulAgentIds = $scanJobProxy->getLatestSuccessfulAgentIds();
186  foreach ($descendants as $child) {
187  if (empty($child)) {
188  continue;
189  }
190  $tableData[] = $this->createFileDataRow($child, $uploadId, $selectedAgentId,
191  $pfileLicenses, $groupId, $baseUri, $UniqueTagArray, $isFlat, $latestSuccessfulAgentIds);
192  }
193 
194  $vars['fileData'] = $tableData;
195  return $vars;
196  }
197 
198 
211  private function createFileDataRow($child, $uploadId, $selectedAgentId, $pfileLicenses, $groupId, $uri, &$UniqueTagArray, $isFlat, $latestSuccessfulAgentIds)
212  {
213  $fileId = $child['pfile_fk'];
214  $childUploadTreeId = $child['uploadtree_pk'];
215  $linkUri = '';
216  if (!empty($fileId)) {
217  $linkUri = Traceback_uri();
218  $linkUri .= "?mod=view-license&upload=$uploadId&item=$childUploadTreeId";
219  if ($selectedAgentId) {
220  $linkUri .= "&agentId=$selectedAgentId";
221  }
222  }
223 
224  /* Determine link for containers */
225  $isContainer = Iscontainer($child['ufile_mode']);
226  if ($isContainer && !$isFlat) {
227  $uploadtree_pk = $child['uploadtree_pk'];
228  $linkUri = "$uri&item=" . $uploadtree_pk;
229  if ($selectedAgentId) {
230  $linkUri .= "&agentId=$selectedAgentId";
231  }
232  } else if ($isContainer) {
233  $uploadtree_pk = Isartifact($child['ufile_mode']) ? DirGetNonArtifact($childUploadTreeId, $this->uploadtree_tablename) : $childUploadTreeId;
234  $linkUri = "$uri&item=" . $uploadtree_pk;
235  if ($selectedAgentId) {
236  $linkUri .= "&agentId=$selectedAgentId";
237  }
238  }
239 
240  /* Populate the output ($VF) - file list */
241  /* id of each element is its uploadtree_pk */
242  $fileName = htmlspecialchars($child['ufile_name']);
243  if ($isContainer) {
244  $fileName = "<a href='$linkUri'><span style='color: darkblue'> <b>$fileName</b> </span></a>";
245  } else if (!empty($linkUri)) {
246  $fileName = "<a href='$linkUri'>$fileName</a>";
247  }
248  /* show licenses under file name */
249  $childItemTreeBounds =
250  new ItemTreeBounds($childUploadTreeId, $this->uploadtree_tablename, $child['upload_fk'], $child['lft'], $child['rgt']);
251  $licenseEntries = array();
252  if ($isContainer) {
253  $agentFilter = $selectedAgentId ? array($selectedAgentId) : $latestSuccessfulAgentIds;
254  $licenseEntries = $this->licenseDao->getLicenseShortnamesContained($childItemTreeBounds, $agentFilter, array());
255  } else {
256  if (array_key_exists($fileId, $pfileLicenses)) {
257  foreach ($pfileLicenses[$fileId] as $shortName => $rfInfo) {
258  $agentEntries = array();
259  foreach ($rfInfo as $agent => $match) {
260  $agentName = $this->agentNames[$agent];
261  $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>";
262 
263  if ($match['match_percentage'] > 0) {
264  $agentEntry .= ": $match[match_percentage]%";
265  }
266  $agentEntries[] = $agentEntry;
267  }
268  $licenseEntries[] = $shortName . " [" . implode("][", $agentEntries) . "]";
269  }
270  }
271  }
272 
273  $licenseList = implode(', ', $licenseEntries);
274 
275  $fileListLinks = FileListLinks($uploadId, $childUploadTreeId, 0, $fileId, true, $UniqueTagArray, $this->uploadtree_tablename, !$isFlat);
276 
277  if (! $isContainer) {
278  $text = _("Copyright/Email/Url");
279  $fileListLinks .= "[<a href='" . Traceback_uri() . "?mod=copyright-view&upload=$uploadId&item=$childUploadTreeId' >$text</a>]";
280  $text = _("ReadMe_OSS");
281  $fileListLinks .= "[<a href='" . Traceback_uri() . "?mod=ui_readmeoss&upload=$uploadId&item=$childUploadTreeId' >$text</a>]";
282  $text = _("SPDX");
283  $fileListLinks .= "[<a href='" . Traceback_uri() . "?mod=ui_spdx2&upload=$uploadId&item=$childUploadTreeId' >$text</a>]";
284  }
285 
286  return array($fileName, $licenseList, $fileListLinks);
287  }
288 }
289 
290 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)
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.