-
Notifications
You must be signed in to change notification settings - Fork 21
/
cwslack-stats.php
83 lines (74 loc) · 2.52 KB
/
cwslack-stats.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
<?php
/**
* Created by PhpStorm.
* User: jundis
* Date: 10/26/2017
* Time: 9:40 AM
*/
ini_set('display_errors', 1);
$mysql = mysqli_connect($dbhost, $dbusername, $dbpassword, $dbdatabase); //Connect MySQL
if (!$mysql) //Check for errors
{
if ($timeoutfix == true) {
cURLPost($_REQUEST["response_url"], array("Content-Type: application/json"), "POST", array("parse" => "full", "response_type" => "ephemeral","text" => "Connection Error: " . mysqli_connect_error()));
} else {
die("Connection Error: " . mysqli_connect_error()); //Return properly encoded arrays in JSON for Slack parsing.
}
die();
}
$sql = "CREATE TABLE IF NOT EXISTS stats (user VARCHAR(50) NOT NULL PRIMARY KEY, lastcommand VARCHAR(10), lastused DATETIME)";
if (mysqli_query($mysql, $sql)) {
//Table created successfully
} else {
echo "stats Table Creation Error: " . mysqli_error($mysql);
}
$statscommand = substr($_REQUEST['command'],1);
$statsuser = $_REQUEST['user_name'];
$sql = "SELECT ".$statscommand." FROM stats"; //SQL Query to insert new map
if(!mysqli_query($mysql,$sql))
{
$sql = "ALTER TABLE `stats` ADD COLUMN " . $statscommand . " INT DEFAULT 0";
if(mysqli_query($mysql,$sql))
{
// Inserted user
}
else
{
die("MySQL Error: " . mysqli_error($mysql));
}
}
$statsdate = date("Y-m-d H:i:s");
$sql = "SELECT * FROM `stats` WHERE `user` = '" . $statsuser . "'"; //SQL Query to select all users
$result = mysqli_query($mysql, $sql); //Run result
$rowcount = mysqli_num_rows($result);
if($rowcount > 1) //If there were too many rows matching query
{
die("Error: too many users somehow?"); //This should NEVER happen.
}
else if ($rowcount == 1) //If exactly 1 row is found.
{
$statsuserdata = mysqli_fetch_assoc($result);
$commandcount = $statsuserdata[$statscommand] + 1;
$sql = "UPDATE `stats` SET lastcommand = '" . $statscommand . "', lastused = '" . $statsdate . "', " . $statscommand ." = '" . $commandcount . "' WHERE `user` = '" . $statsuser . "'"; //SQL Query to insert new map
if(mysqli_query($mysql,$sql))
{
// Inserted user
}
else
{
die("MySQL Error: " . mysqli_error($mysql));
}
}
else
{
$sql = "INSERT INTO `stats` (`user`, `lastcommand`, `lastused`, `" . $statscommand . "`) VALUES ('" . $statsuser . "', '" . $statscommand . "', '" . $statsdate . "', 1);"; //SQL Query to insert new map
if(mysqli_query($mysql,$sql))
{
// Inserted user
}
else
{
die("MySQL Error: " . mysqli_error($mysql));
}
}
?>