FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
AjaxSHDetailsBrowser.php
1 <?php
2 /***********************************************************
3 Copyright (C) 2019
4 Author: Sandip Kumar Bhuyan<sandipbhyan@gmail.com>
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  ***********************************************************/
30 use \Symfony\Component\HttpFoundation\Request;
32 
34 {
35  const NAME = "ajax_sh_browser";
36 
37  private $uploadtree_tablename = "";
39  private $uploadDao;
41  private $licenseDao;
43  private $agentDao;
48  private $shDao;
53  private $configuration;
54 
55  protected $agentNames = array('softwareHeritage' => 'SH');
56 
57  public function __construct()
58  {
59  parent::__construct(self::NAME, array(
60  self::TITLE => _("Ajax: File Browser"),
61  self::DEPENDENCIES => array("fileBrowse"),
62  self::PERMISSION => Auth::PERM_READ,
63  self::REQUIRES_LOGIN => false
64  ));
65 
66  $this->uploadDao = $this->getObject('dao.upload');
67  $this->licenseDao = $this->getObject('dao.license');
68  $this->agentDao = $this->getObject('dao.agent');
69  $this->shDao = $this->container->get('dao.softwareHeritage');
70  $this->configuration = parse_ini_file(__DIR__ . '/../agent/softwareHeritage.conf');
71  }
72 
77  public function handle(Request $request)
78  {
79  $upload = intval($request->get("upload"));
80  $groupId = Auth::getGroupId();
81  if (!$this->uploadDao->isAccessible($upload, $groupId)) {
82  throw new \Exception("Permission Denied");
83  }
84 
85  $item = intval($request->get("item"));
86  $this->uploadtree_tablename = $this->uploadDao->getUploadtreeTableName($upload);
87  $itemTreeBounds = $this->uploadDao->getItemTreeBounds($item, $this->uploadtree_tablename);
88  $left = $itemTreeBounds->getLeft();
89  if (empty($left)) {
90  throw new \Exception("Job unpack/adj2nest hasn't completed.");
91  }
92 
93  $scannerAgents = array_keys($this->agentNames);
94  $scanJobProxy = new ScanJobProxy($this->agentDao, $upload);
95  $scanJobProxy->createAgentStatus($scannerAgents);
96  $selectedAgentId = intval($request->get('agentId'));
97  $tag_pk = intval($request->get('tag'));
98 
99  $UniqueTagArray = array();
100  $this->licenseProjector = new LicenseMap($this->getObject('db.manager'),$groupId,LicenseMap::CONCLUSION,true);
101  $vars = $this->createFileListing($tag_pk, $itemTreeBounds, $UniqueTagArray, $selectedAgentId, $groupId, $scanJobProxy);
102 
103  return new JsonResponse(array(
104  'sEcho' => intval($request->get('sEcho')),
105  'aaData' => $vars['fileData'],
106  'iTotalRecords' => $vars['iTotalDisplayRecords'],
107  'iTotalDisplayRecords' => $vars['iTotalDisplayRecords']
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=sh-agent'.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  $baseUri, $UniqueTagArray, $isFlat);
204  }
205 
206  $vars['fileData'] = $tableData;
207  return $vars;
208  }
209 
219  private function createFileDataRow($child, $uploadId, $selectedAgentId, $uri, &$UniqueTagArray, $isFlat)
220  {
221  $fileId = $child['pfile_fk'];
222  $childUploadTreeId = $child['uploadtree_pk'];
223  $linkUri = '';
224  if (!empty($fileId)) {
225  $linkUri = Traceback_uri();
226  $linkUri .= "?mod=view-license&upload=$uploadId&item=$childUploadTreeId";
227  if ($selectedAgentId) {
228  $linkUri .= "&agentId=$selectedAgentId";
229  }
230  }
231 
232  /* Determine link for containers */
233  $isContainer = Iscontainer($child['ufile_mode']);
234  if ($isContainer && !$isFlat) {
235  $uploadtree_pk = $child['uploadtree_pk'];
236  $linkUri = "$uri&item=" . $uploadtree_pk;
237  if ($selectedAgentId) {
238  $linkUri .= "&agentId=$selectedAgentId";
239  }
240  } else if ($isContainer) {
241  $uploadtree_pk = Isartifact($child['ufile_mode']) ? DirGetNonArtifact($childUploadTreeId, $this->uploadtree_tablename) : $childUploadTreeId;
242  $linkUri = "$uri&item=" . $uploadtree_pk;
243  if ($selectedAgentId) {
244  $linkUri .= "&agentId=$selectedAgentId";
245  }
246  }
247 
248  /* Populate the output ($VF) - file list */
249  /* id of each element is its uploadtree_pk */
250  $fileName = htmlspecialchars($child['ufile_name']);
251  if ($isContainer) {
252  $fileName = "<a href='$linkUri'><span style='color: darkblue'> <b>$fileName</b> </span></a>";
253  } else if (!empty($linkUri)) {
254  $fileName = "<a href='$linkUri'>$fileName</a>";
255  }
256 
257  $pfileHash = $this->uploadDao->getUploadHashesFromPfileId($fileId);
258  $shRecord = $this->shDao->getSoftwareHetiageRecord($fileId);
259  $fileListLinks = FileListLinks($uploadId, $childUploadTreeId, 0, $fileId, true, $UniqueTagArray, $this->uploadtree_tablename, !$isFlat);
260 
261  if (! $isContainer) {
262  $text = _("Software Heritage");
263  $shLink = $this->configuration['api']['url'].$this->configuration['api']['uri'].$pfileHash["sha256"].$this->configuration['api']['content'];
264  $fileListLinks .= "[<a href='".$shLink."' target=\"_blank\">$text</a>]";
265  }
266  $img = "";
267  if (! $isContainer) {
268  $img = $shRecord["img"];
269  }
270 
271  return [$fileName, $pfileHash["sha256"], $shRecord["license"], $img, $fileListLinks];
272  }
273 }
274 
275 register_plugin(new AjaxSHDetailsBrowser());
Traceback_uri()
Get the URI without query to this location.
createFileDataRow($child, $uploadId, $selectedAgentId, $uri, &$UniqueTagArray, $isFlat)
handle(Request $request)
Iscontainer($mode)
Definition: common-dir.php:49
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
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
createFileListing($tagId, ItemTreeBounds $itemTreeBounds, &$UniqueTagArray, $selectedAgentId, $groupId, $scanJobProxy)
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
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