/
client.php
339 lines (299 loc) · 11.3 KB
/
client.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
/*
* to.uri.st HTTP client
*
* The to.uri.st database has an Atom based HTTP interface. This client gives
* you interactive commandline access to the to.uri.st database via this Atom
* interface.
*
* This script requires SimpleXML.
*/
if (!extension_loaded('simplexml')) die('SimpleXML extension not found.');
define('VERBOSE', FALSE);
define('NS_ATOM', 'http://www.w3.org/2005/Atom');
define('NS_ATOM_REVISION', 'http://purl.org/atompub/revision/1.0');
define('NS_GEORSS', 'http://www.georss.org/georss');
define('NS_OPENSEARCH', 'http://a9.com/-/spec/opensearch/1.1/');
define('NS_OPENSEARCH_GEO', 'http://a9.com/-/opensearch/extensions/geo/1.0/');
if (!extension_loaded('curl')) die("CURL extension required\n");
if (!extension_loaded('simplexml')) die("SimpleXML extension required\n");
array_shift($argv);
if (count($argv) == 0) {
$url = new URL('http://to.uri.st/index.atom');
} else {
$url = new URL($argv[0]);
}
list($code, $document, $headers) = $url->exec();
if (substr($code, 0, 1) == '2' && $document) {
processDocument($url, $headers, $document);
} else {
echo "Response: ".$code."\n";
}
function processDocument($url, $headers, $document) {
list($mimetype) = explode(';', $headers['Content-Type']);
echo "Found ".$mimetype."\n";
switch($mimetype) {
case 'application/atomserv+xml':
$actions = parseIndexDocument($document, $url);
break;
case 'application/atom+xml':
if (strpos($document, '<feed') !== FALSE) { // feed
$actions = parseAtomFeed($document, $url, $headers);
} elseif (strpos($document, '<entry') !== FALSE) { // entry
$actions = parseAtomEntry($document, $url, $headers);
}
break;
default:
echo "Unknown format: ".$mimetype."\n";
echo substr($document, 0, 1000)."\n";
exit;
}
if (!$actions) {
echo "Nothing found to do\n";
exit;
}
echo "Available actions:\n";
$items = array_keys($actions);
$choice = doMenu($items);
$chosenUrl = $actions[$items[$choice - 1]];
list($code, $document, $headers) = $chosenUrl->exec();
if (substr($code, 0, 1) == '2') {
echo "Success: ".$code."\n";
if ($document) {
processDocument($chosenUrl, $headers, $document);
}
} else {
echo "Error: ".$code."\n";
}
}
function doMenu($items) {
$foo = 0;
foreach ($items as $name) {
$foo++;
echo " ".$foo.". ".$name."\n";
}
$action = (int)trim(fgets(STDIN));
if ($action > 0 && $action <= $foo) {
return $action;
}
echo "Bad choice, please choose an option:\n";
return doMenu($items);
}
function parseIndexDocument($document, $url) {
$actions = array();
$xml = new SimpleXMLElement($document);
foreach ($xml->workspace->collection as $collection) {
$attrs = $collection->attributes();
$atomChildren = $collection->children(NS_ATOM);
$actions[(string)$atomChildren->title] = new URL((string)$attrs['href'], $url, 'get');
}
return $actions;
}
function parseAtomFeed($document, $url, $headers) {
echo "It's an Atom feed\n";
$xml = new SimpleXMLElement($document);
$feedUrl = new URL($xml->link['href'], $url);
echo "Feed ID: ".$xml->id."\n";
echo "Feed URL: ".$feedUrl->url."\n";
$actions = array();
foreach ($xml->entry as $entry) {
$revision = $entry->children(NS_ATOM_REVISION);
if ($revision) {
$number = $revision->revision->attributes();
$version = ' version '.(string)$number['number'];
} else {
$version = '';
}
foreach ($entry->link as $link) {
if ($link['rel'] == 'self') {
$actions['GET entry'.$version.' "'.(string)$entry->title.'"'] = new URL((string)$link['href'], $url, 'get');
}
}
}
if (strpos($headers['Allow'], 'POST') !== FALSE) {
$actions['POST a new entry'] = new URL($url->url, $url, 'post', 'readEntryData');
}
return $actions;
}
function parseAtomEntry($document, $url, $headers) {
echo "It's an Atom entry\n";
$xml = new SimpleXMLElement($document);
foreach ($xml->link as $link) {
if ($link['rel'] == 'via') {
$linkUrl = new URL($link['href'], $url);
}
}
$georssChildren = $xml->children(NS_GEORSS);
echo "Entry ID: ".$xml->id."\n";
echo "Entry URL: ".$url->url."\n";
echo "Title: ".$xml->title."\n";
echo "Description: ".$xml->content."\n";
echo "Point: ".$georssChildren->point."\n";
echo "Published: ".$xml->published."\n";
echo "Updated: ".$xml->updated."\n";
echo "Author: ".$xml->author->name."\n";
if ($linkUrl) echo "Link: ".$linkUrl->url."\n";
$actions = array();
if (strpos($headers['Allow'], 'PUT') !== FALSE) {
$actions['PUT new version of this entry'] = new URL($entry->url, $url, 'put', 'readEntryData');
}
if (strpos($headers['Allow'], 'DELETE') !== FALSE) {
$actions['DELETE this entry'] = new URL($entry->url, $url, 'delete');
}
foreach ($xml->link as $link) {
if ($link['type'] == 'application/atom+xml') {
$linkUrl = new URL($link['href'], $url);
$actions['GET entry "'.$link['rel'].'"'] = new URL($linkUrl->url, $url, 'get');
}
}
return $actions;
}
function readEntryData() {
echo "Enter entry data to send:\n";
$atom = "<?xml version=\"1.0\" encoding=\"utf-8\"?>
<entry xmlns=\"http://www.w3.org/2005/Atom\" xmlns:georss=\"http://www.georss.org/georss\">";
echo "Title: ";
$title = trim(fgets(STDIN));
if ($title) $atom .= "<title>".$title."</title>";
echo "Description: ";
$description = trim(fgets(STDIN));
if ($description) $atom .= "<content type=\"html\">".$description."</content>";
echo "Latitude: ";
$lat = trim(fgets(STDIN));
echo "Longitude: ";
$lng = trim(fgets(STDIN));
if (is_numeric($lat) && is_numeric($lng)) $atom .= "<georss:point>".$lat." ".$lng."</georss:point>";
return $atom."</entry>";
}
class URL {
var $url, $method, $dataFunc;
function url($url, $parentUrl = NULL, $method = 'get', $dataFunc = NULL) {
$this->url = $this->makeAbsoluteUrl($url, $parentUrl);
$this->method = $method;
if (function_exists($dataFunc)) $this->dataFunc = $dataFunc;
}
function exec() {
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curl, CURLOPT_MAXREDIRS, 10);
curl_setopt($curl, CURLOPT_TIMEOUT, 160);
curl_setopt($curl, CURLOPT_USERAGENT, 'to.uri.st API client');
curl_setopt($curl, CURLOPT_VERBOSE, VERBOSE);
curl_setopt($curl, CURLOPT_HEADER, TRUE);
$headers = array(
'Accept-Language: en-gb,en;q=0.5',
'Accept: application/atom+xml'
);
switch (strtolower($this->method)) {
case 'post':
echo "POSTing to ".$this->url."\n";
curl_setopt($curl, CURLOPT_POST, TRUE);
if ($this->dataFunc) {
$dataFunc = $this->dataFunc;
curl_setopt($curl, CURLOPT_POSTFIELDS, $dataFunc());
}
$headers[] = 'Content-Type: application/atom+xml';
break;
case 'put':
echo "PUTting to ".$this->url."\n";
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
if ($this->dataFunc) {
$dataFunc = $this->dataFunc;
curl_setopt($curl, CURLOPT_POSTFIELDS, $dataFunc());
}
$headers[] = 'Content-type: application/atom+xml';
break;
case 'delete':
echo "DELETEing ".$this->url."\n";
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
break;
case 'head':
echo "HEADing to ".$this->url."\n";
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'HEAD');
break;
case 'options':
echo "OPTIONSing to ".$this->url."\n";
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'OPTIONS');
break;
default:
echo "GETting ".$this->url."\n";
}
curl_setopt($curl, CURLOPT_URL, $this->url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($curl);
$responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if (substr($responseCode, 0, 1) == '2') {
$parts = explode("\r\n\r\n", $response);
$headers = array();
foreach ($parts as $part) {
if (substr($part, 0, 4) == 'HTTP') {
$headerStrings = explode("\r\n", array_shift($parts));
foreach ($headerStrings as $headerString) {
$headerParts = explode(":", $headerString);
$headers[trim($headerParts[0])] = trim($headerParts[1]);
}
}
}
$content = join("\r\n\r\n", $parts);
if (isset($headers['Content-Encoding']) && $headers['Content-Encoding'] == 'gzip') {
$content = gzinflate(substr($content, 10));
}
} else {
$content = '';
$headers = array();
}
return array($responseCode, $content, $headers);
}
function makeAbsoluteUrl($url, $parentUrl = NULL) {
$url = html_entity_decode($url, ENT_NOQUOTES);
if ($parentUrl) {
$parentUrlParts = parse_url($parentUrl->url);
} else {
$parentUrlParts = array();
}
$urlParts = parse_url($url);
if (isset($urlParts['host']) && !isset($urlParts['path']) && !preg_match('/^[a-z]+:\/\//', $url)) { // not fully qualified
// parse_url might have decided that the path is the host, so fix that
$urlParts['path'] = $urlParts['host'];
unset($urlParts['host']);
}
if (isset($urlParts['scheme']) && $urlParts['scheme']) {
return str_replace(' ', '%20', $url);
} else {
$url = $parentUrlParts['scheme'];
}
$url .= '://';
if (isset($urlParts['host']) && $urlParts['host']) {
$url .= $urlParts['host'];
} else {
$url .= $parentUrlParts['host'];
}
if (isset($urlParts['path']) && $urlParts['path']) {
if (substr($urlParts['path'], 0, 1) != '/') {
$path = dirname($parentUrlParts['path']);
if ($path != '.' && $path != '/' && $path != '\\') {
while (substr($urlParts['path'], 0, 3) == '../') { // sort out ..'s in path
$urlParts['path'] = substr($urlParts['path'], 3);
$path = dirname($path);
}
if ($path == '/') {
$path = '';
}
$url .= $path.'/';
} else {
$url .= '/';
}
}
$url .= $urlParts['path'];
} else {
$url .= $parentUrlParts['path'];
}
if (isset($urlParts['query']) && $urlParts['query']) {
$url .= '?'.$urlParts['query'];
}
return str_replace(' ', '%20', $url);
}
}
?>