forked from opendcim/openDCIM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.inc.php
139 lines (120 loc) · 4.33 KB
/
config.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
class Config{
var $ParameterArray;
var $defaults;
function Config(){
global $dbh;
//Get parameter value pairs from fac_Config
$sql='select Parameter, Value, DefaultVal from fac_Config';
$sth=$dbh->prepare($sql);
$sth->execute();
while ($row = $sth->fetch()) {
if ($row['Parameter']== 'ClassList'){
$List = explode(', ', $row['Value']);
$this->ParameterArray[$row['Parameter']]=$List;
$this->defaults[$row['Parameter']]=$row['DefaultVal'];
}else{
// this allows us to support quotes in paths for whatever reason
if(preg_match('/.*path$|PDFLogoFile/',$row['Parameter'])){
$this->ParameterArray[$row['Parameter']]=html_entity_decode($row['Value'],ENT_QUOTES);
}else{
$this->ParameterArray[$row['Parameter']]=$row['Value'];
}
$this->defaults[$row['Parameter']]=$row['DefaultVal'];
}
}
return;
}
function UpdateConfig(){
global $dbh;
foreach($this->ParameterArray as $key=>$value){
if ($key=='ClassList'){
$numItems=count($value);
$i=0;
$valueStr='';
foreach($value as $item){
$valueStr .= $item;
if($i+1 != $numItems){
$valueStr.=', ';
}
$i++;
}
$sql='update fac_Config set Value=\''.sanitize($valueStr).'\' where Parameter=\''.$key.'\'';
if(!$dbh->query($sql)){
$info=$dbh->errorInfo();
error_log("UpdateConfig::PDO Error: {$info[2]} SQL=$sql");
}
}else{
if(preg_match('/[m|w]Date/',$key)){
if($value!='now'){$value='blank';} // if someone puts a weird value in default it back to blank
}
$sql="update fac_Config set Value=\"".sanitize($value)."\" where Parameter=\"$key\";";
if(!$dbh->query($sql)){
$info=$dbh->errorInfo();
error_log("UpdateConfig::PDO Error: {$info[2]} SQL=$sql");
}
}
}
return;
}
static function UpdateParameter($parameter,$value){
global $dbh;
if(is_null($parameter) || is_null($value)){
return false;
}else{
$sql="UPDATE fac_Config SET Value=\"$value\" WHERE Parameter=\"$parameter\";";
if($dbh->query($sql)){
return $value;
}else{
$info=$dbh->errorInfo();
error_log("UpdateParamter::PDO Error: {$info[2]} SQL=$sql");
return false;
}
}
}
static function RevertToDefault($parameter){
global $dbh;
if($parameter=='none'){
$sql='UPDATE fac_Config SET Value=DefaultVal;';
}else{
$sql="UPDATE fac_Config SET Value=DefaultVal WHERE Parameter=\"$parameter\";";
}
$dbh->query($sql);
return;
}
function Rebuild (){
/* Rebuild: This function should only be needed after something like the version erasing glitch from 1.1 and 1.2.
At this time it is possible to get unwanted duplicate configuration parameters and this will clean
them.
I am not sanitizing input here because it should have no user interaction. Read from the db, flush
db, write unique values back to the db.
*/
global $dbh;
$sql='select * from fac_Config';
$uniqueconfig=array();
// Build array of unique config parameters
foreach ( $dbh->query( $sql ) as $row){
if(isset($uniqueconfig[$row['Parameter']]['Value'])){
// if value in the array is equal to the default value AND the current value is different from the value in the array update the value in the array
if($uniqueconfig[$row['Parameter']]['Value']==$row['DefaultVal'] && $uniqueconfig[$row['Parameter']]['Value']!=$row['Value']){
$uniqueconfig[$row['Parameter']]['Value']=$row['Value'];
}
}else{
// value wasn't set in the array so we'll take whatever we're given even if it is the default value
$uniqueconfig[$row['Parameter']]['Value']=$row['Value'];
}
// the following aren't user configurable so no need to check for differences
$uniqueconfig[$row['Parameter']]['UnitOfMeasure']=$row['UnitOfMeasure'];
$uniqueconfig[$row['Parameter']]['ValType']=$row['ValType'];
$uniqueconfig[$row['Parameter']]['DefaultVal']=$row['DefaultVal'];
}
// Empty config table
$dbh->exec('TRUNCATE TABLE fac_Config;');
// Rebuild config table from cleaned array
$sth = $dbh->prepare( "INSERT INTO fac_Config VALUES ( :key, :value, :unitofmeasure, :valtype, :defaultval )" );
foreach($uniqueconfig as $key => $row){
$sth->execute( array( ':key' => $key, ':value' => $row['Value'], ':unitofmeasure' => $row['UnitOfMeasure'], ':valtype' => $row['ValType'], ':defaultval' => $row['DefaultVal'] ) );
}
}
}
?>