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 DataAccess {
15+
16+ private Login $ login ;
17+ private Share $ share ;
18+
19+ private bool $ error = false ;
20+
21+ private array $ cmd = array ();
22+ private array $ cmdSemantical = array ();
23+
24+ private array $ allShares = array ();
25+
26+ public function __construct ( Login $ login , Share $ share ) {
27+ $ this ->login = $ login ;
28+ $ this ->share = $ share ;
29+ }
30+
31+ public function setParams (string $ time , string $ rFrom , string $ rTo , string $ cats , string $ names , array $ devices ) : void {
32+ $ this ->cmd = array ();
33+ $ this ->cmdSemantical = array ();
34+
35+ $ times = array ("today " , "day " , "week " , "month " , "range " , "all " );
36+ if ( isset ($ time ) && in_array ($ time , $ times )){
37+ $ this ->cmd [] = $ time ;
38+ $ this ->cmdSemantical ['time ' ] = $ time ;
39+ }
40+ else {
41+ $ this ->error = true ;
42+ }
43+
44+ if ($ time === "range " ){
45+ if (
46+ !empty ($ rFrom ) && !empty ($ rTo )
47+ &&
48+ preg_match ( TTTStatsData::DATE_PREG , $ rFrom ) === 1 && preg_match ( TTTStatsData::DATE_PREG , $ rTo ) === 1
49+ ){
50+ $ this ->cmd [] = $ rFrom ;
51+ $ this ->cmd [] = $ rTo ;
52+ $ this ->cmdSemantical ['range ' ] = array ($ rFrom , $ rTo );
53+ }
54+ else if ( !empty ($ rFrom ) && preg_match ( TTTStatsData::DATE_PREG , $ rFrom ) === 1 ){
55+ $ this ->cmd [] = $ rFrom ;
56+ $ this ->cmdSemantical ['range ' ] = $ rFrom ;
57+ }
58+ else if ( !empty ($ rTo ) && preg_match ( TTTStatsData::DATE_PREG , $ rTo ) === 1 ){
59+ $ this ->cmd [] = $ rTo ;
60+ $ this ->cmdSemantical ['range ' ] = $ rTo ;
61+ }
62+ else {
63+ $ this ->error = true ;
64+ }
65+ }
66+
67+ if (!empty ($ cats ) && preg_match ('/^[A-Za-z\-\,]+$/ ' , $ cats ) === 1 ){
68+ $ this ->cmd [] = '-cats ' ;
69+ $ this ->cmd [] = $ cats ;
70+ $ this ->cmdSemantical ['cats ' ] = $ cats ;
71+ }
72+
73+ if (!empty ($ names ) && preg_match ('/^[A-Za-z0-9\_\-\,]+$/ ' , $ names ) === 1 ){
74+ $ this ->cmd [] = '-names ' ;
75+ $ this ->cmd [] = $ names ;
76+ $ this ->cmdSemantical ['names ' ] = $ names ;
77+ }
78+
79+ if (!empty ($ _POST ["devices " ]) && is_array ($ _POST ["devices " ])){
80+ $ dev = implode (', ' , $ _POST ["devices " ]);
81+ if (preg_match ('/^[A-Za-z0-9\-\,]+$/ ' , $ dev ) === 1 ){
82+ $ this ->cmd [] = '-devices ' ;
83+ $ this ->cmd [] = $ dev ;
84+ $ this ->cmdSemantical ['devices ' ] = $ dev ;
85+ }
86+ else {
87+ $ this ->error = true ;
88+ }
89+ }
90+ }
91+
92+ public function requestShare (array $ requests ) : void {
93+ $ this ->shares = array ();
94+ $ this ->cmdSemantical ['shares ' ] = array ();
95+
96+ // share
97+ $ withme = $ this ->share ->getSharedWithMe ();
98+ foreach ($ requests as $ sh ){
99+ if (is_string ($ sh )){
100+ $ sh = explode (':: ' , $ sh );
101+ $ gr = preg_replace ('/[^A-Za-z0-9]/ ' , '' , $ sh [0 ]);
102+ if (InputParser::checkCategoryInput ($ sh [1 ]) && !empty ($ gr ) ){
103+ if ( in_array ( array (
104+ 'category ' => $ sh [1 ],
105+ 'group ' => $ gr
106+ ) , $ withme )
107+ ){
108+ if (!isset ($ this ->shares [$ gr ])){
109+ $ this ->shares [$ gr ] = array ();
110+ }
111+ $ this ->shares [$ gr ][] = $ sh [1 ];
112+
113+ $ this ->cmdSemantical ['shares ' ][] = $ gr . ':: ' . $ sh [1 ];
114+ }
115+ }
116+ }
117+ }
118+ $ this ->cmdSemantical ['shares ' ] = implode (', ' , $ this ->cmdSemantical ['shares ' ]);
119+ }
120+
121+ public function getCmd () : array {
122+ return $ this ->cmd ;
123+ }
124+
125+ public function getCmdSemantical () : array {
126+ return $ this ->cmdSemantical ;
127+ }
128+
129+ public function hasError () : bool {
130+ return $ this ->error ;
131+ }
132+
133+
134+ public function getData () : array {
135+ if ( !$ this ->hasError () && !empty ($ this ->cmd ) ){
136+ $ data = new TTTStats ($ this ->cmd , API ::getStorageDir ($ this ->login ->getGroup ()));
137+ $ allData = $ data ->getAllResults ();
138+
139+ if (!empty ($ this ->shares )){
140+ $ this ->addShares ($ allData );
141+ }
142+
143+ return $ allData ;
144+ }
145+ else {
146+ return array ();
147+ }
148+ }
149+
150+ private function addShares ( array &$ allData ) : void {
151+ $ cmdC = $ this ->cmd ;
152+
153+ if (in_array ('-devices ' , $ cmdC )){
154+ $ did = array_search ('-devices ' , $ cmdC );
155+ unset($ cmdC [$ did ], $ cmdC [$ did +1 ]);
156+ }
157+ if (!in_array ('-cats ' , $ cmdC )){
158+ $ cmdC [] = '-cats ' ;
159+ $ cmdC [] = '' ;
160+ }
161+ $ cid = array_search ('-cats ' , $ cmdC ) + 1 ;
162+
163+ foreach ($ this ->shares as $ group => $ cats ){
164+ $ cmdC [$ cid ] = implode (', ' , $ cats );
165+ $ sd = new TTTStats ($ cmdC , API ::getStorageDir ($ group ));
166+ $ data = $ sd ->getAllResults ();
167+ array_walk_recursive ( $ data , function (&$ value , $ key ) use (&$ group ) {
168+ if (in_array ($ key , ['name ' , 'category ' , 'Name ' , 'Category ' , 'Other devices ' , 'device ' ])){
169+ $ value = $ group . ':: ' . $ value ;
170+ }
171+ });
172+ foreach (['table ' ,'plain ' ,'combi ' ,'today ' ] as $ key ){
173+ if (isset ($ allData [$ key ]) && isset ($ data [$ key ]) ){
174+ $ allData [$ key ] = array_merge ($ allData [$ key ], $ data [$ key ]);
175+ }
176+ }
177+ }
178+ }
179+
180+ }
181+ ?>
0 commit comments