Skip to content
This repository was archived by the owner on Mar 30, 2024. It is now read-only.

Commit 8c54d56

Browse files
committed
Finish #10
1 parent 3687290 commit 8c54d56

File tree

4 files changed

+117
-8
lines changed

4 files changed

+117
-8
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
latest
2-
0.4.6
2+
0.5.0
33
0.4
44
0

php/api/ics.php

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,72 @@
1111
*/
1212
define( 'TaskTimeTerminate', 'API' );
1313

14-
die("Coming soon!");
15-
1614
require_once( __DIR__ . '/../core/load.php' );
1715

18-
$login = new Login();
16+
if(
17+
!empty($_GET['group']) && !empty($_GET['token'])
18+
&&
19+
is_string($_GET['group']) && is_string($_GET['token'])
20+
&&
21+
preg_match(API::GROUP_NAME_PREG, $_GET['group']) === 1 && strlen($_GET['token']) === 50
22+
){
23+
$login = new Login($_GET['group'], $_GET['token']);
24+
25+
if($login->isLoggedIn()){
26+
$dataAccess = new DataAccess($login, new Share($login));
27+
28+
// parse range
29+
$rFrom = "";
30+
$rTo = "";
31+
if(isset($_GET['range']) && is_string($_GET['range'])){
32+
if(strpos($_GET['range'], ',') === false ){
33+
$rFrom = $_GET['range'];
34+
}
35+
else{
36+
$parts = explode(',', $_GET['range']);
37+
$rFrom = $parts[0];
38+
$rTo = $parts[1];
39+
}
40+
}
1941

20-
if($login->isLoggedIn()){
21-
$cal = new Calendar($login);
42+
// parse devices
43+
$devices = array();
44+
if(isset($_GET['devices']) && is_string($_GET['devices'])){
45+
$devices = explode(',', $_GET['devices']);
46+
}
2247

48+
// set parsed filters
49+
$dataAccess->setParams(
50+
$_GET['time'], $rFrom, $rTo,
51+
$_GET['cats'] ?? "", $_GET['names'] ?? "", $devices
52+
);
53+
54+
// add shares
55+
if( isset($_GET['shares']) && is_string($_GET['shares']) ){
56+
$dataAccess->requestShare(explode(',', $_GET['shares']));
57+
}
58+
59+
if(!$dataAccess->hasError() ){
60+
// create calendar
61+
$cal = new Calendar($login);
62+
$ics = $cal->generateICS($dataAccess);
63+
64+
// output
65+
header('Content-type: text/calendar; charset=utf-8');
66+
header('Content-Disposition: inline; filename=ttt.ics');
67+
die($ics);
68+
}
69+
else{
70+
// error message
71+
http_response_code(400);
72+
header('Content-Type: text/plain; charset=utf-8');
73+
die("Error – Invalid filter!");
74+
}
75+
}
2376
}
77+
78+
// error message
79+
http_response_code(403);
80+
header('Content-Type: text/plain; charset=utf-8');
81+
die("Error – Failed to authenticate!");
2482
?>

php/core/Calendar.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,38 @@ public function __construct( Login $login ) {
1717
$this->login = $login;
1818
}
1919

20+
public function generateICS(DataAccess $da) : string {
21+
$result = $da->getData();
22+
return $this->toICS($result['plain']);
23+
}
24+
25+
private function toICS(array $plainData) : string {
26+
$cal = '';
27+
$eol = "\r\n";
28+
29+
$cal .= 'BEGIN:VCALENDAR' . $eol;
30+
$cal .= 'VERSION:2.0' . $eol;
31+
$cal .= 'PRODID:https://github.com/KIMB-technologies/TaskTimeTerminateServer' . $eol;
32+
$cal .= 'CALSCALE:GREGORIAN' . $eol;
33+
$cal .= 'METHOD:PUBLISH' . $eol;
34+
35+
$tz = date_default_timezone_get();
36+
37+
foreach($plainData as $dat ){
38+
$cal .= 'BEGIN:VEVENT' . $eol;
39+
$cal .= 'UID:' . uniqid() . '@ttt-server' . $eol;
40+
41+
$cal .= 'SUMMARY:' . $dat['category'] . ' - ' . $dat['name'] . $eol;
42+
$cal .= 'DTSTART;TZID=' . $tz . ':'. date('Ymd', $dat['begin']) .'T'. date('His', $dat['begin']) .'Z' . $eol;
43+
$cal .= 'DTEND;TZID=' . $tz . ':'. date('Ymd', $dat['end']) .'T'. date('His', $dat['end']) .'Z' . $eol;
44+
45+
$cal .= 'END:VEVENT' . $eol;
46+
}
47+
48+
$cal .= 'END:VCALENDAR' . $eol;
49+
return $cal;
50+
}
51+
2052
public function getLink(DataAccess $da) : string {
2153
$params = array(
2254
'time' => 'all'
@@ -30,6 +62,9 @@ public function getLink(DataAccess $da) : string {
3062
. "&" . "token=" . $this->login->getGroupList()->getValue([$this->login->getGroup(), "caltoken"]);
3163

3264
foreach($params as $key => $val){
65+
if(is_array($val)){
66+
$val = implode(',', $val);
67+
}
3368
$query .= "&" . $key . "=" . urlencode($val);
3469
}
3570

php/core/Login.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,13 @@ public function __construct( string $group = '', string $token = '', string $cli
2424
if(!empty($group) && !empty($client) && !empty($token)){
2525
$this->apiClientLogin($group, $client, $token);
2626
}
27-
else if(!empty($group) && !empty($token)){
28-
$this->sessionLogin($group, $token);
27+
else if(!empty($group) && !empty($token) ){
28+
if(TaskTimeTerminate === 'GUI'){
29+
$this->sessionLogin($group, $token);
30+
}
31+
else {
32+
$this->calendarLogin($group, $token);
33+
}
2934
}
3035
else if( TaskTimeTerminate === 'GUI' && session_status() === PHP_SESSION_ACTIVE ){
3136
$this->userSessionLogin();
@@ -46,6 +51,17 @@ private function apiClientLogin(string $group, string $client, string $token) :
4651
$this->logUserOut();
4752
}
4853

54+
public function calendarLogin(string $group, string $token) : void {
55+
if( $this->groupList->isValue([$group]) ){
56+
$grToken = $this->groupList->getValue([$group, 'caltoken']);
57+
if( !empty($grToken) && $token === $grToken ){
58+
$this->logUserIn($group);
59+
return;
60+
}
61+
}
62+
$this->logUserOut();
63+
}
64+
4965
public function sessionLogin(string $group, string $token) : void {
5066
if( $this->groupList->isValue([$group]) ){
5167
$sid = $this->groupList->searchValue([$group, 'sessions'], $token, 'token');

0 commit comments

Comments
 (0)