protected function handleUpload(Request $request) { $folderId = intval($request->get(self::FOLDER_PARAMETER_NAME)); $description = stripslashes($request->get(self::DESCRIPTION_INPUT_NAME)); $description = $this->basicShEscaping($description); $getUrlThatMightIncludeSpaces = trim($request->get(self::GETURL_PARAM)); $getURL = str_replace(" ", "%20", $getUrlThatMightIncludeSpaces); if (empty($getURL)) { return array(false, _("Invalid URL"), $description); } if (preg_match("@^((http)|(https)|(ftp))://([[:alnum:]]+)@i", $getURL) != 1) { return array(false, _("Invalid URL"), $description); } $getUrl = $this->basicShEscaping($getUrl); $name = $request->get(self::NAME_PARAM); if (empty($name)) { $name = basename($getURL); } $shortName = basename($name); if (empty($shortName)) { $shortName = $name; } /* Create an upload record. */ $mode = 1 << 2; // code for "it came from wget" $userId = Auth::getUserId(); $groupId = Auth::getGroupId(); $public = $request->get('public'); $publicPermission = $public == self::PUBLIC_ALL ? Auth::PERM_READ : Auth::PERM_NONE; $uploadId = JobAddUpload($userId, $groupId, $shortName, $getURL, $description, $mode, $folderId, $publicPermission); if (empty($uploadId)) { $text = _("Failed to insert upload record"); return array(false, $text, $description); } $level = intval($request->get(self::LEVEL_PARAM)); if ($level < 0) { $level = 1; } /* first trim, then get rid of whitespaces before and after each comma letter */ $accept = preg_replace('/\\s*,\\s*/', ',', trim($request->get(self::ACCEPT_PARAM))); $accept = $this->basicShEscaping($accept); $reject = preg_replace('/\\s*,\\s*/', ',', trim($request->get(self::REJECT_PARAM))); $reject = $this->basicShEscaping($reject); /* Create the job: job "wget" */ $jobId = JobAddJob($userId, $groupId, "wget", $uploadId); if (empty($jobId) || $jobId < 0) { return array(false, _("Failed to insert job record"), $description); } $jqArgs = "{$uploadId} - {$getURL} -l {$level} "; if (!empty($accept)) { $jqArgs .= "-A {$accept} "; } $jqArgs .= empty($reject) ? "-R index.html* " : "-R {$reject},index.html* "; $jobqueueId = JobQueueAdd($jobId, "wget_agent", $jqArgs, NULL, NULL); if (empty($jobqueueId)) { return array(false, "Failed to insert task 'wget_agent' into job queue", $description); } $message = $this->postUploadAddJobs($request, $shortName, $uploadId, $jobId, $jobqueueId); return array(true, $message, $description); }
/** * \brief Given one object (file or URL), upload it. * * \param $FolderPath - folder path * \param $UploadArchive - upload file(absolute path) or url * \param $UploadName - uploaded file/dir name * \param $UploadDescription - upload description * * \return 1: error, 0: success */ function UploadOne($FolderPath, $UploadArchive, $UploadName, $UploadDescription, $TarSource = NULL) { global $Verbose; global $Test; global $QueueList; global $fossjobs_command; global $public_flag; global $SysConf; global $PG_CONN; global $VCS; global $vcsuser; global $vcspass; global $TarExcludeList; $jobqueuepk = 0; if (empty($UploadName)) { $text = "UploadName is empty\n"; echo $text; return 1; } $user_pk = $SysConf['auth']['UserId']; $group_pk = $SysConf['auth']['GroupId']; /* Get the user record and check the PLUGIN_DB_ level to make sure they have at least write access */ $UsersRow = GetSingleRec("users", "where user_pk={$user_pk}"); if ($UsersRow["user_perm"] < PLUGIN_DB_WRITE) { print "You have no permission to upload files into FOSSology\n"; return 1; } /* Get the folder's primary key */ $root_folder_fk = $UsersRow["root_folder_fk"]; global $OptionA; /* Should it use bucket names? */ if ($OptionA) { global $bucket_size; $FolderPath .= "/" . GetBucketFolder($UploadName, $bucket_size); } $FolderPk = GetFolder($FolderPath, $root_folder_fk); if ($FolderPk == 1) { print " Uploading to folder: 'Software Repository'\n"; } else { print " Uploading to folder: '{$FolderPath}'\n"; } print " Uploading as '{$UploadName}'\n"; if (!empty($UploadDescription)) { print " Upload description: '{$UploadDescription}'\n"; } $Mode = 1 << 3; // code for "it came from web upload" /* Create the upload for the file */ if ($Verbose) { print "JobAddUpload({$user_pk}, {$group_pk}, {$UploadName},{$UploadArchive},{$UploadDescription},{$Mode},{$FolderPk}, {$public_flag});\n"; } if (!$Test) { $Src = $UploadArchive; if (!empty($TarSource)) { $Src = $TarSource; } $UploadPk = JobAddUpload($user_pk, $group_pk, $UploadName, $Src, $UploadDescription, $Mode, $FolderPk, $public_flag); print " UploadPk is: '{$UploadPk}'\n"; } /* Prepare the job: job "wget" */ if ($Verbose) { print "JobAddJob({$user_pk}, {$group_pk}, wget, {$UploadPk});\n"; } if (!$Test) { $jobpk = JobAddJob($user_pk, $group_pk, "wget", $UploadPk); if (empty($jobpk) || $jobpk < 0) { $text = _("Failed to insert job record"); echo $text; return 1; } } $jq_args = "{$UploadPk} - {$Src}"; if ($TarExcludeList) { $jq_args .= " " . $TarExcludeList; } if ($VCS) { $jq_args .= " " . $VCS; } // add flags when upload from version control system if ($vcsuser && $vcspass) { $jq_args .= " --username {$vcsuser} --password {$vcspass} "; } if ($Verbose) { print "JobQueueAdd({$jobpk}, wget_agent, {$jq_args}, no, NULL);\n"; } if (!$Test) { $jobqueuepk = JobQueueAdd($jobpk, "wget_agent", $jq_args, "no", NULL); if (empty($jobqueuepk)) { $text = _("Failed to insert task 'wget' into job queue"); echo $text; return 1; } } /* schedule agents */ global $Plugins; if ($Verbose) { print "AgentAdd wget_agent and dj2nest.\n"; } if (!$Test) { $unpackplugin =& $Plugins[plugin_find_id("agent_unpack")]; $ununpack_jq_pk = $unpackplugin->AgentAdd($jobpk, $UploadPk, $ErrorMsg, array("wget_agent")); if ($ununpack_jq_pk < 0) { echo $ErrorMsg; return 1; } $adj2nestplugin =& $Plugins[plugin_find_id("agent_adj2nest")]; $adj2nest_jq_pk = $adj2nestplugin->AgentAdd($jobpk, $UploadPk, $ErrorMsg, array()); if ($adj2nest_jq_pk < 0) { echo $ErrorMsg; return 1; } } if (!empty($QueueList)) { switch ($QueueList) { case 'ALL': case 'all': $Cmd = "{$fossjobs_command} -U '{$UploadPk}'"; break; default: $Cmd = "{$fossjobs_command} -U '{$UploadPk}' -A '{$QueueList}'"; break; } if ($Verbose) { print "CMD={$Cmd}\n"; } if (!$Test) { system($Cmd); } } else { /* No other agents other than unpack scheduled, attach to unpack*/ } global $OptionS; /* Should it run synchronously? */ if ($OptionS) { $working = true; $waitCount = 0; while ($working && $waitCount++ < 30) { sleep(3); $SQL = "select * from jobqueue inner join job on job.job_pk = jobqueue.jq_job_fk where job_upload_fk = '{$UploadPk}' and jq_end_bits = 0 and jq_type = 'wget_agent'"; $result = pg_query($PG_CONN, $SQL); DBCheckResult($result, $SQL, __FILE__, __LINE__); $row_count = pg_num_rows($result); pg_free_result($result); if ($row_count == 0) { $working = false; } } if ($working) { echo "Gave up waiting for copy completion. Is the scheduler running?"; return 1; } } }
/** * \brief Process the upload request. * \param $Folder * \param $GetURL * \param $Desc * \param $Name * \param $Accept * \param $Reject * \param $Level * \param $public_perm public permission on the upload * Returns NULL on success, string on failure. */ function Upload($Folder, $GetURL, $Desc, $Name, $Accept, $Reject, $Level, $public_perm) { global $SysConf; /* See if the URL looks valid */ if (empty($Folder)) { $text = _("Invalid folder"); return $text; } $GetURL = trim($GetURL); if (empty($GetURL)) { $text = _("Invalid URL"); return $text; } if (preg_match("@^((http)|(https)|(ftp))://([[:alnum:]]+)@i", $GetURL) != 1) { $text = _("Invalid URL"); return "{$text}: " . htmlentities($GetURL); } if (preg_match("@[[:space:]]@", $GetURL) != 0) { $text = _("Invalid URL (no spaces permitted)"); return "{$text}: " . htmlentities($GetURL); } if (empty($Name)) { $Name = basename($GetURL); } $ShortName = basename($Name); if (empty($ShortName)) { $ShortName = $Name; } /* Create an upload record. */ $Mode = 1 << 2; // code for "it came from wget" $user_pk = $SysConf['auth']['UserId']; $uploadpk = JobAddUpload($user_pk, $ShortName, $GetURL, $Desc, $Mode, $Folder, $public_perm); if (empty($uploadpk)) { $text = _("Failed to insert upload record"); return $text; } /* Set default values */ if (empty($Level) && !is_numeric($Level) || $Level < 0) { $Level = 1; } /* first trim, then get rid of whitespaces before and after each comma letter */ $Accept = preg_replace('/\\s*,\\s*/', ',', trim($Accept)); $Reject = preg_replace('/\\s*,\\s*/', ',', trim($Reject)); /* Create the job: job "wget" */ $jobpk = JobAddJob($user_pk, "wget", $uploadpk); if (empty($jobpk) || $jobpk < 0) { $text = _("Failed to insert job record"); return $text; } $jq_args = "{$uploadpk} - {$GetURL} -l {$Level} "; if (!empty($Accept)) { $jq_args .= "-A {$Accept} "; } if (!empty($Reject)) { // reject the files index.html* $jq_args .= "-R {$Reject},index.html* "; } else { $jq_args .= "-R index.html* "; } $jobqueuepk = JobQueueAdd($jobpk, "wget_agent", $jq_args, NULL, NULL); if (empty($jobqueuepk)) { $text = _("Failed to insert task 'wget_agent' into job queue"); return $text; } global $Plugins; /* schedule agents */ $unpackplugin =& $Plugins[plugin_find_id("agent_unpack")]; $ununpack_jq_pk = $unpackplugin->AgentAdd($jobpk, $uploadpk, $ErrorMsg, array("wget_agent")); if ($ununpack_jq_pk < 0) { return $ErrorMsg; } $adj2nestplugin =& $Plugins[plugin_find_id("agent_adj2nest")]; $adj2nest_jq_pk = $adj2nestplugin->AgentAdd($jobpk, $uploadpk, $ErrorMsg, array()); if ($adj2nest_jq_pk < 0) { return $ErrorMsg; } AgentCheckBoxDo($jobpk, $uploadpk); $msg = ""; /** check if the scheudler is running */ $status = GetRunnableJobList(); if (empty($status)) { $msg .= _("Is the scheduler running? "); } $Url = Traceback_uri() . "?mod=showjobs&upload={$uploadpk}"; $text = _("The upload"); $text1 = _("has been queued. It is"); $msg .= "{$text} {$Name} {$text1} "; $keep = "<a href='{$Url}'>upload #" . $uploadpk . "</a>.\n"; print displayMessage($msg, $keep); return NULL; }
/** * @brief Process the upload request. */ protected function handleUpload(Request $request) { global $MODDIR; global $SYSCONFDIR; define("UPLOAD_ERR_EMPTY", 5); define("UPLOAD_ERR_INVALID_FOLDER_PK", 100); define("UPLOAD_ERR_RESEND", 200); $uploadErrors = array(UPLOAD_ERR_OK => _("No errors."), UPLOAD_ERR_INI_SIZE => _("Larger than upload_max_filesize ") . ini_get('upload_max_filesize'), UPLOAD_ERR_FORM_SIZE => _("Larger than form MAX_FILE_SIZE."), UPLOAD_ERR_PARTIAL => _("Partial upload."), UPLOAD_ERR_NO_FILE => _("No file selected."), UPLOAD_ERR_NO_TMP_DIR => _("No temporary directory."), UPLOAD_ERR_CANT_WRITE => _("Can't write to disk."), UPLOAD_ERR_EXTENSION => _("File upload stopped by extension."), UPLOAD_ERR_EMPTY => _("File is empty or you don't have permission to read the file."), UPLOAD_ERR_INVALID_FOLDER_PK => _("Invalid Folder."), UPLOAD_ERR_RESEND => _("This seems to be a resent file.")); $folderId = intval($request->get(self::FOLDER_PARAMETER_NAME)); $description = stripslashes($request->get(self::DESCRIPTION_INPUT_NAME)); $description = $this->basicShEscaping($description); $uploadedFile = $request->files->get(self::FILE_INPUT_NAME); if ($uploadedFile === null) { return array(false, $uploadErrors[UPLOAD_ERR_NO_FILE], $description); } if ($request->getSession()->get(self::UPLOAD_FORM_BUILD_PARAMETER_NAME) != $request->get(self::UPLOAD_FORM_BUILD_PARAMETER_NAME)) { return array(false, $uploadErrors[UPLOAD_ERR_RESEND], $description); } if ($uploadedFile->getSize() == 0 && $uploadedFile->getError() == 0) { return array(false, $uploadErrors[UPLOAD_ERR_EMPTY], $description); } else { if ($uploadedFile->getSize() >= UploadedFile::getMaxFilesize()) { return array(false, $uploadErrors[UPLOAD_ERR_INI_SIZE] . _(" is really ") . $uploadedFile->getSize() . " bytes.", $description); } } if (empty($folderId)) { return array(false, $uploadErrors[UPLOAD_ERR_INVALID_FOLDER_PK], $description); } if (!$uploadedFile->isValid()) { return array(false, $uploadedFile->getErrorMessage(), $description); } $originalFileName = $uploadedFile->getClientOriginalName(); $originalFileName = $this->basicShEscaping($originalFileName); $public = $request->get('public'); $publicPermission = $public == self::PUBLIC_ALL ? Auth::PERM_READ : Auth::PERM_NONE; /* Create an upload record. */ $uploadMode = 1 << 3; // code for "it came from web upload" $userId = Auth::getUserId(); $groupId = Auth::getGroupId(); $uploadId = JobAddUpload($userId, $groupId, $originalFileName, $originalFileName, $description, $uploadMode, $folderId, $publicPermission); if (empty($uploadId)) { return array(false, _("Failed to insert upload record"), $description); } try { $uploadedTempFile = $uploadedFile->move($uploadedFile->getPath(), $uploadedFile->getFilename() . '-uploaded')->getPathname(); } catch (FileException $e) { return array(false, _("Could not save uploaded file"), $description); } $projectGroup = $GLOBALS['SysConf']['DIRECTORIES']['PROJECTGROUP'] ?: 'fossy'; $wgetAgentCall = "{$MODDIR}/wget_agent/agent/wget_agent -C -g {$projectGroup} -k {$uploadId} '{$uploadedTempFile}' -c '{$SYSCONFDIR}'"; $wgetOutput = array(); exec($wgetAgentCall, $wgetOutput, $wgetReturnValue); unlink($uploadedTempFile); if ($wgetReturnValue != 0) { $message = implode(' ', $wgetOutput); if (empty($message)) { $message = _("File upload failed. Error:") . $wgetReturnValue; } return array(false, $message, $description); } $message = $this->postUploadAddJobs($request, $originalFileName, $uploadId); return array(true, $message, $description); }
/** * @brief Process the upload request. */ protected function handleUpload(Request $request) { global $MODDIR; global $SYSCONFDIR; global $Plugins; $folderId = intval($request->get(self::FOLDER_PARAMETER_NAME)); $description = stripslashes($request->get(self::DESCRIPTION_INPUT_NAME)); $description = $this->basicShEscaping($description); $getUrlThatMightIncludeSpaces = trim($request->get(self::GETURL_PARAM)); $getUrl = str_replace(" ", "%20", $getUrlThatMightIncludeSpaces); if (empty($getUrl)) { return array(false, _("Empty URL") . $getUrl, $description); } if (preg_match("@^((http)|(https))://([[:alnum:]]+)@i", $getUrl) != 1) { return array(false, _("Invalid URL") . $getUrl, $description); } $getUrl = $this->basicShEscaping($getUrl); if ($request->getSession()->get(self::UPLOAD_FORM_BUILD_PARAMETER_NAME) != $request->get(self::UPLOAD_FORM_BUILD_PARAMETER_NAME)) { $text = _("This seems to be a resent file."); return array(false, $text, $description); } if (empty($folderId)) { $text = _("Invalid Folder."); return array(false, $text, $description); } $public = $request->get('public'); $publicPermission = $public == self::PUBLIC_ALL ? Auth::PERM_READ : Auth::PERM_NONE; $Name = trim($request->get('name')); if (empty($Name)) { $Name = basename($getUrl); } $ShortName = basename($Name); if (empty($ShortName)) { $ShortName = $Name; } /* Create an upload record. */ $uploadMode = 1 << 2; // code for "it came from wget" $userId = Auth::getUserId(); $groupId = Auth::getGroupId(); $uploadId = JobAddUpload($userId, $groupId, $ShortName, $getUrl, $description, $uploadMode, $folderId, $publicPermission); if (empty($uploadId)) { $text = _("Failed to insert upload record"); return array(false, $text, $description); } /* Create the job: job "wget" */ $jobpk = JobAddJob($userId, $groupId, "wget", $uploadId); if (empty($jobpk) || $jobpk < 0) { $text = _("Failed to insert job record"); return array(false, $text, $description); } $VCSType = trim($request->get('vcstype')); $VCSType = $this->basicShEscaping($VCSType); $jq_args = "{$uploadId} - {$getUrl} {$VCSType} "; $Username = trim($request->get('username')); $Username = $this->basicShEscaping($Username); if (!empty($Username)) { $jq_args .= "--username {$Username} "; } $Passwd = trim($request->get('passwd')); $Passwd = $this->basicShEscaping($Passwd); if (!empty($Passwd)) { $jq_args .= "--password {$Passwd}"; } $jobqueuepk = JobQueueAdd($jobpk, "wget_agent", $jq_args, NULL, NULL); if (empty($jobqueuepk)) { $text = _("Failed to insert task 'wget_agent' into job queue"); return array(false, $text, $description); } /* schedule agents */ $unpackplugin =& $Plugins[plugin_find_id("agent_unpack")]; $ununpack_jq_pk = $unpackplugin->AgentAdd($jobpk, $uploadId, $ErrorMsg, array("wget_agent")); if ($ununpack_jq_pk < 0) { return array(false, _($ErrorMsg), $description); } $adj2nestplugin =& $Plugins[plugin_find_id("agent_adj2nest")]; $adj2nest_jq_pk = $adj2nestplugin->AgentAdd($jobpk, $uploadId, $ErrorMsg, array()); if ($adj2nest_jq_pk < 0) { return array(false, _($ErrorMsg), $description); } AgentCheckBoxDo($jobpk, $uploadId); $msg = ""; /** check if the scheudler is running */ $status = GetRunnableJobList(); if (empty($status)) { $msg .= _("Is the scheduler running? "); } $Url = Traceback_uri() . "?mod=showjobs&upload={$uploadId}"; $text = _("The upload"); $text1 = _("has been queued. It is"); $msg .= "{$text} {$Name} {$text1} "; $keep = "<a href='{$Url}'>upload #" . $uploadId . "</a>.\n"; return array(true, $msg . $keep, $description); }
/** * @brief Process the upload request. */ protected function handleUpload(Request $request) { global $Plugins; define("UPLOAD_ERR_INVALID_FOLDER_PK", 100); define("UPLOAD_ERR_RESEND", 200); $uploadErrors = array(UPLOAD_ERR_INVALID_FOLDER_PK => _("Invalid Folder."), UPLOAD_ERR_RESEND => _("This seems to be a resent file.")); $folderId = intval($request->get(self::FOLDER_PARAMETER_NAME)); $description = stripslashes($request->get(self::DESCRIPTION_INPUT_NAME)); $description = $this->basicShEscaping($description); if ($request->getSession()->get(self::UPLOAD_FORM_BUILD_PARAMETER_NAME) != $request->get(self::UPLOAD_FORM_BUILD_PARAMETER_NAME)) { return array(false, $uploadErrors[UPLOAD_ERR_RESEND], $description); } if (empty($folderId)) { return array(false, $uploadErrors[UPLOAD_ERR_INVALID_FOLDER_PK], $description); } $public = $request->get('public'); $publicPermission = $public == self::PUBLIC_ALL ? Auth::PERM_READ : Auth::PERM_NONE; $sourceFiles = trim($request->get(self::SOURCE_FILES_FIELD)); $sourceFiles = $this->basicShEscaping($sourceFiles); $host = $request->get('host') ?: "localhost"; if (preg_match('/[^a-z.0-9]/i', $host)) { $text = _("The given host is not valid."); return array(false, $text, $description); } if (!$this->check_if_host_is_allowed($host)) { $text = _("You are not allowed to upload from the chosen host."); return array(false, $text, $description); } $shortName = basename($sourceFiles); if (empty($shortName)) { $shortName = $sourceFiles; } if (strcmp($host, "localhost")) { $shortName = $host . ':' . $shortName; } $sourceFiles = $this->normalize_path($sourceFiles, $host); $sourceFiles = str_replace('|', '\\|', $sourceFiles); $sourceFiles = str_replace(' ', '\\ ', $sourceFiles); $sourceFiles = str_replace("\t", "\\t", $sourceFiles); if ($sourceFiles == FALSE) { $text = _("failed to normalize/validate given path"); return array(false, $text, $description); } if ($this->check_by_whitelist($sourceFiles) === FALSE) { $text = _("no suitable prefix found in the whitelist") . ", " . _("you are not allowed to upload this file"); return array(false, $text, $description); } if (!$this->path_is_pattern($sourceFiles) && !$this->remote_file_exists($sourceFiles, $host)) { $text = _("'{$sourceFiles}' does not exist.\n"); return array(false, $text, $description); } if (!$this->path_is_pattern($sourceFiles) && !$this->remote_file_permission($sourceFiles, $host, "r")) { $text = _("Have no READ permission on '{$sourceFiles}'.\n"); return array(false, $text, $description); } if (!$this->path_is_pattern($sourceFiles) && is_file($sourceFiles) && filesize($sourceFiles) <= 0) { $text = _("You can not upload an empty file.\n"); return array(false, $text, $description); } /* Create an upload record. */ $uploadMode = 1 << 3; // code for "it came from web upload" $userId = Auth::getUserId(); $groupId = Auth::getGroupId(); $uploadId = JobAddUpload($userId, $groupId, $shortName, $sourceFiles, $description, $uploadMode, $folderId, $publicPermission); if (empty($uploadId)) { $text = _("Failed to insert upload record"); return array(false, $text, $description); } /* Prepare the job: job "wget" */ $jobpk = JobAddJob($userId, $groupId, "wget", $uploadId); if (empty($jobpk) || $jobpk < 0) { $text = _("Failed to insert upload record"); return array(false, $text, $description); } $jq_args = "{$uploadId} - {$sourceFiles}"; $jobqueuepk = JobQueueAdd($jobpk, "wget_agent", $jq_args, "no", NULL, $host); if (empty($jobqueuepk)) { $text = _("Failed to insert task 'wget' into job queue"); return array(false, $text, $description); } $ErrorMsg = ""; /* schedule agents */ $unpackplugin =& $Plugins[plugin_find_id("agent_unpack")]; $ununpack_jq_pk = $unpackplugin->AgentAdd($jobpk, $uploadId, $ErrorMsg, array("wget_agent")); if ($ununpack_jq_pk < 0) { return array(false, $text, _($ErrorMsg)); } $adj2nestplugin =& $Plugins[plugin_find_id("agent_adj2nest")]; $adj2nest_jq_pk = $adj2nestplugin->AgentAdd($jobpk, $uploadId, $ErrorMsg, array()); if ($adj2nest_jq_pk < 0) { return array(false, $text, _($ErrorMsg)); } AgentCheckBoxDo($jobpk, $uploadId); $message = ""; /** check if the scheudler is running */ $status = GetRunnableJobList(); if (empty($status)) { $message .= _("Is the scheduler running? "); } $Url = Traceback_uri() . "?mod=showjobs&upload={$uploadId}"; $message .= "The file {$sourceFiles} has been uploaded. "; $keep = "It is <a href='{$Url}'>upload #" . $uploadId . "</a>.\n"; return array(true, $message . $keep, $description); }
/** * \brief Given one object (file or URL), upload it. * * \param $FolderPath - folder path * \param $UploadArchive - upload file(absolute path) or url * \param $UploadName - uploaded file/dir name * \param $UploadDescription - upload description * * \return 1: error, 0: success */ function UploadOne($FolderPath, $UploadArchive, $UploadName, $UploadDescription, $TarSource = NULL) { global $Verbose; global $Test; global $QueueList; global $fossjobs_command; global $public_flag; global $SysConf; if (empty($UploadName)) { $text = "UploadName is empty\n"; echo $text; return 1; } $user_pk = $SysConf['auth']['UserId']; /* Get the user record and check the PLUGIN_DB_ level to make sure they have at least write access */ $UsersRow = GetSingleRec("users", "where user_pk={$user_pk}"); if ($UsersRow["user_perm"] < PLUGIN_DB_WRITE) { print "You have no permission to upload files into FOSSology\n"; return 1; } /* Get the folder's primary key */ $root_folder_fk = $UsersRow["root_folder_fk"]; global $OptionA; /* Should it use bucket names? */ if ($OptionA) { global $bucket_size; $FolderPath .= "/" . GetBucketFolder($UploadName, $bucket_size); } $FolderPk = GetFolder($FolderPath, $root_folder_fk); if ($FolderPk == 1) { print " Uploading to folder: 'Software Repository'\n"; } else { print " Uploading to folder: '{$FolderPath}'\n"; } print " Uploading as '{$UploadName}'\n"; if (!empty($UploadDescription)) { print " Upload description: '{$UploadDescription}'\n"; } $Mode = 1 << 3; // code for "it came from web upload" /* Create the upload for the file */ if ($Verbose) { print "JobAddUpload({$user_pk}, {$UploadName},{$UploadArchive},{$UploadDescription},{$Mode},{$FolderPk}, {$public_flag});\n"; } if (!$Test) { $Src = $UploadArchive; if (!empty($TarSource)) { $Src = $TarSource; } $UploadPk = JobAddUpload($user_pk, $UploadName, $Src, $UploadDescription, $Mode, $FolderPk, $public_flag); print " UploadPk is: '{$UploadPk}'\n"; } /* Prepare the job: job "wget" */ if ($Verbose) { print "JobAddJob({$user_pk}, wget, {$UploadPk});\n"; } if (!$Test) { $jobpk = JobAddJob($user_pk, "wget", $UploadPk); if (empty($jobpk) || $jobpk < 0) { $text = _("Failed to insert job record"); echo $text; return 1; } } $jq_args = "{$UploadPk} - {$Src}"; if ($Verbose) { print "JobQueueAdd({$jobpk}, wget_agent, {$jq_args}, no, NULL);\n"; } if (!$Test) { $jobqueuepk = JobQueueAdd($jobpk, "wget_agent", $jq_args, "no", NULL); if (empty($jobqueuepk)) { $text = _("Failed to insert task 'wget' into job queue"); echo $text; return 1; } } /* schedule agents */ global $Plugins; if ($Verbose) { print "AgentAdd wget_agent and dj2nest.\n"; } if (!$Test) { $unpackplugin =& $Plugins[plugin_find_id("agent_unpack")]; $ununpack_jq_pk = $unpackplugin->AgentAdd($jobpk, $UploadPk, $ErrorMsg, array("wget_agent")); if ($ununpack_jq_pk < 0) { echo $ErrorMsg; return 1; } $adj2nestplugin =& $Plugins[plugin_find_id("agent_adj2nest")]; $adj2nest_jq_pk = $adj2nestplugin->AgentAdd($jobpk, $UploadPk, $ErrorMsg, array()); if ($adj2nest_jq_pk < 0) { echo $ErrorMsg; return 1; } } if (!empty($QueueList)) { switch ($QueueList) { case 'ALL': case 'all': $Cmd = "{$fossjobs_command} -U '{$UploadPk}'"; break; default: $Cmd = "{$fossjobs_command} -U '{$UploadPk}' -A '{$QueueList}'"; break; } if ($Verbose) { print "CMD={$Cmd}\n"; } if (!$Test) { system($Cmd); } } else { /* No other agents other than unpack scheduled, attach to unpack*/ } }
/** * \brief Process the upload request. Call the upload by the Name passed in or by * the filename if no name is supplied. * * \param $FolderPk - folder fk to load into * \param $SourceFiles - files to upload, file, tar, directory, etc... * \param $GroupNames - flag for indicating if group names were requested. * passed on as -A option to cp2foss. * \param $Desc - optional description for the upload * \param $Name - optional Name for the upload * \param $public_perm public permission on the upload * * \return NULL on success, string on failure. */ function Upload($FolderPk, $SourceFiles, $GroupNames, $Desc, $Name, $HostName, $public_perm) { global $Plugins; global $SysConf; $FolderPath = FolderGetName($FolderPk); $SourceFiles = trim($SourceFiles); // $FolderPath = str_replace('\\','\\\\',$FolderPath); // $FolderPath = str_replace('"','\"',$FolderPath); $FolderPath = str_replace('`', '\\`', $FolderPath); $FolderPath = str_replace('$', '\\$', $FolderPath); if (!empty($Desc)) { // $Desc = str_replace('\\','\\\\',$Desc); // $Desc = str_replace('"','\"',$Desc); $Desc = str_replace('`', '\\`', $Desc); $Desc = str_replace('$', '\\$', $Desc); } if (!empty($Name)) { // $Name = str_replace('\\','\\\\',$Name); // $Name = str_replace('"','\"',$Name); $Name = str_replace('`', '\\`', $Name); $Name = str_replace('$', '\\$', $Name); } else { $Name = $SourceFiles; } // $SourceFiles = str_replace('\\','\\\\',$SourceFiles); // $SourceFiles = str_replace('"','\"',$SourceFiles); $SourceFiles = str_replace('`', '\\`', $SourceFiles); $SourceFiles = str_replace('$', '\\$', $SourceFiles); $SourceFiles = str_replace('|', '\\|', $SourceFiles); $SourceFiles = str_replace(' ', '\\ ', $SourceFiles); $SourceFiles = str_replace("\t", "\\\t", $SourceFiles); /* Add the job to the queue */ // create the job $ShortName = basename($Name); if (empty($ShortName)) { $ShortName = $Name; } $wildcardpath = strstr($SourceFiles, '*'); /** check if the file/directory is existed (the path does not include wildcards) */ if (empty($wildcardpath) && !$this->remote_file_exists($SourceFiles, $HostName)) { $text = _("'{$SourceFiles}' does not exist.\n"); return $text; } /** check if has the read permission */ if (empty($wildcardpath) && !$this->remote_file_permission($SourceFiles, $HostName, "r")) { $text = _("Have no READ permission on '{$SourceFiles}'.\n"); return $text; } // Create an upload record. $jobq = NULL; $Mode = 1 << 3; // code for "it came from web upload" $user_pk = $SysConf['auth']['UserId']; $uploadpk = JobAddUpload($user_pk, $ShortName, $SourceFiles, $Desc, $Mode, $FolderPk, $public_perm); /* Prepare the job: job "wget" */ $jobpk = JobAddJob($user_pk, "wget", $uploadpk); if (empty($jobpk) || $jobpk < 0) { $text = _("Failed to insert job record"); return $text; } $jq_args = "{$uploadpk} - {$SourceFiles}"; $jobqueuepk = JobQueueAdd($jobpk, "wget_agent", $jq_args, "no", NULL, $HostName); if (empty($jobqueuepk)) { $text = _("Failed to insert task 'wget' into job queue"); return $text; } /* schedule agents */ $unpackplugin =& $Plugins[plugin_find_id("agent_unpack")]; $ununpack_jq_pk = $unpackplugin->AgentAdd($jobpk, $uploadpk, $ErrorMsg, array("wget_agent")); if ($ununpack_jq_pk < 0) { return $ErrorMsg; } $adj2nestplugin =& $Plugins[plugin_find_id("agent_adj2nest")]; $adj2nest_jq_pk = $adj2nestplugin->AgentAdd($jobpk, $uploadpk, $ErrorMsg, array()); if ($adj2nest_jq_pk < 0) { return $ErrorMsg; } AgentCheckBoxDo($jobpk, $uploadpk); $msg = ""; /** check if the scheudler is running */ $status = GetRunnableJobList(); if (empty($status)) { $msg .= _("Is the scheduler running? "); } $Url = Traceback_uri() . "?mod=showjobs&upload={$uploadpk}"; $msg .= "The file {$SourceFiles} has been uploaded. "; $keep = "It is <a href='{$Url}'>upload #" . $uploadpk . "</a>.\n"; print displayMessage($msg, $keep); return NULL; }
/** * \brief Process the upload request. * \param $Folder * \param $VCSType * \param $GetURL * \param $Desc * \param $Name * \param $Username * \param $Passwd * \param $public_perm public permission on the upload * Returns NULL on success, string on failure. */ function Upload($Folder, $VCSType, $GetURL, $Desc, $Name, $Username, $Passwd, $public_perm) { global $SysConf; /* See if the URL looks valid */ if (empty($Folder)) { $text = _("Invalid folder"); return $text; } $GetURL = trim($GetURL); if (empty($GetURL)) { $text = _("Invalid URL"); return $text; } if (preg_match("@^((http)|(https)|(ftp))://([[:alnum:]]+)@i", $GetURL) != 1) { $text = _("Invalid URL"); return "{$text}: " . htmlentities($GetURL); } if (preg_match("@[[:space:]]@", $GetURL) != 0) { $text = _("Invalid URL (no spaces permitted)"); return "{$text}: " . htmlentities($GetURL); } if (empty($Name)) { $Name = basename($GetURL); } $ShortName = basename($Name); if (empty($ShortName)) { $ShortName = $Name; } /* Create an upload record. */ $Mode = 1 << 2; // code for "it came from wget" $user_pk = $SysConf['auth']['UserId']; $uploadpk = JobAddUpload($user_pk, $ShortName, $GetURL, $Desc, $Mode, $Folder, $public_perm); if (empty($uploadpk)) { $text = _("Failed to insert upload record"); return $text; } /* Create the job: job "wget" */ $jobpk = JobAddJob($user_pk, "wget", $uploadpk); if (empty($jobpk) || $jobpk < 0) { $text = _("Failed to insert job record"); return $text; } $jq_args = "{$uploadpk} - {$GetURL} {$VCSType} "; if (!empty($Username)) { $jq_args .= "--username {$Username} "; } if (!empty($Passwd)) { $jq_args .= "--password {$Passwd}"; } $jobqueuepk = JobQueueAdd($jobpk, "wget_agent", $jq_args, NULL, NULL); if (empty($jobqueuepk)) { $text = _("Failed to insert task 'wget_agent' into job queue"); return $text; } global $Plugins; /* schedule agents */ $unpackplugin =& $Plugins[plugin_find_id("agent_unpack")]; $ununpack_jq_pk = $unpackplugin->AgentAdd($jobpk, $uploadpk, $ErrorMsg, array("wget_agent")); if ($ununpack_jq_pk < 0) { return $ErrorMsg; } $adj2nestplugin =& $Plugins[plugin_find_id("agent_adj2nest")]; $adj2nest_jq_pk = $adj2nestplugin->AgentAdd($jobpk, $uploadpk, $ErrorMsg, array()); if ($adj2nest_jq_pk < 0) { return $ErrorMsg; } AgentCheckBoxDo($jobpk, $uploadpk); $msg = ""; /** check if the scheudler is running */ $status = GetRunnableJobList(); if (empty($status)) { $msg .= _("Is the scheduler running? "); } $Url = Traceback_uri() . "?mod=showjobs&upload={$uploadpk}"; $text = _("The upload"); $text1 = _("has been queued. It is"); $msg .= "{$text} {$Name} {$text1} "; $keep = "<a href='{$Url}'>upload #" . $uploadpk . "</a>.\n"; print displayMessage($msg, $keep); return NULL; }
/** * \brief Process the upload request. * * \param $folder_pk * \param $TempFile path to temporary (upload) file * \param $Desc optional upload description. * \param $Name original name of the file on the client machine. * \param $public_perm public permission on the upload * \return NULL on success, error string on failure. */ function Upload($folder_pk, $TempFile, $Desc, $Name, $public_perm) { global $MODDIR; global $SysConf; global $SYSCONFDIR; define("UPLOAD_ERR_EMPTY", 5); define("UPLOAD_ERR_INVALID_FOLDER_PK", 100); $upload_errors = array(UPLOAD_ERR_OK => _("No errors."), UPLOAD_ERR_INI_SIZE => _("Larger than upload_max_filesize ") . ini_get('upload_max_filesize'), UPLOAD_ERR_FORM_SIZE => _("Larger than form MAX_FILE_SIZE."), UPLOAD_ERR_PARTIAL => _("Partial upload."), UPLOAD_ERR_NO_FILE => _("No file."), UPLOAD_ERR_NO_TMP_DIR => _("No temporary directory."), UPLOAD_ERR_CANT_WRITE => _("Can't write to disk."), UPLOAD_ERR_EXTENSION => _("File upload stopped by extension."), UPLOAD_ERR_EMPTY => _("File is empty or you don't have permission to read the file."), UPLOAD_ERR_INVALID_FOLDER_PK => _("Invalid Folder.")); $UploadFile = $_FILES['getfile']; $UploadError = @$UploadFile['error']; /* Additional error checks */ if ($UploadFile['size'] == 0 && $UploadFile['error'] == 0) { $UploadFile['error'] = UPLOAD_ERR_EMPTY; } if (empty($folder_pk)) { $UploadFile['error'] = UPLOAD_ERR_INVALID_FOLDER_PK; } if ($UploadFile['error'] != UPLOAD_ERR_OK) { return $upload_errors[$UploadFile['error']]; } $originName = @$UploadFile['name']; if (empty($Name)) { $Name = basename($originName); } $ShortName = basename($Name); if (empty($ShortName)) { $ShortName = $Name; } // for odd case where $Name is '/' /* Create an upload record. */ $Mode = 1 << 3; // code for "it came from web upload" $user_pk = $SysConf['auth']['UserId']; $uploadpk = JobAddUpload($user_pk, $ShortName, $originName, $Desc, $Mode, $folder_pk, $public_perm); if (empty($uploadpk)) { $text = _("Failed to insert upload record"); return $text; } /* move the temp file */ $UploadedFile = "{$TempFile}" . "-uploaded"; if (!move_uploaded_file($TempFile, "{$UploadedFile}")) { $text = _("Could not save uploaded file"); return $text; } if (!chmod($UploadedFile, 0660)) { $text = _("ERROR! could not update permissions on downloaded file"); return $text; } /* Run wget_agent locally to import the file. */ $Prog = "{$MODDIR}/wget_agent/agent/wget_agent -C -g fossy -k {$uploadpk} '{$UploadedFile}' -c '{$SYSCONFDIR}'"; $wgetOut = array(); $wgetLast = exec($Prog, $wgetOut, $wgetRtn); unlink($UploadedFile); /* Create Job */ $job_pk = JobAddJob($user_pk, $ShortName, $uploadpk); global $Plugins; $adj2nestplugin =& $Plugins[plugin_find_id("agent_adj2nest")]; $Dependencies = array(); $adj2nestplugin->AgentAdd($job_pk, $uploadpk, $ErrorMsg, $Dependencies); AgentCheckBoxDo($job_pk, $uploadpk); if ($wgetRtn == 0) { $Msg = ""; /** check if the scheudler is running */ $status = GetRunnableJobList(); if (empty($status)) { $Msg .= _("Is the scheduler running? "); } $text = _("The file"); $text1 = _("has been uploaded. It is"); $Url = Traceback_uri() . "?mod=showjobs&upload={$uploadpk}"; $Msg .= "{$text} {$Name} {$text1} "; $keep = '<a href=' . $Url . '>upload #' . $uploadpk . "</a>.\n"; print displayMessage($Msg, $keep); return NULL; } else { $ErrMsg = GetArrayVal(0, $wgetOut); if (empty($ErrMsg)) { $ErrMsg = _("File upload failed. Error:") . $wgetRtn; } return $ErrMsg; } return NULL; }