-
Notifications
You must be signed in to change notification settings - Fork 0
/
xnet.php
118 lines (97 loc) · 3.05 KB
/
xnet.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
<?php
// For all the fetchin'
require_once 'HTTP/Request.php';
require_once 'XML/Serializer.php';
require_once 'XML/Unserializer.php';
class Xnet {
var $user;
var $pass;
var $base;
function Xnet($user, $pass, $base) {
$this->user = $user;
$this->pass = $pass;
$this->base = $base;
}
/*======================/
* General
/*=====================*/
// Vraci seznam domen, ktere muzete administrovat pod timto uctem
function domains() {
return $this->hook("/person/domains","domain");
}
// Vraci informace o domene
function domain($fqdn) {
$fqdn = preg_replace('/\./', '_', $fqdn);
return $this->hook("/domains/{$fqdn}","domain");
}
// Vraci seznam subjektu, ktere muzete administrovat pod timto uctem
function subjects() {
return $this->hook("/person/subjects","subject");
}
// Vraci seznam objednavek, ktere jsou vystaveny pro dany subjekt
function subject_orders($subject_id) {
return $this->hook("/subjects/{$subject_id}/orders","order");
}
// Vraci seznam domen, ktere jsou zarazeny pod dany subjekt
function subject_domains($subject_id) {
return $this->hook("/subjects/{$subject_id}/domains","domain");
}
/*===================/
* The Worker Bees
/*===================*/
function hook($url,$expected,$send = false) {
$returned = $this->unserialize($this->request($url,$send));
$placement = $expected;
if (isset($returned->{$expected})) {
$this->{$placement} = $returned->{$expected};
return $returned->{$expected};
} else {
$this->{$placement} = $returned;
return $returned;
}
}
function request($url, $params = false) {
//do the connect to a server thing
$req =& new HTTP_Request($this->base . $url);
//authorize
$req->setBasicAuth($this->user, $this->pass);
//set the headers
$req->addHeader("Accept", "application/xml");
$req->addHeader("Content-Type", "application/xml");
//if were sending stuff
if ($params) {
//serialize the data
$xml = $this->serialize($params);
//print_r($xml);
($xml)?$req->setBody($xml):false;
$req->setMethod(HTTP_REQUEST_METHOD_POST);
}
$response = $req->sendRequest();
// print_r($req->getResponseHeader());
// echo $req->getResponseCode() . "\n";
if (PEAR::isError($response)) {
return $response->getMessage();
} else {
// print_r($req->getResponseBody());
return $req->getResponseBody();
}
}
function serialize($data) {
$options = array( XML_SERIALIZER_OPTION_MODE => XML_SERIALIZER_MODE_SIMPLEXML,
XML_SERIALIZER_OPTION_ROOT_NAME => 'request',
XML_SERIALIZER_OPTION_INDENT => ' ');
$serializer = new XML_Serializer($options);
$result = $serializer->serialize($data);
return ($result)?$serializer->getSerializedData():false;
}
function unserialize($xml) {
$options = array (XML_UNSERIALIZER_OPTION_COMPLEXTYPE => 'object');
$unserializer = &new XML_Unserializer($options);
$status = $unserializer->unserialize($xml);
$data = (PEAR::isError($status))?$status->getMessage():$unserializer->getUnserializedData();
return $data;
}
}
// match comments
// \/\/ .+
?>