FOSSology  4.7.1
Open Source License Compliance by Open Source Software
DeciderAgentPlugin.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014-2018 Siemens AG
4 
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
10 use Symfony\Component\HttpFoundation\Request;
11 
12 include_once(__DIR__ . "/../agent/version.php");
13 
19 {
20  const RULES_FLAG = "-r";
21  const LICENSE_FLAG = "-t";
22 
23  function __construct()
24  {
25  $this->Name = "agent_decider";
26  $this->Title = _("Automatic Concluded License Decider, based on scanners Matches");
27  $this->AgentName = AGENT_DECIDER_NAME;
28 
29  parent::__construct();
30  }
31 
32 
38  public function renderContent(&$vars)
39  {
40  global $SysConf;
41  $renderer = $GLOBALS['container']->get('twig.environment');
42  $vars['isNinkaInstalled'] = false;
43  if ($ninkaUi=plugin_find('agent_ninka')) {
44  $vars['isNinkaInstalled'] = $ninkaUi->isNinkaInstalled();
45  }
46  $vars['isSpacyInstalled'] = $this->isSpacyInstalled();
47  $licenseTypes = array_map('trim', explode(',',
48  $SysConf['SYSCONFIG']['LicenseTypes']));
49  $vars['licenseTypes'] = array_combine($licenseTypes, $licenseTypes);
50  return $renderer->load('agent_decider.html.twig')->render($vars);
51  }
52 
58  public function renderFoot(&$vars)
59  {
60  return "";
61  }
62 
63  public function getScriptIncludes(&$vars)
64  {
65  return "";
66  }
67 
76  public function scheduleAgent($jobId, $uploadId, &$errorMsg, $request)
77  {
78  $dependencies = array();
79 
80  $rules = $request->get('deciderRules', []);
81  if (!is_array($rules)) {
82  $rules = [];
83  }
84  $agents = $request->get('agents', []);
85  if (!is_array($agents)) {
86  $agents = [];
87  }
88  if (in_array('agent_nomos', $agents)) {
89  $checkAgentNomos = true;
90  } else {
91  $checkAgentNomos = $request->get('Check_agent_nomos', false);
92  }
93 
94  if (in_array('agent_copyright', $agents)) {
95  $checkAgentCopyright = true;
96  } else {
97  $checkAgentCopyright = $request->get('Check_agent_copyright') ?: false;
98  }
99  $rulebits = 0;
100 
101  foreach ($rules as $rule) {
102  switch ($rule) {
103  case 'nomosInMonk':
104  $dependencies[] = 'agent_nomos';
105  $dependencies[] = 'agent_monk';
106  $rulebits |= 0x1;
107  break;
108  case 'nomosMonkNinka':
109  $dependencies[] = 'agent_nomos';
110  $dependencies[] = 'agent_monk';
111  $dependencies[] = 'agent_ninka';
112  $rulebits |= 0x2;
113  break;
114  case 'reuseBulk':
115  $dependencies[] = 'agent_nomos';
116  $dependencies[] = 'agent_monk';
117  $dependencies[] = 'agent_reuser';
118  $rulebits |= 0x4;
119  break;
120  case 'wipScannerUpdates':
121  $this->addScannerDependencies($dependencies, $request);
122  $rulebits |= 0x8;
123  break;
124  case 'ojoNoContradiction':
125  if ($checkAgentNomos) {
126  $dependencies[] = 'agent_nomos';
127  }
128  $dependencies[] = 'agent_ojo';
129  $rulebits |= 0x10;
130  break;
131  case 'copyrightDeactivation':
132  if ($checkAgentCopyright) {
133  $dependencies[] = 'agent_copyright';
134  }
135  $rulebits |= 0x20;
136  break;
137  case 'copyrightDeactivationClutterRemoval':
138  if ($checkAgentCopyright) {
139  $dependencies[] = 'agent_copyright';
140  }
141  $rulebits |= 0x40;
142  break;
143  case 'licenseTypeConc':
144  $dependencies[] = 'agent_compatibility';
145  $rulebits |= 0x80;
146  break;
147  case 'kotobaAgent':
148  $rulebits |= 0x100;
149  break;
150  }
151  }
152 
153  if (empty($rulebits)) {
154  return 0;
155  }
156 
157  // If only kotobaAgent is selected, schedule kotoba_bulk agent directly without scheduling decider
158  if ($rulebits == 0x100) {
159  $kotobaPlugin = \plugin_find("agent_kotoba");
160  if ($kotobaPlugin !== null) {
161  return $kotobaPlugin->AgentAdd($jobId, $uploadId, $errorMsg, array(), null, $request);
162  }
163  return 0;
164  }
165 
166  // If kotobaAgent is selected along with other rules, add kotoba_bulk as dependency
167  if ($rulebits & 0x100) {
168  $dependencies[] = 'agent_kotoba';
169  }
170 
171  $args = self::RULES_FLAG . $rulebits;
172 
173  if ($rulebits & 0x80) {
174  $licenseType = $this->getLicenseTypeConf($request);
175  if ($licenseType != "") {
176  $args .= " " . self::LICENSE_FLAG . escapeshellarg($licenseType);
177  }
178  }
179 
180  return parent::AgentAdd($jobId, $uploadId, $errorMsg,
181  array_unique($dependencies), $args, $request);
182  }
183 
189  protected function addScannerDependencies(&$dependencies, Request $request)
190  {
191  $agentList = $request->get('agents') ?: array();
192  if (!is_array($agentList)) {
193  $agentList = array();
194  }
195  foreach (array('agent_nomos', 'agent_monk', 'agent_ninka') as $agentName) {
196  if (in_array($agentName, $dependencies)) {
197  continue;
198  }
199  if ($request->get('Check_'.$agentName)) {
200  $dependencies[] = $agentName;
201  continue;
202  }
203  if (in_array($agentName, $agentList)) {
204  $dependencies[] = $agentName;
205  }
206  }
207  }
208 
213  public function preInstall()
214  {
215  menu_insert("ParmAgents::" . $this->Title, 0, $this->Name);
216  }
217 
223  private function getLicenseTypeConf(Request $request)
224  {
225  global $SysConf;
226  $licenseTypes = array_map('trim', explode(',',
227  $SysConf['SYSCONFIG']['LicenseTypes']));
228  $licenseType = $request->get("licenseTypeConc", "");
229  if (!is_string($licenseType)) {
230  return "";
231  }
232  $licenseType = trim($licenseType);
233  if (in_array($licenseType, $licenseTypes)) {
234  return $licenseType;
235  }
236  return "";
237  }
238 
243  public function isSpacyInstalled()
244  {
245  global $SysConf;
246  return file_exists("/home/" .
247  $SysConf['DIRECTORIES']['PROJECTUSER'] . "/pythondeps/bin/spacy");
248  }
249 }
250 
251 register_plugin(new DeciderAgentPlugin());
UI plugin for DeciderAgent.
renderFoot(&$vars)
Render footer HTML.
addScannerDependencies(&$dependencies, Request $request)
Add dependencies on DeciderAgent.
getLicenseTypeConf(Request $request)
scheduleAgent($jobId, $uploadId, &$errorMsg, $request)
Schedule decider agent.
renderContent(&$vars)
Render HTML from template.
menu_insert($Path, $LastOrder=0, $URI=NULL, $Title=NULL, $Target=NULL, $HTML=NULL)
Given a Path, order level for the last item, and optional plugin name, insert the menu item.
plugin_find($pluginName)
Given the official name of a plugin, return the $Plugins object.
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:690