Skip to content

Commit cccb8f2

Browse files
committed
Initial commit
1 parent 59af9f4 commit cccb8f2

21 files changed

Lines changed: 846 additions & 0 deletions

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.DS_Store
2+
*.sublime-*
3+
vendor/

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# DNS-O-Matic-PHP
2+
3+
This is a simple client for [DNS-O-Matic](https://www.dnsomatic.com/).
4+
5+
## What is DNS-O-Matic?
6+
7+
### For Users
8+
DNS-O-Matic provides you a free and easy way to announce your dynamic IP changes to multiple services with a single update. Using DNS-O-Matic allows you to pick and choose what Dynamic DNS services you want to notify, all from one easy to use interface.
9+
10+
### For Developers
11+
DNS-O-Matic provides a scalable and standardized solution for developers to easily provide support for all dynamic DNS / IP services in their software or platform with one simple and consistent API at no cost.
12+
13+
### For Dynamic DNS Providers
14+
DNS-O-Matic will support dynamic DNS services without any work on your side. As more software clients and hardware vendors adopt the DNS-O-Matic API, the reach and ease of adoption for your service expands automatically.
15+
16+
## Examples
17+
18+
There are a few simple example scripts that can be used to get started with this library.
19+
20+
- [checker.php](example/checker.php): IP Resolution example;
21+
- [cron.php](example/cron.php): Full DNS-O-Matic cronjob script that updates a single hostname;
22+
- [updater.php](example/updater.php): DNS-O-Matic update example.
23+
24+
## Requirements
25+
26+
- PHP7.1+
27+
- [Guzzle](https://github.com/guzzle/guzzle)
28+
29+
## Installing
30+
31+
The recommended way to install this library is through [Composer](http://getcomposer.org/).
32+
33+
```bash
34+
# Install composer
35+
curl -sS https://getcomposer.org/installer | php
36+
```
37+
38+
Next, run the Composer command to install the latest stable version:
39+
40+
```bash
41+
php composer.phar require flavioheleno/dnsomatic-php
42+
```
43+
44+
After installing, you need to require Composer's autoloader:
45+
46+
```php
47+
require 'vendor/autoload.php`;
48+
```
49+
50+
## License
51+
52+
This library is released under the MIT license. See [LICENSE](LICENSE) for details.

composer.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "flavioheleno/dnsomatic-php",
3+
"description": "DNS-O-Matic PHP Client",
4+
"license": "mit",
5+
"type": "library",
6+
"keywords": ["dynamic dns", "ddns", "dns", "dns-o-matic", "opendns"],
7+
"authors": [
8+
{
9+
"name": "Flavio Heleno",
10+
"email": "flaviohbatista@gmail.com"
11+
}
12+
],
13+
"autoload": {
14+
"psr-4": {
15+
"dnsomatic\\": "src/"
16+
}
17+
},
18+
"autoload-dev": {
19+
"psr-4": {
20+
"Test\\": "test/"
21+
}
22+
},
23+
"require": {
24+
"php": ">=7.1",
25+
"guzzlehttp/guzzle": "^6.2"
26+
}
27+
}

example/checker.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../vendor/autoload.php';
4+
5+
use dnsomatic\Factory;
6+
7+
$checker = Factory::createChecker();
8+
var_dump($checker->exec());

example/cron.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../vendor/autoload.php';
4+
5+
use dnsomatic\Factory;
6+
7+
// DNS-O-Matic Username
8+
define('DNSOMATIC_USER', '');
9+
// DNS-O-Matic Password
10+
define('DNSOMATIC_PASS', '');
11+
// DNS-O-Matic Hostname
12+
define('DNSOMATIC_HOST', '');
13+
// Output messages to stdout
14+
define('VERBOSE', true);
15+
// IP Address local cache
16+
define('IP_CACHE', __DIR__ . '/ip.cache');
17+
// Log file
18+
define('LOG_FILE', __DIR__ . '/dnsomatic.log');
19+
20+
21+
try {
22+
if (VERBOSE) {
23+
echo 'Starting..', PHP_EOL;
24+
}
25+
26+
$checker = Factory::createChecker();
27+
// Resolves current IP Address
28+
$ipAddr = $checker->exec();
29+
if (VERBOSE) {
30+
echo 'Resolved IP Address: ', $ipAddr, PHP_EOL;
31+
}
32+
33+
// Retrieves last IP Address from local cache
34+
$cached = '';
35+
if (is_file(IP_CACHE)) {
36+
$cached = trim(file_get_contents(IP_CACHE));
37+
if (VERBOSE) {
38+
echo 'Cached IP Address: ', $cached, PHP_EOL;
39+
}
40+
}
41+
42+
// If resolved IP Address and cached IP Address don't match
43+
if ($ipAddr !== $cached) {
44+
if (VERBOSE) {
45+
echo 'Updating DNS-O-Matic..', PHP_EOL;
46+
}
47+
48+
// Update DNS-O-Matic with current IP Address
49+
$updater = Factory::createUpdater(DNSOMATIC_USER, DNSOMATIC_PASS);
50+
$updater
51+
->setHostname(DNSOMATIC_HOST)
52+
->setMyip($ipAddr);
53+
$updater->exec();
54+
55+
// Stores current IP Address in local cache
56+
file_put_contents(IP_CACHE, $ipAddr, LOCK_EX);
57+
}
58+
} catch (\Exception $exception) {
59+
// Exceptions are sent to the log file
60+
$logLine = sprintf(
61+
'[%s] %s (%s:%d)',
62+
date('d/m/Y H:i:s'),
63+
$exception->getMessage(),
64+
$exception->getFile(),
65+
$exception->getLine()
66+
);
67+
if (VERBOSE) {
68+
echo $logLine, PHP_EOL;
69+
}
70+
71+
file_put_contents(
72+
LOG_FILE,
73+
$logLine,
74+
FILE_APPEND | LOCK_EX
75+
);
76+
} finally {
77+
if (VERBOSE) {
78+
echo 'Done', PHP_EOL;
79+
}
80+
}

example/updater.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../vendor/autoload.php';
4+
5+
use dnsomatic\Factory;
6+
7+
$updater = Factory::createUpdater('username', 'password');
8+
$updater
9+
->setHostname('sub.example.com')
10+
->setMyip('192.168.0.10');
11+
var_dump($updater->exec());

src/Checker.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
namespace dnsomatic;
4+
5+
use \GuzzleHttp\Client;
6+
7+
/*
8+
* DNS-O-Matic IP Address resolution client.
9+
*/
10+
class Checker {
11+
/**
12+
* DNS-O-Matic IP Address resolution service host.
13+
*/
14+
const SERVICE_HOST = 'myip.dnsomatic.com';
15+
16+
/**
17+
* User Agent identification.
18+
*
19+
* @var string
20+
*/
21+
private $userAgent = 'DNS-O-Matic-PHP/0.1 (checker)';
22+
23+
/**
24+
* Returns the resolution url.
25+
*
26+
* @return string
27+
*/
28+
private function getServiceUrl() : string {
29+
return sprintf('http://%s/', self::SERVICE_HOST);
30+
}
31+
32+
/**
33+
* Class constructor.
34+
*
35+
* @param \GuzzleHttp\Client $client
36+
*/
37+
public function __construct(Client $client) {
38+
$this->client = $client;
39+
}
40+
41+
/**
42+
* Gets the User Agent.
43+
*
44+
* @return string
45+
*/
46+
public function getUserAgent() : string {
47+
return $this->userAgent;
48+
}
49+
50+
/**
51+
* Sets the User Agent.
52+
*
53+
* @param string $userAgent
54+
*
55+
* @return self
56+
*/
57+
public function setUserAgent(string $userAgent) : self {
58+
$this->userAgent = $userAgent;
59+
}
60+
61+
/**
62+
* Executes DNS-O-Matic IP Address resolution.
63+
*
64+
* @throws \dnsomatic\Exception\InvalidIPAddress
65+
* @throws \GuzzleHttp\Exception\RequestException
66+
*
67+
* @return string
68+
*/
69+
public function exec() : string {
70+
$response = $this->client->request(
71+
'GET',
72+
$this->getServiceUrl(),
73+
[
74+
'headers' => [
75+
'User-Agent' => $this->userAgent
76+
]
77+
]
78+
);
79+
80+
$body = (string) $response->getBody();
81+
if (! filter_var($body, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4 | \FILTER_FLAG_IPV6)) {
82+
throw new Exception\InvalidIPAddress();
83+
}
84+
85+
return $body;
86+
}
87+
}

src/Exception/Abuse.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace dnsomatic\Exception;
4+
5+
class Abuse extends \Exception {
6+
protected $message = 'The hostname is blocked for update abuse.';
7+
}

src/Exception/BadAgent.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace dnsomatic\Exception;
4+
5+
class BadAgent extends \Exception {
6+
protected $message = 'The user-agent is blocked.';
7+
}

src/Exception/BadAuth.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace dnsomatic\Exception;
4+
5+
class BadAuth extends \Exception {
6+
protected $message = 'The DNS-O-Matic username or password specified are incorrect.';
7+
}

0 commit comments

Comments
 (0)