FOSSology  4.4.0
Open Source License Compliance by Open Source Software
FileSearchController.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2020 Siemens AG
4  Author: Gaurav Mishra <mishra.gaurav@siemens.com>
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
13 namespace Fossology\UI\Api\Controllers;
14 
22 use Psr\Container\ContainerInterface;
23 use Psr\Http\Message\ServerRequestInterface;
24 
30 {
35  private $fileHelper;
36 
41  private $clearingDao;
42 
47  private $licenseDao;
48 
52  public function __construct($container)
53  {
54  parent::__construct($container);
55  $this->fileHelper = $this->container->get('helper.fileHelper');
56  $this->clearingDao = $this->container->get('dao.clearing');
57  $this->licenseDao = $this->container->get('dao.license');
58  }
59 
68  public function getFiles($request, $response, $args)
69  {
70  $fileListJSON = $this->getParsedBody($request);
71  $inputFileList = File::parseFromArray($fileListJSON);
72  $existsList = [];
73  $nonExistsList = [];
74 
75  foreach ($inputFileList as $inputFile) {
76  if ($inputFile->getMessage() == File::INVALID) {
77  $nonExistsList[] = $inputFile;
78  continue;
79  }
80 
81  $pfileId = $this->getPfileId($inputFile);
82  if ($pfileId !== false) {
83  $existsList[$pfileId] = $inputFile;
84  } else {
85  $inputFile->setMessage(File::NOT_FOUND);
86  $nonExistsList[] = $inputFile;
87  }
88  }
89 
90  $this->getInfoForFiles($existsList);
91  $existsList = array_values($existsList);
92 
93  foreach ($nonExistsList as $file) {
94  $existsList[] = $file;
95  }
96  return $response->withJson(array_map(function ($file) {
97  return $file->getArray();
98  }, $existsList), 200);
99  }
100 
108  private function getPfileId(&$file)
109  {
110  $hash = $file->getHash();
111  $pfile = $this->fileHelper->getPfile($hash);
112  if ($pfile === null) {
113  return false;
114  }
115  $newHash = new Hash($pfile['pfile_sha1'], $pfile['pfile_md5'],
116  $pfile['pfile_sha256'], $pfile['pfile_size']);
117  $file->setHash($newHash);
118  return intval($pfile['pfile_pk']);
119  }
120 
127  private function getInfoForFiles(&$inputFileList)
128  {
129  foreach ($inputFileList as $pfileId => $file) {
130  $uploads = $this->getPackageUpload($pfileId);
131  if (! empty($uploads)) {
132  $scannerFindings = [];
133  $copyright = [];
134  $conclusions = $this->getMainLicenses($uploads);
135  } else {
136  $scannerFindings = $this->getFileFindings($pfileId);
137  $conclusions = $this->getFileConclusions($pfileId);
138  $copyright = $this->getFileCopyright($pfileId);
139  }
140  $findings = new Findings();
141  $findings->setScanner($scannerFindings);
142  $findings->setConclusion($conclusions);
143  $findings->setCopyright($copyright);
144  $inputFileList[$pfileId]->setFindings($findings);
145  $inputFileList[$pfileId]->setUploads($uploads);
146  }
147  }
148 
154  private function getFileFindings($pfileId)
155  {
156  return $this->fileHelper->pfileScannerFindings($pfileId);
157  }
158 
164  private function getFileConclusions($pfileId)
165  {
166  return $this->fileHelper->pfileConclusions($this->restHelper->getGroupId(),
167  $pfileId);
168  }
169 
175  private function getFileCopyright($pfileId)
176  {
177  return $this->fileHelper->pfileCopyright($pfileId);
178  }
179 
184  private function getPackageUpload($pfileId)
185  {
186  return $this->filterAccessibleUploads(
187  $this->fileHelper->getPackageUpload($pfileId));
188  }
189 
195  private function filterAccessibleUploads($uploads)
196  {
197  if (empty($uploads)) {
198  return [];
199  }
200  return array_filter($uploads, function ($upload) {
201  return $this->restHelper->getUploadDao()->isAccessible($upload,
202  $this->restHelper->getGroupId());
203  });
204  }
205 
211  private function getMainLicenses($uploads)
212  {
213  $mainLicenses = array();
214  foreach ($uploads as $upload) {
215  $licenses = $this->clearingDao->getMainLicenseIds($upload,
216  $this->restHelper->getGroupId());
217  foreach ($licenses as $licenseId) {
218  $license = $this->licenseDao->getLicenseById($licenseId,
219  $this->restHelper->getGroupId());
220  if ($license !== null) {
221  $mainLicenses[$license->getId()] = $license->getShortName();
222  }
223  }
224  }
225  natcasesort($mainLicenses);
226  return array_values($mainLicenses);
227  }
228 }
Base controller for REST calls.
getParsedBody(ServerRequestInterface $request)
Parse request body as JSON and return associative PHP array.
Handle file related queries.
Definition: FileHelper.php:23
Override Slim response for withJson function.
File model holding information about a single file.
Definition: File.php:19
static parseFromArray($inputList)
Definition: File.php:169
Model holding information about license findings and conclusions.
Definition: Findings.php:18
Hash model holding information about file like checksums and size.
Definition: Hash.php:18