-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
317 lines (271 loc) · 9.18 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
<?php
include('includes/config.php');
$error = false;
if(!isset($_POST['text'])){
echo "Error.";
exit(0);
}
if($_POST['channel_name'] === 'directmessage' || $_POST['channel_name'] === ''){
$words = explode(' ', $_POST['text']);
$command = array_shift($words);
$params = implode($words, ' ');
$user = $_POST['user_name'];
if($command === 'me'){
$old_times = R::findAll('times',' user = ? ORDER BY created_at DESC, channel ASC LIMIT 50', array($user));
if($old_times){
foreach($old_times as $old_time){
$response .= $old_time->created_at.': '.$old_time->channel.' '.$old_time->hours.'h - '.$old_time->description;
$response .= "\n";
}
}else{
$response = 'Nothing here yo';
$error = true;
}
}else if($command === 'listprojects'){
$channels = R::findAll('channels');
if($channels){
foreach($channels as $channel){
$response .= '#'.$channel->channel.' ';
}
}
}else if($command === '' || $command === 'help'){
$response = help();
}
#$response = json_encode($command);
#echo "Hey, you can't message this bot. Use a shared channel.";
#$error = true;
#print_r($_POST);
}else{
$words = explode(' ', $_POST['text']);
$channel = $_POST['channel_name'];
$command = array_shift($words);
$params = implode($words, ' ');
$user = $_POST['user_name'];
# functionality start
if($command === 'add'){
$is_enabled = R::findOne('channels','channel = ?', array($channel));
if(!$is_enabled){
$response = 'This is not a timedude channel! This was your original message, put it where it belongs: '.$_POST['text'];
$error = true;
}else{
$args = explode(' ', $params);
# which date?
if(strtolower($args[0]) === 'yesterday'){
$unit = 'hours';
$date_to_add = date('Y-m-d', strtotime('yesterday'));
array_shift($args); #remove the word yesterday
}else if(seems_to_be_date($args[0])){
$unit = 'hours';
$date_to_add = $args[0];
array_shift($args); #remove the word containing the specific date
}else if(strtolower($args[0]) === 'cost'){
$unit = 'cost';
$date_to_add = date('Y-m-d');
array_shift($args); #remove the word cost
}else{
$unit = 'hours';
$date_to_add = date('Y-m-d'); #today
}
$args[0] = trim($args[0],'h');
if($unitvalue = floatval($args[0])){
array_shift($args); #remove first word, contains the hours/cost-value
if(strlen($args[0]) > 2){
$times = R::dispense('times');
$times->channel = $channel;
$times->user = $user;
if($unit === 'cost'){
$times->cost = $unitvalue;
$times->hours = 0;
}else{
$times->cost = NULL;
$times->hours = $unitvalue;
}
$times->description = implode($args, ' ');
$times->created_at = $date_to_add;
R::store($times);
if($unit === 'cost'){
$response = 'Added '.$unitvalue.'SEK as a cost to this project for '.$date_to_add.' '.random_emoji()."\nType clear if you wish to remove this entire day from your reports.";
}else{
$response = 'Added '.$unitvalue.'h to this project for '.$date_to_add.' '.random_emoji()."\nType clear if you wish to remove this entire day from your reports.";
}
}else{
$response = 'You need to provide some kind of description, like 8h haxxing';
$error = true;
}
}else{
$response = 'You need to start with some hours, like 8h haxxing';
$error = true;
}
}
}else if($command === 'clear'){
# which date?
if(strtolower($params) === 'yesterday'){
$date_to_add = date('Y-m-d', strtotime('yesterday'));
}else if(seems_to_be_date($params)){
$date_to_add = $params;
}else{
$date_to_add = date('Y-m-d'); #today
}
$old_times = R::findAll('times',' user = ? AND channel = ? AND created_at = ?', array($user, $channel, $date_to_add));
if($old_times){
foreach($old_times as $old_time){
R::trash($old_time);
}
$response = 'Ok, cleared your day. You currently have 0 hours reported '.$date_to_add;
}else{
$response = 'No need to clear your day, there\'s nothing there...';
$error = true;
}
}else if($command === 'export'){
$csvfile = $channel."_".date('Ymdhis');
R::exec("SELECT created_at, user, hours, description FROM times WHERE channel = ? ORDER BY created_at, user INTO OUTFILE '".$csvpath.$csvfile.".csv' FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\n';", array($channel));
$response = "Download the full report for this project here:\nhttp://miscbox.earthpeople.se/timedude/csv/".$csvfile.".csv";
}else if($command === 'list'){
if((int)$params > 0){
$limit = $params;
}else{
$limit = 100;
}
$old_times = R::findAll('times',' user = ? AND channel = ? ORDER BY created_at DESC LIMIT '.$limit, array($user, $channel));
if($old_times){
foreach($old_times as $old_time){
$response .= $old_time->created_at;
$response .= " ";
$response .= $old_time->hours;
$response .= "h: ";
$response .= $old_time->description;
$response .= "\n";
}
$response .= "\n\n";
$response .= "http://miscbox.earthpeople.se/timedude/usercal/?uid=".md5($user);
}else{
$response = 'Nothing, nada, etc';
$error = true;
}
}else if($command === 'listall'){
if((int)$params > 0){
$limit = $params;
}else{
$limit = 50;
}
$old_times = R::findAll('times',' channel = ? ORDER BY created_at DESC LIMIT '.$limit, array($channel));
if($old_times){
foreach($old_times as $old_time){
$response .= $old_time->created_at;
$response .= " ";
$response .= $old_time->hours;
$response .= "h: ";
$response .= $old_time->user;
$response .= ": ";
$response .= $old_time->description;
$response .= "\n";
}
}else{
$response = 'Nothing, nada, etc';
$error = true;
}
}else if($command === 'enable'){
$old_channel = R::findOne('channels','channel = ?', array($channel));
if($old_channel){
$response = 'Channel already enabled';
}else{
$channels = R::dispense('channels');
$channels->channel = $channel;
R::store($channels);
$response = 'Ok, enabled this channel';
}
}else if($command === 'disable'){
$old_channel = R::findOne('channels','channel = ?', array($channel));
if($old_channel){
R::trash($old_channel);
$response = 'Ok, disabled this channel';
}else{
$response = 'This channel was already disabled';
}
}else if($command === 'reminder'){
if(strtoupper($params) === 'OFF'){
$old_reminder = R::findOne('reminders',' user = ? AND channel = ?', array($user, $channel));
if($old_reminder){
R::trash($old_reminder);
$response = 'Ok, you won\'t get any more reminders';
}else{
$response = 'You have do reminders but sure whatever';
}
}else if($time = valid_time($params)){
$old_reminder = R::findOne('reminders',' user = ? AND channel = ?', array($user, $channel));
if($old_reminder){
R::trash($old_reminder);
}
$reminders = R::dispense('reminders');
$reminders->channel = $channel;
$reminders->user = $user;
$reminders->time = $time;
$reminders->created_at = date('Y-m-d');
R::store($reminders);
$response = 'Yeah, your reminder for this project is set for '.$params;
}else{
$response = 'Uh I could not understand you. Enter time, like 16:30 or OFF';
$error = true;
}
}else if($command === 'help'){
$response = help();
}else{
$response = ':paperclip: /timedude help for man page';
$error = true;
}
}
function help(){
$str = "Available commands in a channel:\n";
$str .= "add <HOURS> i made a funny gif\n";
$str .= "add ".date('Y-m-d', strtotime('yesterday'))." <HOURS> i made a funny gif\n";
$str .= "add cost 5000 hosting for nice website (amount in SEK pls)\n";
$str .= "clear (will clear your hours in this project for today)\n";
$str .= "export (generates a csv of the entire project)\n";
$str .= "list (shows your reported stuff for this project)\n";
$str .= "listall (shows everyones stuff for this project)\n";
$str .= "reminder <hh:mm or OFF>\n";
$str .= "enable (enabled time reporting for this channel)\n";
$str .= "disable (disable time reporting for this channel)\n";
$str .= "help\n\n";
$str .= "Available commands to @slackbot:\n";
$str .= "me (shows you all your hours reported lately)\n";
$str .= "listprojects (shows you all timedude projects)\n";
$str .= "help";
return $str;
}
if($error){
$errors = R::dispense('errors');
$errors->raw_post = json_encode($_POST);
$errors->user = $user;
$errors->created_at = date('Y-m-d H:i:s');
R::store($errors);
}
# respond!
echo $response;
function valid_time($str = ''){
$hours = null;
$minutes = null;
$timeparts = explode(':', $str);
if(isset($timeparts[0])){
if($timeparts[0] <= 23 && $timeparts[1] >= 00){
$hours = $timeparts[0];
}
}
if(isset($timeparts[1])){
if($timeparts[1] < 59 && $timeparts[1] >= 00){
$minutes = $timeparts[1];
}
}
if($hours && $minutes){
return $hours.':'.$minutes;
}else{
return false;
}
}
function seems_to_be_date($date = ''){
return preg_match( '#^(?P<year>\d{2}|\d{4})([- /.])(?P<month>\d{1,2})\2(?P<day>\d{1,2})$#', $date, $matches ) && checkdate($matches['month'],$matches['day'],$matches['year']);
}
function random_emoji(){
$emojis = array('smile','laughing','blush','smiley','heart_eyes','kissing_heart','kissing_closed_eyes','raised_hands','clap','muscle','mudderverk');
return ':'.$emojis[array_rand($emojis)].':';
}