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