1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Directus – <http://getdirectus.com> |
5
|
|
|
* |
6
|
|
|
* @link The canonical repository – <https://github.com/directus/directus> |
7
|
|
|
* @copyright Copyright 2006-2016 RANGER Studio, LLC – <http://rangerstudio.com> |
8
|
|
|
* @license GNU General Public License (v3) – <http://www.gnu.org/copyleft/gpl.html> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Directus\SDK; |
12
|
|
|
use Directus\Util\ArrayUtils; |
13
|
|
|
use Directus\Util\StringUtils; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Client Remote |
17
|
|
|
* |
18
|
|
|
* @author Welling Guzmán <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class ClientRemote extends BaseClientRemote |
21
|
|
|
{ |
22
|
|
|
protected $baseEndpoint = 'http://localhost/api'; |
23
|
2 |
|
protected $hostedBaseEndpointFormat = 'https://%s.directus.io/api'; |
24
|
|
|
|
25
|
2 |
|
/** |
26
|
|
|
* @inheritdoc |
27
|
|
|
*/ |
28
|
4 |
|
public function getTables(array $params = []) |
29
|
|
|
{ |
30
|
4 |
|
return $this->performRequest('GET', static::TABLE_LIST_ENDPOINT); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
2 |
|
/** |
34
|
|
|
* @inheritdoc |
35
|
2 |
|
*/ |
36
|
|
|
public function getTable($tableName) |
37
|
|
|
{ |
38
|
2 |
|
return $this->performRequest('GET', static::TABLE_INFORMATION_ENDPOINT, $tableName); |
|
|
|
|
39
|
|
|
} |
40
|
2 |
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritdoc |
43
|
2 |
|
*/ |
44
|
|
|
public function getColumns($tableName, array $params = []) |
45
|
2 |
|
{ |
46
|
|
|
return $this->performRequest('GET', static::COLUMN_LIST_ENDPOINT, $tableName); |
|
|
|
|
47
|
|
|
} |
48
|
2 |
|
|
49
|
|
|
/** |
50
|
2 |
|
* @inheritdoc |
51
|
|
|
*/ |
52
|
|
|
public function getColumn($tableName, $columnName) |
53
|
2 |
|
{ |
54
|
|
|
return $this->performRequest('GET', static::COLUMN_INFORMATION_ENDPOINT, [$tableName, $columnName]); |
|
|
|
|
55
|
2 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
2 |
|
* @inheritdoc |
59
|
|
|
*/ |
60
|
2 |
|
public function getEntries($tableName, array $options = []) |
61
|
|
|
{ |
62
|
|
|
return $this->performRequest('GET', static::TABLE_ENTRIES_ENDPOINT, $tableName, null, $options); |
|
|
|
|
63
|
2 |
|
} |
64
|
|
|
|
65
|
2 |
|
/** |
66
|
|
|
* @inheritdoc |
67
|
|
|
*/ |
68
|
2 |
|
public function getEntry($tableName, $id, array $options = []) |
69
|
|
|
{ |
70
|
2 |
|
return $this->performRequest('GET', static::TABLE_ENTRY_ENDPOINT, [$tableName, $id]); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
2 |
|
/** |
74
|
|
|
* @inheritdoc |
75
|
2 |
|
*/ |
76
|
|
|
public function getUsers(array $params = []) |
77
|
|
|
{ |
78
|
2 |
|
return $this->getEntries('directus_users', $params); |
79
|
|
|
} |
80
|
2 |
|
|
81
|
|
|
/** |
82
|
|
|
* @inheritdoc |
83
|
2 |
|
*/ |
84
|
|
|
public function getUser($id, array $params = []) |
85
|
2 |
|
{ |
86
|
|
|
return $this->getEntry($id, 'directus_users', $params); |
87
|
|
|
} |
88
|
2 |
|
|
89
|
|
|
/** |
90
|
2 |
|
* @inheritdoc |
91
|
|
|
*/ |
92
|
|
|
public function getGroups() |
93
|
2 |
|
{ |
94
|
|
|
return $this->performRequest('GET', static::GROUP_LIST_ENDPOINT); |
|
|
|
|
95
|
2 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
2 |
|
* @inheritdoc |
99
|
|
|
*/ |
100
|
2 |
|
public function getGroup($groupID) |
101
|
|
|
{ |
102
|
|
|
return $this->performRequest('GET', static::GROUP_INFORMATION_ENDPOINT, $groupID); |
|
|
|
|
103
|
2 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @inheritdoc |
107
|
|
|
*/ |
108
|
|
|
public function getGroupPrivileges($groupID) |
109
|
|
|
{ |
110
|
|
|
return $this->performRequest('GET', static::GROUP_PRIVILEGES_ENDPOINT, $groupID); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @inheritdoc |
115
|
|
|
*/ |
116
|
|
|
public function getFiles() |
117
|
|
|
{ |
118
|
|
|
return $this->performRequest('GET', static::FILE_LIST_ENDPOINT); |
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @inheritdoc |
123
|
|
|
*/ |
124
|
|
|
public function getFile($fileID) |
125
|
|
|
{ |
126
|
|
|
return $this->performRequest('GET', static::FILE_INFORMATION_ENDPOINT, $fileID); |
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @inheritdoc |
131
|
|
|
*/ |
132
|
|
|
public function getSettings() |
133
|
|
|
{ |
134
|
|
|
return $this->performRequest('GET', static::SETTING_LIST_ENDPOINT); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @inheritdoc |
139
|
|
|
*/ |
140
|
|
|
public function getSettingsByCollection($collectionName) |
141
|
|
|
{ |
142
|
|
|
return $this->performRequest('GET', static::SETTING_COLLECTION_ENDPOINT, $collectionName); |
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @inheritdoc |
147
|
|
|
*/ |
148
|
|
|
public function getMessages($userId) |
149
|
|
|
{ |
150
|
|
|
return $this->performRequest('GET', static::MESSAGES_USER_ENDPOINT, $userId); |
|
|
|
|
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @inheritdoc |
155
|
|
|
*/ |
156
|
|
|
public function createEntry($tableName, array $data) |
157
|
|
|
{ |
158
|
|
|
return $this->performRequest('POST', static::TABLE_ENTRY_CREATE_ENDPOINT, $tableName, $data); |
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @inheritdoc |
163
|
|
|
*/ |
164
|
|
|
public function updateEntry($tableName, $id, array $data) |
165
|
|
|
{ |
166
|
|
|
return $this->performRequest('PUT', static::TABLE_ENTRY_UPDATE_ENDPOINT, [$tableName, $id], $data); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @inheritdoc |
171
|
|
|
*/ |
172
|
|
|
public function deleteEntry($tableName, $ids) |
173
|
|
|
{ |
174
|
|
|
return $this->performRequest('DELETE', static::TABLE_ENTRY_DELETE_ENDPOINT, [$tableName, $ids]); |
|
|
|
|
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @inheritdoc |
179
|
|
|
*/ |
180
|
|
View Code Duplication |
public function createUser(array $data) |
|
|
|
|
181
|
|
|
{ |
182
|
|
|
// @TODO: Add hooks |
183
|
|
|
if (ArrayUtils::has($data, 'password')) { |
184
|
|
|
// @NOTE: Use Directus password hash |
185
|
|
|
$data['password'] = password_hash($data['password'], PASSWORD_DEFAULT, ['cost' => 12]); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return $this->createEntry('directus_users', $data); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @inheritdoc |
193
|
|
|
*/ |
194
|
|
View Code Duplication |
public function updateUser($id, array $data) |
|
|
|
|
195
|
|
|
{ |
196
|
|
|
// @TODO: Add hooks |
197
|
|
|
if (ArrayUtils::has($data, 'password')) { |
198
|
|
|
// @NOTE: Use Directus password hash |
199
|
|
|
$data['password'] = password_hash($data['password'], PASSWORD_DEFAULT, ['cost' => 12]); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
return $this->updateEntry('directus_users', $id, $data); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @inheritdoc |
207
|
|
|
*/ |
208
|
|
|
public function deleteUser($ids) |
209
|
|
|
{ |
210
|
|
|
return $this->deleteEntry('directus_users', $ids); |
|
|
|
|
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @inheritdoc |
215
|
|
|
*/ |
216
|
|
|
public function createFile(array $data) |
217
|
|
|
{ |
218
|
|
|
$attributes = []; |
219
|
|
|
if (ArrayUtils::has($data, 'file')) { |
220
|
|
|
$path = $data['file']; |
221
|
|
|
$ext = pathinfo($path, PATHINFO_EXTENSION); |
222
|
|
|
$mimeType = mime_content_type($path); |
223
|
|
|
$attributes['name'] = pathinfo($path, PATHINFO_FILENAME) . '.' . $ext; |
224
|
|
|
$attributes['type'] = $mimeType; |
225
|
|
|
$content = file_get_contents($path); |
226
|
|
|
$base64 = 'data:' . $mimeType . ';base64,' . base64_encode($content); |
227
|
|
|
$attributes['data'] = $base64; |
228
|
|
|
unset($data['file']); |
229
|
|
|
} else if (ArrayUtils::has($data, 'data')) { |
230
|
|
|
$finfo = new \finfo(FILEINFO_MIME); |
231
|
|
|
list($mimeType, $charset) = explode('; charset=', $finfo->buffer($data['data'])); |
|
|
|
|
232
|
|
|
$base64 = 'data:' . $mimeType . ';base64,' . base64_encode($data['data']); |
233
|
|
|
list($type, $subtype) = explode('/', $mimeType); |
|
|
|
|
234
|
|
|
$attributes['data'] = $base64; |
235
|
|
|
$attributes['type'] = $mimeType; |
236
|
|
|
$attributes['name'] = StringUtils::randomString() . '.' . $subtype; |
237
|
|
|
unset($data['data']); |
238
|
|
|
} else { |
239
|
|
|
throw new \Exception('Missing "file" or "data" attribute.'); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
$data = array_merge($data, $attributes); |
243
|
|
|
|
244
|
|
|
return $this->performRequest('POST', static::FILE_CREATE_ENDPOINT, null, $data); |
|
|
|
|
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @inheritdoc |
249
|
|
|
*/ |
250
|
|
|
public function updateFile($id, array $data) |
251
|
|
|
{ |
252
|
|
|
return $this->updateEntry('directus_files', $id, $data); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @inheritdoc |
257
|
|
|
*/ |
258
|
|
|
public function deleteFile($ids) |
259
|
|
|
{ |
260
|
|
|
return $this->deleteEntry('directus_files', $ids); |
|
|
|
|
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|