More rewrites/fixes (not all is fixed) for ext-sponsor
[mailer.git] / inc / modules / guest / what-sponsor_login.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 06/10/2005 *
4  * ===================                          Last change: 05/18/2008 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : what-sponsor_login.php                           *
8  * -------------------------------------------------------------------- *
9  * Short description : Login form and password resending for sponsor    *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Loginformular und Neues Passwort fuer Sponsor    *
12  * -------------------------------------------------------------------- *
13  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
17  * Needs to be in all Files and every File needs "svn propset           *
18  * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
19  * -------------------------------------------------------------------- *
20  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
21  * Copyright (c) 2009, 2010 by Mailer Developer Team                    *
22  * For more information visit: http://www.mxchange.org                  *
23  *                                                                      *
24  * This program is free software; you can redistribute it and/or modify *
25  * it under the terms of the GNU General Public License as published by *
26  * the Free Software Foundation; either version 2 of the License, or    *
27  * (at your option) any later version.                                  *
28  *                                                                      *
29  * This program is distributed in the hope that it will be useful,      *
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
32  * GNU General Public License for more details.                         *
33  *                                                                      *
34  * You should have received a copy of the GNU General Public License    *
35  * along with this program; if not, write to the Free Software          *
36  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
37  * MA  02110-1301  USA                                                  *
38  ************************************************************************/
39
40 // Some security stuff...
41 if (!defined('__SECURITY')) {
42         die();
43 } // END - if
44
45 // Add description as navigation point
46 addMenuDescription('guest', __FILE__);
47
48 if ((!isExtensionActive('sponsor'))) {
49         loadTemplate('admin_settings_saved', false, generateExtensionInactiveNotInstalledMessage('sponsor'));
50         return;
51 } // END - if
52
53 $mode = '';
54 if (isGetRequestParameterSet('mode')) {
55         // A "special" mode of the login system was requested
56         switch (getRequestParameter('mode')) {
57                 case 'activate' : $mode = 'activate';  break; // Activation link requested
58                 case 'lost_pass': $mode = 'lost_pass'; break; // Request new password
59         } // END - switch
60 } // END - if
61
62 // Check if hash for confirmation of email address is given...
63 if (isGetRequestParameterSet('hash')) {
64         // Lookup sponsor
65         $result = SQL_QUERY_ESC("SELECT
66         `id`, `status`, `gender`, `surname`, `family`,
67         `company`, `position`, `tax_ident`,
68         `street_nr1`, `street_nr2`, `country`, `zip`, `city`, `email`, `phone`, `fax`, `cell`,
69         `points_amount` AS points, `last_pay` AS pay, `last_curr` AS curr
70 FROM
71         `{?_MYSQL_PREFIX?}_sponsor_data`
72 WHERE
73         `hash`='%s' AND (
74                 `status`='UNCONFIRMED' OR
75                 `status`='EMAIL'
76         )
77 LIMIT 1", array(getRequestParameter('hash')), __FILE__, __LINE__);
78         if (SQL_NUMROWS($result) == 1) {
79                 // Sponsor found, load his data...
80                 $data = SQL_FETCHARRAY($result);
81
82                 // Unconfirmed account or changed email address?
83                 if ($data['status'] == 'UNCONFIRMED') {
84                         // Set account to pending
85                         SQL_QUERY_ESC("UPDATE
86         `{?_MYSQL_PREFIX?}_sponsor_data`
87 SET
88         `status`='PENDING',
89         `hash`=NULL
90 WHERE
91         `id`=%s AND
92         `hash`='%s' AND
93         `status`='UNCONFIRMED'
94 LIMIT 1",
95                                 array(
96                                         bigintval($data['id']),
97                                         getRequestParameter('hash')
98                                 ), __FILE__, __LINE__);
99
100                         // Check on success
101                         if (SQL_AFFECTEDROWS() == 1) {
102                                 // Prepare mail and send it to the sponsor
103                                 $message = loadEmailTemplate('sponsor_pending', $data);
104                                 sendEmail($data['email'], '{--SPONSOR_ACCOUNT_PENDING_SUBJECT--}', $message);
105
106                                 // Send email to admin
107                                 sendAdminNotification('{--ADMIN_NEW_SPONSOR--}', 'admin_sponsor_pending', $data);
108
109                                 // Sponsor account set to pending
110                                 loadTemplate('admin_settings_saved', false, '{--SPONSOR_ACCOUNT_IS_PENDING--}');
111                         } else {
112                                 // Could not unlock account!
113                                 loadTemplate('admin_settings_saved', false, '{--SPONSOR_ACCOUNT_PENDING_FAILED--}');
114                         }
115                 } elseif ($data['status'] == 'EMAIL') {
116                         // Changed email adress need to be confirmed
117                         SQL_QUERY_ESC("UPDATE
118         `{?_MYSQL_PREFIX?}_sponsor_data`
119 SET
120         `status`='CONFIRMED',
121         `hash`=NULL
122 WHERE
123         `id`=%s AND
124         `hash`='%s' AND
125         `status`='EMAIL'
126 LIMIT 1",
127                                 array(bigintval($data['id']), getRequestParameter('hash')), __FILE__, __LINE__);
128
129                         // Check on success
130                         if (SQL_AFFECTEDROWS() == 1) {
131                                 // Sponsor account is unlocked again
132                                 loadTemplate('admin_settings_saved', false, '{--SPONSOR_ACCOUNT_IS_CONFIRMED_AGAIN--}');
133                         } else {
134                                 // Could not unlock account!
135                                 loadTemplate('admin_settings_saved', false, '{--SPONSOR_ACCOUNT_EMAIL_FAILED--}');
136                         }
137                 } else {
138                         /// ??? Other status?
139                         loadTemplate('admin_settings_saved', false, '{--SPONSOR_ACCOUNT_STATUS_FAILED--}');
140                 }
141         } else {
142                 // No sponsor found
143                 loadTemplate('admin_settings_saved', false, getMaskedMessage('SPONSOR_ACCOUNT_404', getRequestParameter('hash')));
144         }
145
146         // Free memory
147         SQL_FREERESULT($result);
148 } elseif ($mode == 'activate') {
149         // Send activation link again
150         if (isFormSent()) {
151                 // Check submitted data
152                 if (!isPostRequestParameterSet('email')) unsetPostRequestParameter('ok');
153         }
154
155         if (isFormSent()) {
156                 // Check email
157                 $result = SQL_QUERY_ESC("SELECT id, hash, status, remote_addr, gender, surname, family, sponsor_created
158 FROM `{?_MYSQL_PREFIX?}_sponsor_data`
159 WHERE email='%s' AND (`status`='UNCONFIRMED' OR `status`='EMAIL') LIMIT 1",
160                 array(postRequestParameter('email')), __FILE__, __LINE__);
161
162                 // Entry found?
163                 if (SQL_NUMROWS($result) == 1) {
164                         // Unconfirmed sponsor account found so let's load the requested data
165                         $data = SQL_FETCHARRAY($result);
166
167                         // Translate some data
168                         $data['sponsor_created'] = generateDateTime($data['sponsor_created']);
169
170                         // Prepare email and send it to the sponsor
171                         if ($data['status'] == 'UNCONFIRMED') {
172                                 // Unconfirmed accounts
173                                 $message_sponsor = loadEmailTemplate('sponsor_activate', $data);
174                         } else {
175                                 // Confirmed email address
176                                 $message_sponsor = loadEmailTemplate('sponsor_email', $data);
177                         }
178                         sendEmail(postRequestParameter('email'), '{--SPONSOR_ACTIVATION_LINK_SUBJECT--}', $message_sponsor);
179
180                         // Output message
181                         loadTemplate('admin_settings_saved', false, '{--SPONSOR_ACTIVATION_LINK_SENT--}');
182                 } else {
183                         // No account found or not UNCONFIRMED
184                         loadTemplate('admin_settings_saved', false, '{--SPONSOR_ACTIVATION_LINK_404--}');
185                 }
186
187                 // Free memory
188                 SQL_FREERESULT($result);
189         } else {
190                 // Load form
191                 loadTemplate('guest_sponsor_activate');
192         }
193 } elseif ($mode == 'lost_pass') {
194         // Send new password
195         if (isFormSent()) {
196                 // Check submitted data
197                 if (!isPostRequestParameterSet('email')) unsetPostRequestParameter('ok');
198         } // END - if
199
200         if (isFormSent()) {
201                 // Check email
202                 $result = SQL_QUERY_ESC("SELECT
203         `id`, `hash`, `remote_addr`, `gender`, `surname`, `family`, `sponsor_created`
204 FROM
205         `{?_MYSQL_PREFIX?}_sponsor_data`
206 WHERE
207         `email`='%s' AND
208         `id`=%s AND
209         `status`='CONFIRMED'
210 LIMIT 1",
211                 array(postRequestParameter('email'), bigintval(postRequestParameter('id'))), __FILE__, __LINE__);
212
213                 // Entry found?
214                 if (SQL_NUMROWS($result) == 1) {
215                         // Unconfirmed sponsor account found so let's load the requested data
216                         $DATA = SQL_FETCHARRAY($result);
217
218                         // Translate some data
219                         $DATA['gender']          = translateGender($DATA['gender']);
220                         $DATA['sponsor_created'] = generateDateTime($DATA['sponsor_created']);
221
222                         // Generate password
223                         $DATA['password']        = generatePassword();
224
225                         // Prepare email and send it to the sponsor
226                         $message_sponsor = loadEmailTemplate('sponsor_lost', $DATA);
227                         sendEmail(postRequestParameter('email'), '{--SPONSOR_LOST_PASSWORD_SUBJECT--}', $message_sponsor);
228
229                         // Update password
230                         SQL_QUERY_ESC("UPDATE
231         `{?_MYSQL_PREFIX?}_sponsor_data`
232 SET
233         `password`='%s'
234 WHERE
235         `id`=%s
236 LIMIT 1",
237                                 array(md5($DATA['password']), bigintval($DATA['id'])), __FILE__, __LINE__);
238
239                         // Output message
240                         loadTemplate('admin_settings_saved', false, '{--SPONSOR_LOST_PASSWORD_SENT--}');
241                 } else {
242                         // No account found or not UNCONFIRMED
243                         loadTemplate('admin_settings_saved', false, '{--SPONSOR_LOST_PASSWORD_404--}');
244                 }
245
246                 // Free memory
247                 SQL_FREERESULT($result);
248         } else {
249                 // Load form
250                 loadTemplate('guest_sponsor_lost');
251         }
252 } elseif (isFormSent()) {
253         // Check status and login data ...
254         $result = SQL_QUERY_ESC("SELECT
255         `status`
256 FROM
257         `{?_MYSQL_PREFIX?}_sponsor_data`
258 WHERE
259         `id`=%s AND
260         `password`='%s'
261 LIMIT 1",
262         array(
263                 bigintval(postRequestParameter('sponsor_id')),
264                 md5(postRequestParameter('password'))
265         ), __FILE__, __LINE__);
266
267         if (SQL_NUMROWS($result) == 1) {
268                 // Okay, first login data check passed, now has he/she an approved (CONFIRMED) account?
269                 list($status) = SQL_FETCHROW($result);
270                 if ($status == 'CONFIRMED') {
271                         // Is confirmed so both is fine and we can continue with login procedure
272                         $login = ((setSession('sponsor_id'  , bigintval(postRequestParameter('sponsor_id')))) &&
273                         (setSession('sponsorpass', md5(postRequestParameter('password'))           ))
274                         );
275
276                         if ($login === true) {
277                                 // Cookie setup successfull so we can forward to sponsor area
278                                 redirectToUrl('modules.php?module=sponsor');
279                         } else {
280                                 // Cookie setup failed!
281                                 loadTemplate('admin_settings_saved', false, '{--SPONSPOR_COOKIE_SETUP_FAILED--}');
282
283                                 // Login formular and other links
284                                 loadTemplate('guest_sponsor_login');
285                         }
286                 } else {
287                         // Status is not fine
288                         loadTemplate('admin_settings_saved', false, '{--SPONSOR_LOGIN_FAILED_' . strtoupper($status) . '--}');
289
290                         // Login formular and other links
291                         loadTemplate('guest_sponsor_login');
292                 }
293         } else {
294                 // Account missing or wrong pass! We shall not find this out for the "cracker folks"...
295                 loadTemplate('admin_settings_saved', false, '{--SPONSOR_LOGIN_FAILED_404_WRONG_PASS--}');
296
297                 // Login formular and other links
298                 loadTemplate('guest_sponsor_login');
299         }
300
301         // Free memory
302         SQL_FREERESULT($result);
303 } else {
304         // Login formular and other links
305         loadTemplate('guest_sponsor_login');
306 }
307
308 // [EOF]
309 ?>