forked from ripple-unmaintained/federation-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
86 lines (66 loc) · 1.97 KB
/
index.php
File metadata and controls
86 lines (66 loc) · 1.97 KB
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
<?php
function run()
{
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
$data = json_decode(file_get_contents(dirname(__FILE__) . '/private/data.json'));
if (!$data) {
send_error('unavailable', 'The endpoint is unable to read its configuration file.');
}
$result = array();
if (!isset($_GET['domain']) || !strlen($_GET['domain'])) {
send_error('invalidParams', 'No domain provided.');
}
$domain = strtolower($_GET['domain']);
if (!isset($data->{$domain})) {
send_error('noSuchDomain', 'The supplied domain is not served here.');
}
$users = $data->{$domain};
$visited = array();
while (is_string($users)) {
if (!isset($data->{$users})) {
send_error('unavailable', 'Misconfigured domain alias.');
}
if (isset($visited[$users])) {
send_error('unavailable', 'Circular domain alias.');
} else {
$visited[$users] = true;
}
$users = $data->{$users};
}
if (isset($_GET['destination']) && strlen($_GET['destination'])) {
$user = strtolower($_GET['destination']);
} elseif (isset($_GET['user']) && strlen($_GET['user'])) {
// DEPRECATED
$user = strtolower($_GET['user']);
} else {
send_error('invalidParams', 'No username provided.');
}
if (!isset($users->{$user})) {
send_error('noSuchUser', 'The supplied user was not found.');
}
$result['federation_json'] = array(
'type' => 'federation_record',
'destination' => $destination,
'user' => $user, // DEPRECATED
'destination_address' => $users->{$user},
'domain' => $domain
);
send_result($result);
}
function send_error($errCode, $errMsg) {
$result = array();
$result['result'] = 'error';
$result['error'] = $errCode;
$result['error_message'] = $errMsg;
send_result($result);
}
function send_result($result) {
if (defined('JSON_PRETTY_PRINT')) {
echo json_encode($result, JSON_PRETTY_PRINT);
} else {
echo json_encode($result);
}
exit;
}
run();