Completed
Pull Request — master (#6)
by Peter
07:16
created

update.php (21 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * INWX DDNS Manager
4
 *
5
 * @author Peter Siemer <[email protected]>
6
 * @license https://opensource.org/licenses/GPL-3.0 GNU Public License
7
 * @link https://affenrakete.de
8
 *
9
 */
10
header('Content-type: text/plain; charset=utf-8'); // Set UFT-8 Header
11
12
define("DEBUG", false);
13
define("OUTPUT", true);
14
15
//error_reporting(E_ALL);
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
16
//ini_set("display_errors", 1);
17
18
require_once('./libraries/domrobot.php');
19
20
function precheck($str, $checkEmpty = false)
21
{
22
    $str = htmlspecialchars(trim($str), ENT_QUOTES, 'UTF-8');
23
24
    if($checkEmpty && empty($str))
25
        exit('something went wrong 7');
0 ignored issues
show
Coding Style Compatibility introduced by
The function precheck() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
26
27
    return $str;
28
}
29
30
class DDNS
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
31
{
32
    protected $iniFilePath = "./conf/";
33
    protected $iniFileInwx = "inwx.ini";
34
    protected $iniFileDomain = "";
35
36
    protected $inwx = [];            // {apiurl, username, password}
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
37
    protected $domain = [];          // [inwx] => {domain, subdomain}, [ddns] => {apikey}
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
38
    protected $IP4 = [];          // {oldip, newip, id}
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
39
    protected $IP6 = [];          // {oldip, newip, id}
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
40
    
41
    protected $domrobot;
42
43
    public function __construct($apidomain = null, $apikey = null)
44
    {
45
        $this->setIniFileDomain($apidomain);
46
        $this->readIni();
47
        $this->checkAccess($apikey);
48
    }
49
50
    protected function setIniFileDomain($apidomain = null)
51
    {
52
        preg_match("/(?!.{253})((?!-)[A-Za-z0-9-]{1,63}(?<!-)\.){1,126}+[A-Za-z]{2,6}/", $apidomain, $domainReturn);
53
		$domainReturn[0] = str_replace('.', '-', $domainReturn[0]);
54
		
55
        $this->iniFileDomain = $domainReturn[0] . '.ini';
56
57
        return;        
58
    }
59
60
    protected function readIni()
61
    {
62
        // check if ini files exists
63
        if(!file_exists($this->iniFilePath . $this->iniFileDomain))
64
            exit('something went wrong 8');
0 ignored issues
show
Coding Style Compatibility introduced by
The method readIni() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
65
        if(!file_exists($this->iniFilePath . $this->iniFileInwx))
66
            exit('something went wrong 1');
0 ignored issues
show
Coding Style Compatibility introduced by
The method readIni() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
67
68
        // read domain.ini
69
        $ini = parse_ini_file($this->iniFilePath . $this->iniFileDomain, TRUE);
70
        
71
            $this->domain['inwx']['domain'] = precheck($ini['inwx']['domain'], true);
72
            $this->domain['inwx']['subdomain'] = precheck($ini['inwx']['subdomain'], true);
73
            $this->domain['ddns']['apikey'] = precheck($ini['ddns']['apikey'], true);
74
            $this->domain['log']['filepath'] = precheck($ini['log']['filepath'], true);
75
76
        //read inwx.ini
77
        $ini = parse_ini_file($this->iniFilePath . $this->iniFileInwx, TRUE);
78
79
            $this->inwx['apiurl'] =  precheck($ini['apiurl'], true);
80
            $this->inwx['username'] =  precheck($ini['username'], true);
81
            $this->inwx['password'] =  precheck($ini['password'], true);
82
83
        return;
84
    }
85
86
    protected function checkAccess($apikey = null)
87
    {
88
        if($apikey == null || $this->domain['ddns']['apikey'] !== $apikey)
89
            exit('something went wrong 3');
0 ignored issues
show
Coding Style Compatibility introduced by
The method checkAccess() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
90
91
        return;
92
    }
93
    
94
    protected function inwxLogin()
95
    {
96
        // INWX Setup class
97
        $this->domrobot = new INWX\domrobot($this->inwx['apiurl']);
98
        $this->domrobot->setDebug(false);
99
        $this->domrobot->setLanguage('en');
100
        		
101
        // INWX Login
102
        $result = $this->domrobot->login($this->inwx['username'], $this->inwx['password']);
103
104
        if(DEBUG)
105
            print_r($result);        
106
        
107
        // check result
108
        if ($result['code'] != 1000)
109
        {
110
            if(OUTPUT)
111
                exit('badauth');
0 ignored issues
show
Coding Style Compatibility introduced by
The method inwxLogin() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
112
            
113
            exit('something went wrong 4');
0 ignored issues
show
Coding Style Compatibility introduced by
The method inwxLogin() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
114
        }
115
        
116
        return;
117
    }
118
    
119
    protected function inwxLogout()
120
    {
121
        $result = $this->domrobot->logout();
122
123
        if(DEBUG)
124
            print_r($result);
125
        
126
        // check result
127
        if ($result['code'] != 1500)
128
        {
129
            exit('something went wrong 5');
0 ignored issues
show
Coding Style Compatibility introduced by
The method inwxLogout() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
130
        }
131
        
132
        return;
133
    }
134
    
135
    protected function inwxGetNameserverInfo()
136
    {
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
        if(DEBUG)
147
            print_r($result);
148
        
149
        // check result        
150
        if ($result['code'] != 1000)
151
        {                        
152
            exit('something went wrong 6');
0 ignored issues
show
Coding Style Compatibility introduced by
The method inwxGetNameserverInfo() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
153
        }
154
        
155
		foreach($result["resData"]["record"] as $value)
156
		{
157 View Code Duplication
			if($value['type'] == "A")
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
158
			{
159
				$this->IP4['id'] = $value['id'];
160
				$this->IP4['oldip'] = $value['content'];
161
			}
162 View Code Duplication
			if($value['type'] == "AAAA")
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
163
			{
164
				$this->IP6['id'] = $value['id'];
165
				$this->IP6['oldip'] = $value['content'];
166
			}
167
		}
168
		
169
        return;
170
    }
171
	
172
	protected function inwxSetNameserverInfo($type = null)
173
    {
174
        $object = "nameserver";
175
        $methode = "updateRecord";
176
            
177
        $params = array();
178
		
179
		if($type == 'ipv4')
180
		{
181
			$params['id'] = $this->IP4['id'];
182
			$params['content'] = $this->IP4['newip'];
183
		}
184
		elseif($type == 'ipv6')
185
		{
186
			$params['id'] = $this->IP6['id'];
187
			$params['content'] = $this->IP6['newip'];
188
		}
189
		else
190
			exit('something went wrong 9');	
0 ignored issues
show
Coding Style Compatibility introduced by
The method inwxSetNameserverInfo() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
191
            
192
        $result = $this->domrobot->call($object, $methode, $params);
193
            
194
        if(DEBUG)
195
            print_r($result);
196
        
197
        // check result        
198
        if ($result['code'] != 1000)
199
        {                        
200
            exit('something went wrong 10');
0 ignored issues
show
Coding Style Compatibility introduced by
The method inwxSetNameserverInfo() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
201
        }
202
		
203
	if(OUTPUT)
204
	    echo('good');
205
        		
206
        return;
207
    }
208
    
209
    public function updateIP($ipv4 = null, $ipv6 = null)
210
    {
211
        $this->inwxLogin();
212
        
213
        $this->inwxGetNameserverInfo();
214
		
215 View Code Duplication
		if(filter_var($ipv4, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4))
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
216
		{
217
			$this->IP4['newip'] = $ipv4;
218
			$this->inwxSetNameserverInfo('ipv4');
219
		}
220
            
221 View Code Duplication
		if(filter_var($ipv6, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6))
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
222
		{
223
			$this->IP6['newip'] = $ipv6;
224
			$this->inwxSetNameserverInfo('ipv6');
225
		}
226
		
227
        $this->inwxLogout();
228
        
229
        return;
230
    }
231
}
232
233
$input['domain'] = (isset($_GET['domain'])) ? precheck($_GET['domain'], true) : null;
234
$input['password'] = (isset($_GET['password'])) ? precheck($_GET['password'], true) : null;
235
$input['ipv4'] = (isset($_GET['ipv4'])) ? precheck($_GET['ipv4'], false) : null;
236
$input['ipv6'] = (isset($_GET['ipv6'])) ? precheck($_GET['ipv6'], false) : null;
237
238
$ddns = new DDNS($input['domain'], $input['password']);
239
$ddns->updateIP($input['ipv4'], $input['ipv6']);
240
241
?>
0 ignored issues
show
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
242