/** * Performs the update * * @param RequestInterface $request * * @return ResponseInterface * @throws \RuntimeException When ipv6 address is given * @throws \RuntimeException When no ipv4 address is passed */ public function update(RequestInterface $request) { if (null === $request->getIPv4()) { throw new \RuntimeException('You have to pass an ipv4 address'); } if ($request->getIPv6()) { throw new \RuntimeException('Cannot use ipv6 with dyn.com provider'); } $url = sprintf('https://%s:%s@members.dyndns.org/nic/update?hostname=%s&myip=%s&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG', $this->user, $this->password, $request->getHost(), $request->getIPv4()); $curl = curl_init($url); curl_setopt_array($curl, array(CURLOPT_RETURNTRANSFER => 1, CURLOPT_USERAGENT => 'PHP-DynDNS - Library - 1.0')); $response = curl_exec($curl); curl_close($curl); if (false !== strpos($response, 'good')) { return new Response(true, $response); } else { return new Response(false, $response); } }
/** * Performs the update * * @param RequestInterface $request * * @return ResponseInterface * @throws \RuntimeException */ public function update(RequestInterface $request) { /** * Zone inquire */ $domInquire = $this->loadXml($this->getRequestGet()); $splittedHost = $this->split($request->getHost()); $name = $splittedHost['hostname'] . '.' . $splittedHost['tld']; $domInquire->getElementsByTagName('user')->item(0)->nodeValue = $this->user; $domInquire->getElementsByTagName('password')->item(0)->nodeValue = $this->password; $domInquire->getElementsByTagName('context')->item(0)->nodeValue = $this->context; $domInquire->getElementsByTagName('name')->item(0)->nodeValue = $name; $response = $this->requestCurl($domInquire->saveXML()); $domResponse = $this->loadXml($response); /** * Check if we can get zone records */ $xpath = new \DOMXPath($domResponse); $nodeList = $xpath->query('/response/result/status/type'); if (0 === $nodeList->length) { throw new \RuntimeException('Query path in response not found. Maybe the api changed?'); } if ('success' !== $nodeList->item(0)->nodeValue) { throw new \RuntimeException('Cannot get zone records. ' . $xpath->query('/response/result/status/text')->item(0)->nodeValue); } /** * Zone-Update */ $domUpdate = $this->loadXml($this->getRequestPut()); $domUpdateZone = $domResponse->getElementsByTagName('zone')->item(0); $domUpdateZone->removeChild($domUpdateZone->getElementsByTagName('created')->item(0)); $domUpdateZone->removeChild($domUpdateZone->getElementsByTagName('changed')->item(0)); $domUpdateZone->removeChild($domUpdateZone->getElementsByTagName('domainsafe')->item(0)); $domUpdateZone->removeChild($domUpdateZone->getElementsByTagName('owner')->item(0)); $domUpdateZone->removeChild($domUpdateZone->getElementsByTagName('updated_by')->item(0)); $domUpdate->getElementsByTagName('task')->item(0)->appendChild($domUpdate->importNode($domUpdateZone, true)); $domUpdate->getElementsByTagName('user')->item(0)->nodeValue = $this->user; $domUpdate->getElementsByTagName('password')->item(0)->nodeValue = $this->password; $domUpdate->getElementsByTagName('context')->item(0)->nodeValue = $this->context; /** * Populate Zone-Update-XML */ $subdomain = $splittedHost['subdomain']; if ($request->getIPv4()) { $xpath = new \DOMXPath($domUpdate); $query = "//task/zone/rr[name='" . $subdomain . "' and type='A']/value"; $entries = $xpath->query($query); if ($entries->length !== 1) { throw new \RuntimeException('Domain has no A-record for ' . $subdomain); } $entries->item(0)->nodeValue = $request->getIPv4(); } if ($request->getIpv6()) { $xpath = new \DOMXPath($domUpdate); $query = "//task/zone/rr[name='" . $subdomain . "' and type='AAAA']/value"; $entries = $xpath->query($query); if ($entries->length !== 1) { throw new \RuntimeException('Domain has no AAAA-record for ' . $subdomain); } $entries->item(0)->nodeValue = $request->getIpv6(); } /** * Update-Request */ $response = $this->requestCurl($domUpdate->saveXML()); $domResponse = $this->loadXml($response); $xpath = new \DOMXPath($domResponse); $nodeList = $xpath->query('/response/result/status/type'); if (0 === $nodeList->length) { throw new \RuntimeException('Query path in response not found. Maybe the api changed?'); } if ('success' === $nodeList->item(0)->nodeValue) { $response = new Response(true, $domResponse->saveXML()); return $response; } else { throw new \RuntimeException('Cannot set new zone records. ' . $xpath->query('/response/result/status/text')->item(0)->nodeValue); } }