-
-
Notifications
You must be signed in to change notification settings - Fork 144
/
sync.php
61 lines (58 loc) · 3.11 KB
/
sync.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
/*************************************************************************************************
* Copyright 2016 JPL TSolucio, S.L. -- This file is a part of TSOLUCIO coreBOS Customizations.
* Licensed under the vtiger CRM Public License Version 1.1 (the "License"); you may not use this
* file except in compliance with the License. You can redistribute it and/or modify it
* under the terms of the License. JPL TSolucio, S.L. reserves all rights not expressly
* granted by the License. coreBOS distributed by JPL TSolucio S.L. is distributed in
* the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Unless required by
* applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT ANY WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing
* permissions and limitations under the License. You may obtain a copy of the License
* at <http://corebos.org/documentation/doku.php?id=en:devel:vpl11>
*************************************************************************************************
* Module : Calendar::Google Sync
* Version : 1.0
* Author : JPL TSolucio, S. L.
*************************************************************************************************/
include_once 'include/database/PearDatabase.php';
include_once 'include/utils/utils.php';
global $root_directory, $adb, $site_URL;
coreBOS_Session::init();
set_include_path($root_directory. 'modules/Calendar4You/');
require_once 'vendor/autoload.php';
$service='GoogleCalendar';
$q=$adb->pquery('select * from its4you_googlesync4you_access where userid=? and service=?', array($_SESSION['authenticated_user_id'],$service));
$client_id=$adb->query_result($q, 0, 'google_clientid');
$client_secret=$adb->query_result($q, 0, 'google_login');
$redirect_uri=$adb->query_result($q, 0, 'google_keyfile');
$api_key=$adb->query_result($q, 0, 'google_apikey');
$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->setDeveloperKey($api_key);
$client->setAccessType('offline');
$client->setApplicationName('corebos');
$client->setScopes(array('https://www.googleapis.com/auth/calendar', 'https://www.googleapis.com/auth/calendar.readonly'));
if (isset($_GET['code'])) {
$q=$client->fetchAccessTokenWithAuthCode($_GET['code']);
coreBOS_Session::set('token', $client->getAccessToken());
if (is_array(coreBOS_Session::get('token'))) {
$token=coreBOS_Session::get('token');
$refresh_token = $token['refresh_token'];
} else {
$token=json_decode(coreBOS_Session::get('token'));
$refresh_token = $token->refresh_token;
}
if ($refresh_token!='' && $refresh_token!=null) {
$adb->pquery(
'update its4you_googlesync4you_access set refresh_token=? where userid=? and service=?',
array($refresh_token, $_SESSION['authenticated_user_id'],$service)
);
}
header("Location: $site_URL/index.php?module=Calendar4You&action=index");
}
?>