28 const NAME =
'upload_srv_files';
29 const NAME_PARAM =
'name';
30 const SOURCE_FILES_FIELD =
'sourceFiles';
32 public function __construct()
34 parent::__construct(self::NAME, array(
35 self::TITLE => _(
"Upload from Server"),
36 self::MENU_LIST =>
"Upload::From Server",
37 self::DEPENDENCIES => array(
"agent_unpack",
"showjobs"),
42 function check_if_host_is_allowed($host)
45 $sysConfig = $SysConf[
'SYSCONFIG'];
46 if (array_key_exists(
'UploadFromServerAllowedHosts', $sysConfig)) {
47 $hostListPre = $sysConfig[
'UploadFromServerAllowedHosts'];
48 $hostList = explode(
':', $hostListPre);
50 $hostList = array(
"localhost");
53 return in_array($host,$hostList);
68 $sysConfig = $SysConf[
'SYSCONFIG'];
69 if (array_key_exists(
'UploadFromServerWhitelist', $sysConfig)) {
70 $whitelistPre = $sysConfig[
'UploadFromServerWhitelist'];
71 $whitelist = explode(
':', $whitelistPre);
73 $whitelist = array(
"/tmp");
76 foreach ($whitelist as $item) {
77 if (substr($path, 0,strlen($item)) ===
trim($item)) {
98 if ($server ===
'localhost' || empty($server)) {
99 $temp_path = str_replace(
'\ ',
' ', $path);
100 return @fopen($temp_path, $persmission);
119 if ($server ===
'localhost' || empty($server)) {
120 $temp_path = str_replace(
'\ ',
' ', $path);
121 return file_exists($temp_path);
133 $vars[
'sourceFilesField'] = self::SOURCE_FILES_FIELD;
134 $vars[
'nameField'] = self::NAME_PARAM;
136 return $this->
render(
"upload_srv.html.twig", $this->mergeWithDefault($vars));
146 define(
"UPLOAD_ERR_INVALID_FOLDER_PK", 100);
147 define(
"UPLOAD_ERR_RESEND", 200);
148 $uploadErrors = array(
149 UPLOAD_ERR_INVALID_FOLDER_PK => _(
"Invalid Folder."),
150 UPLOAD_ERR_RESEND => _(
"This seems to be a resent file.")
153 $folderId = intval($request->get(self::FOLDER_PARAMETER_NAME));
154 $description = stripslashes($request->get(self::DESCRIPTION_INPUT_NAME));
155 $description = $this->basicShEscaping($description);
157 if ($request->getSession()->get(self::UPLOAD_FORM_BUILD_PARAMETER_NAME) !=
158 $request->get(self::UPLOAD_FORM_BUILD_PARAMETER_NAME)) {
159 return array(
false, $uploadErrors[UPLOAD_ERR_RESEND], $description);
162 if (empty($folderId)) {
163 return array(
false, $uploadErrors[UPLOAD_ERR_INVALID_FOLDER_PK], $description);
166 $public = $request->get(
'public');
169 $sourceFiles =
trim($request->get(self::SOURCE_FILES_FIELD));
170 $sourceFiles = $this->basicShEscaping($sourceFiles);
171 $host = $request->get(
'host') ?:
"localhost";
172 if (preg_match(
'/[^a-z.0-9]/i', $host)) {
173 $text = _(
"The given host is not valid.");
174 return array(
false, $text, $description);
176 if (! $this->check_if_host_is_allowed($host)) {
177 $text = _(
"You are not allowed to upload from the chosen host.");
178 return array(
false, $text,
183 $name = $request->get(self::NAME_PARAM);
185 if ((preg_match(
'/[*?%$]+/', $sourceFiles)) && empty($name)) {
187 "The file path contains a wildchar, you must provide a name for the upload.");
188 return array(
false, $text, $description);
192 $name = basename($sourceFiles);
194 $shortName = $this->basicShEscaping(basename($name));
195 if (empty($shortName)) {
198 if (strcmp($host,
"localhost")) {
199 $shortName = $host .
':' . $shortName;
203 $sourceFiles = str_replace(
'|',
'\|', $sourceFiles);
204 $sourceFiles = str_replace(
' ',
'\ ', $sourceFiles);
205 $sourceFiles = str_replace(
"\t",
"\\t", $sourceFiles);
206 if ($sourceFiles == FALSE) {
207 $text = _(
"failed to normalize/validate given path");
208 return array(
false, $text, $description);
211 $text = _(
"no suitable prefix found in the whitelist") .
", " .
212 _(
"you are not allowed to upload this file");
213 return array(
false, $text, $description);
217 $text = _(
"'$sourceFiles' does not exist.\n");
218 return array(
false, $text, $description);
222 $text = _(
"Have no READ permission on '$sourceFiles'.\n");
223 return array(
false, $text, $description);
226 filesize($sourceFiles) <= 0) {
227 $text = _(
"You can not upload an empty file.\n");
228 return array(
false, $text, $description);
232 $uploadMode = (1 << 3);
235 $uploadId =
JobAddUpload($userId, $groupId, $shortName, $sourceFiles,
236 $description, $uploadMode, $folderId, $publicPermission);
238 if (empty($uploadId)) {
239 $text = _(
"Failed to insert upload record");
240 return array(
false, $text, $description);
244 $jobpk = JobAddJob($userId, $groupId,
"wget", $uploadId);
245 if (empty($jobpk) || ($jobpk < 0)) {
246 $text = _(
"Failed to insert upload record");
247 return array(
false, $text, $description);
250 $jq_args =
"$uploadId - $sourceFiles";
252 $jobqueuepk =
JobQueueAdd($jobpk,
"wget_agent", $jq_args,
"no", NULL, $host);
253 if (empty($jobqueuepk)) {
254 $text = _(
"Failed to insert task 'wget' into job queue");
255 return array(
false, $text, $description);
261 $unpackplugin = &$Plugins[plugin_find_id(
"agent_unpack")];
262 $unpackArgs = intval($request->get(
'scm') == 1) ?
'-I' :
'';
263 $ununpack_jq_pk = $unpackplugin->AgentAdd($jobpk, $uploadId, $ErrorMsg, array(
"wget_agent"), $unpackargs);
264 if ($ununpack_jq_pk < 0) {
265 return array(
false, $text, _($ErrorMsg));
268 $adj2nestplugin = &$Plugins[plugin_find_id(
"agent_adj2nest")];
269 $adj2nest_jq_pk = $adj2nestplugin->AgentAdd($jobpk, $uploadId, $ErrorMsg, array());
270 if ($adj2nest_jq_pk < 0) {
271 return array(
false, $text, _($ErrorMsg));
279 if (empty($status)) {
280 $message .= _(
"Is the scheduler running? ");
283 $message .=
"The file $sourceFiles has been uploaded. ";
284 $keep =
"It is <a href='$Url'>upload #" . $uploadId .
"</a>.\n";
285 return array(
true, $message.$keep, $description, $uploadId);
GetRunnableJobList()
Get runnable job list, the process is below:
handleUpload(Request $request)
Process the upload request.
Traceback_uri()
Get the URI without query to this location.
static getUserId()
Get the current user's id.
check_by_whitelist($path)
checks, whether a normalized path starts with an path in the whiteliste
AgentCheckBoxDo($job_pk, $upload_pk)
Assume someone called AgentCheckBoxMake() and submitted the HTML form. Run AgentAdd() for each of the...
normalize_path($path, $host="localhost", $appendix="")
normalizes an path and returns FALSE on errors
render($templateName, $vars=null, $headers=null)
JobQueueAdd($job_pk, $jq_type, $jq_args, $jq_runonpfile, $Depends, $host=NULL, $jq_cmd_args=NULL)
Insert a jobqueue + jobdepends records.
handleView(Request $request, $vars)
remote_file_permission($path, $server= 'localhost', $persmission= 'r')
chck if one file/dir has one permission
path_is_pattern($path)
checks, whether a path is a pattern from the perspective of a shell
remote_file_exists($path, $server= 'localhost')
chck if one file/dir exist or not
HostListOption()
Get host list.
static getGroupId()
Get the current user's group id.
JobAddUpload($userId, $groupId, $job_name, $filename, $desc, $UploadMode, $folder_pk, $public_perm=Auth::PERM_NONE)
Insert a new upload record, and update the foldercontents table.
char * trim(char *ptext)
Trimming whitespace.