1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of EC-CUBE |
5
|
|
|
* |
6
|
|
|
* Copyright(c) LOCKON CO.,LTD. All Rights Reserved. |
7
|
|
|
* |
8
|
|
|
* http://www.lockon.co.jp/ |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Eccube\Service; |
15
|
|
|
|
16
|
|
|
use Eccube\Common\Constant; |
17
|
|
|
use Eccube\Common\EccubeConfig; |
18
|
|
|
use Eccube\Entity\BaseInfo; |
19
|
|
|
use Eccube\Repository\BaseInfoRepository; |
20
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
21
|
|
|
|
22
|
|
|
class PluginApiService |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Url for Api |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $apiUrl; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var EccubeConfig |
33
|
|
|
*/ |
34
|
|
|
private $eccubeConfig; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var RequestStack |
38
|
|
|
*/ |
39
|
|
|
private $requestStack; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var BaseInfo |
43
|
|
|
*/ |
44
|
|
|
private $BaseInfo; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* PluginApiService constructor. |
48
|
|
|
* |
49
|
|
|
* @param EccubeConfig $eccubeConfig |
50
|
|
|
* @param RequestStack $requestStack |
51
|
|
|
* @param BaseInfoRepository $baseInfoRepository |
52
|
|
|
* @throws \Doctrine\ORM\NoResultException |
53
|
|
|
* @throws \Doctrine\ORM\NonUniqueResultException |
54
|
|
|
*/ |
55
|
|
|
public function __construct(EccubeConfig $eccubeConfig, RequestStack $requestStack, BaseInfoRepository $baseInfoRepository) |
56
|
|
|
{ |
57
|
|
|
$this->eccubeConfig = $eccubeConfig; |
58
|
|
|
$this->requestStack = $requestStack; |
59
|
|
|
$this->BaseInfo = $baseInfoRepository->get(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return mixed |
64
|
|
|
*/ |
65
|
|
|
public function getApiUrl() |
66
|
|
|
{ |
67
|
|
|
if (empty($this->apiUrl)) { |
68
|
|
|
return $this->eccubeConfig->get('eccube_package_repo_url'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $this->apiUrl; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param mixed $apiUrl |
76
|
|
|
*/ |
77
|
|
|
public function setApiUrl($apiUrl) |
78
|
|
|
{ |
79
|
|
|
$this->apiUrl = $apiUrl; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get master data: category |
84
|
|
|
* |
85
|
|
|
* @return array($result, $info) |
|
|
|
|
86
|
|
|
*/ |
87
|
|
|
public function getCategory() |
88
|
|
|
{ |
89
|
|
|
$urlCategory = $this->getApiUrl().'/category'; |
90
|
|
|
|
91
|
|
|
return $this->getRequestApi($urlCategory); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get plugins list |
96
|
|
|
* |
97
|
|
|
* @param array $data |
98
|
|
|
* |
99
|
|
|
* @return array($result, $info) |
|
|
|
|
100
|
|
|
*/ |
101
|
|
|
public function getPlugins($data) |
102
|
|
|
{ |
103
|
|
|
$url = $this->getApiUrl().'/plugins'; |
104
|
|
|
$params['category_id'] = $data['category_id']; |
|
|
|
|
105
|
|
|
$params['price_type'] = empty($data['price_type']) ? 'all' : $data['price_type']; |
106
|
|
|
$params['keyword'] = $data['keyword']; |
107
|
|
|
$params['sort'] = $data['sort']; |
108
|
|
|
$params['page'] = (isset($data['page_no']) && !empty($data['page_no'])) ? $data['page_no'] : 1; |
109
|
|
|
$params['per_page'] = (isset($data['page_count']) && !empty($data['page_count'])) ? $data['page_count'] : $this->eccubeConfig->get('eccube_default_page_count'); |
110
|
|
|
|
111
|
|
|
return $this->getRequestApi($url, $params); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Get purchased plugins list |
116
|
|
|
* |
117
|
|
|
* @return array($result, $info) |
|
|
|
|
118
|
|
|
*/ |
119
|
|
|
public function getPurchased() |
120
|
|
|
{ |
121
|
|
|
$url = $this->getApiUrl().'/plugins/purchased'; |
122
|
|
|
return $this->getRequestApi($url); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Get a plugin |
127
|
|
|
* |
128
|
|
|
* @param int|string $id Id or plugin code |
129
|
|
|
* |
130
|
|
|
* @return array [$result, $info] |
131
|
|
|
*/ |
132
|
|
|
public function getPlugin($id) |
133
|
|
|
{ |
134
|
|
|
$url = $this->getApiUrl().'/plugin/'.$id; |
135
|
|
|
|
136
|
|
|
return $this->getRequestApi($url); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Get captcha image |
141
|
|
|
* |
142
|
|
|
* @return array($result, $info) |
|
|
|
|
143
|
|
|
*/ |
144
|
|
|
public function getCaptcha() |
145
|
|
|
{ |
146
|
|
|
$apiUrl = $this->getApiUrl().'/captcha'; |
147
|
|
|
|
148
|
|
|
$requestApi = $this->getRequestApi($apiUrl); |
149
|
|
|
|
150
|
|
|
return $requestApi; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Get api key from captcha image |
155
|
|
|
* |
156
|
|
|
* @param array $data |
157
|
|
|
* |
158
|
|
|
* @return array($result, $info) |
|
|
|
|
159
|
|
|
*/ |
160
|
|
|
public function postApiKey($data) |
161
|
|
|
{ |
162
|
|
|
$apiUrl = $this->getApiUrl().'/api_key'; |
163
|
|
|
|
164
|
|
|
$baseUrl = $this->requestStack->getCurrentRequest()->getSchemeAndHttpHost().$this->requestStack->getCurrentRequest()->getBasePath(); |
165
|
|
|
$data['eccube_url'] = $baseUrl; |
166
|
|
|
$data['eccube_version'] = Constant::VERSION; |
167
|
|
|
|
168
|
|
|
$requestApi = $this->postRequestApi($apiUrl, $data); |
169
|
|
|
|
170
|
|
|
return $requestApi; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* API post |
175
|
|
|
* |
176
|
|
|
* @param string $url |
177
|
|
|
* @param array $data |
178
|
|
|
* |
179
|
|
|
* @return array($result, $info) |
|
|
|
|
180
|
|
|
*/ |
181
|
|
|
public function postRequestApi($url, $data = []) |
182
|
|
|
{ |
183
|
|
|
$curl = curl_init($url); |
184
|
|
|
curl_setopt($curl, CURLOPT_POST, 1); |
185
|
|
|
|
186
|
|
|
if (count($data) > 0) { |
187
|
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
// Todo: will implement after server worked |
191
|
|
|
$key = null; |
192
|
|
|
$baseUrl = $this->requestStack->getCurrentRequest()->getSchemeAndHttpHost().$this->requestStack->getCurrentRequest()->getBasePath(); |
193
|
|
|
// Option array |
194
|
|
|
$options = [ |
195
|
|
|
// HEADER |
196
|
|
|
CURLOPT_HTTPHEADER => [ |
197
|
|
|
'X-ECCUBE-KEY: '.base64_encode($key), |
198
|
|
|
'X-ECCUBE-URL: '.base64_encode($baseUrl), |
199
|
|
|
'X-ECCUBE-VERSION: '.base64_encode(Constant::VERSION), |
200
|
|
|
], |
201
|
|
|
CURLOPT_HTTPGET => true, |
202
|
|
|
CURLOPT_SSL_VERIFYPEER => false, |
203
|
|
|
CURLOPT_RETURNTRANSFER => true, |
204
|
|
|
CURLOPT_FAILONERROR => true, |
205
|
|
|
CURLOPT_CAINFO => \Composer\CaBundle\CaBundle::getSystemCaRootBundlePath(), |
206
|
|
|
]; |
207
|
|
|
|
208
|
|
|
// Set option value |
209
|
|
|
curl_setopt_array($curl, $options); |
210
|
|
|
$result = curl_exec($curl); |
211
|
|
|
$info = curl_getinfo($curl); |
212
|
|
|
$message = curl_error($curl); |
213
|
|
|
$info['message'] = $message; |
214
|
|
|
curl_close($curl); |
215
|
|
|
|
216
|
|
|
log_info('http get_info', $info); |
217
|
|
|
|
218
|
|
|
return [$result, $info]; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* API request processing |
223
|
|
|
* |
224
|
|
|
* @param string $url |
225
|
|
|
* @param array $data |
226
|
|
|
* |
227
|
|
|
* @return array($result, $info) |
|
|
|
|
228
|
|
|
*/ |
229
|
|
|
public function getRequestApi($url, $data = []) |
230
|
|
|
{ |
231
|
|
|
if (count($data) > 0) { |
232
|
|
|
$url .= '?'.http_build_query($data); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
$curl = curl_init($url); |
236
|
|
|
|
237
|
|
|
$key = $this->BaseInfo->getAuthenticationKey(); |
238
|
|
|
$baseUrl = $this->requestStack->getCurrentRequest()->getSchemeAndHttpHost().$this->requestStack->getCurrentRequest()->getBasePath(); |
239
|
|
|
|
240
|
|
|
// Option array |
241
|
|
|
$options = [ |
242
|
|
|
// HEADER |
243
|
|
|
CURLOPT_HTTPHEADER => [ |
244
|
|
|
'X-ECCUBE-KEY: '.$key, |
245
|
|
|
'X-ECCUBE-URL: '.$baseUrl, |
246
|
|
|
'X-ECCUBE-VERSION: '.Constant::VERSION, |
247
|
|
|
], |
248
|
|
|
CURLOPT_HTTPGET => true, |
249
|
|
|
CURLOPT_SSL_VERIFYPEER => false, |
250
|
|
|
CURLOPT_RETURNTRANSFER => true, |
251
|
|
|
CURLOPT_FAILONERROR => true, |
252
|
|
|
CURLOPT_CAINFO => \Composer\CaBundle\CaBundle::getSystemCaRootBundlePath(), |
253
|
|
|
]; |
254
|
|
|
|
255
|
|
|
// Set option value |
256
|
|
|
curl_setopt_array($curl, $options); |
257
|
|
|
$result = curl_exec($curl); |
258
|
|
|
$info = curl_getinfo($curl); |
259
|
|
|
$message = curl_error($curl); |
260
|
|
|
$info['message'] = $message; |
261
|
|
|
curl_close($curl); |
262
|
|
|
|
263
|
|
|
log_info('http get_info', $info); |
264
|
|
|
|
265
|
|
|
return [$result, $info]; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Get message |
270
|
|
|
* |
271
|
|
|
* @param $info |
272
|
|
|
* |
273
|
|
|
* @return string |
274
|
|
|
*/ |
275
|
|
View Code Duplication |
public function getResponseErrorMessage($info) |
|
|
|
|
276
|
|
|
{ |
277
|
|
|
if (!empty($info)) { |
278
|
|
|
$statusCode = $info['http_code']; |
279
|
|
|
$message = $info['message']; |
280
|
|
|
$message = $statusCode.' : '.$message; |
281
|
|
|
} else { |
282
|
|
|
$message = trans('ownerstore.text.error.timeout'); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
return $message; |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.