Login completo, campos extras agregados a la base de datos.
[CLab.git] / process.php
blob4d794d6a30742f843551e474f389dc9c8493f1ac
1 <?
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.
10 * Written by: Jpmaster77 a.k.a. The Grandmaster of C++ (GMC)
11 * Last Updated: August 19, 2004
13 include("include/session.php");
15 class Process
17 /* Class constructor */
18 function Process(){
19 global $session;
20 /* User submitted login form */
21 if(isset($_POST['sublogin'])){
22 $this->procLogin();
24 /* User submitted registration form */
25 else if(isset($_POST['subjoin'])){
26 $this->procRegister();
28 /* User submitted forgot password form */
29 else if(isset($_POST['subforgot'])){
30 $this->procForgotPass();
32 /* User submitted edit account form */
33 else if(isset($_POST['subedit'])){
34 $this->procEditAccount();
36 /**
37 * The only other reason user should be directed here
38 * is if he wants to logout, which means user is
39 * logged in currently.
41 else if($session->logged_in){
42 $this->procLogout();
44 /**
45 * Should not get here, which means user is viewing this page
46 * by mistake and therefore is redirected.
48 else{
49 header("Location: ".$session->referrer);
53 /**
54 * procLogin - Processes the user submitted login form, if errors
55 * are found, the user is redirected to correct the information,
56 * if not, the user is effectively logged in to the system.
58 function procLogin(){
59 global $session, $form;
60 /* Login attempt */
61 $retval = $session->login($_POST['user'], $_POST['pass'], isset($_POST['remember']));
63 /* Login successful */
64 if($retval){
65 header("Location: ".$session->referrer);
67 /* Login failed */
68 else{
69 $_SESSION['value_array'] = $_POST;
70 $_SESSION['error_array'] = $form->getErrorArray();
71 header("Location: ".$session->referrer);
75 /**
76 * procLogout - Simply attempts to log the user out of the system
77 * given that there is no logout form to process.
79 function procLogout(){
80 global $session;
81 $retval = $session->logout();
82 header("Location: ".$session->referrer);
85 /**
86 * procRegister - Processes the user submitted registration form,
87 * if errors are found, the user is redirected to correct the
88 * information, if not, the user is effectively registered with
89 * the system and an email is (optionally) sent to the newly
90 * created user.
92 function procRegister(){
93 global $session, $form;
94 /* Convert username to all lowercase (by option) */
95 if(ALL_LOWERCASE){
96 $_POST['user'] = strtolower($_POST['user']);
98 /* Registration attempt */
99 $retval = $session->register($_POST['user'], $_POST['pass'], $_POST['email'],$_POST['nombre'], $_POST['catedratico'], $_POST['tipo'], $_POST['departamento']);
101 /* Registration Successful */
102 if($retval == 0){
103 $_SESSION['reguname'] = $_POST['user'];
104 $_SESSION['regsuccess'] = true;
105 header("Location: ".$session->referrer);
107 /* Error found with form */
108 else if($retval == 1){
109 $_SESSION['value_array'] = $_POST;
110 $_SESSION['error_array'] = $form->getErrorArray();
111 header("Location: ".$session->referrer);
113 /* Registration attempt failed */
114 else if($retval == 2){
115 $_SESSION['reguname'] = $_POST['user'];
116 $_SESSION['regsuccess'] = false;
117 header("Location: ".$session->referrer);
122 * procForgotPass - Validates the given username then if
123 * everything is fine, a new password is generated and
124 * emailed to the address the user gave on sign up.
126 function procForgotPass(){
127 global $database, $session, $mailer, $form;
128 /* Username error checking */
129 $subuser = $_POST['user'];
130 $field = "user"; //Use field name for username
131 if(!$subuser || strlen($subuser = trim($subuser)) == 0){
132 $form->setError($field, "* Username not entered<br>");
134 else{
135 /* Make sure username is in database */
136 $subuser = stripslashes($subuser);
137 if(strlen($subuser) < 5 || strlen($subuser) > 30 ||
138 !eregi("^([0-9a-z])+$", $subuser) ||
139 (!$database->usernameTaken($subuser))){
140 $form->setError($field, "* Username does not exist<br>");
144 /* Errors exist, have user correct them */
145 if($form->num_errors > 0){
146 $_SESSION['value_array'] = $_POST;
147 $_SESSION['error_array'] = $form->getErrorArray();
149 /* Generate new password and email it to user */
150 else{
151 /* Generate new password */
152 $newpass = $session->generateRandStr(8);
154 /* Get email of user */
155 $usrinf = $database->getUserInfo($subuser);
156 $email = $usrinf['email'];
158 /* Attempt to send the email with new password */
159 if($mailer->sendNewPass($subuser,$email,$newpass)){
160 /* Email sent, update database */
161 $database->updateUserField($subuser, "password", md5($newpass));
162 $_SESSION['forgotpass'] = true;
164 /* Email failure, do not change password */
165 else{
166 $_SESSION['forgotpass'] = false;
170 header("Location: ".$session->referrer);
174 * procEditAccount - Attempts to edit the user's account
175 * information, including the password, which must be verified
176 * before a change is made.
178 function procEditAccount(){
179 global $session, $form;
180 /* Account edit attempt */
181 $retval = $session->editAccount($_POST['curpass'], $_POST['newpass'], $_POST['email']);
183 /* Account edit successful */
184 if($retval){
185 $_SESSION['useredit'] = true;
186 header("Location: ".$session->referrer);
188 /* Error found with form */
189 else{
190 $_SESSION['value_array'] = $_POST;
191 $_SESSION['error_array'] = $form->getErrorArray();
192 header("Location: ".$session->referrer);
197 /* Initialize process */
198 $process = new Process;