Completed
Push — master ( 1af4f5...caaac4 )
by Andreas
03:43
created

config::list_files()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
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 $dbtype = 'MySQL';
12
    public $database = 'midgard';
13
    public $port = 0;
14
    public $dbuser = '';
15
    public $dbpass = '';
16
    public $dbdir = '';
17
    public $host = 'localhost';
18
    public $logfilename = '';
19
    public $loglevel = 'warn';
20
    public $tablecreate = false;
21
    public $tableupdate = false;
22
    public $testunit = false;
23
    public $midgardusername = 'admin';
24
    public $midgardpassword = 'password';
25
    public $authtype = '';
26
    public $pamfile = '';
27
    public $blobdir = '/var/lib/midgard2/blobs';
28
    public $sharedir = '/usr/share/midgard2';
29
    public $vardir = '/var/lib/midgard2';
30
    public $cachedir = '/var/cache/midgard2';
31
    public $gdathreads = false;
32
33 3
    public function read_file_at_path($path)
34
    {
35 3
        if (   !file_exists($path)
36 3
            || !is_readable($path)) {
37 1
            return false;
38
        }
39 2
        $parsed = parse_ini_file($path);
40
41 2
        $this->apply_config($parsed);
42
43 2
        return true;
44
    }
45
46
    // TODO: find out if this could be moved to read_data()
47 2
    private function apply_config(array $config)
48
    {
49
        $mapping = array(
50 2
            'type' => 'dbtype',
51 2
            'name' => 'database',
52 2
            'username' => 'dbuser',
53 2
            'password' => 'dbpass',
54 2
            'databasedir' => 'dbdir',
55 2
            'logfilename' => 'logfile',
56 2
        );
57
58 2
        foreach ($config as $key => $value) {
59 2
            $key = strtolower($key);
60 2
            if (array_key_exists($key, $mapping)) {
61 2
                $key = $mapping[$key];
62 2
            }
63 2
            if (property_exists($this, $key)) {
64 2
                if (is_bool($this->$key)) {
65 2
                    $value = (boolean) $value;
66 2
                }
67 2
                $this->$key = $value;
68 2
            }
69 2
        }
70 2
    }
71
72 1
    public function read_file($name, $user = true) // <== TODO: check
73
    {
74 1
        if (!$user) {
75 1
            $prefix = '/etc/midgard2/conf.d';
76 1
        } else {
77
            $prefix = getenv('HOME') . '/.midgard2/conf.d';
78
        }
79 1
        return $this->read_file_at_path($prefix . '/' . $name);
80
    }
81
82 1
    public function save_file($name, $user = true) // <== TODO: check
83
    {
84 1
        if (!$user) {
85
            $prefix = '/etc/midgard2/conf.d';
86
        } else {
87 1
            $prefix = getenv('HOME') . '/.midgard2/conf.d';
88
        }
89 1
        if (!file_exists($prefix)) {
90 1
            mkdir($prefix, 0777, true);
91 1
        }
92 1
        $filename = $prefix . '/' . $name;
93 1
        $contents = "[MidgardDir]\n\n";
94 1
        $contents .= $this->convert_to_storage('ShareDir', $this->sharedir);
95 1
        $contents .= $this->convert_to_storage('VarDir', $this->vardir);
96 1
        $contents .= $this->convert_to_storage('BlobDir', $this->blobdir);
97 1
        $contents .= $this->convert_to_storage('CacheDir', $this->cachedir);
98 1
        $contents .= "[Midgarddatabase]\n\n";
99 1
        $contents .= $this->convert_to_storage('Type', $this->dbtype);
100 1
        $contents .= $this->convert_to_storage('Host', $this->host);
101 1
        $contents .= $this->convert_to_storage('Port', $this->port);
102 1
        $contents .= $this->convert_to_storage('Name', $this->database);
103 1
        $contents .= $this->convert_to_storage('Username', $this->dbuser);
104 1
        $contents .= $this->convert_to_storage('Password', $this->dbpass);
105 1
        $contents .= $this->convert_to_storage('DatabaseDir', $this->dbdir);
106 1
        $contents .= "DefaultLanguage = pl\n\n";
107 1
        $contents .= $this->convert_to_storage('Logfile', $this->logfilename);
108 1
        $contents .= $this->convert_to_storage('Loglevel', $this->loglevel);
109 1
        $contents .= $this->convert_to_storage('TableCreate', $this->tablecreate);
110 1
        $contents .= $this->convert_to_storage('TableUpdate', $this->tableupdate);
111 1
        $contents .= $this->convert_to_storage('TestUnit', $this->testunit);
112 1
        $contents .= $this->convert_to_storage('MidgardUsername', $this->midgardusername);
113 1
        $contents .= $this->convert_to_storage('MidgardPassword', $this->midgardpassword);
114 1
        $contents .= $this->convert_to_storage('AuthType', $this->authtype);
115 1
        $contents .= $this->convert_to_storage('PamFile', $this->pamfile);
116 1
        $contents .= $this->convert_to_storage('GdaThreads', $this->gdathreads);
117
118 1
        $stat = file_put_contents($filename, $contents);
119 1
        if ($stat === false) {
120
            return false;
121
        }
122 1
        return true;
123
    }
124
125 1
    private function convert_to_storage($key, $value)
126
    {
127 1
        if (is_bool($value)) {
128 1
            $value = ($value) ? 'true' : 'false';
129 1
        } elseif ($value === '') {
130 1
            $value = '""';
131 1
        }
132 1
        return $key . ' = ' . $value . "\n\n";
133
    }
134
135
    public function read_data($data)
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
136
    {
137
    }
138
139
    public static function list_files($user = true) // <== TODO: check
0 ignored issues
show
Unused Code introduced by
The parameter $user is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
140
    {
141
    }
142
143 1
    public function create_blobdir()
144
    {
145 1
        $subdirs = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F');
146 1
        foreach ($subdirs as $dir) {
147 1
            foreach ($subdirs as $subdir) {
148 1
                if (   !is_dir($this->blobdir . '/' . $dir . '/' . $subdir)
149 1
                    && !mkdir($this->blobdir . '/' . $dir . '/' . $subdir, 0777, true)) {
150
                    return false;
151
                }
152 1
            }
153 1
        }
154
155 1
        return true;
156
    }
157
}
158