Restaurado: EdiciciĆ³n EliminaciĆ³n de instructores
[CLab.git] / include / process.php
blobcf14565252db8efff364b070ca56d52008083794
1 <?
2 require_once("sesion.php");
4 function DEPURAR($sTexto){
5 if (0) {echo $sTexto."<br />";}
8 class Process
10 /* Class constructor */
11 function Process(){
12 global $session;
13 /* User submitted login form */
14 if(isset($_POST['sublogin'])){
15 $this->procLogin();
17 /* User submitted registration form */
18 else if(isset($_POST['subjoin'])){
19 $this->procRegister();
21 /* User submitted forgot password form */
22 else if(isset($_POST['subforgot'])){
23 $this->procForgotPass();
25 /* User submitted edit account form */
26 else if(isset($_POST['subedit'])){
27 $this->procEditAccount();
29 /**
30 * The only other reason user should be directed here
31 * is if he wants to logout, which means user is
32 * logged in currently.
34 else if($session->logged_in){
35 DEPURAR ('ok Logout');
36 $this->procLogout();
38 /**
39 * Should not get here, which means user is viewing this page
40 * by mistake and therefore is redirected.
42 else{
43 header("Location: ../");
47 /**
48 * procLogin - Processes the user submitted login form, if errors
49 * are found, the user is redirected to correct the information,
50 * if not, the user is effectively logged in to the system.
52 function procLogin(){
53 global $session, $form;
54 /* Login attempt */
55 $retval = $session->login($_POST['user'], $_POST['pass'], isset($_POST['remember']));
57 /* Login successful */
58 if($retval){
59 $_SESSION['regsuccess'] = true;
61 /* Login failed */
62 else{
63 $_SESSION['regsuccess'] = false;
64 $_SESSION['value_array'] = $_POST;
65 $_SESSION['error_array'] = $form->getErrorArray();
67 header("Location:../?accion=ingresar");
70 /**
71 * procLogout - Simply attempts to log the user out of the system
72 * given that there is no logout form to process.
74 function procLogout(){
75 global $session;
76 $retval = $session->logout();
77 header("Location: ../");
80 /**
81 * procRegister - Processes the user submitted registration form,
82 * if errors are found, the user is redirected to correct the
83 * information, if not, the user is effectively registered with
84 * the system and an email is (optionally) sent to the newly
85 * created user.
87 function procRegister(){
88 global $session, $form;
89 /* Convert username to all lowercase (by option) */
90 if(ALL_LOWERCASE){
91 $_POST['user'] = strtolower($_POST['user']);
93 /* Registration attempt */
94 $retval = $session->register($_POST['user'], $_POST['pass'], $_POST['email'],$_POST['nombre'], $_POST['encargado'], $_POST['catedratico'], $_POST['tipo'], $_POST['departamento']);
96 /* Registration Successful */
97 if($retval == 0){
98 $_SESSION['regsuccess'] = true;
99 $_SESSION['reguname'] = $_POST['user'];
101 /* Error found with form */
102 else if($retval == 1){
103 $_SESSION['value_array'] = $_POST;
104 $_SESSION['error_array'] = $form->getErrorArray();
106 /* Registration attempt failed */
107 else if($retval == 2){
108 $_SESSION['reguname'] = $_POST['user'];
109 $_SESSION['regsuccess'] = false;
112 $_SESSION['post-post'] = true;
113 header("Location: ./?accion=registro");
117 * procForgotPass - Validates the given username then if
118 * everything is fine, a new password is generated and
119 * emailed to the address the user gave on sign up.
121 function procForgotPass(){
122 global $database, $session, $mailer, $form;
123 /* Username error checking */
124 $subuser = $_POST['user'];
125 $field = "user"; //Use field name for username
126 if(!$subuser || strlen($subuser = trim($subuser)) == 0){
127 $form->setError($field, "* Username not entered<br>");
129 else{
130 /* Make sure username is in database */
131 $subuser = stripslashes($subuser);
132 if(strlen($subuser) < 5 || strlen($subuser) > 30 ||
133 !eregi("^([0-9a-z])+$", $subuser) ||
134 (!$database->usernameTaken($subuser))){
135 $form->setError($field, "* Username does not exist<br>");
139 /* Errors exist, have user correct them */
140 if($form->num_errors > 0){
141 $_SESSION['value_array'] = $_POST;
142 $_SESSION['error_array'] = $form->getErrorArray();
144 /* Generate new password and email it to user */
145 else{
146 /* Generate new password */
147 $newpass = $session->generateRandStr(8);
149 /* Get email of user */
150 $usrinf = $database->getUserInfo($subuser);
151 $email = $usrinf['email'];
153 /* Attempt to send the email with new password */
154 if($mailer->sendNewPass($subuser,$email,$newpass)){
155 /* Email sent, update database */
156 $database->updateUserField($subuser, "password", md5($newpass));
157 $_SESSION['forgotpass'] = true;
159 /* Email failure, do not change password */
160 else{
161 $_SESSION['forgotpass'] = false;
165 header("Location: ./?x=rpr+clave");
169 * procEditAccount - Attempts to edit the user's account
170 * information, including the password, which must be verified
171 * before a change is made.
173 function procEditAccount(){
174 global $session, $form;
175 /* Account edit attempt */
176 $retval = $session->editAccount($_POST['curpass'], $_POST['newpass'], $_POST['email']);
178 /* Account edit successful */
179 if($retval){
180 $_SESSION['useredit'] = true;
181 header("Location: ".$session->referrer);
183 /* Error found with form */
184 else{
185 $_SESSION['value_array'] = $_POST;
186 $_SESSION['error_array'] = $form->getErrorArray();
187 header("Location: ".$session->referrer);
192 /* Initialize process */
193 $process = new Process;