9 namespace Fossology\UI\Page;
15 use Symfony\Component\HttpFoundation\File\Exception\FileException;
16 use Symfony\Component\HttpFoundation\File\UploadedFile;
17 use Symfony\Component\HttpFoundation\Request;
18 use Symfony\Component\HttpFoundation\Response;
25 const FILE_INPUT_NAME =
'fileInput';
28 public function __construct()
30 parent::__construct(self::NAME, array(
31 self::TITLE => _(
"Upload a New File"),
32 self::MENU_LIST =>
"Upload::From File",
33 self::DEPENDENCIES => array(
"agent_unpack",
"showjobs"),
48 $vars[
'parmAgentContents'] = array();
49 $vars[
'parmAgentFoots'] = array();
50 $vars[
'hiddenAgentContents'] = array();
51 foreach ($parmAgentList as $parmAgent) {
53 if ($parmAgent ==
"agent_reuser") {
54 $vars[
'parmAgentContents'][] = sprintf(
"<li>
55 <div class='form-group'>
59 <img src='images/info_16.png' data-toggle='tooltip' title='%s' alt='' class='info-bullet'/><br/>
60 <button type='button' class='btn btn-default btn-sm' data-toggle='modal' data-target='#reuseModal'>%s</button>
61 <img src='images/info_16.png' data-toggle='tooltip' title='%s' alt='' class='info-bullet'/><br/>
62 </li>", _(
"Optional"), _(
"Reuse"),
63 _(
"Copy clearing decisions if there is the same file hash between two files"),
64 _(
"Set the reuse information"),
65 _(
"Open the pop-up to setup the reuse information for uploads"));
66 $vars[
'hiddenAgentContents'][] = $agent->renderContent($vars);
68 $vars[
'parmAgentContents'][] = $agent->renderContent($vars);
70 $vars[
'parmAgentFoots'][] = $agent->renderFoot($vars);
72 $vars[
'fileInputName'] = self::FILE_INPUT_NAME;
73 return $this->
render(
"upload_file.html.twig", $this->mergeWithDefault($vars));
83 define(
"UPLOAD_ERR_EMPTY", 5);
84 define(
"UPLOAD_ERR_INVALID_FOLDER_PK", 100);
85 define(
"UPLOAD_ERR_RESEND", 200);
86 $uploadErrors = array(
87 UPLOAD_ERR_OK => _(
"No errors."),
88 UPLOAD_ERR_INI_SIZE => _(
"Larger than upload_max_filesize ") . ini_get(
'upload_max_filesize'),
89 UPLOAD_ERR_FORM_SIZE => _(
"Larger than form MAX_FILE_SIZE."),
90 UPLOAD_ERR_PARTIAL => _(
"Partial upload."),
91 UPLOAD_ERR_NO_FILE => _(
"No file selected."),
92 UPLOAD_ERR_NO_TMP_DIR => _(
"No temporary directory."),
93 UPLOAD_ERR_CANT_WRITE => _(
"Can't write to disk."),
94 UPLOAD_ERR_EXTENSION => _(
"File upload stopped by extension."),
95 UPLOAD_ERR_EMPTY => _(
"File is empty or you don't have permission to read the file."),
96 UPLOAD_ERR_INVALID_FOLDER_PK => _(
"Invalid Folder."),
97 UPLOAD_ERR_RESEND => _(
"This seems to be a resent file.")
100 $folderId = intval($request->get(self::FOLDER_PARAMETER_NAME));
101 $descriptions = $request->get(self::DESCRIPTION_INPUT_NAME, []);
102 for ($i = 0; $i < count($descriptions); $i++) {
103 $descriptions[$i] = stripslashes($descriptions[$i]);
104 $descriptions[$i] = $this->basicShEscaping($descriptions[$i]);
106 $uploadedFiles = $request->files->get(self::FILE_INPUT_NAME, []);
108 for ($i = 0; $i < count($uploadedFiles); $i++) {
110 'file' => $uploadedFiles[$i],
111 'description' => $descriptions[$i]
115 if (empty($uploadedFiles)) {
116 return array(
false, $uploadErrors[UPLOAD_ERR_NO_FILE],
"");
120 $request->getSession()->get(self::UPLOAD_FORM_BUILD_PARAMETER_NAME)
121 != $request->get(self::UPLOAD_FORM_BUILD_PARAMETER_NAME)
123 return array(
false, $uploadErrors[UPLOAD_ERR_RESEND],
"");
126 foreach ($uploadFiles as $uploadedFile) {
128 $uploadedFile[
'file']->getSize() == 0 &&
129 $uploadedFile[
'file']->getError() == 0
131 return array(
false, $uploadErrors[UPLOAD_ERR_EMPTY],
"");
132 }
else if ($uploadedFile[
'file']->getSize() >= UploadedFile::getMaxFilesize()) {
133 return array(
false, $uploadErrors[UPLOAD_ERR_INI_SIZE] .
134 _(
" is really ") . $uploadedFile[
'file']->getSize() .
" bytes.",
"");
136 if (!$uploadedFile[
'file']->isValid()) {
137 return array(
false, $uploadedFile[
'file']->getErrorMessage(),
"");
141 if (empty($folderId)) {
142 return array(
false, $uploadErrors[UPLOAD_ERR_INVALID_FOLDER_PK],
"");
145 $setGlobal = ($request->get(
'globalDecisions')) ? 1 : 0;
147 $public = $request->get(
'public');
150 $uploadMode = (1 << 3);
153 $projectGroup = $GLOBALS[
'SysConf'][
'DIRECTORIES'][
'PROJECTGROUP'] ?:
'fossy';
157 foreach ($uploadFiles as $uploadedFile) {
158 $originalFileName = $uploadedFile[
'file']->getClientOriginalName();
159 $originalFileName = $this->basicShEscaping($originalFileName);
161 $uploadId =
JobAddUpload($userId, $groupId, $originalFileName,
162 $originalFileName, $uploadedFile[
'description'], $uploadMode,
163 $folderId, $publicPermission, $setGlobal);
164 if (empty($uploadId)) {
165 $errors[] = _(
"Failed to insert upload record: ") .
171 $uploadedTempFile = $uploadedFile[
'file']->move(
172 $uploadedFile[
'file']->getPath(),
173 $uploadedFile[
'file']->getFilename() .
'-uploaded'
175 }
catch (FileException $e) {
176 $errors[] = _(
"Could not save uploaded file: ") . $originalFileName;
180 "tempfile" => $uploadedTempFile,
181 "orignalfile" => $originalFileName,
182 "uploadid" => $uploadId
186 if (!empty($errors)) {
187 return [
false, implode(
" ; ", $errors),
""];
191 foreach ($success as $row) {
192 $uploadedTempFile = $row[
"tempfile"];
193 $originalFileName = $row[
"orignalfile"];
194 $uploadId = $row[
"uploadid"];
196 $wgetAgentCall =
"$MODDIR/wget_agent/agent/wget_agent -C -g " .
197 "$projectGroup -k $uploadId '$uploadedTempFile' -c '$SYSCONFDIR'";
198 $wgetOutput = array();
199 exec($wgetAgentCall, $wgetOutput, $wgetReturnValue);
200 unlink($uploadedTempFile);
202 if ($wgetReturnValue != 0) {
203 $message = implode(
' ', $wgetOutput);
204 if (empty($message)) {
205 $message = _(
"File upload failed. Error:") . $wgetReturnValue;
207 $errors[] = $message;
210 $messages[] = $this->postUploadAddJobs($reuseRequest, $originalFileName,
215 if (!empty($errors)) {
216 return [
false, implode(
" ; ", $errors),
""];
219 return array(
true, implode(
"", $messages),
"",
220 array_column($success,
"uploadid"));
229 $reuseRequest = clone $request;
231 $reuseSourceRaw = $request->get(
'reuseSource',
'local');
232 $reuseSource = is_array($reuseSourceRaw) ? reset($reuseSourceRaw) : $reuseSourceRaw;
234 $osselotPackageRaw = $request->get(
'osselotPackage');
235 $osselotPackage = is_array($osselotPackageRaw) ? reset($osselotPackageRaw) : $osselotPackageRaw;
237 if ($reuseSource ===
'osselot') {
238 $reuseRequest->request->set(
'osselotPackage', $osselotPackage);
240 $versionRadio = $request->get(
'osselotVersionRadio', []);
241 $selectedVersion =
'';
243 if (is_array($versionRadio)) {
244 $selectedVersion = reset($versionRadio);
245 } elseif (is_string($versionRadio)) {
246 $selectedVersion = $versionRadio;
249 if (empty($selectedVersion)) {
250 $hiddenVersions = $request->get(
'osselotVersions',
'');
251 if (!empty($hiddenVersions)) {
252 $selectedVersion = is_array($hiddenVersions) ? reset($hiddenVersions) : $hiddenVersions;
256 $reuseRequest->request->set(
'osselotVersions', $selectedVersion);
258 $osselotScalarFields = [
259 'osselotAddNewLicensesAs',
260 'osselotLicenseMatch'
263 foreach ($osselotScalarFields as $field) {
264 $value = $request->get($field);
265 $finalValue = is_array($value) ? reset($value) : $value;
266 $reuseRequest->request->set($field, $finalValue);
269 $osselotCheckboxFields = [
270 'osselotAddLicenseInfoFromInfoInFile',
271 'osselotAddLicenseInfoFromConcluded',
272 'osselotAddConcludedAsDecisions',
273 'osselotAddConcludedAsDecisionsOverwrite',
274 'osselotAddConcludedAsDecisionsTBD',
275 'osselotAddCopyrights'
278 foreach ($osselotCheckboxFields as $field) {
279 $value = $request->get($field);
282 if (is_array($value)) {
283 $firstValue = reset($value);
284 $finalValue = ($firstValue ===
'true' || $firstValue ===
true) ?
'true' :
null;
286 $finalValue = ($value ===
'true' || $value ===
true) ?
'true' :
null;
289 $reuseRequest->request->set($field, $finalValue);
292 $reuseRequest->request->set(
'reuseSource',
'osselot');
294 return $reuseRequest;
300 if (is_array($reuseSelector) && array_key_exists($originalFileName, $reuseSelector)) {
301 $reuseRequest->request->set(
303 $reuseSelector[$originalFileName]
306 if (is_array($reuseMode) && array_key_exists($originalFileName, $reuseMode)) {
307 $reuseRequest->request->set(
309 $reuseMode[$originalFileName]
312 return $reuseRequest;
316 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.