DDNS::startLogger()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
3
namespace Affenrakete;
4
5
use INWX\Domrobot;
6
use Katzgrau\KLogger\Logger;
7
use Psr\Log\LogLevel;
8
9
/**
10
 * INWX DDNS Manager
11
 *
12
 * @author Peter Siemer <[email protected]>
13
 * @license https://opensource.org/licenses/GPL-3.0 GNU Public License
14
 * @link https://affenrakete.de
15
 *
16
 */
17
class DDNS {
18
19
    protected $logFilePath = "./logs/";
20
    protected $iniFilePath = "./conf/";
21
    protected $iniFileInwx = "inwx.ini";
22
    protected $iniFileDomain = "";
23
    protected $inwx = [];       // apiurl, username, password
24
    protected $domain = [];     // inwx => domain, subdomain; ddns => apikey
25
    protected $IP = [];        // oldip, newip, id
26
    protected $domrobot;
27
    protected $logger;
28
    protected $returnStatus = "";
29
30
    public function __construct($apidomain = null, $apikey = null) {
31
        self::setIniFile($apidomain);
32
        self::startLogger();
33
        self::readIni();
34
        self::checkAccess($apikey);
35
    }
36
37
    protected function precheck($str = null) {
38
        return htmlspecialchars(trim($str), ENT_QUOTES, 'UTF-8');
39
    }
40
41
    protected function setIniFile($apidomain = null) {
42
        $domainReturn = [];
43
44
        preg_match("/(?!.{253})((?!-)[A-Za-z0-9-]{1,63}(?<!-)\.){1,126}+[A-Za-z]{2,6}/", $apidomain, $domainReturn);
45
        $domainReturn[0] = str_replace('.', '-', $domainReturn[0]);
46
47
        $this->iniFileDomain = $domainReturn[0];
48
49
        return;
50
    }
51
52
    protected function startLogger() {
53
        $logLevel = LogLevel::INFO;
54
        if (DEBUG) {
55
            $logLevel = LogLevel::DEBUG;
56
        }
57
58
        $this->logger = new Logger($this->logFilePath . $this->iniFileDomain, $logLevel);
59
    }
60
61
    protected function readIni() {
62
        // check if ini files exists
63
        if (!file_exists($this->iniFilePath . $this->iniFileDomain . ".ini")) {
64
            $this->logger->error('File does not exists | iniFileDomain :' . $this->iniFileDomain . '.ini');
65
            return false;
66
        }
67
        if (!file_exists($this->iniFilePath . $this->iniFileInwx)) {
68
            $this->logger->error('File does not exists | iniFileDomain :' . $this->iniFileInwx);
69
            return false;
70
        }
71
72
        // read domain.ini
73
        $ini = parse_ini_file($this->iniFilePath . $this->iniFileDomain . ".ini", TRUE);
74
75
        $this->domain['inwx']['domain'] = self::precheck($ini['inwx']['domain']);
76
        $this->domain['inwx']['subdomain'] = self::precheck($ini['inwx']['subdomain']);
77
        $this->domain['ddns']['apikey'] = self::precheck($ini['ddns']['apikey']);
78
79
        //read inwx.ini
80
        $ini = parse_ini_file($this->iniFilePath . $this->iniFileInwx, TRUE);
81
82
        $this->inwx['apiurl'] = self::precheck($ini['apiurl']);
83
        $this->inwx['username'] = self::precheck($ini['username']);
84
        $this->inwx['password'] = self::precheck($ini['password']);
85
86
        return;
87
    }
88
89
    protected function checkAccess($apikey = null) {
90
        if ($apikey == null || $this->domain['ddns']['apikey'] !== $apikey) {
91
            $this->logger->error('unauthorisized access');
92
            return false;
93
        }
94
95
        return true;
96
    }
97
98
    public function inwxLogin() {
99
        // INWX Setup class
100
        $this->domrobot = new Domrobot($this->inwx['apiurl']);
101
        $this->domrobot->setDebug(false);
102
        $this->domrobot->setLanguage('en');
103
104
        // INWX Login
105
        $result = $this->domrobot->login($this->inwx['username'], $this->inwx['password']);
106
107
        $this->logger->debug('Result', $result);
108
109
        // check result
110
        if ($result['code'] != 1000) {
111
            $this->returnStatus = 'badauth';
112
            $this->logger->error('inwx login not successfull');
113
            return false;
114
        }
115
        $this->logger->debug('inwx login successfull');
116
117
        return true;
118
    }
119
120
    public function inwxLogout() {
121
        $result = $this->domrobot->logout();
122
123
        $this->logger->debug('Result', $result);
124
125
        // check result
126
        if ($result['code'] != 1500) {
127
            $this->logger->error('inwx logout NOT successfull');
128
            return false;
129
        }
130
        $this->logger->debug('inwx logout successfull');
131
132
        return true;
133
    }
134
135
    public function inwxGetNameserverInfo() {
136
        $object = "nameserver";
137
        $methode = "info";
138
139
        $params = array();
140
        $params['domain'] = $this->domain['inwx']['domain'];
141
        $params['name'] = $this->domain['inwx']['subdomain'];
142
143
        $result = $this->domrobot->call($object, $methode, $params);
144
145
146
        $this->logger->debug('Result', $result);
147
148
        // check result
149
        if ($result['code'] != 1000) {
150
            $this->logger->error('get nameserver info NOT successfull');
151
            return false;
152
        }
153
154
        foreach ($result["resData"]["record"] as $value) {
155
            $this->IP[$value['type']]['id'] = $value['id'];
156
            $this->IP[$value['type']]['oldip'] = $value['content'];
157
        }
158
159
        $this->logger->debug('get nameserver info successfull');
160
161
        return true;
162
    }
163
164
    public function inwxSetNameserverInfo($ip = null, $type = null) {
165
        $object = "nameserver";
166
        $methode = "updateRecord";
167
168
        $params = array();
169
170
        if (isset($this->IP[$type]['id'])) {
171
            $params['id'] = $this->IP[$type]['id'];
172
            $params['content'] = $this->IP[$type]['newip'] = $ip;
173
            $oldip = $this->IP[$type]['oldip'];
174
        } else {
175
            $this->logger->warning('set nameserver info type: ' . $type . '. Old record not set or invalid IP.');
176
            return false;
177
        }
178
179
        $result = $this->domrobot->call($object, $methode, $params);
180
181
        $this->logger->debug('Result', $result);
182
183
        // check result
184
        if ($result['code'] != 1000) {
185
            $this->logger->error('set nameserver info NOT successfull');
186
            return false;
187
        }
188
189
        $this->returnStatus = 'good';
190
191
        $this->logger->debug('set nameserver info successfull');
192
        $this->logger->info('IP Update successfull | old ip: ' . $oldip . ' | new ip: ' . $params['content']);
193
194
        return true;
195
    }
196
197
    public function printStatus() {
198
        print_r($this->returnStatus);
199
        return true;
200
    }
201
202
}
203