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\models; |
11
|
|
|
|
12
|
|
|
use gplcart\core\interfaces\Crud as CrudInterface; |
13
|
|
|
use gplcart\core\Config; |
14
|
|
|
use gplcart\core\Handler; |
15
|
|
|
use gplcart\core\Hook; |
16
|
|
|
use gplcart\core\models\Translation as TranslationModel; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Manages basic behaviors and data related Google API credentials |
20
|
|
|
*/ |
21
|
|
|
class Credential implements CrudInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Database class instance |
25
|
|
|
* @var \gplcart\core\Database $db |
26
|
|
|
*/ |
27
|
|
|
protected $db; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Hook class instance |
31
|
|
|
* @var \gplcart\core\Hook $hook |
32
|
|
|
*/ |
33
|
|
|
protected $hook; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Translation model instance |
37
|
|
|
* @var \gplcart\core\models\Translation $translation |
38
|
|
|
*/ |
39
|
|
|
protected $translation; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param Hook $hook |
43
|
|
|
* @param Config $config |
44
|
|
|
* @param TranslationModel $translation |
45
|
|
|
*/ |
46
|
|
|
public function __construct(Hook $hook, Config $config, TranslationModel $translation) |
47
|
|
|
{ |
48
|
|
|
$this->hook = $hook; |
49
|
|
|
$this->db = $config->getDb(); |
50
|
|
|
$this->translation = $translation; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Loads a credential |
55
|
|
|
* @param int $id |
56
|
|
|
* @return array |
57
|
|
|
*/ |
58
|
|
|
public function get($id) |
59
|
|
|
{ |
60
|
|
|
$sql = 'SELECT * FROM module_gapi_credential WHERE credential_id=?'; |
61
|
|
|
return $this->db->fetch($sql, array($id), array('unserialize' => 'data')); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Returns an array of credentials or counts them |
66
|
|
|
* @param array $options |
67
|
|
|
* @return array|integer |
68
|
|
|
*/ |
69
|
|
|
public function getList(array $options = array()) |
70
|
|
|
{ |
71
|
|
|
$sql = 'SELECT *'; |
72
|
|
|
|
73
|
|
|
if (!empty($options['count'])) { |
74
|
|
|
$sql = 'SELECT COUNT(credential_id)'; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$sql .= ' FROM module_gapi_credential WHERE credential_id IS NOT NULL'; |
78
|
|
|
|
79
|
|
|
$conditions = array(); |
80
|
|
|
|
81
|
|
|
if (isset($options['type'])) { |
82
|
|
|
$sql .= ' AND type=?'; |
83
|
|
|
$conditions[] = $options['type']; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$allowed_order = array('asc', 'desc'); |
87
|
|
|
$allowed_sort = array('name', 'credential_id', 'created', 'modified', 'type'); |
88
|
|
|
|
89
|
|
|
if (isset($options['sort']) |
90
|
|
|
&& in_array($options['sort'], $allowed_sort) |
91
|
|
|
&& isset($options['order']) |
92
|
|
|
&& in_array($options['order'], $allowed_order)) { |
93
|
|
|
$sql .= " ORDER BY {$options['sort']} {$options['order']}"; |
94
|
|
|
} else { |
95
|
|
|
$sql .= ' ORDER BY created DESC'; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if (!empty($options['limit'])) { |
99
|
|
|
$sql .= ' LIMIT ' . implode(',', array_map('intval', $options['limit'])); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if (empty($options['count'])) { |
103
|
|
|
$fetch_options = array('index' => 'credential_id', 'unserialize' => 'data'); |
104
|
|
|
$result = $this->db->fetchAll($sql, $conditions, $fetch_options); |
105
|
|
|
} else { |
106
|
|
|
$result = (int) $this->db->fetchColumn($sql, $conditions); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $result; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Adds a credential |
114
|
|
|
* @param array $data |
115
|
|
|
* @return int |
116
|
|
|
*/ |
117
|
|
|
public function add(array $data) |
118
|
|
|
{ |
119
|
|
|
$data['created'] = $data['modified'] = GC_TIME; |
120
|
|
|
return $this->db->insert('module_gapi_credential', $data); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Deletes a credential |
125
|
|
|
* @param integer $id |
126
|
|
|
* @return bool |
127
|
|
|
*/ |
128
|
|
|
public function delete($id) |
129
|
|
|
{ |
130
|
|
|
return (bool) $this->db->delete('module_gapi_credential', array('credential_id' => $id)); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Delete "Service" credential |
135
|
|
|
* @param int |
136
|
|
|
* @return bool |
137
|
|
|
*/ |
138
|
|
|
public function deleteService($id) |
139
|
|
|
{ |
140
|
|
|
$data = $this->get($id); |
141
|
|
|
|
142
|
|
|
if (empty($data)) { |
143
|
|
|
return false; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
if (!$this->delete($id)) { |
147
|
|
|
return false; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$file = gplcart_file_absolute($data['data']['file']); |
151
|
|
|
|
152
|
|
|
if (file_exists($file)) { |
153
|
|
|
unlink($file); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return true; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Updates a credential |
161
|
|
|
* @param integer $id |
162
|
|
|
* @param array $data |
163
|
|
|
* @return boolean |
164
|
|
|
*/ |
165
|
|
|
public function update($id, array $data) |
166
|
|
|
{ |
167
|
|
|
$data['modified'] = GC_TIME; |
168
|
|
|
return (bool) $this->db->update('module_gapi_credential', $data, array('credential_id' => $id)); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Returns an array of credential handlers |
173
|
|
|
* @return array |
174
|
|
|
*/ |
175
|
|
|
public function getHandlers() |
176
|
|
|
{ |
177
|
|
|
$handlers = array( |
178
|
|
|
'key' => array( |
179
|
|
|
'name' => $this->translation->text('API key'), |
180
|
|
|
'handlers' => array( |
181
|
|
|
'delete' => array(__CLASS__, 'delete'), |
182
|
|
|
'edit' => array('gplcart\\modules\\gapi\\controllers\\Credential', 'editKeyCredential') |
183
|
|
|
) |
184
|
|
|
), |
185
|
|
|
'oauth' => array( |
186
|
|
|
'name' => $this->translation->text('OAuth client ID'), |
187
|
|
|
'handlers' => array( |
188
|
|
|
'delete' => array(__CLASS__, 'delete'), |
189
|
|
|
'edit' => array('gplcart\\modules\\gapi\\controllers\\Credential', 'editOauthCredential') |
190
|
|
|
) |
191
|
|
|
), |
192
|
|
|
'service' => array( |
193
|
|
|
'name' => $this->translation->text('Service account key'), |
194
|
|
|
'handlers' => array( |
195
|
|
|
'delete' => array(__CLASS__, 'deleteService'), |
196
|
|
|
'edit' => array('gplcart\\modules\\gapi\\controllers\\Credential', 'editServiceCredential') |
197
|
|
|
) |
198
|
|
|
) |
199
|
|
|
); |
200
|
|
|
|
201
|
|
|
$this->hook->attach('module.gapi.credential.handlers', $handlers); |
202
|
|
|
return $handlers; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Call a credential handler |
207
|
|
|
* @param string $handler_id |
208
|
|
|
* @param string $name |
209
|
|
|
* @param array $arguments |
210
|
|
|
* @return mixed |
211
|
|
|
*/ |
212
|
|
|
public function callHandler($handler_id, $name, array $arguments = array()) |
213
|
|
|
{ |
214
|
|
|
return Handler::call($this->getHandlers(), $handler_id, $name, $arguments); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|