FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
FolderController.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 
48  public function getFolders($request, $response, $args)
49  {
50  $id = null;
51  $allUserFolders = null;
52 
53  $folderDao = $this->restHelper->getFolderDao();
54  if (isset($args['id'])) {
55  $id = intval($args['id']);
56  $returnVal = null;
57  if (! $folderDao->isFolderAccessible($id)) {
58  $returnVal = new Info(403, "Folder id $id is not accessible",
59  InfoType::ERROR);
60  }
61  if ($folderDao->getFolder($id) === null) {
62  $returnVal = new Info(404, "Folder id $id does not exists",
63  InfoType::ERROR);
64  }
65  if ($returnVal !== null) {
66  return $response->withJson($returnVal->getArray(), $returnVal->getCode());
67  }
68  $allUserFolders = [
69  $id
70  ];
71  } else {
72  $rootFolder = $folderDao->getRootFolder($this->restHelper->getUserId())->getId();
73  $allUserFolders = array();
74  GetFolderArray($rootFolder, $allUserFolders);
75  $allUserFolders = array_keys($allUserFolders);
76  }
77  $foldersList = array();
78  foreach ($allUserFolders as $folderId) {
79  $folder = $folderDao->getFolder($folderId);
80  $parentId = $folderDao->getFolderParentId($folderId);
81  $folderModel = new Folder($folder->getId(), $folder->getName(),
82  $folder->getDescription(), $parentId);
83  $foldersList[] = $folderModel->getArray();
84  }
85  if ($id !== null) {
86  $foldersList = $foldersList[0];
87  }
88  return $response->withJson($foldersList, 200);
89  }
90 
99  public function createFolder($request, $response, $args)
100  {
101  $info = null;
102  $parentFolder = $request->getHeaderLine('parentFolder');
103  $folderName = trim($request->getHeaderLine('folderName'));
104  $folderDescription = trim($request->getHeaderLine('folderDescription'));
105 
106  if (! is_numeric($parentFolder) || $parentFolder < 0) {
107  $info = new Info(400, "Parent folder id must be a positive integer!",
108  InfoType::ERROR);
109  }
110  if (empty($folderName)) {
111  $info = new Info(400, "Folder name can not be empty!", InfoType::ERROR);
112  }
113  if (! $this->restHelper->getFolderDao()->isFolderAccessible($parentFolder,
114  $this->restHelper->getUserId())) {
115  $info = new Info(403, "Parent folder can not be accessed!", InfoType::ERROR);
116  }
117  if ($info !== null) {
118  return $response->withJson($info->getArray(), $info->getCode());
119  }
120  $folderCreate = $this->restHelper->getPlugin('folder_create');
121  $rc = $folderCreate->create($parentFolder, $folderName, $folderDescription);
122  if ($rc == 4) {
123  $info = new Info(200, "Folder $folderName already exists!", InfoType::INFO);
124  } elseif ($rc == 0) {
125  $info = new Info(404, "Parent folder not found!", InfoType::ERROR);
126  } else {
127  $folderId = $this->restHelper->getFolderDao()->getFolderId($folderName, $parentFolder);
128  $info = new Info(201, intval($folderId), InfoType::INFO);
129  }
130  return $response->withJson($info->getArray(), $info->getCode());
131  }
132 
141  public function deleteFolder($request, $response, $args)
142  {
143  $info = null;
144  $folderDao = $this->restHelper->getFolderDao();
145  $folderId = $args['id'];
146 
147  if (! is_numeric($folderId) || $folderId < 0) {
148  $info = new Info(400, "Folder id must be a positive integer!",
149  InfoType::ERROR);
150  } elseif ($folderDao->getFolder($folderId) === null) {
151  $info = new Info(404, "Folder id not found!", InfoType::ERROR);
152  } else {
153  $folderDelete = $this->restHelper->getPlugin('admin_folder_delete');
154  $folderName = FolderGetName($folderId);
155  $folderArray = Folder2Path($folderId);
156  $folderParent = intval($folderArray[count($folderArray) - 2]['folder_pk']);
157  $folderId = "$folderParent $folderId";
158 
159  $rc = $folderDelete->Delete($folderId, Auth::getUserId());
160  if ($rc == "No access to delete this folder") {
161  $info = new Info(403, $rc, InfoType::ERROR);
162  } elseif ($rc !== null) {
163  $info = new Info(500, $rc, InfoType::ERROR);
164  } else {
165  $info = new Info(202, "Folder, \"$folderName\" deleted.", InfoType::INFO);
166  }
167  }
168  return $response->withJson($info->getArray(), $info->getCode());
169  }
170 
179  public function editFolder($request, $response, $args)
180  {
181  $info = null;
182  $folderDao = $this->restHelper->getFolderDao();
183  $folderId = $args['id'];
184  $newName = $request->getHeaderLine('name');
185  $newDesc = $request->getHeaderLine('description');
186 
187  if ($folderDao->getFolder($folderId) === null) {
188  $info = new Info(404, "Folder id not found!", InfoType::ERROR);
189  } elseif (! $folderDao->isFolderAccessible($folderId, $this->restHelper->getUserId())) {
190  $info = new Info(403, "Folder is not accessible!", InfoType::ERROR);
191  } else {
192  $folderEdit = $this->restHelper->getPlugin('folder_properties');
193  $folderName = FolderGetName($folderId);
194  $folderEdit->Edit($folderId, $newName, $newDesc);
195  $info = new Info(200, "Folder \"$folderName\" updated.", InfoType::INFO);
196  }
197  return $response->withJson($info->getArray(), $info->getCode());
198  }
199 
208  public function copyFolder($request, $response, $args)
209  {
210  $info = null;
211  $folderDao = $this->restHelper->getFolderDao();
212  $folderId = $args['id'];
213  $newParent = $request->getHeaderLine('parent');
214  $action = strtolower($request->getHeaderLine('action'));
215 
216  if (! is_numeric($newParent) || $newParent < 0) {
217  $info = new Info(400, "Parent id must be a positive integer!",
218  InfoType::ERROR);
219  } elseif ($folderDao->getFolder($folderId) === null) {
220  $info = new Info(404, "Folder id not found!", InfoType::ERROR);
221  } elseif ($folderDao->getFolder($newParent) === null) {
222  $info = new Info(404, "Parent folder not found!", InfoType::ERROR);
223  } elseif (! $folderDao->isFolderAccessible($folderId,
224  $this->restHelper->getUserId())) {
225  $info = new Info(403, "Folder is not accessible!", InfoType::ERROR);
226  } elseif (! $folderDao->isFolderAccessible($newParent,
227  $this->restHelper->getUserId())) {
228  $info = new Info(403, "Parent folder is not accessible!", InfoType::ERROR);
229  } elseif (strcmp($action, "copy") != 0 && strcmp($action, "move") != 0) {
230  $info = new Info(400, "Action can be one of [copy,move]!", InfoType::ERROR);
231  } else {
232  $folderMove = $this->restHelper->getPlugin('content_move');
233  $folderName = FolderGetName($folderId);
234  $parentFolderName = FolderGetName($newParent);
235  $isCopy = (strcmp($action, "copy") == 0);
236  $message = $folderMove->copyContent(
237  [
238  $folderDao->getFolderContentsId($folderId)
239  ], $newParent, $isCopy);
240  if (empty($message)) {
241  $info = new Info(202,
242  "Folder \"$folderName\" $action(ed) under \"$parentFolderName\".",
243  InfoType::INFO);
244  } else {
245  $info = new Info(500, $message, InfoType::ERROR);
246  }
247  }
248  return $response->withJson($info->getArray(), $info->getCode());
249  }
250 }
static getUserId()
Get the current user&#39;s id.
Definition: Auth.php:69
Base controller for REST calls.
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.
Info model to contain general error and return values.
Definition: Info.php:29
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:695