|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package Google API |
|
5
|
|
|
* @author Iurii Makukh <[email protected]> |
|
6
|
|
|
* @copyright Copyright (c) 2015, Iurii Makukh |
|
7
|
|
|
* @license https://www.gnu.org/licenses/gpl.html GNU/GPLv3 |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace gplcart\modules\gapi\controllers; |
|
11
|
|
|
|
|
12
|
|
|
use gplcart\core\controllers\backend\Controller; |
|
13
|
|
|
use gplcart\core\models\FileTransfer; |
|
14
|
|
|
use gplcart\modules\gapi\models\Credential as CredentialModel; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Handles incoming requests and outputs data related to Google API credentials |
|
18
|
|
|
*/ |
|
19
|
|
|
class Credential extends Controller |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Credential model class instance |
|
24
|
|
|
* @var \gplcart\modules\gapi\models\Credential $credential |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $credential; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* File transfer module instance |
|
30
|
|
|
* @var \gplcart\core\models\FileTransfer $file_transfer |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $file_transfer; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Pager limit |
|
36
|
|
|
* @var array |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $data_limit; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* The current updating credential |
|
42
|
|
|
* @var array |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $data_credential = array(); |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Credential type |
|
48
|
|
|
* @var string |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $data_type; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param CredentialModel $credential |
|
54
|
|
|
* @param FileTransfer $file_transfer |
|
55
|
|
|
*/ |
|
56
|
|
|
public function __construct(CredentialModel $credential, FileTransfer $file_transfer) |
|
57
|
|
|
{ |
|
58
|
|
|
parent::__construct(); |
|
59
|
|
|
|
|
60
|
|
|
$this->credential = $credential; |
|
61
|
|
|
$this->file_transfer = $file_transfer; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Route page callback |
|
66
|
|
|
* Displays the add credential page |
|
67
|
|
|
* @param string $type |
|
68
|
|
|
*/ |
|
69
|
|
|
public function addCredential($type) |
|
70
|
|
|
{ |
|
71
|
|
|
$this->data_type = $type; |
|
72
|
|
|
|
|
73
|
|
|
$this->setTitleEditCredential(); |
|
74
|
|
|
$this->setBreadcrumbEditCredential(); |
|
75
|
|
|
|
|
76
|
|
|
$this->credential->callHandler($type, 'edit'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Handler callback |
|
81
|
|
|
* Edit "API key" credential page |
|
82
|
|
|
*/ |
|
83
|
|
|
public function editKeyCredential() |
|
84
|
|
|
{ |
|
85
|
|
|
if ($this->validateKeyCredential()) { |
|
86
|
|
|
$this->saveCredential(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$this->output('gapi|credential/edit/key'); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Handler callback |
|
94
|
|
|
* Edit "OAuth" credential page |
|
95
|
|
|
*/ |
|
96
|
|
|
public function editOauthCredential() |
|
97
|
|
|
{ |
|
98
|
|
|
if ($this->validateOauthCredential()) { |
|
99
|
|
|
$this->saveCredential(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$this->output('gapi|credential/edit/oauth'); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Handler callback |
|
107
|
|
|
* Edit "Service" credential page |
|
108
|
|
|
*/ |
|
109
|
|
|
public function editServiceCredential() |
|
110
|
|
|
{ |
|
111
|
|
|
if ($this->validateServiceCredential()) { |
|
112
|
|
|
$this->saveCredential(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$this->output('gapi|credential/edit/service'); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Route page callback |
|
120
|
|
|
* Displays the edit credential page |
|
121
|
|
|
* @param int $credential_id |
|
122
|
|
|
*/ |
|
123
|
|
|
public function editCredential($credential_id) |
|
124
|
|
|
{ |
|
125
|
|
|
$this->setCredential($credential_id); |
|
126
|
|
|
$this->setTitleEditCredential(); |
|
127
|
|
|
$this->setBreadcrumbEditCredential(); |
|
128
|
|
|
|
|
129
|
|
|
$this->setData('credential', $this->data_credential); |
|
130
|
|
|
|
|
131
|
|
|
$this->submitDeleteCredential(); |
|
132
|
|
|
$this->credential->callHandler($this->data_credential['type'], 'edit'); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Sets the credential data |
|
137
|
|
|
* @param $credential_id |
|
138
|
|
|
*/ |
|
139
|
|
|
protected function setCredential($credential_id) |
|
140
|
|
|
{ |
|
141
|
|
|
if (is_numeric($credential_id)) { |
|
142
|
|
|
$this->data_credential = $this->credential->get($credential_id); |
|
143
|
|
|
if (empty($this->data_credential)) { |
|
144
|
|
|
$this->outputHttpStatus(404); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Set titles on the edit credential page |
|
151
|
|
|
*/ |
|
152
|
|
|
protected function setTitleEditCredential() |
|
153
|
|
|
{ |
|
154
|
|
|
if (isset($this->data_credential['name'])) { |
|
155
|
|
|
$text = $this->text('Edit %name', array('%name' => $this->data_credential['name'])); |
|
156
|
|
|
} else { |
|
157
|
|
|
$text = $this->text('Add credential'); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
$this->setTitle($text); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Set breadcrumbs on the edit credential page |
|
165
|
|
|
*/ |
|
166
|
|
|
protected function setBreadcrumbEditCredential() |
|
167
|
|
|
{ |
|
168
|
|
|
$breadcrumbs = array(); |
|
169
|
|
|
|
|
170
|
|
|
$breadcrumbs[] = array( |
|
171
|
|
|
'url' => $this->url('admin'), |
|
172
|
|
|
'text' => $this->text('Dashboard') |
|
173
|
|
|
); |
|
174
|
|
|
|
|
175
|
|
|
$breadcrumbs[] = array( |
|
176
|
|
|
'text' => $this->text('Google API credentials'), |
|
177
|
|
|
'url' => $this->url('admin/report/gapi') |
|
178
|
|
|
); |
|
179
|
|
|
|
|
180
|
|
|
$this->setBreadcrumbs($breadcrumbs); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Validates "API key" credential data |
|
185
|
|
|
* @return bool |
|
186
|
|
|
*/ |
|
187
|
|
|
protected function validateKeyCredential() |
|
188
|
|
|
{ |
|
189
|
|
|
if (!$this->isPosted('save')) { |
|
190
|
|
|
return false; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
$this->setSubmitted('credential'); |
|
194
|
|
|
$this->validateElement(array('name' => $this->text('Name')), 'length', array(1, 255)); |
|
195
|
|
|
$this->validateElement(array('data.key' => $this->text('Key')), 'length', array(1, 255)); |
|
196
|
|
|
|
|
197
|
|
|
return !$this->hasErrors(); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Validates "OAuth" credential data |
|
202
|
|
|
* @return bool |
|
203
|
|
|
*/ |
|
204
|
|
|
protected function validateOauthCredential() |
|
205
|
|
|
{ |
|
206
|
|
|
if (!$this->isPosted('save')) { |
|
207
|
|
|
return false; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
$this->setSubmitted('credential'); |
|
211
|
|
|
$this->validateElement(array('name' => $this->text('Name')), 'length', array(1, 255)); |
|
212
|
|
|
$this->validateElement(array('data.id' => $this->text('Client ID')), 'length', array(1, 255)); |
|
213
|
|
|
$this->validateElement(array('data.secret' => $this->text('Client secret')), 'length', array(1, 255)); |
|
214
|
|
|
|
|
215
|
|
|
return !$this->hasErrors(); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Validates "Service" credential data |
|
220
|
|
|
* @return bool |
|
221
|
|
|
*/ |
|
222
|
|
|
protected function validateServiceCredential() |
|
223
|
|
|
{ |
|
224
|
|
|
if (!$this->isPosted('save')) { |
|
225
|
|
|
return false; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
$this->setSubmitted('credential'); |
|
229
|
|
|
$this->validateElement(array('name' => $this->text('Name')), 'length', array(1, 255)); |
|
230
|
|
|
$this->validateFileUploadCredential(); |
|
231
|
|
|
|
|
232
|
|
|
return !$this->hasErrors(); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* Validates JSON file upload |
|
237
|
|
|
* @return bool |
|
238
|
|
|
*/ |
|
239
|
|
|
protected function validateFileUploadCredential() |
|
240
|
|
|
{ |
|
241
|
|
|
$upload = $this->request->file('file'); |
|
242
|
|
|
|
|
243
|
|
|
if (empty($upload)) { |
|
244
|
|
|
|
|
245
|
|
|
if (!isset($this->data_credential['credential_id'])) { |
|
246
|
|
|
$this->setError('data.file', $this->text('File is required')); |
|
247
|
|
|
return false; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
return null; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
if ($this->isError()) { |
|
254
|
|
|
return null; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
$result = $this->file_transfer->upload($upload, false, gplcart_file_private_module('gapi')); |
|
258
|
|
|
|
|
259
|
|
|
if ($result !== true) { |
|
260
|
|
|
$this->setError('data.file', $result); |
|
261
|
|
|
return false; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
$file = $this->file_transfer->getTransferred(); |
|
265
|
|
|
$decoded = json_decode(file_get_contents($file), true); |
|
266
|
|
|
|
|
267
|
|
|
if (empty($decoded['private_key'])) { |
|
268
|
|
|
unlink($file); |
|
269
|
|
|
$this->setError('data.file', $this->text('Failed to read JSON file')); |
|
270
|
|
|
return false; |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
if (isset($this->data_credential['data']['file'])) { |
|
274
|
|
|
$existing = gplcart_file_absolute($this->data_credential['data']['file']); |
|
275
|
|
|
if (file_exists($existing)) { |
|
276
|
|
|
unlink($existing); |
|
277
|
|
|
} |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
$this->setSubmitted('data.file', gplcart_file_relative($file)); |
|
281
|
|
|
return true; |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* Saves the submitted credential data |
|
286
|
|
|
*/ |
|
287
|
|
|
protected function saveCredential() |
|
288
|
|
|
{ |
|
289
|
|
|
if (isset($this->data_credential['credential_id'])) { |
|
290
|
|
|
$this->updateSubmittedCredential(); |
|
291
|
|
|
} else { |
|
292
|
|
|
$this->addSubmittedCredential(); |
|
293
|
|
|
} |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
/** |
|
297
|
|
|
* Updates a submitted credential |
|
298
|
|
|
*/ |
|
299
|
|
|
protected function updateSubmittedCredential() |
|
300
|
|
|
{ |
|
301
|
|
|
$this->controlAccess('module_gapi_credential_edit'); |
|
302
|
|
|
|
|
303
|
|
|
if ($this->credential->update($this->data_credential['credential_id'], $this->getSubmitted())) { |
|
304
|
|
|
$this->redirect('admin/report/gapi', $this->text('Credential has been updated'), 'success'); |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
$this->redirect('', $this->text('Credential has not been updated'), 'warning'); |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
/** |
|
311
|
|
|
* Adds a submitted credential |
|
312
|
|
|
*/ |
|
313
|
|
|
protected function addSubmittedCredential() |
|
314
|
|
|
{ |
|
315
|
|
|
$this->controlAccess('module_gapi_credential_add'); |
|
316
|
|
|
|
|
317
|
|
|
$submitted = $this->getSubmitted(); |
|
318
|
|
|
$submitted['type'] = $this->data_type; |
|
319
|
|
|
|
|
320
|
|
|
if ($this->credential->add($submitted)) { |
|
321
|
|
|
$this->redirect('admin/report/gapi', $this->text('Credential has been added'), 'success'); |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
|
|
$this->redirect('', $this->text('Credential has not been added'), 'warning'); |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
/** |
|
328
|
|
|
* Delete a submitted credential |
|
329
|
|
|
*/ |
|
330
|
|
|
protected function submitDeleteCredential() |
|
331
|
|
|
{ |
|
332
|
|
|
if ($this->isPosted('delete') && isset($this->data_credential['credential_id'])) { |
|
333
|
|
|
|
|
334
|
|
|
$this->controlAccess('module_gapi_credential_delete'); |
|
335
|
|
|
|
|
336
|
|
|
if ($this->credential->callHandler($this->data_credential['type'], 'delete', array( |
|
337
|
|
|
$this->data_credential['credential_id']))) { |
|
338
|
|
|
$this->redirect('admin/report/gapi', $this->text('Credential has been deleted'), 'success'); |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
$this->redirect('', $this->text('Credential has not been deleted'), 'warning'); |
|
342
|
|
|
} |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
|
|
/** |
|
346
|
|
|
* Route callback |
|
347
|
|
|
* Displays the credential overview page |
|
348
|
|
|
*/ |
|
349
|
|
|
public function listCredential() |
|
350
|
|
|
{ |
|
351
|
|
|
$this->actionListCredential(); |
|
352
|
|
|
$this->setTitleListCredential(); |
|
353
|
|
|
$this->setBreadcrumbListCredential(); |
|
354
|
|
|
$this->setFilterListCredential(); |
|
355
|
|
|
$this->setPagerListCredential(); |
|
356
|
|
|
|
|
357
|
|
|
$this->setData('credentials', $this->getListCredential()); |
|
358
|
|
|
$this->setData('handlers', $this->credential->getHandlers()); |
|
359
|
|
|
$this->outputListCredential(); |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
|
|
/** |
|
363
|
|
|
* Applies an action to the selected credentials |
|
364
|
|
|
*/ |
|
365
|
|
|
protected function actionListCredential() |
|
366
|
|
|
{ |
|
367
|
|
|
list($selected, $action) = $this->getPostedAction(); |
|
368
|
|
|
|
|
369
|
|
|
$deleted = 0; |
|
370
|
|
|
|
|
371
|
|
|
foreach ($selected as $id) { |
|
372
|
|
|
if ($action === 'delete' && $this->access('module_gapi_credential_delete')) { |
|
373
|
|
|
$credential = $this->credential->get($id); |
|
374
|
|
|
$deleted += (int) $this->credential->callHandler($credential['type'], 'delete', array( |
|
375
|
|
|
$credential['credential_id'])); |
|
376
|
|
|
} |
|
377
|
|
|
} |
|
378
|
|
|
|
|
379
|
|
|
if ($deleted > 0) { |
|
380
|
|
|
$message = $this->text('Deleted %num item(s)', array('%num' => $deleted)); |
|
381
|
|
|
$this->setMessage($message, 'success'); |
|
382
|
|
|
} |
|
383
|
|
|
} |
|
384
|
|
|
|
|
385
|
|
|
/** |
|
386
|
|
|
* Sets filter parameters |
|
387
|
|
|
*/ |
|
388
|
|
|
protected function setFilterListCredential() |
|
389
|
|
|
{ |
|
390
|
|
|
$this->setFilter(array('created', 'modified', 'name', 'credential_id', 'type')); |
|
391
|
|
|
} |
|
392
|
|
|
|
|
393
|
|
|
/** |
|
394
|
|
|
* Sets pager |
|
395
|
|
|
* @return array |
|
396
|
|
|
*/ |
|
397
|
|
|
protected function setPagerListCredential() |
|
398
|
|
|
{ |
|
399
|
|
|
$options = $this->query_filter; |
|
400
|
|
|
$options['count'] = true; |
|
401
|
|
|
|
|
402
|
|
|
$pager = array( |
|
403
|
|
|
'query' => $this->query_filter, |
|
404
|
|
|
'total' => (int) $this->credential->getList($options) |
|
405
|
|
|
); |
|
406
|
|
|
|
|
407
|
|
|
return $this->data_limit = $this->setPager($pager); |
|
408
|
|
|
} |
|
409
|
|
|
|
|
410
|
|
|
/** |
|
411
|
|
|
* Returns an array of credentials |
|
412
|
|
|
* @return array |
|
413
|
|
|
*/ |
|
414
|
|
|
protected function getListCredential() |
|
415
|
|
|
{ |
|
416
|
|
|
$options = $this->query_filter; |
|
417
|
|
|
$options['limit'] = $this->data_limit; |
|
418
|
|
|
|
|
419
|
|
|
return $this->credential->getList($options); |
|
420
|
|
|
} |
|
421
|
|
|
|
|
422
|
|
|
/** |
|
423
|
|
|
* Sets title on the credential overview page |
|
424
|
|
|
*/ |
|
425
|
|
|
protected function setTitleListCredential() |
|
426
|
|
|
{ |
|
427
|
|
|
$this->setTitle($this->text('Google API credentials')); |
|
428
|
|
|
} |
|
429
|
|
|
|
|
430
|
|
|
/** |
|
431
|
|
|
* Sets breadcrumbs on the credential overview page |
|
432
|
|
|
*/ |
|
433
|
|
|
protected function setBreadcrumbListCredential() |
|
434
|
|
|
{ |
|
435
|
|
|
$breadcrumb = array( |
|
436
|
|
|
'url' => $this->url('admin'), |
|
437
|
|
|
'text' => $this->text('Dashboard') |
|
438
|
|
|
); |
|
439
|
|
|
|
|
440
|
|
|
$this->setBreadcrumb($breadcrumb); |
|
441
|
|
|
} |
|
442
|
|
|
|
|
443
|
|
|
/** |
|
444
|
|
|
* Render and output the credential overview page |
|
445
|
|
|
*/ |
|
446
|
|
|
protected function outputListCredential() |
|
447
|
|
|
{ |
|
448
|
|
|
$this->output('gapi|credential/list'); |
|
449
|
|
|
} |
|
450
|
|
|
|
|
451
|
|
|
} |
|
452
|
|
|
|