FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
admin-config.php
1 <?php
2 /***********************************************************
3  Copyright (C) 2011-2013 Hewlett-Packard Development Company, L.P.
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU General Public License
7  version 2 as published by the Free Software Foundation.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License along
15  with this program; if not, write to the Free Software Foundation, Inc.,
16  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 ***********************************************************/
18 
20 
21 define("TITLE_FOCONFIG", _("Configuration Variables"));
22 
27 class foconfig extends FO_Plugin
28 {
29  var $CreateAttempts = 0;
31  private $dbManager;
32 
33  function __construct()
34  {
35  $this->Name = "foconfig";
36  $this->Title = TITLE_FOCONFIG;
37  $this->MenuList = "Admin::Customize";
38  $this->DBaccess = PLUGIN_DB_ADMIN;
39  $this->PluginLevel = 50; // run before 'regular' plugins
40  parent::__construct();
41  $this->dbManager = $GLOBALS['container']->get('db.manager');
42  }
43 
47  function HTMLout()
48  {
49  global $PG_CONN;
50  $OutBuf="";
51 
52  /* get config rows from sysconfig table */
53  $sql = "select * from sysconfig order by group_name, group_order";
54  $result = pg_query($PG_CONN, $sql);
55  DBCheckResult($result, $sql, __FILE__, __LINE__);
56 
57  $Group = "";
58  $InputStyle = "style='background-color:#dbf0f7'";
59  $OutBuf .= "<form method='POST'>";
60  while ($row = pg_fetch_assoc($result)) {
61  if ($Group != $row['group_name']) {
62  if ($Group) {
63  $OutBuf .= "</table><br>";
64  }
65  $Group = $row['group_name'];
66  $OutBuf .= "<table border=1>";
67  }
68 
69  $OutBuf .= "<tr><td>$row[ui_label]</td><td>";
70  switch ($row['vartype']) {
71  case CONFIG_TYPE_INT:
72  case CONFIG_TYPE_TEXT:
73  $ConfVal = htmlentities($row['conf_value']);
74  $OutBuf .= "<INPUT type='text' name='new[$row[variablename]]' size='70' value='$ConfVal' title='$row[description]' $InputStyle>";
75  $OutBuf .= "<br>$row[description]";
76  break;
78  $ConfVal = htmlentities($row['conf_value']);
79  $OutBuf .= "<br><textarea name='new[$row[variablename]]' rows=3 cols=80 title='$row[description]' $InputStyle>$ConfVal</textarea>";
80  $OutBuf .= "<br>$row[description]";
81  break;
83  $ConfVal = htmlentities($row['conf_value']);
84  $OutBuf .= "<INPUT type='password' name='new[$row[variablename]]' size='70' value='$ConfVal' title='$row[description]' $InputStyle>";
85  $OutBuf .= "<br>$row[description]";
86  break;
87  case CONFIG_TYPE_DROP:
88  $ConfVal = htmlentities($row['conf_value']);
89  $Options = explode("|",$row['option_value']);
90  $OutBuf .= "<select name='new[$row[variablename]]' title='$row[description]' $InputStyle>";
91  foreach ($Options as $Option) {
92  $matches = array();
93  preg_match('/(\\w+)[{](.*)[}]/', $Option, $matches);
94  $Option_display = $matches[1];
95  $Option_value = $matches[2];
96  $OutBuf .= "<option $InputStyle value='$Option_value' ";
97  if ($ConfVal == $Option_value) {
98  $OutBuf .= "selected";
99  }
100  $OutBuf .= ">$Option_display</option>";
101  }
102  $OutBuf .= "</select>";
103  $OutBuf .= "<br>$row[description]";
104  break;
105  default:
106  $OutBuf .= "Invalid configuration variable. Unknown type.";
107  }
108  $OutBuf .= "</td></tr>";
109  $OutBuf .= "<INPUT type='hidden' name='old[$row[variablename]]' value='$ConfVal'>";
110  }
111  $OutBuf .= "</table>";
112  pg_free_result($result);
113 
114  $btnlabel = _("Update");
115  $OutBuf .= "<p><input type='submit' value='$btnlabel'>";
116  $OutBuf .= "</form>";
117 
118  return $OutBuf;
119  }
120 
124  function Output()
125  {
126  if ($this->State != PLUGIN_STATE_READY) {
127  return;
128  }
129 
130  $newarray = GetParm("new", PARM_RAW);
131  $oldarray = GetParm("old", PARM_RAW);
132 
133  /* Compare new and old array
134  * and update DB with new values */
135  $UpdateMsg = "";
136  if (! empty($newarray)) {
137  foreach ($newarray as $VarName => $VarValue) {
138  if ($VarValue != $oldarray[$VarName]) {
139  /* get validation_function row from sysconfig table */
140  $sys_array = $this->dbManager->getSingleRow("select validation_function, ui_label from sysconfig where variablename=$1",array($VarName),__METHOD__.'.getVarNameData');
141  $validation_function = $sys_array['validation_function'];
142  $ui_label = $sys_array['ui_label'];
143  $is_empty = empty($validation_function);
144  /* 1. the validation_function is empty
145  2. the validation_function is not empty, and after checking, the value is valid
146  update sysconfig table
147  */
148  if ($is_empty || (! $is_empty && (1 == $validation_function($VarValue)))) {
149  $this->dbManager->getSingleRow(
150  "update sysconfig set conf_value=$1 where variablename=$2",
151  array($VarValue, $VarName), __METHOD__ . '.setVarNameData');
152  if (! empty($UpdateMsg)) {
153  $UpdateMsg .= ", ";
154  }
155  $UpdateMsg .= $VarName;
156  } else if (! $is_empty && (0 == $validation_function($VarValue))) {
157  /*
158  * the validation_function is not empty, but after checking, the value
159  * is invalid
160  */
161  if (! strcmp($validation_function, 'check_boolean')) {
162  $warning_msg = _(
163  "Error: You set $ui_label to $VarValue. Valid values are 'true' and 'false'.");
164  echo "<script>alert('$warning_msg');</script>";
165  } else if (strpos($validation_function, "url")) {
166  $warning_msg = _(
167  "Error: $ui_label $VarValue, is not a reachable URL.");
168  echo "<script>alert('$warning_msg');</script>";
169  }
170  }
171  }
172  }
173  if (! empty($UpdateMsg)) {
174  $UpdateMsg .= _(" updated.");
175  }
176  }
177 
178  $OutBuf = '';
179  if ($this->OutputType == 'HTML') {
180  if ($UpdateMsg) {
181  $OutBuf .= "<span style='background-color:#ff8a8a'>$UpdateMsg</style><hr>";
182  }
183  $OutBuf .= $this->HTMLout();
184  }
185  $this->vars['content'] = $OutBuf;
186  }
187 }
188 
189 $NewPlugin = new foconfig;
190 $NewPlugin->Initialize();
const CONFIG_TYPE_TEXTAREA
const PARM_RAW
Definition: common-parm.php:33
#define PLUGIN_DB_ADMIN
Plugin requires admin level permission on DB.
Definition: libfossology.h:51
Output()
Generate output.
const CONFIG_TYPE_PASSWORD
Definition: state.hpp:26
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:57
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28
This is the Plugin class. All plugins should:
Definition: FO_Plugin.php:67
HTMLout()
Generate HTML output.
const CONFIG_TYPE_DROP
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN
const CONFIG_TYPE_TEXT
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:198
display and set FOSSology configuration
const CONFIG_TYPE_INT