-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathatlantic-api.php
105 lines (88 loc) · 2.71 KB
/
atlantic-api.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
<?php
////
// atlantic.net PHP API class (examples)
// https://www.atlantic.net/community/howto/api-documentation/
//
// couple pieces taken from this basic example:
// https://www.atlantic.net/community/howto/atlantic-net-api-php-example/
////
class Atlantic {
var $format = "json"; // only works with json ;)
var $apiurl = "https://cloudapi.atlantic.net/api.php";
var $datef = array("RFC2616" => 'D, d M Y H:i:s \G\M\T',
"ISO8601" => 'Y-m-d\TH:i:s\Z',
"MYSQL" => 'Y-m-d H:i:s');
var $query;
var $res;
// methods available
var $methods = "describe-plan,describe-image,list-instances,run-instance,describe-instance,reboot-instance,terminate-instance";
//
function Atlantic() {
if (!defined("ATL_API_KEY") or
!defined("ATL_API_PKEY") or
!defined("ATL_API_VER")) {
die("Missing global vars\n");
}
}
//
function call_method($method="") {
$tmp = explode(",", $this->methods);
if (!in_array($method, $tmp)) return false;
$this->start_engine($method);
return true;
}
//
function output($print=false) {
if ($this->format == "json") $this->res = json_decode($this->res,true);
if (!$print) return $this->res;
echo $this->res;
}
// core method
function start_engine($method) {
$now = time();
$today = gmdate($this->datef["RFC2616"], $now);
$ts = gmdate($this->datef["ISO8601"], $now);
$ruid = $this->generate_uid();
$this->build_query(array("ACSAccessKeyId" => ATL_API_KEY,
"Action" => $method,
"Version" => ATL_API_VER,
"Format" => $this->format,
"Timestamp" => $now+21,
"Rndguid" => $ruid));
$sts = $this->query["Timestamp"] . $this->query["Rndguid"];
//
$this->build_query(array("Signature" => base64_encode(hash_hmac('sha256', $sts, ATL_API_PKEY, true))));
// convert this array into string
$querystring = $this->atqs($this->query);
// debug
//echo "\n\n*** QUERY STRING: $querystring\n\n".print_r($args,true);
$ch = curl_init($this->apiurl . '?' . $querystring);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$this->res = curl_exec($ch);
curl_close($ch);
}
//
function build_query($array) {
if (is_array($array)) {
foreach ($array as $k => $v) $this->query[$k] = $v;
}
}
//
function generate_uid() {
return sprintf(
'%04X%04X%04X%04X%04X%04X%04X%04X%04X',
mt_rand(0, 65535), mt_rand(16384, 20479),
mt_rand(0, 65535), mt_rand(16384, 20479),
mt_rand(32768, 49151), mt_rand(0, 65535),
mt_rand(0, 65535), mt_rand(0, 65535),
mt_rand(32768, 49151));
}
//
function atqs($array) {
$temp = array();
foreach ($array as $key => $value) {
$temp[] = rawurlencode($key) . '=' . rawurlencode($value);
}
return implode('&', $temp);
}
}