|
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
|
|
|
|
|
24
|
|
|
class ClientUtility implements SingletonInterface |
|
25
|
|
|
{ |
|
26
|
|
|
const AUTHORIZATION_ENDPOINT = 'https://api.hubic.com/oauth/auth/'; |
|
27
|
|
|
|
|
28
|
|
|
const TOKEN_ENDPOINT = 'https://api.hubic.com/oauth/token/'; |
|
29
|
|
|
|
|
30
|
|
|
const DOMAIN_API = 'https://api.hubic.com/'; |
|
31
|
|
|
|
|
32
|
|
|
const VERSION_API = '1.0'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var \Filoucrackeur\Hubic\Service\OAuth2\Client |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $OAuth; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var \TYPO3\CMS\Extbase\Object\ObjectManager |
|
41
|
|
|
* @inject |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $objectManager; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager |
|
47
|
|
|
* @inject |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $persistenceManager; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var \Filoucrackeur\Hubic\Domain\Model\Account |
|
53
|
|
|
* @inject |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $account; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param Account $account |
|
59
|
|
|
* @return bool |
|
60
|
|
|
*/ |
|
61
|
|
|
public function callHubic(Account $account) { |
|
62
|
|
|
$this->account = $account; |
|
63
|
|
|
$this->OAuth = new Client($this->account->getClientId(), $this->account->getClientSecret()); |
|
64
|
|
|
$this->OAuth->setScope('usage.r,account.r,getAllLinks.r,credentials.r,sponsorCode.r,activate.w,sponsored.r,links.drw'); |
|
65
|
|
|
$this->OAuth->setAccessTokenType(1); |
|
66
|
|
|
|
|
67
|
|
|
if ($this->account->getAccessToken()) { |
|
68
|
|
|
$this->OAuth->setAccessToken($this->account->getAccessToken()); |
|
69
|
|
|
} else { |
|
70
|
|
|
if (isset($_GET['formToken'])) { |
|
71
|
|
|
$code = str_replace('code=', '', strstr($_GET['formToken'], "code=")); |
|
72
|
|
|
} |
|
73
|
|
|
if (isset($code)) { |
|
74
|
|
|
$params = array('code' => $code, 'redirect_uri' => $this->getRedirectUri($this->account)); |
|
75
|
|
|
$response = $this->OAuth->getAccessToken(self::TOKEN_ENDPOINT, 'authorization_code', $params); |
|
76
|
|
|
|
|
77
|
|
|
if( $response ){ |
|
|
|
|
|
|
78
|
|
|
if( $response['code'] == 200 ){ |
|
79
|
|
|
$this->account->setAccessToken($response['result']['access_token']); |
|
80
|
|
|
$this->persistenceManager->update($this->account); |
|
81
|
|
|
$this->persistenceManager->persistAll(); |
|
82
|
|
|
return true; |
|
83
|
|
|
}else { |
|
84
|
|
|
return false; |
|
85
|
|
|
} |
|
86
|
|
|
} else { |
|
87
|
|
|
return false; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
return false; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function getAccount() |
|
95
|
|
|
{ |
|
96
|
|
|
$response = $this->OAuth->fetch(self::DOMAIN_API . self::VERSION_API . '/account'); |
|
97
|
|
|
return $response; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function getRedirectUri(Account $account) |
|
101
|
|
|
{ |
|
102
|
|
|
|
|
103
|
|
|
$formProtection = FormProtectionFactory::get(); |
|
104
|
|
|
$formToken = $formProtection->generateToken('AuthorizationRequest'); |
|
105
|
|
|
|
|
106
|
|
|
return 'http://' . $_SERVER['HTTP_HOST'] . BackendUtility::getModuleUrl('tools_HubicHubic', [ |
|
107
|
|
|
'tx_hubic_tools_hubichubic' => [ |
|
108
|
|
|
'action' => 'authenticationResponse', |
|
109
|
|
|
'controller' => 'Backend\Account', |
|
110
|
|
|
'account' => $account->getUid() |
|
111
|
|
|
], |
|
112
|
|
|
'formToken' => $formToken |
|
113
|
|
|
]); |
|
114
|
|
|
|
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function getAuthorizationRequestUrl(Account $account) |
|
118
|
|
|
{ |
|
119
|
|
|
$this->callHubic($account); |
|
120
|
|
|
$authUrl = $this->OAuth->getAuthenticationUrl(self::AUTHORIZATION_ENDPOINT, $this->getRedirectUri($account)); |
|
121
|
|
|
return $authUrl; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Get hubiC account Quota |
|
126
|
|
|
* |
|
127
|
|
|
* @see https://api.hubic.com/console/ |
|
128
|
|
|
* @return array |
|
129
|
|
|
*/ |
|
130
|
|
|
public function getAccountQuota() |
|
131
|
|
|
{ |
|
132
|
|
|
$response = $this->OAuth->fetch(self::DOMAIN_API . self::VERSION_API . '/account/usage'); |
|
133
|
|
|
return $response; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Get hubiC agreements |
|
138
|
|
|
* |
|
139
|
|
|
* @see https://api.hubic.com/console/ |
|
140
|
|
|
* @return array |
|
141
|
|
|
*/ |
|
142
|
|
|
public function getAgreement() |
|
143
|
|
|
{ |
|
144
|
|
|
$response = $this->OAuth->fetch(self::DOMAIN_API . self::VERSION_API . '/agreement'); |
|
145
|
|
|
return $response; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Get hubiC getAllLinks |
|
150
|
|
|
* |
|
151
|
|
|
* @see https://api.hubic.com/console/ |
|
152
|
|
|
* @return array |
|
153
|
|
|
*/ |
|
154
|
|
|
public function getAllLinks() |
|
155
|
|
|
{ |
|
156
|
|
|
$response = $this->OAuth->fetch(self::DOMAIN_API . self::VERSION_API . '/account/getAllLinks'); |
|
157
|
|
|
return $response; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @param Account $account |
|
162
|
|
|
*/ |
|
163
|
|
|
public function setAccount(Account $account) |
|
164
|
|
|
{ |
|
165
|
|
|
$this->account = $account; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
} |
|
169
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.