9 namespace Fossology\UI\Page;
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;
23 const FILE_INPUT_NAME =
'fileInput';
26 public function __construct()
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"),
44 $vars[
'fileInputName'] = self::FILE_INPUT_NAME;
45 return $this->
render(
"upload_file.html.twig", $this->mergeWithDefault($vars));
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.")
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]);
79 $uploadedFiles = $request->files->get(self::FILE_INPUT_NAME);
81 for ($i = 0; $i < count($uploadedFiles); $i++) {
83 'file' => $uploadedFiles[$i],
84 'description' => $descriptions[$i]
88 if (empty($uploadedFiles)) {
89 return array(
false, $uploadErrors[UPLOAD_ERR_NO_FILE],
"");
93 $request->getSession()->get(self::UPLOAD_FORM_BUILD_PARAMETER_NAME)
94 != $request->get(self::UPLOAD_FORM_BUILD_PARAMETER_NAME)
96 return array(
false, $uploadErrors[UPLOAD_ERR_RESEND],
"");
99 foreach ($uploadFiles as $uploadedFile) {
101 $uploadedFile[
'file']->getSize() == 0 &&
102 $uploadedFile[
'file']->getError() == 0
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.",
"");
109 if (!$uploadedFile[
'file']->isValid()) {
110 return array(
false, $uploadedFile[
'file']->getErrorMessage(),
"");
114 if (empty($folderId)) {
115 return array(
false, $uploadErrors[UPLOAD_ERR_INVALID_FOLDER_PK],
"");
118 $setGlobal = ($request->get(
'globalDecisions')) ? 1 : 0;
120 $public = $request->get(
'public');
123 $uploadMode = (1 << 3);
126 $projectGroup = $GLOBALS[
'SysConf'][
'DIRECTORIES'][
'PROJECTGROUP'] ?:
'fossy';
130 foreach ($uploadFiles as $uploadedFile) {
131 $originalFileName = $uploadedFile[
'file']->getClientOriginalName();
132 $originalFileName = $this->basicShEscaping($originalFileName);
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: ") .
144 $uploadedTempFile = $uploadedFile[
'file']->move(
145 $uploadedFile[
'file']->getPath(),
146 $uploadedFile[
'file']->getFilename() .
'-uploaded'
148 }
catch (FileException $e) {
149 $errors[] = _(
"Could not save uploaded file: ") . $originalFileName;
153 "tempfile" => $uploadedTempFile,
154 "orignalfile" => $originalFileName,
155 "uploadid" => $uploadId
159 if (!empty($errors)) {
160 return [
false, implode(
" ; ", $errors),
""];
164 foreach ($success as $row) {
165 $uploadedTempFile = $row[
"tempfile"];
166 $originalFileName = $row[
"orignalfile"];
167 $uploadId = $row[
"uploadid"];
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);
175 if ($wgetReturnValue != 0) {
176 $message = implode(
' ', $wgetOutput);
177 if (empty($message)) {
178 $message = _(
"File upload failed. Error:") . $wgetReturnValue;
180 $errors[] = $message;
182 $messages[] = $this->postUploadAddJobs($request, $originalFileName,
187 if (!empty($errors)) {
188 return [
false, implode(
" ; ", $errors),
""];
191 return array(
true, implode(
"", $messages),
"",
192 array_column($success,
"uploadid"));
196 register_plugin(
new UploadFilePage());
Contains the constants and helpers for authentication of user.
static getUserId()
Get the current user's id.
static getGroupId()
Get the current user's group id.
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.