forked from smob/smob
/
index.php
252 lines (237 loc) · 10.6 KB
/
index.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
<?php
parse_str($_SERVER['QUERY_STRING']);
require_once(dirname(__FILE__).'/lib/smob/SMOB.php');
if(!SMOBTools::check_config()) {
$installer = new SMOBInstaller();
$installer->go();
} else {
require_once(dirname(__FILE__)."/config/config.php");
// Follower / followings
if($a && $a == 'add') {
$u = str_replace('http:/', 'http://', $u);
// Add a new follower
if($t == 'follower') {
// @TODO: has it sense that the user add a follower?. Then the follower should also be notified to add the current user as following
// When the request comes from another user adding a following, the action is ran as there authentication is not needed
$remote_user = SMOBTools::remote_user($u);
if(!$remote_user) die();
$local_user = SMOBTools::user_uri();
$follow = "<$remote_user> sioc:follows <$local_user> . ";
$local = "INSERT INTO <".SMOB_ROOT."data/followers> { $follow }";
error_log("DEBUG: Added follower $remote_user with the query $local", 0);
SMOBStore::query($local);
}
// Add a new following
elseif($t == 'following') {
if(!SMOBAuth::check()) die();
$remote_user = SMOBTools::remote_user($u);
if(!$remote_user) {
SMOBTemplate::header('');
print "<a href='$u'>$u</a> is not a valid Hub, user cannot be added";
SMOBTemplate::footer();
} else {
// @TODO: check that the user were not already a following?
// Store the new relationship in local repository
$local_user = SMOBTools::user_uri();
$follow = "<$local_user> sioc:follows <$remote_user> . ";
$local = "INSERT INTO <".SMOB_ROOT."data/followings> { $follow }";
SMOBStore::query($local);
error_log("DEBUG: Added following $remote_user with the query: $local",0);
SMOBTemplate::header('');
// Subscribe to the hub
// Get the Publisher (following) Hub
$remote_user_feed = $remote_user.FEED_URL_PATH;
$xml = simplexml_load_file($remote_user_feed);
if(count($xml) == 0)
return;
$link_attributes = $xml->channel->link->attributes();
if($link_attributes['rel'] == 'hub') {
$hub_url = $link_attributes['href'];
}
$callback_url = urlencode(SMOB_ROOT."callback");
$feed = urlencode($remote_user_feed);
// Not using subscriber library as it does not allow async verify
// Reusing do_curl function
$result = SMOBTools::do_curl($hub_url, $postfields = "hub.mode=subscribe&hub.verify=async&hub.callback=$callback_url&hub.topic=$feed");
// all good -- anything in the 200 range
if (substr($result[2],0,1) == "2") {
error_log("DEBUG: Successfully subscribed to topic $remote_user_feed using hubsub $hub_url",0);
}
error_log("DEBUG: Server answer: ".join(' ', $result),0);
print "<a href='$remote_user'>$remote_user</a> was added to your following list and was notified about your subscription";
SMOBTemplate::footer();
// And ping to update the followers list remotely
// @TODO: This will work only if $u doesn't have /me or something in the end
//$ping = str_replace("me", "add", $ping)."/follower/$local_user";
$ping = SMOBTools::host($remote_user)."/add/follower/$local_user";
$result = SMOBTools::do_curl($ping);
error_log("DEBUG: Sent $ping",0);
error_log("DEBUG: Server answer: ".join(' ', $result),0);
}
}
}
elseif($a && $a == 'remove') {
//
if(!SMOBAuth::check()) die();
$u = str_replace('http:/', 'http://', $u);
// Remove a follower
if($t == 'follower') {
// @TODO: has it sense that the user remove a follower?. Then the follower should also be notified to remove the current user as following
// Instead, when the request comes from another user removing a following, the action will not be run as there is not authentication
$remote_user = $u;
$local_user = SMOBTools::user_uri();
$follow = "<$remote_user> sioc:follows <$local_user> . ";
$local = "DELETE FROM <".SMOB_ROOT."data/followers> { $follow }";
SMOBStore::query($local);
error_log("DEBUG: Removed follower $remote_user with the query: $local",0);
}
// Remove a following
elseif($t == 'following') {
$remote_user = $u;
$local_user = SMOBTools::user_uri();
$follow = "<$local_user> sioc:follows <$remote_user> . ";
$local = "DELETE FROM <".SMOB_ROOT."data/followings> { $follow }";
SMOBStore::query($local);
error_log("DEBUG: Removed following $remote_user with the query: $local",0);
// And ping to update the followers list remotely
//$ping = str_replace("me","remove", $u)."/follower/$local_user";
$ping = SMOBTools::host($u)."/remove/follower/$local_user";
error_log("DEBUG: Sent $ping",0);
$result = SMOBTools::do_curl($ping);
error_log("DEBUG: Server answer: ".join(' ', $result),0);
// Unsubscribe to the Hub
//@TODO: following Hub should be stored?,
$remote_user_feed = $remote_user.FEED_URL_PATH;
$xml = simplexml_load_file($remote_user_feed);
if(count($xml) == 0)
return;
$link_attributes = $xml->channel->link->attributes();
if($link_attributes['rel'] == 'hub') {
$hub_url = $link_attributes['href'];
}
$callback_url = urlencode(SMOB_ROOT."callback");
$feed = urlencode($remote_user_feed);
$result = SMOBTools::do_curl($hub_url, $postfields = "hub.mode=unsubscribe&hub.verify=async&hub.callback=$callback_url&hub.topic=$feed");
// all good -- anything in the 200 range
if (substr($result[2],0,1) == "2") {
error_log("DEBUG: Successfully unsubscribed to topic $remote_user_feed using hubsub $hub_url",0);
}
error_log("DEBUG: Server answer: ".join(' ', $result),0);
//print "<a href='$remote_user'>$remote_user</a> was deleted from your following list and your subscription was removed";
SMOBTemplate::footer();
}
header("Location: ".SMOB_ROOT."${t}s");
}
elseif($t == 'rss_owner') {
header ("Content-type: text/xml");
//$tweet = new SMOBFeed();
//$tweet->rss();
error_log("DEBUG: rssfilepath: ".FEED_FILE_PATH);
if (!file_exists(FEED_FILE_PATH)) {
error_log("DEBUG: initial RSS file does not exists", 0);
SMOBTools::initial_rss_file();
}
$rssfile = fopen(FEED_FILE_PATH, 'r');
$rss = fread($rssfile, filesize(FEED_FILE_PATH));
fclose($rssfile);
echo($rss);
}
// function to server RDF inside item content
// is not being used for now
elseif($t == 'rssrdf_owner') {
header ("Content-type: text/xml");
$tweet = new SMOBFeed();
$tweet->rssrdf();
}
elseif($t == 'sparql') {
if($_POST) {
SMOBTools::checkAccess($_POST);
}
$ep = ARC2::getStoreEndpoint(SMOBTools::arc_config());
$ep->go();
// callback script to process the incoming hub POSTs
} elseif($t == 'callback') {
if (array_key_exists('REMOTE_HOST',$_SERVER)) {//&& ($_SERVER['REMOTE_HOST'] == HUB_URL_SUBSCRIBE)) {
error_log("DEBUG: request from host: ".$_SERVER['REMOTE_HOST']);
}
if (array_key_exists('HTTP_USER_AGENT',$_SERVER)) {
error_log("DEBUG: request from user_agent: ".$_SERVER['REMOTE_HOST']);
}
// Getting hub_challenge from hub after sending it post subscription
if(isset($_GET["hub_challenge"])) {
// send confirmation to the hub
echo $_GET["hub_challenge"];
error_log("DEBUG: received and sent back hub challenge:".$_GET["hub_challenge"],0);
}
// Getting feed updates from hub
elseif(isset($_POST)) {
$post_data = file_get_contents("php://input");
error_log("DEBUG: received POST with content: $post_data",0);
SMOBTools::get_rdf_from_rss($post_data) ;
}
elseif(isset($_DELETE)) {
$post_data = file_get_contents("php://input");
error_log("DEBUG: received DELETE with content: $post_data",0);
}
elseif(isset($_PUT)) {
$post_data = file_get_contents("php://input");
error_log("DEBUG: received PUT with content: $post_data",0);
}
// same as callback funcion, just to check subscriptions with a different callback URL
} elseif($t == 'callbackrdf') {
if (array_key_exists('REMOTE_HOST',$_SERVER)) {//&& ($_SERVER['REMOTE_HOST'] == HUB_URL_SUBSCRIBE)) {
error_log("DEBUG: request from host: ".$_SERVER['REMOTE_HOST']);
}
if (array_key_exists('HTTP_USER_AGENT',$_SERVER)) {
error_log("DEBUG: request from user_agent: ".$_SERVER['REMOTE_HOST']);
}
// Getting hub_challenge from hub after sending it post subscription
if(isset($_GET["hub_challenge"])) {
// send confirmation to the hub
echo $_GET["hub_challenge"];
error_log("DEBUG: received and sent back hub challenge:".$_GET["hub_challenge"],0);
}
// Getting feed updates from hub
elseif(isset($_POST)) {
$post_data = file_get_contents("php://input");
error_log("DEBUG: received POST with content: $post_data",0);
SMOBTools::get_rdf_from_rss($post_data) ;
}
elseif(isset($_DELETE)) {
$post_data = file_get_contents("php://input");
error_log("DEBUG: received DELETE with content: $post_data",0);
}
elseif(isset($_PUT)) {
$post_data = file_get_contents("php://input");
error_log("DEBUG: received PUT with content: $post_data",0);
}
} elseif($t == 'private') {
// TODO: The private profile graph is the same as the profile graph, privacy preferences will decide what is visible
// TODO: Authorize depending on the WebID URI
if(!SMOBAuth::check()) {
error_log("not authenticated");
//if($a && $a == 'edit'){
// header( 'Location: '.SMOB_ROOT.'auth?redirect=private/edit' ) ;
//} else {
// header( 'Location: '.SMOB_ROOT.'auth?redirect=private' ) ;
//};
header( 'Location: '.SMOB_ROOT.'auth' ) ;
} else {
error_log("authenticated");
if($a && $a == 'edit'){
echo PrivateProfile::view_private_profile_form();
} else {
echo PrivateProfile::view_private_profile();
exit();
};
};
} elseif($t == 'logout'){
session_start();
session_destroy();
echo 'bye';
} else {
$smob = new SMOB($t, $u, $p);
$smob->reply_of($r);
$smob->go();
}
}