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

Commit 6924b27

Browse files
committed
Merge branch 'master' of git.5d7.eu:KIMB-technologies/TaskTimeTerminate-SyncServer
2 parents 3166841 + c0ba986 commit 6924b27

File tree

8 files changed

+374
-21
lines changed

8 files changed

+374
-21
lines changed

VERSION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
latest
2-
0.3.7
3-
0.3
2+
0.4.0
3+
0.4
44
0

php/core/Share.php

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
/**
3+
* TaskTimeTerminate Sync-Server
4+
* https://github.com/KIMB-technologies/TaskTimeTerminate
5+
*
6+
* (c) 2020 KIMB-technologies
7+
* https://github.com/KIMB-technologies/
8+
*
9+
* released under the terms of GNU Public License Version 3
10+
* https://www.gnu.org/licenses/gpl-3.0.txt
11+
*/
12+
defined( 'TaskTimeTerminate' ) or die('Invalid Endpoint!');
13+
14+
class Share {
15+
16+
private JSONReader $shares;
17+
private Login $login;
18+
private String $error = "";
19+
20+
public function __construct( Login $login ) {
21+
$this->shares = new JSONReader('shares');
22+
$this->login = $login;
23+
24+
$this->checkInitGroup($this->login->getGroup());
25+
}
26+
27+
private function checkInitGroup(string $group) : void {
28+
if( !$this->shares->isValue([$group]) ){
29+
$this->shares->setValue([$group], array(
30+
'byme' => array(), // shared by me
31+
'withme' => array() // shared with me
32+
));
33+
}
34+
}
35+
36+
/**
37+
* Get the list of shares this user has created
38+
*/
39+
public function getShares() : array {
40+
return $this->shares->getValue([$this->login->getGroup(), 'byme']);
41+
}
42+
43+
/**
44+
* Add a share for this user (share a category with other group)
45+
*/
46+
public function addShare( string $category, string $group ) : void {
47+
if( $group !== $this->login->getGroup()
48+
&& $this->login->getGroupList()->isValue([$group])
49+
){
50+
if( in_array( $category, $this->getAllCategories() ) ){
51+
$this->checkInitGroup($group);
52+
53+
$byme = array(
54+
'category' => $category,
55+
'group' => $group
56+
);
57+
if( $this->shares->searchValue([$this->login->getGroup(), 'byme'], $byme) === false ){
58+
$this->shares->setValue([$this->login->getGroup(), 'byme', null], $byme);
59+
$this->shares->setValue([$group, 'withme', null], array(
60+
'category' => $category,
61+
'group' => $this->login->getGroup()
62+
));
63+
}
64+
else {
65+
$this->error = "Share already exists.";
66+
}
67+
}
68+
else {
69+
$this->error = "Unknown category to share";
70+
}
71+
}
72+
else {
73+
$this->error = "Invalid group to share with";
74+
}
75+
}
76+
77+
/**
78+
* Remove a share for this user (share a category with other group)
79+
*/
80+
public function removeShare( string $category, string $group ) : void {
81+
if( $group !== $this->login->getGroup()
82+
&& $this->login->getGroupList()->isValue([$group])
83+
){
84+
$this->checkInitGroup($group);
85+
86+
$byme = array(
87+
'category' => $category,
88+
'group' => $group
89+
);
90+
$pos = $this->shares->searchValue([$this->login->getGroup(), 'byme'], $byme);
91+
if( $pos !== false ){
92+
$this->shares->setValue([$this->login->getGroup(), 'byme', $pos], null);
93+
}
94+
95+
$withme = array(
96+
'category' => $category,
97+
'group' => $this->login->getGroup()
98+
);
99+
$pos = $this->shares->searchValue([$group, 'withme'], $withme);
100+
if( $pos !== false ){
101+
$this->shares->setValue([$group, 'withme', $pos], null);
102+
}
103+
}
104+
else {
105+
$this->error = "Invalid group to delete share for";
106+
}
107+
}
108+
109+
private function getAllCategories(){
110+
$stats = new TTTStats(['all'], API::getStorageDir($this->login->getGroup()));
111+
$combi = $stats->getAllResults()['combi'];
112+
return array_values(array_unique(array_column($combi, 'category')));
113+
}
114+
115+
public function getCategoriesAndGroups() : array {
116+
return array(
117+
'categories' => $this->getAllCategories(),
118+
'groups' => array_keys($this->login->getGroupList()->getArray())
119+
);
120+
}
121+
122+
public function getErrorMessage() : string {
123+
return $this->error;
124+
}
125+
126+
public function hasError() : bool {
127+
return !empty($this->error);
128+
}
129+
130+
/**
131+
* Get the list of shares shared with this user
132+
*/
133+
public function getSharedWithMe() : array {
134+
return $this->shares->getValue([$this->login->getGroup(), 'withme']);
135+
}
136+
}
137+
?>

