Exemplo n.º 1
0
 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);
 }
Exemplo n.º 2
0
 /**
  * @param Request $request
  * @return Response
  */
 protected function handle(Request $request)
 {
     $errorMessage = "";
     $jobqueueId = -1;
     $userId = $_SESSION['UserId'];
     $groupId = $_SESSION['GroupId'];
     $uploadId = intval($_POST['uploadId']);
     $agentName = $_POST['agentName'];
     if ($uploadId > 0) {
         $upload = $this->uploadDao->getUpload($uploadId);
         $uploadName = $upload->getFilename();
         $jobId = JobAddJob($userId, $groupId, $uploadName, $uploadId);
         $ourPlugin = plugin_find($agentName);
         $jobqueueId = $ourPlugin->AgentAdd($jobId, $uploadId, $errorMessage, array());
     } else {
         $errorMessage = "bad request";
     }
     ReportCachePurgeAll();
     $headers = array('Content-type' => 'text/json');
     if (empty($errorMessage) && $jobqueueId > 0) {
         return new Response(json_encode(array("jqid" => $jobqueueId)), Response::HTTP_OK, $headers);
     } else {
         return new Response(json_encode(array("error" => $errorMessage)), Response::HTTP_INTERNAL_SERVER_ERROR, $headers);
     }
 }
Exemplo n.º 3
0
 /**
  * \brief Given a folder_pk, add a job.
  * \param $uploadpk - the upload(upload_id) you want to delete
  * \param $Depends - Depends is not used for now
  *
  * \return NULL on success, string on failure.
  */
 function Delete($uploadpk, $Depends = NULL)
 {
     global $SysConf;
     /* Prepare the job: job "Delete" */
     $user_pk = Auth::getUserId();
     $group_pk = Auth::getGroupId();
     $jobpk = JobAddJob($user_pk, $group_pk, "Delete", $uploadpk);
     if (empty($jobpk) || $jobpk < 0) {
         $text = _("Failed to create job record");
         return $text;
     }
     /* Add job: job "Delete" has jobqueue item "delagent" */
     $jqargs = "DELETE UPLOAD {$uploadpk}";
     $jobqueuepk = JobQueueAdd($jobpk, "delagent", $jqargs, NULL, NULL);
     if (empty($jobqueuepk)) {
         $text = _("Failed to place delete in job queue");
         return $text;
     }
     /* Tell the scheduler to check the queue. */
     $success = fo_communicate_with_scheduler("database", $output, $error_msg);
     if (!$success) {
         $error_msg = _("Is the scheduler running? Your jobs have been added to job queue.");
         $URL = Traceback_uri() . "?mod=showjobs&upload={$uploadpk} ";
         $LinkText = _("View Jobs");
         $msg = "{$error_msg} <a href={$URL}>{$LinkText}</a>";
         return $msg;
     }
     return NULL;
 }
Exemplo n.º 4
0
 /**
  * \brief Delete
  * Creates a job to detele the folder
  *
  * \param $folderpk - the folder_pk to remove
  * \return NULL on success, string on failure.
  */
 function Delete($folderpk, $userId)
 {
     /* Can't remove top folder */
     if ($folderpk == FolderGetTop()) {
         $text = _("Can Not Delete Root Folder");
         return $text;
     }
     /* Get the folder's name */
     $FolderName = FolderGetName($folderpk);
     /* Prepare the job: job "Delete" */
     $groupId = Auth::getGroupId();
     $jobpk = JobAddJob($userId, $groupId, "Delete Folder: {$FolderName}");
     if (empty($jobpk) || $jobpk < 0) {
         $text = _("Failed to create job record");
         return $text;
     }
     /* Add job: job "Delete" has jobqueue item "delagent" */
     $jqargs = "DELETE FOLDER {$folderpk}";
     $jobqueuepk = JobQueueAdd($jobpk, "delagent", $jqargs, NULL, NULL);
     if (empty($jobqueuepk)) {
         $text = _("Failed to place delete in job queue");
         return $text;
     }
     /* Tell the scheduler to check the queue. */
     $success = fo_communicate_with_scheduler("database", $output, $error_msg);
     if (!$success) {
         return $error_msg . "\n" . $output;
     }
     return NULL;
 }
Exemplo n.º 5
0
 /**
  * \brief queue the job
  *
  * \param
  * \returns status string
  **/
 function QueueJob()
 {
     global $SysConf;
     /* Find all the maintagent options specified by the user.
      * They look like _REQUEST["a"] = "a", _REQUEST["b"]="b", ...
      */
     $options = "-";
     foreach ($_REQUEST as $key => $value) {
         if ($key == $value) {
             $options .= $value;
         }
     }
     /* Create the maintenance job */
     $user_pk = $SysConf['auth']['UserId'];
     $upload_pk = 0;
     // dummy
     $job_pk = JobAddJob($user_pk, "Maintenance", $upload_pk);
     if (empty($job_pk) || $job_pk < 0) {
         return _("Failed to insert job record");
     }
     $jq_pk = JobQueueAdd($job_pk, "maintagent", NULL, NULL, NULL, NULL, $options);
     if (empty($jq_pk)) {
         return _("Failed to insert task 'Maintenance' into job queue");
     }
     /* Tell the scheduler to check the queue. */
     $success = fo_communicate_with_scheduler("database", $output, $error_msg);
     if (!$success) {
         return $error_msg . "\n" . $output;
     }
     return _("The maintenance job has been queued");
 }
 /**
  * \brief Delete
  * Creates a job to detele the folder
  *
  * \param $folderpk - the folder_pk to remove
  * \return NULL on success, string on failure.
  */
 function Delete($folderpk, $Depends = NULL)
 {
     global $SysConf;
     /* Can't remove top folder */
     if ($folderpk == FolderGetTop()) {
         $text = _("Can Not Delete Root Folder");
         return $text;
     }
     /* Get the folder's name */
     $FolderName = FolderGetName($folderpk);
     /* Prepare the job: job "Delete" */
     $user_pk = $SysConf['auth']['UserId'];
     $jobpk = JobAddJob($user_pk, "Delete Folder: {$FolderName}");
     if (empty($jobpk) || $jobpk < 0) {
         $text = _("Failed to create job record");
         return $text;
     }
     /* Add job: job "Delete" has jobqueue item "delagent" */
     $jqargs = "DELETE FOLDER {$folderpk}";
     $jobqueuepk = JobQueueAdd($jobpk, "delagent", $jqargs, NULL, NULL);
     if (empty($jobqueuepk)) {
         $text = _("Failed to place delete in job queue");
         return $text;
     }
     return NULL;
 }
Exemplo n.º 7
0
 /**
  * \brief Add an upload to multiple agents.
  *
  * \param $uploadpk 
  * \param $agentlist - list of agents
  * \return NULL on success, error message string on failure
  */
 function AgentsAdd($uploadpk, $agentlist)
 {
     global $Plugins;
     global $PG_CONN;
     global $SysConf;
     $rc = "";
     $Alist = array();
     /* Make sure the uploadpk is valid */
     if (!$uploadpk) {
         return "agent-add.php AgentsAdd(): No upload_pk specified";
     }
     $sql = "SELECT upload_pk, upload_filename FROM upload WHERE upload_pk = '{$uploadpk}';";
     $result = pg_query($PG_CONN, $sql);
     DBCheckResult($result, $sql, __FILE__, __LINE__);
     if (pg_num_rows($result) < 1) {
         $ErrMsg = __FILE__ . ":" . __LINE__ . " " . _("Upload") . " " . $uploadpk . " " . _("not found");
         return $ErrMsg;
     }
     $UploadRow = pg_fetch_assoc($result);
     $ShortName = $UploadRow['upload_filename'];
     pg_free_result($result);
     /* Create Job */
     $user_pk = $SysConf['auth']['UserId'];
     $job_pk = JobAddJob($user_pk, $ShortName, $uploadpk);
     /* Validate the agent list and add agents as needed. */
     /** Don't worry about order or duplicates -- it will do the right thing. **/
     $Depth = 0;
     $agent_list = menu_find("Agents", $depth);
     for ($al = 0; !empty($agentlist[$al]); $al++) {
         /* check if the agent exists in the list of viable agents */
         $Found = -1;
         for ($ac = 0; $Found < 0 && !empty($agent_list[$ac]->URI); $ac++) {
             if (!strcmp($agent_list[$ac]->URI, $agentlist[$al])) {
                 $Found = $al;
                 break;
             }
         }
         if ($Found >= 0) {
             //print "Adding to " . $agentlist[$Found] . "<br>\n";
             $Dependencies = array();
             $P =& $Plugins[plugin_find_id($agentlist[$Found])];
             $rv = $P->AgentAdd($job_pk, $uploadpk, $ErrorMsg, $Dependencies);
             if ($rv == -1) {
                 $rc .= $ErrorMsg;
             }
         } else {
             $rc .= "Agent '" . htmlentities($agentlist[$al]) . "' not found.\n";
         }
     }
     return $rc;
 }
Exemplo n.º 8
0
 /**
  * \brief Display the loaded menu and plugins.
  */
 public function Output()
 {
     global $Plugins;
     global $PG_CONN;
     $UploadPk = GetParm("upload", PARM_INTEGER);
     $Agent = GetParm("agent", PARM_STRING);
     if (empty($UploadPk) || empty($Agent)) {
         return new Response('missing parameter', Response::HTTP_BAD_REQUEST, array('Content-type' => 'text/plain'));
     }
     $sql = "SELECT upload_pk, upload_filename FROM upload WHERE upload_pk = '{$UploadPk}'";
     $result = pg_query($PG_CONN, $sql);
     DBCheckResult($result, $sql, __FILE__, __LINE__);
     if (pg_num_rows($result) < 1) {
         $errMsg = __FILE__ . ":" . __LINE__ . " " . _("Upload") . " " . $UploadPk . " " . _("not found");
         return new Response($errMsg, Response::HTTP_BAD_REQUEST, array('Content-type' => 'text/plain'));
     }
     $UploadRow = pg_fetch_assoc($result);
     $ShortName = $UploadRow['upload_filename'];
     pg_free_result($result);
     $user_pk = Auth::getUserId();
     $group_pk = Auth::getGroupId();
     $job_pk = JobAddJob($user_pk, $group_pk, $ShortName, $UploadPk);
     $Dependencies = array();
     $P =& $Plugins[plugin_find_id($Agent)];
     $rv = $P->AgentAdd($job_pk, $UploadPk, $ErrorMsg, $Dependencies);
     if ($rv <= 0) {
         $text = _("Scheduling of Agent(s) failed: ");
         return new Response($text . $rv . $ErrorMsg, Response::HTTP_BAD_REQUEST, array('Content-type' => 'text/plain'));
     }
     /** check if the scheudler is running */
     $status = GetRunnableJobList();
     $scheduler_msg = "";
     if (empty($status)) {
         $scheduler_msg .= _("Is the scheduler running? ");
     }
     $URL = Traceback_uri() . "?mod=showjobs&upload={$UploadPk}";
     /* Need to refresh the screen */
     $text = _("Your jobs have been added to job queue.");
     $LinkText = _("View Jobs");
     $msg = "{$scheduler_msg}" . "{$text} <a href={$URL}>{$LinkText}</a>";
     $this->vars['message'] = $msg;
     return new Response($msg, Response::HTTP_OK, array('Content-type' => 'text/plain'));
 }
 /**
  * \brief Given a folder_pk, add a job.
  * \param $uploadpk - the upload(upload_id) you want to delete
  * \param $Depends - Depends is not used for now
  *
  * \return NULL on success, string on failure.
  */
 function Delete($uploadpk, $Depends = NULL)
 {
     global $SysConf;
     /* Prepare the job: job "Delete" */
     $user_pk = $SysConf['auth']['UserId'];
     $jobpk = JobAddJob($user_pk, "Delete", $uploadpk);
     if (empty($jobpk) || $jobpk < 0) {
         $text = _("Failed to create job record");
         return $text;
     }
     /* Add job: job "Delete" has jobqueue item "delagent" */
     $jqargs = "DELETE UPLOAD {$uploadpk}";
     $jobqueuepk = JobQueueAdd($jobpk, "delagent", $jqargs, NULL, NULL);
     if (empty($jobqueuepk)) {
         $text = _("Failed to place delete in job queue");
         return $text;
     }
     /* Tell the scheduler to check the queue. */
     $success = fo_communicate_with_scheduler("database", $output, $error_msg);
     if (!$success) {
         return $error_msg . "\n" . $output;
     }
     return NULL;
 }
Exemplo n.º 10
0
 /**
  * @brief Add an upload to multiple agents.
  * @param int $uploadId
  * @param string[] $agentsToStart - list of agents
  * @return NULL on success, error message string on failure
  */
 private function agentsAdd($uploadId, $agentsToStart, Request $request)
 {
     if (!is_array($agentsToStart)) {
         return "bad parameters";
     }
     if (!$uploadId) {
         return "agent-add.php AgentsAdd(): No upload_pk specified";
     }
     /* @var $upload Upload */
     $upload = $GLOBALS['container']->get('dao.upload')->getUpload($uploadId);
     if ($upload === null) {
         return _("Upload") . " " . $uploadId . " " . _("not found");
     }
     $agents = array();
     $parmAgentList = MenuHook::getAgentPluginNames("ParmAgents");
     $plainAgentList = MenuHook::getAgentPluginNames("Agents");
     $agentList = array_merge($plainAgentList, $parmAgentList);
     foreach ($agentList as $agentName) {
         if (in_array($agentName, $agentsToStart)) {
             $agents[$agentName] = plugin_find($agentName);
         }
     }
     if (count($agents) == 0) {
         return _("no valid agent specified");
     }
     $jobId = JobAddJob(Auth::getUserId(), Auth::getGroupId(), $upload->getFilename(), $uploadId);
     $errorMsg = '';
     foreach ($parmAgentList as $parmAgent) {
         $agent = plugin_find($parmAgent);
         $agent->scheduleAgent($jobId, $uploadId, $errorMsg, $request);
     }
     foreach ($agents as &$agent) {
         $rv = $agent->AgentAdd($jobId, $uploadId, $errorMsg, array());
         if ($rv == -1) {
             return $errorMsg;
         }
     }
     return null;
 }
Exemplo n.º 11
0
 /**
  * 
  * @param int $uploadTreeId
  * @param Request $request
  * @return int $jobQueueId
  */
 private function getJobQueueId($uploadTreeId, Request $request)
 {
     $uploadEntry = $this->uploadDao->getUploadEntry($uploadTreeId);
     $uploadId = intval($uploadEntry['upload_fk']);
     $userId = Auth::getUserId();
     $groupId = Auth::getGroupId();
     if ($uploadId <= 0 || !$this->uploadDao->isAccessible($uploadId, $groupId)) {
         throw new Exception('permission denied');
     }
     $bulkScope = $request->get('bulkScope');
     switch ($bulkScope) {
         case 'u':
             $uploadTreeTable = $this->uploadDao->getUploadtreeTableName($uploadId);
             $topBounds = $this->uploadDao->getParentItemBounds($uploadId, $uploadTreeTable);
             $uploadTreeId = $topBounds->getItemId();
             break;
         case 'f':
             if (!Isdir($uploadEntry['ufile_mode']) && !Iscontainer($uploadEntry['ufile_mode']) && !Isartifact($uploadEntry['ufile_mode'])) {
                 $uploadTreeId = $uploadEntry['parent'] ?: $uploadTreeId;
             }
             break;
         default:
             throw new InvalidArgumentException('bad scope request');
     }
     $refText = $request->get('refText');
     $actions = $request->get('bulkAction');
     $licenseRemovals = array();
     foreach ($actions as $licenseAction) {
         $licenseRemovals[$licenseAction['licenseId']] = $licenseAction['action'] == 'remove';
     }
     $bulkId = $this->licenseDao->insertBulkLicense($userId, $groupId, $uploadTreeId, $licenseRemovals, $refText);
     if ($bulkId <= 0) {
         throw new Exception('cannot insert bulk reference');
     }
     $upload = $this->uploadDao->getUpload($uploadId);
     $uploadName = $upload->getFilename();
     $job_pk = JobAddJob($userId, $groupId, $uploadName, $uploadId);
     /** @var DeciderJobAgentPlugin $deciderPlugin */
     $deciderPlugin = plugin_find("agent_deciderjob");
     $dependecies = array(array('name' => 'agent_monk_bulk', 'args' => $bulkId));
     $conflictStrategyId = intval($request->get('forceDecision'));
     $errorMsg = '';
     $jqId = $deciderPlugin->AgentAdd($job_pk, $uploadId, $errorMsg, $dependecies, $conflictStrategyId);
     if (!empty($errorMsg)) {
         throw new Exception(str_replace('<br>', "\n", $errorMsg));
     }
     return $jqId;
 }
Exemplo n.º 12
0
 /**
  * @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);
 }
Exemplo n.º 13
0
 protected function postUploadAddJobs(Request $request, $fileName, $uploadId, $jobId = null, $wgetDependency = false)
 {
     $userId = Auth::getUserId();
     $groupId = Auth::getGroupId();
     if ($jobId === null) {
         $jobId = JobAddJob($userId, $groupId, $fileName, $uploadId);
     }
     $dummy = "";
     $adj2nestDependencies = array();
     if ($wgetDependency) {
         $adj2nestDependencies = array(array('name' => 'agent_unpack', AgentPlugin::PRE_JOB_QUEUE => array('wget_agent')));
     }
     $adj2nestplugin = \plugin_find('agent_adj2nest');
     $adj2nestplugin->AgentAdd($jobId, $uploadId, $dummy, $adj2nestDependencies);
     $checkedAgents = checkedAgents();
     AgentSchedule($jobId, $uploadId, $checkedAgents);
     $errorMsg = '';
     $parmAgentList = MenuHook::getAgentPluginNames("ParmAgents");
     $plainAgentList = MenuHook::getAgentPluginNames("Agents");
     $agentList = array_merge($plainAgentList, $parmAgentList);
     foreach ($parmAgentList as $parmAgent) {
         $agent = plugin_find($parmAgent);
         $agent->scheduleAgent($jobId, $uploadId, $errorMsg, $request, $agentList);
     }
     $status = GetRunnableJobList();
     $message = empty($status) ? _("Is the scheduler running? ") : "";
     $jobUrl = Traceback_uri() . "?mod=showjobs&upload={$uploadId}";
     $message .= _("The file") . " " . $fileName . " " . _("has been uploaded. It is") . ' <a href=' . $jobUrl . '>upload #' . $uploadId . "</a>.\n";
     if ($request->get('public') == self::PUBLIC_GROUPS) {
         $this->getObject('dao.upload.permission')->makeAccessibleToAllGroupsOf($uploadId, $userId);
     }
     return $message;
 }
Exemplo n.º 14
0
 /**
  * \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;
 }
Exemplo n.º 15
0
 /**
  * \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;
 }
Exemplo n.º 16
0
 private function getJobQueueId($uploadTreeId)
 {
     $uploadEntry = $this->uploadDao->getUploadEntry($uploadTreeId);
     $uploadId = intval($uploadEntry['upload_fk']);
     if ($uploadId <= 0) {
         throw new Exception('permission denied');
     }
     $bulkScope = filter_input(INPUT_POST, 'bulkScope');
     switch ($bulkScope) {
         case 'u':
             $uploadTreeTable = $this->uploadDao->getUploadtreeTableName($uploadId);
             $row = $this->dbManager->getSingleRow("SELECT uploadtree_pk FROM {$uploadTreeTable} WHERE upload_fk = \$1 ORDER BY uploadtree_pk LIMIT 1", array($uploadId), __METHOD__ . "adam" . $uploadTreeTable);
             $uploadTreeId = $row['uploadtree_pk'];
             break;
         case 'f':
             if (!Isdir($uploadEntry['ufile_mode']) && !Iscontainer($uploadEntry['ufile_mode']) && !Isartifact($uploadEntry['ufile_mode'])) {
                 $uploadTreeId = $uploadEntry['parent'] ?: $uploadTreeId;
             }
             break;
         default:
             throw new InvalidArgumentException('bad scope request');
     }
     $userId = Auth::getUserId();
     $groupId = Auth::getGroupId();
     $refText = filter_input(INPUT_POST, 'refText');
     $action = filter_input(INPUT_POST, 'bulkAction');
     $licenseId = GetParm('licenseId', PARM_INTEGER);
     $removing = $action === 'remove';
     $bulkId = $this->licenseDao->insertBulkLicense($userId, $groupId, $uploadTreeId, $licenseId, $removing, $refText);
     if ($bulkId <= 0) {
         throw new Exception('cannot insert bulk reference');
     }
     $upload = $this->uploadDao->getUpload($uploadId);
     $uploadName = $upload->getFilename();
     $job_pk = JobAddJob($userId, $groupId, $uploadName, $uploadId);
     /** @var DeciderJobAgentPlugin $deciderPlugin */
     $deciderPlugin = plugin_find("agent_deciderjob");
     $dependecies = array(array('name' => 'agent_monk_bulk', 'args' => $bulkId));
     $conflictStrategyId = intval(filter_input(INPUT_POST, 'forceDecision'));
     $errorMsg = '';
     $jqId = $deciderPlugin->AgentAdd($job_pk, $uploadId, $errorMsg, $dependecies, $conflictStrategyId);
     if (!empty($errorMsg)) {
         throw new Exception(str_replace('<br>', "\n", $errorMsg));
     }
     return $jqId;
 }
 function doEdit($userId, $groupId, $itemId)
 {
     $licenses = GetParm("licenseNumbersToBeSubmitted", PARM_RAW);
     $removed = $_POST['removed'] === 't' || $_POST['removed'] === 'true';
     $itemTreeBounds = $this->uploadDao->getItemTreeBounds($itemId);
     $uploadId = $itemTreeBounds->getUploadId();
     $upload = $this->uploadDao->getUpload($uploadId);
     $uploadName = $upload->getFilename();
     $jobId = JobAddJob($userId, $groupId, $uploadName, $uploadId);
     if (isset($licenses)) {
         if (!is_array($licenses)) {
             return $this->errorJson("bad license array");
         }
         foreach ($licenses as $licenseId) {
             if (intval($licenseId) <= 0) {
                 return $this->errorJson("bad license");
             }
             $this->clearingDao->insertClearingEvent($itemId, $userId, $groupId, $licenseId, $removed, ClearingEventTypes::USER, $reportInfo = '', $comment = '', $jobId);
         }
     }
     /** @var agent_fodecider $deciderPlugin */
     $deciderPlugin = plugin_find("agent_deciderjob");
     $conflictStrategyId = null;
     $errorMsg = "";
     $jq_pk = $deciderPlugin->AgentAdd($jobId, $uploadId, $errorMsg, array(), $conflictStrategyId);
     /** after changing one license, purge all the report cache */
     ReportCachePurgeAll();
     //Todo: Change sql statement of fossology/src/buckets/agent/leaf.c line 124 to take the newest valid license, then uncomment this line
     // $this->ChangeBuckets(); // change bucket accordingly
     if (empty($errorMsg) && $jq_pk > 0) {
         return new JsonResponse(array("jqid" => $jq_pk));
     } else {
         return $this->errorJson($errorMsg, 500);
     }
 }
