objectManager: Fix lower-case letter in 'email Text'
[NewAppDB.git] / account.php
blob1ecff469de663841d63528bbc246b1331021f902
1 <?php
2 /**
3 * Account login/logout handler.
5 * Mandatory parameters:
6 * - sCmd, action to perform ("new", "do_new", "login", "do_login", "send_passwd", "logout")
7 *
8 * Optional parameters:
9 * - sUserPassword, new password
10 * - sUserPassword2, new password confirmation
11 * - sUserEmail, e-mail address
12 * - sUserRealname, user's real name
13 * - sWineRelease, user's Wine release
15 * TODO:
16 * - replace sCmd with iAction and replace "new", "login", etc. with integer constants NEW, LOGIN, etc.
17 * - move functions into their respective modules (probably static methods of user class)
20 // application environment
21 require("path.php");
22 require(BASE."include/incl.php");
23 require_once(BASE."include/mail.php");
25 // set http header to not cache
26 header("Pragma: no-cache");
27 header("Cache-control: no-cache");
29 // process command
30 do_account($aClean['sCmd']);
33 /**
34 * process according to $sCmd from URL
36 function do_account($sCmd = null)
38 if (!$sCmd) return 0;
39 switch($sCmd)
41 case "new":
42 apidb_header("New Account");
43 include(BASE."include/"."form_new.php");
44 apidb_footer();
45 exit;
47 case "do_new":
48 cmd_do_new();
49 exit;
51 case "login":
52 apidb_header("Login");
53 include(BASE."include/"."form_login.php");
54 apidb_footer();
55 exit;
57 case "do_login":
58 cmd_do_login();
59 exit;
61 case "send_passwd":
62 cmd_send_passwd();
63 exit;
65 case "logout":
66 /* if we are logged in, log us out */
67 if($_SESSION['current'])
68 $_SESSION['current']->logout();
70 util_redirect_and_exit(apidb_fullurl("index.php"));
72 // not valid command, display error page
73 util_show_error_page_and_exit("Internal Error","This module was called with incorrect parameters");
76 /**
77 * retry
79 function retry($sCmd, $sMsg)
81 addmsg($sMsg, "red");
82 do_account($sCmd);
86 /**
87 * create new account
89 function cmd_do_new()
91 global $aClean;
93 if(!ereg("^.+@.+\\..+$", $aClean['sUserEmail']))
95 $aClean['sUserEmail'] = "";
96 retry("new", "Invalid email address");
97 return;
99 if(empty($aClean['sUserRealname']))
101 retry("new", "You don't have a Real name?");
102 return;
105 $oUser = new User();
106 $sPassword = substr(base_convert(rand(0, PHP_INT_MAX),10, 36), 0, 9);
107 $iResult = $oUser->create($aClean['sUserEmail'], $sPassword,
108 $aClean['sUserRealname'], $aClean['sWineRelease'] );
110 if($iResult == SUCCESS)
112 mail_appdb($oUser->sEmail, "New account", "Your password is ".$sPassword);
113 addmsg("Account created! Check your email for your password. (".$aClean['sUserEmail'].")", "green");
114 util_redirect_and_exit(apidb_fullurl());
116 else if($iResult == USER_CREATE_EXISTS)
118 addmsg("An account with this e-mail exists already.", "red");
119 retry("new", "Failed to create account");
120 } else if($iResult = USER_CREATE_FAILED)
122 addmsg("Error while creating a new user.", "red");
123 retry("new", "Failed to create account");
124 } else
126 addmsg("Unknown failure while creating new user. Please report this problem to appdb admins.", "red");
127 retry("new", "Failed to create account");
133 * email lost password
135 function cmd_send_passwd()
137 global $aClean;
139 /* if the user didn't enter any email address we should */
140 /* ask them to */
141 if($aClean['sUserEmail'] == "")
143 addmsg("Please enter your email address in the 'E-mail' field and re-request a new password",
144 "green");
145 util_redirect_and_exit(apidb_fullurl("account.php?sCmd=login"));
148 $shNote = '(<b>Note</b>: accounts for <b>appdb</b>.winehq.org and <b>bugs</b>.winehq.org '
149 .'are separated, so You might need to <b>create second</b> account for appdb.)';
151 $iUserId = User::exists($aClean['sUserEmail']);
152 $sPasswd = User::generate_passwd();
153 $oUser = new User($iUserId);
154 if ($iUserId)
156 if ($oUser->update_password($sPasswd))
158 $sSubject = "Application DB Lost Password";
159 $sMsg = "We have received a request that you lost your password.\r\n";
160 $sMsg .= "We will create a new password for you. You can then change\r\n";
161 $sMsg .= "your password at the Preferences screen.\r\n";
162 $sMsg .= "Your new password is: ".$sPasswd."\r\n";
165 if (mail_appdb($oUser->sEmail, $sSubject ,$sMsg))
167 addmsg("Your new password has been emailed to you.", "green");
169 else
171 addmsg("Your password has changed, but we could not email it to you. Contact Support (".APPDB_OWNER_EMAIL.") !", "red");
174 else
176 addmsg("Internal Error, we could not update your password.", "red");
179 else
181 addmsg("Sorry, that user (".$aClean['sUserEmail'].") does not exist.<br><br>"
182 .$shNote, "red");
185 util_redirect_and_exit(apidb_fullurl("account.php?sCmd=login"));
189 * on login handler
191 function cmd_do_login()
193 global $aClean;
195 $oUser = new User();
196 $iResult = $oUser->login($aClean['sUserEmail'], $aClean['sUserPassword']);
198 if($iResult == SUCCESS)
200 $sReturnUrl = urldecode($aClean['sReturnTo']);
201 if(!$sReturnUrl)
202 $sReturnUrl = apidb_fullurl("index.php");
203 addmsg("You are successfully logged in as '$oUser->sRealname'.", "green");
204 util_redirect_and_exit($sReturnUrl);
205 } else
207 retry("login","Login failed ".$shNote);