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);
75 $isLocalHost = ($host ===
'localhost' || empty($host));
77 $sysConfig = $SysConf[
'SYSCONFIG'];
78 if (array_key_exists(
'UploadFromServerWhitelist', $sysConfig)) {
79 $whitelistPre = $sysConfig[
'UploadFromServerWhitelist'];
80 $whitelist = explode(
':', $whitelistPre);
82 $whitelist = array(
"/tmp");
85 foreach ($whitelist as $item) {
86 if (
trim($item) ===
'/') {
101 $path = str_replace(
'\ ',
' ', $path);
104 if (preg_match(
'#(^|/)\.\.(/|$)#', $path)) {
108 foreach ($whitelist as $item) {
109 $item = rtrim(
trim($item),
'/');
113 if ($item ===
'' || $item[0] !==
'/' ||
114 preg_match(
'#(^|/)\.\.(/|$)#', $item)) {
117 $prefixes = array($item);
118 $realItem = $isLocalHost ? realpath($item) :
false;
119 if ($realItem ===
'/') {
122 if ($realItem !==
false && $realItem !== $item) {
123 $prefixes[] = rtrim($realItem,
'/');
125 foreach ($prefixes as $prefix) {
126 if ($prefix !==
'' &&
127 ($path === $prefix || strpos($path, $prefix .
'/') === 0)) {
149 if ($server ===
'localhost' || empty($server)) {
150 $temp_path = str_replace(
'\ ',
' ', $path);
151 return @fopen($temp_path, $persmission);
170 if ($server ===
'localhost' || empty($server)) {
171 $temp_path = str_replace(
'\ ',
' ', $path);
172 return file_exists($temp_path);
185 $vars[
'sourceFilesField'] = self::SOURCE_FILES_FIELD;
186 $vars[
'nameField'] = self::NAME_PARAM;
188 return $this->
render(
"upload_srv.html.twig", $this->mergeWithDefault($vars));
198 define(
"UPLOAD_ERR_INVALID_FOLDER_PK", 100);
199 define(
"UPLOAD_ERR_RESEND", 200);
200 $uploadErrors = array(
201 UPLOAD_ERR_INVALID_FOLDER_PK => _(
"Invalid Folder."),
202 UPLOAD_ERR_RESEND => _(
"This seems to be a resent file.")
205 $folderId = intval($request->get(self::FOLDER_PARAMETER_NAME));
206 $description = stripslashes($request->get(self::DESCRIPTION_INPUT_NAME));
207 $description = $this->basicShEscaping($description);
209 if ($request->getSession()->get(self::UPLOAD_FORM_BUILD_PARAMETER_NAME) !=
210 $request->get(self::UPLOAD_FORM_BUILD_PARAMETER_NAME)) {
211 return array(
false, $uploadErrors[UPLOAD_ERR_RESEND], $description);
214 if (empty($folderId)) {
215 return array(
false, $uploadErrors[UPLOAD_ERR_INVALID_FOLDER_PK], $description);
218 $setGlobal = ($request->get(
'globalDecisions')) ? 1 : 0;
220 $public = $request->get(
'public');
223 $sourceFiles =
trim($request->get(self::SOURCE_FILES_FIELD));
224 $sourceFiles = $this->basicShEscaping($sourceFiles);
225 $host = $request->get(
'host') ?:
"localhost";
226 if (preg_match(
'/[^a-z.0-9]/i', $host)) {
227 $text = _(
"The given host is not valid.");
228 return array(
false, $text, $description);
230 if (! $this->check_if_host_is_allowed($host)) {
231 $text = _(
"You are not allowed to upload from the chosen host.");
232 return array(
false, $text,
237 $name = $request->get(self::NAME_PARAM);
239 if ((preg_match(
'/[*?%$]+/', $sourceFiles)) && empty($name)) {
241 "The file path contains a wildchar, you must provide a name for the upload.");
242 return array(
false, $text, $description);
246 $name = basename($sourceFiles);
248 $shortName = $this->basicShEscaping(basename($name));
249 if (empty($shortName)) {
252 if (strcmp($host,
"localhost")) {
253 $shortName = $host .
':' . $shortName;
257 $sourceFiles = str_replace(
'|',
'\|', $sourceFiles);
258 $sourceFiles = str_replace(
' ',
'\ ', $sourceFiles);
259 $sourceFiles = str_replace(
"\t",
"\\t", $sourceFiles);
260 if ($sourceFiles == FALSE) {
261 $text = _(
"failed to normalize/validate given path");
262 return array(
false, $text, $description);
265 $text = _(
"no suitable prefix found in the whitelist") .
", " .
266 _(
"you are not allowed to upload this file");
267 return array(
false, $text, $description);
271 $text = _(
"'$sourceFiles' does not exist.\n");
272 return array(
false, $text, $description);
276 $text = _(
"Have no READ permission on '$sourceFiles'.\n");
277 return array(
false, $text, $description);
280 filesize($sourceFiles) <= 0) {
281 $text = _(
"You can not upload an empty file.\n");
282 return array(
false, $text, $description);
286 $uploadMode = (1 << 3);
289 $uploadId =
JobAddUpload($userId, $groupId, $shortName, $sourceFiles,
290 $description, $uploadMode, $folderId, $publicPermission, $setGlobal);
292 if (empty($uploadId)) {
293 $text = _(
"Failed to insert upload record");
294 return array(
false, $text, $description);
298 $jobpk = JobAddJob($userId, $groupId,
"wget", $uploadId);
299 if (empty($jobpk) || ($jobpk < 0)) {
300 $text = _(
"Failed to insert upload record");
301 return array(
false, $text, $description);
304 $jq_args =
"$uploadId - $sourceFiles";
306 $jobqueuepk =
JobQueueAdd($jobpk,
"wget_agent", $jq_args,
"no", NULL, $host);
307 if (empty($jobqueuepk)) {
308 $text = _(
"Failed to insert task 'wget' into job queue");
309 return array(
false, $text, $description);
315 $unpackplugin = &$Plugins[plugin_find_id(
"agent_unpack")];
316 $unpackargs = intval($request->get(
'scm') == 1) ?
'-I' :
'';
317 $ununpack_jq_pk = $unpackplugin->AgentAdd($jobpk, $uploadId, $ErrorMsg, array(
"wget_agent"), $unpackargs);
318 if ($ununpack_jq_pk < 0) {
319 return array(
false, $text, _($ErrorMsg));
322 $adj2nestplugin = &$Plugins[plugin_find_id(
"agent_adj2nest")];
323 $adj2nest_jq_pk = $adj2nestplugin->AgentAdd($jobpk, $uploadId, $ErrorMsg, array());
324 if ($adj2nest_jq_pk < 0) {
325 return array(
false, $text, _($ErrorMsg));
328 $message = $this->postUploadAddJobs($request, $Name, $uploadId, $jobpk);
329 return array(
true, $message.$keep, $description, $uploadId);
332 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
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
check_by_whitelist($path, $host='localhost')
checks, whether a normalized path lies inside one of the directories in the whitelist
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.