Files
crm.clientright.ru/modules/ITS4YouGoogleCalendarSync/connectors/Oauth2.php
Fedor ac7467f0b4 Major CRM updates: AI Assistant, Court Status API, S3 integration improvements, and extensive file storage system
- Added comprehensive AI Assistant system (aiassist/ directory):
  * Vector search and embedding capabilities
  * Typebot proxy integration
  * Elastic search functionality
  * Message classification and chat history
  * MCP proxy for external integrations

- Implemented Court Status API (GetCourtStatus.php):
  * Real-time court document status checking
  * Integration with external court systems
  * Comprehensive error handling and logging

- Enhanced S3 integration:
  * Improved file backup system with metadata
  * Batch processing capabilities
  * Enhanced error logging and recovery
  * Copy operations with URL fixing

- Added Telegram contact creation API
- Improved error logging across all modules
- Enhanced callback system for AI responses
- Extensive backup file storage with timestamps
- Updated documentation and README files

- File storage improvements:
  * Thousands of backup files with proper metadata
  * Fix operations for broken file references
  * Project-specific backup and recovery systems
  * Comprehensive file integrity checking

Total: 26,461+ files added/modified including AWS SDK, vendor dependencies, and extensive backup system.
2025-10-16 11:17:21 +03:00

264 lines
10 KiB
PHP

