-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.php
More file actions
162 lines (116 loc) · 4.1 KB
/
index.php
File metadata and controls
162 lines (116 loc) · 4.1 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
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
<?php
require __DIR__.'/vendor/phpish/app/app.php';
require __DIR__.'/vendor/phpish/template/template.php';
require __DIR__.'/vendor/phpish/http/http.php';
use phpish\app;
use phpish\template;
use phpish\http;
define('ENDPOINT_HOST', 'pingback.converspace.com');
app\get('/', function() {
return template\render('index.html', array('endpoint_host'=>ENDPOINT_HOST));
});
app\post('/', function ($req) {
$actor = $req['form']['actor'];
$activityid = $req['form']['activityid'];
$object = $req['form']['object'];
$result = array();
$result['actor']['url'] = $actor;
$result['activityid'] = $activityid;
$result['object']['url'] = $object;
$endpoint = activity_pingback_discover_endpoint($object, $result['object']);
$result['object']['endpoint'] = $endpoint;
if (!preg_match('#^(http://)?'.ENDPOINT_HOST.'/?$#', $endpoint))
{
activity_pingback_notify($endpoint, $actor, $activityid, $object);
return "Activity Pingback sent.";
}
else
{
if (activity_pingback_get_and_validate_activity($actor, $activityid, $object, $result))
{
return template\render('test.html', compact('result'));
}
}
return app\response_500('There was an error');
});
function activity_pingback_discover_endpoint($resource, &$result)
{
//TODO: try/catch
$response = http\request("GET $resource", '', '', array(), $response_headers);
$result['response'] = $response;
$result['response_headers'] = $response_headers;
$link_header_regex = '#<([^"]+)>; rel="http://activitypingback.org/"#';
$link_header = isset($response_headers['link']) ? $response_headers['link'] : NULL;
if (preg_match($link_header_regex, $link_header, $matches))
{
$result['link_header'] = $matches[0];
return $matches[1];
}
$link_element_regex = '#<link href="([^"]+)" rel="http://activitypingback.org/" ?/?/>#';
if (preg_match($link_element_regex, $response, $matches))
{
$result['link_element'] = $matches[0];
return $matches[1];
}
return false;
}
function activity_pingback_notify($endpoint, $actor, $activityid, $object)
{
//TODO: try/catch
//TODO: Check for 201 response
http\request("POST $endpoint", '', compact('actor', 'activityid', 'object'));
}
function activity_pingback_get_and_validate_activity($actor, $activityid, $object, &$result)
{
//TODO: try/catch
$endpoint = activity_pingback_discover_endpoint($actor, $result['actor']);
$result['actor']['endpoint'] = $endpoint;
if (!preg_match('#^(http://)?'.ENDPOINT_HOST.'/?$#', $endpoint))
{
$response = http\request("GET $endpoint", compact('actor', 'activityid', 'object'));
$result['activity']['json'] = $response;
$activity = json_decode($response, true);
$result['activity']['php'] = $activity;
return (($activity['actor']['url'] == $actor) and
($activity['id'] == $activityid) and
($activity['object']['url'] == $object)) ? $activity : false;
}
return false;
}
app\get('/test/alice', function() {
return app\response
(
template\render('test-alice.html', array('endpoint_host'=>ENDPOINT_HOST)),
200,
array('Link'=>'<http://'.ENDPOINT_HOST.'/test/alice/endpoint>; rel="http://activitypingback.org/"')
);
});
app\get('/test/bob/post', function() {
return app\response
(
template\render('test-bobs-post.html', array('endpoint_host'=>ENDPOINT_HOST)),
200,
array('Link'=>'<http://'.ENDPOINT_HOST.'/>; rel="http://activitypingback.org/"')
);
});
app\query('/test/alice/endpoint', function ($req) {
if (preg_match('#^(http://)?'.ENDPOINT_HOST.'/test/alice/?$#', $req['query']['actor']) and
preg_match('#^(http://)?'.ENDPOINT_HOST.'/test/alice/activity/?$#', $req['query']['activityid']) and
preg_match('#^(http://)?'.ENDPOINT_HOST.'/test/bob/post/?$#', $req['query']['object']))
{
return test_activity();
}
});
app\get('/test/alice/activity', function ($req) {
return test_activity();
});
function test_activity()
{
return app\response
(
template\render('test-alice-activity.json', array('endpoint_host'=>ENDPOINT_HOST)),
200,
array('Content-Type'=>'application/stream+json')
);
}
?>