forked from dotproject/dotProject
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquickstart.php
executable file
·279 lines (249 loc) · 10.2 KB
/
quickstart.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
/*
! PROOF OF CONCEPT !
This script gets contents of emails and insert them into a new task log
dependent on the tag of an email. e.g. an email sent to
dotprojectemail+1234@gmail.com will add a task log for the task with
task_id=1234.
? Currently this script has to be run from the command line.
Required files not included in commit:
- credentials.json - google developer credentials
- token.json - will be created automatically
*/
require_once 'base.php';
require_once DP_BASE_DIR.'/includes/config.php';
require_once DP_BASE_DIR.'/includes/main_functions.php';
require_once DP_BASE_DIR.'/includes/db_connect.php';
require_once DP_BASE_DIR.'/classes/ui.class.php';
require_once DP_BASE_DIR.'/classes/event_queue.class.php';
require_once DP_BASE_DIR.'/classes/query.class.php';
$AppUI = new CAppUI;
$AppUI->setUserLocale();
$perms =& $AppUI->acl();
require_once($AppUI->getLibraryClass('google-api-php-client-2.2.1/vendor/autoload'));
require_once($AppUI->getModuleClass('tasks'));
if (php_sapi_name() != 'cli') {
throw new Exception('This application must be run on the command line.');
}
/**
* Returns an authorized API client.
* @return Google_Client the authorized client object
*/
function getClient()
{
$client = new Google_Client();
// ! Proxy settings for DBW
// $client->setHttpClient(new GuzzleHttp\Client([
// 'proxy' => 'proxy1:3128',
// 'verify' => false,
// ]));
$client->setApplicationName('Gmail API PHP Quickstart');
$client->setScopes(Google_Service_Gmail::GMAIL_READONLY);
$client->setAuthConfig('credentials.json');
$client->setAccessType('offline');
$client->setPrompt('select_account consent');
// Load previously authorized token from a file, if it exists.
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
$tokenPath = 'token.json';
if (file_exists($tokenPath)) {
$accessToken = json_decode(file_get_contents($tokenPath), true);
$client->setAccessToken($accessToken);
}
// If there is no previous token or it's expired.
if ($client->isAccessTokenExpired()) {
// Refresh the token if possible, else fetch a new one.
if ($client->getRefreshToken()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
} else {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:\n%s\n", $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
$client->setAccessToken($accessToken);
// Check to see if there was an error.
if (array_key_exists('error', $accessToken)) {
throw new Exception(join(', ', $accessToken));
}
}
// Save the token to a file.
if (!file_exists(dirname($tokenPath))) {
mkdir(dirname($tokenPath), 0700, true);
}
file_put_contents($tokenPath, json_encode($client->getAccessToken()));
}
return $client;
}
$client = getClient();
$service = new Google_Service_Gmail($client);
// print_r("$service");
$user = 'me';
$labels = ['Label_4983233390187973438', "UNREAD"];
$results = $service->users_messages->listUsersMessages($user, ["labelIds" => $labels]);
// $results = $service->users_messages->listUsersMessages($user);
$allowedDomains = ["debortoli.com.au"];
/*
Decodes body of email
*/
// function decodeBody($body) {
// $rawData = $body;
// $sanitisedData = strtr($rawData, '-_', '+/');
// $decodedMessage = base64_decode($sanitisedData);
// if (!$decodedMessage) {
// $decodedMessage = FALSE;
// }
// return $decodedMessage;
// }
function decodeBody($data) {
$data = base64_decode(str_replace(array('-', '_'), array('+', '/'), $data));
// $data = imap_qprint($data);
// print_r($data);
return $data;
}
function findBody($parts, $type='text/plain') {
foreach ($parts as $part) {
if ($part['mimeType'] == $type)
return $part->getBody()->getData();
else {
if ($part->getParts()) {
$recurse = findBody($part->getParts());
if (gettype($recurse) != 'array')
return $recurse;
}
}
}
}
function getCcAddresses($headers) {
foreach ($headers as $header) {
if ($header->getName() == 'Cc')
return $header->getValue();
}
}
function getTags($addresses) {
$tags = [];
foreach ($addresses as $address) {
if (strpos($address, '+') !== false) {
$pos1 = strpos($address, '+')+1;
$pos2 = strpos($address, '@');
$length = abs($pos1 - $pos2);
$tag = substr($address, $pos1, $length);
$currentAddress = str_replace('+'.$tag, '', $address);
if ($currentAddress == $projectAddress)
array_push($tags, $tag);
}
}
return $tags;
}
if (strpos($toAddress, "+") !== false) {
$pos1 = strpos($toAddress, "+")+1;
$pos2 = strpos($toAddress, "@");
$length = abs($pos1 - $pos2);
$taskId = substr($toAddress, $pos1, $length);
try {
foreach($results->getMessages() as $mlist) {
$message_id = $mlist->id;
$optParamsGet2['format'] = 'full';
$single_message = $service->users_messages->get($user, $message_id, $optParamsGet2);
// print_r("MESSAGE ID: " . $single_message->id);
$payload = $single_message->getPayload();
// print_r($payload);
$body = $payload->getBody()->getData();
if (!$body) {
$body = findBody($payload->getParts());
// print_r(decodeBody($body));
}
// if (!$body) {
// $part = $payload->getParts()[0]->getParts()[0]->getBody()->getData();
// print_r(decodeBody($part));
// } else {
// print_r(decodeBody($body));
// }
// $correctPart = $payload->getParts()[0]->getParts();
// print_r(decodeBody($correctPart[0]['body']->getData()));
$body = $payload->getBody();
$headers = $payload->getHeaders();
// print_r($body['data']);
$FOUND_BODY = decodeBody($body['data']);
$subject = array_values(array_filter($headers, function($k) {
return $k['name'] == 'Subject';
}));
$toAddresses = array_values(array_filter($headers, function($k) {
return $k['name'] == "To";
}));
$fromAddresses = array_values(array_filter($headers, function($k) {
return $k['name'] == "From";
}));
$CcAddresses = array_values(array_filter($headers, function($k) {
return $k['name'] = 'Cc';
}));
print_r($subject[0]->getValue());
$toAddress = $toAddresses[0]->getValue();
$date = $single_message->getInternalDate();
if (!$FOUND_BODY) {
$parts = $payload->getParts();
foreach ($parts as $part) {
if ($part['body']) {
$FOUND_BODY = decodeBody($part['body']->data);
break;
}
if ($part['parts'] && !$FOUND_BODY) {
foreach ($part['parts'] as $p) {
if ($p['mimeType'] == 'text/html' && $p['body']) {
$FOUND_BODY = decodeBody($p['body']->data);
break;
}
}
}
if ($FOUND_BODY) {
break;
}
}
// print_r($FOUND_BODY->toSimpleObject());
}
$fromAddress = $fromAddresses[0]->getValue();
$adpos1 = strpos($fromAddress, "<")+1;
$adpos2 = strpos($fromAddress, ">");
$adLength = abs($adpos1 - $adpos2);
$fromAddress = substr($fromAddress, $adpos1, $adLength);
// print_r("FROM ADDRESS\n\n\n$fromAddress\n\n\nEND FROM ADDRESS");
foreach ($allowedDomains as $allowedDomain) {
if (strpos($fromAddress, $allowedDomain) !== false) {
$toAddress = $toAddresses[0]->getValue();
if (strpos($toAddress, "+") !== false) {
// Formatting address address to get task_id
$pos1 = strpos($toAddress, "+")+1;
$pos2 = strpos($toAddress, "@");
$length = abs($pos1 - $pos2);
$task_id = substr($toAddress, $pos1, $length);
// Task log creation
// $log = new CTaskLog();
// $log->task_log_task = intval($task_id);
// $log->task_log_name = $subject[0]->getValue();
// $log->task_log_description= $FOUND_BODY;
// $log->task_log_creator = 285;
// $log->task_log_hours = 1;
// $log->task_log_costcode = 1;
// $log->store();
// print_r($fromAddress . "\n");
// print_r($FOUND_BODY . "\n\n");
$mods = new Google_Service_Gmail_ModifyMessageRequest();
$mods->setAddLabelIds("READ");
$service->users_messages->modify($user, $message_id, $mods);
}
}
}
if ($results->getNextPageToken() != null) {
$pageToken = $list->getNextPageToken();
$list = $gmail->users_messages->listUsersMessages($user, ['pageToken' => $pageToken, 'maxResults' => 1000]);
} else {
break;
}
}
} catch (Exception $e) {
echo $e->getMessage();
}
?>