Passed
Pull Request — master (#12)
by Peter
02:34
created

DDNS::inwxSetNameserverInfo()   C

Complexity

Conditions 8
Paths 5

Size

Total Lines 36
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

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