FOSSology  4.4.0
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)) {
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  $folderEdit->Edit($folderId, $newName, $newDesc);
200  $info = new Info(200, "Folder \"$folderName\" updated.", InfoType::INFO);
201  return $response->withJson($info->getArray(), $info->getCode());
202  }
203 
213  public function copyFolder($request, $response, $args)
214  {
215  $folderDao = $this->restHelper->getFolderDao();
216  $folderId = $args['id'];
217  if (ApiVersion::getVersion($request) == ApiVersion::V2) {
218  $queryParams = $request->getQueryParams();
219  $newParent = $queryParams['parent'];
220  $action = strtolower($queryParams['action']);
221  } else {
222  $newParent = $request->getHeaderLine('parent');
223  $action = strtolower($request->getHeaderLine('action'));
224  }
225 
226  if (! is_numeric($newParent) || $newParent < 0) {
227  throw new HttpBadRequestException(
228  "Parent id must be a positive integer!");
229  }
230  if ($folderDao->getFolder($folderId) === null) {
231  throw new HttpNotFoundException("Folder id not found!");
232  }
233  if ($folderDao->getFolder($newParent) === null) {
234  throw new HttpNotFoundException("Parent folder id not found!");
235  }
236  if (! $folderDao->isFolderAccessible($folderId,
237  $this->restHelper->getUserId())) {
238  throw new HttpForbiddenException("Folder is not accessible!");
239  }
240  if (! $folderDao->isFolderAccessible($newParent,
241  $this->restHelper->getUserId())) {
242  throw new HttpForbiddenException("Parent folder is not accessible!");
243  }
244  if (strcmp($action, "copy") != 0 && strcmp($action, "move") != 0) {
245  throw new HttpBadRequestException(
246  "Action can be one of [copy,move]!");
247  }
249  $folderMove = $this->restHelper->getPlugin('content_move');
250  $folderName = FolderGetName($folderId);
251  $parentFolderName = FolderGetName($newParent);
252  $isCopy = (strcmp($action, "copy") == 0);
253  $message = $folderMove->copyContent(
254  [
255  $folderDao->getFolderContentsId($folderId, $folderDao::MODE_FOLDER)
256  ], $newParent, $isCopy);
257  if (!empty($message)) {
258  throw new HttpInternalServerErrorException($message);
259  }
260  $info = new Info(202,
261  "Folder \"$folderName\" $action(ed) under \"$parentFolderName\".",
262  InfoType::INFO);
263  return $response->withJson($info->getArray(), $info->getCode());
264  }
265 
275  public function getUnlinkableFolderContents($request, $response, $args)
276  {
277  $folderId = $args['id'];
278  $folderDao = $this->restHelper->getFolderDao();
279 
280  if ($folderDao->getFolder($folderId) === null) {
281  throw new HttpNotFoundException("Folder id not found!");
282  }
283  if (! $folderDao->isFolderAccessible($folderId, $this->restHelper->getUserId())) {
284  throw new HttpForbiddenException("Folder is not accessible!");
285  }
286 
288  $folderContents = $this->restHelper->getPlugin('foldercontents');
289  $symfonyRequest = new \Symfony\Component\HttpFoundation\Request();
290  $symfonyRequest->request->set('folder', $folderId);
291  $symfonyRequest->request->set('removable', 1);
292  $symfonyRequest->request->set('fromRest', true);
293  $res = $folderContents->handle($symfonyRequest);
294  return $response->withJson($res, 200);
295  }
296 
306  public function unlinkFolder($request, $response, $args)
307  {
308  $folderContentId = $args['contentId'];
309  if (!$this->dbHelper->doesIdExist("foldercontents", "foldercontents_pk", $folderContentId)) {
310  throw new HttpNotFoundException("Folder content id not found!");
311  }
313  $folderDao = $this->container->get('dao.folder');
314  if (!$folderDao->removeContent($folderContentId)) {
315  throw new HttpBadRequestException("Content cannot be unlinked.");
316  }
317  $info = new Info(200, "Folder unlinked successfully.", InfoType::INFO);
318  return $response->withJson($info->getArray(), $info->getCode());
319  }
320 
330  public function getAllFolderContents($request, $response, $args)
331  {
332  $folderId = $args['id'];
333  $folderDao = $this->restHelper->getFolderDao();
334 
335  if ($folderDao->getFolder($folderId) === null) {
336  throw new HttpNotFoundException("Folder id not found!");
337  }
338  if (! $folderDao->isFolderAccessible($folderId, $this->restHelper->getUserId())) {
339  throw new HttpForbiddenException("Folder is not accessible!");
340  }
341 
343  $folderContents = $this->restHelper->getPlugin('foldercontents');
344  $symfonyRequest = new \Symfony\Component\HttpFoundation\Request();
345  $symfonyRequest->request->set('folder', $folderId);
346  $symfonyRequest->request->set('fromRest', true);
347  $contentList = $folderContents->handle($symfonyRequest);
348  $removableContents = $folderDao->getRemovableContents($folderId);
349 
350  foreach ($contentList as &$value) {
351  if (in_array($value['id'], $removableContents)) {
352  $value['removable'] = true;
353  }
354  }
355  return $response->withJson($contentList, 200);
356  }
357 }
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