-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
161 lines (132 loc) · 4.65 KB
/
functions.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
/* ---------------------------------------- */
/* ---------------------------------------- */
/* ---------------------------------------- */
/* GHL API Functions Credentials */
/* ---------------------------------------- */
/* ---------------------------------------- */
/* ---------------------------------------- */
function curl_get($url, $headers) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
if (curl_errno($ch)) {
log_error('Curl error: ' . curl_error($ch));
}
curl_close($ch);
return $response;
}
function curl_post($url, $data, $headers) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
if (curl_errno($ch)) {
log_error('Curl error: ' . curl_error($ch));
}
curl_close($ch);
return $response;
}
function store_tokens($tokens) {
global $tokens_file;
file_put_contents($tokens_file, json_encode($tokens));
}
function load_tokens() {
global $tokens_file;
if (file_exists($tokens_file)) {
return json_decode(file_get_contents($tokens_file), true);
}
return null;
}
function redirect_to_authorization() {
global $authorization_url, $client_id, $redirection_url, $scope;
$authorization_url = $authorization_url . '?' . http_build_query([
'response_type' => 'code',
'client_id' => $client_id,
'redirect_uri' => $redirection_url,
'scope' => $scope,
]);
header('Location: ' . $authorization_url);
exit();
}
$tokens = load_tokens();
if (isset($_GET['code'])) {
log_error('Authorization code received: ' . $_GET['code']);
$code = $_GET['code'];
$data = [
'grant_type' => 'authorization_code',
'client_id' => $client_id,
'client_secret' => $client_secret,
'redirect_uri' => $redirection_url,
'code' => $code,
'user_type' => 'Location',
];
$response = curl_post($token_url, $data, ['Content-Type: application/x-www-form-urlencoded']);
$response_data = json_decode($response, true);
log_error('Token response: ' . json_encode($response_data));
if (isset($response_data['access_token'])) {
$_SESSION['access_token'] = $response_data['access_token'];
$_SESSION['refresh_token'] = $response_data['refresh_token'];
$_SESSION['token_expires'] = time() + $response_data['expires_in'];
store_tokens([
'access_token' => $response_data['access_token'],
'refresh_token' => $response_data['refresh_token'],
'expires_in' => time() + $response_data['expires_in']
]);
header('Location: ' . $redirection_url);
exit();
} else {
echo 'Error retrieving access token: ' . $response;
log_error('Error retrieving access token: ' . $response);
exit();
}
}
if ($tokens && time() < $tokens['expires_in']) {
log_error('Tokens loaded successfully.');
$_SESSION['access_token'] = $tokens['access_token'];
$_SESSION['refresh_token'] = $tokens['refresh_token'];
$_SESSION['token_expires'] = $tokens['expires_in'];
} else {
log_error('No valid tokens found or tokens expired.');
redirect_to_authorization();
}
function load_user_names() {
global $user_names_file;
if (file_exists($user_names_file)) {
return json_decode(file_get_contents($user_names_file), true);
}
return [];
}
function save_user_names($user_names) {
global $user_names_file;
file_put_contents($user_names_file, json_encode($user_names));
}
function get_user_name($user_id, $access_token) {
$url = 'https://services.leadconnectorhq.com/users/' . $user_id;
$headers = [
"Accept: application/json",
"Authorization: Bearer $access_token",
"Version: 2021-07-28"
];
$response = curl_get($url, $headers);
$user_data = json_decode($response, true);
if (isset($user_data['name'])) {
return $user_data['name'];
} else {
return 'Unknown';
}
}
function getSheetId($gSheetService, $sheet_ID, $sheet_name) {
$spreadsheet = $gSheetService->spreadsheets->get($sheet_ID);
$sheets = $spreadsheet->getSheets();
foreach ($sheets as $sheet) {
$properties = $sheet->getProperties();
if ($properties->getTitle() == $sheet_name) {
return $properties->getSheetId();
}
}
throw new Exception("Sheet not found: " . $sheet_name);
}