54fa6cd0d43dc2eb275fcd74bf04f0572b2f9db3
[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 (`status`='UNCONFIRMED' OR `status`='EMAIL')
74 LIMIT 1", array(getRequestParameter('hash')), __FILE__, __LINE__);
75         if (SQL_NUMROWS($result) == 1) {
76                 // Sponsor found, load his data...
77                 $data = SQL_FETCHARRAY($result);
78
79                 // Unconfirmed account or changed email address?
80                 if ($data['status'] == 'UNCONFIRMED') {
81                         // Set account to pending
82                         SQL_QUERY_ESC("UPDATE
83         `{?_MYSQL_PREFIX?}_sponsor_data`
84 SET
85         `status`='PENDING'
86 WHERE
87         `id`=%s AND
88         hash='%s' AND
89         `status`='UNCONFIRMED'
90 LIMIT 1",
91                                 array(
92                                         bigintval($data['id']),
93                                         getRequestParameter('hash')
94                                 ), __FILE__, __LINE__);
95
96                         // Check on success
97                         if (SQL_AFFECTEDROWS() == 1) {
98                                 // Prepare mail and send it to the sponsor
99                                 $message = loadEmailTemplate('sponsor_pending', $data);
100                                 sendEmail($data['email'], '{--SPONSOR_ACCOUNT_PENDING_SUBJECT--}', $message);
101
102                                 // Send email to admin
103                                 sendAdminNotification('{--ADMIN_NEW_SPONSOR--}', 'admin_sponsor_pending', $data);
104
105                                 // Sponsor account set to pending
106                                 loadTemplate('admin_settings_saved', false, '{--SPONSOR_ACCOUNT_IS_PENDING--}');
107                         } else {
108                                 // Could not unlock account!
109                                 loadTemplate('admin_settings_saved', false, '{--SPONSOR_ACCOUNT_PENDING_FAILED--}');
110                         }
111                 } elseif ($data['status'] == 'EMAIL') {
112                         // Changed email adress need to be confirmed
113                         SQL_QUERY_ESC("UPDATE
114         `{?_MYSQL_PREFIX?}_sponsor_data`
115 SET
116         `status`='CONFIRMED'
117 WHERE
118         `id`=%s AND
119         `hash`='%s' AND
120         `status`='EMAIL'
121 LIMIT 1",
122                                 array(bigintval($data['id']), getRequestParameter('hash')), __FILE__, __LINE__);
123
124                         // Check on success
125                         if (SQL_AFFECTEDROWS() == 1) {
126                                 // Sponsor account is unlocked again
127                                 loadTemplate('admin_settings_saved', false, '{--SPONSOR_ACCOUNT_IS_CONFIRMED_AGAIN--}');
128                         } else {
129                                 // Could not unlock account!
130                                 loadTemplate('admin_settings_saved', false, '{--SPONSOR_ACCOUNT_EMAIL_FAILED--}');
131                         }
132                 } else {
133                         /// ??? Other status?
134                         loadTemplate('admin_settings_saved', false, '{--SPONSOR_ACCOUNT_STATUS_FAILED--}');
135                 }
136         } else {
137                 // No sponsor found
138                 loadTemplate('admin_settings_saved', false, getMaskedMessage('SPONSOR_ACCOUNT_404', getRequestParameter('hash')));
139         }
140
141         // Free memory
142         SQL_FREERESULT($result);
143 } elseif ($mode == 'activate') {
144         // Send activation link again
145         if (isFormSent()) {
146                 // Check submitted data
147                 if (!isPostRequestParameterSet('email')) unsetPostRequestParameter('ok');
148         }
149
150         if (isFormSent()) {
151                 // Check email
152                 $result = SQL_QUERY_ESC("SELECT id, hash, status, remote_addr, gender, surname, family, sponsor_created
153 FROM `{?_MYSQL_PREFIX?}_sponsor_data`
154 WHERE email='%s' AND (`status`='UNCONFIRMED' OR `status`='EMAIL') LIMIT 1",
155                 array(postRequestParameter('email')), __FILE__, __LINE__);
156
157                 // Entry found?
158                 if (SQL_NUMROWS($result) == 1) {
159                         // Unconfirmed sponsor account found so let's load the requested data
160                         $data = SQL_FETCHARRAY($result);
161
162                         // Translate some data
163                         $data['sponsor_created'] = generateDateTime($data['sponsor_created']);
164
165                         // Prepare email and send it to the sponsor
166                         if ($data['status'] == 'UNCONFIRMED') {
167                                 // Unconfirmed accounts
168                                 $message_sponsor = loadEmailTemplate('sponsor_activate', $data);
169                         } else {
170                                 // Confirmed email address
171                                 $message_sponsor = loadEmailTemplate('sponsor_email', $data);
172                         }
173                         sendEmail(postRequestParameter('email'), '{--SPONSOR_ACTIVATION_LINK_SUBJECT--}', $message_sponsor);
174
175                         // Output message
176                         loadTemplate('admin_settings_saved', false, '{--SPONSOR_ACTIVATION_LINK_SENT--}');
177                 } else {
178                         // No account found or not UNCONFIRMED
179                         loadTemplate('admin_settings_saved', false, '{--SPONSOR_ACTIVATION_LINK_404--}');
180                 }
181
182                 // Free memory
183                 SQL_FREERESULT($result);
184         } else {
185                 // Load form
186                 loadTemplate('guest_sponsor_activate');
187         }
188 } elseif ($mode == 'lost_pass') {
189         // Send new password
190         if (isFormSent()) {
191                 // Check submitted data
192                 if (!isPostRequestParameterSet('email')) unsetPostRequestParameter('ok');
193         } // END - if
194
195         if (isFormSent()) {
196                 // Check email
197                 $result = SQL_QUERY_ESC("SELECT
198         `id`, `hash`, `remote_addr`, `gender`, `surname`, `family`, `sponsor_created`
199 FROM
200         `{?_MYSQL_PREFIX?}_sponsor_data`
201 WHERE
202         `email`='%s' AND
203         `id`=%s AND
204         `status`='CONFIRMED'
205 LIMIT 1",
206                 array(postRequestParameter('email'), bigintval(postRequestParameter('id'))), __FILE__, __LINE__);
207
208                 // Entry found?
209                 if (SQL_NUMROWS($result) == 1) {
210                         // Unconfirmed sponsor account found so let's load the requested data
211                         $DATA = SQL_FETCHARRAY($result);
212
213                         // Translate some data
214                         $DATA['gender']          = translateGender($DATA['gender']);
215                         $DATA['sponsor_created'] = generateDateTime($DATA['sponsor_created']);
216
217                         // Generate password
218                         $DATA['password']        = generatePassword();
219
220                         // Prepare email and send it to the sponsor
221                         $message_sponsor = loadEmailTemplate('sponsor_lost', $DATA);
222                         sendEmail(postRequestParameter('email'), '{--SPONSOR_LOST_PASSWORD_SUBJECT--}', $message_sponsor);
223
224                         // Update password
225                         SQL_QUERY_ESC("UPDATE
226         `{?_MYSQL_PREFIX?}_sponsor_data`
227 SET
228         `password`='%s'
229 WHERE
230         `id`=%s
231 LIMIT 1",
232                                 array(md5($DATA['password']), bigintval($DATA['id'])), __FILE__, __LINE__);
233
234                         // Output message
235                         loadTemplate('admin_settings_saved', false, '{--SPONSOR_LOST_PASSWORD_SENT--}');
236                 } else {
237                         // No account found or not UNCONFIRMED
238                         loadTemplate('admin_settings_saved', false, '{--SPONSOR_LOST_PASSWORD_404--}');
239                 }
240
241                 // Free memory
242                 SQL_FREERESULT($result);
243         } else {
244                 // Load form
245                 loadTemplate('guest_sponsor_lost');
246         }
247 } elseif (isFormSent()) {
248         // Check status and login data ...
249         $result = SQL_QUERY_ESC("SELECT
250         `status`
251 FROM
252         `{?_MYSQL_PREFIX?}_sponsor_data`
253 WHERE
254         `id`=%s AND
255         `password`='%s'
256 LIMIT 1",
257         array(
258                 bigintval(postRequestParameter('sponsor_id')),
259                 md5(postRequestParameter('password'))
260         ), __FILE__, __LINE__);
261
262         if (SQL_NUMROWS($result) == 1) {
263                 // Okay, first login data check passed, now has he/she an approved (CONFIRMED) account?
264                 list($status) = SQL_FETCHROW($result);
265                 if ($status == 'CONFIRMED') {
266                         // Is confirmed so both is fine and we can continue with login procedure
267                         $login = ((setSession('sponsor_id'  , bigintval(postRequestParameter('sponsor_id')))) &&
268                         (setSession('sponsorpass', md5(postRequestParameter('password'))           ))
269                         );
270
271                         if ($login === true) {
272                                 // Cookie setup successfull so we can forward to sponsor area
273                                 redirectToUrl('modules.php?module=sponsor');
274                         } else {
275                                 // Cookie setup failed!
276                                 loadTemplate('admin_settings_saved', false, '{--SPONSPOR_COOKIE_SETUP_FAILED--}');
277
278                                 // Login formular and other links
279                                 loadTemplate('guest_sponsor_login');
280                         }
281                 } else {
282                         // Status is not fine
283                         loadTemplate('admin_settings_saved', false, '{--SPONSOR_LOGIN_FAILED_' . strtoupper($status) . '--}');
284
285                         // Login formular and other links
286                         loadTemplate('guest_sponsor_login');
287                 }
288         } else {
289                 // Account missing or wrong pass! We shall not find this out for the "cracker folks"...
290                 loadTemplate('admin_settings_saved', false, '{--SPONSOR_LOGIN_FAILED_404_WRONG_PASS--}');
291
292                 // Login formular and other links
293                 loadTemplate('guest_sponsor_login');
294         }
295
296         // Free memory
297         SQL_FREERESULT($result);
298 } else {
299         // Login formular and other links
300         loadTemplate('guest_sponsor_login');
301 }
302
303 // [EOF]
304 ?>