Exemplo n.º 18
0
 /**
  * \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;
 }
Exemplo n.º 19
0
 /**
  * \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;
 }
Exemplo n.º 20
0
/**
 * \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*/
    }
}
Exemplo n.º 21
0
/**
 * \brief Schedule delagent on upload ids
 *
 * \param $upload_pk_list -  upload ids, The string can be a comma-separated list of upload ids.
 * Or, use 'ALL' to specify all upload ids.
 */
function QueueUploadsOnDelagents($upload_pk_list)
{
    /* Get the users.default_bucketpool_fk */
    $user_pk = Auth::getUserId();
    $group_pk = Auth::getGroupId();
    if (!empty($upload_pk_list)) {
        foreach (explode(",", $upload_pk_list) as $upload_pk) {
            if (empty($upload_pk)) {
                continue;
            }
            // Create a job for the upload
            $jobpk = JobAddJob($user_pk, $group_pk, "Delete", $upload_pk);
            if (empty($jobpk) || $jobpk < 0) {
                echo "WARNING: Failed to schedule Delagent for Upload {$upload_pk}";
            }
            $jqargs = "DELETE UPLOAD {$upload_pk}";
            $jobqueuepk = JobQueueAdd($jobpk, "delagent", $jqargs, NULL, NULL);
            if (empty($jobqueuepk)) {
                echo "WARNING: Failed to schedule Delagent for Upload {$upload_pk}";
            }
            print "Delagent is queued to run on Upload: {$upload_pk}.\n";
        }
        /* for each $upload_pk */
    }
    // if $upload_pk is defined
    /* Tell the scheduler to check the queue. */
    $success = fo_communicate_with_scheduler("database", $output, $error_msg);
    if (!$success) {
        echo $error_msg . "\n" . $output;
    }
}
Exemplo n.º 22
0
 protected function getJobAndJobqueue($groupId, $upload, $addUploads)
 {
     $uploadId = $upload->getId();
     $spdxTwoAgent = plugin_find('agent_' . $this->outputFormat);
     $userId = Auth::getUserId();
     $jqCmdArgs = $this->uploadsAdd($addUploads);
     $dbManager = $this->getObject('db.manager');
     $sql = 'SELECT jq_pk,job_pk FROM jobqueue, job ' . 'WHERE jq_job_fk=job_pk AND jq_type=$1 AND job_group_fk=$4 AND job_user_fk=$3 AND jq_args=$2 AND jq_endtime IS NULL';
     $params = array($spdxTwoAgent->AgentName, $uploadId, $userId, $groupId);
     $log = __METHOD__;
     if ($jqCmdArgs) {
         $sql .= ' AND jq_cmd_args=$5';
         $params[] = $jqCmdArgs;
         $log .= '.args';
     } else {
         $sql .= ' AND jq_cmd_args IS NULL';
     }
     $scheduled = $dbManager->getSingleRow($sql, $params, $log);
     if (!empty($scheduled)) {
         return array($scheduled['job_pk'], $scheduled['jq_pk']);
     }
     $jobId = JobAddJob($userId, $groupId, $upload->getFilename(), $uploadId);
     $error = "";
     $jobQueueId = $spdxTwoAgent->AgentAdd($jobId, $uploadId, $error, array(), $jqCmdArgs);
     if ($jobQueueId < 0) {
         throw new Exception(_("Cannot schedule") . ": " . $error);
     }
     return array($jobId, $jobQueueId);
 }
Exemplo n.º 23
0
 /**
  * @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);
 }
Exemplo n.º 24
0
/**
 * \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;
        }
    }
}
Exemplo n.º 25
0
 private function insertPermission($groupId, $uploadId, $permission, $uploadList)
 {
     $fileName = false;
     foreach ($uploadList as $uploadEntry) {
         if ($uploadEntry['upload_pk']) {
             $fileName = $uploadEntry['name'];
         }
     }
     if (empty($fileName)) {
         throw new \Exception('This upload is missing or inaccessible');
     }
     $reuseBit = $permission & self::MOD_REUSE;
     if ($reuseBit) {
         $jobId = \JobAddJob(Auth::getUserId(), $groupId, $fileName, $uploadId);
         $reuserAgent = \plugin_find('agent_reuser');
         $request = new Request(array('uploadToReuse' => "{$uploadId}," . Auth::getGroupId(), 'groupId' => $groupId));
         $reuserAgent->scheduleAgent($jobId, $uploadId, $errorMsg, $request);
         if (!empty($errorMsg)) {
             throw new Exception($errorMsg);
         }
         $permission ^= $reuseBit;
     }
     $this->uploadPermDao->insertPermission($uploadId, $groupId, $permission);
 }