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