1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author CONTENT CONTROL http://www.contentcontrol-berlin.de/ |
4
|
|
|
* @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/ |
5
|
|
|
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License |
6
|
|
|
*/ |
7
|
|
|
namespace midgard\portable\api; |
8
|
|
|
|
9
|
|
|
class config |
10
|
|
|
{ |
11
|
|
|
public string $dbtype = 'MySQL'; |
12
|
|
|
public string $database = 'midgard'; |
13
|
|
|
public $port = 0; |
14
|
|
|
public string $dbuser = ''; |
15
|
|
|
public string $dbpass = ''; |
16
|
|
|
public string $dbdir = ''; |
17
|
|
|
public string $host = 'localhost'; |
18
|
|
|
public string $logfilename = ''; |
19
|
|
|
public string $loglevel = 'warn'; |
20
|
|
|
public string $midgardusername = 'admin'; |
21
|
|
|
public string $midgardpassword = 'password'; |
22
|
|
|
public string $authtype = ''; |
23
|
|
|
public string $pamfile = ''; |
24
|
|
|
public string $blobdir = '/var/lib/midgard2/blobs'; |
25
|
|
|
public string $sharedir = '/usr/share/midgard2'; |
26
|
|
|
public string $vardir = '/var/lib/midgard2'; |
27
|
|
|
public string $cachedir = '/var/cache/midgard2'; |
28
|
|
|
public bool $tablecreate = false; |
29
|
|
|
|
30
|
3 |
|
public function read_file_at_path(string $path) : bool |
31
|
|
|
{ |
32
|
3 |
|
if (!is_readable($path)) { |
33
|
1 |
|
return false; |
34
|
|
|
} |
35
|
2 |
|
$parsed = parse_ini_file($path); |
36
|
|
|
|
37
|
2 |
|
$this->apply_config($parsed); |
38
|
|
|
|
39
|
2 |
|
return true; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
// TODO: find out if this could be moved to read_data() |
43
|
2 |
|
private function apply_config(array $config) |
44
|
|
|
{ |
45
|
2 |
|
$mapping = [ |
46
|
2 |
|
'type' => 'dbtype', |
47
|
2 |
|
'name' => 'database', |
48
|
2 |
|
'username' => 'dbuser', |
49
|
2 |
|
'password' => 'dbpass', |
50
|
2 |
|
'databasedir' => 'dbdir', |
51
|
2 |
|
'logfilename' => 'logfile', |
52
|
2 |
|
]; |
53
|
|
|
|
54
|
2 |
|
foreach ($config as $key => $value) { |
55
|
2 |
|
$key = strtolower($key); |
56
|
2 |
|
if (isset($mapping[$key])) { |
57
|
2 |
|
$key = $mapping[$key]; |
58
|
|
|
} |
59
|
2 |
|
if (property_exists($this, $key)) { |
60
|
2 |
|
if (is_bool($this->$key)) { |
61
|
|
|
$value = (boolean) $value; |
62
|
|
|
} |
63
|
2 |
|
$this->$key = $value; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
2 |
|
private function get_prefix(bool $user) : string |
69
|
|
|
{ |
70
|
2 |
|
if ($user) { |
71
|
1 |
|
return getenv('HOME') . '/.midgard2/conf.d'; |
72
|
|
|
} |
73
|
1 |
|
return '/etc/midgard2/conf.d'; |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
public function read_file(string $name, bool $user = true) : bool // <== TODO: check |
77
|
|
|
{ |
78
|
1 |
|
return $this->read_file_at_path($this->get_prefix($user) . '/' . $name); |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
public function save_file(string $name, bool $user = true) : bool // <== TODO: check |
82
|
|
|
{ |
83
|
1 |
|
$prefix = $this->get_prefix($user); |
84
|
1 |
|
if (!file_exists($prefix)) { |
85
|
1 |
|
mkdir($prefix, 0777, true); |
86
|
|
|
} |
87
|
1 |
|
$filename = $prefix . '/' . $name; |
88
|
1 |
|
$contents = "[MidgardDir]\n\n"; |
89
|
1 |
|
$contents .= $this->convert_to_storage('ShareDir', $this->sharedir); |
90
|
1 |
|
$contents .= $this->convert_to_storage('VarDir', $this->vardir); |
91
|
1 |
|
$contents .= $this->convert_to_storage('BlobDir', $this->blobdir); |
92
|
1 |
|
$contents .= $this->convert_to_storage('CacheDir', $this->cachedir); |
93
|
1 |
|
$contents .= "[Midgarddatabase]\n\n"; |
94
|
1 |
|
$contents .= $this->convert_to_storage('Type', $this->dbtype); |
95
|
1 |
|
$contents .= $this->convert_to_storage('Host', $this->host); |
96
|
1 |
|
$contents .= $this->convert_to_storage('Port', $this->port); |
97
|
1 |
|
$contents .= $this->convert_to_storage('Name', $this->database); |
98
|
1 |
|
$contents .= $this->convert_to_storage('Username', $this->dbuser); |
99
|
1 |
|
$contents .= $this->convert_to_storage('Password', $this->dbpass); |
100
|
1 |
|
$contents .= $this->convert_to_storage('DatabaseDir', $this->dbdir); |
101
|
1 |
|
$contents .= "DefaultLanguage = pl\n\n"; |
102
|
1 |
|
$contents .= $this->convert_to_storage('Logfile', $this->logfilename); |
103
|
1 |
|
$contents .= $this->convert_to_storage('Loglevel', $this->loglevel); |
104
|
1 |
|
$contents .= $this->convert_to_storage('MidgardUsername', $this->midgardusername); |
105
|
1 |
|
$contents .= $this->convert_to_storage('MidgardPassword', $this->midgardpassword); |
106
|
1 |
|
$contents .= $this->convert_to_storage('AuthType', $this->authtype); |
107
|
1 |
|
$contents .= $this->convert_to_storage('PamFile', $this->pamfile); |
108
|
|
|
|
109
|
1 |
|
return file_put_contents($filename, $contents) !== false; |
110
|
|
|
} |
111
|
|
|
|
112
|
1 |
|
private function convert_to_storage(string $key, $value) : string |
113
|
|
|
{ |
114
|
1 |
|
if (is_bool($value)) { |
115
|
|
|
$value = $value ? 'true' : 'false'; |
116
|
1 |
|
} elseif ($value === '') { |
117
|
1 |
|
$value = '""'; |
118
|
|
|
} |
119
|
1 |
|
return $key . ' = ' . $value . "\n\n"; |
120
|
|
|
} |
121
|
|
|
|
122
|
2 |
|
public function create_blobdir() : bool |
123
|
|
|
{ |
124
|
2 |
|
$subdirs = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F']; |
125
|
2 |
|
foreach ($subdirs as $dir) { |
126
|
2 |
|
foreach ($subdirs as $subdir) { |
127
|
2 |
|
if ( !is_dir($this->blobdir . '/' . $dir . '/' . $subdir) |
128
|
2 |
|
&& !mkdir($this->blobdir . '/' . $dir . '/' . $subdir, 0777, true)) { |
129
|
|
|
return false; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
2 |
|
return true; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|