-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
344 lines (315 loc) · 12.8 KB
/
index.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php
require_once './vendor/autoload.php';
$cfg = [
'appId' => 'in the config',
'appSecret' => 'in the config',
];
require_once './config.php';
function getClient($appName, $scopes, $appId, $appSecret) {
$client = new Google_Client();
$client->setApplicationName($appName);
$client->setScopes($scopes);
$client->setClientId($appId);
$client->setClientSecret($appSecret);
$client->setRedirectUri(getCurrentUrl());
return $client;
}
function getCurrentUrl($queryParams = false)
{
$php_request_uri = ($queryParams)
? htmlentities(
substr($_SERVER['REQUEST_URI'], 0, strcspn($_SERVER['REQUEST_URI'], "\n\r")),
ENT_QUOTES
)
: dirname($_SERVER['PHP_SELF']).'/';
$protocol = (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on')
? 'https://'
: 'http://';
$host = $_SERVER['HTTP_HOST'];
$port = ($_SERVER['SERVER_PORT'] != '' &&
(($protocol == 'http://' && $_SERVER['SERVER_PORT'] != '80') ||
($protocol == 'https://' && $_SERVER['SERVER_PORT'] != '443')))
? ':' . $_SERVER['SERVER_PORT']
: '';
return $protocol . $host . $port . $php_request_uri;
}
function lastDay($month = '', $year = '', $format = 'Y-m-d H:i:s')
{
if (empty($month))
$month = date('m');
if (empty($year))
$year = date('Y');
$result = strtotime("{$year}-{$month}-01");
$result = strtotime('-1 second', strtotime('+1 month', $result));
return date($format, $result);
}
session_start();
header('Content-type: text/html; charset=utf-8');
$gClient = getClient(
'GCal Billing', Google_Service_Calendar::CALENDAR_READONLY,
$cfg['appId'], $cfg['appSecret']
);
if(!empty($_GET['action'])) {
if($_GET['action'] == 'logout') {
unset($_SESSION['googleToken']);
header('Location: '.getCurrentUrl());
exit;
}
}
if(!empty($_GET['code'])) {
try {
$token = $gClient->authenticate($_GET['code']);
} catch(Exception $e) {
$token = '';
}
if(!empty($token))
$_SESSION['googleToken'] = $token;
header('Location: '.getCurrentUrl());
exit;
}
if (isset($_SESSION['googleToken'])) {
$gClient->setAccessToken($_SESSION['googleToken']);
if($gClient->isAccessTokenExpired()) {
try {
$gClient->refreshToken($gClient->getRefreshToken());
} catch(Exception $e) {
header('Location: '.$gClient->createAuthUrl());
exit;
}
}
$calId = isset($_GET['calendar']) ? $_GET['calendar'] : '';
$clientName = isset($_GET['client']) ? $_GET['client'] : '';
$hourlyRate = isset($_GET['rate']) ? (float)$_GET['rate'] : 0;
$dateChangeHour = isset($_GET['datechangehour']) ? (int)$_GET['datechangehour'] : 4;
$calendarService = new Google_Service_Calendar($gClient);
$calendarList = $calendarService->calendarList->listCalendarList();
if(!empty($calId)) {
$isValidCal = false;
foreach ($calendarList as $calendar) {
if($calendar->id == $calId) {
$isValidCal = true;
break;
}
}
if(!$isValidCal)
die(':(');
$range = @$_GET['range'];
if(empty($range))
$range = 'now';
$firstDay = new DateTime($range);
$firstDay->setDate($firstDay->format('Y'), $firstDay->format('m'), 1);
$firstDay->setTime(0, 0, 0);
$numRange = [
$firstDay->getTimestamp(),
(int)lastDay($firstDay->format('m'), $firstDay->format('Y'), 'U')
];
$range = [
$firstDay->format(DATE_RFC3339),
lastDay($firstDay->format('m'), $firstDay->format('Y'), DATE_RFC3339)
];
$query = [
'singleEvents' => true,
'orderBy' => 'startTime',
'maxResults' => 2500, // max
'timeMin' => $range[0],
'timeMax' => $range[1],
];
if(!empty($clientName))
$query['q'] = $clientName;
$events = $calendarService->events->listEvents($calId, $query);
$eventsList = [];
while(true) {
foreach($events->getItems() as $event)
$eventsList[] = $event;
$pageToken = $events->getNextPageToken();
if ($pageToken)
$events = $calendarService->events->listEvents(
$calId,
array_merge($query, ['pageToken' => $pageToken])
);
else
break;
}
$sum = 0;
$entries = $entriesByDate = [];
foreach ($eventsList as $event) {
if(!empty($clientName) && stripos($event->summary, $clientName) === false)
continue;
$startTime = new DateTime($event->start->dateTime);
$endTime = new DateTime($event->end->dateTime);
$diff = $endTime->getTimestamp() - $startTime->getTimestamp();
if($diff >= 24 * 3600)
continue;
$sum += $diff;
$diffMinutes = ($diff % 3600)/60;
$diffMinutes = ($diffMinutes > 0)
? ':'.str_pad($diffMinutes, 2, '0', STR_PAD_LEFT)
: '';
$date = $startTime->format('j');
if($startTime->format('G') < $dateChangeHour && $date > 1)
$date--;
$title = trim(str_ireplace($clientName, '', $event->summary));
$entry = [
'title' => empty($title) ? '' : ' — '.$title,
'date' => $date,
'startTime' => $startTime->format('H:i'),
'endTime' => $endTime->format('H:i'),
'diff' => floor($diff / 3600).$diffMinutes,
];
$entries[] = $entry;
if(empty($entriesByDate[$date]))
$entriesByDate[$date] = [];
$entriesByDate[$date][] = $entry;
}
$sumHours = floor($sum / 3600);
$sumMinutes = ($sum % 3600) / 60;
$adjustMinutes = 0;
// rounding to 30 m, >= 15 means +
$adjustMinutes = $sumMinutes % 30;
if($adjustMinutes > 0) {
if($adjustMinutes >= 15) {
$adjustMinutes = 30 - $adjustMinutes;
$sumMinutes += $adjustMinutes;
if($sumMinutes == 60) {
$sumMinutes = 0;
$sumHours++;
}
} else {
$adjustMinutes = -$adjustMinutes;
$sumMinutes += $adjustMinutes;
}
$sum += $adjustMinutes * 60;
}
$sumMinutes = ($sumMinutes > 0) ? ':'.str_pad($sumMinutes, '0', 2) : '';
$sumWages = number_format($sum * $hourlyRate / 3600, 2);
}
}
?><!doctype html>
<html>
<head>
<title>Billing</title>
<link href='http://fonts.googleapis.com/css?family=Archivo+Narrow:400,700&subset=latin,cyrillic' rel='stylesheet' type='text/css' />
<style>
body {
margin: 20px 40px;
}
html, body, caption, th, td, p, select, option, li, fieldset, button, input, a {
font-family: "Archivo Narrow", Tahoma, sans-serif;
font-size: 1em;
font-weight: 400;
}
th {
font-weight: 700;
}
table, th, td {
border: 0;
}
th, td {
padding: 5px 10px;
}
</style>
<script>
function showhide(id) {
var el = document.getElementById(id);
el.style.display = (el.style.display == 'none') ? 'block' : 'none';
}
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-1931660-8']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body>
<h1>Billing</h1>
<?php
if (!isset($_SESSION['googleToken'])) { ?>
<a href="<?=$gClient->createAuthUrl()?>">
Please login to your Google Account. Nothing is saved on the server.
</a>
<?php } elseif(!empty($calId)) { ?>
<form method="get" action="">
<fieldset>
<legend>Choose events</legend>
<p>
Calendar:
<select name="calendar" onchange="this.form.submit()">
<?php foreach ($calendarList as $calendar) { ?>
<option value="<?=$calendar->id?>"<?=($calId == $calendar->id ? ' selected' : '')?>><?=$calendar->summary?></option>
<?php } ?>
</select>
Month:
<select name="range" onchange="this.form.submit()">
<?php for($i = 0; $i < 12; $i++) {
$d = new DateTime("first day of -$i month");
$d->setTime(0, 0, 0); ?>
<option value="<?=$d->format(DATE_ATOM)?>"<?=($d->getTimestamp() == $numRange[0] ? ' selected' : '')?>><?=$d->format('F Y')?></option>
<?php } ?>
</select>
Date change hour: <input type="text" name="datechangehour" value="<?=$dateChangeHour?>" size="2" />:00
Rate: <input type="text" name="rate" value="<?=$hourlyRate?>" size="5" />
Client: <input type="text" name="client" value="<?=$clientName?>" />
<input type="submit" value="Calculate" />
</p>
<p><small>Expected calendar entry title format: «[Client] [Optional comments]».
Enter hourly rate to see expected wage.
Each entry is rounded to 15 minutes.
The total time is rounded to the closest 30 minutes, up or down.
Date change hour makes sense if it's between 0 and 12.
</small></p>
<p><small>Privacy policy: nothing is saved on the server.</small></p>
<p><a href="?action=logout">Log out</a></p>
</fieldset>
</form>
<p><button onclick="showhide('t')">Show/hide table</button></p>
<table id="t" style="display: none">
<caption>
<?=date('Y-m-d', $numRange[0])?>–<?=date('Y-m-d', $numRange[1])?>
</caption>
<thead>
<tr><th>Date</th><th>Start</th><th>End</th><th>Time</th><th>Comments</th></tr>
</thead>
<tbody>
<?php $lastDate = ''; foreach($entries as $item) { ?>
<tr>
<td><?php if($item['date'] != $lastDate) { echo $item['date']; $lastDate = $item['date']; }?></td>
<td><?=$item['startTime']?></td><td><?=$item['endTime']?></td>
<td><?=$item['diff']?></td><td><?=$item['title']?></td>
</tr>
<?php } ?>
</tbody>
</table>
<p><button onclick="showhide('l')">Show/hide list</button></p>
<ul id="l">
<?php foreach($entriesByDate as $date => $items){ ?>
<li>
<?=$date?>.
<?php foreach($items as $n => $item){ ?>
<?=$item['startTime']?>–<?=$item['endTime']?> =
<?=trim( $item['diff'].$item['title'].($n < count($items)-1 ? '; ' : '') )?>
<?php } ?>
</li>
<?php } ?>
</ul>
<p><strong>Total:</strong> <?=$sumHours.$sumMinutes?> h <?=($adjustMinutes != 0 ? " (adjusted by $adjustMinutes m)" : '')?></p>
<?php if($sumWages > 0) { ?><p><small>(<?=$sumWages?> coins)</small></p><?php } ?>
<?php } else { ?>
<form method="get" action="">
<fieldset>
<legend>Choose events</legend>
<p>Calendar:
<select name="calendar" onchange="this.form.submit()">
<?php foreach ($calendarList as $calendar) { ?>
<option value="<?=$calendar->id?>"><?=$calendar->summary?></option>
<?php } ?>
</select>
<input type="submit" value="Continue" />
</p>
</fieldset>
</form>
<?php } ?>
</body>
</html>