<?php
/*******************************************************************************
* The content of this file is subject to the ITS4YouGoogleCalendarSync license.
* ("License"); You may not use this file except in compliance with the License
* The Initial Developer of the Original Code is IT-Solutions4You s.r.o.
* Portions created by IT-Solutions4You s.r.o. are Copyright(C) IT-Solutions4You s.r.o.
* All Rights Reserved.
***************************************************************************** */
vimport('~~/libraries/google-api-php-client/autoload.php');
class ITS4YouGoogleCalendarSync_Oauth2_Connector extends Google_Oauth2_Connector
{
const OAUTH2_AUTH_URL = 'https://accounts.google.com/o/oauth2/auth';
const OAUTH2_TOKEN_URI = 'https://accounts.google.com/o/oauth2/token';
public $token;
public $loginUser;
protected $source_module;
protected $user_id;
protected $table_name = 'its4you_google_oauth2';
protected $service_name;
protected $client_id;
protected $client_secret;
protected $redirect_uri;
protected $scope;
protected $state;
protected $response_type = 'code';
protected $access_type = 'offline';
protected $approval_prompt = 'force';
function __construct($clientId, $clientSecret, $userId = false)
{
global $site_URL;
$this->source_module = 'ITS4YouGoogleCalendarSync';
if ($userId) {
$this->user_id = $userId;
}
$this->service_name = $this->service_provider . $this->source_module;
$this->client_id = $clientId;
$this->client_secret = $clientSecret;
$this->redirect_uri = self::getGoogleRedirectUri();
$this->scope = Google_Service_Calendar::CALENDAR;
$this->loginUser = Users_Record_Model::getInstanceById($this->user_id, 'Users');
}
public static function getGoogleRedirectUri()
{
$redirectUri = '/index.php?module=Calendar&view=Extension&extensionModule=ITS4YouGoogleCalendarSync&extensionView=Extension&authorize=true&service=GoogleITS4YouGoogleCalendarSync';
if (ITS4YouGoogleCalendarSync_Utils_Helper::isModuleActive('ITS4YouCalendar')) {
$redirectUri = '/index.php?module=ITS4YouGoogleCalendarSync&view=CalendarSettings&authorize=true&service=GoogleITS4YouGoogleCalendarSync';
}
global $site_URL;
return rtrim($site_URL, '/') . $redirectUri;
}
public static function removeConnector($userId, $delCredentials = true)
{
$adb = PearDatabase::getInstance();
ITS4YouGoogleCalendarSync_License_Model::controlVersion('remove', $userId);
if ($delCredentials) {
$adb->pquery("DELETE FROM its4you_google_credentials WHERE userid= ?", array($userId));
} else {
ITS4YouGoogleCalendarSync_License_Model::updateClientToken($userId, '');
}
$adb->pquery("DELETE FROM its4you_google_oauth2 WHERE userid = ?", array($userId));
}
public function getLoginUser()
{
return $this->loginUser;
}
public function getScope()
{
return $this->scope;
}
public function getState($source)
{
global $site_URL;
$callbackUri = rtrim($site_URL, '/') . '/index.php?module=Google&view=List&operation=sync&sourcemodule=' . $this->source_module . '&service=' . $source;
$stateDetails['url'] = $callbackUri;
$parse = parse_url($site_URL);
$ipAddr = getHostByName($parse['host']);
$stateDetails['dnf'] = md5($ipAddr);
$state = json_encode($stateDetails, JSON_FORCE_OBJECT);
return $state;
}
protected function setState()
{
$this->state = $this->getState($this->service_name);
}
public function authorize()
{
if ($this->hasStoredToken()) {
$token = $this->retreiveToken();
$this->setToken($token);
if ($this->isTokenExpired()) {
$this->refreshToken();
}
return $this;
} elseif ($_REQUEST['service'] && $_REQUEST['code']) {
$control = ITS4YouGoogleCalendarSync_License_Model::controlVersion('add');
if ('validated' === $control) {
$authCode = $_REQUEST['code'];
$token = $this->exchangeCodeForToken($authCode);
$this->storeToken($token);
$_SESSION['ITS4YouGoogleCalendarSyncError'] = '';
$success = true;
} else {
$_SESSION['ITS4YouGoogleCalendarSyncError'] = 'LBL_LICENCE_MAX_USERS';
$success = false;
}
echo '<script>window.opener.sync(' . $success . ');window.close();</script>';
return null;
} elseif ($_REQUEST['service']) {
echo '<script>window.close();</script>';
return null;
} else {
$this->setState();
$this->showConsentScreen();
}
return null;
}
public function hasStoredToken()
{
if (!isset($this->user_id)) {
$this->user_id = Users_Record_Model::getCurrentUserModel()->getId();
}
if (!isset($this->db)) {
$this->db = PearDatabase::getInstance();
}
$sql = 'SELECT 1 FROM ' . $this->table_name . ' WHERE userid = ? AND service = ? AND access_token<>? AND access_token IS NOT NULL';
$params = array($this->user_id, $this->service_name, '');
$res = $this->db->pquery($sql, $params);
$hasStoredToken = ($this->db->num_rows($res) > 0) ? true : false;
return $hasStoredToken;
}
protected function retreiveToken()
{
if (!$this->user_id) {
$this->user_id = Users_Record_Model::getCurrentUserModel()->getId();
}
if (!isset($this->db)) {
$this->db = PearDatabase::getInstance();
}
$query = 'SELECT access_token,refresh_token FROM ' . $this->table_name . ' WHERE userid=? AND service =?';
$params = array($this->user_id, $this->service_name);
$result = $this->db->pquery($query, $params);
$data = $this->db->fetch_array($result);
$decodedAccessToken = json_decode(decode_html($data['access_token']), true);
$refreshToken = decode_html($data['refresh_token']);
return array('access_token' => $decodedAccessToken, 'refresh_token' => $refreshToken);
}
protected function setToken($token)
{
$this->token = $token;
}
public function isTokenExpired()
{
if (null == $this->token || null == $this->token['access_token']) {
return true;
}
$expired = ($this->token['access_token']['created'] + ($this->token['access_token']['expires_in'] - 30)) < time();
return $expired;
}
public function refreshToken()
{
if ($this->token['refresh_token'] == null) {
throw new AppException('refresh token is null');
}
$params = array('grant_type' => 'refresh_token', 'refresh_token' => $this->token['refresh_token'], 'client_id' => $this->client_id, 'client_secret' => $this->client_secret);
$encodedToken = $this->fireRequest(self::OAUTH2_TOKEN_URI, array(), $params);
$decodedToken = json_decode($encodedToken, true);
$decodedToken['created'] = time();
$token['access_token'] = $decodedToken;
$token['refresh_token'] = $this->token['refresh_token'];
$this->updateAccessToken(json_encode($decodedToken), $token['refresh_token']);
$this->setToken($token);
}
protected function fireRequest($url, $headers, $params = array(), $method = 'POST')
{
$httpClient = new Vtiger_Net_Client($url);
if (count($headers)) {
$httpClient->setHeaders($headers);
}
switch ($method) {
case 'POST':
$response = $httpClient->doPost($params);
break;
case 'GET':
$response = $httpClient->doGet($params);
break;
}
return $response;
}
protected function updateAccessToken($accesstoken, $refreshtoken)
{
if (!isset($this->db)) {
$this->db = PearDatabase::getInstance();
}
$sql = 'UPDATE ' . $this->table_name . ' SET access_token = ? WHERE refresh_token = ? AND service = ?';
$params = array($accesstoken, $refreshtoken, $this->service_name);
$this->db->pquery($sql, $params);
}
protected function exchangeCodeForToken($code)
{
$params = array('grant_type' => 'authorization_code', 'code' => $code, 'client_id' => $this->client_id, 'client_secret' => $this->client_secret, 'redirect_uri' => $this->redirect_uri);
$response = $this->fireRequest(self::OAUTH2_TOKEN_URI, array(), $params);
return $response;
}
protected function storeToken($token)
{
if (!isset($this->user_id)) {
$this->user_id = Users_Record_Model::getCurrentUserModel()->getId();
}
if (!isset($this->db)) {
$this->db = PearDatabase::getInstance();
}
$decodedToken = json_decode($token, true);
$refresh_token = $decodedToken['refresh_token'];
unset($decodedToken['refresh_token']);
$decodedToken['created'] = time();
$accessToken = json_encode($decodedToken);
$params = array($this->service_name, $accessToken, $refresh_token, $this->user_id);
$sql = 'INSERT INTO ' . $this->table_name . ' VALUES (' . generateQuestionMarks($params) . ')';
$this->db->pquery($sql, $params);
}
public function showConsentScreen()
{
header('Location: ' . $this->getAuthUrl());
}
protected function getAuthUrl()
{
$params = array('response_type=' . urlencode($this->response_type), 'redirect_uri=' . urlencode($this->redirect_uri), 'client_id=' . urlencode($this->client_id), 'scope=' . urlencode($this->scope), 'access_type=' . urlencode($this->access_type), 'approval_prompt=' . urlencode($this->approval_prompt),);
$queryString = implode('&', $params);
return self::OAUTH2_AUTH_URL . "?$queryString";
}
}