PageRenderTime 23ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/web/OrganicPM/process.php

http://praticarh.googlecode.com/
PHP | 279 lines | 162 code | 36 blank | 81 comment | 18 complexity | de0940836c4ad1e876f6ed1e8ca93ab0 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  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. * @author Jpmaster77 a.k.a. The Grandmaster of C++ (GMC)
  11. * @version August 19, 2004
  12. */
  13. include_once 'config/config.inc.php';
  14. include_once 'lib/LoginSystem/Session.class.php';
  15. include_once 'plugins/validateUsername.function.php';
  16. class Process
  17. {
  18. //==================================================================
  19. // Attributes ======================================================
  20. //==================================================================
  21. public $session;
  22. public $form;
  23. public $mailer;
  24. public $user;
  25. //==================================================================
  26. // Magic Methods ===================================================
  27. //==================================================================
  28. /* Class constructor */
  29. function __construct()
  30. {
  31. global $session, $form, $mailer, $user;
  32. $this->session = $session;
  33. $this->form = $form;
  34. $this->mailer = $mailer;
  35. $this->user = $user;
  36. /* User submitted login form */
  37. if(isset($_POST['sublogin']))
  38. {
  39. $this->procLogin();
  40. }
  41. /* User submitted registration form */
  42. elseif(isset($_POST['subjoin']))
  43. {
  44. $this->procRegister();
  45. }
  46. /* User submitted forgot password form */
  47. elseif(isset($_POST['subforgot']))
  48. {
  49. $this->procForgotPass();
  50. }
  51. /* User submitted edit account form */
  52. elseif(isset($_POST['subedit']))
  53. {
  54. $this->procEditAccount();
  55. }
  56. /**
  57. * The only other reason user should be directed here
  58. * is if he wants to logout, which means user is
  59. * logged in currently.
  60. */
  61. elseif($session->loggedIn)
  62. {
  63. $this->procLogout();
  64. }
  65. /**
  66. * Should not get here, which means user is viewing this page
  67. * by mistake and therefore is redirected.
  68. */
  69. else
  70. {
  71. header("Location: login.php");
  72. }
  73. }
  74. //==================================================================
  75. // Methods =========================================================
  76. //==================================================================
  77. /**
  78. * procLogin - Processes the user submitted login form, if errors
  79. * are found, the user is redirected to correct the information,
  80. * if not, the user is effectively logged in to the system.
  81. */
  82. function procLogin()
  83. {
  84. /* Login attempt */
  85. $retval = $this->session->login($_POST['user'], $_POST['pass'], $_POST['challenge'], isset($_POST['remember']));
  86. /* Login successful */
  87. if($retval)
  88. {
  89. if ($this->session->isCandidato())
  90. {
  91. header("Location: candidato/");
  92. }
  93. elseif ($this->session->isAdmin())
  94. {
  95. header("Location: admin/");
  96. }
  97. elseif ($this->session->isFuncionario())
  98. {
  99. header("Location: candidato/");
  100. }
  101. elseif ($this->session->isFuncionarioRH())
  102. {
  103. header("Location: admin/");
  104. }
  105. else
  106. {
  107. header("Location: index.php");
  108. }
  109. }
  110. /* Login failed */
  111. else
  112. {
  113. $_SESSION['value_array'] = $_POST;
  114. $_SESSION['error_array'] = $this->form->getErrorArray();
  115. header("Location: login.php");
  116. }
  117. }
  118. /**
  119. * procLogout - Simply attempts to log the user out of the system
  120. * given that there is no logout form to process.
  121. */
  122. function procLogout()
  123. {
  124. $retval = $this->session->logout();
  125. header("Location: login.php");
  126. }
  127. /**
  128. * procRegister - Processes the user submitted registration form,
  129. * if errors are found, the user is redirected to correct the
  130. * information, if not, the user is effectively registered with
  131. * the system and an email is (optionally) sent to the newly
  132. * created user.
  133. */
  134. function procRegister()
  135. {
  136. /* Convert username to all lowercase (by option) */
  137. if(ALL_LOWERCASE)
  138. {
  139. $_POST['user'] = strtolower($_POST['user']);
  140. }
  141. /* Registration attempt */
  142. $retval = $this->session->register($_POST['user'], $_POST['pass'], $_POST['email']);
  143. /* Registration Successful */
  144. if($retval == 0)
  145. {
  146. $_SESSION['reguname'] = $_POST['user'];
  147. $_SESSION['regsuccess'] = true;
  148. header("Location: ".$this->session->referrer);
  149. }
  150. /* Error found with form */
  151. elseif($retval == 1)
  152. {
  153. $_SESSION['value_array'] = $_POST;
  154. $_SESSION['error_array'] = $this->form->getErrorArray();
  155. header("Location: ".$this->session->referrer);
  156. }
  157. /* Registration attempt failed */
  158. elseif($retval == 2)
  159. {
  160. $_SESSION['reguname'] = $_POST['user'];
  161. $_SESSION['regsuccess'] = false;
  162. header("Location: ".$this->session->referrer);
  163. }
  164. }
  165. /**
  166. * procForgotPass - Validates the given username then if
  167. * everything is fine, a new password is generated and
  168. * emailed to the address the user gave on sign up.
  169. */
  170. function procForgotPass()
  171. {
  172. /* Username error checking */
  173. $subuser = $_POST['user'];
  174. $field = "user"; //Use field name for username
  175. if(!$subuser || strlen($subuser = trim($subuser)) == 0)
  176. {
  177. $this->form->setError($field, "Usuário n?o informado");
  178. }
  179. else
  180. {
  181. /* Make sure username is in database */
  182. $subuser = stripslashes($subuser);
  183. if(strlen($subuser) < 5 || strlen($subuser) > 30 || !validateUsername($subuser) || (!$this->user->usernameTaken($subuser)))
  184. {
  185. $this->form->setError($field, "Usuário n?o existe");
  186. }
  187. }
  188. /* Errors exist, have user correct them */
  189. if($this->form->numErrors > 0)
  190. {
  191. $_SESSION['value_array'] = $_POST;
  192. $_SESSION['error_array'] = $this->form->getErrorArray();
  193. }
  194. /* Generate new password and email it to user */
  195. else
  196. {
  197. /* Generate new password */
  198. $newpass = $this->session->generateRandStr(8);
  199. /* Get email of user */
  200. $usrinf = $this->user->getUserInfo($subuser);
  201. $email = $usrinf['email'];
  202. /* Attempt to send the email with new password */
  203. if($this->mailer->sendNewPass($subuser,$email,$newpass))
  204. {
  205. /* Email sent, update database */
  206. $this->user->updateUserField($subuser, "password", md5($newpass));
  207. $_SESSION['forgotpass'] = true;
  208. }
  209. /* Email failure, do not change password */
  210. else
  211. {
  212. $_SESSION['forgotpass'] = false;
  213. }
  214. }
  215. header("Location: ".$this->session->referrer);
  216. }
  217. /**
  218. * procEditAccount - Attempts to edit the user's account
  219. * information, including the password, which must be verified
  220. * before a change is made.
  221. */
  222. function procEditAccount()
  223. {
  224. /* Account edit attempt */
  225. $retval = $this->session->editAccount($_POST['curpass'], $_POST['newpass'], $_POST['email']);
  226. /* Account edit successful */
  227. if($retval)
  228. {
  229. $_SESSION['useredit'] = true;
  230. header("Location: ".$this->session->referrer);
  231. }
  232. /* Error found with form */
  233. else
  234. {
  235. $_SESSION['value_array'] = $_POST;
  236. $_SESSION['error_array'] = $this->form->getErrorArray();
  237. header("Location: ".$this->session->referrer);
  238. }
  239. }
  240. };
  241. /* Initialize process */
  242. $process = new Process;
  243. ?>