1
+ <?php
2
+
3
+ class DynamicDNS_Receptor {
4
+
5
+ private $ datastore ;
6
+
7
+ public function __construct () {
8
+ $ this ->datastore = new DynamicDNS_DataStore ();
9
+ }
10
+
11
+ private function is_request_valid () {
12
+ $ hostname = isset ($ _GET ['hostname ' ]) && !empty ($ _GET ['hostname ' ]);
13
+ $ token = isset ($ _GET ['token ' ]) && !empty ($ _GET ['token ' ]);
14
+ return $ hostname && $ token ;
15
+ }
16
+
17
+ private function verify_hostname () {
18
+ $ hostname = $ _GET ['hostname ' ];
19
+ $ config = isset ( $ this ->datastore ->config ->hosts ->{$ hostname } );
20
+ if ( $ config ) {
21
+ if ( isset ($ this ->datastore ->data ->hosts ->{$ hostname }) == false ) {
22
+ $ obj = new stdClass ();
23
+ $ obj ->{'last_update ' } = 0 ;
24
+ $ obj ->{'updates ' } = 0 ;
25
+ $ obj ->{'ip ' } = '' ;
26
+ $ this ->datastore ->data ->hosts ->{$ hostname } = $ obj ;
27
+ }
28
+ return true ;
29
+ }
30
+ return false ;
31
+ }
32
+
33
+ private function verify_token () {
34
+ $ token = $ _GET ['token ' ];
35
+ if ( $ token == $ this ->datastore ->config ->token ) {
36
+ return true ;
37
+ }
38
+ return false ;
39
+ }
40
+
41
+ private function verify_change ($ hostname ) {
42
+ $ client_ip = $ _SERVER ['REMOTE_ADDR ' ];
43
+ // has not changed
44
+ if ( $ this ->datastore ->data ->hosts ->{$ hostname }->{'ip ' } == $ client_ip ) {
45
+ return false ;
46
+ }
47
+
48
+ // has changed
49
+ return true ;
50
+ }
51
+
52
+
53
+ public function refresh () {
54
+
55
+ if ( !$ this ->is_request_valid () ) {
56
+ Helper::feedback (array ("code " => 400 , "status " => "invalid request " ));
57
+ return false ;
58
+ }
59
+ if ( !$ this ->verify_token () ) {
60
+ Helper::feedback (array ("code " => 401 , "status " => "invalid token " ));
61
+ return false ;
62
+ }
63
+ if ( !$ this ->verify_hostname () ) {
64
+ Helper::feedback (array ("code " => 401 , "status " => "invalid hostname " ));
65
+ return false ;
66
+ }
67
+
68
+ $ hostname = $ _GET ['hostname ' ];
69
+
70
+ $ config = $ this ->datastore ->config ->hosts ->{$ hostname };
71
+ $ data = $ this ->datastore ->data ->hosts ->{$ hostname };
72
+
73
+ if ( $ this ->verify_change ($ hostname ) == false ) {
74
+ Helper::feedback (array ("code " => 202 , "status " => "host not changed " ));
75
+ return false ;
76
+ }
77
+
78
+ $ result = $ this ->update ($ hostname );
79
+ if ( $ result ) {
80
+ Helper::feedback (array ("code " => 201 , "status " => "dynamic dns successful " ));
81
+ } else {
82
+ Helper::feedback (array ("code " => 500 , "status " => "dynamic dns failed " ));
83
+ }
84
+
85
+ }
86
+
87
+ private function update ($ hostname ) {
88
+
89
+ $ ip = $ _SERVER ['REMOTE_ADDR ' ];
90
+
91
+ $ url = sprintf ("%sapi_key=%s&api_action=domain.resource.update&DomainID=%s&ResourceID=%s&Target=%s " , LINODE_API , $ this ->datastore ->config ->api_key , $ this ->datastore ->config ->hosts ->{$ hostname }->{'domain_id ' }, $ this ->datastore ->config ->hosts ->{$ hostname }->{'resource_id ' }, $ ip );
92
+
93
+ $ response = file_get_contents ($ url );
94
+ $ response_json = json_decode ($ response );
95
+
96
+ if ( count ($ response_json ->{'ERRORARRAY ' }) == 0 ) {
97
+ $ this ->datastore ->data ->hosts ->{$ hostname }->{'ip ' } = $ ip ;
98
+ $ this ->datastore ->data ->hosts ->{$ hostname }->{'updates ' } = $ this ->datastore ->data ->hosts ->{$ hostname }->{'updates ' } + 1 ;
99
+ $ this ->datastore ->data ->hosts ->{$ hostname }->{'last_update ' } = time ();
100
+ $ this ->datastore ->save ();
101
+ return true ;
102
+ }
103
+
104
+ return false ;
105
+
106
+ }
107
+
108
+
109
+ }
0 commit comments