php/core/Stats.php

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,47 @@ class Stats {
1818

1919
private Template $temp;
2020
private Login $login;
21+
private Share $share;
2122

2223
public function __construct( Template $temp, Login $login ) {
2324
$this->login = $login;
2425
$this->temp = $temp;
26+
$this->share = new Share($this->login);
2527

2628
$this->setUpHtml();
2729

2830
if( $_SERVER['REQUEST_METHOD'] === 'POST' ){
2931
$cmd = $this->paramsToCmd();
30-
if( !empty($cmd)){
32+
if( !empty($cmd) ){
3133
$data = new TTTStats($cmd, API::getStorageDir($this->login->getGroup()));
32-
$this->displayContent($data->getAllResults());
34+
$allData = $data->getAllResults();
35+
36+
// share
37+
if( isset($_POST['shares']) && is_array($_POST['shares']) ){
38+
$withme = $this->share->getSharedWithMe();
39+
$shares = array();
40+
foreach($_POST['shares'] as $sh ){
41+
if(is_string($sh)){
42+
$sh = explode('::', $sh);
43+
$gr = preg_replace('/[^A-Za-z0-9]/', '', $sh[0]);
44+
if(InputParser::checkCategoryInput($sh[1]) && !empty($gr) ){
45+
if( in_array( array(
46+
'category' => $sh[1],
47+
'group' => $gr
48+
) , $withme )
49+
){
50+
if(!isset($shares[$gr])){
51+
$shares[$gr] = array();
52+
}
53+
$shares[$gr][] = $sh[1];
54+
}
55+
}
56+
}
57+
}
58+
$this->addShares($allData, $cmd, $shares);
59+
}
60+
61+
$this->displayContent($allData);
3362
}
3463
}
3564
}
@@ -47,6 +76,34 @@ private function displayContent(array $data) : void {
4776
}
4877
}
4978

79+
private function addShares( array &$allData, array $cmd, array $shares) : void {
80+
if(in_array('-devices', $cmd)){
81+
$did = array_search('-devices', $cmd);
82+
unset($cmd[$did], $cmd[$did+1]);
83+
}
84+
if(!in_array('-cats', $cmd)){
85+
$cmd[] = '-cats';
86+
$cmd[] = '';
87+
}
88+
$cid = array_search('-cats', $cmd) + 1;
89+
90+
foreach($shares as $group => $cats){
91+
$cmd[$cid] = implode(',', $cats);
92+
$sd = new TTTStats($cmd, API::getStorageDir($group));
93+
$data = $sd->getAllResults();
94+
array_walk_recursive( $data, function (&$value, $key) use (&$group) {
95+
if(in_array($key, ['name', 'category', 'Name', 'Category', 'Other devices', 'device'])){
96+
$value = $group . '::' . $value;
97+
}
98+
});
99+
foreach(['table','plain','combi','today'] as $key ){
100+
if(isset($allData[$key]) && isset($data[$key]) ){
101+
$allData[$key] = array_merge($allData[$key], $data[$key]);
102+
}
103+
}
104+
}
105+
}
106+
50107
private function arrayToTable(array $data) : string {
51108
$table = "<table class=\"table table-striped table-responsive-sm statstable\">";
52109
$head = false;
@@ -162,6 +219,19 @@ private function setUpHtml(){
162219
$this->temp->setMultipleContent('Devices', $ds);
163220
}
164221

222+
$withme = $this->share->getSharedWithMe();
223+
if(!empty($withme)){
224+
$this->temp->setContent('SHARESDIABLE', '');
225+
$d = array();
226+
foreach($withme as $w){
227+
$d[] = array(
228+
'%%VALUE%%' => $w['group'] . '::' . $w['category'],
229+
'%%NAME%%' => '"' . $w['category'] . '" from "' . $w['group'] . '"'
230+
);
231+
}
232+
$this->temp->setMultipleContent('Shares', $d);
233+
}
234+
165235
$this->temp->setContent('GRAPHES', json_encode(
166236
array_values(array_map(
167237
function ($s) {

php/core/WebGUI.php

Lines changed: 80 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,28 +49,95 @@ public function accountManage() : void {
4949
$account = new Template('account');
5050
$this->mainTemp->includeTemplate($account);
5151

52-
// handle pw change
53-
if( !empty($_POST['type']) && $_POST['type'] === 'change'){
54-
$newPw = $this->param->loginPost('password');
52+
$share = new Share($this->login);
53+
54+
// handle pw change, share add
55+
if( !empty($_POST['type']) ){
56+
if($_POST['type'] === 'change'){
57+
$newPw = $this->param->loginPost('password');
58+
$account->setContent('NOTEDISABLE','');
59+
if(!empty($newPw)){
60+
if(Login::createNewGroup(
61+
$this->login->getGroupList(),
62+
$this->login->getGroup(),
63+
$newPw,
64+
$this->login->isAdmin()
65+
)){
66+
$account->setContent('NOTEMSG','Changed password!');
67+
}
68+
else{
69+
$account->setContent('NOTEMSG','Error changing password!');
70+
}
71+
}
72+
else{
73+
$account->setContent('NOTEMSG','Invalid password given!');
74+
}
75+
}
76+
else if($_POST['type'] === 'share'){
77+
$account->setContent('NOTEDISABLE','');
78+
if( !empty($_POST['category']) && !empty($_POST['group']) ){
79+
$cat = $_POST['category'];
80+
$gr = $this->param->loginPost('group');
81+
if( InputParser::checkCategoryInput($cat) ){
82+
$share->addShare($cat, $gr);
83+
if($share->hasError()){
84+
$account->setContent('NOTEMSG', $share->getErrorMessage());
85+
}
86+
else{
87+
$account->setContent('NOTEMSG','Share was added.');
88+
}
89+
}
90+
else{
91+
$account->setContent('NOTEMSG','Invalid category given.');
92+
}
93+
}
94+
else {
95+
$account->setContent('NOTEMSG','Please give category and user to share with.');
96+
}
97+
}
98+
}
99+
else if(!empty($_GET['remove']) && is_string($_GET['remove'])
100+
&& !empty($_GET['group']) && is_string($_GET['group'])
101+
){
55102
$account->setContent('NOTEDISABLE','');
56-
if(!empty($newPw)){
57-
if(Login::createNewGroup(
58-
$this->login->getGroupList(),
59-
$this->login->getGroup(),
60-
$newPw,
61-
$this->login->isAdmin()
62-
)){
63-
$account->setContent('NOTEMSG','Changed password!');
103+
$gr = preg_replace('/[^A-Za-z0-9]/', '', $_GET['group']);
104+
$cat = $_GET['remove'];
105+
106+
if( InputParser::checkCategoryInput($cat) ){
107+
$share->removeShare($cat, $gr);
108+
if($share->hasError()){
109+
$account->setContent('NOTEMSG', $share->getErrorMessage());
64110
}
65111
else{
66-
$account->setContent('NOTEMSG','Error changing password!');
112+
$account->setContent('NOTEMSG','Share was deleted.');
67113
}
68114
}
69115
else{
70-
$account->setContent('NOTEMSG','Invalid password given!');
116+
$account->setContent('NOTEMSG','Invalid category given while deleting share.');
71117
}
72118
}
73119

120+
$d = array();
121+
foreach($share->getCategoriesAndGroups() as $n => $v){
122+
$d[$n] = array();
123+
foreach($v as $e){
124+
$d[$n][] = array(
125+
"NAME" => $e,
126+
"VALUE" => $e
127+
);
128+
}
129+
}
130+
$account->setMultipleContent('Category', $d['categories']);
131+
$account->setMultipleContent('Group', $d['groups']);
132+
$shares = array();
133+
foreach($share->getShares() as $s){
134+
$shares[] = array(
135+
'CATEGORY' => $s['category'],
136+
'GROUP' => $s['group']
137+
);
138+
}
139+
$account->setMultipleContent('Shares', $shares);
140+
74141
if($this->login->isAdmin()){
75142
$account->setContent('ISADMIN', '');
76143

php/core/templates/account.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@
88
"%%ANAME%%" : "Account",
99
"%%AADMIN%%" : "No",
1010
"%%AID%%" : "-1"
11+
},
12+
"Shares" : {
13+
"%%CATEGORY%%" : "",
14+
"%%GROUP%%" : ""
15+
},
16+
"Category" : {
17+
"%%VALUE%%" : "",
18+
"%%NAME%%" : ""
19+
},
20+
"Group" : {
21+
"%%VALUE%%" : "",
22+
"%%NAME%%" : ""
1123
}
1224
}
1325
}

0 commit comments

Comments
 (0)