Initial revision
[lhc/web/wiklou.git] / includes / SpecialUserlogin.php
1 <?
2
3 function wfSpecialUserlogin()
4 {
5 global $wpCreateaccount, $wpLoginattempt, $wpMailmypassword;
6 global $action;
7
8 $fields = array( "wpName", "wpPassword", "wpName",
9 "wpPassword", "wpRetype", "wpEmail" );
10 wfCleanFormFields( $fields );
11
12 if ( isset( $wpCreateaccount ) ) {
13 addNewAccount();
14 } else if ( isset( $wpMailmypassword ) ) {
15 mailPassword();
16 } else if ( "submit" == $action || isset( $wpLoginattempt ) ) {
17 processLogin();
18 } else {
19 mainLoginForm( "" );
20 }
21 }
22
23 /* private */ function addNewAccount()
24 {
25 global $wgUser, $wgOut, $wpPassword, $wpRetype, $wpName, $wpRemember;
26 global $wpEmail, $wgDeferredUpdateList;
27
28 if ( 0 != strcmp( $wpPassword, $wpRetype ) ) {
29 mainLoginForm( wfMsg( "badretype" ) );
30 return;
31 }
32 $wpName = trim( $wpName );
33 if ( ( "" == $wpName ) ||
34 preg_match( "/^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$/", $wpName ) ) {
35 mainLoginForm( wfMsg( "noname" ) );
36 return;
37 }
38 if ( wfReadOnly() ) {
39 $wgOut->readOnlyPage();
40 return;
41 }
42 $u = User::newFromName( $wpName );
43
44 if ( 0 != $u->idForName() ) {
45 mainLoginForm( wfMsg( "userexists" ) );
46 return;
47 }
48 $u->addToDatabase();
49 $u->setPassword( $wpPassword );
50 $u->setEmail( $wpEmail );
51 if ( 1 == $wpRemember ) { $r = 1; }
52 else { $r = 0; }
53 $u->setOption( "rememberpassword", $r );
54
55 $wgUser = $u;
56 $m = str_replace( "$1", $wgUser->getName(), wfMsg( "welcomecreation" ) );
57 successfulLogin( $m );
58 }
59
60 /* private */ function processLogin()
61 {
62 global $wgUser, $wpName, $wpPassword, $wpRemember;
63 global $returnto;
64
65 if ( "" == $wpName ) {
66 mainLoginForm( wfMsg( "noname" ) );
67 return;
68 }
69 $u = User::newFromName( $wpName );
70 $id = $u->idForName();
71 if ( 0 == $id ) {
72 $m = str_replace( "$1", $u->getName(), wfMsg( "nosuchuser" ) );
73 mainLoginForm( $m );
74 return;
75 }
76 $u->setId( $id );
77 $u->loadFromDatabase();
78 $ep = User::encryptPassword( $wpPassword );
79 if ( 0 != strcmp( $ep, $u->getPassword() ) ) {
80 if ( 0 != strcmp( $ep, $u->getNewpassword() ) ) {
81 mainLoginForm( wfMsg( "wrongpassword" ) );
82 return;
83 }
84 }
85 # We've verified now, update the real record
86 #
87 if ( 1 == $wpRemember ) { $r = 1; }
88 else { $r = 0; }
89 $u->setOption( "rememberpassword", $r );
90
91 $wgUser = $u;
92 $m = str_replace( "$1", $wgUser->getName(), wfMsg( "loginsuccess" ) );
93 successfulLogin( $m );
94 }
95
96 /* private */ function mailPassword()
97 {
98 global $wgUser, $wpName, $wgDeferredUpdateList, $wgOutputEncoding;
99
100 if ( "" == $wpName ) {
101 mainLoginForm( wfMsg( "noname" ) );
102 return;
103 }
104 $u = User::newFromName( $wpName );
105 $id = $u->idForName();
106 if ( 0 == $id ) {
107 $m = str_replace( "$1", $u->getName(), wfMsg( "nosuchuser" ) );
108 mainLoginForm( $m );
109 return;
110 }
111 $u->setId( $id );
112 $u->loadFromDatabase();
113
114 if ( "" == $u->getEmail() ) {
115 $m = str_replace( "$1", $u->getName(), wfMsg( "noemail" ) );
116 mainLoginForm( $m );
117 return;
118 }
119 $np = User::randomPassword();
120 $u->setNewpassword( $np );
121 setcookie( "wcUserPassword", "", time() - 3600 );
122 $u->saveSettings();
123
124 $ip = getenv( "REMOTE_ADDR" );
125 if ( "" == $ip ) { $ip = "(Unknown)"; }
126
127 $m = str_replace( "$1", $ip, wfMsg( "passwordremindertext" ) );
128 $m = str_replace( "$2", $u->getName(), $m );
129 $m = str_replace( "$3", $np, $m );
130
131 #FIXME: Generilize the email addresses for 3rd party sites...
132 mail( $u->getEmail(), wfMsg( "passwordremindertitle" ), $m,
133 "MIME-Version: 1.0\r\n" .
134 "Content-type: text/plain; charset={$wgOutputEncoding}\r\n" .
135 "Content-transfer-encoding: 8bit\r\n" .
136 "From: Wikipedia Mail <apache@www.wikipedia.org>\r\n" .
137 "Reply-To: webmaster@www.wikipedia.org" );
138 $m = str_replace( "$1", $u->getName(), wfMsg( "passwordsent" ) );
139 mainLoginForm( $m );
140 }
141
142 /* private */ function successfulLogin( $msg )
143 {
144 global $wgUser, $wgOut, $returnto;
145 global $wgDeferredUpdateList;
146
147 $wgUser->setCookies();
148 $up = new UserUpdate();
149 array_push( $wgDeferredUpdateList, $up );
150
151 $wgOut->setPageTitle( wfMsg( "loginsuccesstitle" ) );
152 $wgOut->setRobotpolicy( "noindex,nofollow" );
153 $wgOut->setArticleFlag( false );
154 $wgOut->addHTML( $msg . "\n<p>" );
155 $wgOut->returnToMain();
156 }
157
158 /* private */ function mainLoginForm( $err )
159 {
160 global $wgUser, $wgOut, $wgLang, $returnto;
161 global $wpName, $wpPassword, $wpRetype, $wpRemember;
162 global $wpEmail, $HTTP_COOKIE_VARS;
163
164 $le = wfMsg( "loginerror" );
165 $yn = wfMsg( "yourname" );
166 $yp = wfMsg( "yourpassword" );
167 $ypa = wfMsg( "yourpasswordagain" );
168 $rmp = wfMsg( "remembermypassword" );
169 $ayn = wfMsg( "areyounew" );
170 $nuo = wfMsg( "newusersonly" );
171 $li = wfMsg( "login" );
172 $ca = wfMsg( "createaccount" );
173 $ye = wfMsg( "youremail" );
174 $efl = wfMsg( "emailforlost" );
175 $mmp = wfMsg( "mailmypassword" );
176
177 $name = $wpName;
178 if ( "" == $name ) {
179 if ( 0 != $wgUser->getID() ) {
180 $name = $wgUser->getName();
181 } else {
182 $name = $HTTP_COOKIE_VARS["wcUserName"];
183 }
184 }
185 $pwd = $wpPassword;
186
187 $wgOut->setPageTitle( wfMsg( "userlogin" ) );
188 $wgOut->setRobotpolicy( "noindex,nofollow" );
189 $wgOut->setArticleFlag( false );
190
191 if ( "" == $err ) {
192 $wgOut->addHTML( "<h2>$li:</h2>\n" );
193 } else {
194 $wgOut->addHTML( "<h2>$le:</h2>\n<font size='+1' color='red'>$err</font>\n" );
195 }
196 if ( 1 == $wgUser->getOption( "rememberpassword" ) ) {
197 $checked = " checked";
198 } else {
199 $checked = "";
200 }
201 $q = "action=submit";
202 if ( "" != $returnto ) { $q .= "&returnto=" . wfUrlencode($returnto); }
203 $action = wfLocalUrlE( $wgLang->specialPage( "Userlogin" ), $q );
204
205 $wpName = wfEscapeHTML( $wpName );
206 $wpPassword = wfEscapeHTML( $wpPassword );
207 $wpRetype = wfEscapeHTML( $wpRetype );
208 $wpEmail = wfEscapeHTML( $wpEmail );
209
210 $wgOut->addHTML( "
211 <form id=\"userlogin\" method=\"post\" action=\"{$action}\">
212 <table border=0><tr>
213 <td align=right>$yn:</td>
214 <td colspan=2 align=left>
215 <input tabindex=1 type=text name=\"wpName\" value=\"{$name}\" size=20>
216 </td></tr><tr>
217 <td align=right>$yp:</td>
218 <td align=left>
219 <input tabindex=2 type=password name=\"wpPassword\" value=\"{$pwd}\" size=20>
220 </td>
221 <td align=left>
222 <input tabindex=3 type=submit name=\"wpLoginattempt\" value=\"{$li}\">
223 </td></tr>
224 <tr><td colspan=3>&nbsp;</td></tr><tr>
225 <td align=right>$ypa:</td>
226 <td align=left>
227 <input tabindex=4 type=password name=\"wpRetype\" value=\"{$wpRetype}\" size=20>
228 </td><td>$nuo</td></tr>
229 <tr>
230 <td align=right>$ye:</td>
231 <td align=left>
232 <input tabindex=5 type=text name=\"wpEmail\" value=\"{$wpEmail}\" size=20>
233 </td><td align=left>
234 <input tabindex=6 type=submit name=\"wpCreateaccount\" value=\"{$ca}\">
235 </td></tr>
236 <tr>
237 <td colspan=3 align=left>
238 <input tabindex=7 type=checkbox name=\"wpRemember\" value=\"1\"$checked>$rmp
239 </td></tr>
240 <tr><td colspan=3>&nbsp;</td></tr><tr>
241 <td colspan=3 align=left>
242 <p>$efl<br>
243 <input tabindex=8 type=submit name=\"wpMailmypassword\" value=\"{$mmp}\">
244 </td></tr></table>
245 </form>\n" );
246 }
247
248 ?>