forked from elbereth/dashninja-ctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDashConfig.class.php
151 lines (121 loc) · 3.69 KB
/
DashConfig.class.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
140
141
142
143
144
145
146
147
148
149
150
151
<?php
/*
This file is part of Dash Ninja.
https://github.com/elbereth/dashninja-ctl
Dash Ninja is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Dash Ninja is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Dash Ninja. If not, see <http://www.gnu.org/licenses/>.
*/
// Deal with dash.conf configuration (read/write)
class DashConfig {
// Normal dash.conf configuration
private $config;
// Masternode Control configuration
private $mnctlcfg;
// Masternode Control Magic Keyword
const MAGIC = '#mnctlcfg#';
// Path to config file
private $configfilename;
private $configloaded = false;
// Load the config file
private function loadconfig() {
$this->config = array();
$this->mnctlcfg = array();
$this->configloaded = false;
if (file_exists($this->configfilename)) {
$rawconf = file_get_contents($this->configfilename);
$conf = explode("\n",trim($rawconf));
$magiclen = strlen(DashConfig::MAGIC);
for ($x = 0; $x < count($conf); $x++) {
if ((substr($conf[$x],0,1) == '#') && (substr($conf[$x],0,$magiclen) != DashConfig::MAGIC)) {
$lineval = array(0 => $conf[$x]);
}
else {
$lineval = explode('=',$conf[$x]);
}
if (substr($lineval[0],0,$magiclen) == DashConfig::MAGIC) {
$this->mnctlcfg[substr($lineval[0],$magiclen)] = $lineval[1];
}
else {
if (isset($lineval[1])) {
$this->config[$lineval[0]] = $lineval[1];
}
else {
$this->config[$lineval[0]] = false;
}
}
}
$this->configloaded = true;
}
}
function __construct($uname) {
if (file_exists('/home/'.$uname.'/.dashcore/dash.conf')) {
$this->configfilename = '/home/'.$uname.'/.dashcore/dash.conf';
}
elseif (file_exists('/home/'.$uname.'/.dash/dash.conf')) {
$this->configfilename = '/home/'.$uname.'/.dash/dash.conf';
}
else {
$this->configfilename = '/home/'.$uname.'/.darkcoin/darkcoin.conf';
}
$this->loadconfig();
}
function getconfig($key) {
$res = false;
if (array_key_exists($key,$this->config)) {
$res = $this->config[$key];
}
return $res;
}
function setconfig($key,$value) {
$this->config[$key] = $value;
}
function getmnctlconfig($key) {
$res = false;
if (array_key_exists($key,$this->mnctlcfg)) {
$res = $this->mnctlcfg[$key];
}
return $res;
}
function setmnctlconfig($key,$value) {
$this->mnctlcfg[$key] = $value;
}
// Save the config file
function saveconfig() {
if ($this->configloaded) {
$rawconf = '';
foreach ($this->config as $key => $value) {
if ($value === false) {
$rawconf .= $key."\n";
}
else {
$rawconf .= $key.'='.$value."\n";
}
}
foreach ($this->mnctlcfg as $key => $value) {
if ($value === false) {
$rawconf .= DashConfig::MAGIC.$key."\n";
}
else {
$rawconf .= DashConfig::MAGIC.$key.'='.$value."\n";
}
}
$res = file_put_contents($this->configfilename,$rawconf);
}
else {
$res = false;
}
return $res;
}
function isConfigLoaded() {
return $this->configloaded;
}
}
?>