-
Notifications
You must be signed in to change notification settings - Fork 735
Expand file tree
/
Copy pathMySqlDbProvider.cs
More file actions
582 lines (477 loc) · 21.4 KB
/
MySqlDbProvider.cs
File metadata and controls
582 lines (477 loc) · 21.4 KB
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
using System;
using System.Data;
using MySql.Data.MySqlClient;
using log4net;
using System.Reflection;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Web;
namespace OWASP.WebGoat.NET.App_Code.DB
{
public class MySqlDbProvider : IDbProvider
{
private readonly string _connectionString;
private readonly string _host;
private readonly string _port;
private readonly string _pwd;
private readonly string _uid;
private readonly string _database;
private readonly string _clientExec;
private readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public MySqlDbProvider(ConfigFile configFile)
{
if (configFile == null)
_connectionString = string.Empty;
if (!string.IsNullOrEmpty(configFile.Get(DbConstants.KEY_PWD)))
{
_connectionString = string.Format("SERVER={0};PORT={1};DATABASE={2};UID={3};PWD={4}",
configFile.Get(DbConstants.KEY_HOST),
configFile.Get(DbConstants.KEY_PORT),
configFile.Get(DbConstants.KEY_DATABASE),
configFile.Get(DbConstants.KEY_UID),
configFile.Get(DbConstants.KEY_PWD));
}
else
{
_connectionString = string.Format("SERVER={0};PORT={1};DATABASE={2};UID={3}",
configFile.Get(DbConstants.KEY_HOST),
configFile.Get(DbConstants.KEY_PORT),
configFile.Get(DbConstants.KEY_DATABASE),
configFile.Get(DbConstants.KEY_UID));
}
_uid = configFile.Get(DbConstants.KEY_UID);
_pwd = configFile.Get(DbConstants.KEY_PWD);
_database = configFile.Get(DbConstants.KEY_DATABASE);
_host = configFile.Get(DbConstants.KEY_HOST);
_clientExec = configFile.Get(DbConstants.KEY_CLIENT_EXEC);
_port = configFile.Get(DbConstants.KEY_PORT);
}
public string Name { get { return DbConstants.DB_TYPE_MYSQL; } }
public bool TestConnection()
{
try
{
/*using (MySqlConnection connection = new MySqlConnection(_connectionString))
{
connection.Open();
MySqlCommand cmd = new MySqlCommand("select * from information_schema.TABLES", connection);
cmd.ExecuteNonQuery();
connection.Close();
}*/
MySqlHelper.ExecuteNonQuery(_connectionString, "select * from information_schema.TABLES");
return true;
}
catch (Exception ex)
{
log.Error("Error testing DB", ex);
return false;
}
}
public DataSet GetCatalogData()
{
using (MySqlConnection connection = new MySqlConnection(_connectionString))
{
MySqlDataAdapter da = new MySqlDataAdapter("select * from Products", connection);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
}
public bool RecreateGoatDb()
{
string args;
if (string.IsNullOrEmpty(_pwd))
args = string.Format("--user={0} --database={1} --host={2} --port={3} -f",
_uid, _database, _host, _port);
else
args = string.Format("--user={0} --password={1} --database={2} --host={3} --port={4} -f",
_uid, _pwd, _database, _host, _port);
log.Info("Running recreate");
string createScript = HttpContext.Current.Server.MapPath(DbConstants.DB_CREATE_MYSQL_SCRIPT.Replace("\\", "/"));
string loadScript = HttpContext.Current.Server.MapPath(DbConstants.DB_LOAD_MYSQL_SCRIPT.Replace("\\", "/"));
int retVal1 = Math.Abs(Util.RunProcessWithInput(_clientExec, args, createScript));
int retVal2 = Math.Abs(Util.RunProcessWithInput(_clientExec, args, loadScript));
return Math.Abs(retVal1) + Math.Abs(retVal2) == 0;
}
public bool IsValidCustomerLogin(string email, string password)
{
//encode password
string encoded_password = Encoder.Encode(password);
//check email/password
string sql = "select * from CustomerLogin where email = '" + email +
"' and password = '" + encoded_password + "';";
using (MySqlConnection connection = new MySqlConnection(_connectionString))
{
MySqlDataAdapter da = new MySqlDataAdapter(sql, connection);
//TODO: User reader instead (for all calls)
DataSet ds = new DataSet();
da.Fill(ds);
try
{
return ds.Tables[0].Rows.Count != 0;
}
catch (Exception ex)
{
//Log this and pass the ball along.
log.Error("Error checking login", ex);
throw new Exception("Error checking login", ex);
}
}
}
//Find the bugs!
public string CustomCustomerLogin(string email, string password)
{
string error_message = null;
try
{
//get data
string sql = "select * from CustomerLogin where email = '" + email + "';";
using (MySqlConnection connection = new MySqlConnection(_connectionString))
{
MySqlDataAdapter da = new MySqlDataAdapter(sql, connection);
DataSet ds = new DataSet();
da.Fill(ds);
//check if email address exists
if (ds.Tables[0].Rows.Count == 0)
{
error_message = "Email Address Not Found!";
return error_message;
}
string encoded_password = ds.Tables[0].Rows[0]["Password"].ToString();
string decoded_password = Encoder.Decode(encoded_password);
if (password.Trim().ToLower() != decoded_password.Trim().ToLower())
{
error_message = "Password Not Valid For This Email Address!";
}
else
{
//login successful
error_message = null;
}
}
}
catch (MySqlException ex)
{
log.Error("Error with custom customer login", ex);
error_message = ex.Message;
}
catch (Exception ex)
{
log.Error("Error with custom customer login", ex);
}
return error_message;
}
public string GetCustomerEmail(string customerNumber)
{
string output = null;
try
{
using (MySqlConnection connection = new MySqlConnection(_connectionString))
{
string sql = "select email from CustomerLogin where customerNumber = " + customerNumber;
MySqlCommand command = new MySqlCommand(sql, connection);
output = command.ExecuteScalar().ToString();
}
}
catch (Exception ex)
{
output = ex.Message;
}
return output;
}
public DataSet GetCustomerDetails(string customerNumber)
{
string sql = "select Customers.customerNumber, Customers.customerName, Customers.logoFileName, Customers.contactLastName, Customers.contactFirstName, " +
"Customers.phone, Customers.addressLine1, Customers.addressLine2, Customers.city, Customers.state, Customers.postalCode, Customers.country, " +
"Customers.salesRepEmployeeNumber, Customers.creditLimit, CustomerLogin.email, CustomerLogin.password, CustomerLogin.question_id, CustomerLogin.answer " +
"From Customers, CustomerLogin where Customers.customerNumber = CustomerLogin.customerNumber and Customers.customerNumber = " + customerNumber;
DataSet ds = new DataSet();
try
{
using (MySqlConnection connection = new MySqlConnection(_connectionString))
{
MySqlDataAdapter da = new MySqlDataAdapter(sql, connection);
da.Fill(ds);
}
}
catch (Exception ex)
{
log.Error("Error getting customer details", ex);
throw new ApplicationException("Error getting customer details", ex);
}
return ds;
}
public DataSet GetOffice(string city)
{
using (MySqlConnection connection = new MySqlConnection(_connectionString))
{
string sql = "select * from Offices where city = @city";
MySqlDataAdapter da = new MySqlDataAdapter(sql, connection);
da.SelectCommand.Parameters.AddWithValue("@city", city);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
}
public DataSet GetComments(string productCode)
{
using (MySqlConnection connection = new MySqlConnection(_connectionString))
{
string sql = "select * from Comments where productCode = @productCode";
MySqlDataAdapter da = new MySqlDataAdapter(sql, connection);
da.SelectCommand.Parameters.AddWithValue("@productCode", productCode);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
}
public string AddComment(string productCode, string email, string comment)
{
string sql = "insert into Comments(productCode, email, comment) values ('" + productCode + "','" + email + "','" + comment + "');";
string output = null;
try
{
using (MySqlConnection connection = new MySqlConnection(_connectionString))
{
connection.Open();
MySqlCommand command = new MySqlCommand(sql, connection);
command.ExecuteNonQuery();
}
}
catch (Exception ex)
{
log.Error("Error adding comment", ex);
output = ex.Message;
}
return output;
}
public string UpdateCustomerPassword(int customerNumber, string password)
{
string sql = "update CustomerLogin set password = '" + Encoder.Encode(password) + "' where customerNumber = " + customerNumber;
string output = null;
try
{
using (MySqlConnection connection = new MySqlConnection(_connectionString))
{
MySqlCommand command = new MySqlCommand(sql, connection);
int rows_added = command.ExecuteNonQuery();
log.Info("Rows Added: " + rows_added + " to comment table");
}
}
catch (Exception ex)
{
log.Error("Error updating customer password", ex);
output = ex.Message;
}
return output;
}
public string[] GetSecurityQuestionAndAnswer(string email)
{
string sql = "select SecurityQuestions.question_text, CustomerLogin.answer from CustomerLogin, " +
"SecurityQuestions where CustomerLogin.email = '" + email + "' and CustomerLogin.question_id = " +
"SecurityQuestions.question_id;";
string[] qAndA = new string[2];
using (MySqlConnection connection = new MySqlConnection(_connectionString))
{
MySqlDataAdapter da = new MySqlDataAdapter(sql, connection);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
DataRow row = ds.Tables[0].Rows[0];
qAndA[0] = row[0].ToString();
qAndA[1] = row[1].ToString();
}
}
return qAndA;
}
public string GetPasswordByEmail(string email)
{
string result = string.Empty;
try
{
using (MySqlConnection connection = new MySqlConnection(_connectionString))
{
//get data
string sql = "select * from CustomerLogin where email = '" + email + "';";
MySqlDataAdapter da = new MySqlDataAdapter(sql, connection);
DataSet ds = new DataSet();
da.Fill(ds);
//check if email address exists
if (ds.Tables[0].Rows.Count == 0)
{
result = "Email Address Not Found!";
}
string encoded_password = ds.Tables[0].Rows[0]["Password"].ToString();
string decoded_password = Encoder.Decode(encoded_password);
result = decoded_password;
}
}
catch (Exception ex)
{
result = ex.Message;
}
return result;
}
public DataSet GetUsers()
{
using (MySqlConnection connection = new MySqlConnection(_connectionString))
{
string sql = "select * from CustomerLogin;";
MySqlDataAdapter da = new MySqlDataAdapter(sql, connection);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
}
public DataSet GetOrders(int customerID)
{
using (MySqlConnection connection = new MySqlConnection(_connectionString))
{
string sql = "select * from Orders where customerNumber = " + customerID;
MySqlDataAdapter da = new MySqlDataAdapter(sql, connection);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count == 0)
return null;
else
return ds;
}
}
public DataSet GetProductDetails(string productCode)
{
string sql = string.Empty;
MySqlDataAdapter da;
DataSet ds = new DataSet();
using (MySqlConnection connection = new MySqlConnection(_connectionString))
{
sql = "select * from Products where productCode = '" + productCode + "'";
da = new MySqlDataAdapter(sql, connection);
da.Fill(ds, "products");
sql = "select * from Comments where productCode = '" + productCode + "'";
da = new MySqlDataAdapter(sql, connection);
da.Fill(ds, "comments");
DataRelation dr = new DataRelation("prod_comments",
ds.Tables["products"].Columns["productCode"], //category table
ds.Tables["comments"].Columns["productCode"], //product table
false);
ds.Relations.Add(dr);
return ds;
}
}
public DataSet GetOrderDetails(int orderNumber)
{
string sql = "select Customers.customerName, Orders.customerNumber, Orders.orderNumber, Products.productName, " +
"OrderDetails.quantityOrdered, OrderDetails.priceEach, Products.productImage " +
"from OrderDetails, Products, Orders, Customers where " +
"Customers.customerNumber = Orders.customerNumber " +
"and OrderDetails.productCode = Products.productCode " +
"and Orders.orderNumber = OrderDetails.orderNumber " +
"and OrderDetails.orderNumber = " + orderNumber;
using (MySqlConnection connection = new MySqlConnection(_connectionString))
{
MySqlDataAdapter da = new MySqlDataAdapter(sql, connection);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count == 0)
return null;
else
return ds;
}
}
public DataSet GetPayments(int customerNumber)
{
using (MySqlConnection connection = new MySqlConnection(_connectionString))
{
string sql = "select * from Payments where customerNumber = " + customerNumber;
MySqlDataAdapter da = new MySqlDataAdapter(sql, connection);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count == 0)
return null;
else
return ds;
}
}
public DataSet GetProductsAndCategories()
{
return GetProductsAndCategories(0);
}
public DataSet GetProductsAndCategories(int catNumber)
{
//TODO: Rerun the database script.
string sql = string.Empty;
MySqlDataAdapter da;
DataSet ds = new DataSet();
//catNumber is optional. If it is greater than 0, add the clause to both statements.
string catClause = string.Empty;
if (catNumber >= 1)
catClause += " where catNumber = " + catNumber;
using (MySqlConnection connection = new MySqlConnection(_connectionString))
{
sql = "select * from Categories" + catClause;
da = new MySqlDataAdapter(sql, connection);
da.Fill(ds, "categories");
sql = "select * from Products" + catClause;
da = new MySqlDataAdapter(sql, connection);
da.Fill(ds, "products");
//category / products relationship
DataRelation dr = new DataRelation("cat_prods",
ds.Tables["categories"].Columns["catNumber"], //category table
ds.Tables["products"].Columns["catNumber"], //product table
false);
ds.Relations.Add(dr);
return ds;
}
}
public DataSet GetEmailByName(string name)
{
string sql = "select firstName, lastName, email from Employees where firstName like '" + name + "%' or lastName like '" + name + "%'";
using (MySqlConnection connection = new MySqlConnection(_connectionString))
{
MySqlDataAdapter da = new MySqlDataAdapter(sql, connection);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count == 0)
return null;
else
return ds;
}
}
public string GetEmailByCustomerNumber(string num)
{
string output = "";
try
{
output = (String)MySqlHelper.ExecuteScalar(_connectionString, "select email from CustomerLogin where customerNumber = " + num);
/*using (MySqlConnection connection = new MySqlConnection(_connectionString))
{
string sql = "select email from CustomerLogin where customerNumber = " + num;
MySqlCommand cmd = new MySqlCommand(sql, connection);
output = (string)cmd.ExecuteScalar();
}*/
}
catch (Exception ex)
{
log.Error("Error getting email by customer number", ex);
output = ex.Message;
}
return output;
}
public DataSet GetCustomerEmails(string email)
{
string sql = "select email from CustomerLogin where email like '" + email + "%'";
using (MySqlConnection connection = new MySqlConnection(_connectionString))
{
MySqlDataAdapter da = new MySqlDataAdapter(sql, connection);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count == 0)
return null;
else
return ds;
}
}
}
}