This repository has been archived by the owner on Dec 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.php
279 lines (240 loc) · 6.79 KB
/
helper.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
/**
* Expands the home directory alias '~' to the full path.
* @param string $path the path to expand.
* @return string the expanded path.
*/
function expandHomeDirectory($path)
{
$homeDirectory = getenv('HOME');
if (empty($homeDirectory)) {
$homeDirectory = getenv("HOMEDRIVE") . getenv("HOMEPATH");
}
return str_replace('~', realpath($homeDirectory), $path);
}
/**
* Colorize Console text
* @param $text
* @param $status
* @return string
* @throws Exception
*/
function colorize($text, $status)
{
return $text;
}
/**
* Colorize Console text
* @param $text
* @param $status
* @return string
* @throws Exception
*/
function colorizeCli($text, $status)
{
$out = "";
switch ($status) {
case "SUCCESS":
$out = "[42m"; //Green background
break;
case "FAILURE":
$out = "[41m"; //Red background
break;
case "WARNING":
$out = "[43m"; //Yellow background
break;
case "NOTE":
$out = "[44m"; //Blue background
break;
default:
throw new Exception("Invalid status: " . $status);
}
return chr(27) . "$out" . "$text" . chr(27) . "[0m";
}
function getStartTasksDate()
{
$weekDay = date("w");
if ($weekDay < 7 && $weekDay > 1) {
$previous = "-1";
} elseif ($weekDay == 7) {
$previous = "-2";
} else {
$previous = "-3";
}
$datetime = new DateTime(date('Y-m-d ' . TIME_CHECK_FROM, strtotime($previous . ' day')),
new DateTimeZone(DATETIME_TIMEZONE_CURRENT));
$datetime->setTimezone(new DateTimeZone(DATETIME_TIMEZONE_ASANA));
return $datetime->format('Y-m-d\TH:i:s\Z');
}
function getClientNameByProjectId($projectId)
{
global $clientsProjects;
foreach ($clientsProjects as $clientData) {
foreach ($clientData['projects'] as $project) {
if ($project['id'] == $projectId) {
return $clientData['name'];
}
}
}
return NOT_DEFINED_CLIENT_FOLDER;
}
function getClientEmailsByProjectId($projectId)
{
global $clientsProjects;
foreach ($clientsProjects as $clientData) {
foreach ($clientData['projects'] as $project) {
if ($project['id'] == $projectId) {
return $clientData['send_to'];
}
}
}
return [];
}
function getClientEmailsByClientName($findClientName)
{
global $clientsProjects;
foreach ($clientsProjects as $clientData) {
if ($clientData['name'] == $findClientName) {
return $clientData['send_to'];
}
}
return [];
}
function getPropertyByKey($file, $key)
{
$properties = $file->getProperties();
foreach ($properties as $property) {
if ($property->key == $key) {
return $property->value;
}
}
return null;
}
function logMessage($message, $level = LOG_INFO, $logHasStatus = false)
{
global $isCli;
$statusTxt = "";
switch ($level) {
default:
case LOG_INFO:
$prefix = "INFO";
$status = "NOTE";
break;
case LOG_ERR:
$prefix = "ERROR";
$status = "FAILURE";
break;
case LOG_WARNING:
$prefix = "WARN";
$status = "WARNING";
break;
}
if ($logHasStatus !== false)
switch ($logHasStatus) {
case 1:
$statusTxt = "SUCCESS";
$statusColor = "SUCCESS";
break;
case 0:
$statusTxt = "FAILED";
$statusColor = "FAILURE";
break;
}
if ($isCli) {
$showMessage = colorizeCli($prefix, $status) . ": " . $message;
$delimeter = "\n";
} else {
$showMessage = $prefix . ": " . $message;
$delimeter = "<br>";
}
if ($statusTxt) {
$showMessage .= $isCli ? " [STATUS: " . colorizeCli($statusTxt,
$statusColor) . "]" : " [STATUS: " . $statusTxt . "]";
$message .= " [STATUS: " . $statusTxt . "]";
}
$showMessage .= $delimeter;
print($showMessage);
pushToLog($prefix . ": " . $message);
}
function pushToLog($msg)
{
global $log;
if (LOG_SHOW_DATETIME) {
$msg = "<strong>" . date(LOG_DATETIME_FORMAT) . "</strong>" . " " . $msg;
}
$log[] = $msg;
}
function reportMessage($message)
{
global $report;
$report[] = $message;
}
function logError($message)
{
logMessage($message, LOG_ERR);
}
function logStatusFailure($message)
{
logMessage($message, LOG_WARNING, 0);
}
function logStatusSuccess($message)
{
logMessage($message, LOG_INFO, 1);
}
function closeSession($sendMail = true)
{
logMessage("Close session");
if ($sendMail) {
sendAdminEmail(prepareEmailMessage());
}
die;
}
function prepareEmailMessage()
{
global $report, $log;
// var_dump($log + $report);die;
return implode("<br />", array_merge($report, ["<br><hr>"], $log));
}
function sendAdminEmail($messages)
{
global $emailConfig;
logMessage('Send email to ' . EMAIL_ADMIN);
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = $emailConfig['host']; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = $emailConfig['username']; // SMTP username
$mail->Password = $emailConfig['password']; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = isset($emailConfig['port']) ? $emailConfig['port'] : 587; // TCP port to connect to
$mail->From = $emailConfig['from'];
$mail->FromName = $emailConfig['fromName'];
$mail->addAddress(EMAIL_ADMIN); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $emailConfig['subject'];
$mail->Body = $messages;
if (!$mail->send()) {
logMessage('Message could not be sent.');
logMessage('Mailer Error: ' . $mail->ErrorInfo);
} else {
logMessage('Message has been sent');
}
}
function refreshToken($client)
{
global $credentialsPath;
$client->refreshToken($client->getRefreshToken());
if (file_put_contents($credentialsPath, $client->getAccessToken())) {
// chown($credentialsPath, 'www-dataa');
logStatusSuccess("Refreshing Token");
} else {
logStatusFailure("Refreshing Token");
closeSession(false);
}
return $client;
}
function getConnectedEmail($client)
{
$service = new Google_Service_Gmail($client);
return $service->users->getProfile('me')->emailAddress;
}