FOSSology  4.4.0
Open Source License Compliance by Open Source Software
agent-add.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2008-2013 Hewlett-Packard Development Company, L.P.
4  SPDX-FileCopyrightText: © 2015 Siemens AG
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
13 use Symfony\Component\HttpFoundation\Request;
14 
16 {
17  const NAME = "agent_add";
18 
19  public function __construct()
20  {
21  parent::__construct(self::NAME, array(
22  self::TITLE => _("Schedule an Analysis"),
23  self::MENU_LIST => "Jobs::Schedule Agents",
24  self::PERMISSION => Auth::PERM_WRITE
25  ));
26  }
27 
32  protected function handle(Request $request)
33  {
34  $folderId = intval($request->get('folder'));
35  if (empty($folderId)) {
36  $folderId = FolderGetTop();
37  }
38  $uploadId = intval($request->get('upload'));
39  $agents = $request->get('agents') ?: '';
40  $vars = [];
41 
42  if (! empty($uploadId) && ! empty($agents) && is_array($agents)) {
43  $rc = $this->agentsAdd($uploadId, $agents, $request);
44  if (is_numeric($rc)) {
45  $status = GetRunnableJobList();
46  $scheduler_msg = empty($status) ? _("Is the scheduler running? ") : '';
47  $url = Traceback_uri() . "?mod=showjobs&upload=$uploadId";
48  $text = _("Your jobs have been added to job queue.");
49  $linkText = _("View Jobs");
50  $msg = "$scheduler_msg" . "$text <a href=\"$url\">$linkText</a>";
51  $vars['message'] = $msg;
52  } else {
53  $text = _("Scheduling of Agent(s) failed: ");
54  $vars['message'] = $text.$rc;
55  }
56  }
57 
58  $vars['uploadScript'] = ActiveHTTPscript("Uploads");
59  $vars['agentScript'] = ActiveHTTPscript("Agents");
60  $vars['folderId'] = $folderId;
61  $vars['folderListOptions'] = FolderListOption(-1,0,1,$folderId);
62  $vars['folderListUploads'] = FolderListUploads_perm($folderId, Auth::PERM_WRITE);
63  $vars['baseUri'] = Traceback_uri();
64  $vars['uploadId'] = $uploadId;
65 
66  $parmAgentList = MenuHook::getAgentPluginNames("ParmAgents");
67  $out = '<ol>';
68  $parmAgentFoots = '';
69  foreach ($parmAgentList as $parmAgent) {
70  $agent = plugin_find($parmAgent);
71  $out .= "<br/><b>".$agent->AgentName.":</b><br/>";
72  $out .= $agent->renderContent($vars);
73  $parmAgentFoots .= $agent->renderFoot($vars);
74  }
75  $out .= '</ol>';
76  $vars['out'] = $out;
77  $vars['outFoot'] = '<script language="javascript"> '.$parmAgentFoots.'</script>';
78 
79  return $this->render('agent_adder.html.twig', $this->mergeWithDefault($vars));
80  }
81 
88  private function agentsAdd($uploadId, $agentsToStart, Request $request)
89  {
90  $mimetypeIgnore = intval($request->get('scm') == 1) ? '-I' : '';
91  if (! is_array($agentsToStart)) {
92  return "bad parameters";
93  }
94  if (! $uploadId) {
95  return "agent-add.php AgentsAdd(): No upload_pk specified";
96  }
97 
98  /* @var $upload Upload */
99  $upload = $GLOBALS['container']->get('dao.upload')->getUpload($uploadId);
100  if ($upload===null) {
101  return _("Upload") . " " . $uploadId . " " . _("not found");
102  }
103  $agents = array();
104  $parmAgentList = MenuHook::getAgentPluginNames("ParmAgents");
105  $plainAgentList = MenuHook::getAgentPluginNames("Agents");
106  $agentList = array_merge($plainAgentList, $parmAgentList);
107  foreach ($agentList as $agentName) {
108  if (in_array($agentName, $agentsToStart)) {
109  $agents[$agentName] = plugin_find($agentName);
110  }
111  }
112 
113  $jobId = JobAddJob(Auth::getUserId(), Auth::getGroupId(), $upload->getFilename(), $uploadId);
114  $errorMsg = '';
115  foreach ($parmAgentList as $parmAgent) {
116  $agent = plugin_find($parmAgent);
117  $agent->scheduleAgent($jobId, $uploadId, $errorMsg, $request);
118  }
119 
120  foreach ($agents as &$agent) {
121  if (!empty($mimetypeIgnore)) {
122  $rv = $agent->AgentAdd($jobId, $uploadId, $errorMsg,
123  array("agent_mimetype"), $mimetypeIgnore, $request);
124  } else {
125  $rv = $agent->AgentAdd($jobId, $uploadId, $errorMsg, array(), null,
126  $request);
127  }
128  if ($rv == -1) {
129  return $errorMsg;
130  }
131  }
132  return $jobId;
133  }
134 
142  public function scheduleAgents($uploadId, $agentsToStart, Request $request)
143  {
144  return $this->agentsAdd($uploadId, $agentsToStart, $request);
145  }
146 }
147 
148 register_plugin(new AgentAdder());
agentsAdd($uploadId, $agentsToStart, Request $request)
Add an upload to multiple agents.
Definition: agent-add.php:88
handle(Request $request)
Definition: agent-add.php:32
scheduleAgents($uploadId, $agentsToStart, Request $request)
Add an upload to multiple agents (wrapper for agentsAdd()).
Definition: agent-add.php:142
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
render($templateName, $vars=null, $headers=null)
ActiveHTTPscript($RequestName, $IncludeScriptTags=1)
Given a function name, create the JavaScript needed for doing the request.
FolderListOption($ParentFolder, $Depth, $IncludeTop=1, $SelectId=-1, $linkParent=false, $OldParent=0)
Create the folder tree, using OPTION tags.
FolderGetTop()
DEPRECATED! Find the top-of-tree folder_pk for the current user.
FolderListUploads_perm($ParentFolder, $perm)
Returns an array of uploads in a folder.
Traceback_uri()
Get the URI without query to this location.
Definition: common-parm.php:97
plugin_find($pluginName)
Given the official name of a plugin, return the $Plugins object.
GetRunnableJobList()
Get runnable job list, the process is below:
#define PERM_WRITE
Read-Write permission.
Definition: libfossology.h:33