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