forked from sgoendoer/sonic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SonicServerExample.php
195 lines (153 loc) · 6.75 KB
/
SonicServerExample.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
<?php namespace sgoendoer\Sonic\examples;
require_once(__DIR__ . '/../vendor/autoload.php');
use sgoendoer\Sonic\Sonic;
use sgoendoer\Sonic\Configuration\Configuration;
use sgoendoer\Sonic\Identity\EntityAuthData;
use sgoendoer\Sonic\Identity\SocialRecordManager;
use sgoendoer\Sonic\AccessControl\AccessControlManager;
use sgoendoer\Sonic\AccessControl\AccessControlException;
use sgoendoer\Sonic\Request\IncomingRequest;
use sgoendoer\Sonic\Api\ResponseBuilder;
use sgoendoer\Sonic\Api\MethodNotAllowedException;
use sgoendoer\Sonic\Model\PersonObjectBuilder;
use sgoendoer\Sonic\Model\LikeObjectBuilder;
use sgoendoer\Sonic\examples\SocialRecordCachingExample;
use sgoendoer\Sonic\examples\AccessControlManagerExample;
use sgoendoer\Sonic\examples\UniqueIDManagerExample;
// all requests to the Sonic API need to be redirected to be handled by this script
// clean url functionality: the script expects the request path to be passed as a variable $urlpath
$targetedGID = NULL;
$resource = NULL;
$resourceID = NULL;
$subresource = NULL;
$subresourceID = NULL;
if($_REQUEST['urlpath'] != '')
{
$requestURL = explode('/', $_REQUEST['urlpath']);
$targetedGID = $requestURL[0];
if(count($requestURL) >= 2)
$resource = strtoupper($requestURL[1]);
if(count($requestURL) >= 3)
$resourceID = $requestURL[2];
if(count($requestURL) >= 4)
$subresource = strtolower($requestURL[3]);
if(count($requestURL) >= 5)
$subresourceID = $requestURL[4];
if(count($requestURL) >= 6)
$subsubresource = $requestURL[5];
}
try
{
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// setting Configuration
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Configuration::setLogfile('sonic.log');
Configuration::setApiPath('/sonic-api-endpoint/');
Configuration::setTimezone('Europe/Berlin');
Configuration::setPrimaryGSLSNode('130.149.22.135:4002');
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// importing SocialRecord objects to work with
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// load SocialRecords from files to instatiaze the Sonic framework
$srp = SocialRecordManager::importSocialRecord(file_get_contents(__DIR__ . '/data/SRPlatform.json'));
$platformSocialRecord = $srp['socialRecord'];
$platformAccountKeyPair = $srp['accountKeyPair'];
$platformPersonalKeyPair = $srp['personalKeyPair'];
$sra = SocialRecordManager::importSocialRecord(file_get_contents(__DIR__ . '/data/SRAlice.json'));
$userSocialRecord = $sra['socialRecord'];
$userAccountKeyPair = $sra['accountKeyPair'];
$userPersonalKeyPair = $sra['personalKeyPair'];
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// initializing Sonic SDK
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// instantiaze the Sonic framework with the platform's SocialRecord
$sonic = Sonic::initInstance(new EntityAuthData($platformSocialRecord, $platformAccountKeyPair, $platformPersonalKeyPair));
Sonic::setUserAuthData(new EntityAuthData($userSocialRecord, $userAccountKeyPair));
Sonic::setContext(Sonic::CONTEXT_USER);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// setting up managers
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// setting a AccessPermissionManager
$sonic->setAccessControlManager(new AccessControlManagerExample(AccessControlManager::DIRECTIVE_ALLOW, AccessControlManager::DIRECTIVE_DENY));
// setting up SocialRecordCaching
$sonic->setSocialRecordCaching(new SocialRecordCacheExample());
// setting up UniqueIDManagement
$sonic->setUniqueIDManager(new UniqueIDManagerExample());
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// request handling
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$request = new IncomingRequest();
$globalIDAlice = '4802C8DE6UZZ5BICQI830A8P8BW3YB5EBPGXWNRH1EP7H838V7';
// for demo purposes, we are only handling requests for Alice
if($targetedGID != $globalIDAlice)
{
$response = new ResponseBuilder(404);
$response->init('Unknown GlobalID: ' . $targetedGID);
$response->dispatch();
die();
}
// check access permissions for the addressed interface
if(!Sonic::getAccessControlManager()->hasInterfaceAccessPriviledges($request->getHeaderSourceGID(), $resource, $request->getMethod()))
throw new AccessControlException();
switch(strtolower($resource))
{
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// resource PERSON
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
case 'person':
if($request->getMethod() == 'GET')
{
$personObject = PersonObjectBuilder::buildFromJSON(file_get_contents(__DIR__ . '/data/AlicePerson.json'));
// check access permissions for the content object
if(!Sonic::getAccessControlManager()->hasContentAccessPriviledges($request->getHeaderSourceGID(), $personObject->getObjectID()))
throw new AccessControlException();
$response = new ResponseBuilder(200);
$response->init()->setBody($personObject->getJSON());
$response->dispatch();
}
else
{
throw new MethodNotAllowedException();
}
break;
case 'like':
if($request->getMethod() == 'POST')
{
// building LIKE object from request body data
$likeObject = LikeObjectBuilder::buildFromJSON($request->getBody());
// here, we would store the received object in the database
$response = new ResponseBuilder(200);
$response->init('like object received: [' . $likeObject->getObjectID() . ']');
$response->dispatch();
}
else
{
throw new MethodNotAllowedException();
}
break;
default:
$response = new ResponseBuilder(404);
$response->init('Resource ' . $resource . ' not found');
$response->dispatch();
break;
}
}
catch (AccessControlException $e)
{
$response = new ResponseBuilder(403);
$response->init('access denied');
$response->dispatch();
}
catch (MethodNotAllowedException $e)
{
$response = new ResponseBuilder(405);
$response->init('method not allowed for resource ' . $resource);
$response->dispatch();
}
catch (\Exception $e)
{
$response = new ResponseBuilder(500);
$response->init('internal server error');
$response->dispatch();
}
?>