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 'wipScannerUpdates':
115  $this->addScannerDependencies($dependencies, $request);
116  $rulebits |= 0x8;
117  break;
118  case 'ojoNoContradiction':
119  if ($checkAgentNomos) {
120  $dependencies[] = 'agent_nomos';
121  }
122  $dependencies[] = 'agent_ojo';
123  $rulebits |= 0x10;
124  break;
125  case 'copyrightDeactivation':
126  if ($checkAgentCopyright) {
127  $dependencies[] = 'agent_copyright';
128  }
129  $rulebits |= 0x20;
130  break;
131  case 'copyrightDeactivationClutterRemoval':
132  if ($checkAgentCopyright) {
133  $dependencies[] = 'agent_copyright';
134  }
135  $rulebits |= 0x40;
136  break;
137  case 'licenseTypeConc':
138  $dependencies[] = 'agent_compatibility';
139  $rulebits |= 0x80;
140  break;
141  case 'kotobaAgent':
142  $rulebits |= 0x100;
143  break;
144  }
145  }
146 
147  if (empty($rulebits)) {
148  return 0;
149  }
150 
151  // If only kotobaAgent is selected, schedule kotoba_bulk agent directly without scheduling decider
152  if ($rulebits == 0x100) {
153  $kotobaPlugin = \plugin_find("agent_kotoba");
154  if ($kotobaPlugin !== null) {
155  return $kotobaPlugin->AgentAdd($jobId, $uploadId, $errorMsg, array(), null, $request);
156  }
157  return 0;
158  }
159 
160  // If kotobaAgent is selected along with other rules, add kotoba_bulk as dependency
161  if ($rulebits & 0x100) {
162  $dependencies[] = 'agent_kotoba';
163  }
164 
165  $args = self::RULES_FLAG . $rulebits;
166 
167  if ($rulebits & 0x80) {
168  $licenseType = $this->getLicenseTypeConf($request);
169  if ($licenseType != "") {
170  $args .= " " . self::LICENSE_FLAG . escapeshellarg($licenseType);
171  }
172  }
173 
174  return parent::AgentAdd($jobId, $uploadId, $errorMsg,
175  array_unique($dependencies), $args, $request);
176  }
177 
183  protected function addScannerDependencies(&$dependencies, Request $request)
184  {
185  $agentList = $request->get('agents') ?: array();
186  if (!is_array($agentList)) {
187  $agentList = array();
188  }
189  foreach (array('agent_nomos', 'agent_monk', 'agent_ninka') as $agentName) {
190  if (in_array($agentName, $dependencies)) {
191  continue;
192  }
193  if ($request->get('Check_'.$agentName)) {
194  $dependencies[] = $agentName;
195  continue;
196  }
197  if (in_array($agentName, $agentList)) {
198  $dependencies[] = $agentName;
199  }
200  }
201  }
202 
207  public function preInstall()
208  {
209  menu_insert("ParmAgents::" . $this->Title, 0, $this->Name);
210  }
211 
217  private function getLicenseTypeConf(Request $request)
218  {
219  global $SysConf;
220  $licenseTypes = array_map('trim', explode(',',
221  $SysConf['SYSCONFIG']['LicenseTypes']));
222  $licenseType = $request->get("licenseTypeConc", "");
223  if (!is_string($licenseType)) {
224  return "";
225  }
226  $licenseType = trim($licenseType);
227  if (in_array($licenseType, $licenseTypes)) {
228  return $licenseType;
229  }
230  return "";
231  }
232 
237  public function isSpacyInstalled()
238  {
239  global $SysConf;
240  return file_exists("/home/" .
241  $SysConf['DIRECTORIES']['PROJECTUSER'] . "/pythondeps/bin/spacy");
242  }
243 }
244 
245 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