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