FOSSology  4.4.0
Open Source License Compliance by Open Source Software
admin-fossdash-config.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © Darshan Kansagara <kansagara.darshan97@gmail.com>
4 
5  SPDX-License-Identifier: GPL-2.0-only
6  Author: Darshan Kansagara <kansagara.darshan97@gmail.com>
7 */
8 
10 
11 define("TITLE_FOSSDASH_CONFIG", _("Fossdash Configuration"));
12 
18 {
19  var $CreateAttempts = 0;
21  private $dbManager;
22 
23  function __construct()
24  {
25  $this->Name = "FossdashConfig";
26  $this->Title = TITLE_FOSSDASH_CONFIG;
27  $this->MenuList = "Admin::Fossdash";
28  $this->DBaccess = PLUGIN_DB_ADMIN;
29  $this->PluginLevel = 50; // run before 'regular' plugins
30  parent::__construct();
31  $this->dbManager = $GLOBALS['container']->get('db.manager');
32  }
33 
37  function HTMLout()
38  {
39  global $PG_CONN;
40  $OutBuf="";
41 
42  /* get config rows from fossdashconfig table */
43  $sql = "select * from fossdashconfig order by group_name, group_order";
44  $result = pg_query($PG_CONN, $sql);
45  DBCheckResult($result, $sql, __FILE__, __LINE__);
46 
47  $Group = "";
48  $InputStyle = "style='background-color:#dbf0f7'";
49  $OutBuf .= '<style> table.myTable > tbody > tr:first-child > td:first-child{width:20%} </style>';
50  $OutBuf .= "<form method='POST' enctype='multipart/form-data'>";
51  while ($row = pg_fetch_assoc($result)) {
52  if ($Group != $row['group_name']) {
53  if ($Group) {
54  $OutBuf .= '</table><br>';
55  }
56  $Group = $row['group_name'];
57  $OutBuf .= '<table border=1 class="myTable table table-striped" style="border-collapse: unset;" >';
58  }
59  if ($row['variablename']=="InfluxDBUser" || $row['variablename']=="InfluxDBUserPassword") {
60  $OutBuf .= "<tr id='rowId$row[variablename]' style='display: none;'><td>$row[ui_label]</td><td>";
61  } else {
62  $OutBuf .= "<tr id='rowId$row[variablename]'><td>$row[ui_label]</td><td>";
63  }
64 
65  switch ($row['vartype']) {
66  case CONFIG_TYPE_INT:
67  case CONFIG_TYPE_TEXT:
68  $ConfVal = htmlentities($row['conf_value']);
69  $OutBuf .= "<INPUT type='text' name='new[$row[variablename]]' size='70' value='$ConfVal' title='$row[description]' $InputStyle>";
70  $OutBuf .= "<br>$row[description]";
71  break;
73  $ConfVal = htmlentities($row['conf_value']);
74  $OutBuf .= "<br><textarea name='new[$row[variablename]]' rows=3 cols=100 title='$row[description]' $InputStyle>$ConfVal</textarea>";
75  $OutBuf .= "<br>$row[description]";
76  break;
78  $ConfVal = htmlentities($row['conf_value']);
79  $OutBuf .= "<INPUT type='password' name='new[$row[variablename]]' size='70' value='$ConfVal' title='$row[description]' $InputStyle>";
80  $OutBuf .= "<br>$row[description]";
81  break;
82  case CONFIG_TYPE_DROP:
83  $ConfVal = htmlentities($row['conf_value']);
84  $Options = explode("|",$row['option_value']);
85  $OutBuf .= "<select name='new[$row[variablename]]' title='$row[description]' $InputStyle>";
86  foreach ($Options as $Option) {
87  $matches = array();
88  preg_match('/(\\w+)[{](.*)[}]/', $Option, $matches);
89  $Option_display = $matches[1];
90  $Option_value = $matches[2];
91  $OutBuf .= "<option $InputStyle value='$Option_value' ";
92  if ($ConfVal == $Option_value) {
93  $OutBuf .= "selected";
94  }
95  $OutBuf .= ">$Option_display</option>";
96  }
97  $OutBuf .= "</select>";
98  $OutBuf .= "<br>$row[description]";
99  break;
100  default:
101  $OutBuf .= "Invalid configuration variable. Unknown type.";
102  }
103  $OutBuf .= "</td></tr>";
104  $OutBuf .= "<INPUT type='hidden' name='old[$row[variablename]]' value='$ConfVal'>";
105  }
106  $OutBuf .= "</table>";
107  pg_free_result($result);
108 
109  $btnlabel = _("Update");
110  $OutBuf .= "<p><input type='submit' class='btn btn-secondary btn-sm' style='display: block; margin: 0 auto;' value='$btnlabel'>";
111  $OutBuf .= "</form>";
112 
113  $scriptToHideShow = '
114  <script>
115  function showHide() {
116  if($(\'[name="new[AuthType]"]\').val() == "0") {
117  $("#rowIdInfluxDBToken").show();
118  $("#rowIdInfluxDBUser").hide();
119  $("#rowIdInfluxDBUserPassword").hide();
120 
121  } else {
122  $("#rowIdInfluxDBToken").hide();
123  $("#rowIdInfluxDBUser").show();
124  $("#rowIdInfluxDBUserPassword").show();
125  }
126  }
127  $(function () {
128  $(\'[name="new[AuthType]"]\').change(showHide);
129  });
130 
131  window.onload = function() {
132  showHide()
133  };
134  </script>';
135 
136  $this->renderScripts($scriptToHideShow);
137 
138  return $OutBuf;
139  }
140 
144  function Output()
145  {
146  if ($this->State != PLUGIN_STATE_READY) {
147  return;
148  }
149 
150  $newarray = GetParm("new", PARM_RAW);
151  $oldarray = GetParm("old", PARM_RAW);
152  $LIBEXECDIR = $GLOBALS['SysConf']['DIRECTORIES']['LIBEXECDIR'];
153 
154  /* Compare new and old array
155  * and update DB with new values */
156  $UpdateMsg = "";
157  $ErrorMsg="";
158  if (! empty($newarray)) {
159  foreach ($newarray as $VarName => $VarValue) {
160  if ($VarValue != $oldarray[$VarName]) {
161  /* get validation_function row from fossdashconfig table */
162  $sys_array = $this->dbManager->getSingleRow("select validation_function, ui_label from fossdashconfig 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 fossdashconfig table
169  */
170  if ($is_empty || (! $is_empty && (1 == $validation_function($VarValue)))) {
171  $this->dbManager->getSingleRow(
172  "update fossdashconfig set conf_value=$1 where variablename=$2",
173  array($VarValue, $VarName), __METHOD__ . '.setVarNameData');
174  if ($VarName == "FossdashEnableDisable") {
175  $exec_fossdash_configuration_cmd = "python3 ".$LIBEXECDIR."/fossdash-publish.py fossdash_configure " . escapeshellarg($VarValue);
176  $output = shell_exec($exec_fossdash_configuration_cmd);
177  file_put_contents('php://stderr', "output of the cmd for fossology_configuration(enable/Disable) changed ={$output} \n");
178  } elseif ($VarName == "FossologyInstanceName") {
179  $parameterName = "uuid";
180  $exec_script_uuid_cmd = "python3 ".$LIBEXECDIR."/fossdash-publish.py " . $parameterName;
181  $output = shell_exec($exec_script_uuid_cmd);
182  file_put_contents('php://stderr', "output of cmd for fossology_instance_name changed ={$output} \n");
183  } elseif ($VarName == "FossDashScriptCronSchedule") {
184  $parameterName = "cron";
185  $exec_script_cron_cmd = "python3 ".$LIBEXECDIR."/fossdash-publish.py " . $parameterName;
186  $output = shell_exec($exec_script_cron_cmd);
187  file_put_contents('php://stderr', "output of cmd for cron job changed ={$output} \n");
188  }
189 
190  if (! empty($UpdateMsg)) {
191  $UpdateMsg .= ", ";
192  }
193  $UpdateMsg .= $VarName;
194  } else if (! $is_empty && (0 == $validation_function($VarValue))) {
195  /*
196  * the validation_function is not empty, but after checking, the value
197  * is invalid
198  */
199  if (! empty($ErrorMsg)) {
200  $ErrorMsg .= ", ";
201  }
202  $ErrorMsg .= $VarName;
203 
204  }
205  }
206  }
207  if (! empty($UpdateMsg)) {
208  $UpdateMsg .= _(" updated.");
209  }
210  if (! empty($ErrorMsg)) {
211  $ErrorMsg .= _(" Error occurred.");
212  }
213  }
214 
215  $OutBuf = '';
216  if ($this->OutputType == 'HTML') {
217  $OutBuf .= "<div>";
218  if ($UpdateMsg) {
219  $OutBuf .= "<span style='background-color:#99FF99'>$UpdateMsg</style>";
220  }
221  if ($ErrorMsg) {
222  $OutBuf .= "<span style='background-color:#FF8181'>$ErrorMsg</style><hr>";
223  }
224  $OutBuf .= "</div> <hr>";
225  $OutBuf .= $this->HTMLout();
226  }
227  $this->vars['content'] = $OutBuf;
228  }
229 }
230 
231 $NewPlugin = new FossdashConfig;
232 $NewPlugin->Initialize();
This is the Plugin class. All plugins should:
Definition: FO_Plugin.php:57
renderScripts($scripts)
Render JavaScript in the template's footer.
Definition: FO_Plugin.php:424
display and set FOSSology configuration
HTMLout()
Generate HTML output.
Output()
Generate output.
__construct()
base constructor. Most plugins will just use this
Definition: state.hpp:16
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_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