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 
26 use Psr\Http\Message\ServerRequestInterface;
27 
33 {
34 
44  public function getFolders($request, $response, $args)
45  {
46  $id = null;
47  $allUserFolders = null;
48 
49  $folderDao = $this->restHelper->getFolderDao();
50  if (isset($args['id'])) {
51  $id = intval($args['id']);
52  if (! $folderDao->isFolderAccessible($id)) {
53  throw new HttpForbiddenException("Folder id $id is not accessible");
54  }
55  if ($folderDao->getFolder($id) === null) {
56  throw new HttpNotFoundException("Folder id $id does not exists");
57  }
58  $allUserFolders = [
59  $id
60  ];
61  } else {
62  $rootFolder = $folderDao->getRootFolder($this->restHelper->getUserId())->getId();
63  $allUserFolders = array();
64  GetFolderArray($rootFolder, $allUserFolders);
65  $allUserFolders = array_keys($allUserFolders);
66  }
67  $foldersList = array();
68  foreach ($allUserFolders as $folderId) {
69  $folder = $folderDao->getFolder($folderId);
70  $parentId = $folderDao->getFolderParentId($folderId);
71  $folderModel = new Folder($folder->getId(), $folder->getName(),
72  $folder->getDescription(), $parentId);
73  $foldersList[] = $folderModel->getArray();
74  }
75  if ($id !== null) {
76  $foldersList = $foldersList[0];
77  }
78  return $response->withJson($foldersList, 200);
79  }
80 
90  public function createFolder($request, $response, $args)
91  {
92  $parentFolder = $request->getHeaderLine('parentFolder');
93  $folderName = trim($request->getHeaderLine('folderName'));
94  $folderDescription = trim($request->getHeaderLine('folderDescription'));
95 
96  if (! is_numeric($parentFolder) || $parentFolder < 0) {
97  throw new HttpBadRequestException(
98  "Parent folder id must be a positive integer!");
99  }
100  if (empty($folderName)) {
101  throw new HttpBadRequestException("Folder name can not be empty!");
102  }
103  if (! $this->restHelper->getFolderDao()->isFolderAccessible($parentFolder,
104  $this->restHelper->getUserId())) {
105  throw new HttpForbiddenException("Parent folder is not accessible!");
106  }
108  $folderCreate = $this->restHelper->getPlugin('folder_create');
109  $rc = $folderCreate->create($parentFolder, $folderName, $folderDescription);
110  if ($rc == 4) {
111  $info = new Info(200, "Folder $folderName already exists!", InfoType::INFO);
112  } elseif ($rc == 0) {
113  throw new HttpNotFoundException("Parent folder not found!");
114  } else {
115  $folderId = $this->restHelper->getFolderDao()->getFolderId($folderName, $parentFolder);
116  $info = new Info(201, intval($folderId), InfoType::INFO);
117  }
118  return $response->withJson($info->getArray(), $info->getCode());
119  }
120 
130  public function deleteFolder($request, $response, $args)
131  {
132  $info = null;
133  $folderDao = $this->restHelper->getFolderDao();
134  $folderId = $args['id'];
135 
136  if (! is_numeric($folderId) || $folderId < 0) {
137  throw new HttpBadRequestException(
138  "Folder id must be a positive integer!");
139  }
140  if ($folderDao->getFolder($folderId) === null) {
141  throw new HttpNotFoundException("Folder id not found!");
142  }
144  $folderDelete = $this->restHelper->getPlugin('admin_folder_delete');
145  $folderName = FolderGetName($folderId);
146  $folderArray = Folder2Path($folderId);
147  $folderParent = intval($folderArray[count($folderArray) - 2]['folder_pk']);
148  $folderId = "$folderParent $folderId";
149 
150  $rc = $folderDelete->Delete($folderId, $this->restHelper->getUserId());
151  if ($rc == "No access to delete this folder") {
152  throw new HttpForbiddenException($rc);
153  } elseif ($rc !== null) {
154  throw new HttpInternalServerErrorException($rc);
155  }
156  $info = new Info(202, "Folder, \"$folderName\" deleted.", InfoType::INFO);
157  return $response->withJson($info->getArray(), $info->getCode());
158  }
159 
169  public function editFolder($request, $response, $args)
170  {
171  $folderDao = $this->restHelper->getFolderDao();
172  $folderId = $args['id'];
173  $newName = $request->getHeaderLine('name');
174  $newDesc = $request->getHeaderLine('description');
175 
176  if ($folderDao->getFolder($folderId) === null) {
177  throw new HttpNotFoundException("Folder id not found!");
178  }
179  if (! $folderDao->isFolderAccessible($folderId, $this->restHelper->getUserId())) {
180  throw new HttpForbiddenException("Folder is not accessible!");
181  }
183  $folderEdit = $this->restHelper->getPlugin('folder_properties');
184  $folderName = FolderGetName($folderId);
185  $folderEdit->Edit($folderId, $newName, $newDesc);
186  $info = new Info(200, "Folder \"$folderName\" updated.", InfoType::INFO);
187  return $response->withJson($info->getArray(), $info->getCode());
188  }
189 
199  public function copyFolder($request, $response, $args)
200  {
201  $folderDao = $this->restHelper->getFolderDao();
202  $folderId = $args['id'];
203  $newParent = $request->getHeaderLine('parent');
204  $action = strtolower($request->getHeaderLine('action'));
205 
206  if (! is_numeric($newParent) || $newParent < 0) {
207  throw new HttpBadRequestException(
208  "Parent id must be a positive integer!");
209  }
210  if ($folderDao->getFolder($folderId) === null) {
211  throw new HttpNotFoundException("Folder id not found!");
212  }
213  if ($folderDao->getFolder($newParent) === null) {
214  throw new HttpNotFoundException("Parent folder id not found!");
215  }
216  if (! $folderDao->isFolderAccessible($folderId,
217  $this->restHelper->getUserId())) {
218  throw new HttpForbiddenException("Folder is not accessible!");
219  }
220  if (! $folderDao->isFolderAccessible($newParent,
221  $this->restHelper->getUserId())) {
222  throw new HttpForbiddenException("Parent folder is not accessible!");
223  }
224  if (strcmp($action, "copy") != 0 && strcmp($action, "move") != 0) {
225  throw new HttpBadRequestException(
226  "Action can be one of [copy,move]!");
227  }
229  $folderMove = $this->restHelper->getPlugin('content_move');
230  $folderName = FolderGetName($folderId);
231  $parentFolderName = FolderGetName($newParent);
232  $isCopy = (strcmp($action, "copy") == 0);
233  $message = $folderMove->copyContent(
234  [
235  $folderDao->getFolderContentsId($folderId, $folderDao::MODE_FOLDER)
236  ], $newParent, $isCopy);
237  if (!empty($message)) {
238  throw new HttpInternalServerErrorException($message);
239  }
240  $info = new Info(202,
241  "Folder \"$folderName\" $action(ed) under \"$parentFolderName\".",
242  InfoType::INFO);
243  return $response->withJson($info->getArray(), $info->getCode());
244  }
245 
255  public function getUnlinkableFolderContents($request, $response, $args)
256  {
257  $folderId = $args['id'];
258  $folderDao = $this->restHelper->getFolderDao();
259 
260  if ($folderDao->getFolder($folderId) === null) {
261  throw new HttpNotFoundException("Folder id not found!");
262  }
263  if (! $folderDao->isFolderAccessible($folderId, $this->restHelper->getUserId())) {
264  throw new HttpForbiddenException("Folder is not accessible!");
265  }
266 
268  $folderContents = $this->restHelper->getPlugin('foldercontents');
269  $symfonyRequest = new \Symfony\Component\HttpFoundation\Request();
270  $symfonyRequest->request->set('folder', $folderId);
271  $symfonyRequest->request->set('removable', 1);
272  $symfonyRequest->request->set('fromRest', true);
273  $res = $folderContents->handle($symfonyRequest);
274  return $response->withJson($res, 200);
275  }
276 
286  public function unlinkFolder($request, $response, $args)
287  {
288  $folderContentId = $args['contentId'];
289  if (!$this->dbHelper->doesIdExist("foldercontents", "foldercontents_pk", $folderContentId)) {
290  throw new HttpNotFoundException("Folder content id not found!");
291  }
293  $folderDao = $this->container->get('dao.folder');
294  if (!$folderDao->removeContent($folderContentId)) {
295  throw new HttpBadRequestException("Content cannot be unlinked.");
296  }
297  $info = new Info(200, "Folder unlinked successfully.", InfoType::INFO);
298  return $response->withJson($info->getArray(), $info->getCode());
299  }
300 
310  public function getAllFolderContents($request, $response, $args)
311  {
312  $folderId = $args['id'];
313  $folderDao = $this->restHelper->getFolderDao();
314 
315  if ($folderDao->getFolder($folderId) === null) {
316  throw new HttpNotFoundException("Folder id not found!");
317  }
318  if (! $folderDao->isFolderAccessible($folderId, $this->restHelper->getUserId())) {
319  throw new HttpForbiddenException("Folder is not accessible!");
320  }
321 
323  $folderContents = $this->restHelper->getPlugin('foldercontents');
324  $symfonyRequest = new \Symfony\Component\HttpFoundation\Request();
325  $symfonyRequest->request->set('folder', $folderId);
326  $symfonyRequest->request->set('fromRest', true);
327  $contentList = $folderContents->handle($symfonyRequest);
328  $removableContents = $folderDao->getRemovableContents($folderId);
329 
330  foreach ($contentList as &$value) {
331  if (in_array($value['id'], $removableContents)) {
332  $value['removable'] = true;
333  }
334  }
335  return $response->withJson($contentList, 200);
336  }
337 }
Base controller for REST calls.
Override Slim response for withJson function.
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:972
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:690