FOSSology  4.7.1
Open Source License Compliance by Open Source Software
FolderController.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2018 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 
27 use Psr\Http\Message\ServerRequestInterface;
28 
34 {
35 
45  public function getFolders($request, $response, $args)
46  {
47  $id = null;
48  $allUserFolders = null;
49 
50  $folderDao = $this->restHelper->getFolderDao();
51  if (isset($args['id'])) {
52  $id = intval($args['id']);
53  if (! $folderDao->isFolderAccessible($id, $this->restHelper->getUserId())) {
54  throw new HttpForbiddenException("Folder id $id is not accessible");
55  }
56  if ($folderDao->getFolder($id) === null) {
57  throw new HttpNotFoundException("Folder id $id does not exists");
58  }
59  $allUserFolders = [
60  $id
61  ];
62  } else {
63  $rootFolder = $folderDao->getRootFolder($this->restHelper->getUserId())->getId();
64  $allUserFolders = array();
65  GetFolderArray($rootFolder, $allUserFolders);
66  $allUserFolders = array_keys($allUserFolders);
67  }
68  $foldersList = array();
69  foreach ($allUserFolders as $folderId) {
70  $folder = $folderDao->getFolder($folderId);
71  $parentId = $folderDao->getFolderParentId($folderId);
72  $folderModel = new Folder($folder->getId(), $folder->getName(),
73  $folder->getDescription(), $parentId);
74  $foldersList[] = $folderModel->getArray();
75  }
76  if ($id !== null) {
77  $foldersList = $foldersList[0];
78  }
79  return $response->withJson($foldersList, 200);
80  }
81 
91  public function createFolder($request, $response, $args)
92  {
93  if (ApiVersion::getVersion($request) == ApiVersion::V2) {
94  $queryParams = $request->getQueryParams();
95  $parentFolder = $queryParams['parentFolder'] ?? '';
96  $folderName = trim($queryParams['folderName'] ?? '');
97  $folderDescription = trim($queryParams['folderDescription'] ?? '');
98  } else {
99  $parentFolder = $request->getHeaderLine('parentFolder');
100  $folderName = trim($request->getHeaderLine('folderName'));
101  $folderDescription = trim($request->getHeaderLine('folderDescription'));
102  }
103 
104  if (! is_numeric($parentFolder) || $parentFolder < 0) {
105  throw new HttpBadRequestException(
106  "Parent folder id must be a positive integer!");
107  }
108  if (empty($folderName)) {
109  throw new HttpBadRequestException("Folder name can not be empty!");
110  }
111  if (! $this->restHelper->getFolderDao()->isFolderAccessible($parentFolder,
112  $this->restHelper->getUserId())) {
113  throw new HttpForbiddenException("Parent folder is not accessible!");
114  }
116  $folderCreate = $this->restHelper->getPlugin('folder_create');
117  $rc = $folderCreate->create($parentFolder, $folderName, $folderDescription);
118  if ($rc == 4) {
119  $info = new Info(200, "Folder $folderName already exists!", InfoType::INFO);
120  } elseif ($rc == 0) {
121  throw new HttpNotFoundException("Parent folder not found!");
122  } else {
123  $folderId = $this->restHelper->getFolderDao()->getFolderId($folderName, $parentFolder);
124  $info = new Info(201, intval($folderId), InfoType::INFO);
125  }
126  return $response->withJson($info->getArray(), $info->getCode());
127  }
128 
138  public function deleteFolder($request, $response, $args)
139  {
140  $info = null;
141  $folderDao = $this->restHelper->getFolderDao();
142  $folderId = $args['id'];
143 
144  if (! is_numeric($folderId) || $folderId < 0) {
145  throw new HttpBadRequestException(
146  "Folder id must be a positive integer!");
147  }
148  if ($folderDao->getFolder($folderId) === null) {
149  throw new HttpNotFoundException("Folder id not found!");
150  }
152  $folderDelete = $this->restHelper->getPlugin('admin_folder_delete');
153  $folderName = FolderGetName($folderId);
154  $folderArray = Folder2Path($folderId);
155  $folderParent = intval($folderArray[count($folderArray) - 2]['folder_pk']);
156  $folderId = "$folderParent $folderId";
157 
158  $rc = $folderDelete->Delete($folderId, $this->restHelper->getUserId());
159  if ($rc == "No access to delete this folder") {
160  throw new HttpForbiddenException($rc);
161  } elseif ($rc !== null) {
162  throw new HttpInternalServerErrorException($rc);
163  }
164  $info = new Info(202, "Folder, \"$folderName\" deleted.", InfoType::INFO);
165  return $response->withJson($info->getArray(), $info->getCode());
166  }
167 
177  public function editFolder($request, $response, $args)
178  {
179  $folderDao = $this->restHelper->getFolderDao();
180  $folderId = $args['id'];
181  if (ApiVersion::getVersion($request) == ApiVersion::V2) {
182  $queryParams = $request->getQueryParams();
183  $newName = $queryParams['name'] ?? '';
184  $newDesc = $queryParams['description'] ?? '';
185  } else {
186  $newName = $request->getHeaderLine('name');
187  $newDesc = $request->getHeaderLine('description');
188  }
189 
190  if ($folderDao->getFolder($folderId) === null) {
191  throw new HttpNotFoundException("Folder id not found!");
192  }
193  if (! $folderDao->isFolderAccessible($folderId, $this->restHelper->getUserId())) {
194  throw new HttpForbiddenException("Folder is not accessible!");
195  }
197  $folderEdit = $this->restHelper->getPlugin('folder_properties');
198  $folderName = FolderGetName($folderId);
199  $rc = $folderEdit->Edit($folderId, $newName, $newDesc);
200  if ($rc == 4) {
201  $newName = trim($newName);
202  $info = new Info(200, "Folder $newName already exists!", InfoType::INFO);
203  } else {
204  $info = new Info(200, "Folder \"$folderName\" updated.", InfoType::INFO);
205  }
206  return $response->withJson($info->getArray(), $info->getCode());
207  }
208 
218  public function copyFolder($request, $response, $args)
219  {
220  $folderDao = $this->restHelper->getFolderDao();
221  $folderId = $args['id'];
222  if (ApiVersion::getVersion($request) == ApiVersion::V2) {
223  $queryParams = $request->getQueryParams();
224  $newParent = $queryParams['parent'] ?? '';
225  $action = strtolower($queryParams['action'] ?? '');
226  } else {
227  $newParent = $request->getHeaderLine('parent');
228  $action = strtolower($request->getHeaderLine('action'));
229  }
230 
231  if (! is_numeric($newParent) || $newParent < 0) {
232  throw new HttpBadRequestException(
233  "Parent id must be a positive integer!");
234  }
235  if ($folderDao->getFolder($folderId) === null) {
236  throw new HttpNotFoundException("Folder id not found!");
237  }
238  if ($folderDao->getFolder($newParent) === null) {
239  throw new HttpNotFoundException("Parent folder id not found!");
240  }
241  if (! $folderDao->isFolderAccessible($folderId,
242  $this->restHelper->getUserId())) {
243  throw new HttpForbiddenException("Folder is not accessible!");
244  }
245  if (! $folderDao->isFolderAccessible($newParent,
246  $this->restHelper->getUserId())) {
247  throw new HttpForbiddenException("Parent folder is not accessible!");
248  }
249  if (strcmp($action, "copy") != 0 && strcmp($action, "move") != 0) {
250  throw new HttpBadRequestException(
251  "Action can be one of [copy,move]!");
252  }
254  $folderMove = $this->restHelper->getPlugin('content_move');
255  $folderName = FolderGetName($folderId);
256  $parentFolderName = FolderGetName($newParent);
257  $isCopy = (strcmp($action, "copy") == 0);
258  $message = $folderMove->copyContent(
259  [
260  $folderDao->getFolderContentsId($folderId, $folderDao::MODE_FOLDER)
261  ], $newParent, $isCopy);
262  if (!empty($message)) {
263  throw new HttpInternalServerErrorException($message);
264  }
265  $info = new Info(202,
266  "Folder \"$folderName\" $action(ed) under \"$parentFolderName\".",
267  InfoType::INFO);
268  return $response->withJson($info->getArray(), $info->getCode());
269  }
270 
280  public function getUnlinkableFolderContents($request, $response, $args)
281  {
282  $folderId = $args['id'];
283  $folderDao = $this->restHelper->getFolderDao();
284 
285  if ($folderDao->getFolder($folderId) === null) {
286  throw new HttpNotFoundException("Folder id not found!");
287  }
288  if (! $folderDao->isFolderAccessible($folderId, $this->restHelper->getUserId())) {
289  throw new HttpForbiddenException("Folder is not accessible!");
290  }
291 
293  $folderContents = $this->restHelper->getPlugin('foldercontents');
294  $symfonyRequest = new \Symfony\Component\HttpFoundation\Request();
295  $symfonyRequest->request->set('folder', $folderId);
296  $symfonyRequest->request->set('removable', 1);
297  $symfonyRequest->request->set('fromRest', true);
298  $res = $folderContents->handle($symfonyRequest);
299  return $response->withJson($res, 200);
300  }
301 
311  public function unlinkFolder($request, $response, $args)
312  {
313  $folderContentId = $args['contentId'];
314  if (!$this->dbHelper->doesIdExist("foldercontents", "foldercontents_pk", $folderContentId)) {
315  throw new HttpNotFoundException("Folder content id not found!");
316  }
318  $folderDao = $this->restHelper->getFolderDao();
319  $content = $folderDao->getContent($folderContentId);
320  if (! $folderDao->isFolderAccessible($content['parent_fk'], $this->restHelper->getUserId())) {
321  throw new HttpForbiddenException("Folder is not accessible!");
322  }
323  if (!$folderDao->removeContent($folderContentId)) {
324  throw new HttpBadRequestException("Content cannot be unlinked.");
325  }
326  $info = new Info(200, "Folder unlinked successfully.", InfoType::INFO);
327  return $response->withJson($info->getArray(), $info->getCode());
328  }
329 
339  public function getAllFolderContents($request, $response, $args)
340  {
341  $folderId = $args['id'];
342  $folderDao = $this->restHelper->getFolderDao();
343 
344  if ($folderDao->getFolder($folderId) === null) {
345  throw new HttpNotFoundException("Folder id not found!");
346  }
347  if (! $folderDao->isFolderAccessible($folderId, $this->restHelper->getUserId())) {
348  throw new HttpForbiddenException("Folder is not accessible!");
349  }
350 
352  $folderContents = $this->restHelper->getPlugin('foldercontents');
353  $symfonyRequest = new \Symfony\Component\HttpFoundation\Request();
354  $symfonyRequest->request->set('folder', $folderId);
355  $symfonyRequest->request->set('fromRest', true);
356  $contentList = $folderContents->handle($symfonyRequest);
357  $removableContents = $folderDao->getRemovableContents($folderId);
358 
359  foreach ($contentList as &$value) {
360  if (in_array($value['id'], $removableContents)) {
361  $value['removable'] = true;
362  }
363  }
364  return $response->withJson($contentList, 200);
365  }
366 }
Base controller for REST calls.
Override Slim response for withJson function.
static getVersion(ServerRequestInterface $request)
Definition: ApiVersion.php:29
Different type of infos provided by REST.
Definition: InfoType.php:16
Info model to contain general error and return values.
Definition: Info.php:19
FolderGetName($FolderPk, $Top=-1)
Given a folder_pk, return the full path to this folder.
GetFolderArray($RootFolder, &$FolderArray)
Get an array of all the folders from a $RootFolder on down.
Folder2Path($folder_pk)
Return an array of folder_pk, folder_name from the users.root_folder_fk to $folder_pk.
int deleteFolder(long cFolder, long pFolder, int userId, int userPerm)
recursively delete a folder
Definition: util.c:1018
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:690