1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This program is free software: you can redistribute it and/or modify |
4
|
|
|
* it under the terms of the GNU General Public License as published by |
5
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
6
|
|
|
* (at your option) any later version. |
7
|
|
|
* |
8
|
|
|
* This program is distributed in the hope that it will be useful, |
9
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
10
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11
|
|
|
* GNU General Public License for more details. |
12
|
|
|
* |
13
|
|
|
* You should have received a copy of the GNU General Public License |
14
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
15
|
|
|
*/ |
16
|
|
|
namespace Filoucrackeur\Hubic\Utility; |
17
|
|
|
|
18
|
|
|
use Filoucrackeur\Hubic\Domain\Model\Account; |
19
|
|
|
use Filoucrackeur\Hubic\Service\OAuth2\Client; |
20
|
|
|
use TYPO3\CMS\Backend\Utility\BackendUtility; |
21
|
|
|
use TYPO3\CMS\Core\FormProtection\FormProtectionFactory; |
22
|
|
|
use TYPO3\CMS\Core\SingletonInterface; |
23
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
24
|
|
|
use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager; |
25
|
|
|
|
26
|
|
|
class ClientUtility implements SingletonInterface |
27
|
|
|
{ |
28
|
|
|
const AUTHORIZATION_ENDPOINT = 'https://api.hubic.com/oauth/auth/'; |
29
|
|
|
|
30
|
|
|
const TOKEN_ENDPOINT = 'https://api.hubic.com/oauth/token/'; |
31
|
|
|
|
32
|
|
|
const DOMAIN_API = 'https://api.hubic.com/'; |
33
|
|
|
|
34
|
|
|
const VERSION_API = '1.0'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var \Filoucrackeur\Hubic\Service\OAuth2\Client |
38
|
|
|
*/ |
39
|
|
|
protected $OAuth; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var \TYPO3\CMS\Extbase\Object\ObjectManager |
43
|
|
|
*/ |
44
|
|
|
protected $objectManager; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager |
48
|
|
|
*/ |
49
|
|
|
protected $persistenceManager; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var \Filoucrackeur\Hubic\Domain\Model\Account |
53
|
|
|
*/ |
54
|
|
|
protected $account; |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param ObjectManager $objectManager |
60
|
|
|
*/ |
61
|
|
|
public function injectAccount(ObjectManager $objectManager) { |
62
|
|
|
$this->objectManager = $objectManager; |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param PersistenceManager $persistenceManager |
67
|
|
|
*/ |
68
|
|
|
public function injectPersistenceManager(PersistenceManager $persistenceManager) { |
69
|
|
|
$this->persistenceManager = $persistenceManager; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param Account $account |
74
|
|
|
*/ |
75
|
|
|
public function injectAccount(Account $account) { |
76
|
|
|
$this->account = $account; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param Account $account |
81
|
|
|
* @return bool |
82
|
|
|
*/ |
83
|
|
|
public function callHubic(Account $account) : bool |
84
|
|
|
{ |
85
|
|
|
$this->account = $account; |
86
|
|
|
$this->OAuth = new Client($this->account->getClientId(), $this->account->getClientSecret()); |
87
|
|
|
$this->OAuth->setScope('usage.r,account.r,getAllLinks.r,credentials.r,sponsorCode.r,activate.w,sponsored.r,links.drw'); |
88
|
|
|
$this->OAuth->setAccessTokenType(1); |
89
|
|
|
|
90
|
|
|
if ($this->account->getAccessToken()) { |
91
|
|
|
$this->OAuth->setAccessToken($this->account->getAccessToken()); |
92
|
|
|
} else { |
93
|
|
|
if (isset($_GET['formToken'])) { |
94
|
|
|
$code = str_replace('code=', '', strstr(GeneralUtility::_GET('formToken'), "code=")); |
95
|
|
|
} |
96
|
|
|
if (isset($code)) { |
97
|
|
|
$params = array('code' => $code, 'redirect_uri' => $this->getRedirectUri($this->account)); |
98
|
|
|
$response = $this->OAuth->getAccessToken(self::TOKEN_ENDPOINT, 'authorization_code', $params); |
99
|
|
|
|
100
|
|
|
if( $response ){ |
|
|
|
|
101
|
|
|
if( $response['code'] == 200 ){ |
102
|
|
|
$this->account->setAccessToken($response['result']['access_token']); |
103
|
|
|
$this->persistenceManager->update($this->account); |
104
|
|
|
$this->persistenceManager->persistAll(); |
105
|
|
|
return true; |
106
|
|
|
} else { |
107
|
|
|
return false; |
108
|
|
|
} |
109
|
|
|
} else { |
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
return false; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function getAccount() |
118
|
|
|
{ |
119
|
|
|
$response = $this->OAuth->fetch(self::DOMAIN_API . self::VERSION_API . '/account'); |
120
|
|
|
return $response; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param Account $account |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
public function getRedirectUri(Account $account) |
128
|
|
|
{ |
129
|
|
|
|
130
|
|
|
$formProtection = FormProtectionFactory::get(); |
131
|
|
|
$formToken = $formProtection->generateToken('AuthorizationRequest'); |
132
|
|
|
|
133
|
|
|
return 'http://' . $_SERVER['HTTP_HOST'] . BackendUtility::getModuleUrl('tools_HubicHubic', [ |
134
|
|
|
'tx_hubic_tools_hubichubic' => [ |
135
|
|
|
'action' => 'authenticationResponse', |
136
|
|
|
'controller' => 'Backend\Account', |
137
|
|
|
'account' => $account->getUid() |
138
|
|
|
], |
139
|
|
|
'formToken' => $formToken |
140
|
|
|
]); |
141
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param Account $account |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
|
|
public function getAuthorizationRequestUrl(Account $account) |
149
|
|
|
{ |
150
|
|
|
$this->callHubic($account); |
151
|
|
|
$authUrl = $this->OAuth->getAuthenticationUrl(self::AUTHORIZATION_ENDPOINT, $this->getRedirectUri($account)); |
152
|
|
|
return $authUrl; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Get hubiC account Quota |
157
|
|
|
* |
158
|
|
|
* @see https://api.hubic.com/console/ |
159
|
|
|
* @return array |
160
|
|
|
*/ |
161
|
|
|
public function getAccountQuota() |
162
|
|
|
{ |
163
|
|
|
$response = $this->OAuth->fetch(self::DOMAIN_API . self::VERSION_API . '/account/usage'); |
164
|
|
|
return $response; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Get hubiC agreements |
169
|
|
|
* |
170
|
|
|
* @see https://api.hubic.com/console/ |
171
|
|
|
* @return array |
172
|
|
|
*/ |
173
|
|
|
public function getAgreement() |
174
|
|
|
{ |
175
|
|
|
$response = $this->OAuth->fetch(self::DOMAIN_API . self::VERSION_API . '/agreement'); |
176
|
|
|
return $response; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Get hubiC getAllLinks |
181
|
|
|
* |
182
|
|
|
* @see https://api.hubic.com/console/ |
183
|
|
|
* @return array |
184
|
|
|
*/ |
185
|
|
|
public function getAllLinks() |
186
|
|
|
{ |
187
|
|
|
$response = $this->OAuth->fetch(self::DOMAIN_API . self::VERSION_API . '/account/getAllLinks'); |
188
|
|
|
return $response; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @param Account $account |
193
|
|
|
*/ |
194
|
|
|
public function setAccount(Account $account) |
195
|
|
|
{ |
196
|
|
|
$this->account = $account; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
} |
200
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..