Completed
Pull Request — develop (#8)
by Peter
01:59
created

DDNS::printStatus()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
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
    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
        foreach ($result["resData"]["record"] as $value) {
156
            if ($value['type'] == "A") {
157
                $this->IP4['id'] = $value['id'];
158
                $this->IP4['oldip'] = $value['content'];
159
            }
160
            if ($value['type'] == "AAAA") {
161
                $this->IP6['id'] = $value['id'];
162
                $this->IP6['oldip'] = $value['content'];
163
            }
164
        }
165
166
        $this->logger->debug('get nameserver info successfull');
167
168
        return true;
169
    }
170
171
    protected function inwxSetNameserverInfo($ip = null, $type = null) {
172
        $object = "nameserver";
173
        $methode = "updateRecord";
174
175
        $params = array();
176
177
        if ($type == 'ipv4' && filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
178
            $params['id'] = $this->IP4['id'];
179
            $params['content'] = $this->IP4['newip'] = $ip;
180
            $oldip = $this->IP4['oldip'];
181
        } elseif ($type == 'ipv6' && filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
182
            $params['id'] = $this->IP6['id'];
183
            $params['content'] = $this->IP6['newip'] = $ip;
184
            $oldip = $this->IP6['oldip'];
185
        } else {
186
            $this->logger->warning('set nameserver info type: ' . $type);
187
            return false;
188
        }
189
190
        $result = $this->domrobot->call($object, $methode, $params);
191
192
        $this->logger->debug('Result', $result);
193
194
        // check result
195
        if ($result['code'] != 1000) {
196
            $this->logger->error('set nameserver info NOT successfull');
197
            return false;
198
        }
199
200
        $this->returnStatus = 'good';
201
        
202
        $this->logger->debug('set nameserver info successfull');
203
        $this->logger->info('IP Update successfull | old ip: ' . $oldip . ' | new ip: ' . $params['content']);
204
        
205
        return true;
206
    }
207
208
    public function printStatus() {
209
210
        print_r($this->returnStatus);
211
212
        return true;
213
    }
214
215
}
216