-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClientRegistration.php
More file actions
72 lines (66 loc) · 2.05 KB
/
Copy pathClientRegistration.php
File metadata and controls
72 lines (66 loc) · 2.05 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
<?php
namespace Pdsinterop\PhpSolid;
use Pdsinterop\PhpSolid\Db;
class ClientRegistration {
public static function getRegistration($clientId) {
Db::connect();
$query = Db::$pdo->prepare(
'SELECT clientData FROM clients WHERE clientId=:clientId'
);
$query->execute([
':clientId' => $clientId
]);
$result = $query->fetchAll();
if (sizeof($result) === 1) {
return json_decode($result[0]['clientData'], true);
}
if (preg_match("/^http(s)?:/", $clientId)) {
$clientData = self::getRemoteRegistration($clientId);
if (!isset($clientData['origin']) && isset($clientData['client_uri'])) {
$clientData['origin'] = preg_replace("/\/$/", "", $clientData['client_uri']);
}
self::saveClientRegistration($clientData);
return $clientData;
}
return false;
}
public static function getRemoteRegistration($url) {
$clientDocument = file_get_contents($url);
$clientRegistration = json_decode($clientDocument, true);
if (!isset($clientRegistration['client_id'])) {
throw new \Exception("No client ID found in client document");
}
if (!isset($clientRegistration['redirect_uris'])) {
throw new \Exception("No redirect URIs found in client document");
}
return $clientRegistration;
}
public static function saveClientRegistration($clientData) {
Db::connect();
if (!isset($clientData['client_name'])) {
$clientData['client_name'] = $clientData['origin'];
}
$query = Db::$pdo->prepare(
'INSERT INTO clients VALUES(:clientId, :origin, :clientData)'
);
$query->execute([
':clientId' => $clientData['client_id'],
':origin' => $clientData['origin'],
':clientData' => json_encode($clientData)
]);
}
public static function getClientByOrigin($origin) {
Db::connect();
$query = Db::$pdo->prepare(
'SELECT clientData FROM clients WHERE origin=:origin'
);
$query->execute([
':origin' => $origin
]);
$result = $query->fetchAll();
if (sizeof($result)=== 1) {
return json_decode($result[0]['clientData'], true);
}
return false;
}
}