public string Signup(Login login) { string hash = Logdata.GetHash(login.PW); LogEntry log = new LogEntry("Signup", login.Name); MailAddress emailAddr; string result = "OK, now please wait for an email and click the link to complete your registration"; try { emailAddr = new MailAddress(login.Email); // Valid address } catch { return("This email address appears to be invalid"); } if (login.PW.Length < 4 || login.PW.Length > 10) { return("Password must be between 4 and 10 characters"); } if (gpxConnection.IsConnect()) { // check username and email string query = "SELECT Id, name, pw, email FROM logins"; try { using (MySqlDataAdapter loginAdapter = new MySqlDataAdapter(query, gpxConnection.Connection)) { dataLogins = new DataTable(); loginAdapter.Fill(dataLogins); int length = dataLogins.Rows.Count; for (int row = 0; row < length; row++) { DataRow dr = dataLogins.Rows[row]; string dbname = (string)dr["name"]; dbname = dbname.Trim(); string dbpw = (string)dr["pw"]; dbpw = dbpw.Trim(); string dbemail = (string)dr["email"]; dbemail = dbemail.Trim(); if (dbname.ToLower() == login.Name.ToLower()) { return("Sorry, this username has already been taken"); } if (dbemail == login.Email) { return("Sorry, only one login allowed per email address"); } if (EmailConnection.IsValidEmail(dbemail) == false) { return("Sorry, this email doesn't appear to be valid"); } } } } catch (Exception ex) { log.Error = ex.Message; return("DB error: " + ex.Message); } // create and send an email try { // create a code based on data login.EmailCode = Logdata.GetHash(login.Name + login.Name); string URLstr = string.Format(Connections.serviceURL + "?user={0}®code={1}", login.Name, login.EmailCode); //string URLstr = string.Format("http://localhost/routes/www?user={0}®code={1}",login.Name, login.EmailCode); EmailConnection ec = new EmailConnection(); MailAddress from = new MailAddress("*****@*****.**"); MailMessage message = new MailMessage(from, emailAddr) { Subject = "TCC rides signup", Body = string.Format("Please click {0} to complete your registration", URLstr) }; try { SmtpClient client = new System.Net.Mail.SmtpClient(ec.Server) { Credentials = new System.Net.NetworkCredential(ec.User, ec.PW) }; client.Send(message); // save the login details but with role as zero so login won't yet work log = new LogEntry("Register1", login.Name + " " + login.EmailCode); query = string.Format("insert into logins (name, pw, email,role,messagetime,units,climbs) values ('{0}','{1}','{2}',{3},'{4}','{5}',{6})", login.Name, hash, login.Email, 0, Logdata.DBTimeString(DateTime.Now), login.Units, login.Climbs); try { var cmd = new MySqlCommand(query, gpxConnection.Connection); cmd.ExecuteNonQuery(); result = "Thank you, please wait for an email and click link to complete registration"; } catch (Exception ex2) { result = "There is a database error, please try again:" + ex2.Message;; } } catch (Exception ex) { result = "Sorry, there is an error with the email service: " + ex.Message; } } catch (Exception ex2) { result = "Error: " + ex2.Message; log.Error = ex2.Message; } finally { log.Result = result; log.Save(gpxConnection); gpxConnection.Close(); } return(result); } else { return(DBConnection.ErrStr); } }
public string ForgetPassword(string email) { LogEntry log = new LogEntry("ForgetPassword", email); string result = "OK, now please wait for an email and click the link to set a new password"; string username = ""; MailAddress emailAddr; try { emailAddr = new MailAddress(email); // Valid address } catch { return("This email address appears to be invalid"); } if (gpxConnection.IsConnect()) { string query = string.Format("SELECT Id, name, email FROM logins where email = '{0}'", email); try { using (MySqlDataAdapter loginAdapter = new MySqlDataAdapter(query, gpxConnection.Connection)) { dataLogins = new DataTable(); loginAdapter.Fill(dataLogins); int count = dataLogins.Rows.Count; if (count == 1) { DataRow dr = dataLogins.Rows[0]; string dbname = (string)dr["name"]; username = dbname.Trim(); } else if (count == 0) { return(string.Format("Error: cannot find an account with that email")); } else { return(string.Format("Error: {0} users found with that email", dataLogins.Rows.Count)); } } } catch (Exception ex) { log.Error = ex.Message; return("DB error: " + ex.Message); } // create and send an email try { // create a code based on data string emailCode = Logdata.GetHash(username + username); // string URLstr = string.Format("https://quilkin.co.uk/tccrides?pwuser={0}®code={1}", username, emailCode); string URLstr = string.Format(Connections.serviceURL + "?pwuser={0}®code={1}", username, emailCode); EmailConnection ec = new EmailConnection(); MailAddress from = new MailAddress("*****@*****.**"); MailMessage message = new MailMessage(from, emailAddr) { Subject = "TCC rides forgotten password", Body = string.Format("Please click {0} to reset your password or other details.\n\rFor security, this link will expire in 15 minutes!", URLstr) }; try { SmtpClient client = new System.Net.Mail.SmtpClient(ec.Server) { Credentials = new System.Net.NetworkCredential(ec.User, ec.PW) }; client.Send(message); // save the time this message was delivered query = string.Format("update logins set messagetime = '{0}' where email = '{1}'", Logdata.DBTimeString(DateTime.Now), email); try { var cmd = new MySqlCommand(query, gpxConnection.Connection); cmd.ExecuteNonQuery(); } catch (Exception ex2) { result = "There is a database error, please try again:" + ex2.Message; log.Error = ex2.Message; } result = "OK, now please wait for an email and click the link to set a new password"; } catch (Exception ex) { result = "Sorry, there is an error with the email service: " + ex.Message; log.Error = ex.Message; } } catch (Exception ex2) { return("Error: " + ex2.Message); } finally { log.Result = result; log.Save(gpxConnection); gpxConnection.Close(); } return(result); } else { return(DBConnection.ErrStr); } }
public string SaveCafe(Cafe cafe) { cafe.Name = GetRidOfApostrophes(cafe.Name); cafe.Notes = GetRidOfApostrophes(cafe.Notes); cafe.PlaceName = GetRidOfApostrophes(cafe.PlaceName); cafe.TimesOpen = GetRidOfApostrophes(cafe.TimesOpen); cafe.DaysOpen = GetRidOfApostrophes(cafe.DaysOpen); LogEntry log = new LogEntry("SaveCafe", cafe.ID + " " + cafe.Name + " " + cafe.User); string result = ""; if (gpxConnection.IsConnect()) { try { string query; //// check ride with same leader and date isn't already there *************** //string query = string.Format("SELECT dest FROM rides where date= '{0}' and leaderName = '{1}'", ride.Date, ride.LeaderName); //bool exists = true; //string now = Logdata.TimeString(DateTime.Now); //string rideDest = ""; //using (MySqlDataAdapter routeAdapter = new MySqlDataAdapter(query, gpxConnection.Connection)) //{ // dataRoutes = new DataTable(); // routeAdapter.Fill(dataRoutes); // if (dataRoutes.Rows.Count == 0) // { // exists = false; // } // else // { // DataRow dr = dataRoutes.Rows[0]; // try { rideDest = (string)dr["dest"]; } catch { } // } //} //if (exists) //{ // result = string.Format("There is already a ride with you as leader on the same date. Please choose another date."); //} // using (System.Net.WebClient client = new System.Net.WebClient()) { if (cafe.ID > 0) { // already exists, just update query = string.Format("update cafes set name = '{0}',placename = '{1}', notes = '{2}',daysopen = '{3}',timesopen ='{4}',user ='******', updated = '{6}' where id = {7} ", cafe.Name, cafe.PlaceName, cafe.Notes, cafe.DaysOpen, cafe.TimesOpen, cafe.User, Logdata.DBTimeString(DateTime.Now), cafe.ID); using (MySqlCommand command = new MySqlCommand(query, gpxConnection.Connection)) { command.ExecuteNonQuery(); } result = "0"; } else { query = string.Format("insert into cafes (lat,lng,name,placename,notes,daysopen,timesopen,user,updated) values ({0},{1},'{2}','{3}','{4}','{5}','{6}','{7}','{8}')", cafe.Lat, cafe.Lng, cafe.Name, cafe.PlaceName, cafe.Notes, cafe.DaysOpen, cafe.TimesOpen, cafe.User, Logdata.DBTimeString(DateTime.Now)); // get new ride ID query += "; SELECT CAST(LAST_INSERT_ID() AS int)"; object cafeID = null; using (MySqlCommand command = new MySqlCommand(query, gpxConnection.Connection)) { cafeID = command.ExecuteScalar(); } // return id of new cafe result = cafeID.ToString(); } } } catch (Exception ex) { result = string.Format("Database error: ride \"{0}\" not saved: {1}", cafe.Name, ex.Message); log.Error = ex.Message; } finally { log.Result = result; log.Save(gpxConnection); gpxConnection.Close(); } } else { return(DBConnection.ErrStr); } return(result); }