Completed
Push — develop ( 04f410 )
by Peter
08:03
created

DDNS::startLogger()   A

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 $IP4 = [];        // {oldip, newip, id}
26
    protected $IP6 = [];        // {oldip, newip, id}
27
    protected $domrobot;
28
    protected $logger;
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
    protected 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
112
            if (OUTPUT) {
113
                echo('badauth');
114
            }
115
116
            $this->logger->error('inwx login not successfull');
117
            return false;
118
        }
119
        $this->logger->debug('inwx login successfull');
120
121
        return true;
122
    }
123
124
    public function inwxLogout() {
125
        $result = $this->domrobot->logout();
126
127
        $this->logger->debug('Result', $result);
128
129
        // check result
130
        if ($result['code'] != 1500) {
131
            $this->logger->error('inwx logout NOT successfull');
132
            return false;
133
        }
134
        $this->logger->debug('inwx logout successfull');
135
136
        return true;
137
    }
138
139
    protected function inwxGetNameserverInfo() {
140
        $object = "nameserver";
141
        $methode = "info";
142
143
        $params = array();
144
        $params['domain'] = $this->domain['inwx']['domain'];
145
        $params['name'] = $this->domain['inwx']['subdomain'];
146
147
        $result = $this->domrobot->call($object, $methode, $params);
148
149
150
        $this->logger->debug('Result', $result);
151
152
        // check result
153
        if ($result['code'] != 1000) {
154
            $this->logger->error('get nameserver info NOT successfull');
155
            return false;
156
        }
157
158
        foreach ($result["resData"]["record"] as $value) {
159
            if ($value['type'] == "A") {
160
                $this->IP4['id'] = $value['id'];
161
                $this->IP4['oldip'] = $value['content'];
162
            }
163
            if ($value['type'] == "AAAA") {
164
                $this->IP6['id'] = $value['id'];
165
                $this->IP6['oldip'] = $value['content'];
166
            }
167
        }
168
169
        $this->logger->debug('get nameserver info successfull');
170
171
        return true;
172
    }
173
174
    protected function inwxSetNameserverInfo($type = null) {
175
        $object = "nameserver";
176
        $methode = "updateRecord";
177
178
        $params = array();
179
180
        if ($type == 'ipv4') {
181
            $params['id'] = $this->IP4['id'];
182
            $params['content'] = $this->IP4['newip'];
183
            $oldip = $this->IP4['oldip'];
184
        } elseif ($type == 'ipv6') {
185
            $params['id'] = $this->IP6['id'];
186
            $params['content'] = $this->IP6['newip'];
187
            $oldip = $this->IP6['oldip'];
188
        } else {
189
            $this->logger->warning('set nameserver info type: ' . $type);
190
            return false;
191
        }
192
193
        $result = $this->domrobot->call($object, $methode, $params);
194
195
        $this->logger->debug('Result', $result);
196
197
        // check result
198
        if ($result['code'] != 1000) {
199
            $this->logger->error('set nameserver info NOT successfull');
200
            return false;
201
        }
202
203
        $this->logger->debug('set nameserver info successfull');
204
        $this->logger->info('IP Update successfull | old ip: ' . $oldip . ' | new ip: ' . $params['content']);
205
206
        return true;
207
    }
208
209
    public function updateIP($ipv4 = null, $ipv6 = null) {
210
211
        if (!self::inwxLogin()) {
212
            return false;
213
        }
214
215
        if (!self::inwxGetNameserverInfo()) {
216
            return false;
217
        }
218
219
        if (filter_var($ipv4, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
220
            $this->IP4['newip'] = $ipv4;
221
222
            if (!self::inwxSetNameserverInfo('ipv4')) {
223
                return false;
224
            }
225
        }
226
227
        if (filter_var($ipv6, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
228
            $this->IP6['newip'] = $ipv6;
229
230
            if (!self::inwxSetNameserverInfo('ipv6')) {
231
                return false;
232
            }
233
        }
234
235
        if (OUTPUT) {
236
            echo('good');
237
        }
238
239
        $this->logger->debug('updateIp successfull');
240
241
        return true;
242
    }
243
244
}
245