FOSSology  4.4.0
Open Source License Compliance by Open Source Software
UploadFilePage.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2008-2013 Hewlett-Packard Development Company, L.P.
4  SPDX-FileCopyrightText: © 2014-2017 Siemens AG
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
9 namespace Fossology\UI\Page;
10 
13 use Symfony\Component\HttpFoundation\File\Exception\FileException;
14 use Symfony\Component\HttpFoundation\File\UploadedFile;
15 use Symfony\Component\HttpFoundation\Request;
16 use Symfony\Component\HttpFoundation\Response;
17 
22 {
23  const FILE_INPUT_NAME = 'fileInput';
24 
25 
26  public function __construct()
27  {
28  parent::__construct(self::NAME, array(
29  self::TITLE => _("Upload a New File"),
30  self::MENU_LIST => "Upload::From File",
31  self::DEPENDENCIES => array("agent_unpack", "showjobs"),
32  self::PERMISSION => Auth::PERM_WRITE
33  ));
34  }
35 
36 
42  protected function handleView(Request $request, $vars)
43  {
44  $vars['fileInputName'] = self::FILE_INPUT_NAME;
45  return $this->render("upload_file.html.twig", $this->mergeWithDefault($vars));
46  }
47 
51  protected function handleUpload(Request $request)
52  {
53  global $MODDIR;
54  global $SYSCONFDIR;
55 
56  define("UPLOAD_ERR_EMPTY", 5);
57  define("UPLOAD_ERR_INVALID_FOLDER_PK", 100);
58  define("UPLOAD_ERR_RESEND", 200);
59  $uploadErrors = array(
60  UPLOAD_ERR_OK => _("No errors."),
61  UPLOAD_ERR_INI_SIZE => _("Larger than upload_max_filesize ") . ini_get('upload_max_filesize'),
62  UPLOAD_ERR_FORM_SIZE => _("Larger than form MAX_FILE_SIZE."),
63  UPLOAD_ERR_PARTIAL => _("Partial upload."),
64  UPLOAD_ERR_NO_FILE => _("No file selected."),
65  UPLOAD_ERR_NO_TMP_DIR => _("No temporary directory."),
66  UPLOAD_ERR_CANT_WRITE => _("Can't write to disk."),
67  UPLOAD_ERR_EXTENSION => _("File upload stopped by extension."),
68  UPLOAD_ERR_EMPTY => _("File is empty or you don't have permission to read the file."),
69  UPLOAD_ERR_INVALID_FOLDER_PK => _("Invalid Folder."),
70  UPLOAD_ERR_RESEND => _("This seems to be a resent file.")
71  );
72 
73  $folderId = intval($request->get(self::FOLDER_PARAMETER_NAME));
74  $descriptions = $request->get(self::DESCRIPTION_INPUT_NAME);
75  for ($i = 0; $i < count($descriptions); $i++) {
76  $descriptions[$i] = stripslashes($descriptions[$i]);
77  $descriptions[$i] = $this->basicShEscaping($descriptions[$i]);
78  }
79  $uploadedFiles = $request->files->get(self::FILE_INPUT_NAME);
80  $uploadFiles = [];
81  for ($i = 0; $i < count($uploadedFiles); $i++) {
82  $uploadFiles[] = [
83  'file' => $uploadedFiles[$i],
84  'description' => $descriptions[$i]
85  ];
86  }
87 
88  if (empty($uploadedFiles)) {
89  return array(false, $uploadErrors[UPLOAD_ERR_NO_FILE], "");
90  }
91 
92  if (
93  $request->getSession()->get(self::UPLOAD_FORM_BUILD_PARAMETER_NAME)
94  != $request->get(self::UPLOAD_FORM_BUILD_PARAMETER_NAME)
95  ) {
96  return array(false, $uploadErrors[UPLOAD_ERR_RESEND], "");
97  }
98 
99  foreach ($uploadFiles as $uploadedFile) {
100  if (
101  $uploadedFile['file']->getSize() == 0 &&
102  $uploadedFile['file']->getError() == 0
103  ) {
104  return array(false, $uploadErrors[UPLOAD_ERR_EMPTY], "");
105  } else if ($uploadedFile['file']->getSize() >= UploadedFile::getMaxFilesize()) {
106  return array(false, $uploadErrors[UPLOAD_ERR_INI_SIZE] .
107  _(" is really ") . $uploadedFile['file']->getSize() . " bytes.", "");
108  }
109  if (!$uploadedFile['file']->isValid()) {
110  return array(false, $uploadedFile['file']->getErrorMessage(), "");
111  }
112  }
113 
114  if (empty($folderId)) {
115  return array(false, $uploadErrors[UPLOAD_ERR_INVALID_FOLDER_PK], "");
116  }
117 
118  $setGlobal = ($request->get('globalDecisions')) ? 1 : 0;
119 
120  $public = $request->get('public');
121  $publicPermission = ($public == self::PUBLIC_ALL) ? Auth::PERM_READ : Auth::PERM_NONE;
122 
123  $uploadMode = (1 << 3); // code for "it came from web upload"
124  $userId = Auth::getUserId();
125  $groupId = Auth::getGroupId();
126  $projectGroup = $GLOBALS['SysConf']['DIRECTORIES']['PROJECTGROUP'] ?: 'fossy';
127 
128  $errors = [];
129  $success = [];
130  foreach ($uploadFiles as $uploadedFile) {
131  $originalFileName = $uploadedFile['file']->getClientOriginalName();
132  $originalFileName = $this->basicShEscaping($originalFileName);
133  /* Create an upload record. */
134  $uploadId = JobAddUpload($userId, $groupId, $originalFileName,
135  $originalFileName, $uploadedFile['description'], $uploadMode,
136  $folderId, $publicPermission, $setGlobal);
137  if (empty($uploadId)) {
138  $errors[] = _("Failed to insert upload record: ") .
139  $originalFileName;
140  continue;
141  }
142 
143  try {
144  $uploadedTempFile = $uploadedFile['file']->move(
145  $uploadedFile['file']->getPath(),
146  $uploadedFile['file']->getFilename() . '-uploaded'
147  )->getPathname();
148  } catch (FileException $e) {
149  $errors[] = _("Could not save uploaded file: ") . $originalFileName;
150  continue;
151  }
152  $success[] = [
153  "tempfile" => $uploadedTempFile,
154  "orignalfile" => $originalFileName,
155  "uploadid" => $uploadId
156  ];
157  }
158 
159  if (!empty($errors)) {
160  return [false, implode(" ; ", $errors), ""];
161  }
162 
163  $messages = [];
164  foreach ($success as $row) {
165  $uploadedTempFile = $row["tempfile"];
166  $originalFileName = $row["orignalfile"];
167  $uploadId = $row["uploadid"];
168 
169  $wgetAgentCall = "$MODDIR/wget_agent/agent/wget_agent -C -g " .
170  "$projectGroup -k $uploadId '$uploadedTempFile' -c '$SYSCONFDIR'";
171  $wgetOutput = array();
172  exec($wgetAgentCall, $wgetOutput, $wgetReturnValue);
173  unlink($uploadedTempFile);
174 
175  if ($wgetReturnValue != 0) {
176  $message = implode(' ', $wgetOutput);
177  if (empty($message)) {
178  $message = _("File upload failed. Error:") . $wgetReturnValue;
179  }
180  $errors[] = $message;
181  } else {
182  $messages[] = $this->postUploadAddJobs($request, $originalFileName,
183  $uploadId);
184  }
185  }
186 
187  if (!empty($errors)) {
188  return [false, implode(" ; ", $errors), ""];
189  }
190 
191  return array(true, implode("", $messages), "",
192  array_column($success, "uploadid"));
193  }
194 }
195 
196 register_plugin(new UploadFilePage());
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)
Upload a file from the users computer using the UI.
handleView(Request $request, $vars)
handleUpload(Request $request)
Process the upload request.
JobAddUpload($userId, $groupId, $job_name, $filename, $desc, $UploadMode, $folder_pk, $public_perm=Auth::PERM_NONE, $setGlobal=0)
Insert a new upload record, and update the foldercontents table.
Definition: common-job.php:56