FOSSology  4.7.1
Open Source License Compliance by Open Source Software
admin-upload-edit.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2008-2013 Hewlett-Packard Development Company, L.P.
4  SPDX-FileCopyrightText: © 2015 Siemens AG
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
14 
16 {
18  private $uploadDao;
20  private $dbManager;
22  private $folderDao;
23 
24  function __construct()
25  {
26  $this->Name = "upload_properties";
27  $this->Title = _("Edit Uploaded File Properties");
28  $this->MenuList = "Organize::Uploads::Edit Properties";
29  $this->DBaccess = PLUGIN_DB_WRITE;
30  parent::__construct();
31  $this->uploadDao = $GLOBALS['container']->get('dao.upload');
32  $this->dbManager = $GLOBALS['container']->get('db.manager');
33  $this->folderDao = $GLOBALS['container']->get('dao.folder');
34  }
35 
47  public function UpdateUploadProperties($uploadId, $newName, $newDesc)
48  {
49  /* Treat a whitespace-only name like an empty one, keep the old name */
50  $newName = trim($newName ?? '');
51  if (empty($newName) and empty($newDesc)) {
52  return 2;
53  }
54 
55  if (!empty($newName)) {
56  /*
57  * Use pfile_fk to select the correct entry in the upload tree, artifacts
58  * (e.g. directories of the upload do not have pfiles).
59  */
60  $row = $this->dbManager->getSingleRow(
61  "SELECT pfile_fk FROM upload WHERE upload_pk=$1",array($uploadId),__METHOD__.'.getPfileId');
62  if (empty($row)) {
63  return 0;
64  }
65  $pfileFk = $row['pfile_fk'];
66 
67  /* Always keep uploadtree.ufile_name and upload.upload_filename in sync */
68  $this->dbManager->getSingleRow(
69  "UPDATE uploadtree SET ufile_name=$3 WHERE upload_fk=$1 AND pfile_fk=$2",
70  array($uploadId, $pfileFk, $newName),
71  __METHOD__ . '.updateItem');
72  $this->dbManager->getSingleRow(
73  "UPDATE upload SET upload_filename=$3 WHERE upload_pk=$1 AND pfile_fk=$2",
74  array($uploadId, $pfileFk, $newName),
75  __METHOD__ . '.updateUpload.name');
76  }
77 
78  if (! empty($newDesc)) {
79  $trimNewDesc = trim($newDesc);
80  $this->dbManager->getSingleRow("UPDATE upload SET upload_desc=$2 WHERE upload_pk=$1",
81  array($uploadId, $trimNewDesc), __METHOD__ . '.updateUpload.desc');
82  }
83  return 1;
84  }
85 
86  public function Output()
87  {
88  $groupId = Auth::getGroupId();
89  $rootFolder = $this->folderDao->getRootFolder(Auth::getUserId());
90  $folderStructure = $this->folderDao->getFolderStructure($rootFolder->getId());
91 
92  $V = "";
93  $folder_pk = GetParm('folder', PARM_INTEGER);
94  if (empty($folder_pk)) {
95  $folder_pk = $rootFolder->getId();
96  }
97 
98  $NewName = GetArrayVal("newname", $_POST);
99  $NewDesc = GetArrayVal("newdesc", $_POST);
100  $upload_pk = GetArrayVal("upload_pk", $_POST);
101  if (empty($upload_pk)) {
102  $upload_pk = GetParm('upload', PARM_INTEGER);
103  }
104  /* Check Upload permission */
105  if (! empty($upload_pk) && !$this->uploadDao->isEditable($upload_pk, $groupId)) {
106  $text = _("Permission Denied");
107  return "<h2>$text</h2>";
108  }
109  $rc = $this->UpdateUploadProperties($upload_pk, $NewName, $NewDesc);
110  if ($rc == 0) {
111  $text = _("Nothing to Change");
112  $this->vars['message'] = $text;
113  } else if ($rc == 1) {
114  $text = _("Upload Properties successfully changed");
115  $this->vars['message'] = $text;
116  }
117 
118  $this->vars['folderStructure'] = $folderStructure;
119  $this->vars['folderId'] = $folder_pk;
120  $this->vars['baseUri'] = $Uri = Traceback_uri() . "?mod=" . $this->Name . "&folder=";
121 
122  $folderUploads = $this->folderDao->getFolderUploads($folder_pk, $groupId);
123  $uploadsById = array();
124  /* @var $uploadProgress UploadProgress */
125  foreach ($folderUploads as $uploadProgress) {
126  if ($uploadProgress->getGroupId() != $groupId) {
127  continue;
128  }
129  if (! $this->uploadDao->isEditable($uploadProgress->getId(), $groupId)) {
130  continue;
131  }
132  $display = $uploadProgress->getFilename() . _(" from ") . Convert2BrowserTime(date("Y-m-d H:i:s",$uploadProgress->getTimestamp()));
133  $uploadsById[$uploadProgress->getId()] = $display;
134  }
135  $this->vars['uploadList'] = $uploadsById;
136  if (empty($upload_pk)) {
137  reset($uploadsById);
138  $upload_pk = key($uploadsById);
139  }
140  $this->vars['uploadId'] = $upload_pk;
141 
142  if ($upload_pk) {
143  $upload = $this->uploadDao->getUpload($upload_pk);
144  if (empty($upload)) {
145  $this->vars['message'] = _("Missing upload.");
146  return 0;
147  }
148  } else {
149  $upload = null;
150  }
151 
152  $baseFolderUri = $this->vars['baseUri']."$folder_pk&upload=";
153  $this->vars['uploadAction'] = "onchange=\"js_url(this.value, '$baseFolderUri')\"";
154 
155  $this->vars['uploadFilename'] = $upload ? $upload->getFilename() : '';
156  $this->vars['uploadDesc'] = $upload ? $upload->getDescription() : '';
157  $this->vars['content'] = $V;
158 
159  return $this->render('admin_upload_edit.html.twig');
160  }
161 }
162 $NewPlugin = new upload_properties;
This is the Plugin class. All plugins should:
Definition: FO_Plugin.php:57
render($templateName, $vars=null)
Definition: FO_Plugin.php:434
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
Output()
This function is called when user output is requested. This function is responsible for content....
UpdateUploadProperties($uploadId, $newName, $newDesc)
Update upload properties (name, description)
__construct()
base constructor. Most plugins will just use this
Traceback_uri()
Get the URI without query to this location.
Definition: common-parm.php:97
const PARM_INTEGER
Definition: common-parm.php:14
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:46
GetArrayVal($Key, $Arr)
Get the value from a array(map)
Definition: common-ui.php:157
Convert2BrowserTime($server_time)
Convert the server time to browser time.
Definition: common-ui.php:312
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:690
#define PLUGIN_DB_WRITE
Plugin requires write permission on DB.
Definition: libfossology.h:38
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16