Permitir registro e inicio de sesiĆ³n de usuarios sin clave.
[CLab.git] / include / process.php
blob0d759f95400b4628c5bf47e072d680d328d22d64
1 <?
2 require_once("sesion.php");
4 class Process
6 /* Class constructor */
7 function Process(){
8 global $session;
9 /* User submitted login form */
10 if(isset($_POST['sublogin'])){
11 $this->procLogin();
13 /* User submitted registration form */
14 else if(isset($_POST['subjoin'])){
15 $this->procRegister();
17 /* User submitted forgot password form */
18 else if(isset($_POST['subforgot'])){
19 $this->procForgotPass();
21 /* User submitted edit account form */
22 else if(isset($_POST['subedit'])){
23 $this->procEditAccount();
25 /**
26 * The only other reason user should be directed here
27 * is if he wants to logout, which means user is
28 * logged in currently.
30 else if($session->logged_in){
31 DEPURAR ('ok Logout');
32 $this->procLogout();
34 /**
35 * Should not get here, which means user is viewing this page
36 * by mistake and therefore is redirected.
38 else{
39 header("Location: ../");
43 /**
44 * procLogin - Processes the user submitted login form, if errors
45 * are found, the user is redirected to correct the information,
46 * if not, the user is effectively logged in to the system.
48 function procLogin(){
49 global $session, $form;
50 /* Login attempt */
51 $retval = $session->login($_POST['user'], $_POST['pass'], isset($_POST['remember']));
53 /* Login successful */
54 if($retval){
55 $_SESSION['regsuccess'] = true;
58 /* Login failed */
59 else{
60 $_SESSION['regsuccess'] = false;
61 $_SESSION['value_array'] = $_POST;
62 $_SESSION['error_array'] = $form->getErrorArray();
64 header("Location:../?accion=ingresar");
67 /**
68 * procLogout - Simply attempts to log the user out of the system
69 * given that there is no logout form to process.
71 function procLogout(){
72 global $session;
73 $retval = $session->logout();
74 header("Location: ../");
77 /**
78 * procRegister - Processes the user submitted registration form,
79 * if errors are found, the user is redirected to correct the
80 * information, if not, the user is effectively registered with
81 * the system and an email is (optionally) sent to the newly
82 * created user.
84 function procRegister(){
85 global $session, $form;
86 /* Convert username to all lowercase (by option) */
87 if(ALL_LOWERCASE){
88 $_POST['user'] = strtolower($_POST['user']);
90 /* Registration attempt */
91 $retval = $session->register($_POST['user'], $_POST['pass'], $_POST['email'],$_POST['nombre'], $_POST['encargado'], $_POST['catedratico'], $_POST['tipo'], $_POST['departamento']);
93 /* Registration Successful */
94 if($retval == 0){
95 $_SESSION['regsuccess'] = true;
96 $_SESSION['reguname'] = $_POST['user'];
98 /* Error found with form */
99 else if($retval == 1){
100 $_SESSION['value_array'] = $_POST;
101 $_SESSION['error_array'] = $form->getErrorArray();
103 /* Registration attempt failed */
104 else if($retval == 2){
105 $_SESSION['reguname'] = $_POST['user'];
106 $_SESSION['regsuccess'] = false;
109 header("Location: ../?accion=registro");
113 * procForgotPass - Validates the given username then if
114 * everything is fine, a new password is generated and
115 * emailed to the address the user gave on sign up.
117 function procForgotPass(){
118 global $database, $session, $mailer, $form;
119 /* Username error checking */
120 $subuser = $_POST['user'];
121 $field = "user"; //Use field name for username
122 if(!$subuser || strlen($subuser = trim($subuser)) == 0){
123 $form->setError($field, "* Username not entered<br>");
125 else{
126 /* Make sure username is in database */
127 $subuser = stripslashes($subuser);
128 if(strlen($subuser) < 5 || strlen($subuser) > 30 ||
129 !eregi("^([0-9a-z])+$", $subuser) ||
130 (!$database->usernameTaken($subuser))){
131 $form->setError($field, "* Username does not exist<br>");
135 /* Errors exist, have user correct them */
136 if($form->num_errors > 0){
137 $_SESSION['value_array'] = $_POST;
138 $_SESSION['error_array'] = $form->getErrorArray();
140 /* Generate new password and email it to user */
141 else{
142 /* Generate new password */
143 $newpass = $session->generateRandStr(8);
145 /* Get email of user */
146 $usrinf = $database->getUserInfo($subuser);
147 $email = $usrinf['email'];
149 /* Attempt to send the email with new password */
150 if($mailer->sendNewPass($subuser,$email,$newpass)){
151 /* Email sent, update database */
152 $database->updateUserField($subuser, "password", md5($newpass));
153 $_SESSION['forgotpass'] = true;
155 /* Email failure, do not change password */
156 else{
157 $_SESSION['forgotpass'] = false;
161 header("Location: ./?x=rpr+clave");
165 * procEditAccount - Attempts to edit the user's account
166 * information, including the password, which must be verified
167 * before a change is made.
169 function procEditAccount(){
170 global $session, $form;
171 /* Account edit attempt */
172 $retval = $session->editAccount($_POST['curpass'], $_POST['newpass'], $_POST['email'], $_POST['username']);
174 /* Account edit successful */
175 if($retval){
176 $_SESSION['useredit'] = true;
177 header("Location: ".$session->referrer);
179 /* Error found with form */
180 else{
181 $_SESSION['value_array'] = $_POST;
182 $_SESSION['error_array'] = $form->getErrorArray();
183 header("Location: ".$session->referrer);
188 /* Initialize process */
189 $process = new Process;