2 /************************************************************************
3 * MXChange v0.2.1 Start: 04/23/2005 *
4 * =============== Last change: 05/18/2008 *
6 * -------------------------------------------------------------------- *
7 * File : sponsor_functions.php *
8 * -------------------------------------------------------------------- *
9 * Short description : Functions for the sponsor area *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Funktionen fuer den Sponsorenbereich *
12 * -------------------------------------------------------------------- *
14 * -------------------------------------------------------------------- *
15 * Copyright (c) 2003 - 2008 by Roland Haeder *
16 * For more information visit: http://www.mxchange.org *
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. *
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. *
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, *
31 ************************************************************************/
33 // Some security stuff...
34 if (!defined('__SECURITY')) {
35 $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4)."/security.php";
40 function SPONSOR_HANDLE_SPONSOR (&$POST, $NO_UPDATE=false, $MSGs=array(), $RET_STATUS=false) {
41 // Init a lot variables
50 'ok', 'edit', 'terms', 'pay_type'
59 // Check if sponsor already exists
60 foreach ($POST as $k => $v) {
61 if (!(array_search($k, $SKIPPED) > -1)) {
62 // Check only posted input entries not the submit button
67 if (!VALIDATE_EMAIL($v)) {
68 // Email address is not valid
71 // Do we want to add a new sponsor or update his data?
72 $result = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_sponsor_data` WHERE email='%s' LIMIT 1",
73 array($POST['email']), __FUNCTION__, __LINE__);
75 // Is a sponsor alread in the db?
76 if (SQL_NUMROWS($result) == 1) {
78 if (($GLOBALS['what'] == "add_sponsor") || ($NO_UPDATE)) {
88 SQL_FREERESULT($result);
97 $k = "password"; $v = md5($v);
101 if (!VALIDATE_URL($v)) $SAVE = false;
105 // Test if there is are time selections
106 CONVERT_SELECTIONS_TO_TIMESTAMP($POST, $DATA, $k, $skip);
110 if ((!empty($k)) && ($skip == false)) {
112 $DATA['keys'][] = $k; $DATA['values'][] = $v;
119 // Default is no force even when a guest want to abuse this force switch
120 if ((empty($POST['force'])) || (!IS_ADMIN())) $POST['force'] = 0;
122 // SQL and message string is empty by default
123 $SQL = ""; $MSG = "";
128 $SQL = "UPDATE `{!_MYSQL_PREFIX!}_sponsor_data` SET ";
129 foreach ($DATA['keys'] as $k => $v) {
130 $SQL .= $v."='%s', ";
133 // Remove last ", " from SQL string
134 $SQL = substr($SQL, 0, -2)." WHERE id='%s' LIMIT 1";
135 $DATA['values'][] = bigintval(REQUEST_GET('id'));
138 $MSG = SPONSOR_GET_MESSAGE(ADMIN_SPONSOR_UPDATED, "updated", $MSGs);
140 } elseif ((!$ALREADY) || (($POST['force'] == "1") && (IS_ADMIN()))) {
141 // Add new sponsor, first add more data
142 $DATA['keys'][] = "sponsor_created"; $DATA['values'][] = time();
143 $DATA['keys'][] = "status";
144 if ((!$NO_UPDATE) && (IS_ADMIN()) && ($GLOBALS['what'] == "add_sponsor")) {
145 // Only allowed for admin
146 $DATA['values'][] = "PENDING";
149 $DATA['values'][] = "UNCONFIRMED";
151 // Generate hash code
152 $DATA['keys'][] = "hash";
153 $DATA['values'][] = md5(session_id().":".$POST['email'].":".GET_REMOTE_ADDR().":".GET_USER_AGENT().":".time());
154 $DATA['keys'][] = "remote_addr";
155 $DATA['values'][] = GET_REMOTE_ADDR();
158 // Implode all data into strings
159 $KEYS = implode(", " , $DATA['keys']);
160 $VALUES = str_repeat("%s', '", count($DATA['values']) - 1);
163 $SQL = "INSERT INTO `{!_MYSQL_PREFIX!}_sponsor_data` (".$KEYS.") VALUES ('".$VALUES."%s')";
166 $MSG = SPONSOR_GET_MESSAGE(getMessage('ADMIN_SPONSOR_ADDED'), "added", $MSGs);
168 } elseif ((!$NO_UPDATE) && (IS_ADMIN())) {
169 // Add all data as hidden data
171 foreach ($POST as $k => $v) {
172 // Do not add 'force' !
174 $OUT .= "<input type=\"hidden\" name=\"".$k."\" value=\"".stripslashes($v)."\" />\n";
177 define('__HIDDEN_DATA', $OUT);
178 define('__EMAIL' , $POST['email']);
180 // Ask for adding a sponsor with same email address
181 LOAD_TEMPLATE("admin_add_sponsor_already");
185 $MSG = sprintf(getMessage('SPONSOR_ALREADY_FOUND', $POST['email']));
191 $result = SQL_QUERY_ESC($SQL, $DATA['values'], __FUNCTION__, __LINE__);
195 if ((!$NO_UPDATE) && (IS_ADMIN())) {
196 LOAD_TEMPLATE("admin_settings_saved", false, $MSG);
200 $MSG = SPONSOR_GET_MESSAGE(getMessage('SPONSOR_DATA_NOT_SAVED'), "failed", $MSGs);
201 LOAD_TEMPLATE("admin_settings_saved", false, $MSG);
204 // Shall we return the status?
205 if ($RET_STATUS) return $ret;
208 function SPONSOR_TRANSLATE_STATUS ($status) {
209 // Construct constant name
210 $constantName = sprintf("ACCOUNT_%s", $status);
212 // Is the constant there?
213 if (defined($constantName)) {
215 $ret = constant($constantName);
218 DEBUG_LOG(__FUNCTION__, __LINE__, sprintf("Unknown status %s detected.", $status));
219 $ret = sprintf(getMessage('UNKNOWN_STATUS'), $status);
223 // Search for an email address in the database
224 function SPONSOR_FOUND_EMAIL_DB ($email) {
225 // Do we already have the provided email address in our DB?
226 $ret = (GET_TOTAL_DATA($email, "sponsor_data", "id", "email", true) == 1);
232 function SPONSOR_GET_MESSAGE ($msg, $pos, $array) {
233 // Check if the requested message was found in array
234 if (isset($array[$pos])) {
235 // ... if yes then use it!
238 // ... else use default message
247 function IS_SPONSOR () {
250 if ((isSessionVariableSet('sponsorid')) && (isSessionVariableSet('sponsorpass'))) {
251 // Check cookies against database records...
252 $result = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_sponsor_data`
253 WHERE id='%s' AND password='%s' AND `status`='CONFIRMED' LIMIT 1",
254 array(bigintval(get_session('sponsorid')), get_session('sponsorpass')), __FUNCTION__, __LINE__);
255 if (SQL_NUMROWS($result) == 1) {
261 SQL_FREERESULT($result);
268 function GENERATE_SPONSOR_MENU($current)
271 $WHERE = " AND active='Y'";
272 if (IS_ADMIN()) $WHERE = "";
274 // Load main menu entries
275 $result_main = SQL_QUERY("SELECT action, title FROM `{!_MYSQL_PREFIX!}_sponsor_menu`
276 WHERE (what='' OR `what` IS NULL) ".$WHERE."
277 ORDER BY `sort`", __FUNCTION__, __LINE__);
278 if (SQL_NUMROWS($result_main) > 0)
280 // Load every menu and it's sub menus
281 while (list($action, $title_main) = SQL_FETCHROW($result_main))
284 $result_sub = SQL_QUERY_ESC("SELECT what, title FROM `{!_MYSQL_PREFIX!}_sponsor_menu`
285 WHERE `action`='%s' AND `what` != '' AND `what` IS NOT NULL ".$WHERE."
286 ORDER BY `sort`", array($action), __FUNCTION__, __LINE__);
287 if (SQL_NUMROWS($result_sub) > 0)
291 while (list($what, $title_sub) = SQL_FETCHROW($result_sub))
293 // Check if current selected menu is matching the loaded one
294 if ($current == $what) $title_sub = "<strong>".$title_sub."</strong>";
296 // Prepare data for the sub template
299 'title' => $title_sub
303 $SUB .= LOAD_TEMPLATE("sponsor_what", true, $content);
306 // Prepare data for the main template
308 'title' => $title_main,
312 // Load menu template
313 $OUT .= LOAD_TEMPLATE("sponsor_action", true, $content);
317 // No sub menus active
318 $OUT .= LOAD_TEMPLATE("admin_settings_saved", true, SPONSOR_NO_SUB_MENUS_ACTIVE);
322 SQL_FREERESULT($result_sub);
327 // No main menus active
328 $OUT .= LOAD_TEMPLATE("admin_settings_saved", true, SPONSOR_NO_MAIN_MENUS_ACTIVE);
332 SQL_FREERESULT($result_main);
338 function GENERATE_SPONSOR_CONTENT($what)
341 $INC = sprintf("inc/modules/sponsor/%s.php", $what);
342 if (INCLUDE_READABLE($INC)) {
343 // Every sponsor action will output nothing directly. It will be written into $OUT!
347 $OUT .= LOAD_TEMPLATE("admin_settings_saved", true, SPONSOR_CONTENT_404_1.$what.SPONSOR_CONTENT_404_2);
354 function UPDATE_SPONSOR_LOGIN () {
360 // Update last online timestamp
361 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_sponsor_data`
362 SET last_online=UNIX_TIMESTAMP()
363 WHERE id='%s' AND password='%s' LIMIT 1",
364 array(bigintval(get_session('sponsorid')), get_session('sponsorpass')), __FUNCTION__, __LINE__);
366 // This update went fine?
367 $login = (SQL_AFFECTEDROWS() == 1);
374 function SPONSOR_SAVE_DATA ($POST, $content) {
377 // Unsecure data which we don't want
378 $UNSAFE = array('password', 'id', 'remote_addr', 'sponsor_created', 'last_online', 'status', 'ref_count',
379 'points_amount', 'points_used', 'refid', 'hash', 'last_pay', 'last_curr', 'pass_old',
380 'ok', 'pass1', 'pass2');
382 // Set default message ("not saved")
383 $MSG = SPONSOR_ACCOUNT_DATA_NOT_SAVED;
385 // Check for submitted passwords
386 if ((!empty($POST['pass1'])) && (!empty($POST['pass2']))) {
387 // Are both passwords the same?
388 if ($POST['pass1'] == $POST['pass2']) {
389 // Okay, then set password and remove pass1 and pass2
390 $POST['password'] = md5($POST['pass1']);
394 // Remove all (maybe spoofed) unsafe data from array
395 foreach ($UNSAFE as $remove) {
396 unset($POST[$remove]);
399 // This array is for the submitted data which we will use with the SQL_QUERY_ESC() function to
403 // Prepare SQL string
404 $SQL = "UPDATE `{!_MYSQL_PREFIX!}_sponsor_data` SET";
405 foreach ($POST as $key => $value) {
406 // Mmmmm, too less security here???
407 $SQL .= " ".strip_tags($key)."='%s',";
409 // We will secure this later inside the SQL_QUERY_ESC() function
410 $DATA[] = strip_tags($value);
412 // Compile {SLASH} and so on for the email templates
413 $POST[$key] = COMPILE_CODE($value);
416 // Check if email has changed
417 if ((!empty($content['email'])) && (!empty($POST['email']))) {
418 if ($content['email'] != $POST['email']) {
419 // Change email address
422 // Okay, has changed then add status with UNCONFIRMED and new hash code
423 $SQL .= " `status`='EMAIL', hash='%s',";
425 // Generate hash code
426 $HASH = md5(session_id().":".$POST['email'].":".GET_REMOTE_ADDR().":".GET_USER_AGENT().":".time());
431 // Remove last commata
432 $SQL = substr($SQL, 0, -1);
435 $SQL .= " WHERE id='%s' AND password='%s' LIMIT 1";
436 $DATA[] = bigintval(get_session('sponsorid'));
437 $DATA[] = get_session('sponsorpass');
439 // Saving data was completed... ufff...
440 switch ($GLOBALS['what'])
442 case "account": // Change account data
443 if ($EMAIL === true) {
444 $MSG = getMessage('SPONSOR_ACCOUNT_EMAIL_CHANGED');
445 $templ = "admin_sponsor_change_email";
446 $subj = getMessage('ADMIN_SPONSOR_ACC_EMAIL_SUBJ');
448 $MSG = getMessage('SPONSOR_ACCOUNT_DATA_SAVED');
449 $templ = "admin_sponsor_change_data";
450 $subj = getMessage('ADMIN_SPONSOR_ACC_DATA_SUBJ');
454 case "settings": // Change settings
455 // Translate some data
456 $content['receive'] = TRANSLATE_YESNO($content['receive_warnings']);
457 $content['interval'] = CREATE_FANCY_TIME($content['warning_interval']);
459 // Set message template and subject for admin
460 $MSG = getMessage('SPONSOR_SETTINGS_SAVED');
461 $templ = "admin_sponsor_settings";
462 $subj = getMessage('ADMIN_SPONSOR_SETTINGS_SUBJ');
465 default: // Unknown sponsor what value!
466 DEBUG_LOG(__FUNCTION__, __LINE__, sprintf("Unknown sponsor module (what) %s detected.", $GLOBALS['what']));
467 $MSG = sprintf(getMessage('SPONSOR_UNKNOWN_WHAT'), $GLOBALS['what']);
468 $templ = ""; $subj = "";
472 if (SQL_AFFECTEDROWS() == 1) {
473 if (!empty($templ) && !empty($subj)) {
474 // Run SQL command and check for success
475 $result = SQL_QUERY_ESC($SQL, $DATA, __FUNCTION__, __LINE__);
477 // Add all data to content
482 if (isset($content['gender'])) $content['gender'] = TRANSLATE_GENDER($content['gender']);
483 if (isset($DATA['gender'])) $DATA['gender'] = TRANSLATE_GENDER($DATA['gender']);
484 if (isset($content['receive_warnings'])) $DATA['receive'] = TRANSLATE_YESNO($POST['receive_warnings']);
485 if (isset($content['warning_interval'])) $DATA['interval'] = CREATE_FANCY_TIME($POST['warning_interval']);
487 // Send email to admins
488 SEND_ADMIN_NOTIFICATION($subj, $templ, $content);
490 // Shall we send mail to the sponsor's new email address?
491 if ($content['receive_warnings'] == "Y") {
492 // Okay send email with confirmation link to new address and with no confirmation link
493 // to the old address
495 // First to old address
496 switch ($GLOBALS['what'])
498 case "account": // Change account data
499 $email_msg = LOAD_EMAIL_TEMPLATE("sponsor_change_data", $content);
500 SEND_EMAIL($content['email'], getMessage('SPONSOR_ACC_DATA_SUBJ'), $email_msg);
502 if ($EMAIL === true) {
503 // Add hash code to content array
504 $content['hash'] = $HASH;
506 // Second mail goes to the new address
507 $email_msg = LOAD_EMAIL_TEMPLATE("sponsor_change_email", $content);
508 SEND_EMAIL($content['email'], getMessage('SPONSOR_ACC_EMAIL_SUBJ'), $email_msg);
512 case "settings": // Change settings
514 $email_msg = LOAD_EMAIL_TEMPLATE("sponsor_settings", $content);
515 SEND_EMAIL($content['email'], getMessage('SPONSOR_SETTINGS_SUBJ'), $email_msg);
522 // Return final message