dyndns.class 57.7 KB
Newer Older
Ad Schellevis's avatar
Ad Schellevis committed
1
<?php
2

Ad Schellevis's avatar
Ad Schellevis committed
3
	/*
4
	 * PHP.updateDNS (OPNsense version)
Ad Schellevis's avatar
Ad Schellevis committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
	 *
	 * +====================================================+
	 *  Services Supported:
	 *    - DynDns (dyndns.org) [dynamic, static, custom]
	 *    - DHSDns (dhs.org)
	 *    - No-IP (no-ip.com)
	 *    - EasyDNS (easydns.com)
	 *    - DHS (www.dhs.org)
	 *    - HN (hn.org) -- incomplete checking!
	 *    - DynS (dyns.org)
	 *    - ZoneEdit (zoneedit.com)
	 *    - FreeDNS (freedns.afraid.org)
	 *    - Loopia (loopia.se)
	 *    - StaticCling (staticcling.org)
	 *    - DNSexit (dnsexit.com)
	 *    - OpenDNS (opendns.com)
	 *    - Namecheap (namecheap.com)
	 *    - HE.net (dns.he.net)
	 *    - HE.net IPv6 (dns.he.net)
	 *    - HE.net Tunnelbroker IP update (ipv4.tunnelbroker.net)
	 *    - SelfHost (selfhost.de)
	 *    - Amazon Route 53 (aws.amazon.com)
	 *    - DNS-O-Matic (dnsomatic.com)
28 29
	 *    - Custom dynamic DNS (any URL)
	 *    - Custom dynamic DNS IPv6 (any URL)
Ad Schellevis's avatar
Ad Schellevis committed
30
	 *    - CloudFlare (www.cloudflare.com)
31 32
	 *    - Eurodns (eurodns.com)
	 *    - GratisDNS (gratisdns.dk)
33 34
	 *    - City Network (citynetwork.se)
	 *    - Duck DNS (duckdns.org)
Ad Schellevis's avatar
Ad Schellevis committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
	 * +----------------------------------------------------+
	 *  Requirements:
	 *    - PHP version 4.0.2 or higher with the CURL Library and the PCRE Library
	 * +----------------------------------------------------+
	 *  Public Functions
	 *    - updatedns()
	 *
	 *  Private Functions
	 *    - _update()
	 *    - _checkStatus()
	 *    - _error()
	 *    - _detectChange()
	 *    - _debug()
	 *    - _checkIP()
	 * +----------------------------------------------------+
	 *  DynDNS Dynamic - Last Tested: 12 July 2005
	 *  DynDNS Static  - Last Tested: NEVER
	 *  DynDNS Custom  - Last Tested: NEVER
	 *  No-IP          - Last Tested: 20 July 2008
	 *  HN.org         - Last Tested: 12 July 2005
	 *  EasyDNS        - Last Tested: 20 July 2008
	 *  DHS            - Last Tested: 12 July 2005
	 *  ZoneEdit       - Last Tested: NEVER
	 *  Dyns           - Last Tested: NEVER
	 *  ODS            - Last Tested: 02 August 2005
	 *  FreeDNS        - Last Tested: 23 Feb 2011
	 *  Loopia         - Last Tested: NEVER
	 *  StaticCling    - Last Tested: 27 April 2006
	 *  DNSexit	   - Last Tested: 20 July 2008
	 *  OpenDNS	   - Last Tested: 4 August 2008
	 *  Namecheap	   - Last Tested: 31 August 2010
	 *  HE.net         - Last Tested: 7 July 2013
	 *  HE.net IPv6    - Last Tested: 7 July 2013
	 *  HE.net Tunnel  - Last Tested: 28 June 2011
	 *  SelfHost       - Last Tested: 26 December 2011
	 *  Amazon Route 53 - Last tested: 01 April 2012
	 *  DNS-O-Matic	   - Last Tested: 9 September 2010
	 *  CloudFlare     - Last Tested: 30 May 2013
73 74
	 *  Eurodns	   - Last Tested: 27 June 2013
	 *  GratisDNS      - Last Tested: 15 August 2012
Ad Schellevis's avatar
Ad Schellevis committed
75
	 *  OVH DynHOST	   - Last Tested: NEVER
76 77
	 *  City Network   - Last Tested: 13 November 2013
	 *  Duck DNS       - Last Tested: 04 March 2015
Ad Schellevis's avatar
Ad Schellevis committed
78 79
	 * +====================================================+
	 *
80 81 82
	 * @author	E.Kristensen
	 * @link	http://www.idylldesigns.com/projects/phpdns/
	 * @version	0.8
Ad Schellevis's avatar
Ad Schellevis committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
	 * @updated	13 October 05 at 21:02:42 GMT
	 *
	 * DNSexit/OpenDNS support and multiwan extension for pfSense by Ermal Luci
	 * Custom DNS support by Matt Corallo
	 *
	 */

	class updatedns {
		var $_cacheFile;
		var $_cacheFile_v6;
		var $_debugFile;
		var $_UserAgent = 'User-Agent: phpDynDNS/0.7';
		var $_errorVerbosity = 0;
		var $_dnsService;
		var $_dnsUser;
		var $_dnsPass;
		var $_dnsHost;
		var $_dnsIP;
		var $_dnsWildcard;
		var $_dnsMX;
		var $_dnsBackMX;
		var $_dnsServer;
		var $_dnsPort;
		var $_dnsUpdateURL;
		var $_dnsZoneID;
		var $_dnsTTL;
		var $status;
		var $_debugID;
		var $_if;
		var $_dnsResultMatch;
		var $_dnsRequestIf;
		var $_dnsRequestIfIP;
		var $_dnsVerboseLog;
		var $_curlIpresolveV4;
		var $_curlSslVerifypeer;
		var $_dnsMaxCacheAgeDays;
		var $_dnsDummyUpdateDone;
		var $_forceUpdateNeeded;
		var $_useIPv6;
122 123

		/*
Ad Schellevis's avatar
Ad Schellevis committed
124
		 * Public Constructor Function (added 12 July 05) [beta]
125
		 *   - Gets the dice rolling for the update.
Ad Schellevis's avatar
Ad Schellevis committed
126
		 *   - $dnsResultMatch should only be used with $dnsService = 'custom'
127
		 *   -  $dnsResultMatch is parsed for '%IP%', which is the IP the provider was updated to,
Ad Schellevis's avatar
Ad Schellevis committed
128 129 130 131 132 133 134 135 136
		 *   -  it is otherwise expected to be exactly identical to what is returned by the Provider.
		 *   - $dnsUser, and $dnsPass indicate HTTP Auth for custom DNS, if they are needed in the URL (GET Variables), include them in $dnsUpdateURL.
		 *   - $For custom requests, $dnsUpdateURL is parsed for '%IP%', which is replaced with the new IP.
		 */
		function updatedns ($dnsService = '', $dnsHost = '', $dnsUser = '', $dnsPass = '',
				    $dnsWildcard = 'OFF', $dnsMX = '', $dnsIf = '', $dnsBackMX = '',
				    $dnsServer = '', $dnsPort = '', $dnsUpdateURL = '', $forceUpdate = false,
				    $dnsZoneID ='', $dnsTTL='', $dnsResultMatch = '', $dnsRequestIf = '',
				    $dnsID = '', $dnsVerboseLog = false, $curlIpresolveV4 = false, $curlSslVerifypeer = true) {
137

Franco Fichtner's avatar
Franco Fichtner committed
138
			global $config;
139

140 141
			$this->_cacheFile = "/conf/dyndns_{$dnsIf}{$dnsService}" . escapeshellarg($dnsHost) . "{$dnsID}.cache";
			$this->_cacheFile_v6 = "/conf/dyndns_{$dnsIf}{$dnsService}" . escapeshellarg($dnsHost) . "{$dnsID}_v6.cache";
142
			$this->_debugFile = "/var/etc/dyndns_{$dnsIf}{$dnsService}" . escapeshellarg($dnsHost) . "{$dnsID}.debug";
Ad Schellevis's avatar
Ad Schellevis committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167

			$this->_curlIpresolveV4 = $curlIpresolveV4;
			$this->_curlSslVerifypeer = $curlSslVerifypeer;
			$this->_dnsVerboseLog = $dnsVerboseLog;
			if ($this->_dnsVerboseLog)
				log_error("DynDns: updatedns() starting");

			$dyndnslck = lock("DDNS".$dnsID, LOCK_EX);

			if (!$dnsService) $this->_error(2);
			switch ($dnsService) {
			case 'freedns':
				if (!$dnsHost) $this->_error(5);
				break;
			case 'namecheap':
				if (!$dnsPass) $this->_error(4);
				if (!$dnsHost) $this->_error(5);
				break;
			case 'route53':
				if (!$dnsZoneID) $this->_error(8);
				if (!$dnsTTL) $this->_error(9);
				break;
			case 'custom':
				if (!$dnsUpdateURL) $this->_error(7);
				break;
168 169 170 171
			case 'duckdns':
				if (!$dnsUser) $this->_error(3);
				if (!$dnsHost) $this->_error(5);
				break;
Ad Schellevis's avatar
Ad Schellevis committed
172 173 174 175
			default:
				if (!$dnsUser) $this->_error(3);
				if (!$dnsPass) $this->_error(4);
				if (!$dnsHost) $this->_error(5);
176
				break;
Ad Schellevis's avatar
Ad Schellevis committed
177
			}
178

Ad Schellevis's avatar
Ad Schellevis committed
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
			switch ($dnsService) {
			case 'he-net-v6':
			case 'custom-v6':
				$this->_useIPv6 = true;
				break;
			default:
				$this->_useIPv6 = false;
			}
			$this->_dnsService = strtolower($dnsService);
			$this->_dnsUser = $dnsUser;
			$this->_dnsPass = $dnsPass;
			$this->_dnsHost = $dnsHost;
			$this->_dnsServer = $dnsServer;
			$this->_dnsPort = $dnsPort;
			$this->_dnsWildcard = $dnsWildcard;
			$this->_dnsMX = $dnsMX;
			$this->_dnsZoneID = $dnsZoneID;
			$this->_dnsTTL = $dnsTTL;
			$this->_if = get_failover_interface($dnsIf);
			$this->_checkIP();
			$this->_dnsUpdateURL = $dnsUpdateURL;
			$this->_dnsResultMatch = $dnsResultMatch;
			$this->_dnsRequestIf = get_failover_interface($dnsRequestIf);
			if ($this->_dnsVerboseLog)
203
				log_error("Dynamic DNS ({$this->_dnsHost}): running get_failover_interface for {$dnsRequestIf}. found {$this->_dnsRequestIf}");
Ad Schellevis's avatar
Ad Schellevis committed
204 205 206 207
			$this->_dnsRequestIfIP = get_interface_ip($dnsRequestIf);
			$this->_dnsMaxCacheAgeDays = 25;
			$this->_dnsDummyUpdateDone = false;
			$this->_forceUpdateNeeded = $forceUpdate;
208

Ad Schellevis's avatar
Ad Schellevis committed
209 210
			// Ensure that we were able to lookup the IP
			if(!is_ipaddr($this->_dnsIP)) {
211
				log_error("Dynamic DNS ({$this->_dnsHost}) There was an error trying to determine the public IP for interface - {$dnsIf}({$this->_if}). Probably interface is not a WAN interface.");
Ad Schellevis's avatar
Ad Schellevis committed
212 213 214 215 216
				unlock($dyndnslck);
				return;
			}

			$this->_debugID = rand(1000000, 9999999);
217

Ad Schellevis's avatar
Ad Schellevis committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
			if ($forceUpdate == false && $this->_detectChange() == false) {
				$this->_error(10);
			} else {
				switch ($this->_dnsService) {
				case 'dnsomatic':
				case 'dyndns':
				case 'dyndns-static':
				case 'dyndns-custom':
				case 'dhs':
				case 'noip':
				case 'noip-free':
				case 'easydns':
				case 'hn':
				case 'zoneedit':
				case 'dyns':
				case 'ods':
				case 'freedns':
				case 'loopia':
				case 'staticcling':
				case 'dnsexit':
				case 'custom':
				case 'custom-v6':
				case 'opendns':
				case 'namecheap':
				case 'he-net':
				case 'he-net-v6':
				case 'selfhost':
				case 'he-net-tunnelbroker':
				case 'route53':
				case 'cloudflare':
				case 'eurodns':
				case 'gratisdns':
				case 'ovh-dynhost':
251 252
				case 'citynetwork':
				case 'duckdns':
Ad Schellevis's avatar
Ad Schellevis committed
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
					$this->_update();
					if($this->_dnsDummyUpdateDone == true) {
						// If a dummy update was needed, then sleep a while and do the update again to put the proper address back.
						// Some providers (e.g. No-IP free accounts) need to have at least 1 address change every month.
						// If the address has not changed recently, or the user did "Force Update", then the code does
						// a dummy address change for providers like this.
						sleep(10);
						$this->_update();
					}
					break;
				default:
					$this->_error(6);
					break;
				}
			}

			unlock($dyndnslck);
		}
271

Ad Schellevis's avatar
Ad Schellevis committed
272 273 274 275 276
		/*
		 * Private Function (added 12 July 05) [beta]
		 *   Send Update To Selected Service.
		 */
		function _update() {
277

Ad Schellevis's avatar
Ad Schellevis committed
278
			if ($this->_dnsVerboseLog)
279
				log_error("Dynamic DNS ({$this->_dnsHost}): DynDns _update() starting.");
280

Ad Schellevis's avatar
Ad Schellevis committed
281 282 283 284 285 286 287 288 289 290 291 292 293 294
			if ($this->_dnsService != 'ods' and $this->_dnsService != 'route53 ') {
				$ch = curl_init();
				curl_setopt($ch, CURLOPT_HEADER, 0);
				curl_setopt($ch, CURLOPT_USERAGENT, $this->_UserAgent);
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
				curl_setopt($ch, CURLOPT_INTERFACE, $this->_dnsRequestIfIP);
				curl_setopt($ch, CURLOPT_TIMEOUT, 120); // Completely empirical
			}

			switch ($this->_dnsService) {
				case 'dyndns':
				case 'dyndns-static':
				case 'dyndns-custom':
					if ($this->_dnsVerboseLog)
295
						log_error("Dynamic DNS: ({$this->_dnsHost}) DNS update() starting.");
Ad Schellevis's avatar
Ad Schellevis committed
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") $this->_dnsWildcard = "ON";
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
					$server = "https://members.dyndns.org/nic/update";
					$port = "";
					if($this->_dnsServer)
						$server = $this->_dnsServer;
					if($this->_dnsPort)
						$port = ":" . $this->_dnsPort;
					curl_setopt($ch, CURLOPT_URL, $server .$port . '?system=dyndns&hostname=' . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard='.$this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=NO');
					break;
				case 'dhs':
					$post_data['hostscmd'] = 'edit';
					$post_data['hostscmdstage'] = '2';
					$post_data['type'] = '4';
					$post_data['updatetype'] = 'Online';
					$post_data['mx'] = $this->_dnsMX;
					$post_data['mx2'] = '';
					$post_data['txt'] = '';
					$post_data['offline_url'] = '';
					$post_data['cloak'] = 'Y';
					$post_data['cloak_title'] = '';
					$post_data['ip'] = $this->_dnsIP;
					$post_data['domain'] = 'dyn.dhs.org';
					$post_data['hostname'] = $this->_dnsHost;
					$post_data['submit'] = 'Update';
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
					$server = "https://members.dhs.org/nic/hosts";
					$port = "";
					if($this->_dnsServer)
						$server = $this->_dnsServer;
					if($this->_dnsPort)
328
						$port = ":" . $this->_dnsPort;
Ad Schellevis's avatar
Ad Schellevis committed
329 330 331 332 333 334 335 336 337 338 339 340 341
					curl_setopt($ch, CURLOPT_URL, '{$server}{$port}');
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
					curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
					break;
				case 'noip':
				case 'noip-free':
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
					$server = "https://dynupdate.no-ip.com/ducupdate.php";
					$port = "";
					if($this->_dnsServer)
						$server = $this->_dnsServer;
					if($this->_dnsPort)
						$port = ":" . $this->_dnsPort;
342 343
					if(($this->_dnsService == "noip-free") &&
					   ($this->_forceUpdateNeeded == true) &&
Ad Schellevis's avatar
Ad Schellevis committed
344 345 346 347
					   ($this->_dnsDummyUpdateDone == false)) {
						// Update the IP to a dummy value to force No-IP free accounts to see a change.
						$iptoset = "192.168.1.1";
						$this->_dnsDummyUpdateDone = true;
348
						log_error("Dynamic DNS ({$this->_dnsHost}): Processing dummy update on No-IP free account. IP temporarily set to " . $iptoset);
Ad Schellevis's avatar
Ad Schellevis committed
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
					} else {
						$iptoset = $this->_dnsIP;
					}
					curl_setopt($ch, CURLOPT_URL, $server . $port . '?username=' . urlencode($this->_dnsUser) . '&pass=' . urlencode($this->_dnsPass) . '&hostname=' . $this->_dnsHost.'&ip=' . $iptoset);
					break;
				case 'easydns':
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
					$server = "https://members.easydns.com/dyn/dyndns.php";
					$port = "";
					if($this->_dnsServer)
						$server = $this->_dnsServer;
					if($this->_dnsPort)
						$port = ":" . $this->_dnsPort;
					curl_setopt($ch, CURLOPT_URL, $server . $port . '?hostname=' . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard=' . $this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=' . $this->_dnsBackMX);
					break;
				case 'hn':
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
					$server = "http://dup.hn.org/vanity/update";
					$port = "";
					if($this->_dnsServer)
						$server = $this->_dnsServer;
					if($this->_dnsPort)
						$port = ":" . $this->_dnsPort;
					curl_setopt($ch, CURLOPT_URL, $server . $port . '?ver=1&IP=' . $this->_dnsIP);
					break;
				case 'zoneedit':
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
					curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);

					$server = "https://dynamic.zoneedit.com/auth/dynamic.html";
					$port = "";
					if($this->_dnsServer)
						$server = $this->_dnsServer;
					if($this->_dnsPort)
						$port = ":" . $this->_dnsPort;
					curl_setopt($ch, CURLOPT_URL, "{$server}{$port}?host=" .$this->_dnsHost);
					break;
				case 'dyns':
					$server = "https://www.dyns.cx/postscript011.php";
					$port = "";
					if($this->_dnsServer)
						$server = $this->_dnsServer;
					if($this->_dnsPort)
395
						$port = ":" . $this->_dnsPort;
Ad Schellevis's avatar
Ad Schellevis committed
396 397 398 399 400 401 402 403 404 405
					curl_setopt($ch, CURLOPT_URL, $server . $port . '?username=' . urlencode($this->_dnsUser) . '&password=' . $this->_dnsPass . '&host=' . $this->_dnsHost);
					break;
				case 'ods':
					$misc_errno = 0;
					$misc_error = "";
					$server = "ods.org";
					$port = "";
					if($this->_dnsServer)
						$server = $this->_dnsServer;
					if($this->_dnsPort)
406
						$port = ":" . $this->_dnsPort;
Ad Schellevis's avatar
Ad Schellevis committed
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
					$this->con['socket'] = fsockopen("{$server}{$port}", "7070", $misc_errno, $misc_error, 30);
					/* Check that we have connected */
					if (!$this->con['socket']) {
						print "error! could not connect.";
						break;
					}
					/* Here is the loop. Read the incoming data (from the socket connection) */
					while (!feof($this->con['socket'])) {
						$this->con['buffer']['all'] = trim(fgets($this->con['socket'], 4096));
						$code = substr($this->con['buffer']['all'], 0, 3);
						sleep(1);
						switch($code) {
							case 100:
								fputs($this->con['socket'], "LOGIN ".$this->_dnsUser." ".$this->_dnsPass."\n");
								break;
							case 225:
								fputs($this->con['socket'], "DELRR ".$this->_dnsHost." A\n");
								break;
							case 901:
								fputs($this->con['socket'], "ADDRR ".$this->_dnsHost." A ".$this->_dnsIP."\n");
								break;
							case 795:
								fputs($this->con['socket'], "QUIT\n");
								break;
						}
					}
					$this->_checkStatus(0, $code);
					break;
				case 'freedns':
					curl_setopt($ch, CURLOPT_URL, 'https://freedns.afraid.org/dynamic/update.php?' . $this->_dnsPass);
					break;
				case 'dnsexit':
					curl_setopt($ch, CURLOPT_URL, 'https://www.dnsexit.com/RemoteUpdate.sv?login='.$this->_dnsUser. '&password='.$this->_dnsPass.'&host='.$this->_dnsHost.'&myip='.$this->_dnsIP);
					break;
				case 'loopia':
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
					curl_setopt($ch, CURLOPT_URL, 'https://dns.loopia.se/XDynDNSServer/XDynDNS.php?hostname='.$this->_dnsHost.'&myip='.$this->_dnsIP);
					break;
				case 'opendns':
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") $this->_dnsWildcard = "ON";
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
					$server = "https://updates.opendns.com/nic/update?hostname=". $this->_dnsHost;
					$port = "";
					if($this->_dnsServer)
						$server = $this->_dnsServer;
					if($this->_dnsPort)
						$port = ":" . $this->_dnsPort;
					curl_setopt($ch, CURLOPT_URL, $server .$port);
					break;

				case 'staticcling':
					curl_setopt($ch, CURLOPT_URL, 'https://www.staticcling.org/update.html?login='.$this->_dnsUser.'&pass='.$this->_dnsPass);
461
					break;
Ad Schellevis's avatar
Ad Schellevis committed
462
				case 'dnsomatic':
463
					/* Example syntax
Ad Schellevis's avatar
Ad Schellevis committed
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
						https://username:password@updates.dnsomatic.com/nic/update?hostname=yourhostname&myip=ipaddress&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG
					*/
					if ($this->_dnsVerboseLog)
						log_error("DNS-O-Matic: DNS update() starting.");
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") $this->_dnsWildcard = "ON";
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
					/*
					Reference: https://www.dnsomatic.com/wiki/api
						DNS-O-Matic usernames are 3-25 characters.
						DNS-O-Matic passwords are 6-20 characters.
						All ASCII letters and numbers accepted.
						Dots, dashes, and underscores allowed, but not at the beginning or end of the string.
					Required: "rawurlencode" http://www.php.net/manual/en/function.rawurlencode.php
						Encodes the given string according to RFC 3986.
					*/
					$server = "https://" . rawurlencode($this->_dnsUser) . ":" . rawurlencode($this->_dnsPass) . "@updates.dnsomatic.com/nic/update?hostname=";
					if($this->_dnsServer)
						$server = $this->_dnsServer;
					if($this->_dnsPort)
						$port = ":" . $this->_dnsPort;
					curl_setopt($ch, CURLOPT_URL, $server . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard='.$this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=NOCHG');
					break;
				case 'namecheap':
					/* Example:
						https://dynamicdns.park-your-domain.com/update?host=[host_name]&domain=[domain.com]&password=[domain_password]&ip=[your_ip]
					*/
					if ($this->_dnsVerboseLog)
						log_error("Namecheap ({$this->_dnsHost}): DNS update() starting.");
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
					$dparts = explode(".", trim($this->_dnsHost));
					$domain_part_count = ($dparts[count($dparts)-1] == "uk") ? 3 : 2;
					$domain_offset = count($dparts) - $domain_part_count;
					$hostname = implode(".", array_slice($dparts, 0, $domain_offset));
					$domain = implode(".", array_slice($dparts, $domain_offset));
					$dnspass = trim($this->_dnsPass);
					$server = "https://dynamicdns.park-your-domain.com/update?host={$hostname}&domain={$domain}&password={$dnspass}&ip={$this->_dnsIP}";
					curl_setopt($ch, CURLOPT_URL, $server);
					break;
				case 'he-net':
				case 'he-net-v6':
					if ($this->_dnsVerboseLog)
						log_error("HE.net ({$this->_dnsHost}): DNS update() starting.");
					$server = "https://dyn.dns.he.net/nic/update?";
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
					curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
					curl_setopt($ch, CURLOPT_URL, $server . 'hostname=' . $this->_dnsHost . '&password=' . $this->_dnsPass . '&myip=' . $this->_dnsIP);
					break;
				case 'he-net-tunnelbroker':
					if ($this->_dnsVerboseLog)
						log_error("HE.net Tunnelbroker: DNS update() starting.");
					$server = "https://ipv4.tunnelbroker.net/ipv4_end.php?";
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser . ':' . $this->_dnsPass);
					curl_setopt($ch, CURLOPT_URL, $server . 'tid=' . $this->_dnsHost);
					break;
				case 'selfhost':
					if ($this->_dnsVerboseLog)
						log_error("SelfHost: DNS update() starting.");
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") $this->_dnsWildcard = "ON";
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
					$server = "https://carol.selfhost.de/nic/update";
					$port = "";
					if($this->_dnsServer)
						$server = $this->_dnsServer;
					if($this->_dnsPort)
						$port = ":" . $this->_dnsPort;
					curl_setopt($ch, CURLOPT_URL, $server .$port . '?system=dyndns&hostname=' . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard='.$this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=NO');
					break;
				case 'route53':
					if ($this->_dnsVerboseLog)
						log_error("Route53 ({$this->_dnsHost}): DNS update() starting.");
536

Ad Schellevis's avatar
Ad Schellevis committed
537 538 539 540 541 542 543 544 545
					/* Setting Variables */
					$hostname = "{$this->_dnsHost}.";
					$ZoneID = $this->_dnsZoneID;
					$AccessKeyId=$this->_dnsUser;
					$SecretAccessKey=$this->_dnsPass;
					$NewIP=$this->_dnsIP;
					$NewTTL=$this->_dnsTTL;

					/* Include Route 53 Library Class */
546
					require_once('dyndns.r53.class');
Ad Schellevis's avatar
Ad Schellevis committed
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592

					/* Set Amazon AWS Credentials for this record */
					$r53 = new Route53($AccessKeyId, $SecretAccessKey);

					/* Function to find old values of records in Route 53 */
					if(!function_exists('Searchrecords')) {
						function SearchRecords($records, $name) {
							$result = array();
							foreach($records as $record) {
								if(strtolower($record['Name']) == strtolower($name)) {
									$result [] = $record;
								}
							}
							return ($result) ? $result : false;
						}
					}

					$records = $r53->listResourceRecordSets("/hostedzone/$ZoneID");

					/* Get IP for your hostname in Route 53 */
					if(false !== ($a_result = SearchRecords($records['ResourceRecordSets'], "$hostname"))) {
						$OldTTL=$a_result[0][TTL];
						$OldIP=$a_result[0][ResourceRecords][0];
					} else {
						$OldIP="";
					}

					/* Check if we need to update DNS Record */
					if ($OldIP !== $NewIP) {
						if(!empty($OldIP)) {
							/* Your Hostname already exists, deleting and creating it again */
							$changes = array();
							$changes[] = $r53->prepareChange(DELETE, $hostname, A, $OldTTL, $OldIP);
							$changes[] = $r53->prepareChange(CREATE, $hostname, A, $NewTTL, $NewIP);
							$result = $r53->changeResourceRecordSets("/hostedzone/$ZoneID", $changes);
						} else {
							/* Your Hostname does not exist yet, creating it */
							$changes = $r53->prepareChange(CREATE, $hostname, A, $NewTTL, $NewIP);
							$result = $r53->changeResourceRecordSets("/hostedzone/$ZoneID", $changes);
						}
					}
					$this->_checkStatus(0, $result);
					break;
				case 'custom':
				case 'custom-v6':
					if ($this->_dnsVerboseLog)
593
						log_error("Custom dynamic DNS ({$this->_dnsHost}): DNS update() starting.");
Ad Schellevis's avatar
Ad Schellevis committed
594 595 596 597 598 599 600
					if ($this->_dnsUser != '') {
						if ($this->_curlIpresolveV4)
							curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
						if ($this->_curlSslVerifypeer)
							curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
						else
							curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
601
						curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
Ad Schellevis's avatar
Ad Schellevis committed
602 603 604 605 606 607 608 609 610 611 612 613 614 615
						curl_setopt($ch, CURLOPT_USERPWD, "{$this->_dnsUser}:{$this->_dnsPass}");
					}
					$server = str_replace("%IP%", $this->_dnsIP, $this->_dnsUpdateURL);
					if ($this->_dnsVerboseLog)
						log_error("Sending request to: ".$server);
					curl_setopt($ch, CURLOPT_URL, $server);
					break;
				case 'cloudflare':
					$dnsServer ='www.cloudflare.com';
					$dnsHost = str_replace(' ','', $this->_dnsHost);
					$URL = "https://{$dnsServer}/api.html?a=DIUP&email={$this->_dnsUser}&tkn={$this->_dnsPass}&ip={$this->_dnsIP}&hosts={$dnsHost}";
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
					curl_setopt($ch, CURLOPT_URL, $URL);
					break;
616
				case 'eurodns':
Ad Schellevis's avatar
Ad Schellevis committed
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
					if ($this->_dnsVerboseLog)
						log_error("EuroDynDns ({$this->_dnsHost}) DNS update() starting.");
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
					$server = "https://eurodyndns.org/update/";
					$port = "";
					if($this->_dnsPort)
						$port = ":" . $this->_dnsPort;
					curl_setopt($ch, CURLOPT_URL, $server .$port . '?hostname=' . $this->_dnsHost . '&myip=' . $this->_dnsIP);
					break;
				case 'gratisdns':
					if ($this->_dnsVerboseLog)
						log_error("GratisDNS.dk ({$this->_dnsHost}): DNS update() starting.");
					$server = "https://ssl.gratisdns.dk/ddns.phtml";
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
					list($hostname, $domain) = explode(".", $this->_dnsHost, 2);
					curl_setopt($ch, CURLOPT_URL, $server . '?u=' . $this->_dnsUser . '&p=' . $this->_dnsPass . '&h=' . $this->_dnsHost . '&d=' . $domain);
					break;
				case 'ovh-dynhost':
					if ($this->_dnsVerboseLog)
						log_error("OVH DynHOST: ({$this->_dnsHost}) DNS update() starting.");
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") $this->_dnsWildcard = "ON";
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
					$server = "https://www.ovh.com/nic/update";
					$port = "";
					if($this->_dnsServer)
						$server = $this->_dnsServer;
					if($this->_dnsPort)
						$port = ":" . $this->_dnsPort;
					curl_setopt($ch, CURLOPT_URL, $server .$port . '?system=dyndns&hostname=' . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard='.$this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=NO');
					break;
				case 'citynetwork':
					if ($this->_dnsVerboseLog)
						log_error("City Network: ({$this->_dnsHost}) DNS update() starting.");
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
					$server = 'https://dyndns.citynetwork.se/nic/update';
					$port = "";
					if($this->_dnsServer)
						$server = $this->_dnsServer;
					if($this->_dnsPort)
						$port = ":" . $this->_dnsPort;
					curl_setopt($ch, CURLOPT_URL, $server .$port . '?hostname=' . $this->_dnsHost . '&myip=' . $this->_dnsIP);
					break;
662 663 664 665 666
				case 'duckdns':
					if ($this->_dnsVerboseLog)
						log_error("Duck DNS ({$this->_dnsHost}): DNS update() starting.");
					$server = "https://www.duckdns.org/update";
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
667
					curl_setopt($ch, CURLOPT_URL, $server . '?domains=' . str_replace('.duckdns.org', '', $this->_dnsHost) . '&token=' . $this->_dnsUser);
668
					break;
Ad Schellevis's avatar
Ad Schellevis committed
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
				default:
					break;
			}
			if ($this->_dnsService != 'ods' and $this->_dnsService != 'route53') {
				$data = curl_exec($ch);
				$this->_checkStatus($ch, $data);
				@curl_close($ch);
			}
		}

		/*
		 * Private Function (added 12 July 2005) [beta]
		 *   Retrieve Update Status
		 */
		function _checkStatus($ch, $data) {
			if ($this->_dnsVerboseLog) {
685 686
				log_error("Dynamic DNS ({$this->_dnsHost}): DynDns _checkStatus() starting.");
				log_error("Dynamic DNS ({$this->_dnsHost}): Current Service: {$this->_dnsService}");
Ad Schellevis's avatar
Ad Schellevis committed
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
			}
			$successful_update = false;
			if ($this->_dnsService != 'ods' and $this->_dnsService != 'route53' && @curl_error($ch)) {
				$status = "Curl error occurred: " . curl_error($ch);
				log_error($status);
				$this->status = $status;
				return;
			}
			switch ($this->_dnsService) {
				case 'dnsomatic':
					if (preg_match('/badauth/i', $data)) {
						$status = "DNS-O-Matic ({$this->_dnsHost}): The DNS-O-Matic username or password specified are incorrect. No updates will be distributed to services until this is resolved.";
					} else if (preg_match('/notfqdn /i', $data)) {
						$status = "DNS-O-Matic ({$this->_dnsHost}): The hostname specified is not a fully-qualified domain name. If no hostnames included, notfqdn will be returned once.";
					} else if (preg_match('/nohost/i', $data)) {
						$status = "DNS-O-Matic ({$this->_dnsHost}): The hostname passed could not be matched to any services configured. The service field will be blank in the return code.";
					} else if (preg_match('/numhost/i', $data)) {
704
						$status = "DNS-O-Matic ({$this->_dnsHost}): You may update up to 20 hosts. numhost is returned if you try to update more than 20 or update a round-robin.";
Ad Schellevis's avatar
Ad Schellevis committed
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
					} else if (preg_match('/abuse/i', $data)) {
						$status = "DNS-O-Matic ({$this->_dnsHost}): The hostname is blocked for update abuse.";
					} else if (preg_match('/good/i', $data)) {
						$status = "DNS-O-Matic ({$this->_dnsHost}): (Success) IP Address Changed Successfully! (".$this->_dnsIP.")";
						$successful_update = true;
					} else if (preg_match('/dnserr/i', $data)) {
						$status = "DNS-O-Matic ({$this->_dnsHost}): DNS error encountered. Stop updating for 30 minutes.";
					} else {
						$status = "DNS-O-Matic ({$this->_dnsHost}): (Unknown Response)";
						log_error("DNS-O-Matic ({$this->_dnsHost}): PAYLOAD: {$data}");
						$this->_debug($data);
					}
					break;
				case 'citynetwork':
					if (preg_match('/notfqdn/i', $data)) {
720
						$status = "Dynamic DNS ({$this->_dnsHost}): (Error) Not A FQDN!";
Ad Schellevis's avatar
Ad Schellevis committed
721
					} else if (preg_match('/nohost/i', $data)) {
722
						$status = "Dynamic DNS ({$this->_dnsHost}): (Error) No such host";
Ad Schellevis's avatar
Ad Schellevis committed
723
					} else if (preg_match('/nochg/i', $data)) {
724
						$status = "Dynamic DNS ({$this->_dnsHost}): (Success) No Change In IP Address";
Ad Schellevis's avatar
Ad Schellevis committed
725 726
						$successful_update = true;
					} else if (preg_match('/good/i', $data)) {
727
						$status = "Dynamic DNS ({$this->_dnsHost}): (Success) IP Address Changed Successfully! (".$this->_dnsIP.")";
Ad Schellevis's avatar
Ad Schellevis committed
728 729
						$successful_update = true;
					} else if (preg_match('/badauth/i', $data)) {
730
						$status = "Dynamic DNS ({$this->_dnsHost}): (Error) User Authorization Failed";
Ad Schellevis's avatar
Ad Schellevis committed
731
					} else {
732 733
						$status = "Dynamic DNS ({$this->_dnsHost}): (Unknown Response)";
						log_error("Dynamic DNS ({$this->_dnsHost}): PAYLOAD: {$data}");
Ad Schellevis's avatar
Ad Schellevis committed
734 735 736 737 738 739
						$this->_debug($data);
					}
					break;
				case 'ovh-dynhost':
				case 'dyndns':
					if (preg_match('/notfqdn/i', $data)) {
740
						$status = "Dynamic DNS ({$this->_dnsHost}): (Error) Not A FQDN!";
Ad Schellevis's avatar
Ad Schellevis committed
741
					} else if (preg_match('/nochg/i', $data)) {
742
						$status = "Dynamic DNS ({$this->_dnsHost}): (Success) No Change In IP Address";
Ad Schellevis's avatar
Ad Schellevis committed
743 744
						$successful_update = true;
					} else if (preg_match('/good/i', $data)) {
745
						$status = "Dynamic DNS ({$this->_dnsHost}): (Success) IP Address Changed Successfully! (".$this->_dnsIP.")";
Ad Schellevis's avatar
Ad Schellevis committed
746 747
						$successful_update = true;
					} else if (preg_match('/noauth/i', $data)) {
748
						$status = "Dynamic DNS ({$this->_dnsHost}): (Error) User Authorization Failed";
Ad Schellevis's avatar
Ad Schellevis committed
749
					} else {
750 751
						$status = "Dynamic DNS ({$this->_dnsHost}): (Unknown Response)";
						log_error("Dynamic DNS ({$this->_dnsHost}): PAYLOAD: {$data}");
Ad Schellevis's avatar
Ad Schellevis committed
752 753 754 755 756
						$this->_debug($data);
					}
					break;
				case 'dyndns-static':
					if (preg_match('/notfqdn/i', $data)) {
757
						$status = "Dynamic DNS ({$this->_dnsHost}): (Error) Not A FQDN!";
Ad Schellevis's avatar
Ad Schellevis committed
758
					} else if (preg_match('/nochg/i', $data)) {
759
						$status = "Dynamic DNS ({$this->_dnsHost}): (Success) No Change In IP Address";
Ad Schellevis's avatar
Ad Schellevis committed
760 761
						$successful_update = true;
					} else if (preg_match('/good/i', $data)) {
762
						$status = "Dynamic DNS ({$this->_dnsHost}): (Success) IP Address Changed Successfully!";
Ad Schellevis's avatar
Ad Schellevis committed
763 764
						$successful_update = true;
					} else if (preg_match('/noauth/i', $data)) {
765
						$status = "Dynamic DNS ({$this->_dnsHost}): (Error) User Authorization Failed";
Ad Schellevis's avatar
Ad Schellevis committed
766
					} else {
767 768
						$status = "Dynamic DNS ({$this->_dnsHost}): (Unknown Response)";
						log_error("Dynamic DNS ({$this->_dnsHost}): PAYLOAD: {$data}");
Ad Schellevis's avatar
Ad Schellevis committed
769 770 771 772 773
						$this->_debug($data);
					}
					break;
				case 'dyndns-custom':
					if (preg_match('/notfqdn/i', $data)) {
774
						$status = "Dynamic DNS ({$this->_dnsHost}): (Error) Not A FQDN!";
Ad Schellevis's avatar
Ad Schellevis committed
775
					} else if (preg_match('/nochg/i', $data)) {
776
						$status = "Dynamic DNS: (Success) No Change In IP Address";
Ad Schellevis's avatar
Ad Schellevis committed
777 778
						$successful_update = true;
					} else if (preg_match('/good/i', $data)) {
779
						$status = "Dynamic DNS ({$this->_dnsHost}): (Success) IP Address Changed Successfully!";
Ad Schellevis's avatar
Ad Schellevis committed
780 781
						$successful_update = true;
					} else if (preg_match('/noauth/i', $data)) {
782
						$status = "Dynamic DNS ({$this->_dnsHost}): (Error) User Authorization Failed";
Ad Schellevis's avatar
Ad Schellevis committed
783
					} else {
784 785
						$status = "Dynamic DNS ({$this->_dnsHost}): (Unknown Response)";
						log_error("Dynamic DNS ({$this->_dnsHost}): PAYLOAD: {$data}");
Ad Schellevis's avatar
Ad Schellevis committed
786 787 788 789 790 791 792 793 794 795
						$this->_debug($data);
					}
					break;
				case 'dhs':
					break;
				case 'noip':
				case 'noip-free':
					list($ip,$code) = explode(":",$data);
					switch ($code) {
						case 0:
796
							$status = "Dynamic DNS ({$this->_dnsHost}): (Success) IP address is current, no update performed.";
Ad Schellevis's avatar
Ad Schellevis committed
797 798 799
							$successful_update = true;
							break;
						case 1:
800
							$status = "Dynamic DNS ({$this->_dnsHost}): (Success) DNS hostname update successful.";
Ad Schellevis's avatar
Ad Schellevis committed
801 802 803
							$successful_update = true;
							break;
						case 2:
804
							$status = "Dynamic DNS ({$this->_dnsHost}): (Error) Hostname supplied does not exist.";
Ad Schellevis's avatar
Ad Schellevis committed
805 806
							break;
						case 3:
807
							$status = "Dynamic DNS ({$this->_dnsHost}): (Error) Invalid Username.";
Ad Schellevis's avatar
Ad Schellevis committed
808 809
							break;
						case 4:
810
							$status = "Dynamic DNS ({$this->_dnsHost}): (Error) Invalid Password.";
Ad Schellevis's avatar
Ad Schellevis committed
811 812
							break;
						case 5:
813
							$status = "Dynamic DNS ({$this->_dnsHost}): (Error) To many updates sent.";
Ad Schellevis's avatar
Ad Schellevis committed
814 815
							break;
						case 6:
816
							$status = "Dynamic DNS ({$this->_dnsHost}): (Error) Account disabled due to violation of No-IP terms of service.";
Ad Schellevis's avatar
Ad Schellevis committed
817 818
							break;
						case 7:
819
							$status = "Dynamic DNS ({$this->_dnsHost}): (Error) Invalid IP. IP Address submitted is improperly formatted or is a private IP address or is on a blacklist.";
Ad Schellevis's avatar
Ad Schellevis committed
820 821
							break;
						case 8:
822
							$status = "Dynamic DNS ({$this->_dnsHost}): (Error) Disabled / Locked Hostname.";
Ad Schellevis's avatar
Ad Schellevis committed
823 824
							break;
						case 9:
825
							$status = "Dynamic DNS ({$this->_dnsHost}): (Error) Host updated is configured as a web redirect and no update was performed.";
Ad Schellevis's avatar
Ad Schellevis committed
826 827
							break;
						case 10:
828
							$status = "Dynamic DNS ({$this->_dnsHost}): (Error) Group supplied does not exist.";
Ad Schellevis's avatar
Ad Schellevis committed
829 830
							break;
						case 11:
831
							$status = "Dynamic DNS ({$this->_dnsHost}): (Success) DNS group update is successful.";
Ad Schellevis's avatar
Ad Schellevis committed
832 833 834
							$successful_update = true;
							break;
						case 12:
835
							$status = "Dynamic DNS ({$this->_dnsHost}): (Success) DNS group is current, no update performed.";
Ad Schellevis's avatar
Ad Schellevis committed
836 837 838
							$successful_update = true;
							break;
						case 13:
839
							$status = "Dynamic DNS ({$this->_dnsHost}): (Error) Update client support not available for supplied hostname or group.";
Ad Schellevis's avatar
Ad Schellevis committed
840 841
							break;
						case 14:
842
							$status = "Dynamic DNS ({$this->_dnsHost}): (Error) Hostname supplied does not have offline settings configured.";
Ad Schellevis's avatar
Ad Schellevis committed
843 844
							break;
						case 99:
845
							$status = "Dynamic DNS ({$this->_dnsHost}): (Error) Client disabled. Client should exit and not perform any more updates without user intervention.";
Ad Schellevis's avatar
Ad Schellevis committed
846 847
							break;
						case 100:
848
							$status = "Dynamic DNS ({$this->_dnsHost}): (Error) Client disabled. Client should exit and not perform any more updates without user intervention.";
Ad Schellevis's avatar
Ad Schellevis committed
849 850
							break;
						default:
851
							$status = "Dynamic DNS ({$this->_dnsHost}): (Unknown Response)";
Ad Schellevis's avatar
Ad Schellevis committed
852 853 854 855 856 857
							$this->_debug("Unknown Response: ".$data);
							break;
					}
					break;
				case 'easydns':
					if (preg_match('/NOACCESS/i', $data)) {
858
						$status = "Dynamic DNS ({$this->_dnsHost}): (Error) Authentication Failed: Username and/or Password was Incorrect.";
Ad Schellevis's avatar
Ad Schellevis committed
859
					} else if (preg_match('/NOSERVICE/i', $data)) {
860
						$status = "Dynamic DNS ({$this->_dnsHost}): (Error) No Service: Dynamic DNS Service has been disabled for this domain.";
Ad Schellevis's avatar
Ad Schellevis committed
861
					} else if (preg_match('/ILLEGAL INPUT/i', $data)) {
862
						$status = "Dynamic DNS ({$this->_dnsHost}): (Error) Illegal Input: Self-Explanatory";
Ad Schellevis's avatar
Ad Schellevis committed
863
					} else if (preg_match('/TOOSOON/i', $data)) {
864
						$status = "Dynamic DNS ({$this->_dnsHost}): (Error) Too Soon: Not Enough Time Has Elapsed Since Last Update";
Ad Schellevis's avatar
Ad Schellevis committed
865
					} else if (preg_match('/NOERROR/i', $data)) {
866
						$status = "Dynamic DNS ({$this->_dnsHost}): (Success) IP Updated Successfully!";
Ad Schellevis's avatar
Ad Schellevis committed
867 868
						$successful_update = true;
					} else {
869 870
						$status = "Dynamic DNS ({$this->_dnsHost}): (Unknown Response)";
						log_error("Dynamic DNS ({$this->_dnsHost}): PAYLOAD: {$data}");
Ad Schellevis's avatar
Ad Schellevis committed
871 872 873 874 875 876 877 878
						$this->_debug($data);
					}
					break;
				case 'hn':
					/* FIXME: add checks */
					break;
				case 'zoneedit':
					if (preg_match('/799/i', $data)) {
879
						$status = "Dynamic DNS ({$this->_dnsHost}): (Error 799) Update Failed!";
Ad Schellevis's avatar
Ad Schellevis committed
880
					} else if (preg_match('/700/i', $data)) {
881
						$status = "Dynamic DNS ({$this->_dnsHost}): (Error 700) Update Failed!";
Ad Schellevis's avatar
Ad Schellevis committed
882
					} else if (preg_match('/200/i', $data)) {
883
						$status = "Dynamic DNS ({$this->_dnsHost}): (Success) IP Address Updated Successfully!";
Ad Schellevis's avatar
Ad Schellevis committed
884 885
						$successful_update = true;
					} else if (preg_match('/201/i', $data)) {
886
						$status = "Dynamic DNS ({$this->_dnsHost}): (Success) IP Address Updated Successfully!";
887
						$successful_update = true;
Ad Schellevis's avatar
Ad Schellevis committed
888
					} else {
889 890
						$status = "Dynamic DNS ({$this->_dnsHost}): (Unknown Response)";
						log_error("Dynamic DNS ({$this->_dnsHost}): PAYLOAD: {$data}");
Ad Schellevis's avatar
Ad Schellevis committed
891 892 893 894 895
						$this->_debug($data);
					}
					break;
				case 'dyns':
					if (preg_match("/400/i", $data)) {
896
						$status = "Dynamic DNS ({$this->_dnsHost}): (Error) Bad Request - The URL was malformed. Required parameters were not provided.";
Ad Schellevis's avatar
Ad Schellevis committed
897
					} else if (preg_match('/402/i', $data)) {
898
						$status = "Dynamic DNS ({$this->_dnsHost}): (Error) Update Too Soon - You have tried updating to quickly since last change.";
Ad Schellevis's avatar
Ad Schellevis committed
899
					} else if (preg_match('/403/i', $data)) {
900
						$status = "Dynamic DNS ({$this->_dnsHost}): (Error) Database Error - There was a server-sided database error.";
Ad Schellevis's avatar
Ad Schellevis committed
901
					} else if (preg_match('/405/i', $data)) {
902
						$status = "Dynamic DNS ({$this->_dnsHost}): (Error) Hostname Error - The hostname (".$this->_dnsHost.") doesn't belong to you.";
Ad Schellevis's avatar
Ad Schellevis committed
903
					} else if (preg_match('/200/i', $data)) {
904
						$status = "Dynamic DNS ({$this->_dnsHost}): (Success) IP Address Updated Successfully!";
Ad Schellevis's avatar
Ad Schellevis committed
905 906
						$successful_update = true;
					} else {
907 908
						$status = "Dynamic DNS ({$this->_dnsHost}): (Unknown Response)";
						log_error("Dynamic DNS ({$this->_dnsHost}): PAYLOAD: {$data}");
Ad Schellevis's avatar
Ad Schellevis committed
909 910 911 912 913
						$this->_debug($data);
					}
					break;
				case 'ods':
					if (preg_match("/299/i", $data)) {
914
						$status = "Dynamic DNS ({$this->_dnsHost}): (Success) IP Address Updated Successfully!";
Ad Schellevis's avatar
Ad Schellevis committed
915 916
						$successful_update = true;
					} else {
917 918
						$status = "Dynamic DNS ({$this->_dnsHost}): (Unknown Response)";
						log_error("Dynamic DNS ({$this->_dnsHost}): PAYLOAD: {$data}");
Ad Schellevis's avatar
Ad Schellevis committed
919 920 921 922 923
						$this->_debug($data);
					}
					break;
				case 'freedns':
					if (preg_match("/has not changed./i", $data)) {
924
						$status = "Dynamic DNS ({$this->_dnsHost}): (Success) No Change In IP Address";
Ad Schellevis's avatar
Ad Schellevis committed
925 926
						$successful_update = true;
					} else if (preg_match("/Updated/i", $data)) {
927
						$status = "Dynamic DNS ({$this->_dnsHost}): (Success) IP Address Changed Successfully!";
Ad Schellevis's avatar
Ad Schellevis committed
928 929
						$successful_update = true;
					} else {
930 931
						$status = "Dynamic DNS ({$this->_dnsHost}): (Unknown Response)";
						log_error("Dynamic DNS ({$this->_dnsHost}): PAYLOAD: {$data}");
Ad Schellevis's avatar
Ad Schellevis committed
932
						$this->_debug($data);
933
					}
Ad Schellevis's avatar
Ad Schellevis committed
934 935 936 937 938 939
					break;
				case 'dnsexit':
					if (preg_match("/is the same/i", $data)) {
						$status = "phpDynDns ({$this->_dnsHost}): (Success) No Change In IP Address";
						$successful_update = true;
					} else if (preg_match("/Success/i", $data)) {
940
						$status = "Dynamic DNS ({$this->_dnsHost}): (Success) IP Address Changed Successfully!";
Ad Schellevis's avatar
Ad Schellevis committed
941 942
						$successful_update = true;
					} else {
943 944
						$status = "Dynamic DNS ({$this->_dnsHost}): (Unknown Response)";
						log_error("Dynamic DNS ({$this->_dnsHost}): PAYLOAD: {$data}");
Ad Schellevis's avatar
Ad Schellevis committed
945 946 947 948 949
						$this->_debug($data);
					}
					break;
				case 'loopia':
					if (preg_match("/nochg/i", $data)) {
950
						$status = "Dynamic DNS ({$this->_dnsHost}): (Success) No Change In IP Address";
Ad Schellevis's avatar
Ad Schellevis committed
951 952
						$successful_update = true;
					} else if (preg_match("/good/i", $data)) {
953
						$status = "Dynamic DNS ({$this->_dnsHost}): (Success) IP Address Changed Successfully!";
Ad Schellevis's avatar
Ad Schellevis committed
954 955
						$successful_update = true;
					} else if (preg_match('/badauth/i', $data)) {
956
						$status = "Dynamic DNS ({$this->_dnsHost}): (Error) User Authorization Failed";
Ad Schellevis's avatar
Ad Schellevis committed
957
					} else {
958 959
						$status = "Dynamic DNS ({$this->_dnsHost}): (Unknown Response)";
						log_error("Dynamic DNS ({$this->_dnsHost}): PAYLOAD: {$data}");
Ad Schellevis's avatar
Ad Schellevis committed
960 961 962 963 964
						$this->_debug($data);
					}
					break;
				case 'opendns':
					if (preg_match('/badauth/i', $data)) {
965
						$status = "Dynamic DNS({$this->_dnsHost}): (Error) Not a valid username or password!";
Ad Schellevis's avatar
Ad Schellevis committed
966
					} else if (preg_match('/nohost/i', $data)) {
967
						$status = "Dynamic DNS ({$this->_dnsHost}): (Error) Hostname you are trying to update does not exist.";
Ad Schellevis's avatar
Ad Schellevis committed
968 969
						$successful_update = true;
					} else if (preg_match('/good/i', $data)) {
970
						$status = "Dynamic DNS ({$this->_dnsHost}): (Success) IP Address Changed Successfully! (".$this->_dnsIP.")";
Ad Schellevis's avatar
Ad Schellevis committed
971 972
						$successful_update = true;
					} else if (preg_match('/yours/i', $data)) {
973
						$status = "Dynamic DNS ({$this->_dnsHost}): (Error) hostname specified exists, but not under the username specified.";
Ad Schellevis's avatar
Ad Schellevis committed
974 975 976
					} else if (preg_match('/abuse/i', $data)) {
						$status = "phpDynDns ({$this->_dnsHost}): (Error) Updating too frequently, considered abuse.";
					} else {
977 978
						$status = "Dynamic DNS ({$this->_dnsHost}): (Unknown Response)";
						log_error("Dynamic DNS ({$this->_dnsHost}): PAYLOAD: {$data}");
Ad Schellevis's avatar
Ad Schellevis committed
979 980 981 982 983
						$this->_debug($data);
					}
					break;
				case 'staticcling':
					if (preg_match("/invalid ip/i", $data)) {
984
						$status = "Dynamic DNS ({$this->_dnsHost}): (Error) Bad Request - The IP provided was invalid.";
Ad Schellevis's avatar
Ad Schellevis committed
985
					} else if (preg_match('/required info missing/i', $data)) {
986
						$status = "Dynamic DNS ({$this->_dnsHost}): (Error) Bad Request - Required parameters were not provided.";
Ad Schellevis's avatar
Ad Schellevis committed
987
					} else if (preg_match('/invalid characters/i', $data)) {
988
						$status = "Dynamic DNS ({$this->_dnsHost}): (Error) Bad Request - Illegal characters in either the username or the password.";
Ad Schellevis's avatar
Ad Schellevis committed
989
					} else if (preg_match('/bad password/i', $data)) {
990
						$status = "Dynamic DNS ({$this->_dnsHost}): (Error) Invalid password.";
Ad Schellevis's avatar
Ad Schellevis committed
991
					} else if (preg_match('/account locked/i', $data)) {
992
						$status = "Dynamic DNS ({$this->_dnsHost}): (Error) This account has been administratively locked.";
Ad Schellevis's avatar
Ad Schellevis committed
993
					} else if (preg_match('/update too frequent/i', $data)) {
994
						$status = "Dynamic DNS ({$this->_dnsHost}): (Error) Updating too frequently.";
Ad Schellevis's avatar
Ad Schellevis committed
995
					} else if (preg_match('/DB error/i', $data)) {
996
						$status = "Dynamic DNS ({$this->_dnsHost}): (Error) Server side error.";
Ad Schellevis's avatar
Ad Schellevis committed
997
					} else if (preg_match('/success/i', $data)) {
998
						$status = "Dynamic DNS ({$this->_dnsHost}): (Success) IP Address Updated Successfully!";
Ad Schellevis's avatar
Ad Schellevis committed
999 1000
						$successful_update = true;
					} else {
1001 1002
						$status = "Dynamic DNS ({$this->_dnsHost}): (Unknown Response)";
						log_error("Dynamic DNS ({$this->_dnsHost}): PAYLOAD: {$data}");
Ad Schellevis's avatar
Ad Schellevis committed
1003 1004 1005 1006 1007 1008 1009
						$this->_debug($data);
					}
					break;
				case 'namecheap':
					$tmp = str_replace("^M", "", $data);
					$ncresponse = @xml2array($tmp);
					if (preg_match("/internal server error/i", $data)) {
1010
						$status = "Dynamic DNS: (Error) Server side error.";
Ad Schellevis's avatar
Ad Schellevis committed
1011
					} else if (preg_match("/request is badly formed/i", $data)) {
1012
						$status = "Dynamic DNS: (Error) Badly Formed Request (check your settings).";
Ad Schellevis's avatar
Ad Schellevis committed
1013
					} else if ($ncresponse['interface-response']['ErrCount'] === "0") {
1014
						$status = "Dynamic DNS: (Success) IP Address Updated Successfully!";
Ad Schellevis's avatar
Ad Schellevis committed
1015 1016
						$successful_update = true;
					} else if (is_numeric($ncresponse['interface-response']['ErrCount']) && ($ncresponse['interface-response']['ErrCount'] > 0)) {
1017
						$status = "Dynamic DNS: (Error) " . implode(", ", $ncresponse["interface-response"]["errors"]);
Ad Schellevis's avatar
Ad Schellevis committed
1018 1019
						$successful_update = true;
					} else {
1020 1021
						$status = "Dynamic DNS: (Unknown Response)";
						log_error("Dynamic DNS: PAYLOAD: {$data}");
Ad Schellevis's avatar
Ad Schellevis committed
1022 1023 1024
						$this->_debug($data);
					}
					break;
1025

Ad Schellevis's avatar
Ad Schellevis committed
1026 1027 1028
				case 'he-net':
				case 'he-net-v6':
					if (preg_match("/badip/i", $data)) {
1029
						$status = "Dynamic DNS: (Error) Bad Request - The IP provided was invalid.";
Ad Schellevis's avatar
Ad Schellevis committed
1030
					} else if (preg_match('/nohost/i', $data)) {
1031
						$status = "Dynamic DNS: (Error) Bad Request - A hostname was not provided.";
Ad Schellevis's avatar
Ad Schellevis committed
1032
					} else if (preg_match('/badauth/i', $data)) {
1033
						$status = "Dynamic DNS: (Error) Invalid username or password.";
Ad Schellevis's avatar
Ad Schellevis committed
1034
					} else if (preg_match('/good/i', $data)) {
1035
						$status = "Dynamic DNS: (Success) IP Address Updated Successfully!";
Ad Schellevis's avatar
Ad Schellevis committed
1036 1037
						$successful_update = true;
					} else if (preg_match('/nochg/i', $data)) {
1038
						$status = "Dynamic DNS: (Success) No Change In IP Address.";
Ad Schellevis's avatar
Ad Schellevis committed
1039 1040
						$successful_update = true;
					} else {
1041 1042
						$status = "Dynamic DNS: (Unknown Response)";
						log_error("Dynamic DNS: PAYLOAD: {$data}");
Ad Schellevis's avatar
Ad Schellevis committed
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
						$this->_debug($data);
					}
					break;
				case 'he-net-tunnelbroker':
					/*
					-ERROR: Missing parameter(s).
					-ERROR: Invalid API key or password
					-ERROR: Tunnel not found
					-ERROR: Another tunnel exists for this IP.
					-ERROR: This tunnel is already associated with this IP address
					+OK: Tunnel endpoint updated to: x.x.x.x
					*/
					if (preg_match("/Missing parameter/i", $data)) {
1056
						$status = "Dynamic DNS: (Error) Bad Request - Missing/Invalid Parameters.";
Ad Schellevis's avatar
Ad Schellevis committed
1057
					} else if (preg_match('/Tunnel not found/i', $data)) {
1058
						$status = "Dynamic DNS: (Error) Bad Request - Invalid Tunnel ID.";
Ad Schellevis's avatar
Ad Schellevis committed
1059
					} else if (preg_match('/Invalid API key or password/i', $data)) {
1060
						$status = "Dynamic DNS: (Error) Invalid username or password.";
Ad Schellevis's avatar
Ad Schellevis committed
1061
					} else if (preg_match('/OK:/i', $data)) {
1062
						$status = "Dynamic DNS: (Success) IP Address Updated Successfully!";
Ad Schellevis's avatar
Ad Schellevis committed
1063 1064
						$successful_update = true;
					} else if (preg_match('/This tunnel is already associated with this IP address/i', $data)) {
1065
						$status = "Dynamic DNS: (Success) No Change In IP Address.";
Ad Schellevis's avatar
Ad Schellevis committed
1066 1067
						$successful_update = true;
					} else {
1068 1069
						$status = "Dynamic DNS: (Unknown Response)";
						log_error("Dynamic DNS: PAYLOAD: {$data}");
Ad Schellevis's avatar
Ad Schellevis committed
1070 1071 1072 1073 1074
						$this->_debug($data);
					}
					break;
				case 'selfhost':
					if (preg_match('/notfqdn/i', $data)) {
1075
						$status = "Dynamic DNS: (Error) Not A FQDN!";
Ad Schellevis's avatar
Ad Schellevis committed
1076
					} else if (preg_match('/nochg/i', $data)) {
1077
						$status = "Dynamic DNS: (Success) No Change In IP Address";
Ad Schellevis's avatar
Ad Schellevis committed
1078 1079
						$successful_update = true;
					} else if (preg_match('/good/i', $data)) {
1080
						$status = "Dynamic DNS: (Success) IP Address Changed Successfully! (".$this->_dnsIP.")";
Ad Schellevis's avatar
Ad Schellevis committed
1081 1082
						$successful_update = true;
					} else if (preg_match('/noauth/i', $data)) {
1083
						$status = "Dynamic DNS: (Error) User Authorization Failed";
Ad Schellevis's avatar
Ad Schellevis committed
1084
					} else {
1085 1086
						$status = "Dynamic DNS: (Unknown Response)";
						log_error("Dynamic DNS: PAYLOAD: {$data}");
Ad Schellevis's avatar
Ad Schellevis committed
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
						$this->_debug($data);
					}
					break;
				case 'route53':
					$successful_update = true;
					break;
				case 'custom':
				case 'custom-v6':
					$successful_update = false;
					if ($this->_dnsResultMatch == "") {
						$successful_update = true;
					}else {
						$this->_dnsResultMatch = str_replace("%IP%", $this->_dnsIP, $this->_dnsResultMatch);
						$matches = preg_split("/(?<!\\\\)\\|/", $this->_dnsResultMatch);
						foreach($matches as $match) {
							$match= str_replace("\\|", "|", $match);
							if(strcmp($match, trim($data, "\t\n\r")) == 0)
								$successful_update = true;
						}
						unset ($matches);
					}
					if ($successful_update == true)
1109
						$status = "Dynamic DNS: (Success) IP Address Updated Successfully!";
Ad Schellevis's avatar
Ad Schellevis committed
1110
					else
1111
						$status = "Dynamic DNS: (Error) Result did not match.";
Ad Schellevis's avatar
Ad Schellevis committed
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
					break;
				case 'cloudflare':
					// recieve multipe results
					$data = explode("\n",$data);
					$lines = count($data)-1;

					// loop over the lines
					for ($pos=0; ($successful_update || $pos == 0) && $pos < $lines; $pos++){
						$resp = $data[$pos];
						if (preg_match('/UAUTH/i', $resp)) {
1122
							$status = "Dynamic DNS: The username specified is not authorized to update this hostname and domain.";
Ad Schellevis's avatar
Ad Schellevis committed
1123
						} else if (preg_match('/NOHOST/i', $resp)) {
1124
							$status = "Dynamic DNS: No valid FQDN (fully qualified domain name) was specified";
Ad Schellevis's avatar
Ad Schellevis committed
1125
						} else if (preg_match('/INVLDHST/i', $resp)) {
1126
							$status = "Dynamic DNS: An invalid hostname was specified. This may be due to the fact the hostname has not been created in the system. Creating new host names via clients is not supported.";
Ad Schellevis's avatar
Ad Schellevis committed
1127
						} else if (preg_match('/INVLDIP/i', $resp)) {
1128
							$status = "Dynamic DNS: The IP address given is not valid.";
Ad Schellevis's avatar
Ad Schellevis committed
1129
						} else if (preg_match('/DUPHST/i', $resp)) {
1130
							$status = "Dynamic DNS: Duplicate values exist for a record. Only single values for records are supported currently.";
Ad Schellevis's avatar
Ad Schellevis committed
1131
						} else if (preg_match('/NOUPDATE/i', $resp)) {
1132
							$status = "Dynamic DNS: No changes made to the hostname (".strtok($resp,' ')."). Continual updates with no changes lead to blocked clients.";
Ad Schellevis's avatar
Ad Schellevis committed
1133 1134
							$successful_update = true; //success if it is the same so that it saves
						} else if (preg_match('/OK/i', $resp)) {
1135
							$status = "Dynamic DNS: (Success) (".strtok($resp,' ').") IP Address for Changed Successfully!";
Ad Schellevis's avatar
Ad Schellevis committed
1136 1137
							$successful_update = true;
						} else {
1138 1139
							$status = "Dynamic DNS: (Unknown Response)";
							log_error("Dynamic DNS: PAYLOAD: {$resp}");
Ad Schellevis's avatar
Ad Schellevis committed
1140 1141 1142 1143 1144 1145 1146
							$this->_debug($resp);
						}
						log_error($status);
					}
					break;
				case 'eurodns':
					if (preg_match('/notfqdn/i', $data)) {
1147
						$status = "Dynamic DNS: (Error) Not A FQDN!";
Ad Schellevis's avatar
Ad Schellevis committed
1148
					} else if (preg_match('/nochg/i', $data)) {
1149
						$status = "Dynamic DNS: (Success) No Change In IP Address";
Ad Schellevis's avatar
Ad Schellevis committed
1150 1151
						$successful_update = true;
					} else if (preg_match('/good/i', $data)) {
1152
						$status = "Dynamic DNS: (Success) IP Address Changed Successfully! (".$this->_dnsIP.")";
Ad Schellevis's avatar
Ad Schellevis committed
1153 1154
						$successful_update = true;
					} else if (preg_match('/badauth/i', $data)) {
1155
						$status = "Dynamic DNS: (Error) User Authorization Failed";
Ad Schellevis's avatar
Ad Schellevis committed
1156
					} else {
1157 1158
						$status = "Dynamic DNS: (Unknown Response)";
						log_error("Dynamic DNS: PAYLOAD: {$data}");
Ad Schellevis's avatar
Ad Schellevis committed
1159 1160 1161 1162 1163
						$this->_debug($data);
					}
					break;
				case 'gratisdns':
					if (preg_match('/Forkerte værdier/i', $data)) {
1164
					        $status = "Dynamic DNS: (Error) Wrong values - Update could not be completed.";
Ad Schellevis's avatar
Ad Schellevis committed
1165
					} else if (preg_match('/Bruger login: Bruger eksistere ikke/i', $data)) {
1166
					        $status = "Dynamic DNS: (Error) Unknown username - User does not exist.";
Ad Schellevis's avatar
Ad Schellevis committed
1167
					} else if (preg_match('/Bruger login: 1Fejl i kodeord/i', $data)) {
1168
					        $status = "Dynamic DNS: (Error) Wrong password - Remember password is case sensitive.";
Ad Schellevis's avatar
Ad Schellevis committed
1169
					} else if (preg_match('/Domæne kan IKKE administreres af bruger/i', $data)) {
1170
					        $status = "Dynamic DNS: (Error) User unable to administer the selected domain.";
Ad Schellevis's avatar
Ad Schellevis committed
1171
					} else if (preg_match('/OK/i', $data)) {
1172
					        $status = "Dynamic DNS: (Success) IP Address Updated Successfully!";
Ad Schellevis's avatar
Ad Schellevis committed
1173 1174
					        $successful_update = true;
					} else {
1175 1176
					        $status = "Dynamic DNS: (Unknown Response)";
					        log_error("Dynamic DNS: PAYLOAD: {$data}");
Ad Schellevis's avatar
Ad Schellevis committed
1177 1178 1179
					        $this->_debug($data);
					}
					break;
1180 1181
				case 'duckdns':
					if (preg_match('/OK/i', $data)) {
1182
					        $status = "Dynamic DNS: (Success) IP Address Updated Successfully!";
1183 1184
					        $successful_update = true;
					} else {
1185 1186
					        $status = "Dynamic DNS: (Unknown Response)";
					        log_error("Dynamic DNS: PAYLOAD: {$data}");
1187 1188 1189
					        $this->_debug($data);
					}
					break;
Ad Schellevis's avatar
Ad Schellevis committed
1190
			}
1191

Ad Schellevis's avatar
Ad Schellevis committed
1192 1193 1194 1195 1196
			if($successful_update == true) {
				/* Write WAN IP to cache file */
				$wan_ip = $this->_checkIP();
				if ($this->_useIPv6 == false && $wan_ip > 0) {
					$currentTime = time();
1197 1198
					notify_all_remote(sprintf(gettext("Dynamic DNS updated IP Address on %s (%s) to %s"), convert_real_interface_to_friendly_descr($this->_if), $this->_if, $wan_ip));
					log_error("Dynamic DNS: updating cache file {$this->_cacheFile}: {$wan_ip}");
Ad Schellevis's avatar
Ad Schellevis committed
1199 1200 1201 1202 1203
					@file_put_contents($this->_cacheFile, "{$wan_ip}:{$currentTime}");
				} else
					@unlink($this->_cacheFile);
				if ($this->_useIPv6 == true && $wan_ip > 0) {
					$currentTime = time();
1204 1205
					notify_all_remote(sprintf(gettext("Dynamic DNS updated IPv6 Address on %s (%s) to %s"), convert_real_interface_to_friendly_descr($this->_if), $this->_if, $wan_ip));
					log_error("Dynamic DNS: updating cache file {$this->_cacheFile_v6}: {$wan_ip}");
Ad Schellevis's avatar
Ad Schellevis committed
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
					@file_put_contents($this->_cacheFile_v6, "{$wan_ip}|{$currentTime}");
				} else
					@unlink($this->_cacheFile_v6);
			}
			$this->status = $status;
			log_error($status);
		}

		/*
		 * Private Function (added 12 July 05) [beta]
		 *   Return Error, Set Last Error, and Die.
		 */
		function _error($errorNumber = '1') {
			switch ($errorNumber) {
				case 0:
					break;
				case 2:
1223
					$error = 'Dynamic DNS: (ERROR!) No Dynamic DNS Service provider was selected.';
Ad Schellevis's avatar
Ad Schellevis committed
1224 1225
					break;
				case 3:
1226
					$error = 'Dynamic DNS: (ERROR!) No Username Provided.';
Ad Schellevis's avatar
Ad Schellevis committed
1227 1228
					break;
				case 4:
1229
					$error = 'Dynamic DNS: (ERROR!) No Password Provided.';
Ad Schellevis's avatar
Ad Schellevis committed
1230 1231
					break;
				case 5:
1232
					$error = 'Dynamic DNS: (ERROR!) No Hostname Provided.';
Ad Schellevis's avatar
Ad Schellevis committed
1233 1234
					break;
				case 6:
1235
					$error = 'Dynamic DNS: (ERROR!) The Dynamic DNS Service provided is not yet supported.';
Ad Schellevis's avatar
Ad Schellevis committed
1236 1237
					break;
				case 7:
1238
					$error = 'Dynamic DNS: (ERROR!) No Update URL Provided.';
Ad Schellevis's avatar
Ad Schellevis committed
1239 1240 1241 1242 1243 1244
					break;
				case 8:
					$status = "Route 53: (Error) Invalid ZoneID";
					break;
				case 9:
					$status = "Route 53: (Error) Invalid TTL";
1245
					break;
Ad Schellevis's avatar
Ad Schellevis committed
1246
				case 10:
1247
					$error = "Dynamic DNS ({$this->_dnsHost}): No change in my IP address and/or " . $this->_dnsMaxCacheAgeDays . " days has not passed. Not updating dynamic DNS entry.";
Ad Schellevis's avatar
Ad Schellevis committed
1248 1249
					break;
				default:
1250
					$error = "Dynamic DNS: (ERROR!) Unknown Response.";
Ad Schellevis's avatar
Ad Schellevis committed
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
					/* FIXME: $data isn't in scope here */
					/* $this->_debug($data); */
					break;
			}
			$this->lastError = $error;
			log_error($error);
		}

		/*
		 * Private Function (added 12 July 05) [beta]
		 *   - Detect whether or not IP needs to be updated.
		 *      | Written Specifically for pfSense (https://www.pfsense.org) may
		 *      | work with other systems. pfSense base is FreeBSD.
		 */
		function _detectChange() {
			$currentTime = time();

			$wan_ip = $this->_checkIP();
			if ($wan_ip == 0) {
				log_error("DynDns ({$this->_dnsHost}): Current WAN IP could not be determined, skipping update process.");
				return false;
			}
			$log_error = "DynDns ({$this->_dnsHost}): Current WAN IP: {$wan_ip} ";

			if ($this->_useIPv6 == true) {
				if (file_exists($this->_cacheFile_v6)) {
					$contents = file_get_contents($this->_cacheFile_v6);
					list($cacheIP,$cacheTime) = explode('|', $contents);
					$this->_debug($cacheIP.'/'.$cacheTime);
					$initial = false;
					$log_error .= "Cached IPv6: {$cacheIP} ";
				} else {
					$cacheIP = '::';
					@file_put_contents($this->_cacheFile, "::|{$currentTime}");
					$cacheTime = $currentTime;
					$initial = true;
					$log_error .= "No Cached IPv6 found.";
				}
			} else {
				if (file_exists($this->_cacheFile)) {
					$contents = file_get_contents($this->_cacheFile);
					list($cacheIP,$cacheTime) = explode(':', $contents);
					$this->_debug($cacheIP.'/'.$cacheTime);
					$initial = false;
					$log_error .= "Cached IP: {$cacheIP} ";
				} else {
					$cacheIP = '0.0.0.0';
					@file_put_contents($this->_cacheFile, "0.0.0.0:{$currentTime}");
					$cacheTime = $currentTime;
					$initial = true;
					$log_error .= "No Cached IP found.";
				}
			}
			if ($this->_dnsVerboseLog)
				log_error($log_error);

			// Convert seconds = days * hr/day * min/hr * sec/min
			$maxCacheAgeSecs = $this->_dnsMaxCacheAgeDays * 24 * 60 * 60;

			$needs_updating = FALSE;
			/* lets determine if the item needs updating */
			if ($cacheIP != $wan_ip) {
				$needs_updating = true;
				$update_reason = "DynDns: cacheIP != wan_ip.  Updating. ";
				$update_reason .= "Cached IP: {$cacheIP} WAN IP: {$wan_ip} ";
			}
			if (($currentTime - $cacheTime) > $maxCacheAgeSecs) {
				$needs_updating = true;
				$this->_forceUpdateNeeded = true;
				$update_reason = "DynDns: More than " . $this->_dnsMaxCacheAgeDays . " days.  Updating. ";
				$update_reason .= "{$currentTime} - {$cacheTime} > {$maxCacheAgeSecs} ";
			}
			if ($initial == true) {
				$needs_updating = true;
				$update_reason .= "Initial update. ";
			}

			/*   finally if we need updating then store the
			 *   new cache value and return true
			 */
			if ($needs_updating == true) {
				if ($this->_dnsVerboseLog)
					log_error("DynDns ({$this->_dnsHost}): {$update_reason}");
				return true;
			}

1337
			return false;
Ad Schellevis's avatar
Ad Schellevis committed
1338 1339 1340 1341 1342 1343
		}

		/*
		 * Private Function (added 16 July 05) [beta]
		 *   - Writes debug information to a file.
		 *   - This function is only called when a unknown response
1344
		 *   - status is returned from a dynamic DNS service provider.
Ad Schellevis's avatar
Ad Schellevis committed
1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407
		 */
		function _debug($data) {
			$string = date('m-d-y h:i:s').' - ('.$this->_debugID.') - ['.$this->_dnsService.'] - '.$data."\n";
			$file = fopen($this->_debugFile, 'a');
			fwrite($file, $string);
			fclose($file);
		}
		function _checkIP() {
			if ($this->_useIPv6 == true) {
				$ip_address = find_interface_ipv6($this->_if);
				if (!is_ipaddrv6($ip_address))
					return 0;
			} else {
				$ip_address = find_interface_ip($this->_if);
				if (!is_ipaddr($ip_address))
					return 0;
			}
			if ($this->_useIPv6 == false && is_private_ip($ip_address)) {
				$hosttocheck = "checkip.dyndns.org";
				$try = 0;
				while ($try < 3) {
					$checkip = gethostbyname($hosttocheck);
					if (is_ipaddr($checkip))
						break;
					$try++;
				}
				if ($try >= 3) {
					log_error("Dyndns debug information ({$this->_dnsHost}): Could not resolve {$hosttocheck} to ip using interface ip {$ip_address}.");
					return 0;
				}
				$ip_ch = curl_init("http://{$checkip}");
				curl_setopt($ip_ch, CURLOPT_RETURNTRANSFER, 1);
				curl_setopt($ip_ch, CURLOPT_SSL_VERIFYPEER, FALSE);
				curl_setopt($ip_ch, CURLOPT_INTERFACE, $ip_address);
				curl_setopt($ip_ch, CURLOPT_CONNECTTIMEOUT, '30');
				curl_setopt($ip_ch, CURLOPT_TIMEOUT, 120);
				if ($this->_useIPv6 == false) {
					curl_setopt($ip_ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
				}
				$ip_result_page = curl_exec($ip_ch);
				curl_close($ip_ch);
				$ip_result_decoded = urldecode($ip_result_page);
				preg_match('/Current IP Address: (.*)<\/body>/', $ip_result_decoded, $matches);
				$ip_address = trim($matches[1]);
				if (is_ipaddr($ip_address)) {
					if ($this->_dnsVerboseLog)
						log_error("DynDns ({$this->_dnsHost}): {$ip_address} extracted from {$hosttocheck}");
				} else {
					log_error("DynDns ({$this->_dnsHost}): IP address could not be extracted from {$hosttocheck}");
					return 0;
				}
			} else {
				if ($this->_dnsVerboseLog)
					log_error("DynDns ({$this->_dnsHost}): {$ip_address} extracted from local system.");
			}
			$this->_dnsIP = $ip_address;

			return $ip_address;
		}

	}

?>