FOSSology  4.4.0
Open Source License Compliance by Open Source Software
AdminUploadDelete.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2008-2013 Hewlett-Packard Development Company, L.P.
4  SPDX-FileCopyrightText: © 2015-2017 Siemens AG
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
13 
18 use Symfony\Component\HttpFoundation\Request;
19 use Symfony\Component\HttpFoundation\Response;
22 
28 {
29  const NAME = "admin_upload_delete";
30 
32  private $uploadDao;
33 
35  private $folderDao;
36 
37  function __construct()
38  {
39  parent::__construct(self::NAME, array(
40  self::TITLE => _("Delete Uploaded File"),
41  self::MENU_LIST => "Organize::Uploads::Delete Uploaded File",
42  self::PERMISSION => Auth::PERM_ADMIN,
43  self::REQUIRES_LOGIN => true
44  ));
45 
46  global $container;
47  $this->uploadDao = $container->get('dao.upload');
48  $this->folderDao = $container->get('dao.folder');
49  }
50 
51 
57  private function delete($uploadpk)
58  {
59  /* Prepare the job: job "Delete" */
60  $user_pk = Auth::getUserId();
61  $group_pk = Auth::getGroupId();
62  $jobpk = JobAddJob($user_pk, $group_pk, "Delete", $uploadpk);
63  if (empty($jobpk) || ($jobpk < 0)) {
64  return _("Failed to create job record");
65  }
66  /* Add job: job "Delete" has jobqueue item "delagent" */
67  $jqargs = "DELETE UPLOAD $uploadpk";
68  $jobqueuepk = JobQueueAdd($jobpk, "delagent", $jqargs, null, null);
69  if (empty($jobqueuepk)) {
70  return _("Failed to place delete in job queue");
71  }
72 
73  /* Tell the scheduler to check the queue. */
74  $success = fo_communicate_with_scheduler("database", $output, $error_msg);
75  if (!$success) {
76  $error_msg = _("Is the scheduler running? Your jobs have been added to job queue.");
77  $URL = Traceback_uri() . "?mod=showjobs&upload=$uploadpk ";
78  $LinkText = _("View Jobs");
79  return "$error_msg <a href=\"$URL\">$LinkText</a>";
80  }
81  return null;
82  }
83 
88  protected function handle(Request $request)
89  {
90  $vars = array();
91 
92  $uploadpks = $request->get('uploads');
93  $folderId = $request->get('folder');
94 
95  if (!empty($uploadpks)) {
96  $vars['message'] = $this->initDeletion($uploadpks, $folderId);
97  }
98 
99  $vars['uploadScript'] = ActiveHTTPscript("Uploads");
100  $vars['tracbackUri'] = Traceback_uri();
101  $root_folder_pk = GetUserRootFolder();
102  $vars['rootFolderListOptions'] = FolderListOption($root_folder_pk, 0);
103 
104  $uploadList = array();
105  $folderList = FolderListUploads_perm($root_folder_pk, Auth::PERM_WRITE);
106  foreach ($folderList as $L) {
107  $desc = $L['name'];
108  if (!empty($L['upload_desc'])) {
109  $desc .= " (" . $L['upload_desc'] . ")";
110  }
111  if (!empty($L['upload_ts'])) {
112  $desc .= " :: " . substr($L['upload_ts'], 0, 19);
113  }
114  $uploadList[$L['upload_pk']] = $desc;
115  }
116  $vars['uploadList'] = $uploadList;
117 
118  return $this->render('admin_upload_delete.html.twig', $this->mergeWithDefault($vars));
119  }
120 
121 
128  private function initDeletion($uploadpks, $folderId)
129  {
130  if (sizeof($uploadpks) <= 0) {
131  return _("No uploads selected");
132  }
133 
134  $errorMessages = [];
135  $deleteResponse = null;
136  foreach ($uploadpks as $uploadPk) {
137  $deleteResponse = $this->TryToDelete(intval($uploadPk), $folderId);
138 
139  if ($deleteResponse->getDeleteMessageCode() != DeleteMessages::SUCCESS) {
140  $errorMessages[] = $deleteResponse;
141  }
142  }
143 
144  if (sizeof($uploadpks) == 1) {
145  return $deleteResponse->getDeleteMessageString().$deleteResponse->getAdditionalMessage();
146  }
147 
148  $displayMessage = "";
149  $countErrorMessages = array_count_values(array_filter($errorMessages));
150  if (in_array(DeleteMessages::SCHEDULING_FAILED, $errorMessages)) {
151  $displayMessage .= "<br/>Scheduling failed for " .
152  $countErrorMessages[DeleteMessages::SCHEDULING_FAILED] . " uploads<br/>";
153  }
154 
155  if (in_array(DeleteMessages::NO_PERMISSION, $errorMessages)) {
156  $displayMessage .= "No permission to delete " .
157  $countErrorMessages[DeleteMessages::NO_PERMISSION] . " uploads<br/>";
158  }
159 
160  $displayMessage .= "Deletion of " .
161  (sizeof($uploadpks) - sizeof($errorMessages)) . " projects queued";
162  return DisplayMessage($displayMessage);
163  }
164 
171  public function TryToDelete($uploadpk, $folderId)
172  {
173  if (!$this->uploadDao->isEditable($uploadpk, Auth::getGroupId())) {
174  $returnMessage = DeleteMessages::NO_PERMISSION;
175  return new DeleteResponse($returnMessage);
176  }
177 
178  if (!empty($this->folderDao->isRemovableContent($uploadpk,2))) {
179  $this->folderDao->removeContentById($uploadpk, $folderId);
180  $returnMessage = DeleteMessages::SUCCESS;
181  return new DeleteResponse($returnMessage);
182  } else {
183  $rc = $this->delete(intval($uploadpk));
184  }
185 
186  if (! empty($rc)) {
187  $returnMessage = DeleteMessages::SCHEDULING_FAILED;
188  return new DeleteResponse($returnMessage);
189  }
190 
191  /* Need to refresh the screen */
192  $URL = Traceback_uri() . "?mod=showjobs&upload=$uploadpk ";
193  $LinkText = _("View Jobs");
194  $returnMessage = DeleteMessages::SUCCESS;
195  return new DeleteResponse($returnMessage,
196  " <a href=$URL>$LinkText</a>");
197  }
198 }
199 
200 register_plugin(new AdminUploadDelete());
Messages which can be generated by delagent.
Handle response from delagent.
UI plugin to delete uploaded files.
initDeletion($uploadpks, $folderId)
starts deletion and handles error messages
TryToDelete($uploadpk, $folderId)
Given a folder_pk, try to add a job after checking permissions.
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
static getUserId()
Get the current user's id.
Definition: Auth.php:68
static getGroupId()
Get the current user's group id.
Definition: Auth.php:80
render($templateName, $vars=null, $headers=null)
ActiveHTTPscript($RequestName, $IncludeScriptTags=1)
Given a function name, create the JavaScript needed for doing the request.
GetUserRootFolder()
Get the top-of-tree folder_pk for the current user. Fail if there is no user session.
FolderListOption($ParentFolder, $Depth, $IncludeTop=1, $SelectId=-1, $linkParent=false, $OldParent=0)
Create the folder tree, using OPTION tags.
FolderListUploads_perm($ParentFolder, $perm)
Returns an array of uploads in a folder.
JobQueueAdd($job_pk, $jq_type, $jq_args, $jq_runonpfile, $Depends, $host=NULL, $jq_cmd_args=NULL)
Insert a jobqueue + jobdepends records.
Definition: common-job.php:157
Traceback_uri()
Get the URI without query to this location.
Definition: common-parm.php:97
fo_communicate_with_scheduler($input, &$output, &$error_msg)
Communicate with scheduler, send commands to the scheduler, then get the output.
UI namespace for DelAgent.