1 | <?php |
||
8 | class VKBase { |
||
9 | |||
10 | /** |
||
11 | * Application ID |
||
12 | * @var string |
||
13 | */ |
||
14 | private $applicationID; |
||
15 | |||
16 | /** |
||
17 | * Application Secret Key |
||
18 | * @var string |
||
19 | */ |
||
20 | private $APISecret; |
||
21 | |||
22 | /** |
||
23 | * API Version. If null, uses latest available version |
||
24 | * @var string |
||
25 | */ |
||
26 | private $APIVersion; |
||
27 | |||
28 | /** |
||
29 | * Acess Token String |
||
30 | * @var string |
||
31 | */ |
||
32 | private $accessToken; |
||
33 | |||
34 | /** |
||
35 | * Authorization Status |
||
36 | * @var bool |
||
37 | */ |
||
38 | private $authorizationStatus = false; |
||
39 | |||
40 | /** |
||
41 | * CURL Object |
||
42 | * @var Resource |
||
43 | */ |
||
44 | private $curlObject; |
||
45 | |||
46 | /** |
||
47 | * Users Access Permissions |
||
48 | * @var int |
||
49 | */ |
||
50 | private $permissionsMask = 0; |
||
51 | |||
52 | /** |
||
53 | * Determine if permissions mask is set |
||
54 | * @var bool |
||
55 | */ |
||
56 | private $isPermissionsMaskSet = false; |
||
57 | |||
58 | /** |
||
59 | * URL For Authorization |
||
60 | */ |
||
61 | const AUTHORIZATION_URL = 'https://oauth.vk.com/authorize'; |
||
62 | |||
63 | /** |
||
64 | * URL For Access Token |
||
65 | */ |
||
66 | const ACCESS_TOKEN_URL = 'https://oauth.vk.com/access_token'; |
||
67 | |||
68 | /** |
||
69 | * URL For VK API |
||
70 | */ |
||
71 | const METHOD_URL = 'https://api.vk.com/method/'; |
||
72 | |||
73 | /** |
||
74 | * URL For OAuth Token |
||
75 | */ |
||
76 | const TOKEN_URL = 'https://oauth.vk.com/token'; |
||
77 | |||
78 | const DEFAULT_CALLBACK = 'https://api.vk.com/blank.html'; |
||
79 | |||
80 | const PACKAGE_VERSION = '1.0.5'; |
||
81 | |||
82 | /** |
||
83 | * VK constructor. |
||
84 | * @param string $appID |
||
85 | * @param string $apiSecret |
||
86 | * @param string $accessToken |
||
87 | * @throws VKException |
||
88 | */ |
||
89 | public function __construct($appID, $apiSecret, $accessToken = null) { |
||
90 | $this->applicationID = $appID; |
||
91 | $this->APISecret = $apiSecret; |
||
92 | $this->accessToken = $accessToken; |
||
93 | $this->curlObject = curl_init(); |
||
94 | if (!is_null($accessToken)) { |
||
95 | if (!$this->isPermissionsMaskSet) { |
||
96 | $VKUser = new VKUsers($this); |
||
97 | $VKAccount = new VKAccount($this); |
||
98 | $CurrentUser = $VKUser->get([ '' ])[ 'response' ][ 0 ][ 'uid' ]; |
||
99 | $this->setPermissionsMask($VKAccount->getAppPermissions($CurrentUser)[ 'response' ]); |
||
100 | $this->isPermissionsMaskSet = true; |
||
101 | unset($VKUser); |
||
102 | unset($VKAccount); |
||
103 | } |
||
104 | } |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * VK destructor. |
||
109 | */ |
||
110 | public function __destruct() { |
||
113 | |||
114 | /** |
||
115 | * Set API Version Provided By User |
||
116 | * @param int $apiVersion |
||
117 | */ |
||
118 | public function setAPIVersion($apiVersion) { |
||
121 | |||
122 | /** |
||
123 | * Returns Base API URL |
||
124 | * @param string $apiMethod |
||
125 | * @param string $responseFormat |
||
126 | * @return string |
||
127 | */ |
||
128 | public function getAPIUrl($apiMethod, $responseFormat = 'json') { |
||
131 | |||
132 | /** |
||
133 | * Returns Authorization Link With Passed Parameters |
||
134 | * @param string $apiSettings |
||
135 | * @param string $callbackURL |
||
136 | * @param string $responseType |
||
137 | * @param bool $testMode |
||
138 | * @return string |
||
139 | */ |
||
140 | public function getAuthorizationURL($apiSettings = '', $callbackURL = self::DEFAULT_CALLBACK, $responseType = 'code', $testMode = false) { |
||
155 | |||
156 | /** |
||
157 | * Returns Access Token From Authorization Link |
||
158 | * @param $resultCode |
||
159 | * @param string $callbackURL |
||
160 | * @return mixed |
||
161 | * @throws VKException |
||
162 | */ |
||
163 | public function getAccessToken($resultCode, $callbackURL = self::DEFAULT_CALLBACK) { |
||
164 | if (!is_null($this->accessToken) && $this->authorizationStatus) { |
||
165 | throw new VKException('Already Authorized!', 1); |
||
166 | } |
||
167 | |||
168 | $requestParameters = [ |
||
169 | 'client_id' => $this->applicationID, |
||
170 | 'client_secret' => $this->APISecret, |
||
171 | 'redirect_uri' => $callbackURL, |
||
172 | 'code' => $resultCode |
||
173 | ]; |
||
174 | |||
175 | $apiResponse = json_decode($this->performRequest($this->createURL(self::ACCESS_TOKEN_URL, $requestParameters)), true); |
||
176 | |||
177 | try { |
||
178 | if (isset($apiResponse[ 'error' ])) { |
||
179 | throw new VKException($apiResponse[ 'error' ].(!isset($apiResponse[ 'error_description' ]) ?: ': '.$apiResponse[ 'error_description' ]), '0'); |
||
180 | } else { |
||
181 | $this->authorizationStatus = true; |
||
182 | $this->accessToken = $apiResponse[ 'access_token' ]; |
||
183 | return $apiResponse; |
||
184 | } |
||
185 | } catch (VKException $ex) { |
||
186 | echo $ex->getMessage(); |
||
187 | return [ ]; |
||
188 | } |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Returns User Authorization Status |
||
193 | * @return bool |
||
194 | */ |
||
195 | public function isAuthorized() { |
||
198 | |||
199 | |||
200 | /** |
||
201 | * Execute API Method With Parameters and return Result |
||
202 | * @param string $apiMethod |
||
203 | * @param array $requestParameters |
||
204 | * @param string $resultType |
||
205 | * @param string $requestMethod |
||
206 | * @return mixed |
||
207 | */ |
||
208 | public function apiQuery($apiMethod, $requestParameters = [ ], $resultType = 'array', $requestMethod = 'get') { |
||
209 | $requestParameters[ 'timestamp' ] = time(); |
||
210 | $requestParameters[ 'api_id' ] = $this->applicationID; |
||
211 | $requestParameters[ 'random' ] = rand(0, 10000); |
||
212 | |||
213 | if (!array_key_exists('access_token', $requestParameters) && !is_null($this->accessToken)) { |
||
214 | $requestParameters[ 'access_token' ] = $this->accessToken; |
||
215 | } |
||
216 | |||
217 | if (!array_key_exists('v', $requestParameters) && !is_null($this->APIVersion)) { |
||
218 | $requestParameters[ 'v' ] = $this->APIVersion; |
||
219 | } |
||
220 | |||
221 | ksort($requestParameters); |
||
222 | |||
223 | $parametersSignature = ''; |
||
224 | foreach ($requestParameters as $pKey=>$pValue) { |
||
225 | if (is_array($pValue)) { |
||
226 | $pValue = implode(', ', $pValue); |
||
227 | } |
||
228 | $parametersSignature .= $pKey . '=' . $pValue; |
||
229 | } |
||
230 | $parametersSignature .= $this->APISecret; |
||
231 | |||
232 | $requestParameters[ 'sig' ] = md5($parametersSignature); |
||
233 | |||
234 | if ($apiMethod == 'execute' || $requestMethod == 'post') { |
||
235 | $apiResponse = $this->performRequest($this->getAPIUrl($apiMethod, $resultType == 'array' ? 'json' : $resultType), "POST", $requestParameters); |
||
236 | } else { |
||
237 | $apiResponse = $this->performRequest($this->createURL($this->getAPIUrl($apiMethod, $resultType == 'array' ? 'json' : $resultType), $requestParameters)); |
||
238 | } |
||
239 | |||
240 | try { |
||
241 | $decodedJSON = json_decode($apiResponse, true); |
||
242 | if (isset($decodedJSON[ 'error' ])) { |
||
243 | throw new VKException($decodedJSON[ 'error' ][ 'error_msg' ], $decodedJSON[ 'error' ][ 'error_code' ], $decodedJSON[ 'error' ][ 'request_params' ]); |
||
244 | } |
||
245 | |||
246 | return $resultType == 'array' ? $decodedJSON : $apiResponse; |
||
247 | } catch(VKException $ex) { |
||
248 | echo $ex->getMessage(); |
||
249 | return [ ]; |
||
250 | } |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * Set Permissions Mask |
||
255 | * @param int $permMask |
||
256 | */ |
||
257 | public function setPermissionsMask($permMask) { |
||
260 | |||
261 | /** |
||
262 | * Get Permissions Mask |
||
263 | * @return int |
||
264 | */ |
||
265 | public function getPermissionsMask() { |
||
268 | |||
269 | /** |
||
270 | * Concatenate Keys And Values Of Parameters Array And Return URL String |
||
271 | * @param string $urlString |
||
272 | * @param array $parametersArray |
||
273 | * @return string |
||
274 | */ |
||
275 | private function createURL($urlString, $parametersArray) { |
||
279 | |||
280 | /** |
||
281 | * Execute Request |
||
282 | * @param string $requestURL |
||
283 | * @param string $requestMethod |
||
284 | * @param array $postFields |
||
285 | * @return string |
||
286 | */ |
||
287 | private function performRequest($requestURL, $requestMethod = 'GET', $postFields = [ ]) { |
||
299 | |||
300 | } |
||
301 | |||
382 | } |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.