Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
{
"name": "ujy/bitrix24_api_authorization",
"description": "Authorization class to easy get Bitrix24 Access Tokens for use in API",
"keywords": ["Bitrix24", "Access", "API", "Token"],
"version": "0.1.2",
"keywords": [
"Bitrix24",
"Access",
"API",
"Token"
],
"type": "library",
"homepage": "https://github.com/xUJYx/bitrix24_api_authorization",
"license": "MIT",
Expand All @@ -15,12 +21,19 @@
"name": "Oleg Pravdin",
"email": "opravdin@gmail.com",
"role": "Developer"
},
{
"name": "Oleg Kosarev",
"email": "dev.olegkosarev@outlook.com",
"role": "Developer"
}
],
"autoload": {
"classmap": ["src/"]
"classmap": [
"src/"
]
},
"require": {
"mervick/curl-helper": "^1.1"
"mervick/curl-helper": "^1.2"
}
}
28 changes: 18 additions & 10 deletions src/bitrix24Authorization.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
namespace Bitrix24Authorization;

use CurlHelper;

class Bitrix24Authorization
Expand Down Expand Up @@ -47,7 +48,7 @@ private function authorize()
->exec();
$b24_login_check_data = json_decode($b24_auth_response['content'], true);
if (!is_array($b24_login_check_data) || !array_key_exists('status', $b24_login_check_data) || $b24_login_check_data['status'] != 'success') {
throw new \Exception('Login and Password check fails at Bitrix24 portal!');
throw new \Exception('Login check fails at Bitrix24 portal!');
}

// Авторизуемся на портале Bitrix24 по проверенным данным...
Expand All @@ -62,6 +63,12 @@ private function authorize()
->setCookies($b24_auth_cookies)
->exec();

if (
!array_key_exists('X-User-Id', $b24_auth_response)
) {
throw new \Exception('Password check fails at Bitrix24 portal!');
}

// Dыдергиваем js-redirect из body, который редиректит нас на портал компании
$b24_js_domain_backurl = $this->getRespondUrlFromCurl($b24_auth_response['content']);

Expand Down Expand Up @@ -109,7 +116,7 @@ private function authorize()
*/
private function getRespondUrlFromCurl($curl_response)
{
if(!preg_match('~window\.location(\.href)?[\s\=]{1,3}\'(.+?)\'~m', $curl_response, $result)) {
if (!preg_match('~window\.location(\.href)?[\s\=]{1,3}\'(.+?)\'~m', $curl_response, $result)) {
throw new \Exception("THERE IS NO ~bitrix24 chain URL~ IN CURL ANSWER... <br>\r\nINPUT CURL BODY: <br>\r\n{$curl_response}");
}

Expand All @@ -123,7 +130,7 @@ private function getRespondUrlFromCurl($curl_response)
*/
public function is_authorize()
{
if(!empty($this->bitrix24_access) && $this->bitrix24_access['expires'] > time())
if (!empty($this->bitrix24_access) && $this->bitrix24_access['expires'] > time())
return true;

return false;
Expand All @@ -134,7 +141,7 @@ public function is_authorize()
* @return bool
* @throws \Exception
*/
private function checkAuthorizationVars ()
private function checkAuthorizationVars()
{
$error = '';
$error .= empty($this->app_scope) ? "\r\napp_scope with 'setApplicationScope' method <br>" : '';
Expand All @@ -144,7 +151,8 @@ private function checkAuthorizationVars ()
$error .= empty($this->bitrix24_login) ? "\r\nbitrix24_login with 'setBitrix24Login' method <br>" : '';
$error .= empty($this->bitrix24_password) ? "\r\nbitrix24_password with 'setBitrix24Password' method <br>" : '';

if(!empty($error)) throw new \Exception('You need to set this variables to get authorization data: <br>' . $error);
if (!empty($error))
throw new \Exception('You need to set this variables to get authorization data: <br>' . $error);
return true;
}

Expand All @@ -162,14 +170,14 @@ public function initialize(\Bitrix24\Bitrix24 $B24App = null)
}


if(!$this->is_authorize())
if (!$this->is_authorize())
try {
$this->authorize();
} catch (\Exception $error) {
die($error->getMessage());
}

if(is_object($B24App)) {
if (is_object($B24App)) {
$B24App->setApplicationScope(array($this->app_scope));
$B24App->setApplicationId($this->app_id);
$B24App->setApplicationSecret($this->app_secret);
Expand Down Expand Up @@ -242,7 +250,7 @@ public function setBitrix24Password($bitrix24_user_password)
*/
public function __get($name)
{
if(isset($this->{$name}))
if (isset($this->{$name}))
return $this->{$name};
}

Expand All @@ -254,7 +262,7 @@ public function __get($name)
*/
private function getBitrixSessionIdFromCurl($curl_response)
{
if(!preg_match('~\'bitrix_sessid\':\'([\d\w]+)\'~', $curl_response, $result)) {
if (!preg_match('~\'bitrix_sessid\':\'([\d\w]+)\'~', $curl_response, $result)) {
throw new \Exception("THERE IS NO ~bitrix_sessid~ IN CURL ANSWER... <br>\r\nINPUT CURL BODY: <br>\r\n{$curl_response}");
}

Expand All @@ -270,7 +278,7 @@ private function getBitrixSessionIdFromCurl($curl_response)
*/
private function getBitrixAuthCodeFromCurl($curl_response)
{
if(!preg_match('~code=([^\&]+)~', $curl_response, $b24_auth_code))
if (!preg_match('~code=([^\&]+)~', $curl_response, $b24_auth_code))
throw new \Exception("NO PARAMETER ~CODE~ IN BITRIX24 ANSWER: ... <br>\r\nINPUT CURL BODY: <br>\r\n{$curl_response}");
$b24_auth_code = $b24_auth_code[1];

Expand Down
Loading