Guest User

Untitled

a guest
Feb 11th, 2016
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.40 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Diagnostics;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. // To detect internet connection detection
  12. using System.Net;
  13. using System.Net.Sockets;
  14. using System.Runtime.InteropServices;
  15. // For periodically run specific function
  16. // add a timer to form
  17. // for SQLite
  18. using System.Data.SQLite;
  19. using System.IO;
  20.  
  21. namespace kospen
  22. {
  23. public partial class Login : Form
  24. {
  25. // API for internet connection detection
  26. [DllImport("wininet.dll")]
  27. private extern static bool InternetGetConnectedState(out int Description, int ReservedValue);
  28.  
  29.  
  30. public Login()
  31. {
  32. InitializeComponent();
  33. }
  34.  
  35. // function to detect internet connection
  36. public static bool InternetExist()
  37. {
  38. try
  39. {
  40. int ConnDesc;
  41. return InternetGetConnectedState(out ConnDesc, 0);
  42. // if connection detected, it will return true
  43. }
  44. catch
  45. {
  46. return false;
  47. }
  48. }
  49.  
  50.  
  51. private void linkLabelRegister_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  52. {
  53. //ProcessStartInfo sInfo = new ProcessStartInfo("http://offline.moves.moh.gov.my/register");
  54. ProcessStartInfo sInfo = new ProcessStartInfo("http://offline.moves.alqadam.net/register");
  55. Process.Start(sInfo);
  56. }
  57.  
  58. private void Login_Load(object sender, EventArgs e)
  59. {
  60. // we put timer on form..so it will always reload this page at 100ms
  61. // put some function to detect internet
  62. Boolean internetDetected = InternetExist();
  63.  
  64. if (internetDetected)
  65. {
  66. toolStripStatusLabelOnlineStatus.Text = "ONLINE";
  67. toolStripStatusLabelOnlineStatus.ForeColor = Color.Green;
  68. }
  69. else
  70. {
  71. toolStripStatusLabelOnlineStatus.Text = "OFFLINE";
  72. toolStripStatusLabelOnlineStatus.ForeColor = Color.Red;
  73. }
  74. }
  75.  
  76. private void buttonLogin_Click(object sender, EventArgs e)
  77. {
  78.  
  79. String username = textBoxUsername.Text;
  80. String password = textBoxPassword.Text;
  81. String userNamaDb = "";
  82. String userIdDb = "";
  83. String usernameDb = "";
  84. String passwordDb = "";
  85. Boolean loginPass = false;
  86.  
  87. Boolean internetDetected = InternetExist();
  88. int rowCount;
  89.  
  90. // Boolean internet_connection = true;
  91. Boolean internet_connection = internetDetected;
  92. //check if online
  93. if (internet_connection == true)
  94. {
  95.  
  96. //authenticate with webserver
  97. try
  98. {
  99. string user = username;
  100. string pass = password;
  101.  
  102. ASCIIEncoding encoding = new ASCIIEncoding();
  103. string postData = "email=" + user + "&password=" + pass;
  104. byte[] data = encoding.GetBytes(postData);
  105.  
  106. WebRequest request = WebRequest.Create("http://www.moves.moh.gov.my/kospenoffline/result");
  107. //WebRequest request = WebRequest.Create("http://www.moves.moh.gov.my/training/kospenoffline/result");
  108. //WebRequest request = WebRequest.Create("http://offline.kospen.zulfatech.com/result");
  109. //WebRequest request = WebRequest.Create("http://offline.moves.alqadam.net/result");
  110. //WebRequest request = WebRequest.Create("http://domb.app/result");
  111. request.Method = "POST";
  112. request.ContentType = "application/x-www-form-urlencoded";
  113. request.ContentLength = data.Length;
  114.  
  115. Stream stream = request.GetRequestStream();
  116. stream.Write(data, 0, data.Length);
  117. stream.Close();
  118.  
  119. WebResponse response = request.GetResponse();
  120. stream = response.GetResponseStream();
  121.  
  122. StreamReader sr = new StreamReader(stream);
  123.  
  124. //split response into array
  125. String statusAuth = sr.ReadToEnd().ToString();
  126. string[] tokens = statusAuth.Split(',');
  127.  
  128. if (tokens[0] == "true")
  129. {
  130. loginPass = true;
  131.  
  132. //Get array response
  133. String id = tokens[1];
  134. String nama = tokens[2];
  135. String no_kp = tokens[3];
  136. String no_tel = tokens[4];
  137. String email = tokens[5];
  138. String uname = tokens[6];
  139. String passw = tokens[7];
  140. String permissions = tokens[8];
  141. String activated = tokens[9];
  142. String activation_code = tokens[10];
  143. String activated_at = tokens[11];
  144. String last_login = tokens[12];
  145. String persist_code = tokens[13];
  146. String reset_password_code = tokens[14];
  147. String created_at = tokens[15];
  148. String updated_at = tokens[16];
  149.  
  150. //patch by firdaus 4/2/2016
  151.  
  152. String ud_id = tokens[17];
  153. String ud_neg_id = tokens[18];
  154. String ud_dae_id = tokens[19];
  155. String ud_use_id = tokens[20];
  156. String ud_lokaliti_id = tokens[21];
  157. String ud_created_at = tokens[22];
  158. String ud_updated_at = tokens[23];
  159.  
  160.  
  161. // insert into database
  162. SQLiteConnection conn = new SQLiteConnection("data source=kospen.sqlite");
  163. conn.Open();
  164.  
  165. SQLiteCommand cmd = new SQLiteCommand(conn);
  166. SQLiteCommand cmdLatestId = new SQLiteCommand();
  167. SQLiteCommand cmdUsers = new SQLiteCommand(conn);
  168.  
  169. // convert plain password into base64
  170. var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(passw);
  171. String passwEnc = System.Convert.ToBase64String(plainTextBytes);
  172.  
  173. cmd.CommandText = "SELECT COUNT(*) FROM auth where id='" + id + "'";
  174. cmd.CommandType = CommandType.Text;
  175. rowCount = Convert.ToInt32(cmd.ExecuteScalar());
  176.  
  177. cmdUsers.CommandText = "SELECT COUNT(*) FROM users where id='" + id + "'";
  178. cmdUsers.CommandType = CommandType.Text;
  179. int rowCountUsers = Convert.ToInt32(cmdUsers.ExecuteScalar());
  180.  
  181. if (rowCount.ToString() == "0")
  182. {
  183. cmd.CommandText = "INSERT INTO auth (id,nama,no_kp,no_tel,email,username,password,permissions,activated,activation_code,activated_at,last_login,persist_code,reset_password_code,created_at,updated_at) " +
  184. "VALUES ('" + id + "','" +
  185. nama + "','" +
  186. no_kp + "','" +
  187. no_tel + "','" +
  188. email + "','" +
  189. uname + "','" +
  190. passwEnc + "','" +
  191. permissions + "','" +
  192. activated + "','" +
  193. activation_code + "','" +
  194. activated_at + "','" +
  195. last_login + "','" +
  196. persist_code + "','" +
  197. reset_password_code + "','" +
  198. created_at + "','" +
  199. updated_at + "')";
  200.  
  201. var status = cmd.ExecuteNonQuery();
  202. }
  203. else
  204. {
  205. cmd.CommandText = "UPDATE auth SET nama = '" + nama +
  206. "',no_kp = '" + no_kp +
  207. "',no_tel = '" + no_tel +
  208. "',email = '" + email +
  209. "',username = '" + uname +
  210. "',permissions = '" + permissions +
  211. "',activated = '" + activated +
  212. "',activation_code = '" + activation_code +
  213. "',activated_at = '" + activated_at +
  214. "',last_login = '" + last_login +
  215. "',persist_code = '" + persist_code +
  216. "',reset_password_code = '" + reset_password_code +
  217. "',created_at = '" + created_at +
  218. "',updated_at = '" + updated_at + "' where id ='" + id + "'";
  219.  
  220. cmd.ExecuteNonQuery();
  221. }
  222.  
  223.  
  224. if (rowCountUsers.ToString() == "0")
  225. {
  226. cmd.CommandText = "INSERT INTO users (id,nama,no_kp,no_tel,email,username,password,permissions,activated,activation_code,activated_at,last_login,persist_code,reset_password_code,created_at,updated_at) " +
  227. "VALUES ('" + id + "','" +
  228. nama + "','" +
  229. no_kp + "','" +
  230. no_tel + "','" +
  231. email + "','" +
  232. uname + "','" +
  233. passwEnc + "','" +
  234. permissions + "','" +
  235. activated + "','" +
  236. activation_code + "','" +
  237. activated_at + "','" +
  238. last_login + "','" +
  239. persist_code + "','" +
  240. reset_password_code + "','" +
  241. created_at + "','" +
  242. updated_at + "')";
  243. cmd.ExecuteNonQuery();
  244.  
  245. cmd.CommandText = "INSERT INTO users_details (id,negeri_id,daerah_id,users_id,lokaliti_id,created_at,updated_at) " +
  246. "VALUES ('" + ud_id + "','" +
  247. ud_neg_id + "','" +
  248. ud_dae_id + "','" +
  249. ud_use_id + "','" +
  250. ud_lokaliti_id + "','" +
  251. ud_created_at + "','" +
  252. ud_updated_at + "')";
  253. cmd.ExecuteNonQuery();
  254.  
  255. }
  256. else
  257. {
  258. cmd.CommandText = "UPDATE users SET nama = '" + nama +
  259. "',no_kp = '" + no_kp +
  260. "',no_tel = '" + no_tel +
  261. "',email = '" + email +
  262. "',username = '" + uname +
  263. "',permissions = '" + permissions +
  264. "',activated = '" + activated +
  265. "',activation_code = '" + activation_code +
  266. "',activated_at = '" + activated_at +
  267. "',last_login = '" + last_login +
  268. "',persist_code = '" + persist_code +
  269. "',reset_password_code = '" + reset_password_code +
  270. "',created_at = '" + created_at +
  271. "',updated_at = '" + updated_at + "' where id ='" + id + "'";
  272. cmd.ExecuteNonQuery();
  273.  
  274.  
  275. cmd.CommandText = "UPDATE users_details SET id = '" + ud_id +
  276. "',negeri_id = '" + ud_neg_id +
  277. "',daerah_id = '" + ud_dae_id +
  278. "',users_id = '" + ud_use_id +
  279. "',lokaliti_id = '" + ud_lokaliti_id +
  280. "',created_at = '" + ud_created_at +
  281. "',updated_at = '" + ud_updated_at + "' where users_id ='" + ud_use_id + "'";
  282. cmd.ExecuteNonQuery();
  283.  
  284.  
  285. }
  286.  
  287.  
  288. }
  289. else
  290. {
  291. loginPass = false;
  292. }
  293.  
  294.  
  295. sr.Close();
  296. stream.Close();
  297. }
  298. catch (Exception ex)
  299. {
  300. MessageBox.Show("Error : " + ex.Message);
  301. }
  302.  
  303. }
  304. else
  305. {
  306. // Execute here when offline
  307. // Establish connection with local sqlite
  308. SQLiteConnection conn = new SQLiteConnection("data source=kospen.sqlite");
  309. conn.Open();
  310.  
  311.  
  312. var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(password);
  313. String passwEnc = System.Convert.ToBase64String(plainTextBytes);
  314.  
  315. // count record
  316. SQLiteCommand cmd2 = new SQLiteCommand(conn);
  317. cmd2.CommandText = "SELECT COUNT(*) FROM auth where email='"+username+ "' and password = '"+ passwEnc + "'";
  318. cmd2.CommandType = CommandType.Text;
  319. rowCount = Convert.ToInt32(cmd2.ExecuteScalar());
  320.  
  321. //if record count equal to 1 means user is valid
  322. if(rowCount.ToString() == "1")
  323. {
  324. loginPass = true;
  325. }
  326. else
  327. {
  328. loginPass = false;
  329. }
  330.  
  331.  
  332. }
  333.  
  334. // Establish connection
  335. SQLiteConnection con = new SQLiteConnection("data source=kospen.sqlite");
  336. con.Open();
  337.  
  338. // SQL command start here
  339. SQLiteCommand cmd1 = new SQLiteCommand(con);
  340. cmd1.CommandText = "SELECT * FROM users WHERE email='"+username+"'";
  341. SQLiteDataReader reader = cmd1.ExecuteReader();
  342.  
  343. while (reader.Read())
  344. {
  345. usernameDb = reader["email"].ToString();
  346. passwordDb = reader["no_kp"].ToString();
  347. userNamaDb = reader["nama"].ToString();
  348. userIdDb = reader["id"].ToString();
  349. }
  350.  
  351. // Check authencation pass or not using local storage
  352. if (loginPass)
  353. {
  354. this.Hide();
  355. var fListPatients = new FormListPatients( userIdDb, userNamaDb);
  356. // if new form is close, we want the original form to be closed as well. Else, mem leak nanti.
  357. fListPatients.Closed += (s, args) => this.Close();
  358. fListPatients.Show();
  359.  
  360.  
  361. }
  362.  
  363. // count record
  364. // SQLiteCommand cmdCount = new SQLiteCommand(con);
  365. // cmdCount.CommandText = "SELECT COUNT(*) FROM users";
  366. // cmdCount.CommandType = CommandType.Text;
  367. // rowCount = Convert.ToInt32(cmdCount.ExecuteScalar());
  368.  
  369. // MessageBox.Show(rowCount.ToString());
  370.  
  371. // SQL command end here
  372.  
  373. con.Close();
  374.  
  375.  
  376. // If not, the user may not exist in our local storage/db, but
  377. // already registered in the server. So, check internet connection
  378. // and sync database. Ask user either they want to sync db with server or not
  379. if (loginPass == false)
  380. {
  381. DialogResult dbSync = MessageBox.Show("Kombinasi katalaluan yang salah. " +
  382. "Anda perlukan akses internet sekiranya ini adalah kali pertama anda menggunakan akaun anda di aplikasi ini");
  383. if (dbSync == DialogResult.Yes)
  384. {
  385. // User want to sync database with the server. If internet exist, proceed with update, else notify user to connect to the internet
  386. if (InternetExist())
  387. {
  388. // perform data sync
  389. }
  390. else
  391. {
  392. MessageBox.Show("No internet connection. Please connect to the Internet and try again once the connection status become ONLINE");
  393. }
  394. }
  395. }
  396.  
  397.  
  398.  
  399.  
  400. // Then perform authencation using local one.
  401. }
  402.  
  403. private void pictureBox1_Click(object sender, EventArgs e)
  404. {
  405. // FormInit systemInitializer = new FormInit();
  406. // systemInitializer.Show();
  407. }
  408.  
  409. private void textBoxPassword_TextChanged(object sender, EventArgs e)
  410. {
  411.  
  412. }
  413.  
  414. private void textBoxUsername_TextChanged(object sender, EventArgs e)
  415. {
  416.  
  417. }
  418.  
  419. private void label2_Click(object sender, EventArgs e)
  420. {
  421.  
  422. }
  423.  
  424. private void label1_Click(object sender, EventArgs e)
  425. {
  426.  
  427. }
  428. }
  429. }
Add Comment
Please, Sign In to add comment