FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
agent-add.php
1 <?php
2 /***********************************************************
3  Copyright (C) 2008-2013 Hewlett-Packard Development Company, L.P.
4  Copyright (C) 2015 Siemens AG
5 
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public License
8  version 2 as published by the Free Software Foundation.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 ***********************************************************/
19 
25 
27 {
28  const NAME = "agent_add";
29 
30  public function __construct()
31  {
32  parent::__construct(self::NAME, array(
33  self::TITLE => _("Schedule an Analysis"),
34  self::MENU_LIST => "Jobs::Schedule Agents",
35  self::PERMISSION => Auth::PERM_WRITE
36  ));
37  }
38 
43  protected function handle(Request $request)
44  {
45  $folderId = intval($request->get('folder'));
46  if (empty($folderId)) {
47  $folderId = FolderGetTop();
48  }
49  $uploadId = intval($request->get('upload'));
50  $agents = $request->get('agents') ?: '';
51  $vars = [];
52 
53  if (! empty($uploadId) && ! empty($agents) && is_array($agents)) {
54  $rc = $this->agentsAdd($uploadId, $agents, $request);
55  if (is_numeric($rc)) {
56  $status = GetRunnableJobList();
57  $scheduler_msg = empty($status) ? _("Is the scheduler running? ") : '';
58  $url = Traceback_uri() . "?mod=showjobs&upload=$uploadId";
59  $text = _("Your jobs have been added to job queue.");
60  $linkText = _("View Jobs");
61  $msg = "$scheduler_msg" . "$text <a href=\"$url\">$linkText</a>";
62  $vars['message'] = $msg;
63  } else {
64  $text = _("Scheduling of Agent(s) failed: ");
65  $vars['message'] = $text.$rc;
66  }
67  }
68 
69  $vars['uploadScript'] = ActiveHTTPscript("Uploads");
70  $vars['agentScript'] = ActiveHTTPscript("Agents");
71  $vars['folderId'] = $folderId;
72  $vars['folderListOptions'] = FolderListOption(-1,0,1,$folderId);
73  $vars['folderListUploads'] = FolderListUploads_perm($folderId, Auth::PERM_WRITE);
74  $vars['baseUri'] = Traceback_uri();
75  $vars['uploadId'] = $uploadId;
76 
77  $parmAgentList = MenuHook::getAgentPluginNames("ParmAgents");
78  $out = '<ol>';
79  $parmAgentFoots = '';
80  foreach ($parmAgentList as $parmAgent) {
81  $agent = plugin_find($parmAgent);
82  $out .= "<br/><b>".$agent->AgentName.":</b><br/>";
83  $out .= $agent->renderContent($vars);
84  $parmAgentFoots .= $agent->renderFoot($vars);
85  }
86  $out .= '</ol>';
87  $vars['out'] = $out;
88  $vars['outFoot'] = '<script language="javascript"> '.$parmAgentFoots.'</script>';
89 
90  return $this->render('agent_adder.html.twig', $this->mergeWithDefault($vars));
91  }
92 
99  private function agentsAdd($uploadId, $agentsToStart, Request $request)
100  {
101  $mimetypeIgnore = intval($request->get('scm') == 1) ? '-I' : '';
102  if (! is_array($agentsToStart)) {
103  return "bad parameters";
104  }
105  if (! $uploadId) {
106  return "agent-add.php AgentsAdd(): No upload_pk specified";
107  }
108 
109  /* @var $upload Upload */
110  $upload = $GLOBALS['container']->get('dao.upload')->getUpload($uploadId);
111  if ($upload===null) {
112  return _("Upload") . " " . $uploadId . " " . _("not found");
113  }
114  $agents = array();
115  $parmAgentList = MenuHook::getAgentPluginNames("ParmAgents");
116  $plainAgentList = MenuHook::getAgentPluginNames("Agents");
117  $agentList = array_merge($plainAgentList, $parmAgentList);
118  foreach ($agentList as $agentName) {
119  if (in_array($agentName, $agentsToStart)) {
120  $agents[$agentName] = plugin_find($agentName);
121  }
122  }
123 
124  $jobId = JobAddJob(Auth::getUserId(), Auth::getGroupId(), $upload->getFilename(), $uploadId);
125  $errorMsg = '';
126  foreach ($parmAgentList as $parmAgent) {
127  $agent = plugin_find($parmAgent);
128  $agent->scheduleAgent($jobId, $uploadId, $errorMsg, $request);
129  }
130 
131  foreach ($agents as &$agent) {
132  if (!empty($mimetypeIgnore)) {
133  $rv = $agent->AgentAdd($jobId, $uploadId, $errorMsg, array("agent_mimetype"), $mimetypeIgnore);
134  } else {
135  $rv = $agent->AgentAdd($jobId, $uploadId, $errorMsg, array());
136  }
137  if ($rv == -1) {
138  return $errorMsg;
139  }
140  }
141  return $jobId;
142  }
143 
151  public function scheduleAgents($uploadId, $agentsToStart, Request $request)
152  {
153  return $this->agentsAdd($uploadId, $agentsToStart, $request);
154  }
155 }
156 
157 register_plugin(new AgentAdder());
GetRunnableJobList()
Get runnable job list, the process is below:
agentsAdd($uploadId, $agentsToStart, Request $request)
Add an upload to multiple agents.
Definition: agent-add.php:99
Traceback_uri()
Get the URI without query to this location.
FolderListOption($ParentFolder, $Depth, $IncludeTop=1, $SelectId=-1, $linkParent=false, $OldParent=0)
Create the folder tree, using OPTION tags.
FolderListUploads_perm($ParentFolder, $perm)
Returns an array of uploads in a folder.
render($templateName, $vars=null, $headers=null)
ActiveHTTPscript($RequestName, $IncludeScriptTags=1)
Given a function name, create the JavaScript needed for doing the request.
handle(Request $request)
Definition: agent-add.php:43
FolderGetTop()
DEPRECATED! Find the top-of-tree folder_pk for the current user.
plugin_find($pluginName)
Given the official name of a plugin, return the $Plugins object.
scheduleAgents($uploadId, $agentsToStart, Request $request)
Add an upload to multiple agents (wrapper for agentsAdd()).
Definition: agent-add.php:151
#define PERM_WRITE
Read-Write permission.
Definition: libfossology.h:45