FOSSology  4.4.0
Open Source License Compliance by Open Source Software
admin-config.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2011-2013 Hewlett-Packard Development Company, L.P.
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
9 use GuzzleHttp\Client;
10 use GuzzleHttp\Exception\RequestException;
11 
12 define("TITLE_FOCONFIG", _("Configuration Variables"));
13 
18 class foconfig extends FO_Plugin
19 {
20  var $CreateAttempts = 0;
22  private $dbManager;
23 
24  function __construct()
25  {
26  $this->Name = "foconfig";
27  $this->Title = TITLE_FOCONFIG;
28  $this->MenuList = "Admin::Customize";
29  $this->DBaccess = PLUGIN_DB_ADMIN;
30  $this->PluginLevel = 50; // run before 'regular' plugins
31  parent::__construct();
32  $this->dbManager = $GLOBALS['container']->get('db.manager');
33  }
34 
38  function HTMLout()
39  {
40  global $PG_CONN;
41  $OutBuf="";
42 
43  /* get config rows from sysconfig table */
44  $sql = "select * from sysconfig order by group_name, group_order";
45  $result = pg_query($PG_CONN, $sql);
46  DBCheckResult($result, $sql, __FILE__, __LINE__);
47 
48  $Group = "";
49  $InputStyle = "style='background-color:#dbf0f7'";
50  $OutBuf .= '<style> table.myTable > tbody > tr:first-child > td:first-child{width:20%} </style>';
51  $OutBuf .= "<form method='POST'>";
52  while ($row = pg_fetch_assoc($result)) {
53  if ($Group != $row['group_name']) {
54  if ($Group) {
55  $OutBuf .= "</table><br>";
56  }
57  $Group = $row['group_name'];
58  $OutBuf .= '<table border=1 class="myTable table table-striped" style="border-collapse: unset;">';
59  }
60 
61  $OutBuf .= "<tr><td>$row[ui_label]</td><td>";
62  switch ($row['vartype']) {
63  case CONFIG_TYPE_INT:
64  case CONFIG_TYPE_TEXT:
65  $ConfVal = htmlentities($row['conf_value']);
66  $OutBuf .= "<INPUT type='text' name='new[$row[variablename]]' size='70' value='$ConfVal' title='$row[description]' $InputStyle>";
67  $OutBuf .= "<br>$row[description]";
68  break;
70  $ConfVal = htmlentities($row['conf_value']);
71  $OutBuf .= "<br><textarea name='new[$row[variablename]]' rows=3 cols=80 title='$row[description]' $InputStyle>$ConfVal</textarea>";
72  $OutBuf .= "<br>$row[description]";
73  break;
75  $ConfVal = htmlentities($row['conf_value']);
76  $OutBuf .= "<INPUT type='password' name='new[$row[variablename]]' size='70' value='$ConfVal' title='$row[description]' $InputStyle>";
77  $OutBuf .= "<br>$row[description]";
78  break;
79  case CONFIG_TYPE_DROP:
80  $ConfVal = htmlentities($row['conf_value']);
81  $Options = explode("|",$row['option_value']);
82  $OutBuf .= "<select name='new[$row[variablename]]' title='$row[description]' $InputStyle>";
83  foreach ($Options as $Option) {
84  $matches = array();
85  preg_match('/([ \\w]+)[{​​​​](.*)[}​​​​]/', $Option, $matches);
86  $Option_display = $matches[1];
87  $Option_value = $matches[2];
88  $OutBuf .= "<option $InputStyle value='$Option_value' ";
89  if ($ConfVal == $Option_value) {
90  $OutBuf .= "selected";
91  }
92  $OutBuf .= ">$Option_display</option>";
93  }
94  $OutBuf .= "</select>";
95  $OutBuf .= "<br>$row[description]";
96  break;
97  case CONFIG_TYPE_BOOL:
98  $ConfVal = filter_var($row['conf_value'], FILTER_VALIDATE_BOOLEAN);
99  $checked = $ConfVal ? "checked" : "";
100  $ConfVal = $ConfVal ? "true" : "false";
101  $OutBuf .= "<input type='checkbox' name='new[" . $row['variablename'] .
102  "]' id='" . $row['variablename'] . "' value='true' title='" .
103  $row['description'] . "' $InputStyle $checked />";
104  $OutBuf .= "<label for='" . $row['variablename'] .
105  "'>" . $row['description'] . "</label>";
106  break;
107  default:
108  $OutBuf .= "Invalid configuration variable. Unknown type.";
109  }
110  $OutBuf .= "</td></tr>";
111  $OutBuf .= "<INPUT type='hidden' name='old[$row[variablename]]' value='$ConfVal'>";
112  }
113  $OutBuf .= "</table>";
114  pg_free_result($result);
115 
116  $btnlabel = _("Update");
117  $OutBuf .= "<p><input type='submit' value='$btnlabel'>";
118  $OutBuf .= "</form>";
119 
120  return $OutBuf;
121  }
122 
126  function Output()
127  {
128  if ($this->State != PLUGIN_STATE_READY) {
129  return;
130  }
131 
132  $newarray = GetParm("new", PARM_RAW);
133  $oldarray = GetParm("old", PARM_RAW);
134 
135  if (!empty($newarray)) {
136  // Get missing keys from new array (unchecked checkboxes are not sent)
137  $boolFalseArray = array_diff_key($oldarray, $newarray);
138  foreach ($boolFalseArray as $varname => $value) {
139  // Make sure it was boolean data
140  $isBoolean = $this->dbManager->getSingleRow("SELECT 1 FROM sysconfig " .
141  "WHERE variablename = $1 AND vartype = " . CONFIG_TYPE_BOOL,
142  array($varname), __METHOD__ . '.checkIfBool');
143  if (! empty($isBoolean)) {
144  $newarray[$varname] = 'false';
145  }
146  }
147  }
148 
149  /* Compare new and old array
150  * and update DB with new values */
151  $UpdateMsg = "";
152  $ErrorMsg="";
153  if (! empty($newarray)) {
154  // Fetch endpoints from OIDC documentation
155  if (! empty($newarray["OidcDiscoveryURL"]) &&
156  $newarray["OidcDiscoveryURL"] != $oldarray["OidcDiscoveryURL"]) {
157  $this->updateOidcEndpoints($newarray, $oldarray);
158  }
159  foreach ($newarray as $VarName => $VarValue) {
160  if ($VarValue != $oldarray[$VarName]) {
161  /* get validation_function row from sysconfig table */
162  $sys_array = $this->dbManager->getSingleRow("select validation_function, ui_label from sysconfig where variablename=$1",array($VarName),__METHOD__.'.getVarNameData');
163  $validation_function = $sys_array['validation_function'];
164  $ui_label = $sys_array['ui_label'];
165  $is_empty = empty($validation_function);
166  /* 1. the validation_function is empty
167  2. the validation_function is not empty, and after checking, the value is valid
168  update sysconfig table
169  */
170  if ($is_empty || (! $is_empty && (1 == $validation_function($VarValue)))) {
171  $this->dbManager->getSingleRow(
172  "update sysconfig set conf_value=$1 where variablename=$2",
173  array($VarValue, $VarName), __METHOD__ . '.setVarNameData');
174  if (! empty($UpdateMsg)) {
175  $UpdateMsg .= ", ";
176  }
177  $UpdateMsg .= $VarName;
178  } else if (! $is_empty && (0 == $validation_function($VarValue))) {
179  /*
180  * the validation_function is not empty, but after checking, the value
181  * is invalid
182  */
183  if (! strcmp($validation_function, 'check_boolean')) {
184  $warning_msg = _(
185  "Error: You set $ui_label to ".htmlspecialchars($VarValue).". Valid values are 'true' and 'false'.");
186  echo "<script>alert('$warning_msg');</script>";
187  } else if (strpos($validation_function, "url")) {
188  $warning_msg = _(
189  "Error: $ui_label ".htmlspecialchars($VarValue).", is not a reachable URL.");
190  echo "<script>alert('$warning_msg');</script>";
191  }
192 
193  if (! empty($ErrorMsg)) {
194  $ErrorMsg .= ", ";
195  }
196  $ErrorMsg .= $VarName;
197 
198  }
199  }
200  }
201  if (! empty($UpdateMsg)) {
202  $UpdateMsg .= _(" updated.");
203  }
204  if (! empty($ErrorMsg)) {
205  $ErrorMsg .= _(" Error occurred.");
206  }
207  }
208 
209  $OutBuf = '';
210  if ($this->OutputType == 'HTML') {
211  $OutBuf .= "<div>";
212  if ($UpdateMsg) {
213  $OutBuf .= "<span style='background-color:#99FF99'>$UpdateMsg</style>";
214  }
215  if ($ErrorMsg) {
216  $OutBuf .= "<span style='background-color:#FF8181'>$ErrorMsg</style><hr>";
217  }
218  $OutBuf .= "</div> <hr>";
219  $OutBuf .= $this->HTMLout();
220  }
221  $this->vars['content'] = $OutBuf;
222  }
223 
234  private function updateOidcEndpoints(&$newarray, &$oldarray)
235  {
236  global $SysConf;
237  $client = new Client();
238  $proxy = [];
239  if (array_key_exists('http_proxy', $SysConf['FOSSOLOGY']) &&
240  ! empty($SysConf['FOSSOLOGY']['http_proxy'])) {
241  $proxy['http'] = $SysConf['FOSSOLOGY']['http_proxy'];
242  }
243  if (array_key_exists('https_proxy', $SysConf['FOSSOLOGY']) &&
244  ! empty($SysConf['FOSSOLOGY']['https_proxy'])) {
245  $proxy['https'] = $SysConf['FOSSOLOGY']['https_proxy'];
246  }
247  if (array_key_exists('no_proxy', $SysConf['FOSSOLOGY']) &&
248  ! empty($SysConf['FOSSOLOGY']['no_proxy'])) {
249  $proxy['no'] = explode(',', $SysConf['FOSSOLOGY']['no_proxy']);
250  }
251  try {
252  $res = $client->request("GET", $newarray["OidcDiscoveryURL"], [
253  "proxy" => $proxy
254  ]);
255  } catch (RequestException $e) {
256  return;
257  }
258  if ($res->getStatusCode() !== 200) {
259  return;
260  }
261  $body = (string)$res->getBody();
262  $body = json_decode($body, true);
263  // Reset old values to make sure the update happens
264  $oldarray["OidcIssuer"] = "";
265  $oldarray["OidcAuthorizeURL"] = "";
266  $oldarray["OidcAccessTokenURL"] = "";
267  $oldarray["OidcResourceURL"] = "";
268  $oldarray["OidcJwksURL"] = "";
269  $newarray["OidcIssuer"] = $body["issuer"];
270  $newarray["OidcAuthorizeURL"] = $body["authorization_endpoint"];
271  $newarray["OidcAccessTokenURL"] = $body["token_endpoint"];
272  $newarray["OidcResourceURL"] = $body["userinfo_endpoint"];
273  $newarray["OidcJwksURL"] = $body["jwks_uri"];
274  }
275 }
276 
277 $NewPlugin = new foconfig;
278 $NewPlugin->Initialize();
This is the Plugin class. All plugins should:
Definition: FO_Plugin.php:57
Definition: state.hpp:16
display and set FOSSology configuration
Output()
Generate output.
updateOidcEndpoints(&$newarray, &$oldarray)
Update OIDC endpoints from OIDC discovery document.
HTMLout()
Generate HTML output.
__construct()
base constructor. Most plugins will just use this
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:187
const PARM_RAW
Definition: common-parm.php:22
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:46
const CONFIG_TYPE_TEXTAREA
const CONFIG_TYPE_TEXT
const CONFIG_TYPE_DROP
const CONFIG_TYPE_INT
const CONFIG_TYPE_BOOL
const CONFIG_TYPE_PASSWORD
#define PLUGIN_DB_ADMIN
Plugin requires admin level permission on DB.
Definition: libfossology.h:39
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16