private bool securityCodeCheck(String username, String securityCodeCompare) { bool checkPass = false; Thread t = Utilities.runInThread(() => { DB TempLogUser = Utilities.AsyncDB(); TempLogUser.bind(new string[] { "user", username }); DataTable dt = TempLogUser.query("select securitycode from users where user=@user"); if (dt.Rows.Count == 1) { String securityCode = dt.Rows[0][0].ToString(); if (securityCode.Equals(securityCodeCompare)) { checkPass = true; } else { checkPass = false; } } else { checkPass = false; } }); t.Start(); t.Join(); return(checkPass); }
private void newRandomPassword(String user, int length) { if (Globals.Connected) { String password = createPassword(length); Utilities.runInThread(() => { DB changePassDB = Utilities.AsyncDB(); changePassDB.bind(new string[] { "usern", user, "pass1", Utilities.MD5Hash(password) }); int qreg = changePassDB.nQuery("UPDATE users SET pass=@pass1 where user=@usern"); if (qreg > 0) { Utilities.notifyThem(ntfForgot, "Successfully Generated", NotificationBox.Type.Success); Utilities.InvokeMe(txtPassPassword, () => { txtPassPassword.Text = password; }); } else { Utilities.notifyThem(ntfForgot, "Failed to Generate", NotificationBox.Type.Error); } }).Start(); } }
private void btnconnect_Click(object sender, EventArgs e) { Utilities.runInThread(() => { Globals.ConnectionStr(txtSHost.Text, txtSUser.Text, txtSPass.Text, txtSDatabase.Text); if (Utilities.AsyncDB().Connected()) { Globals.Connected = true; } else { Globals.Connected = false; } if (Globals.Connected) { if (checkBoxRemember.Checked) { Settings.save("StoredSettings", checkBoxRemember.Checked.ToString(), checkBoxAutoConnect.Checked.ToString(), txtSHost.Text, txtSUser.Text, txtSPass.Text, txtSDatabase.Text); } else { Settings.delete("StoredSettings"); } Utilities.InvokeMe(btnconnect, () => { btnconnect.Text = "Reconnect"; }); Utilities.notifyThem(ntfBox3, "Successfully Connected", NotificationBox.Type.Success); } else { Utilities.notifyThem(ntfBox3, "Could not connect to the Database", NotificationBox.Type.Error); } }).Start(); }
private void btnChangeSecurity_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(txtspassword.Text) == true || string.IsNullOrEmpty(txtncode.Text.Trim()) == true) { Utilities.notifyThem(ntfC, "All fields are necessary", NotificationBox.Type.Error); } else if (!Globals.logUser.pass.Equals(Utilities.MD5Hash(txtspassword.Text))) { Utilities.notifyThem(ntfC, "Wrong Password", NotificationBox.Type.Error); } else if (txtncode.Text.Length < 4) { Utilities.notifyThem(ntfC, "Security code must be at least 4 characters", NotificationBox.Type.Warning); } else if (Validation.IsValidSecurityCode(txtncode.Text)) { Utilities.notifyThem(ntfC, "Security code must contain\na-z, A-Z, 0-9 characters", NotificationBox.Type.Warning); } else { Utilities.runInThread(() => { String HashSecur = Utilities.MD5Hash(txtncode.Text.Trim()); DB TempDB = Utilities.AsyncDB(); TempDB.bind(new string[] { "Code", HashSecur }); TempDB.nQuery("UPDATE users SET securitycode=@Code WHERE id=" + Globals.logUser.id); Globals.logUser.scode = HashSecur; Utilities.clearText(txtspassword, txtncode); }).Start(); Utilities.notifyThem(ntfC, "Security Code Changed", NotificationBox.Type.Success); } }
private void btnChangePassword_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(txtopassword.Text) == true || string.IsNullOrEmpty(txtnpassword.Text) == true || string.IsNullOrEmpty(txtrnpassword.Text)) { Utilities.notifyThem(ntfP, "All fields are necessary", NotificationBox.Type.Error); } else if (!txtnpassword.Text.Equals(txtrnpassword.Text)) { Utilities.notifyThem(ntfP, "Different new Password fields", NotificationBox.Type.Error); } else if (!Globals.logUser.pass.Equals(Utilities.MD5Hash(txtopassword.Text))) { Utilities.notifyThem(ntfP, "Wrong old Password", NotificationBox.Type.Error); } else { Utilities.runInThread(() => { String HashPass = Utilities.MD5Hash(txtnpassword.Text); DB TempDB = Utilities.AsyncDB(); TempDB.bind(new string[] { "Pass", HashPass }); TempDB.nQuery("UPDATE users SET pass=@Pass WHERE id=" + Globals.logUser.id); Globals.logUser.pass = HashPass; Utilities.clearText(txtopassword, txtnpassword, txtrnpassword); }).Start(); Utilities.notifyThem(ntfP, "Password Changed", NotificationBox.Type.Success); } }
private void btnChangeEmail_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(txtepassword.Text) == true || string.IsNullOrEmpty(txtnemail.Text.Trim()) == true) { Utilities.notifyThem(ntfE, "All fields are necessary", NotificationBox.Type.Error); } else if (!Globals.logUser.pass.Equals(Utilities.MD5Hash(txtepassword.Text))) { Utilities.notifyThem(ntfE, "Wrong Password", NotificationBox.Type.Error); } else if (!Validation.IsValidEmail(txtnemail.Text)) { Utilities.notifyThem(ntfE, "Email is not valid", NotificationBox.Type.Error); } else if (Validation.EmailAvailibility(txtnemail.Text)) { Utilities.notifyThem(ntfE, "Email already exists", NotificationBox.Type.Warning); } else { Utilities.runInThread(() => { DB TempDB = Utilities.AsyncDB(); TempDB.bind(new string[] { "Email", txtnemail.Text.Trim() }); TempDB.nQuery("UPDATE users SET email=@Email WHERE id=" + Globals.logUser.id); Globals.logUser.email = txtnemail.Text; Utilities.clearText(txtepassword, txtnemail); }).Start(); Utilities.notifyThem(ntfE, "Email Changed", NotificationBox.Type.Success); } }
private void btnSaveChanges_Click(object sender, EventArgs e) { btnSaveChanges.Focus(); bool tagsdel = false; Utilities.runInThread(() => { if (string.IsNullOrEmpty(txtAddQ.Text.Trim()) == true || string.IsNullOrEmpty(txtAddTags.Text.Trim()) == true || (dgvAnswerlist.Rows.Count < 2)) { Utilities.notifyThem(ntfEdit, "You must fill some info about your question first.", NotificationBox.Type.Error); } else { List <Answer> Answers = new List <Answer>(); for (int a = 0; a < dgvAnswerlist.Rows.Count - 1; a++) { bool correct; if (dgvAnswerlist.Rows[a].Cells[1].Value == null) { correct = false; } else { correct = bool.Parse(dgvAnswerlist.Rows[a].Cells[1].Value.ToString()); } Answers.Add(new Answer(dgvAnswerlist.Rows[a].Cells[0].Value.ToString().TrimEnd().TrimStart(), correct)); } DB TempDB = Utilities.AsyncDB(true); TempDB.bind(new string[] { "Question", txtAddQ.Text.TrimEnd().TrimStart(), "Answers", JsonConvert.SerializeObject(Answers).ToString(), "Dlevel", difficultyLvl.Value.ToString(), "Prive", (switchPrivate.isOn ? 1 : 0).ToString(), "UID", Globals.logUser.id.ToString() }); string qid = TempDB.single("UPDATE questions SET question = @Question, answers = @Answers, dlevel = @Dlevel, prive = @Prive, uid = @UID WHERE id=" + _quest.id + ";"); int qAddTag = 0; string[] tags = txtAddTags.Text.TrimEnd(',').Split(','); foreach (string tag in tags) { if (!string.IsNullOrEmpty(tag)) { if (tagsdel == true) { TempDB.bind(new string[] { "TAG", tag.ToLower(), "QID", _quest.id.ToString() }); qAddTag += TempDB.nQuery("INSERT INTO tags (nametag, qid) VALUES (@TAG, @QID)"); } else { TempDB.nQuery("DELETE FROM tags WHERE qid = " + _quest.id); TempDB.bind(new string[] { "TAG", tag.ToLower(), "QID", _quest.id.ToString() }); qAddTag += TempDB.nQuery("INSERT INTO tags (nametag, qid) VALUES (@TAG, @QID)"); tagsdel = true; } } } Utilities.notifyThem(ntfEdit, "Successfully Saved.", NotificationBox.Type.Success); Functionality.RefreshMyQuestions(); change = false; } }).Start(); }
private void frmEdit_Load(object sender, EventArgs e) { autocompleteMenu1.Items = Globals.colTags.ToArray(); string questtags = ""; txtAddQ.Text = _quest.question; Utilities.runInThread(() => { DB TempDB = Utilities.AsyncDB(true); DataTable dt = new DataTable(); dt = TempDB.query("select nametag from tags where qid='" + _quest.id + "'"); for (int i = 0; i < dt.Rows.Count; i++) { if (i == dt.Rows.Count - 1) { questtags += dt.Rows[i][0].ToString(); } else { questtags += dt.Rows[i][0].ToString() + ","; } } Utilities.InvokeMe(txtAddTags, () => { txtAddTags.Text = "" + questtags; }); }).Start(); for (int k = 0; k < _quest.anwsers.Count; k++) { dgvAnswerlist.Rows.Add(_quest.anwsers[k].text.ToString(), _quest.anwsers[k].correct); } }
private void btnAdd_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(txtAddQ.Text.Trim()) == true || string.IsNullOrEmpty(txtAddTags.Text.Trim()) == true || (dgvAnswerlist.Rows.Count < 1)) { Utilities.notifyThem(ntfAdd, "You must fill some info about your question first", NotificationBox.Type.Error); } else { List <Answer> Answers = new List <Answer>(); for (int i = 0; i < dgvAnswerlist.Rows.Count; i++) { Answers.Add(new Answer(dgvAnswerlist.Rows[i].Cells[0].Value.ToString().TrimEnd().TrimStart(), bool.Parse(dgvAnswerlist.Rows[i].Cells[1].Value.ToString()))); } Utilities.runInThread(() => { DB TempDB = Utilities.AsyncDB(true); TempDB.bind(new string[] { "Question", txtAddQ.Text.TrimEnd().TrimStart(), "Answers", JsonConvert.SerializeObject(Answers).ToString(), "Dlevel", difficultyLvl.Value.ToString(), "Prive", (switchPrivate.isOn ? 1 : 0).ToString(), "UID", Globals.logUser.id.ToString() }); string qid = TempDB.single("INSERT INTO questions (question, answers, dlevel, prive, uid) VALUES (@Question, @Answers, @Dlevel, @Prive, @UID); select last_insert_id();"); int qAddTag = 0; string[] tags = txtAddTags.Text.TrimEnd(',').Split(','); foreach (string tag in tags) { if (!string.IsNullOrEmpty(tag)) { TempDB.bind(new string[] { "TAG", tag.ToLower(), "QID", qid }); qAddTag += TempDB.nQuery("INSERT INTO tags (nametag, qid) VALUES (@TAG, @QID)"); } } if (string.IsNullOrEmpty(qid) == false && qAddTag > 0) { Utilities.notifyThem(ntfAdd, "Successfull Added Question !", NotificationBox.Type.Success); Functionality.RefreshMyQuestions(); Utilities.clearText(txtAddQ, txtAddTags, txtAnswer); Utilities.InvokeMe(dgvAnswerlist, () => { dgvAnswerlist.Rows.Clear(); dgvAnswerlist.Refresh(); }); Utilities.InvokeMe(difficultyLvl, () => { difficultyLvl.Value = 1; }); Utilities.InvokeMe(switchPrivate, () => { switchPrivate.isOn = false; }); Utilities.InvokeMe(switchCorrectAnswer, () => { switchCorrectAnswer.isOn = true; }); Functionality.LoadTags(autocompleteMenu1); } }).Start(); } }
private void btnLogin_Click(object sender, EventArgs e) { if (Globals.Connected) { txtLUser.Text = txtLUser.Text.Trim(); if (string.IsNullOrEmpty(txtLUser.Text) || string.IsNullOrEmpty(txtLPass.Text)) { Utilities.notifyThem(ntfBox1, "Empty username or password!", NotificationBox.Type.Warning); } else { Utilities.runInThread(() => { String HashPass = Utilities.MD5Hash(txtLPass.Text); DB TempLogUser = Utilities.AsyncDB(); TempLogUser.bind(new string[] { "user", txtLUser.Text, "pass", HashPass }); DataTable dt = TempLogUser.query("select * from users where user = @user and pass = @pass"); if (dt.Rows.Count == 1) { if (ckbLRemember.Checked) { Settings.save("StoredAccount", ckbLRemember.Checked.ToString(), txtLUser.Text, txtLPass.Text); } else { Settings.delete("StoredAccount"); } //Load Tags Functionality.LoadTags(); int id = int.Parse(dt.Rows[0][0].ToString()); string user = dt.Rows[0][1].ToString(); string pass = dt.Rows[0][2].ToString(); string mail = dt.Rows[0][3].ToString(); string scode = dt.Rows[0][4].ToString(); Globals.logUser = new User(id, user, pass, mail, scode); Utilities.InvokeMe(this, () => { ntfBox1.Visible = false; this.Hide(); Globals.formMain.Show(); }); } else { Utilities.notifyThem(ntfBox1, "Username or Password were incorrect", NotificationBox.Type.Error); } }).Start(); } } else { Utilities.notifyThem(ntfBox1, "Not connected to DB!", NotificationBox.Type.Warning); } }
private void btnRegister_Click(object sender, EventArgs e) { Utilities.runInThread(() => { if (Globals.Connected) { if (String.IsNullOrEmpty(txtRUser.Text.Trim()) || String.IsNullOrEmpty(txtRPass.Text) || String.IsNullOrEmpty(txtRrepeatPass.Text) || String.IsNullOrEmpty(txtREmail.Text.Trim()) || String.IsNullOrEmpty(txtRSecurityCode.Text.Trim())) { Utilities.notifyThem(ntfBox2, "All fields are necessary.", NotificationBox.Type.Warning); } else if (txtRPass.Text != txtRrepeatPass.Text) { Utilities.notifyThem(ntfBox2, "Passwords don't match.", NotificationBox.Type.Warning); } else if (!Validation.IsValidEmail(txtREmail.Text)) { Utilities.notifyThem(ntfBox2, "Email is not valid.", NotificationBox.Type.Warning); } else if (Validation.EmailAvailibility(txtREmail.Text)) { Utilities.notifyThem(ntfBox2, "Email is already exists.", NotificationBox.Type.Warning); } else if (txtRSecurityCode.Text.Length < 4) { Utilities.notifyThem(ntfBox2, "Security code must be at least 4 characters.", NotificationBox.Type.Warning); } else if (Validation.IsValidSecurityCode(txtRSecurityCode.Text)) { Utilities.notifyThem(ntfBox2, "Security code must contain\na-z, A-Z, 0-9 characters", NotificationBox.Type.Warning); } else if (Validation.UsernameAvailibility(txtRUser.Text)) { Utilities.notifyThem(ntfBox2, "Username is not available.", NotificationBox.Type.Warning); } else { String HashPass = Utilities.MD5Hash(txtRPass.Text); String HashSecur = Utilities.MD5Hash(txtRSecurityCode.Text); DB tDB = Utilities.AsyncDB(); tDB.bind(new string[] { "usern", txtRUser.Text, "pass1", HashPass, "email1", txtREmail.Text, "securcode", HashSecur }); int qreg = tDB.nQuery("INSERT INTO users (user, pass, email,securitycode) VALUES (@usern, @pass1, @email1, @securcode)"); if (qreg > 0) { Utilities.notifyThem(ntfBox2, "Successfull Registration.", NotificationBox.Type.Success); } } } else { Utilities.notifyThem(ntfBox2, "Not connected to DB!", NotificationBox.Type.Warning); } }).Start(); }
private void frmMain_Load(object sender, EventArgs e) { lblUserMessage.Text += Globals.logUser.user + " !"; tabUser.SelectTab(1); autocompleteMenu1.Items = Globals.colTags.ToArray(); Utilities.runInThread(() => { Functionality.RefreshMyQuestions(); }).Start(); pusername.Text = Globals.logUser.user; pemail.Text = Globals.logUser.email; pdatabase.Text = Utilities.FindControl(Globals.formStart, "txtSDatabase").Text; }
private void tabUser_SelectedIndexChanged(object sender, EventArgs e) { if (tabUser.SelectedIndex == 7) { Globals.MyTestQids.Clear(); Globals.formStart.Show(); Globals.formMain.Close(); Globals.formMain = new frmMain(); } if (tabUser.SelectedIndex == 2 || tabUser.SelectedIndex == 3) { Utilities.runInThread(() => { Functionality.LoadTags(autocompleteMenu1); }).Start(); } }
private void deleteToolStripMenuItem_Click(object sender, EventArgs e) { var msgbResult = MessageBox.Show("Are you sure that you want to\nPermanatly delete the highlighted Question ?", "Delete Question", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (msgbResult == DialogResult.Yes) { int sid = int.Parse(dgvMyQ.SelectedRows[0].Cells[1].Value.ToString()); Utilities.runInThread(() => { Utilities.AsyncDB().nQuery("DELETE FROM questions WHERE id = " + sid); Utilities.AsyncDB().nQuery("DELETE FROM tags WHERE qid = " + sid); }).Start(); dgvMyQ.Rows.Remove(dgvMyQ.SelectedRows[0]); pnumQ.Text = dgvMyQ.Rows.Count.ToString(); Utilities.notifyThem(ntfMyQ, "Successfully Deleted !", NotificationBox.Type.Success); } }
private void btnDeleteSelected_Click(object sender, EventArgs e) { btnDeleteSelected.Focus(); string idsForDelete = ""; List <int> cids = new List <int>(); for (int i = 0; i < dgvMyQ.Rows.Count; i++) { if (bool.Parse(dgvMyQ.Rows[i].Cells[0].Value.ToString())) { cids.Add(i); idsForDelete += dgvMyQ.Rows[i].Cells[1].Value.ToString() + ","; } } if (cids.Count > 0) { var msgbResult = MessageBox.Show("Are you sure that you want to\nPermanatly delete the selected Questions ?", "Delete Question", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (msgbResult == DialogResult.Yes) { for (int k = (cids.Count - 1); k >= 0; k--) { dgvMyQ.Rows.RemoveAt(int.Parse(cids[k].ToString())); } idsForDelete = idsForDelete.TrimEnd(','); Utilities.runInThread(() => { Utilities.AsyncDB().nQuery("DELETE FROM questions WHERE id IN (" + idsForDelete + ")"); Utilities.AsyncDB().nQuery("DELETE FROM tags WHERE qid IN (" + idsForDelete + ")"); Utilities.notifyThem(ntfMyQ, "Successfully Deleted " + cids.Count + " Questions !", NotificationBox.Type.Success); }).Start(); pnumQ.Text = dgvMyQ.Rows.Count.ToString(); } } else { Utilities.notifyThem(ntfMyQ, "You didn't select any questions", NotificationBox.Type.Warning); } }
private void btnSearch_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(txtTags.Text)) { Utilities.notifyThem(ntbfindQ, "You must add some tags first", NotificationBox.Type.Error); } else { Utilities.runInThread(() => { DB TempDB = Utilities.AsyncDB(true); string[] tags = txtTags.Text.TrimEnd(',').Split(','); string conQuery = ""; for (int i = 0; i < tags.Length; i++) { conQuery += "@" + i + ","; } string fromall = ""; if (switchFindAll.isOn) { fromall = "(prive = 0 and uid != " + Globals.logUser.id + ") or "; } DataTable dt = new DataTable(); TempDB.qBind(tags); if (!switchAllTags.isOn) { dt = TempDB.query("select * from questions where (" + fromall + "uid = " + Globals.logUser.id + ") and dlevel >= " + numericMin.Value + " and dlevel <= " + numericMax.Value + " and id in(select distinct qid from tags where nametag in (" + conQuery.TrimEnd(',') + "));"); } else { dt = TempDB.query("SELECT * FROM `questions` WHERE (" + fromall + "uid = " + Globals.logUser.id + ") and dlevel >= " + numericMin.Value + " and dlevel <= " + numericMax.Value + " and id IN (SELECT qid FROM `tags` WHERE nametag IN (SELECT nametag FROM tags WHERE nametag IN (" + conQuery.TrimEnd(',') + ")) GROUP BY qid HAVING COUNT( qid ) = " + tags.Length + ")"); } for (int i = 0; i < dt.Columns.Count; i++) { dt.Columns[i].ReadOnly = true; } Utilities.InvokeMe(dgvFoundQ, () => { dgvFoundQ.DataSource = dt; dgvFoundQ.Columns[0].Visible = true; dgvFoundQ.Columns[0].Width = 50; dgvFoundQ.Columns[1].Visible = false; dgvFoundQ.Columns[2].HeaderText = "Questions"; dgvFoundQ.Columns[2].SortMode = DataGridViewColumnSortMode.NotSortable; dgvFoundQ.Columns[2].Width = 390; dgvFoundQ.Columns[3].Visible = false; dgvFoundQ.Columns[4].HeaderText = "Difficulty"; dgvFoundQ.Columns[4].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; dgvFoundQ.Columns[4].SortMode = DataGridViewColumnSortMode.NotSortable; dgvFoundQ.Columns[4].Width = 80; dgvFoundQ.Columns[5].Visible = false; dgvFoundQ.Columns[6].Visible = false; for (int i = 0; i < dgvFoundQ.Rows.Count; i++) { dgvFoundQ.Rows[i].Cells[0].Value = "False"; } }); if (dt.Rows.Count < 1) { DataTable da = new DataTable(); Utilities.InvokeMe(dgvFoundQ, () => { dgvFoundQ.DataSource = da; dgvFoundQ.Columns[0].Visible = false; }); } Utilities.notifyThem(ntbfindQ, "Found " + dt.Rows.Count + " Questions", NotificationBox.Type.Notice); }).Start(); } }