Advertisement
Guest User

Untitled

a guest
Jul 30th, 2015
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.35 KB | None | 0 0
  1. <?php
  2. if (isset($_POST['tag']) && $_POST['tag'] != '') {
  3. // Get tag
  4. $tag = $_POST['tag'];
  5.  
  6. // Include Database handler
  7. require_once 'CONNECT/DB_Functions.php';
  8. $db = new DB_Functions();
  9. // response Array
  10. $response = array("tag" => $tag, "success" => 0, "error" => 0);
  11. // check for tag type
  12. if ($tag == 'login') {
  13. // Request type is check Login
  14. $email = $_POST['email'];
  15. $password = $_POST['password'];
  16. // check for user
  17. $user = $db->getUserByEmailAndPassword($email, $password);
  18. if ($user != false) {
  19. // user found
  20. // echo json with success = 1
  21. $response["success"] = 1;
  22. $response["user"]["fname"] = $user["firstname"];
  23. $response["user"]["lname"] = $user["lastname"];
  24. $response["user"]["email"] = $user["email"];
  25. $response["user"]["uname"] = $user["username"];
  26. $response["user"]["uid"] = $user["unique_id"];
  27. $response["user"]["created_at"] = $user["created_at"];
  28.  
  29. echo json_encode($response);
  30. } else {
  31. // user not found
  32. // echo json with error = 1
  33. $response["error"] = 1;
  34. $response["error_msg"] = "Incorrect email or password!";
  35. echo json_encode($response);
  36. }
  37. }
  38. else if ($tag == 'chgpass'){
  39. $email = $_POST['email'];
  40.  
  41. $newpassword = $_POST['newpas'];
  42.  
  43.  
  44. $hash = $db->hashSSHA($newpassword);
  45. $encrypted_password = $hash["encrypted"]; // encrypted password
  46. $salt = $hash["salt"];
  47. $subject = "Change Password Notification";
  48. $message = "Hello User,nnYour Password was sucessfully changed.nnRegards,nAnas Tawtaw.";
  49. $from = "Sender";
  50. $headers = "From:" . $from;
  51. if ($db->isUserExisted($email)) {
  52.  
  53. $user = $db->forgotPassword($email, $encrypted_password, $salt);
  54. if ($user) {
  55. $response["success"] = 1;
  56. mail($email,$subject,$message,$headers);
  57. echo json_encode($response);
  58. }
  59. else {
  60. $response["error"] = 1;
  61. echo json_encode($response);
  62. }
  63. // user is already existed - error response
  64. }
  65. else {
  66. $response["error"] = 2;
  67. $response["error_msg"] = "User doesn't exist";
  68. echo json_encode($response);
  69. }
  70. }
  71. else if ($tag == 'forpass'){
  72. $forgotpassword = $_POST['forgotpassword'];
  73.  
  74. $randomcode = $db->random_string();
  75.  
  76.  
  77. $hash = $db->hashSSHA($randomcode);
  78. $encrypted_password = $hash["encrypted"]; // encrypted password
  79. $salt = $hash["salt"];
  80. $subject = "Password Recovery";
  81. $message = "Hello $fname,nnYour Password is sucessfully changed. Your new Password is $randomcode . Login with your new Password and change it in the User Panel.nnRegards,nAnas Tawtaw.";
  82. $from = "sender";
  83. $headers = "From:" . $from;
  84. if ($db->isUserExisted($forgotpassword)) {
  85.  
  86. $user = $db->forgotPassword($forgotpassword, $encrypted_password, $salt);
  87. if ($user) {
  88. $response["success"] = 1;
  89. mail($forgotpassword,$subject,$message,$headers);
  90. echo json_encode($response);
  91. }
  92. else {
  93. $response["error"] = 1;
  94. echo json_encode($response);
  95. }
  96. // user is already existed - error response
  97.  
  98. }
  99. else {
  100. $response["error"] = 2;
  101. $response["error_msg"] = "User not exist";
  102. echo json_encode($response);
  103. }
  104.  
  105. }
  106. else if ($tag == 'register') {
  107. // Request type is Register new user
  108. $fname = $_POST['fname'];
  109. $lname = $_POST['lname'];
  110. $email = $_POST['email'];
  111. $uname = $_POST['uname'];
  112. $password = $_POST['password'];
  113.  
  114. $subject = "Welcome to the app";
  115. $message = "Hello $fname,nnWelcome to my app .nnRegards,nAnas Tawtaw.";
  116. $from = "Sender";
  117. $headers = "From:" . $from;
  118.  
  119. // check if user is already existed
  120. if ($db->isUserExisted($email)) {
  121. // user is already existed - error response
  122. $response["error"] = 2;
  123. $response["error_msg"] = "User already existed";
  124. echo json_encode($response);
  125. }
  126. else if(!$db->validEmail($email)){
  127. $response["error"] = 3;
  128. $response["error_msg"] = "Invalid Email Id";
  129. echo json_encode($response);
  130. }
  131. else {
  132. // store user
  133. $user = $db->storeUser($fname, $lname, $email, $uname, $password);
  134. if ($user) {
  135. // user stored successfully
  136. $response["success"] = 1;
  137. $response["user"]["fname"] = $user["firstname"];
  138. $response["user"]["lname"] = $user["lastname"];
  139. $response["user"]["email"] = $user["email"];
  140. $response["user"]["uname"] = $user["username"];
  141. $response["user"]["uid"] = $user["unique_id"];
  142. $response["user"]["created_at"] = $user["created_at"];
  143. mail($email,$subject,$message,$headers);
  144.  
  145. echo json_encode($response);
  146. } else {
  147. // user failed to store
  148. $response["error"] = 1;
  149. $response["error_msg"] = "JSON Error occured in Registartion";
  150. echo json_encode($response);
  151. }
  152. }
  153. } else {
  154. $response["error"] = 3;
  155. $response["error_msg"] = "JSON ERROR";
  156. echo json_encode($response);
  157. }
  158. } else {
  159. echo "Hello SOF";
  160. }
  161. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement