/
UserController.php
439 lines (367 loc) · 15.3 KB
/
UserController.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<?php
/**
* This file is part of the {@link http://aksw.org/Projects/Xodx Xodx} project.
*
* @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
*/
// include password hash functions for 5.3.7 <= PHP < 5.5
require_once('password_compat/lib/password.php');
/**
* This class manages instances of Xodx_User.
* this includes:
* - subscribing to a feed
* - getting notifications
*/
class Xodx_UserController extends Xodx_ResourceController
{
/**
* A registry of already loaded Xodx_User objects
*/
private $_users = array();
public function homeAction ($template)
{
$bootstrap = $this->_app->getBootstrap();
$model = $bootstrap->getResource('model');
$request = $bootstrap->getResource('request');
$user = $this->getUser();
if ($user->getName() !== 'guest') {
$personUri = $user->getPerson();
$nsFoaf = 'http://xmlns.com/foaf/0.1/';
$profileQuery = 'PREFIX foaf: <' . $nsFoaf . '>' . PHP_EOL;
$profileQuery.= 'SELECT ?depiction ?name ?nick' . PHP_EOL;
$profileQuery.= 'WHERE {' . PHP_EOL;
$profileQuery.= ' <' . $personUri . '> a foaf:Person .' . PHP_EOL;
$profileQuery.= ' OPTIONAL {<' . $personUri . '> foaf:depiction ?depiction .}' . PHP_EOL;
$profileQuery.= ' OPTIONAL {<' . $personUri . '> foaf:name ?name .}' . PHP_EOL;
$profileQuery.= ' OPTIONAL {<' . $personUri . '> foaf:nick ?nick .}' . PHP_EOL;
$profileQuery.= '}';
// TODO deal with language tags
$contactsQuery = 'PREFIX foaf: <' . $nsFoaf . '> ' . PHP_EOL;
$contactsQuery.= 'SELECT ?contactUri ?name ?nick ' . PHP_EOL;
$contactsQuery.= 'WHERE { ' . PHP_EOL;
$contactsQuery.= ' <' . $personUri . '> foaf:knows ?contactUri . ' . PHP_EOL;
$contactsQuery.= ' OPTIONAL {?contactUri foaf:name ?name .} ' . PHP_EOL;
$contactsQuery.= ' OPTIONAL {?contactUri foaf:nick ?nick .} ' . PHP_EOL;
$contactsQuery.= '}';
$profile = $model->sparqlQuery($profileQuery);
if (count($profile) > 0) {
$knows = $model->sparqlQuery($contactsQuery);
$activities = $this->getActivityStream($user);
$factory = new Xodx_NotificationFactory($this->_app);
$notifications = $factory->getForUser($user->getUri(), false);
$template->profileshowPersonUri = $personUri;
$template->profileshowDepiction = $profile[0]['depiction'];
$template->profileshowName = $profile[0]['name'];
$template->profileshowNick = $profile[0]['nick'];
$template->profileshowActivities = $activities;
$template->profileshowKnows = $knows;
$template->profileshowNews = $notifications;
}
}
return $template;
}
public function getPersonUriAction ($template)
{
$personUri = $this->getUser()->getPerson();
$template->disableLayout();
$template->setRawContent($personUri);
return $template;
}
public function getActivityStreamAction ($template)
{
$bootstrap = $this->_app->getBootstrap();
$activities = $this->getActivityStream();
$request = $bootstrap->getResource('request');
$num = $request->getValue('num');
$own = $request->getValue('own');
if ($own === null || $own == 'true') {
$own = true;
} else if ($own == 'false') {
$own = false;
$personUri = $this->getUser()->getPerson();
}
$stream = '';
foreach ($activities as $activity) {
if (!$own && $activity['authorUri'] == $personUri) {
continue;
}
if ($num !== null) {
if ($num < 1) {
break;
} else {
$num--;
}
}
$stream .= $activity['uri'] . PHP_EOL;
}
$template->disableLayout();
$template->setRawContent($stream);
return $template;
}
/**
*
* Enter description here ...
* @param URI $subscriberUri
* @param URI $resourceUri
* @param URI $feedUri
* @param boolean $local specifies if the feed should not be subscribed at the hub
* (this is meant for local resources)
*/
public function subscribeToResource ($subscriberUri, $resourceUri, $feedUri = null, $local = false)
{
$bootstrap = $this->_app->getBootstrap();
$model = $bootstrap->getResource('model');
if ($feedUri === null) {
$feedUri = $this->getActivityFeedUri($resourceUri);
}
$feedObject = array(
'type' => 'uri',
'value' => $feedUri
);
$nsDssn = 'http://purl.org/net/dssn/';
$model->addStatement($resourceUri, $nsDssn . 'activityFeed', $feedObject);
$this->_subscribeToFeed($subscriberUri, $feedUri, $local);
}
/**
* This method subscribes a user to a feed
* @param $userUri the uri of the user who wants to be subscribed
* @param $feedUri the uri of the feed where she wants to subscribe
*/
private function _subscribeToFeed ($subscriberUri, $feedUri, $local = false)
{
$bootstrap = $this->_app->getBootstrap();
$logger = $bootstrap->getResource('logger');
$resourceController = $this->_app->getController('Xodx_ResourceController');
$nsFoaf = 'http://xmlns.com/foaf/0.1/';
$type = $resourceController->getType($subscriberUri);
if ($type === $nsFoaf . 'Person') {
$subscriberUri = $this->getUserUri($subscriberUri);
}
$logger->info('subscribeToFeed: user: ' . $subscriberUri . ', feed: ' . $feedUri);
if (!$this->_isSubscribed($subscriberUri, $feedUri)) {
$pushController = $this->_app->getController('Xodx_PushController');
if ($local || $pushController->subscribe($feedUri)) {
$model = $bootstrap->getResource('model');
$nsDssn = 'http://purl.org/net/dssn/';
$nsRdf = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
$subUri = $this->_app->getBaseUri() . '&c=ressource&id=' . md5(rand());
$cbUri = $this->_app->getBaseUri() . '?c=push&a=callback';
$subscription = array(
$subUri => array(
$nsRdf . 'type' => array(
array('type' => 'uri', 'value' => $nsDssn . 'Subscription')
),
$nsDssn . 'subscriptionCallback' => array(
array('type' => 'uri', 'value' => $cbUri)
),
$nsDssn . 'subscriptionTopic' => array(
array('type' => 'uri', 'value' => $feedUri)
)
)
);
if (!$local) {
$feed = DSSN_Activity_Feed_Factory::newFromUrl($feedUri);
$subscription[$subUri][$nsDssn . 'subscriptionHub'][] = array(
'type' => 'uri', 'value' => $feed->getLinkHub()
);
}
$subscribeStatement = array(
$subscriberUri => array(
$nsDssn . 'subscribedTo' => array(
array('type' => 'uri', 'value' => $subUri)
)
)
);
$model->addMultipleStatements($subscription);
$model->addMultipleStatements($subscribeStatement);
}
}
}
/**
* This method creates a new object of the class Xodx_User
* @param $userUri a string which contains the URI of the required user
* @return Xodx_User instance with the specified URI
*/
public function getUser ($userUri = null)
{
if ($userUri === null) {
$applicationController = $this->_app->getController('Xodx_ApplicationController');
$userId = $applicationController->getUser();
$userUri = $this->_app->getBaseUri() . '?c=user&id=' . $userId;
}
if (!isset($this->_users[$userUri])) {
$bootstrap = $this->_app->getBootstrap();
$model = $bootstrap->getResource('model');
$query = 'PREFIX foaf: <http://xmlns.com/foaf/0.1/> ' . PHP_EOL;
$query.= 'PREFIX sioc: <http://rdfs.org/sioc/ns#> ' . PHP_EOL;
$query.= 'SELECT ?name ?person' . PHP_EOL;
$query.= 'WHERE {' . PHP_EOL;
$query.= ' <' . $userUri . '> foaf:accountName ?name ;' . PHP_EOL;
$query.= ' sioc:account_of ?person .' . PHP_EOL;
$query.= '}' . PHP_EOL;
$result = $model->sparqlQuery($query);
if (count($result) > 0) {
$userId = $result[0]['name'];
$personUri = $result[0]['person'];
} else {
// This case is needed because ne guest account exists but it should throw an
// Exception if this issue is cleared
if (!isset($userId)) {
$userId = 'unkown';
}
$personUri = null;
}
$user = new Xodx_User($userUri);
$user->setName($userId);
$user->setPerson($personUri);
$this->_users[$userUri] = $user;
}
return $this->_users[$userUri];
}
/**
* Get the userAccount of a Person
* @param string $personUri the uri of the person
* @return Xodx_User for the given person
*/
public function getUserForPerson ($personUri)
{
$bootstrap = $this->_app->getBootstrap();
$model = $bootstrap->getResource('model');
// SPARQL-Query
$query = 'PREFIX foaf: <http://xmlns.com/foaf/0.1/> ' . PHP_EOL;
$query.= 'SELECT ?userUri ' . PHP_EOL;
$query.= 'WHERE {' . PHP_EOL;
$query.= ' <' . $personUri . '> foaf:account ?userUri. ' . PHP_EOL;
$query.= '}' . PHP_EOL;
$userResult = $model->sparqlQuery($query);
if (count($userResult[0])>0) {
return $this->getUser($userResult[0]['userUri']);
} else {
return null;
}
}
/**
* Returns the user uri of a user which is accosiated to the given person
* @deprecated use getUserForPerson
*/
public function getUserUri ($personUri)
{
$user = $this->getUserForPerson($personUri);
if ($user !== null) {
return $user->getUri();
} else {
return null;
}
}
/**
* This function verifies the given credentials for a user
* @param $userName a string with the username of the user
* @param $password a string containing the password of the given user
*/
public function verifyPasswordCredentials ($userName, $password)
{
$bootstrap = $this->_app->getBootstrap();
$model = $bootstrap->getResource('model');
// TODO prevent sparql injection
$query = '' .
'PREFIX ow: <http://ns.ontowiki.net/SysOnt/> ' .
'PREFIX sioc: <http://rdfs.org/sioc/ns#> ' .
'PREFIX foaf: <http://xmlns.com/foaf/0.1/> ' .
'SELECT ?userUri ?passwordHash ' .
'WHERE { ' .
' ?userUri a sioc:UserAccount ; ' .
' foaf:accountName "' . $userName . '" ; ' .
' ow:userPassword ?passwordHash . ' .
'}';
$passwordQueryResult = $model->sparqlQuery($query);
if (count($passwordQueryResult) > 0) {
$passwordHash = $passwordQueryResult[0]['passwordHash'];
return password_verify($password, $passwordHash);
} else {
return false;
}
}
/**
* Check if a user is already subscribed to a feed
* @param $userUri the uri of the user in question
* @param $feedUri the uri of the feed in question
*/
private function _isSubscribed ($userUri, $feedUri)
{
$bootstrap = $this->_app->getBootstrap();
$model = $bootstrap->getResource('model');
$query = 'PREFIX dssn: <http://purl.org/net/dssn/> ' . PHP_EOL;
$query.= 'ASK ' . PHP_EOL;
$query.= 'WHERE { ' . PHP_EOL;
$query.= ' <' . $userUri . '> dssn:subscribedTo ?subUri. ' . PHP_EOL;
$query.= ' ?subUri dssn:subscriptionTopic <' . $feedUri. '> . ' . PHP_EOL;
$query.= '}' . PHP_EOL;
$subscribedResult = $model->sparqlQuery($query);
if (count($subscribedResult) > 0) {
if (is_array($subscribedResult)) {
// Erfurt problem
return empty($subscribedResult[0]['__ask_retval']);
} else if (is_bool($subscribedResult)) {
return $subscriptionResult;
} else {
$logger = $bootstrap->getResource('logger');
$logger->info('isSubscribed: user: ' . $userUri . ', feed: ' . $feedUri . '. ASK Query returned unexpectedly: ' . var_export($subscriptionResult));
throw new Exception('Erfurt returned an unexpected result to the ask query.');
}
}
return null;
}
/**
*
* Method returns all activities the user is subscribed to
* @param Xodx_User $user
* @return array of activities
*/
public function getActivityStream (Xodx_User $user = null)
{
if ($user === null) {
$user = $this->getUser();
}
$subscribedResources = $this->getSubscribedResources($user);
$activityController = $this->_app->getController('Xodx_ActivityController');
$activities = array();
foreach ($subscribedResources as $resourceUri) {
$act = $activityController->getActivities($resourceUri);
$activities = array_merge($activities, $act);
}
$tmp = Array();
foreach($activities as &$act) {
$tmp[] = &$act["pubDate"];
}
array_multisort($tmp, SORT_DESC, $activities);
return $activities;
}
/**
* Find all resources a user is subscribed to via Activity Feed
* @param $userUri the uri of the user in question
* @return array $subscribedResources all resource a user is subscribed to
*/
public function getSubscribedResources (Xodx_User $user)
{
$bootstrap = $this->_app->getBootstrap();
$model = $bootstrap->getResource('model');
$userUri = $user->getUri();
// SPARQL-Query
$query = 'PREFIX dssn: <http://purl.org/net/dssn/> ' . PHP_EOL;
$query.= 'SELECT DISTINCT ?resUri' . PHP_EOL;
$query.= 'WHERE {' . PHP_EOL;
$query.= ' <' . $userUri . '> dssn:subscribedTo ?subUri. ' . PHP_EOL;
$query.= ' ?subUri dssn:subscriptionTopic ?feedUri. ' . PHP_EOL;
$query.= ' ?resUri dssn:activityFeed ?feedUri. ' . PHP_EOL;
$query.= '}' . PHP_EOL;
$result = $model->sparqlQuery($query);
$subscribedResources = array();
// results in array
foreach ($result as $resource) {
if (isset($resource['resUri'])) {
$subscribedResources[] = $resource['resUri'];
}
}
return $subscribedResources;
}
}