-
Notifications
You must be signed in to change notification settings - Fork 0
/
siteSettings.php
86 lines (78 loc) · 2.49 KB
/
siteSettings.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
<?php
class siteSettings{
/*Reads and Writes persistent settings for the website.*/
private $dbIniFile = './db_config.ini';
private $configKeys = array(
'server', 'db', 'username', 'password',
'teamnumber', 'eventcode', 'tbakey',
'datatable', 'tbatable', 'pitScouttable', 'strikeScouttable', 'LSTable'
);
public $settings;
function __construct(){
$this->settings = $this->readDbConfig();
}
function readDbConfig(){
// Read dbIniFile
// If File doesn't exist, instantiate array as empty
try {
$ini_arr = parse_ini_file($this->dbIniFile);
}
catch (Exception $e){
$ini_arr = array();
}
// If required keys don't exist, instantiate them to default empty string
foreach ($this->configKeys as $key){
if (!isset($ini_arr[$key])){
$ini_arr[$key] = '';
}
}
return $ini_arr;
}
function writeDbConfig($dat){
// Get values to write
// If value is not in input, read from current DB config
$currDBConfig = $this->readDbConfig();
foreach ($dat as $key => $value){
$currDBConfig[$key] = $value;
}
// Build ini file string
$data = '';
foreach ($currDBConfig as $key => $value){
$data = $data . $key . '=' . $value . PHP_EOL;
}
// Write ini file string to actual file
if ($fp = fopen($this->dbIniFile, 'w')){
$startTime = microtime(True);
do {
$writeLock = flock($fp, LOCK_EX);
if (!$writeLock){
usleep(round(34760));
}
} while ((!$writeLock) and ((microtime(True) - $startTime) < 5));
if ($writeLock){
fwrite($fp, $data);
flock($fp, LOCK_UN);
}
fclose($fp);
}
}
function get($key){
return $this->settings[$key];
}
function getSanitizedConfig(){
$out = array();
$out['server'] = $this->settings['server'];
$out['db'] = $this->settings['db'];
$out['username'] = $this->settings['username'];
$out['teamnumber'] = $this->settings['teamnumber'];
$out['eventcode'] = $this->settings['eventcode'];
$out['tbakey'] = substr($this->settings['tbakey'], 0, 3) . '******';
$out['datatable'] = $this->settings['datatable'];
$out['tbatable'] = $this->settings['tbatable'];
$out['pitScouttable'] = $this->settings['pitScouttable'];
$out['strikeScouttable'] = $this->settings['strikeScouttable'];
$out['LSTable'] = $this->settings['LSTable'];
return $out;
}
}
?>