FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
AdminUploadDelete.php
1 <?php
2 /***********************************************************
3  Copyright (C) 2008-2013 Hewlett-Packard Development Company, L.P.
4  Copyright (C) 2015-2017 Siemens AG
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 ***********************************************************/
24 
33 
39 {
40  const NAME = "admin_upload_delete";
41 
43  private $uploadDao;
44 
46  private $folderDao;
47 
48  function __construct()
49  {
50  parent::__construct(self::NAME, array(
51  self::TITLE => _("Delete Uploaded File"),
52  self::MENU_LIST => "Organize::Uploads::Delete Uploaded File",
53  self::PERMISSION => Auth::PERM_ADMIN,
54  self::REQUIRES_LOGIN => true
55  ));
56 
57  global $container;
58  $this->uploadDao = $container->get('dao.upload');
59  $this->folderDao = $container->get('dao.folder');
60  }
61 
62 
68  private function delete($uploadpk)
69  {
70  /* Prepare the job: job "Delete" */
71  $user_pk = Auth::getUserId();
72  $group_pk = Auth::getGroupId();
73  $jobpk = JobAddJob($user_pk, $group_pk, "Delete", $uploadpk);
74  if (empty($jobpk) || ($jobpk < 0)) {
75  return _("Failed to create job record");
76  }
77  /* Add job: job "Delete" has jobqueue item "delagent" */
78  $jqargs = "DELETE UPLOAD $uploadpk";
79  $jobqueuepk = JobQueueAdd($jobpk, "delagent", $jqargs, null, null);
80  if (empty($jobqueuepk)) {
81  return _("Failed to place delete in job queue");
82  }
83 
84  /* Tell the scheduler to check the queue. */
85  $success = fo_communicate_with_scheduler("database", $output, $error_msg);
86  if (!$success) {
87  $error_msg = _("Is the scheduler running? Your jobs have been added to job queue.");
88  $URL = Traceback_uri() . "?mod=showjobs&upload=$uploadpk ";
89  $LinkText = _("View Jobs");
90  return "$error_msg <a href=\"$URL\">$LinkText</a>";
91  }
92  return null;
93  }
94 
99  protected function handle(Request $request)
100  {
101  $vars = array();
102 
103  $uploadpks = $request->get('uploads');
104  $folderId = $request->get('folder');
105 
106  if (!empty($uploadpks)) {
107  $vars['message'] = $this->initDeletion($uploadpks, $folderId);
108  }
109 
110  $vars['uploadScript'] = ActiveHTTPscript("Uploads");
111  $vars['tracbackUri'] = Traceback_uri();
112  $root_folder_pk = GetUserRootFolder();
113  $vars['rootFolderListOptions'] = FolderListOption($root_folder_pk, 0);
114 
115  $uploadList = array();
116  $folderList = FolderListUploads_perm($root_folder_pk, Auth::PERM_WRITE);
117  foreach ($folderList as $L) {
118  $desc = $L['name'];
119  if (!empty($L['upload_desc'])) {
120  $desc .= " (" . $L['upload_desc'] . ")";
121  }
122  if (!empty($L['upload_ts'])) {
123  $desc .= " :: " . substr($L['upload_ts'], 0, 19);
124  }
125  $uploadList[$L['upload_pk']] = $desc;
126  }
127  $vars['uploadList'] = $uploadList;
128 
129  return $this->render('admin_upload_delete.html.twig', $this->mergeWithDefault($vars));
130  }
131 
132 
139  private function initDeletion($uploadpks, $folderId)
140  {
141  if (sizeof($uploadpks) <= 0) {
142  return _("No uploads selected");
143  }
144 
145  $errorMessages = [];
146  $deleteResponse = null;
147  foreach ($uploadpks as $uploadPk) {
148  $deleteResponse = $this->TryToDelete(intval($uploadPk), $folderId);
149 
150  if ($deleteResponse->getDeleteMessageCode() != DeleteMessages::SUCCESS) {
151  $errorMessages[] = $deleteResponse;
152  }
153  }
154 
155  if (sizeof($uploadpks) == 1) {
156  return $deleteResponse->getDeleteMessageString().$deleteResponse->getAdditionalMessage();
157  }
158 
159  $displayMessage = "";
160  $countErrorMessages = array_count_values(array_filter($errorMessages));
161  if (in_array(DeleteMessages::SCHEDULING_FAILED, $errorMessages)) {
162  $displayMessage .= "<br/>Scheduling failed for " .
163  $countErrorMessages[DeleteMessages::SCHEDULING_FAILED] . " uploads<br/>";
164  }
165 
166  if (in_array(DeleteMessages::NO_PERMISSION, $errorMessages)) {
167  $displayMessage .= "No permission to delete " .
168  $countErrorMessages[DeleteMessages::NO_PERMISSION] . " uploads<br/>";
169  }
170 
171  $displayMessage .= "Deletion of " .
172  (sizeof($uploadpks) - sizeof($errorMessages)) . " projects queued";
173  return DisplayMessage($displayMessage);
174  }
175 
182  private function TryToDelete($uploadpk, $folderId)
183  {
184  if (!$this->uploadDao->isEditable($uploadpk, Auth::getGroupId())) {
185  $returnMessage = DeleteMessages::NO_PERMISSION;
186  return new DeleteResponse($returnMessage);
187  }
188 
189  if (!empty($this->folderDao->isRemovableContent($uploadpk,2))) {
190  $this->folderDao->removeContentById($uploadpk, $folderId);
191  $returnMessage = DeleteMessages::SUCCESS;
192  return new DeleteResponse($returnMessage);
193  } else {
194  $rc = $this->delete(intval($uploadpk));
195  }
196 
197  if (! empty($rc)) {
198  $returnMessage = DeleteMessages::SCHEDULING_FAILED;
199  return new DeleteResponse($returnMessage);
200  }
201 
202  /* Need to refresh the screen */
203  $URL = Traceback_uri() . "?mod=showjobs&upload=$uploadpk ";
204  $LinkText = _("View Jobs");
205  $returnMessage = DeleteMessages::SUCCESS;
206  return new DeleteResponse($returnMessage,
207  " <a href=$URL>$LinkText</a>");
208  }
209 }
210 
211 register_plugin(new AdminUploadDelete());
Traceback_uri()
Get the URI without query to this location.
static getUserId()
Get the current user&#39;s id.
Definition: Auth.php:69
UI namespace for delagent.
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.
render($templateName, $vars=null, $headers=null)
ActiveHTTPscript($RequestName, $IncludeScriptTags=1)
Given a function name, create the JavaScript needed for doing the request.
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:159
Handle response from delagent.
GetUserRootFolder()
Get the top-of-tree folder_pk for the current user. Fail if there is no user session.
UI plugin to delete uploaded files.
fo_communicate_with_scheduler($input, &$output, &$error_msg)
Communicate with scheduler, send commands to the scheduler, then get the output.
initDeletion($uploadpks, $folderId)
starts deletion and handles error messages
TryToDelete($uploadpk, $folderId)
Given a folder_pk, try to add a job after checking permissions.
static getGroupId()
Get the current user&#39;s group id.
Definition: Auth.php:78