FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
UploadFilePage.php
1 <?php
2 /***********************************************************
3  * Copyright (C) 2008-2013 Hewlett-Packard Development Company, L.P.
4  * Copyright (C) 2014-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  ***********************************************************/
19 
20 namespace Fossology\UI\Page;
21 
28 
33 {
34  const FILE_INPUT_NAME = 'fileInput';
35 
36 
37  public function __construct()
38  {
39  parent::__construct(self::NAME, array(
40  self::TITLE => _("Upload a New File"),
41  self::MENU_LIST => "Upload::From File",
42  self::DEPENDENCIES => array("agent_unpack", "showjobs"),
43  self::PERMISSION => Auth::PERM_WRITE
44  ));
45  }
46 
47 
52  protected function handleView(Request $request, $vars)
53  {
54  $vars['fileInputName'] = self::FILE_INPUT_NAME;
55  return $this->render("upload_file.html.twig", $this->mergeWithDefault($vars));
56  }
57 
61  protected function handleUpload(Request $request)
62  {
63  global $MODDIR;
64  global $SYSCONFDIR;
65 
66  define("UPLOAD_ERR_EMPTY", 5);
67  define("UPLOAD_ERR_INVALID_FOLDER_PK", 100);
68  define("UPLOAD_ERR_RESEND", 200);
69  $uploadErrors = array(
70  UPLOAD_ERR_OK => _("No errors."),
71  UPLOAD_ERR_INI_SIZE => _("Larger than upload_max_filesize ") . ini_get('upload_max_filesize'),
72  UPLOAD_ERR_FORM_SIZE => _("Larger than form MAX_FILE_SIZE."),
73  UPLOAD_ERR_PARTIAL => _("Partial upload."),
74  UPLOAD_ERR_NO_FILE => _("No file selected."),
75  UPLOAD_ERR_NO_TMP_DIR => _("No temporary directory."),
76  UPLOAD_ERR_CANT_WRITE => _("Can't write to disk."),
77  UPLOAD_ERR_EXTENSION => _("File upload stopped by extension."),
78  UPLOAD_ERR_EMPTY => _("File is empty or you don't have permission to read the file."),
79  UPLOAD_ERR_INVALID_FOLDER_PK => _("Invalid Folder."),
80  UPLOAD_ERR_RESEND => _("This seems to be a resent file.")
81  );
82 
83  $folderId = intval($request->get(self::FOLDER_PARAMETER_NAME));
84  $description = stripslashes($request->get(self::DESCRIPTION_INPUT_NAME));
85  $description = $this->basicShEscaping($description);
86  $uploadedFile = $request->files->get(self::FILE_INPUT_NAME);
87 
88  if ($uploadedFile === null) {
89  return array(false, $uploadErrors[UPLOAD_ERR_NO_FILE], $description);
90  }
91 
92  if ($request->getSession()->get(self::UPLOAD_FORM_BUILD_PARAMETER_NAME)
93  != $request->get(self::UPLOAD_FORM_BUILD_PARAMETER_NAME)) {
94  return array(false, $uploadErrors[UPLOAD_ERR_RESEND], $description);
95  }
96 
97  if ($uploadedFile->getSize() == 0 && $uploadedFile->getError() == 0) {
98  return array(false, $uploadErrors[UPLOAD_ERR_EMPTY], $description);
99  } else if ($uploadedFile->getSize() >= UploadedFile::getMaxFilesize()) {
100  return array(false, $uploadErrors[UPLOAD_ERR_INI_SIZE] .
101  _(" is really ") . $uploadedFile->getSize() . " bytes.", $description);
102  }
103 
104  if (empty($folderId)) {
105  return array(false, $uploadErrors[UPLOAD_ERR_INVALID_FOLDER_PK], $description);
106  }
107 
108  if (!$uploadedFile->isValid()) {
109  return array(false, $uploadedFile->getErrorMessage(), $description);
110  }
111 
112  $originalFileName = $uploadedFile->getClientOriginalName();
113  $originalFileName = $this->basicShEscaping($originalFileName);
114 
115  $public = $request->get('public');
116  $publicPermission = ($public == self::PUBLIC_ALL) ? Auth::PERM_READ : Auth::PERM_NONE;
117 
118  /* Create an upload record. */
119  $uploadMode = (1 << 3); // code for "it came from web upload"
120  $userId = Auth::getUserId();
121  $groupId = Auth::getGroupId();
122  $uploadId = JobAddUpload($userId, $groupId, $originalFileName,
123  $originalFileName, $description, $uploadMode, $folderId, $publicPermission);
124  if (empty($uploadId)) {
125  return array(false, _("Failed to insert upload record"), $description);
126  }
127 
128  try {
129  $uploadedTempFile = $uploadedFile->move($uploadedFile->getPath(),
130  $uploadedFile->getFilename() . '-uploaded')->getPathname();
131  } catch (FileException $e) {
132  return array(false, _("Could not save uploaded file"), $description);
133  }
134 
135  $projectGroup = $GLOBALS['SysConf']['DIRECTORIES']['PROJECTGROUP'] ?: 'fossy';
136  $wgetAgentCall = "$MODDIR/wget_agent/agent/wget_agent -C -g $projectGroup -k $uploadId '$uploadedTempFile' -c '$SYSCONFDIR'";
137  $wgetOutput = array();
138  exec($wgetAgentCall, $wgetOutput, $wgetReturnValue);
139  unlink($uploadedTempFile);
140 
141  if ($wgetReturnValue != 0) {
142  $message = implode(' ', $wgetOutput);
143  if (empty($message)) {
144  $message = _("File upload failed. Error:") . $wgetReturnValue;
145  }
146  return array(false, $message, $description);
147  }
148 
149  $message = $this->postUploadAddJobs($request, $originalFileName, $uploadId);
150 
151  return array(true, $message, $description, $uploadId);
152  }
153 }
154 
155 register_plugin(new UploadFilePage());
static getUserId()
Get the current user&#39;s id.
Definition: Auth.php:69
render($templateName, $vars=null, $headers=null)
Upload a file from the users computer using the UI.
handleView(Request $request, $vars)
static getGroupId()
Get the current user&#39;s group id.
Definition: Auth.php:78
JobAddUpload($userId, $groupId, $job_name, $filename, $desc, $UploadMode, $folder_pk, $public_perm=Auth::PERM_NONE)
Insert a new upload record, and update the foldercontents table.
Definition: common-job.php:66
handleUpload(Request $request)
Process the upload request.