-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathServer.php
288 lines (267 loc) · 10.1 KB
/
Server.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
<?php
/**
* Server-related features
*/
namespace PHPCouchDB;
const VERSION = "0.1.2";
/**
* Server class deals with operations on the server level, rather than specific
* to a particular database
*/
class Server
{
protected $client;
const OPTION_CLIENT = 'client';
const OPTION_URL = 'url';
const OPTION_NAME = 'name';
const OPTION_CREATE_IF_NOT_EXISTS = 'create_if_not_exists';
/**
* Create the object to represent the server
*
* See also: \PHPCouchDB\Server:createFromURL()
*
* @param array $options Supply either a string "url" parameter OR a
* \GuzzleHttp\ClientInterface "client" parameter if more configuration
* is required
* @throws \PHPCouchDB\Exception\ServerException if there's a problem
* with parsing arguments or connecting to the database
*/
public function __construct(array $options)
{
if (empty($options) || !is_array($options)) {
throw new Exception\ServerException(
'$options is a required parameter, array should contain either a url or a client'
);
}
if (isset($options[self::OPTION_CLIENT])
&& $options[self::OPTION_CLIENT] instanceof \GuzzleHttp\ClientInterface
) {
$client = $options[self::OPTION_CLIENT];
} elseif (isset($options[self::OPTION_URL])) {
// set a descriptive user agent
$user_agent = \GuzzleHttp\default_user_agent();
$client = new \GuzzleHttp\Client(
["base_uri" => $options[self::OPTION_URL],
"headers" => ["User-Agent" => "PHPCouchDB/" . VERSION . " " . $user_agent]]
);
} else {
throw new Exception\ServerException(
'Failed to parse $options, array should contain either a url or a client'
);
}
$this->client = $client;
}
/**
* Ask the CouchDB server what version it is running
*
* @return string Version, e.g. "2.0.1"
* @throws \PHPCouchDB\Exception\ServerException if there's a problem with
* connecting to the server or parsing arguments
*/
public function getVersion() : string
{
try {
$response = $this->client->request("GET", "/");
if ($response->getStatusCode() == 200) {
// try to decode JSON
if ($json_data = json_decode($response->getBody(), true)) {
if ($json_data['version']) {
return $json_data['version'];
} else {
return "unknown";
}
} else {
throw new Exception\ServerException('JSON response not received or not understood');
}
}
} catch (\GuzzleHttp\Exception\ConnectException $e) {
throw new Exception\ServerException(
"Could not connect to database. Error: " . $e->getMessage(),
0,
$e
);
}
}
/**
* Get a list of databases
*
* @return array The database names
* @throws \PHPCouchDB\Exception\ServerException if there's a problem with parsing arguments or creating the client
*/
public function getAllDbs() : array
{
$response = $this->client->request("GET", "/_all_dbs");
if ($response->getStatusCode() == 200) {
// try to decode JSON
if ($json_data = json_decode($response->getBody(), true)) {
return $json_data;
} else {
throw new Exception\ServerException('JSON response not received or not understood');
}
}
}
/**
* Create and return a Database object to work with
*
* @param $options Supply the "name" (required) and an optional boolean
* "create_if_not_exists" value (default is false)
* @return \CouchDB\Database represents the named database
* @throws \PHPCouchDB\Exception\ServerException if there's a problem
* with parsing arguments or creating the database object (e.g. database
* doesn't exist and shouldn't be created)
*/
public function useDb($options) : Database
{
// check the $options array is sane
if (!isset($options[self::OPTION_NAME])) {
throw new Exception\ServerException(
'"name" is a required $options parameter'
);
} else {
$db_name = $options[self::OPTION_NAME];
}
if (isset($options[self::OPTION_CREATE_IF_NOT_EXISTS])) {
$create_if_not_exists = $options[self::OPTION_CREATE_IF_NOT_EXISTS];
} else {
// default value
$create_if_not_exists = false;
}
// does this database exist?
$exists = false;
try {
$response = $this->client->request("GET", "/" . $db_name);
if ($response->getStatusCode() == 200) {
$exists = true;
}
} catch (\GuzzleHttp\Exception\ClientException $e) {
// it doesn't exist, should we create it?
if ($create_if_not_exists) {
$create_response = $this->client->request("PUT", "/" . $db_name);
if ($create_response->getStatusCode() == 201) {
$exists = true;
}
}
}
if ($exists) {
return new Database($this->client, $db_name);
}
throw new Exception\ServerException(
'Database doesn\'t exist, include "create_if_not_exists" parameter to create it'
);
}
/**
* Create a database user and return the revision of the user record (for later updating)
*
* @author David Baltusavich <[email protected]>
* @param $username the new username
* @param $password the password to setup for the user
* @param optional $roles if you want to specify the roles on the server for the user. defaults to []
* @return string revision string
* @throws \PHPCouchDB\Exception\ServerException if there's a problem
*/
public function createUser($username, $password, $roles = [])
{
$doc = [
'name' => $username,
'password' => $password,
'roles' => $roles,
'type' => 'user',
];
try {
$response = $this->client->request("PUT", "/_users/org.couchdb.user:" . $username, ['json' => $doc]);
if ($response->getStatusCode() == 201 && $response_data = json_decode($response->getBody(), true)) {
return $response_data['_rev'];
}
else
{
throw new Exception\ServerException(
'Problem creating user'
);
}
}
catch (\GuzzleHttp\Exception\ClientException $e){
throw new Exception\ServerException(
'Connection or other Error:' . $e->getMessage()
);
}
}
/**
* Update a database user and return the revision of the user record (for further updating)
*
* @author David Baltusavich <[email protected]>
* @param $username the new username
* @param $password the new password
* @param optional $rev the revision of the current record (saves a query if you can specify this)
* @param optional $roles if you want to specify the roles on the server for the user. defaults to []
* @return string revision string
* @throws \PHPCouchDB\Exception\ServerException if there's a problem
*/
public function updateUser($username, $password, $rev = false, $roles = [])
{
$doc = [
'password' => $password,
'type' => 'user',
'name' => $username,
'roles' => $roles,
];
if(!$rev) {
try
{
$response = $this->client->request("GET", "/_users/org.couchdb.user:" . $username);
if ($response->getStatusCode() == 200 && $response_data = json_decode($response->getBody(), true)) {
$rev = $response_data['_rev'];
}
else
{
if($response->getStatusCode() == 404) {
throw new Exception\ServerException(
"Your connection doesn't have privileges to confirm the user, or the user does not exist"
);
}
throw new Exception\ServerException("Something went wrong: " . $response->getStatusCode());
}
}
catch (\GuzzleHttp\Exception\ClientException $e){
throw new Exception\ServerException(
'Could not retrieve the username provided'
);
}
}
try
{
$response = $this->client->request(
"PUT", "/_users/org.couchdb.user:" . $username, [
'json' => $doc,
'headers' => [
'If-Match' => $rev
],
]
);
if ($response->getStatusCode() == 201 && $response_data = json_decode($response->getBody(), true)) {
return $response_data['_rev'];
}
else
{
if($response->getStatusCode() == 409) {
throw new Exception\ServerException("Bad permissions");
}
throw new Exception\ServerException("Something went wrong: " . $response->getStatusCode());
}
}
catch (\GuzzleHttp\Exception\ClientException $e)
{
throw new Exception\ServerException(
"Failing Updating User: " . $e->getMessage() . "REV: $rev"
);
}
}
/**
* If you need to make a request that isn't supported by this library,
* use this method to get the client to use. Aimed at more advanced
* users/requirements
*/
public function getClient() : \GuzzleHttp\ClientInterface
{
return $this->client;
}
}