FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
UploadController.php
Go to the documentation of this file.
1 <?php
2 /***************************************************************
3  Copyright (C) 2018,2020 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 
37 
43 {
44 
48  const AGENT_PARAM = "agent";
49 
53  const CONTAINER_PARAM = "containers";
54 
55  public function __construct($container)
56  {
57  parent::__construct($container);
58  $groupId = $this->restHelper->getGroupId();
59  $dbManager = $this->dbHelper->getDbManager();
60  $uploadBrowseProxy = new UploadBrowseProxy($groupId, 0, $dbManager, false);
61  $uploadBrowseProxy->sanity();
62  }
63 
72  public function getUploads($request, $response, $args)
73  {
74  $id = null;
75  if (isset($args['id'])) {
76  $id = intval($args['id']);
77  $upload = $this->uploadAccessible($this->restHelper->getGroupId(), $id);
78  if ($upload !== true) {
79  return $response->withJson($upload->getArray(), $upload->getCode());
80  }
81  $temp = $this->isAdj2nestDone($id, $response);
82  if ($temp !== true) {
83  return $temp;
84  }
85  }
86  $uploads = $this->dbHelper->getUploads($this->restHelper->getUserId(), $id);
87  if ($id !== null) {
88  $uploads = $uploads[0];
89  }
90  return $response->withJson($uploads, 200);
91  }
92 
101  public function getUploadSummary($request, $response, $args)
102  {
103  $id = intval($args['id']);
104  $upload = $this->uploadAccessible($this->restHelper->getGroupId(), $id);
105  if ($upload !== true) {
106  return $response->withJson($upload->getArray(), $upload->getCode());
107  }
108  $temp = $this->isAdj2nestDone($id, $response);
109  if ($temp !== true) {
110  return $temp;
111  }
112  $uploadHelper = new UploadHelper();
113  $uploadSummary = $uploadHelper->generateUploadSummary($id,
114  $this->restHelper->getGroupId());
115  return $response->withJson($uploadSummary->getArray(), 200);
116  }
117 
126  public function deleteUpload($request, $response, $args)
127  {
128  require_once dirname(dirname(dirname(dirname(__DIR__)))) .
129  "/delagent/ui/delete-helper.php";
130  $returnVal = null;
131  $id = intval($args['id']);
132 
133  $upload = $this->uploadAccessible($this->restHelper->getGroupId(), $id);
134  if ($upload !== true) {
135  return $response->withJson($upload->getArray(), $upload->getCode());
136  }
137  $result = TryToDelete($id, $this->restHelper->getUserId(),
138  $this->restHelper->getGroupId(), $this->restHelper->getUploadDao());
139  if ($result->getDeleteMessageCode() !== DeleteMessages::SUCCESS) {
140  $returnVal = new Info(500, $result->getDeleteMessageString(),
141  InfoType::ERROR);
142  } else {
143  $returnVal = new Info(202, "Delete Job for file with id " . $id,
144  InfoType::INFO);
145  }
146  return $response->withJson($returnVal->getArray(), $returnVal->getCode());
147  }
148 
157  public function copyUpload($request, $response, $args)
158  {
159  return $this->changeUpload($request, $response, $args, true);
160  }
161 
170  public function moveUpload($request, $response, $args)
171  {
172  return $this->changeUpload($request, $response, $args, false);
173  }
174 
184  private function changeUpload($request, $response, $args, $isCopy)
185  {
186  $returnVal = null;
187  if ($request->hasHeader('folderId') &&
188  is_numeric($newFolderID = $request->getHeaderLine('folderId'))) {
189  $id = intval($args['id']);
190  $returnVal = $this->restHelper->copyUpload($id, $newFolderID, $isCopy);
191  } else {
192  $returnVal = new Info(400, "folderId header should be an integer!",
193  InfoType::ERROR);
194  }
195  return $response->withJson($returnVal->getArray(), $returnVal->getCode());
196  }
197 
206  public function postUpload($request, $response, $args)
207  {
208  $uploadHelper = new UploadHelper();
209  if ($request->hasHeader('folderId') &&
210  is_numeric($folderId = $request->getHeaderLine('folderId')) && $folderId > 0) {
211 
212  $allFolderIds = $this->restHelper->getFolderDao()->getAllFolderIds();
213  if (!in_array($folderId, $allFolderIds)) {
214  $error = new Info(404, "folderId $folderId does not exists!", InfoType::ERROR);
215  return $response->withJson($error->getArray(), $error->getCode());
216  }
217  if (!$this->restHelper->getFolderDao()->isFolderAccessible($folderId)) {
218  $error = new Info(403, "folderId $folderId is not accessible!",
219  InfoType::ERROR);
220  return $response->withJson($error->getArray(), $error->getCode());
221  }
222 
223  $description = $request->getHeaderLine('uploadDescription');
224  $public = $request->getHeaderLine('public');
225  $public = empty($public) ? 'protected' : $public;
226  $ignoreScm = $request->getHeaderLine('ignoreScm');
227  $uploadType = $request->getHeaderLine('uploadType');
228  if (empty($uploadType)) {
229  $uploadType = "vcs";
230  }
231  $uploadResponse = $uploadHelper->createNewUpload($request, $folderId,
232  $description, $public, $ignoreScm, $uploadType);
233  $status = $uploadResponse[0];
234  $message = $uploadResponse[1];
235  $statusDescription = $uploadResponse[2];
236  if (! $status) {
237  $info = new Info(500, $message . "\n" . $statusDescription,
238  InfoType::ERROR);
239  } else {
240  $uploadId = $uploadResponse[3];
241  $info = new Info(201, intval($uploadId), InfoType::INFO);
242  }
243  return $response->withJson($info->getArray(), $info->getCode());
244  } else {
245  $error = new Info(400, "folderId must be a positive integer!",
246  InfoType::ERROR);
247  return $response->withJson($error->getArray(), $error->getCode());
248  }
249  }
250 
259  public function getUploadLicenses($request, $response, $args)
260  {
261  $id = intval($args['id']);
262  $query = $request->getQueryParams();
263 
264  if (! array_key_exists(self::AGENT_PARAM, $query)) {
265  $error = new Info(400, "'agent' parameter missing from query.",
266  InfoType::ERROR);
267  return $response->withJson($error->getArray(), $error->getCode());
268  }
269  $agents = explode(",", $query[self::AGENT_PARAM]);
270  $containers = true;
271  if (array_key_exists(self::CONTAINER_PARAM, $query)) {
272  $containers = (strcasecmp($query[self::CONTAINER_PARAM], "true") === 0);
273  }
274 
275  $upload = $this->uploadAccessible($this->restHelper->getGroupId(), $id);
276  if ($upload !== true) {
277  return $response->withJson($upload->getArray(), $upload->getCode());
278  }
279  $adj2nest = $this->isAdj2nestDone($id, $response);
280  $agentScheduled = $this->areAgentsScheduled($id, $agents, $response);
281  if ($adj2nest !== true) {
282  return $adj2nest;
283  } else if ($agentScheduled !== true) {
284  return $agentScheduled;
285  }
286 
287  $uploadHelper = new UploadHelper();
288  $licenseList = $uploadHelper->getUploadLicenseList($id, $agents,
289  $containers);
290  return $response->withJson($licenseList, 200);
291  }
292 
300  private function uploadAccessible($groupId, $id)
301  {
302  if (! $this->dbHelper->doesIdExist("upload", "upload_pk", $id)) {
303  return new Info(404, "Upload does not exist", InfoType::ERROR);
304  } else if (! $this->restHelper->getUploadDao()->isAccessible($id, $groupId)) {
305  return new Info(403, "Upload is not accessible", InfoType::ERROR);
306  }
307  return true;
308  }
309 
316  private function isAdj2nestDone($id, $response)
317  {
318  $itemTreeBounds = $this->restHelper->getUploadDao()->getParentItemBounds(
319  $id);
320  if ($itemTreeBounds === false || empty($itemTreeBounds->getLeft())) {
321  $returnVal = new Info(503,
322  "Ununpack job not started. Please check job status at " .
323  "/api/v1/jobs?upload=" . $id, InfoType::INFO);
324  return $response->withHeader('Retry-After', '60')
325  ->withHeader('Look-at', "/api/v1/jobs?upload=" . $id)
326  ->withJson($returnVal->getArray(), $returnVal->getCode());
327  }
328  return true;
329  }
330 
339  private function areAgentsScheduled($uploadId, $agents, $response)
340  {
341  global $container;
342  $agentDao = $container->get('dao.agent');
343 
344  $agentList = array_keys(AgentRef::AGENT_LIST);
345  $intersectArray = array_intersect($agents, $agentList);
346 
347  $error = null;
348  if (count($agents) != count($intersectArray)) {
349  $error = new Info(400, "Agent should be any of " .
350  implode(", ", $agentList) . ". " . implode(",", $agents) . " passed.",
351  InfoType::ERROR);
352  } else {
353  // Agent is valid, check if they have ars tables.
354  foreach ($agents as $agent) {
355  if (! $agentDao->arsTableExists($agent)) {
356  $error = new Info(412, "Agent $agent not scheduled for the upload. " .
357  "Please POST to /jobs", InfoType::ERROR);
358  break;
359  }
360  }
361  }
362  if ($error !== null) {
363  return $response->withJson($error->getArray(), $error->getCode());
364  }
365 
366  $scanProx = new ScanJobProxy($agentDao, $uploadId);
367  $agentList = $scanProx->createAgentStatus($agents);
368 
369  foreach ($agentList as $agent) {
370  if (! array_key_exists('currentAgentId', $agent)) {
371  $error = new Info(412, "Agent " . $agent["agentName"] .
372  " not scheduled for the upload. Please POST to /jobs",
373  InfoType::ERROR);
374  $response = $response->withJson($error->getArray(), $error->getCode());
375  } else if ($agent['isAgentRunning']) {
376  $error = new Info(503, "Agent $agent is running. " .
377  "Please check job status at /api/v1/jobs?upload=" . $uploadId,
378  InfoType::INFO);
379  $response = $response->withHeader('Retry-After', '60')
380  ->withHeader('Look-at', "/api/v1/jobs?upload=" . $uploadId)
381  ->withJson($error->getArray(), $error->getCode());
382  }
383  if ($error !== null) {
384  return $response;
385  }
386  }
387  return true;
388  }
389 }
changeUpload($request, $response, $args, $isCopy)
Base controller for REST calls.
Handle new file uploads from Slim framework and move to FOSSology.
areAgentsScheduled($uploadId, $agents, $response)
Info model to contain general error and return values.
Definition: Info.php:29