FOSSology  4.4.0
Open Source License Compliance by Open Source Software
ScheduleAgent.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 namespace Fossology\UI\Ajax;
9 
13 use Symfony\Component\HttpFoundation\Request;
14 use Symfony\Component\HttpFoundation\Response;
15 
17 {
18  const NAME = "scheduleAgentAjax";
19 
21  private $uploadDao;
22 
23  function __construct()
24  {
25  parent::__construct(self::NAME, array(
26  self::TITLE => _("Private: schedule a agent scan from post"),
27  self::PERMISSION => Auth::PERM_WRITE,
28  ));
29 
30  $this->uploadDao = $this->getObject('dao.upload');
31  }
32 
37  protected function handle(Request $request)
38  {
39  $errorMessage = "";
40  $jobqueueId = -1;
41 
42  $userId = $_SESSION['UserId'];
43  $groupId = $_SESSION['GroupId'];
44  $uploadId = intval($_POST['uploadId']);
45  $agentName = $_POST['agentName'];
46 
47  if ($uploadId > 0) {
48  $upload = $this->uploadDao->getUpload($uploadId);
49  $uploadName = $upload->getFilename();
50  $ourPlugin = plugin_find($agentName);
51  if ($ourPlugin === null) {
52  return new Response(json_encode(["error" => "Unable to find $agentName"]),
53  Response::HTTP_INTERNAL_SERVER_ERROR, ["Content-type" => "text/json"]);
54  }
55 
56  $jobqueueId = isAlreadyRunning($ourPlugin->AgentName, $uploadId);
57  if ($jobqueueId == 0) {
58  $jobId = JobAddJob($userId, $groupId, $uploadName, $uploadId);
59  $jobqueueId = $ourPlugin->AgentAdd($jobId, $uploadId, $errorMessage, array());
60  }
61  } else {
62  $errorMessage = "bad request";
63  }
64 
66 
67  $headers = array('Content-type' => 'text/json');
68  if (empty($errorMessage) && ($jobqueueId > 0)) {
69  return new Response(json_encode(array("jqid" => $jobqueueId)), Response::HTTP_OK, $headers);
70  } else {
71  return new Response(json_encode(array("error" => $errorMessage)), Response::HTTP_INTERNAL_SERVER_ERROR, $headers);
72  }
73  }
74 }
75 
76 register_plugin(new ScheduleAgent());
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
ReportCachePurgeAll()
Purge all records from the report cache.
isAlreadyRunning($agentName, $upload_pk)
Check if an agent is already running in a job.
Definition: common-job.php:498
plugin_find($pluginName)
Given the official name of a plugin, return the $Plugins object.