-
Notifications
You must be signed in to change notification settings - Fork 21
/
cwslack-follow.php
147 lines (126 loc) · 4.38 KB
/
cwslack-follow.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
<?php
/*
CWSlack-SlashCommands
Copyright (C) 2018 jundis
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
ini_set('display_errors', 1); //Display errors in case something occurs
header('Content-Type: application/json'); //Set the header to return JSON, required by Slack
require_once 'config.php';
require_once 'functions.php';
$link=0;
if(empty($_REQUEST['method']) || ($_REQUEST['method'] != $followtoken && $_REQUEST['method'] != $unfollowtoken)){
if(empty($_REQUEST['token']) || $_REQUEST['token'] != $slackfollowtoken) die("Slack token invalid."); //If Slack token is not correct, kill the connection. This allows only Slack to access the page for security purposes.
if(empty($_REQUEST['text'])) die("No text provided."); //If there is no text added, kill the connection.
$exploded = explode(" ",$_REQUEST['text']); //Explode the string attached to the slash command for use in variables.
} else {
$link=1;
}
$command=NULL; //Set a null command variable, so it has something set no matter what.
//Check for command errors.
if($link==0 && !is_numeric($exploded[0])) {
//Check to see if the first command in the text array is actually help, if so redirect to help webpage detailing slash command use.
if ($exploded[0]=="help") {
$test=json_encode(array("parse" => "full", "response_type" => "in_channel","text" => "Please visit " . $helpurl . " for more help information","mrkdwn"=>true));
echo $test;
return;
}
else //Else close the connection.
{
echo "Unknown entry for ticket number.";
return;
}
}
if($link==0){
$ticketnumber = $exploded[0]; //Read ticket number to variable for convenience.
$username = $_REQUEST['user_name']; //Read Slack username to variable for convenience.
if (array_key_exists(1,$exploded)) //If a second string exists in the slash command array, make it the command.
{
$command = $exploded[1];
}
}
else
{
$ticketnumber = $_REQUEST['srnumber'];
$mysql = mysqli_connect($dbhost, $dbusername, $dbpassword, $dbdatabase);
if (!$mysql) //Check for errors
{
die("Connection Error: " . mysqli_connect_error());
}
$val1 = mysqli_real_escape_string($mysql,$_REQUEST['memberid']);
$sql = "SELECT slackuser FROM usermap where cwname = '".$val1."'";
$result = mysqli_query($mysql, $sql); //Run result
// Check for mapping, otherwise use Connectwise
if(mysqli_num_rows($result) > 0)
{
$user = mysqli_fetch_assoc($result);
$username = $user['slackuser'];
}
else
{
$username = $_REQUEST['memberid'];
}
mysqli_close($mysql);
if($_REQUEST['method']==$followtoken)
{
//For future use.
}
else if ($_REQUEST['method']==$unfollowtoken)
{
$command="unfollow"; //Set command to unfollow if it matches the CW unfollowtoken
}
else
{
die("Method does not match follow or unfollow tokens."); //If matches neither token, die.
}
}
if($usedatabase==1)
{
$mysql = mysqli_connect($dbhost, $dbusername, $dbpassword, $dbdatabase);
if (!$mysql)
{
die("Connection Error: " . mysqli_connect_error());
}
if ($command == "unfollow")
{
$val1 = mysqli_real_escape_string($mysql,$ticketnumber);
$val2 = mysqli_real_escape_string($mysql,$username);
$sql = "DELETE FROM `follow` WHERE `ticketnumber`=\"" . $val1 . "\" AND `slackuser`=\"" . $val2 . "\"";
if(mysqli_query($mysql,$sql))
{
die("Successfully unfollowed ticket #".$ticketnumber);
}
else
{
die("MySQL Error: " . mysqli_error($mysql));
}
}
else
{
$val1 = mysqli_real_escape_string($mysql,$ticketnumber);
$val2 = mysqli_real_escape_string($mysql,$username);
$sql = "INSERT INTO `follow` (`id`, `ticketnumber`, `slackuser`) VALUES (NULL, '" . $val1 . "', '" . $val2 . "');";
if(mysqli_query($mysql,$sql))
{
die("Successfully followed ticket #".$ticketnumber);
}
else
{
die("MySQL Error: " . mysqli_error($mysql));
}
}
}
else
{
die("Follow module requires MySQL to function.");
}
?>