-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajax-v1.aspx
370 lines (326 loc) · 12.2 KB
/
ajax-v1.aspx
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.Web.Security" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.Net.Mail" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.Web.Script.Serialization" %>
<%@ Import Namespace="Newtonsoft.Json" %>
<script runat="server">
//VERSION 1
const string API_URL = "https://lnf.umich.edu/helpdesk/api/data-exec.php";
int? errno = null;
string error = null;
string command = null;
dynamic GetConfig()
{
string configFilePath = Path.Combine(Server.MapPath("."), "config.json");
if (File.Exists(configFilePath))
{
string content = File.ReadAllText(configFilePath);
var config = JsonConvert.DeserializeAnonymousType(content, new {ApiKey = "", SmtpHost = ""});
return config;
}
else
{
throw new Exception("Missing config file: "+configFilePath);
}
}
string GetApiKey()
{
var config = GetConfig();
return config.ApiKey;
}
string GetSmtpHost()
{
var config = GetConfig();
return config.SmtpHost;
}
string UserCheck()
{
string result = "";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://ssel-apps.eecs.umich.edu/webapi/data/client/current");
var authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
string token = authCookie.Value;
req.Headers[HttpRequestHeader.Authorization] = string.Format("Forms {0}", token);
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Stream dataStream = resp.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
result = reader.ReadToEnd();
reader.Close();
resp.Close();
return result;
}
string ApiPost(Dictionary<string, string> data, int timeout = 5000)
{
errno = null;
string content = string.Empty;
try
{
string postData = string.Empty;
string amp = string.Empty;
foreach (KeyValuePair<string, string> kvp in data){
postData += amp + kvp.Key + "=" + Server.UrlEncode(kvp.Value);
amp = "&";
}
byte[] postBytes = Encoding.UTF8.GetBytes(postData);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(API_URL);
Stream dataStream = null;
req.Timeout = timeout;
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = postBytes.Length;
req.UserAgent = GetApiKey();
dataStream = req.GetRequestStream();
dataStream.Write(postBytes, 0, postBytes.Length);
dataStream.Close();
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
dataStream = resp.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
content = reader.ReadToEnd();
reader.Close();
dataStream.Close();
resp.Close();
}
catch(WebException ex)
{
errno = (int)ex.Status;
error = ex.Message;
}
catch(Exception ex)
{
errno = 500;
error = ex.Message;
}
if (errno != null)
throw new Exception(error);
return content;
}
string SelectTicketsByEmail(string email)
{
string result = ApiPost(new Dictionary<string, string>
{
{"action", command},
{"email", email},
{"status", "open"},
{"format", "json"}
});
return result;
}
string SelectTicketsByResource(string resource_id)
{
string result = ApiPost(new Dictionary<string, string>
{
{"action", command},
{"resource_id", resource_id},
{"status", "open"},
{"format", "json"}
});
return result;
}
string SelectTicketsByDate(string sdate, string edate, string resource_id, string email, string name, string assigned_to, string status, string format)
{
DateTime sd;
DateTime ed;
if (string.IsNullOrEmpty(sdate))
sd = DateTime.Now.Date.AddDays(-7);
else
sd = DateTime.Parse(sdate);
if (string.IsNullOrEmpty(edate))
ed = sd.AddDays(7);
else
ed = DateTime.Parse(edate);
if (string.IsNullOrEmpty(status))
status = "open";
if (string.IsNullOrEmpty(format))
format = "xml"; //default
if (format == "xml")
Response.ContentType = "text/xml";
else if (format == "json")
Response.ContentType = "application/json";
else
Response.ContentType = "text/plain";
var args = new Dictionary<string, string>
{
{"sdate", sd.ToString("yyyy-MM-dd")},
{"edate", ed.ToString("yyyy-MM-dd")},
{"status", status},
{"format", format}
};
if (!string.IsNullOrEmpty(resource_id))
args.Add("resource_id", resource_id);
if (!string.IsNullOrEmpty(email))
args.Add("email", email);
if (!string.IsNullOrEmpty(name))
args.Add("name", name);
if (!string.IsNullOrEmpty(assigned_to))
args.Add("assigned_to", assigned_to);
string result = ApiPost(args);
return result;
}
string DumpServerVars()
{
Dictionary<string, string> serverVars = new Dictionary<string, string>();
foreach (string key in Request.ServerVariables.AllKeys)
serverVars.Add(key, Request.ServerVariables[key].ToString());
JavaScriptSerializer jss = new JavaScriptSerializer();
string result = jss.Serialize(serverVars);
return result;
}
string TicketDetail(string ticketID)
{
string result = ApiPost(new Dictionary<string, string>
{
{"action", command},
{"ticketID", ticketID},
{"format", "json"}
});
return result;
}
string PostMessage(string ticketID, string message)
{
string result = ApiPost(new Dictionary<string, string>
{
{"action", command},
{"ticketID", ticketID},
{"message", message},
{"format", "json"}
}, 10000);
return result;
}
string AddTicket(string resource_id, string email, string name, string queue, string subject, string message, string pri, string search, string cc)
{
if (!string.IsNullOrEmpty(cc))
{
using (SmtpClient client = new SmtpClient(GetSmtpHost()))
using (MailMessage mm = new MailMessage("[email protected]", cc, subject, message))
client.Send(mm);
}
string result = ApiPost(new Dictionary<string, string>
{
{"action", command},
{"resource_id", resource_id},
{"email", email},
{"name", name},
{"queue", queue},
{"subject", subject},
{"message", message},
{"pri", pri},
{"search", search},
{"format", "json"}
}, 120000);
return result;
}
string GetSummary(string resources)
{
string result = ApiPost(new Dictionary<string, string>
{
{"action", command},
{"resources", resources},
{"format", "json"}
}, 5000);
return result;
}
string GetRequestVar(string key)
{
return (!string.IsNullOrEmpty(Request[key])) ? Request[key] : "";
}
string GetCc()
{
string raw = GetRequestVar("cc");
string[] splitter = raw.Split(',');
string result = string.Join(",", splitter.Where(x => !string.IsNullOrEmpty(x)));
return result;
}
void Page_Load(object sender, EventArgs e)
{
Response.ContentType = "application/json";
command = (!string.IsNullOrEmpty(Request["command"])) ? Request["command"] : string.Empty;
string ticketID = string.Empty;
string source = string.Empty;
string queue = string.Empty;
string name = string.Empty;
string email = string.Empty;
string message = string.Empty;
string topic = string.Empty;
string location = string.Empty;
string subject = string.Empty;
string pri = string.Empty;
string search = string.Empty;
string resources = string.Empty;
string resource_id = string.Empty;
string cc = string.Empty;
string sdate = string.Empty;
string edate = string.Empty;
string status = string.Empty;
string format = string.Empty;
string assigned_to = string.Empty;
try
{
switch (command)
{
case "user-check":
Response.Write(UserCheck());
break;
case "select-tickets-by-email":
email = GetRequestVar("email");
Response.Write(SelectTicketsByEmail(email));
break;
case "select-tickets-by-resource":
resource_id = GetRequestVar("resource_id");
Response.Write(SelectTicketsByResource(resource_id));
break;
case "select-tickets-by-date":
sdate = GetRequestVar("sdate");
edate = GetRequestVar("edate");
resource_id = GetRequestVar("resource_id");
email = GetRequestVar("email");
name = GetRequestVar("name");
assigned_to = GetRequestVar("assigned_to");
status = GetRequestVar("status");
format = GetRequestVar("format");
Response.Write(SelectTicketsByDate(sdate, edate, resource_id, email, name, assigned_to, status, format));
break;
case "dump-server-vars":
Response.Write(DumpServerVars());
break;
case "ticket-detail":
ticketID = GetRequestVar("ticketID");
Response.Write(TicketDetail(ticketID));
break;
case "post-message":
ticketID = GetRequestVar("ticketID");
message = GetRequestVar("message");
Response.Write(PostMessage(ticketID, message));
break;
case "add-ticket":
resource_id = GetRequestVar("resource_id");
email = GetRequestVar("email");
name = GetRequestVar("name");
queue = GetRequestVar("queue");
subject = GetRequestVar("subject");
message = GetRequestVar("message");
pri = GetRequestVar("pri");
search = GetRequestVar("search"); //"by-resource", "by-email" or ""
cc = GetCc();
Response.Write(AddTicket(resource_id, email, name, queue, subject, message, pri, search, cc));
break;
case "summary":
resources = GetRequestVar("resources");
Response.Write(GetSummary(resources));
break;
default:
if (string.IsNullOrEmpty(command))
Response.Write("{\"error\":true, \"errno\":500, \"message\":\"Missing command\"}");
else
Response.Write("{\"error\":true, \"errno\":500, \"message\":\"Invalid command: "+command+"\"}");
break;
}
}
catch(Exception ex)
{
Response.Write("{\"error\": true, \"errno\":"+errno.ToString()+",\"message\":\""+ex.Message+"\"}");
}
}
</script>