FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
ReportController.php
Go to the documentation of this file.
1 <?php
2 /***************************************************************
3  Copyright (C) 2018 Siemens AG
4  Author: Gaurav Mishra <mishra.gaurav@siemens.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  ***************************************************************/
25 
32 
38 {
39 
44  private $reportsAllowed = array(
45  'dep5',
46  'spdx2',
47  'spdx2tv',
48  'readmeoss',
49  'unifiedreport'
50  );
51 
60  public function getReport($request, $response, $args)
61  {
62  $uploadId = $request->getHeaderLine('uploadId');
63  $reportFormat = $request->getHeaderLine('reportFormat');
64 
65  if (! in_array($reportFormat, $this->reportsAllowed)) {
66  $error = new Info(400,
67  "reportFormat must be from [" . implode(",", $this->reportsAllowed) . "]",
68  InfoType::ERROR);
69  return $response->withJson($error->getArray(), $error->getCode());
70  }
71  $upload = $this->getUpload($uploadId);
72  if (get_class($upload) === Info::class) {
73  return $response->withJson($upload->getArray(), $upload->getCode());
74  }
75  $jobId = null;
76  $jobQueueId = null;
77  $error = "";
78 
79  try {
80  switch ($reportFormat) {
81  case $this->reportsAllowed[0]:
82  case $this->reportsAllowed[1]:
83  case $this->reportsAllowed[2]:
84  $spdxGenerator = $this->restHelper->getPlugin('ui_spdx2');
85  list ($jobId, $jobQueueId, $error) = $spdxGenerator->scheduleAgent(
86  Auth::getGroupId(), $upload, $reportFormat);
87  break;
88  case $this->reportsAllowed[3]:
89  $readmeGenerator = $this->restHelper->getPlugin('ui_readmeoss');
90  list ($jobId, $jobQueueId, $error) = $readmeGenerator->scheduleAgent(
91  Auth::getGroupId(), $upload);
92  break;
93  case $this->reportsAllowed[4]:
94  $unifiedGenerator = $this->restHelper->getPlugin('agent_founifiedreport');
95  list ($jobId, $jobQueueId, $error) = $unifiedGenerator->scheduleAgent(
96  Auth::getGroupId(), $upload);
97  break;
98  default:
99  $error = new Info(500, "Some error occured!", InfoType::ERROR);
100  return $response->withJson($error->getArray(), $error->getCode());
101  }
102  } catch (\Exception $e) {
103  $error = new Info(500, $e->getMessage(), InfoType::ERROR);
104  return $response->withJson($error->getArray(), $error->getCode());
105  }
106  if (! empty($error)) {
107  $info = new Info(500, $error, InfoType::ERROR);
108  } else {
109  $download_path = $this->buildDownloadPath($request, $jobId);
110  $info = new Info(201, $download_path, InfoType::INFO);
111  }
112  return $response->withJson($info->getArray(), $info->getCode());
113  }
114 
121  private function getUpload($uploadId)
122  {
123  $upload = null;
124  if (empty($uploadId) || ! is_numeric($uploadId) || $uploadId <= 0) {
125  $upload = new Info(400, "uploadId must be a positive integer!",
126  InfoType::ERROR);
127  }
128  $uploadDao = $this->restHelper->getUploadDao();
129  if (! $uploadDao->isAccessible($uploadId, $this->restHelper->getGroupId())) {
130  $upload = new Info(403, "Upload is not accessible!", InfoType::ERROR);
131  }
132  if ($upload !== null) {
133  return $upload;
134  }
135  $upload = $uploadDao->getUpload($uploadId);
136  if ($upload === null) {
137  $upload = new Info(404, "Upload does not exists!", InfoType::ERROR);
138  }
139  return $upload;
140  }
141 
148  private function buildDownloadPath($request, $jobId)
149  {
150  $path = $request->getUri()->getHost();
151  $path .= $request->getRequestTarget();
152  $url_parts = parse_url($path);
153  $download_path = "";
154  if (array_key_exists("scheme", $url_parts)) {
155  $download_path .= $url_parts["scheme"] . "://";
156  }
157  if (array_key_exists("user", $url_parts)) {
158  $download_path .= $url_parts["user"];
159  }
160  if (array_key_exists("pass", $url_parts)) {
161  $download_path .= ':' . $url_parts["pass"];
162  }
163  if (array_key_exists("host", $url_parts)) {
164  $download_path .= $url_parts["host"];
165  }
166  if (array_key_exists("port", $url_parts)) {
167  $download_path .= ':' . $url_parts["port"];
168  }
169  if ($url_parts["path"][-1] !== '/') {
170  $url_parts["path"] .= '/';
171  }
172  $download_path .= $url_parts["path"] . $jobId;
173  if (array_key_exists("query", $url_parts)) {
174  $download_path .= '?' . $url_parts["query"];
175  }
176  if (array_key_exists("fragment", $url_parts)) {
177  $download_path .= '#' . $url_parts["fragment"];
178  }
179  return $download_path;
180  }
181 
190  public function downloadReport($request, $response, $args)
191  {
192  $id = $args['id'];
193  $returnVal = $this->checkReport($id);
194  if ($returnVal !== true) {
195  $newResponse = $response;
196  if ($returnVal->getCode() == 503) {
197  $newResponse = $response->withHeader('Retry-After', '10');
198  }
199  return $newResponse->withJson($returnVal->getArray(),
200  $returnVal->getCode());
201  }
202  $ui_download = $this->restHelper->getPlugin('download');
203  try {
207  $responseFile = $ui_download->getReport($args['id']);
208  $responseContent = $responseFile->getFile();
209  $newResponse = $response->withHeader('Content-Description',
210  'File Transfer')
211  ->withHeader('Content-Type',
212  $responseFile->headers->get('Content-Type'))
213  ->withHeader('Content-Disposition',
214  $responseFile->headers->get('Content-Disposition'))
215  ->withHeader('Cache-Control', 'must-revalidate')
216  ->withHeader('Pragma', 'private')
217  ->withHeader('Content-Length', filesize($responseContent));
218 
219  readfile($responseContent);
220  return $newResponse;
221  } catch (\Exception $e) {
222  $error = new Info(500, $e->getMessage(), InfoType::ERROR);
223  return $response->withJson($error->getArray(), $error->getCode());
224  }
225  }
226 
233  private function checkReport($id)
234  {
235  $dbManager = $this->dbHelper->getDbManager();
236  $row = $dbManager->getSingleRow(
237  'SELECT jq_type FROM jobqueue WHERE jq_job_fk = $1', array(
238  $id
239  ), "reportValidity");
240  if (! in_array($row['jq_type'], $this->reportsAllowed)) {
241  return new Info(404, "No report scheduled with given job id.",
242  InfoType::ERROR);
243  }
244  $row = $dbManager->getSingleRow('SELECT job_upload_fk FROM job WHERE job_pk = $1',
245  array($id), "reportFileUpload");
246  $uploadId = intval($row['job_upload_fk']);
247  $uploadDao = $this->restHelper->getUploadDao();
248  if (! $uploadDao->isAccessible($uploadId, $this->restHelper->getGroupId())) {
249  return new Info(403, "Report is not accessible.", InfoType::INFO);
250  }
251  $row = $dbManager->getSingleRow('SELECT * FROM reportgen WHERE job_fk = $1',
252  array($id), "reportFileName");
253  if ($row === false) {
254  return new Info(503, "Report is not ready. Retry after 10s.", InfoType::INFO);
255  }
256  // Everything went well
257  return true;
258  }
259 }
Base controller for REST calls.
list_t type structure used to keep various lists. (e.g. there are multiple lists).
Definition: nomos.h:321
Info model to contain general error and return values.
Definition: Info.php:29
static getGroupId()
Get the current user&#39;s group id.
Definition: Auth.php:78