9 namespace Fossology\UI\Page;
14 use Symfony\Component\HttpFoundation\File\Exception\FileException;
15 use Symfony\Component\HttpFoundation\File\UploadedFile;
16 use Symfony\Component\HttpFoundation\Request;
17 use Symfony\Component\HttpFoundation\Response;
24 const FILE_INPUT_NAME =
'fileInput';
27 public function __construct()
29 parent::__construct(self::NAME, array(
30 self::TITLE => _(
"Upload a New File"),
31 self::MENU_LIST =>
"Upload::From File",
32 self::DEPENDENCIES => array(
"agent_unpack",
"showjobs"),
47 $vars[
'parmAgentContents'] = array();
48 $vars[
'parmAgentFoots'] = array();
49 $vars[
'hiddenAgentContents'] = array();
50 foreach ($parmAgentList as $parmAgent) {
52 if ($parmAgent ==
"agent_reuser") {
53 $vars[
'parmAgentContents'][] = sprintf(
"<li>
54 <div class='form-group'>
58 <img src='images/info_16.png' data-toggle='tooltip' title='%s' alt='' class='info-bullet'/><br/>
59 <button type='button' class='btn btn-default btn-sm' data-toggle='modal' data-target='#reuseModal'>%s</button>
60 <img src='images/info_16.png' data-toggle='tooltip' title='%s' alt='' class='info-bullet'/><br/>
61 </li>", _(
"Optional"), _(
"Reuse"),
62 _(
"Copy clearing decisions if there is the same file hash between two files"),
63 _(
"Set the reuse information"),
64 _(
"Open the pop-up to setup the reuse information for uploads"));
65 $vars[
'hiddenAgentContents'][] = $agent->renderContent($vars);
67 $vars[
'parmAgentContents'][] = $agent->renderContent($vars);
69 $vars[
'parmAgentFoots'][] = $agent->renderFoot($vars);
71 $vars[
'fileInputName'] = self::FILE_INPUT_NAME;
72 return $this->
render(
"upload_file.html.twig", $this->mergeWithDefault($vars));
82 define(
"UPLOAD_ERR_EMPTY", 5);
83 define(
"UPLOAD_ERR_INVALID_FOLDER_PK", 100);
84 define(
"UPLOAD_ERR_RESEND", 200);
85 $uploadErrors = array(
86 UPLOAD_ERR_OK => _(
"No errors."),
87 UPLOAD_ERR_INI_SIZE => _(
"Larger than upload_max_filesize ") . ini_get(
'upload_max_filesize'),
88 UPLOAD_ERR_FORM_SIZE => _(
"Larger than form MAX_FILE_SIZE."),
89 UPLOAD_ERR_PARTIAL => _(
"Partial upload."),
90 UPLOAD_ERR_NO_FILE => _(
"No file selected."),
91 UPLOAD_ERR_NO_TMP_DIR => _(
"No temporary directory."),
92 UPLOAD_ERR_CANT_WRITE => _(
"Can't write to disk."),
93 UPLOAD_ERR_EXTENSION => _(
"File upload stopped by extension."),
94 UPLOAD_ERR_EMPTY => _(
"File is empty or you don't have permission to read the file."),
95 UPLOAD_ERR_INVALID_FOLDER_PK => _(
"Invalid Folder."),
96 UPLOAD_ERR_RESEND => _(
"This seems to be a resent file.")
99 $folderId = intval($request->get(self::FOLDER_PARAMETER_NAME));
100 $descriptions = $request->get(self::DESCRIPTION_INPUT_NAME, []);
101 for ($i = 0; $i < count($descriptions); $i++) {
102 $descriptions[$i] = stripslashes($descriptions[$i]);
103 $descriptions[$i] = $this->basicShEscaping($descriptions[$i]);
105 $uploadedFiles = $request->files->get(self::FILE_INPUT_NAME, []);
107 for ($i = 0; $i < count($uploadedFiles); $i++) {
109 'file' => $uploadedFiles[$i],
110 'description' => $descriptions[$i]
114 if (empty($uploadedFiles)) {
115 return array(
false, $uploadErrors[UPLOAD_ERR_NO_FILE],
"");
119 $request->getSession()->get(self::UPLOAD_FORM_BUILD_PARAMETER_NAME)
120 != $request->get(self::UPLOAD_FORM_BUILD_PARAMETER_NAME)
122 return array(
false, $uploadErrors[UPLOAD_ERR_RESEND],
"");
125 foreach ($uploadFiles as $uploadedFile) {
127 $uploadedFile[
'file']->getSize() == 0 &&
128 $uploadedFile[
'file']->getError() == 0
130 return array(
false, $uploadErrors[UPLOAD_ERR_EMPTY],
"");
131 }
else if ($uploadedFile[
'file']->getSize() >= UploadedFile::getMaxFilesize()) {
132 return array(
false, $uploadErrors[UPLOAD_ERR_INI_SIZE] .
133 _(
" is really ") . $uploadedFile[
'file']->getSize() .
" bytes.",
"");
135 if (!$uploadedFile[
'file']->isValid()) {
136 return array(
false, $uploadedFile[
'file']->getErrorMessage(),
"");
140 if (empty($folderId)) {
141 return array(
false, $uploadErrors[UPLOAD_ERR_INVALID_FOLDER_PK],
"");
144 $setGlobal = ($request->get(
'globalDecisions')) ? 1 : 0;
146 $public = $request->get(
'public');
149 $uploadMode = (1 << 3);
152 $projectGroup = $GLOBALS[
'SysConf'][
'DIRECTORIES'][
'PROJECTGROUP'] ?:
'fossy';
156 foreach ($uploadFiles as $uploadedFile) {
157 $originalFileName = $uploadedFile[
'file']->getClientOriginalName();
158 $originalFileName = $this->basicShEscaping($originalFileName);
160 $uploadId =
JobAddUpload($userId, $groupId, $originalFileName,
161 $originalFileName, $uploadedFile[
'description'], $uploadMode,
162 $folderId, $publicPermission, $setGlobal);
163 if (empty($uploadId)) {
164 $errors[] = _(
"Failed to insert upload record: ") .
170 $uploadedTempFile = $uploadedFile[
'file']->move(
171 $uploadedFile[
'file']->getPath(),
172 $uploadedFile[
'file']->getFilename() .
'-uploaded'
174 }
catch (FileException $e) {
175 $errors[] = _(
"Could not save uploaded file: ") . $originalFileName;
179 "tempfile" => $uploadedTempFile,
180 "orignalfile" => $originalFileName,
181 "uploadid" => $uploadId
185 if (!empty($errors)) {
186 return [
false, implode(
" ; ", $errors),
""];
190 foreach ($success as $row) {
191 $uploadedTempFile = $row[
"tempfile"];
192 $originalFileName = $row[
"orignalfile"];
193 $uploadId = $row[
"uploadid"];
195 $wgetAgentCall =
"$MODDIR/wget_agent/agent/wget_agent -C -g " .
196 "$projectGroup -k $uploadId '$uploadedTempFile' -c '$SYSCONFDIR'";
197 $wgetOutput = array();
198 exec($wgetAgentCall, $wgetOutput, $wgetReturnValue);
199 unlink($uploadedTempFile);
201 if ($wgetReturnValue != 0) {
202 $message = implode(
' ', $wgetOutput);
203 if (empty($message)) {
204 $message = _(
"File upload failed. Error:") . $wgetReturnValue;
206 $errors[] = $message;
209 $messages[] = $this->postUploadAddJobs($reuseRequest, $originalFileName,
214 if (!empty($errors)) {
215 return [
false, implode(
" ; ", $errors),
""];
218 return array(
true, implode(
"", $messages),
"",
219 array_column($success,
"uploadid"));
228 $reuseRequest = clone $request;
232 if (is_array($reuseSelector) && array_key_exists($originalFileName, $reuseSelector)) {
233 $reuseRequest->request->set(
235 $reuseSelector[$originalFileName]
238 if (is_array($reuseMode) && array_key_exists($originalFileName, $reuseMode)) {
239 $reuseRequest->request->set(
241 $reuseMode[$originalFileName]
244 return $reuseRequest;
248 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)
UI element for reuser during Uploading new package.
const REUSE_MODE
Form element name for main license to reuse.
const UPLOAD_TO_REUSE_SELECTOR_NAME
Form element name for main license to reuse.
Upload a file from the users computer using the UI.
handleView(Request $request, $vars)
getRequestForReuse(Request $request, string $originalFileName)
Check if parameters exits for the request Create a new request object and update parameter expected v...
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.
plugin_find($pluginName)
Given the official name of a plugin, return the $Plugins object.