forked from 77bx/alidns-api-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalidns.php
154 lines (144 loc) · 4.08 KB
/
alidns.php
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
<?php
/***
* Alidns-api-php V1.1
* By Star.Yu
***/
if($_SERVER['REQUEST_METHOD']=="POST"){
$request = $_POST;
}
if($_SERVER['REQUEST_METHOD']=="GET"){
$request = $_GET;
}
if(is_array($request)&&count($request)<1){
Header("Location: http://www.myxzy.com/alidns-api-php.html");
exit('2');
}
if(empty($request['id'])){
exit('2');
}elseif(empty($request['secret'])){
exit('2');
}elseif(empty($request['domain'])){
exit('2');
}elseif(empty($request['record'])){
exit('2');
}else{
$ip = empty($request['ip']) ? $_SERVER['REMOTE_ADDR'] : addslashes($request['ip']);
$accessKeyId = addslashes($request['id']);
$accessKeySecret = addslashes($request['secret']);
$record = addslashes($request['record']);
$domain = addslashes($request['domain']);
}
//公共参数Timestamp GMT时间
$Timestamp = gmdate('Y-m-d\TH:i:s\Z',time());
//Signature percentEncode函数
function percentEncode($str) {
$res = urlencode($str);
$res = str_replace(array('+', '*'), array('%20', '%2A'), $res);
$res = preg_replace('/%7E/', '~', $res);
return $res;
}
//唯一数,用于防止网络重放攻击
function generateByMicrotime() {
$microtime = microtime(true);
$microtime = str_replace('.', '', $microtime);
return $microtime;
}
//sign
function sign($parameters, $accessKeySecret){
ksort($parameters);
$canonicalizedQueryString = '';
foreach ($parameters as $key => $value) {
$canonicalizedQueryString .= '&' . percentEncode($key) . '=' . percentEncode($value);
}
$stringToBeSigned = 'POST&%2F&' . percentEncode(substr($canonicalizedQueryString, 1));
$signature = base64_encode(hash_hmac('sha1', $stringToBeSigned, $accessKeySecret. '&', true));
return $signature;
}
function geturl($public, $request, $accessKeySecret){
$params = array_merge($public, $request);
$params['Signature'] = sign($params, $accessKeySecret);
$uri = http_build_query($params);
$url = 'https://alidns.aliyuncs.com/?'.$uri;
return $url;
}
function ssl_post($url){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$tmpInfo = curl_exec($curl);
if (curl_errno($curl)) {
echo 'Errno'.curl_error($curl);
}
curl_close($curl);
return $tmpInfo;
}
$public = array(
'Format' => 'json',
'Version' => '2015-01-09',
'AccessKeyId' => $accessKeyId,
'SignatureMethod' => 'HMAC-SHA1',
'Timestamp' => $Timestamp,
'SignatureVersion' => '1.0',
'SignatureNonce' => generateByMicrotime()
);
$search = array(
'Action' => 'DescribeDomainRecords',
'DomainName' => $domain,
'PageSize' => '500',
'RRKeyWord' => $record,
'Type' => 'A'
);
//搜索record相关的记录列表
$data = json_decode(ssl_post(geturl($public,$search, $accessKeySecret)),true);
if(empty($data['DomainRecords'])){
exit('1');
}else{
foreach($data['DomainRecords']['Record'] as $value){
$record_arr = array();
if($value['RR'] == $record){
$record_id = $value['RecordId'];
$record_arr = $value;
break;
}
}
if(empty($record_id)){
$add = array(
'Action' => 'AddDomainRecord',
'DomainName' => $domain,
'RR' => $record,
'Type' => 'A',
'Value' => $ip,
'TTL' => '600',
);
$data = json_decode(ssl_post(geturl($public,$add, $accessKeySecret)),true);
if(empty($data['RecordId'])){
exit('1');
}else{
exit('0');
}
}else{
if($record_arr['Value'] == $ip){
exit('0');
}else{
$edit = array(
'Action' => 'UpdateDomainRecord',
'RecordId' => $record_id,
'RR' => $record,
'Type' => 'A',
'Value' => $ip,
'TTL' => '600',
);
$data = json_decode(ssl_post(geturl($public,$edit, $accessKeySecret)),true);
if(empty($data['RecordId'])){
exit('1');
}else{
exit('0');
}
}
}
}