|
1
|
|
|
<?php
|
|
2
|
|
|
namespace Kendox;
|
|
3
|
|
|
|
|
4
|
|
|
require_once("class.kendox-token-generator.php");
|
|
5
|
|
|
|
|
6
|
|
|
class Client
|
|
7
|
|
|
{
|
|
8
|
|
|
|
|
9
|
|
|
/**
|
|
10
|
|
|
* Generated token for user
|
|
11
|
|
|
*
|
|
12
|
|
|
* @var StdClass
|
|
|
|
|
|
|
13
|
|
|
*/
|
|
14
|
|
|
public $Token = null;
|
|
15
|
|
|
|
|
16
|
|
|
/**
|
|
17
|
|
|
* URL of Kendox Service Endpoint
|
|
18
|
|
|
* @var string
|
|
19
|
|
|
*/
|
|
20
|
|
|
public $ServiceEndpoint = null;
|
|
21
|
|
|
|
|
22
|
|
|
/**
|
|
23
|
|
|
* Connection Id returned from service on successful login
|
|
24
|
|
|
* @var string
|
|
25
|
|
|
*/
|
|
26
|
|
|
public $ConnectionId = null;
|
|
27
|
|
|
|
|
28
|
|
|
function __construct($serviceEndpoint)
|
|
29
|
|
|
{
|
|
30
|
|
|
if (!str_ends_with($serviceEndpoint, '/')) $serviceEndpoint .= '/';
|
|
31
|
|
|
$this->ServiceEndpoint = $serviceEndpoint;
|
|
32
|
|
|
}
|
|
33
|
|
|
|
|
34
|
|
|
/**
|
|
35
|
|
|
* Login to kendox service by creating a user based token, signed by Kendox trusted certificate
|
|
36
|
|
|
*
|
|
37
|
|
|
* @param string $pfxFile The full path and file name of the PFX-File (certificate)
|
|
38
|
|
|
* @param string $pfxPassword The password used for reading the PFX-File (certificate)
|
|
39
|
|
|
* @param string $userName Username for token generation
|
|
40
|
|
|
*
|
|
41
|
|
|
* @return bool
|
|
42
|
|
|
*/
|
|
43
|
|
|
public function loginWithToken($pfxFile, $pfxPassword, $userName) {
|
|
44
|
|
|
try {
|
|
45
|
|
|
$issuer = gethostname();
|
|
46
|
|
|
$tokenGenerator = new \Kendox\TokenGenerator($issuer, $pfxFile, $pfxPassword);
|
|
47
|
|
|
$this->Token = $tokenGenerator->generateToken($userName);
|
|
|
|
|
|
|
48
|
|
|
$logonParameters = [
|
|
49
|
|
|
"tenantName" => "",
|
|
50
|
|
|
"token" => $this->Token,
|
|
51
|
|
|
"tokenType" => "InfoShareToken"
|
|
52
|
|
|
];
|
|
53
|
|
|
$result = $this->post("Authentication/LogonWithToken", $logonParameters);
|
|
54
|
|
|
$this->ConnectionId = $result->LogonWithTokenResult->ConnectionId;
|
|
55
|
|
|
return true;
|
|
56
|
|
|
} catch(\Exception $ex) {
|
|
57
|
|
|
throw new \Exception("Token-Login failed: ".$ex->getMessage());
|
|
58
|
|
|
}
|
|
59
|
|
|
}
|
|
60
|
|
|
|
|
61
|
|
|
/**
|
|
62
|
|
|
* Logout from kendox
|
|
63
|
|
|
*
|
|
64
|
|
|
* @return bool
|
|
65
|
|
|
*/
|
|
66
|
|
|
public function logout() {
|
|
67
|
|
|
try {
|
|
68
|
|
|
$logoutParameters = [
|
|
69
|
|
|
"connectionId" => $this->ConnectionId
|
|
70
|
|
|
];
|
|
71
|
|
|
$result = $this->post("Authentication/Logout", $logoutParameters);
|
|
|
|
|
|
|
72
|
|
|
$this->ConnectionId = null;
|
|
73
|
|
|
return true;
|
|
74
|
|
|
} catch(\Exception $ex) {
|
|
75
|
|
|
throw new \Exception("Token-Login failed: ".$ex->getMessage());
|
|
76
|
|
|
}
|
|
77
|
|
|
}
|
|
78
|
|
|
|
|
79
|
|
|
/**
|
|
80
|
|
|
* Performs a user table query and fetch the result records
|
|
81
|
|
|
*
|
|
82
|
|
|
* @param $userTableName The name of the user table
|
|
|
|
|
|
|
83
|
|
|
* @param $whereClauseElements Array with fields "ColumnName", "RelationalOperator" and "Value" for filter defintion of the query
|
|
84
|
|
|
* @param $addColumnHeaders Add column headers to result?
|
|
|
|
|
|
|
85
|
|
|
*
|
|
86
|
|
|
* @return array The data result as an array
|
|
87
|
|
|
*/
|
|
88
|
|
|
public function userTableQuery($userTableName, $whereClauseElements, $addColumnHeaders) {
|
|
89
|
|
|
try {
|
|
90
|
|
|
$parameters = [
|
|
91
|
|
|
"connectionId" => $this->ConnectionId,
|
|
92
|
|
|
"userTable" => $userTableName,
|
|
93
|
|
|
"whereClauseElements" => $whereClauseElements,
|
|
94
|
|
|
"addColumnHeaders" => $addColumnHeaders
|
|
95
|
|
|
];
|
|
96
|
|
|
$result = $this->post("UserTable/UserTableGetRecords", $parameters);
|
|
97
|
|
|
if (!isset($result->UserTableGetRecordsResult)) throw new \Exception("Unexpected result");
|
|
98
|
|
|
return $result->UserTableGetRecordsResult;
|
|
99
|
|
|
} catch(\Exception $ex) {
|
|
100
|
|
|
throw new \Exception("User table query failed: ".$ex->getMessage());
|
|
101
|
|
|
}
|
|
102
|
|
|
}
|
|
103
|
|
|
|
|
104
|
|
|
/**
|
|
105
|
|
|
* Uploading a file
|
|
106
|
|
|
* @param string $file Path and file name of file to upload
|
|
107
|
|
|
*/
|
|
108
|
|
|
public function uploadFile($file) {
|
|
109
|
|
|
$content = file_get_contents($file);
|
|
110
|
|
|
return $this->uploadContent($content);
|
|
111
|
|
|
}
|
|
112
|
|
|
|
|
113
|
|
|
/**
|
|
114
|
|
|
* Uploading a stream of data
|
|
115
|
|
|
* @param Stream $stream Stream of content to upload
|
|
|
|
|
|
|
116
|
|
|
*/
|
|
117
|
|
|
public function uploadStream($stream) {
|
|
118
|
|
|
$content = stream_get_contents($stream);
|
|
|
|
|
|
|
119
|
|
|
return $this->uploadContent($content);
|
|
120
|
|
|
}
|
|
121
|
|
|
|
|
122
|
|
|
private function uploadContent($content) {
|
|
123
|
|
|
$base64 = base64_encode($content);
|
|
124
|
|
|
$uploadParameters = [
|
|
125
|
|
|
"connectionId" => $this->ConnectionId,
|
|
126
|
|
|
"fileContentbase64" => $base64
|
|
127
|
|
|
];
|
|
128
|
|
|
$result = $this->post("File/UploadFileBase64", $uploadParameters);
|
|
129
|
|
|
return $result->UploadFileBase64Result;
|
|
130
|
|
|
}
|
|
131
|
|
|
|
|
132
|
|
|
/**
|
|
133
|
|
|
* Performing a post request to service
|
|
134
|
|
|
*
|
|
135
|
|
|
* @param string $path the route to the API endpoint (without service endpoint url)
|
|
136
|
|
|
* @param string Associated array with data to post
|
|
|
|
|
|
|
137
|
|
|
*
|
|
138
|
|
|
* @return object Returns object with data. If service returns an error an exception will be thrown with detailed information
|
|
139
|
|
|
*/
|
|
140
|
|
|
private function post($path, $data)
|
|
141
|
|
|
{
|
|
142
|
|
|
$ch = curl_init();
|
|
143
|
|
|
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
|
144
|
|
|
curl_setopt($ch, CURLOPT_URL, $this->ServiceEndpoint.$path);
|
|
145
|
|
|
curl_setopt($ch, CURLOPT_POST, 1);
|
|
146
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
|
|
147
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
148
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
|
|
149
|
|
|
$jsonResult = curl_exec($ch);
|
|
150
|
|
|
if (curl_errno($ch)) {
|
|
151
|
|
|
throw new \Exception("Error on post request: ".curl_errno($ch));
|
|
152
|
|
|
}
|
|
153
|
|
|
return $this->handleJsonResult($jsonResult);
|
|
154
|
|
|
|
|
155
|
|
|
}
|
|
156
|
|
|
|
|
157
|
|
|
private function handleJsonResult($json)
|
|
158
|
|
|
{
|
|
159
|
|
|
if ($json === FALSE) {
|
|
160
|
|
|
throw new \Exception("No valid JSON has been returned from service.");
|
|
161
|
|
|
}
|
|
162
|
|
|
$result = json_decode($json);
|
|
163
|
|
|
if (isset($result->ErrorNumber)) {
|
|
164
|
|
|
throw new \Exception("(".$result->ErrorNumber.") ".$result->Message);
|
|
165
|
|
|
}
|
|
166
|
|
|
return $result;
|
|
167
|
|
|
}
|
|
168
|
|
|
|
|
169
|
|
|
}
|
|
170
|
|
|
|
|
171
|
|
|
?> |
|
|
|
|
|