Handler.php (5960B)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 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 73 74 75 76 77 78 79 80 81 82 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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | <?php namespace netcup\DNS\API; use RuntimeException; final class Handler { /** * @var array */ private $log; /** * @var Config */ private $config; /** * @var Payload */ private $payload; public function __construct(array $config, array $payload) { $this->config = new Config($config); if (!$this->config->isValid()) { throw new RuntimeException('configuration invalid'); } $this->payload = new Payload($payload); if (!$this->payload->isValid()) { throw new RuntimeException('payload invalid'); } if ( $this->config->getUsername() !== $this->payload->getUser() || $this->config->getPassword() !== $this->payload->getPassword() ) { throw new RuntimeException('credentials wrong'); } if (is_readable($this->config->getLogFile())) { $this->log = json_decode(file_get_contents($this->config->getLogFile()), true); } else { $this->log[$this->payload->getDomain()] = []; } } public function __destruct() { $this->doExit(); } /** * @param string $msg * * @return self */ private function doLog($msg) { $this->log[$this->payload->getDomain()][] = sprintf('[%s] %s', date('c'), $msg); if ($this->config->isDebug()) { printf('[DEBUG] %s %s', $msg, PHP_EOL); } return $this; } private function doExit() { if (!$this->config->isLog()) { return; } if (!file_exists($this->config->getLogFile())) { if (!touch($this->config->getLogFile())) { printf('[ERROR] unable to create %s %s', $this->config->getLogFile(), PHP_EOL); } } // save only the newest 100 log entries for each domain $this->log[$this->payload->getDomain()] = array_reverse(array_slice(array_reverse($this->log[$this->payload->getDomain()]), 0, 100)); if (!is_writable($this->config->getLogFile()) || !file_put_contents($this->config->getLogFile(), json_encode($this->log, JSON_PRETTY_PRINT))) { printf('[ERROR] unable to write %s %s', $this->config->getLogFile(), PHP_EOL); } } /** * * @return self */ public function doRun() { $clientRequestId = md5($this->payload->getDomain() . time()); $dnsClient = new Soap\DomainWebserviceSoapClient(); $loginHandle = $dnsClient->login( $this->config->getCustomerId(), $this->config->getApiKey(), $this->config->getApiPassword(), $clientRequestId ); if (2000 === $loginHandle->statuscode) { $this->doLog('api login successful'); } else { $this->doLog(sprintf('api login failed, message: %s', $loginHandle->longmessage)); } $infoHandle = $dnsClient->infoDnsRecords( $this->payload->getHostname(), $this->config->getCustomerId(), $this->config->getApiKey(), $loginHandle->responsedata->apisessionid, $clientRequestId ); $changes = false; foreach ($infoHandle->responsedata->dnsrecords as $key => $record) { $recordHostnameReal = (!in_array($record->hostname, $this->payload->getMatcher())) ? $record->hostname . '.' . $this->payload->getHostname() : $this->payload->getHostname(); if ($recordHostnameReal === $this->payload->getDomain()) { // update A Record if exists and IP has changed if ('A' === $record->type && $this->payload->getIpv4() && ( $this->payload->isForce() || $record->destination !== $this->payload->getIpv4() ) ) { $record->destination = $this->payload->getIpv4(); $this->doLog(sprintf('IPv4 for %s set to %s', $record->hostname . '.' . $this->payload->getHostname(), $this->payload->getIpv4())); $changes = true; } // update AAAA Record if exists and IP has changed if ('AAAA' === $record->type && $this->payload->getIpv6() && ( $this->payload->isForce() || $record->destination !== $this->payload->getIpv6() ) ) { $record->destination = $this->payload->getIpv6(); $this->doLog(sprintf('IPv6 for %s set to %s', $record->hostname . '.' . $this->payload->getHostname(), $this->payload->getIpv6())); $changes = true; } } } if (true === $changes) { $recordSet = new Soap\Dnsrecordset(); $recordSet->dnsrecords = $infoHandle->responsedata->dnsrecords; $dnsClient->updateDnsRecords( $this->payload->getHostname(), $this->config->getCustomerId(), $this->config->getApiKey(), $loginHandle->responsedata->apisessionid, $clientRequestId, $recordSet ); $this->doLog('dns recordset updated'); } else { $this->doLog('dns recordset NOT updated (no changes)'); } $logoutHandle = $dnsClient->logout( $this->config->getCustomerId(), $this->config->getApiKey(), $loginHandle->responsedata->apisessionid, $clientRequestId ); if (2000 === $logoutHandle->statuscode) { $this->doLog('api logout successful'); } else { $this->doLog(sprintf('api logout failed, message: %s', $loginHandle->longmessage)); } return $this; } } |