Advertisement
Guest User

process.php

a guest
Jun 27th, 2015
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.00 KB | None | 0 0
  1. <?php
  2. /**
  3. * Process.php
  4. *
  5. * The Process class is meant to simplify the task of processing
  6. * user submitted forms, redirecting the user to the correct
  7. * pages if errors are found, or if form is successful, either
  8. * way. Also handles the logout procedure.
  9. *
  10. * Please subscribe to our feeds at http://blog.geotitles.com for more such tutorials
  11. */
  12. include("include/session.php");
  13.  
  14. class Process
  15. {
  16. /* Class constructor */
  17. function Process(){
  18. global $session;
  19. /* User submitted login form */
  20. if(isset($_POST['sublogin'])){
  21. $this->procLogin();
  22. }
  23. /* User submitted registration form */
  24. else if(isset($_POST['subjoin'])){
  25. $this->procRegister();
  26. }
  27. /* User submitted forgot password form */
  28. else if(isset($_POST['subforgot'])){
  29. $this->procForgotPass();
  30. }
  31. /* User submitted edit account form */
  32. else if(isset($_POST['subedit'])){
  33. $this->procEditAccount();
  34. }
  35. /**
  36. * The only other reason user should be directed here
  37. * is if he wants to logout, which means user is
  38. * logged in currently.
  39. */
  40. else if($session->logged_in){
  41. $this->procLogout();
  42. }
  43. /**
  44. * Should not get here, which means user is viewing this page
  45. * by mistake and therefore is redirected.
  46. */
  47. else{
  48. header("Location: login.php");
  49. }
  50. }
  51.  
  52. /**
  53. * procLogin - Processes the user submitted login form, if errors
  54. * are found, the user is redirected to correct the information,
  55. * if not, the user is effectively logged in to the system.
  56. */
  57. function procLogin(){
  58. global $session, $form;
  59. /* Login attempt */
  60. $retval = $session->login($_POST['user'], $_POST['pass'], isset($_POST['remember']));
  61.  
  62. /* Login successful */
  63. if($retval){
  64. header("Location: ".$session->referrer);
  65. }
  66. /* Login failed */
  67. else{
  68. $_SESSION['value_array'] = $_POST;
  69. $_SESSION['error_array'] = $form->getErrorArray();
  70. header("Location: ".$session->referrer);
  71. }
  72. }
  73.  
  74. /**
  75. * procLogout - Simply attempts to log the user out of the system
  76. * given that there is no logout form to process.
  77. */
  78. function procLogout(){
  79. global $session;
  80. $retval = $session->logout();
  81. header("Location: login.php");
  82. }
  83.  
  84. /**
  85. * procRegister - Processes the user submitted registration form,
  86. * if errors are found, the user is redirected to correct the
  87. * information, if not, the user is effectively registered with
  88. * the system and an email is (optionally) sent to the newly
  89. * created user.
  90. */
  91. function procRegister(){
  92. global $session, $form;
  93. /* Convert username to all lowercase (by option) */
  94. if(ALL_LOWERCASE){
  95. $_POST['user'] = strtolower($_POST['user']);
  96. }
  97. /* Registration attempt */
  98. $retval = $session->register($_POST['user'], $_POST['pass'], $_POST['email']);
  99.  
  100. /* Registration Successful */
  101. if($retval == 0){
  102. $_SESSION['reguname'] = $_POST['user'];
  103. $_SESSION['regsuccess'] = true;
  104. header("Location: ".$session->referrer);
  105. }
  106. /* Error found with form */
  107. else if($retval == 1){
  108. $_SESSION['value_array'] = $_POST;
  109. $_SESSION['error_array'] = $form->getErrorArray();
  110. header("Location: ".$session->referrer);
  111. }
  112. /* Registration attempt failed */
  113. else if($retval == 2){
  114. $_SESSION['reguname'] = $_POST['user'];
  115. $_SESSION['regsuccess'] = false;
  116. header("Location: ".$session->referrer);
  117. }
  118. }
  119.  
  120. /**
  121. * procForgotPass - Validates the given username then if
  122. * everything is fine, a new password is generated and
  123. * emailed to the address the user gave on sign up.
  124. */
  125. /*function procForgotPass(){
  126. global $database, $session, $mailer, $form;
  127. /* Username error checking */
  128. /*$subuser = $_POST['user'];
  129. $field = "user"; //Use field name for username
  130. if(!$subuser || strlen($subuser = trim($subuser)) == 0){
  131. $form->setError($field, "* Username not entered<br>");
  132. }
  133. else{
  134. /* Make sure username is in database */
  135. /*$subuser = stripslashes($subuser);
  136. if(strlen($subuser) < 5 || strlen($subuser) > 30 ||
  137. !preg_match("/^([0-9a-z])+$/i", $subuser) ||
  138. (!$database->usernameTaken($subuser))){
  139. $form->setError($field, "* Username does not exist<br>");
  140. }
  141. }*/
  142.  
  143.  
  144. function procForgotPass(){
  145. global $database, $session, $mailer, $form;
  146. /* email error checking */
  147. $subemail = $_POST['email'];
  148. $field = "email"; //Use field name for email
  149. if(!$subemail || strlen($subemail = trim($subemail)) == 0){
  150. $form->setError($field, "* Email not entered<br>");
  151. }
  152. else{
  153. /* Make sure email is in database */
  154. $subemail = stripslashes($subemail);
  155. if(!$subemail || strlen($subemail = trim($subemail)) == 0){
  156. $form->setError($field, "* Email does not exist<br>");
  157. }
  158. }
  159.  
  160.  
  161. /* Errors exist, have user correct them */
  162. if($form->num_errors > 0){
  163. $_SESSION['value_array'] = $_POST;
  164. $_SESSION['error_array'] = $form->getErrorArray();
  165. }
  166. /* Generate new password and email it to user */
  167. else{
  168. /* Generate new password */
  169. $newpass = $session->generateRandStr(8);
  170.  
  171. /* Get email of user */
  172. $usrinf = $database->getUserInfo($subuser);
  173. $email = $usrinf['email'];
  174.  
  175. /* Attempt to send the email with new password */
  176. if($mailer->sendNewPass($subuser,$email,$newpass)){
  177. /* Email sent, update database */
  178. $database->updateUserField($subuser, "password", md5($newpass));
  179. $_SESSION['forgotpass'] = true;
  180. }
  181. /* Email failure, do not change password */
  182. else{
  183. $_SESSION['forgotpass'] = false;
  184. }
  185. }
  186.  
  187. header("Location: ".$session->referrer);
  188. }
  189.  
  190. /**
  191. * procEditAccount - Attempts to edit the user's account
  192. * information, including the password, which must be verified
  193. * before a change is made.
  194. */
  195. function procEditAccount(){
  196. global $session, $form;
  197. /* Account edit attempt */
  198. $retval = $session->editAccount($_POST['curpass'], $_POST['newpass'], $_POST['email']);
  199.  
  200. /* Account edit successful */
  201. if($retval){
  202. $_SESSION['useredit'] = true;
  203. header("Location: ".$session->referrer);
  204. }
  205. /* Error found with form */
  206. else{
  207. $_SESSION['value_array'] = $_POST;
  208. $_SESSION['error_array'] = $form->getErrorArray();
  209. header("Location: ".$session->referrer);
  210. }
  211. }
  212. };
  213.  
  214. /* Initialize process */
  215. $process = new Process;
  216.  
  217. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement