9 namespace Fossology\UI\Page;
13 use Symfony\Component\HttpFoundation\Request;
17 const NAME =
'upload_srv_files';
18 const NAME_PARAM =
'name';
19 const SOURCE_FILES_FIELD =
'sourceFiles';
21 public function __construct()
23 parent::__construct(self::NAME, array(
24 self::TITLE => _(
"Upload from Server"),
25 self::MENU_LIST =>
"Upload::From Server",
26 self::DEPENDENCIES => array(
"agent_unpack",
"showjobs"),
31 function check_if_host_is_allowed($host)
34 $sysConfig = $SysConf[
'SYSCONFIG'];
35 if (array_key_exists(
'UploadFromServerAllowedHosts', $sysConfig)) {
36 $hostListPre = $sysConfig[
'UploadFromServerAllowedHosts'];
37 $hostList = explode(
':', $hostListPre);
39 $hostList = array(
"localhost");
42 return in_array($host,$hostList);
57 $sysConfig = $SysConf[
'SYSCONFIG'];
58 if (array_key_exists(
'UploadFromServerWhitelist', $sysConfig)) {
59 $whitelistPre = $sysConfig[
'UploadFromServerWhitelist'];
60 $whitelist = explode(
':', $whitelistPre);
62 $whitelist = array(
"/tmp");
65 foreach ($whitelist as $item) {
66 if (substr($path, 0,strlen($item)) ===
trim($item)) {
87 if ($server ===
'localhost' || empty($server)) {
88 $temp_path = str_replace(
'\ ',
' ', $path);
89 return @fopen($temp_path, $persmission);
108 if ($server ===
'localhost' || empty($server)) {
109 $temp_path = str_replace(
'\ ',
' ', $path);
110 return file_exists($temp_path);
123 $vars[
'sourceFilesField'] = self::SOURCE_FILES_FIELD;
124 $vars[
'nameField'] = self::NAME_PARAM;
126 return $this->
render(
"upload_srv.html.twig", $this->mergeWithDefault($vars));
136 define(
"UPLOAD_ERR_INVALID_FOLDER_PK", 100);
137 define(
"UPLOAD_ERR_RESEND", 200);
138 $uploadErrors = array(
139 UPLOAD_ERR_INVALID_FOLDER_PK => _(
"Invalid Folder."),
140 UPLOAD_ERR_RESEND => _(
"This seems to be a resent file.")
143 $folderId = intval($request->get(self::FOLDER_PARAMETER_NAME));
144 $description = stripslashes($request->get(self::DESCRIPTION_INPUT_NAME));
145 $description = $this->basicShEscaping($description);
147 if ($request->getSession()->get(self::UPLOAD_FORM_BUILD_PARAMETER_NAME) !=
148 $request->get(self::UPLOAD_FORM_BUILD_PARAMETER_NAME)) {
149 return array(
false, $uploadErrors[UPLOAD_ERR_RESEND], $description);
152 if (empty($folderId)) {
153 return array(
false, $uploadErrors[UPLOAD_ERR_INVALID_FOLDER_PK], $description);
156 $setGlobal = ($request->get(
'globalDecisions')) ? 1 : 0;
158 $public = $request->get(
'public');
161 $sourceFiles =
trim($request->get(self::SOURCE_FILES_FIELD));
162 $sourceFiles = $this->basicShEscaping($sourceFiles);
163 $host = $request->get(
'host') ?:
"localhost";
164 if (preg_match(
'/[^a-z.0-9]/i', $host)) {
165 $text = _(
"The given host is not valid.");
166 return array(
false, $text, $description);
168 if (! $this->check_if_host_is_allowed($host)) {
169 $text = _(
"You are not allowed to upload from the chosen host.");
170 return array(
false, $text,
175 $name = $request->get(self::NAME_PARAM);
177 if ((preg_match(
'/[*?%$]+/', $sourceFiles)) && empty($name)) {
179 "The file path contains a wildchar, you must provide a name for the upload.");
180 return array(
false, $text, $description);
184 $name = basename($sourceFiles);
186 $shortName = $this->basicShEscaping(basename($name));
187 if (empty($shortName)) {
190 if (strcmp($host,
"localhost")) {
191 $shortName = $host .
':' . $shortName;
195 $sourceFiles = str_replace(
'|',
'\|', $sourceFiles);
196 $sourceFiles = str_replace(
' ',
'\ ', $sourceFiles);
197 $sourceFiles = str_replace(
"\t",
"\\t", $sourceFiles);
198 if ($sourceFiles == FALSE) {
199 $text = _(
"failed to normalize/validate given path");
200 return array(
false, $text, $description);
203 $text = _(
"no suitable prefix found in the whitelist") .
", " .
204 _(
"you are not allowed to upload this file");
205 return array(
false, $text, $description);
209 $text = _(
"'$sourceFiles' does not exist.\n");
210 return array(
false, $text, $description);
214 $text = _(
"Have no READ permission on '$sourceFiles'.\n");
215 return array(
false, $text, $description);
218 filesize($sourceFiles) <= 0) {
219 $text = _(
"You can not upload an empty file.\n");
220 return array(
false, $text, $description);
224 $uploadMode = (1 << 3);
227 $uploadId =
JobAddUpload($userId, $groupId, $shortName, $sourceFiles,
228 $description, $uploadMode, $folderId, $publicPermission, $setGlobal);
230 if (empty($uploadId)) {
231 $text = _(
"Failed to insert upload record");
232 return array(
false, $text, $description);
236 $jobpk = JobAddJob($userId, $groupId,
"wget", $uploadId);
237 if (empty($jobpk) || ($jobpk < 0)) {
238 $text = _(
"Failed to insert upload record");
239 return array(
false, $text, $description);
242 $jq_args =
"$uploadId - $sourceFiles";
244 $jobqueuepk =
JobQueueAdd($jobpk,
"wget_agent", $jq_args,
"no", NULL, $host);
245 if (empty($jobqueuepk)) {
246 $text = _(
"Failed to insert task 'wget' into job queue");
247 return array(
false, $text, $description);
253 $unpackplugin = &$Plugins[plugin_find_id(
"agent_unpack")];
254 $unpackargs = intval($request->get(
'scm') == 1) ?
'-I' :
'';
255 $ununpack_jq_pk = $unpackplugin->AgentAdd($jobpk, $uploadId, $ErrorMsg, array(
"wget_agent"), $unpackargs);
256 if ($ununpack_jq_pk < 0) {
257 return array(
false, $text, _($ErrorMsg));
260 $adj2nestplugin = &$Plugins[plugin_find_id(
"agent_adj2nest")];
261 $adj2nest_jq_pk = $adj2nestplugin->AgentAdd($jobpk, $uploadId, $ErrorMsg, array());
262 if ($adj2nest_jq_pk < 0) {
263 return array(
false, $text, _($ErrorMsg));
266 $message = $this->postUploadAddJobs($request, $Name, $uploadId, $jobpk);
267 return array(
true, $message.$keep, $description, $uploadId);
270 register_plugin(
new UploadSrvPage());
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)
path_is_pattern($path)
checks, whether a path is a pattern from the perspective of a shell
normalize_path($path, $host="localhost", $appendix="")
normalizes an path and returns FALSE on errors
check_by_whitelist($path)
checks, whether a normalized path starts with an path in the whiteliste
handleView(Request $request, $vars)
handleUpload(Request $request)
Process the upload request.
remote_file_permission($path, $server='localhost', $persmission='r')
chck if one file/dir has one permission
remote_file_exists($path, $server='localhost')
chck if one file/dir exist or not
JobQueueAdd($job_pk, $jq_type, $jq_args, $jq_runonpfile, $Depends, $host=NULL, $jq_cmd_args=NULL)
Insert a jobqueue + jobdepends records.
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.
HostListOption()
Get host list.
char * trim(char *ptext)
Trimming whitespace.