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
|
<?php
class OERHost extends OERIdentity
{
//These two HTTP-headers are non-conformant custom HTTP-headers for requests
const OER_HEADER_PUBLIC_KEY_HASH = "Publickey-Hash";
const OER_HEADER_SIGNATURE = "RSA-Signature-Base64";
/**
* Fetches the OERHost of this Stud.IP. If none existed before it will be created and the object returnd.
* @return OERHost
*/
public static function thisOne()
{
$host = self::findOneBySQL("private_key IS NOT NULL LIMIT 1");
if ($host) {
$host['url'] = $GLOBALS['oer_PREFERRED_URI'] ?: $GLOBALS['ABSOLUTE_URI_STUDIP']."dispatch.php/oer/endpoints/";
if ($host->isFieldDirty("url")) {
$host->store();
}
return $host;
} else {
$host = new static();
$host['name'] = Config::get()->UNI_NAME_CLEAN;
$host['url'] = $GLOBALS['oer_PREFERRED_URI'] ?: $GLOBALS['ABSOLUTE_URI_STUDIP']."dispatch.php/oer/endpoints/";
$host['last_updated'] = time();
$host->store();
return $host;
}
}
/**
* Fetches and returns all known hosts from database.
* @return array of all OERHost objects
*/
public static function findAll()
{
return self::findBySQL("1=1 ORDER BY name ASC");
}
/**
* configures this class
* @param array $config
*/
protected static function configure($config = [])
{
$config['db_table'] = 'oer_hosts';
parent::configure($config);
}
/**
* Checks if this object is the OERHost of this Stud.IP.
* @return bool
*/
public function isMe()
{
return (bool) $this['private_key'];
}
/**
* Fetches the public key from an OERHost. Makes a request to the other host.
*/
public function fetchPublicKey()
{
$endpoint_url = $this['url']."fetch_public_host_key";
$endpoint_url .= "?from=".urlencode($GLOBALS['oer_PREFERRED_URI'] ?: $GLOBALS['ABSOLUTE_URI_STUDIP']."dispatch.php/oer/endpoints/");
$host_data = @file_get_contents($endpoint_url, false, get_default_http_stream_context($endpoint_url));
if ($host_data) {
$host_data = json_decode($host_data, true);
if ($host_data) {
$this['name'] = $host_data['name'];
$this['public_key'] = preg_replace("/\r/", "", $host_data['public_key']);
$this['url'] = $host_data['url'];
$this['index_server'] = $host_data['index_server'];
$host['last_updated'] = time();
if ($this->isNew()) {
$host['active'] = 1;
}
}
}
}
/**
* Ask this host for all hosts that it knows. This is a request to the other host.
*/
public function askKnownHosts()
{
$endpoint_url = $this['url']."fetch_known_hosts"
."?from=".urlencode($GLOBALS['oer_PREFERRED_URI'] ?: $GLOBALS['ABSOLUTE_URI_STUDIP']."dispatch.php/oer/endpoints/");
$output = @file_get_contents($endpoint_url, false, get_default_http_stream_context($endpoint_url));
if ($output) {
$output = json_decode($output, true);
foreach ((array) $output['hosts'] as $host_data) {
$host = OERHost::findOneByUrl($host_data['url']);
if (!$host) {
$host = new OERHost();
$host['url'] = $host_data['url'];
}
if ($host['last_updated'] < time() - 60 * 60 * 8) {
$host->fetchPublicKey();
}
$host->store();
}
} else {
PageLayout::postWarning(_("Kann von dem Server keine Daten bekommen."));
}
return $output;
}
/**
* Executes a search request on the host.
* @param string|null $text : the serach string
* @param string|null $tag : a tag to search for
*/
public function fetchRemoteSearch($text = null, $tag = null)
{
$endpoint_url = $this['url']."search_items";
if ($tag) {
$endpoint_url .= "?tag=".urlencode($tag);
} else {
$endpoint_url .= "?text=".urlencode($text);
}
$output = @file_get_contents($endpoint_url, false, get_default_http_stream_context($endpoint_url));
if ($output) {
$output = json_decode($output, true);
foreach ((array) $output['results'] as $material_data) {
$host = OERHost::findOneBySQL("public_key = ?", [$material_data['host']['public_key']]);
if (!$host) {
$host = new OERHost();
$host['url'] = $material_data['host']['url'];
$host->fetchPublicKey();
$host->store();
}
if (!$host->isMe()) {
//set material:
$material_data['data']['foreign_material_id'] = $material_data['data']['id'];
$material = OERMaterial::findOneBySQL("foreign_material_id = ? AND host_id = ?", [
$material_data['data']['foreign_material_id'],
$host->getId()
]);
if (!$material) {
$material = new OERMaterial();
}
unset($material_data['data']['id']);
$usersdata = $material_data['data']['users'];
unset($material_data['data']['users']);
$material->setData($material_data['data']);
if (!$material['category']) {
$material['category'] = $material->autoDetectCategory();
}
$material['host_id'] = $host->getId();
$material->store();
//set topics:
$material->setUsers($usersdata);
//set topics:
$material->setTopics($material_data['topics']);
}
}
}
}
/**
* Pushes some data to the foreign OERHost.
* @param string $endpoint : part behind the host-url like "push_data"
* @param array $data : the data to be pushed as an associative array
* @return bool|CurlHandle|resource
*/
public function pushDataToEndpoint($endpoint, $data)
{
$payload = json_encode($data);
$myHost = OERHost::thisOne();
$endpoint_url = $this['url'].$endpoint;
$request = curl_init();
curl_setopt($request, CURLOPT_URL, $endpoint_url);
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($request, CURLOPT_VERBOSE, 0);
curl_setopt($request, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($request, CURLOPT_POST, true);
curl_setopt($request, CURLOPT_POSTFIELDS, $payload);
curl_setopt($request, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($request, CURLOPT_SSL_VERIFYHOST, false);
$header = [
OERHost::OER_HEADER_SIGNATURE.": ".base64_encode($myHost->createSignature($payload)),
OERHost::OER_HEADER_PUBLIC_KEY_HASH.": ".md5($myHost['public_key'])
];
curl_setopt($request, CURLOPT_HTTPHEADER, $header);
$result = curl_exec($request);
$response_code = curl_getinfo($request, CURLINFO_HTTP_CODE);
curl_close($request);
return $response_code < 300;
}
/**
* Fetches all information of an item from that host. This is a request.
* @param string $foreign_material_id : foreign id of that oer-material
* @return array|null : data of that material or null on error.
*/
public function fetchItemData($foreign_material_id)
{
$endpoint_url = $this['url']."get_item_data/".urlencode($foreign_material_id);
$output = @file_get_contents($endpoint_url, false, get_default_http_stream_context($endpoint_url));
if ($output) {
$output = json_decode($output, true);
if ($output) {
return $output;
}
}
}
}
|