/** * \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; }
/** * \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; }
/** * \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"); }
function AddReunpackjob($uploadpk, $Depends = NULL, $priority = 0) { global $DB; if (empty($DB)) { return; } $Job_name = str_replace("'", "''", "unpack"); $SQLInsert = "INSERT INTO job\n (job_queued,job_priority,job_name,job_upload_fk) VALUES\n (now(),'{$priority}','{$Job_name}','{$uploadpk}');"; $SQLcheck = "SELECT job_pk FROM job WHERE job_upload_fk = '{$uploadpk}' AND job_name = '{$Job_name}' AND job_user_fk is NULL;"; $Results = $DB->Action($SQLcheck); if (!empty($Results)) { $jobpk = $Results[0]['job_pk']; } else { $DB->Action($SQLInsert); $SQLcheck = "SELECT job_pk FROM job WHERE job_upload_fk = '{$uploadpk}' AND job_name = '{$Job_name}' AND job_user_fk is NULL;"; $Results = $DB->Action($SQLcheck); $jobpk = $Results[0]['job_pk']; } if (empty($jobpk) || $jobpk < 0) { return "Failed to insert job record! {$SQLInsert}"; } if (!empty($Depends) && !is_array($Depends)) { $Depends = array($Depends); } /* job "unpack" has jobqueue item "unpack" */ $jqargs = "SELECT pfile.pfile_sha1 || '.' || pfile.pfile_md5 || '.' || pfile.pfile_size AS pfile,\n upload_pk, pfile_fk\n FROM upload\n INNER JOIN pfile ON upload.pfile_fk = pfile.pfile_pk\n WHERE upload.upload_pk = '{$uploadpk}';"; $jobqueuepk = JobQueueAdd($jobpk, "unpack", $jqargs, "no", "pfile", $Depends, 1); if (empty($jobqueuepk)) { return "Failed to insert item into job queue"; } return $jobqueuepk; }
/** * \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; }
/** * \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; }
/** * @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 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 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*/ } }
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 an uploadpk, add a job. * \param $Depends - specifying other dependencies. * $Depends can be a jq_pk, or an array of jq_pks, or NULL. * * \return NULL on success, string on failure. */ function AgentAdd($uploadpk, $Depends = NULL, $priority = 0) { global $PG_CONN; $Job_name = str_replace("'", "''", "reunpack"); //get userpk from uploadpk $UploadRec = GetSingleRec("upload", "where upload_pk='{$uploadpk}'"); //updated ununpack_ars table to let reunpack run $SQLARS = "UPDATE ununpack_ars SET ars_success = FALSE WHERE upload_fk = '{$uploadpk}';"; $result = pg_query($PG_CONN, $SQLARS); DBCheckResult($result, $SQLARS, __FILE__, __LINE__); pg_free_result($result); if (empty($uploadpk)) { $SQLInsert = "INSERT INTO job\n (job_queued,job_priority,job_name,job_user_fk) VALUES\n (now(),'{$priority}','{$Job_name}',{$UploadRec['user_fk']});"; } else { $SQLInsert = "INSERT INTO job\n (job_queued,job_priority,job_name,job_upload_fk,job_user_fk) VALUES\n (now(),'{$priority}','{$Job_name}','{$uploadpk}',{$UploadRec['user_fk']});"; } $SQLcheck = "SELECT job_pk FROM job WHERE job_upload_fk = '{$uploadpk}' AND job_name = '{$Job_name}' AND job_user_fk = {$UploadRec['user_fk']} ORDER BY job_pk DESC LIMIT 1;"; $result = pg_query($PG_CONN, $SQLcheck); DBCheckResult($result, $SQLcheck, __FILE__, __LINE__); $row = pg_fetch_assoc($result); pg_free_result($result); if (!empty($row)) { $jobpk = $row['job_pk']; } else { $result = pg_query($PG_CONN, $SQLInsert); DBCheckResult($result, $SQLInsert, __FILE__, __LINE__); $row = pg_fetch_assoc($result); pg_free_result($result); $SQLcheck = "SELECT job_pk FROM job WHERE job_upload_fk = '{$uploadpk}' AND job_name = '{$Job_name}' AND job_user_fk = {$UploadRec['user_fk']};"; $result = pg_query($PG_CONN, $SQLcheck); DBCheckResult($result, $SQLcheck, __FILE__, __LINE__); $row = pg_fetch_assoc($result); pg_free_result($result); $jobpk = $row['job_pk']; } if (empty($jobpk) || $jobpk < 0) { return "Failed to insert job record! {$SQLInsert}"; } if (!empty($Depends) && !is_array($Depends)) { $Depends = array($Depends); } /* job "unpack" has jobqueue item "unpack" */ $jqargs = "SELECT pfile.pfile_sha1 || '.' || pfile.pfile_md5 || '.' || pfile.pfile_size AS pfile,\n upload_pk, pfile_fk\n FROM upload\n INNER JOIN pfile ON upload.pfile_fk = pfile.pfile_pk\n WHERE upload.upload_pk = '{$uploadpk}';"; echo "JobQueueAdd used to do a reschedule here<br>"; $jobqueuepk = JobQueueAdd($jobpk, "ununpack", $uploadpk, NULL, $Depends); if (empty($jobqueuepk)) { return "Failed to insert item into job queue"; } return NULL; }
/** * \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 $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 Queue an agent. This is a simple version of AgentAdd() that can be * used by multiple plugins that only use upload_pk as jqargs. * Before queuing, check if agent needs to be queued. It doesn't need to be queued if: * - It is already queued * - It has already been run by the latest agent version * * \param $plugin caller plugin object * \param $job_pk * \param $upload_pk * \param $ErrorMsg - error message on failure * \param $Dependencies - array of named dependencies. Each array element is the plugin name. * For example, array(agent_adj2nest, agent_pkgagent). * Typically, this will just be array(agent_adj2nest). * \param $jqargs (optional) jobqueue.jq_args * * \returns * - jq_pk Successfully queued * - 0 Not queued, latest version of agent has previously run successfully * - -1 Not queued, error, error string in $ErrorMsg **/ function CommonAgentAdd($plugin, $job_pk, $upload_pk, &$ErrorMsg, $Dependencies, $jqargs = "", $jq_cmd_args = NULL) { global $Plugins; $Deps = array(); $DependsEmpty = array(); /* check if the latest agent has already been run */ if ($plugin->AgentHasResults($upload_pk) == 1) { return 0; } /* if it is already scheduled, then return success */ if (($jq_pk = IsAlreadyScheduled($job_pk, $plugin->AgentName, $upload_pk)) != 0) { return $jq_pk; } /* queue up dependencies */ foreach ($Dependencies as $Dependency) { if (is_array($Dependency)) { $PluginName = $Dependency['name']; $DepArgs = $Dependency['args']; } else { $PluginName = $Dependency; $DepArgs = null; } $DepPlugin = plugin_find($PluginName); if ($DepPlugin === null) { $ErrorMsg = "Invalid plugin name: {$PluginName}, (CommonAgentAdd())"; return -1; } if (($Deps[] = $DepPlugin->AgentAdd($job_pk, $upload_pk, $ErrorMsg, $DependsEmpty, $DepArgs)) == -1) { return -1; } } /* schedule AgentName */ if (empty($jqargs)) { $jqargs = $upload_pk; } $jq_pk = JobQueueAdd($job_pk, $plugin->AgentName, $jqargs, "", $Deps, NULL, $jq_cmd_args); if (empty($jq_pk)) { $ErrorMsg = _("Failed to insert agent {$plugin->AgentName} into job queue. jqargs: {$jqargs}"); return -1; } /* Tell the scheduler to check the queue. */ $success = fo_communicate_with_scheduler("database", $output, $error_msg); if (!$success) { $ErrorMsg = $error_msg . "\n" . $output; } return $jq_pk; }
/** * \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; } } }
/** * @param int $jobId * @param int $uploadId * @param &string $errorMsg - error message on failure * @param array $dependencies * @param string|null $jqargs (optional) jobqueue.jq_args * @return * * jqId Successfully queued * * 0 Not queued, latest version of agent has previously run successfully * * -1 Not queued, error, error string in $ErrorMsg **/ protected function doAgentAdd($jobId, $uploadId, &$errorMsg, $dependencies, $jqargs = "", $jq_cmd_args = null) { $deps = array(); foreach ($dependencies as $dependency) { $dep = $this->implicitAgentAdd($jobId, $uploadId, $errorMsg, $dependency); if ($dep == -1) { return -1; } $deps[] = $dep; } if (empty($jqargs)) { $jqargs = $uploadId; } $jobQueueId = \JobQueueAdd($jobId, $this->AgentName, $jqargs, "", $deps, NULL, $jq_cmd_args); if (empty($jobQueueId)) { $errorMsg = "Failed to insert agent {$this->AgentName} into job queue. jqargs: {$jqargs}"; return -1; } $success = \fo_communicate_with_scheduler("database", $output, $errorMsg); if (!$success) { $errorMsg .= "\n" . $output; } return $jobQueueId; }