Rewrote some parts:
[mailer.git] / doubler.php
index 4bbc6581b02d46f7d2319a8834136b641185606e..46f16f9122829361268eb60b0b08369c2629307e 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /************************************************************************
- * MXChange v0.2.1                                    Start: 02/13/2005 *
- * ===============                              Last change: 02/13/2005 *
+ * Mailer v0.2.1-FINAL                                Start: 02/13/2005 *
+ * ===================                          Last change: 02/13/2005 *
  *                                                                      *
  * -------------------------------------------------------------------- *
  * File              : doubler.php                                      *
  * -------------------------------------------------------------------- *
  * Kurzbeschreibung  : Punkteverdoppler                                 *
  * -------------------------------------------------------------------- *
- *                                                                      *
+ * $Revision::                                                        $ *
+ * $Date::                                                            $ *
+ * $Tag:: 0.2.1-FINAL                                                 $ *
+ * $Author::                                                          $ *
  * -------------------------------------------------------------------- *
- * Copyright (c) 2003 - 2008 by Roland Haeder                           *
- * For more information visit: http://www.mxchange.org                  *
+ * Copyright (c) 2003 - 2009 by Roland Haeder                           *
+ * Copyright (c) 2009 - 2013 by Mailer Developer Team                   *
+ * For more information visit: http://mxchange.org                      *
  *                                                                      *
  * This program is free software; you can redistribute it and/or modify *
  * it under the terms of the GNU General Public License as published by *
  * MA  02110-1301  USA                                                  *
  ************************************************************************/
 
-// Load security stuff here (Oh, I hope this is not unsecure? Am I paranoia??? ;-) )
-require_once("inc/libs/security_functions.php");
+// Load security stuff here
+require('inc/libs/security_functions.php');
 
-// Init "action" and "what"
-global $what, $action, $startTime;
-$GLOBALS['startTime'] = microtime(true);
-$GLOBALS['what'] = "";
-$GLOBALS['action'] = "";
+// Init start time
+$GLOBALS['__start_time'] = microtime(TRUE);
 
-// Set module
-$GLOBALS['module'] = "doubler";
-$GLOBALS['refid'] = 0;
-$CSS = 0;
+// Set module and output mode
+$GLOBALS['__module']      = 'doubler';
+$GLOBALS['__output_mode'] = '0';
 
 // Load the required file(s)
-require("inc/config.php");
-
-// Is the "doubler" extension active?
-if (!EXT_IS_ACTIVE("doubler")) {
-       // Redirect to index
-       LOAD_URL("modules.php?module=index&amp;msg=".CODE_EXTENSION_PROBLEM."&amp;ext=doubler");
-} // END - if
-
-// Is the script installed?
-if (isBooleanConstantAndTrue('mxchange_installed')) {
-       // Probe for referal ID
-       if (!empty($_GET['refid'])) $GLOBALS['refid'] = SQL_ESCAPE($_GET['refid']);
-
-       // Probe for nickname extension and if a nickname was supplied by URL
-       $probe_nickname = ((EXT_IS_ACTIVE("nickname")) && (("".round($GLOBALS['refid'])."") != $GLOBALS['refid']));
-       if ($probe_nickname) {
-               // Nickname in URL, so load the ID
-               $result = SQL_QUERY_ESC("SELECT userid, status FROM "._MYSQL_PREFIX."_user_data WHERE nickname='%s' LIMIT 1",
-                array(bigintval($GLOBALS['refid'])), __FILE__, __LINE__);
-       } else {
-               // Direct userid entered
-               $result = SQL_QUERY_ESC("SELECT userid, status FROM "._MYSQL_PREFIX."_user_data WHERE userid=%s LIMIT 1",
-                array(bigintval($GLOBALS['refid'])), __FILE__, __LINE__);
-       }
-
-       // Load data
-       list($rid, $status_ref) = SQL_FETCHROW($result);
-       $GLOBALS['refid'] = bigintval($rid);
-
-       // Free memory
-       SQL_FREERESULT($result);
-       $uid = 0;
-
-       // If no account was found set default refid and status to CONFIRMED
-       if (empty($GLOBALS['refid'])) {
-               $GLOBALS['refid'] = $_CONFIG['def_refid'];
-               $status = "CONFIRMED";
-       } // END - if
-
-       // Begin with doubler script...
-       if (isset($_POST['ok'])) {
-               // Secure points (so only integer/double values are allowed
-               $_POST['points'] = bigintval($_POST['points']);
-
-               // Begin with doubling process
-               if ((!empty($_POST['userid'])) && (!empty($_POST['pass'])) && (!empty($_POST['points']))) {
-                       // Probe for nickname extension and if a nickname was entered
-                       $probe_nickname = ((EXT_IS_ACTIVE("nickname")) && (("".round($_POST['userid'])."") != $_POST['userid']));
-                       if ($probe_nickname) {
-                               // Nickname in URL, so load the ID
-                               $result = SQL_QUERY_ESC("SELECT userid, status, password FROM "._MYSQL_PREFIX."_user_data WHERE nickname='%s' LIMIT 1",
-                                array($_POST['userid']), __FILE__, __LINE__);
-                       } else {
-                               // Direct userid entered
-                               $result = SQL_QUERY_ESC("SELECT userid, status, password FROM "._MYSQL_PREFIX."_user_data WHERE userid=%s LIMIT 1",
-                                array(bigintval($_POST['userid'])), __FILE__, __LINE__);
-                       }
-
-                       // Load data
-                       list($uid, $status, $password) = SQL_FETCHROW($result);
-                       $uid = bigintval($uid);
-
-                       // Free result
-                       SQL_FREERESULT($result);
-
-                       // Remove any dots and unwanted chars from the points
-                       $_POST['points'] = bigintval(round(REVERT_COMMA($_POST['points'])));
-
-                       // Probe for enough points
-                       $probe_points = (($_POST['points'] >= $_CONFIG['doubler_min']) && ($_POST['points'] <= $_CONFIG['doubler_max']));
-
-                       // Check all together
-                       if ((!empty($uid)) && ($password == generateHash($_POST['pass'], substr($password, 0, -40))) && ($status == "CONFIRMED") && ($probe_points)) {
-                               // Nickname resolved to a unique userid or direct userid entered by the member
-                               $DOUBLER_UID = $uid;
-
-                               // Calulcate points
-                               $points = GET_TOTAL_DATA($uid, "user_points", "points") - GET_TOTAL_DATA($uid, "user_data", "used_points");
-
-                               // So let's continue with probing his points amount
-                               if (($points - $_CONFIG['doubler_left'] - $_POST['points'] * $_CONFIG['doubler_charge']) >= 0)
-                               {
-                                       // Enough points are left so let's continue with the doubling process
-                                       // Create doubling "account" width *DOUBLED* points
-                                       $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_doubler (userid, refid, points, remote_ip, timemark, completed, is_ref) VALUES ('%s','%s','%s','".GET_REMOTE_ADDR()."', UNIX_TIMESTAMP(), 'N','N')",
-                                        array($uid, bigintval($GLOBALS['refid']), bigintval($_POST['points'] * 2)), __FILE__, __LINE__);
-
-                                       // Subtract entered points
-                                       SUB_POINTS("doubler", $uid, $_POST['points']);
-
-                                       // Add points to "total payed" including charge
-                                       $points = $_POST['points'] - $_POST['points'] * $_CONFIG['doubler_charge'];
-                                       UPDATE_CONFIG("doubler_points", $points, "+");
-                                       $_CONFIG['doubler_points'] += $points;
-
-                                       // Add second line for the referal but only when uid != refid
-                                       if (($GLOBALS['refid'] > 0) && ($GLOBALS['refid'] != $uid)) {
-                                               // Okay add a refid line and apply refid percents
-                                               $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_doubler (userid,refid,points,remote_ip,timemark,completed,is_ref) VALUES ('%s',0,'%s','".GET_REMOTE_ADDR()."',UNIX_TIMESTAMP(),'N','Y')",
-                                                       array(bigintval($GLOBALS['refid']), bigintval($_POST['points'] * 2 * $_CONFIG['doubler_ref'])), __FILE__, __LINE__);
-
-                                               // And that's why we dont't want to you more than one referal level of doubler-points. ^^^
-                                       } // END - if
-
-                                       // Update usage counter
-                                       UPDATE_CONFIG("doubler_counter", 1, "+");
-                                       $_CONFIG['doubler_counter']++;
+require('inc/config-global.php');
+
+// Set content type
+setContentType('text/html');
+
+// Is the 'doubler' extension active?
+redirectOnUninstalledExtension('doubler');
+
+// Init content array
+$content = array(
+       'message' => '',
+);
+
+// Begin with doubler script...
+if (isFormSent()) {
+       // Secure points (so only integer/double values are allowed
+       setPostRequestElement('points', bigintval(postRequestElement('points')));
+
+       // Begin with doubling process
+       if ((isPostRequestElementSet('userid')) && (isPostRequestElementSet('password')) && (isPostRequestElementSet('points'))) {
+               // Probe for nickname extension and if a nickname was entered
+               if (isNicknameUsed(postRequestElement('userid'))) {
+                       // Nickname in URL, so load the id
+                       fetchUserData(postRequestElement('userid'), 'nickname');
+               } else {
+                       // Direct userid entered
+                       fetchUserData(postRequestElement('userid'));
+               }
 
-                                       // Set constant
-                                       define('__DOUBLER_MSG', LOAD_TEMPLATE("doubler_reflink", true, $_POST['userid']));
-                               } else {
-                                       // Not enougth points left
-                                       define('__ERROR_MSG', DOUBLER_FORM_NO_POINTS_LEFT);
-                               }
-                       } elseif ($status == "CONFIRMED") {
-                               // Account is unconfirmed!
-                               define('__ERROR_MSG', DOUBLER_FORM_WRONG_PASS);
-                       } elseif ($status == "UNCONFIRMED") {
-                               // Account is unconfirmed!
-                               define('__ERROR_MSG', DOUBLER_FORM_STATUS_UNCONFIRMED);
-                       } elseif ($status == "LOCKED") {
-                               // Account is locked by admin / holiday!
-                               define('__ERROR_MSG', DOUBLER_FORM_STATUS_LOCKED);
-                       } elseif ($_POST['points'] < $_CONFIG['doubler_min']) {
-                               // Not enougth points entered
-                               define('__ERROR_MSG', DOUBLER_FORM_POINTS_MIN);
-                       } elseif ($_POST['points'] > $_CONFIG['doubler_max']) {
-                               // Too much points entered
-                               define('__ERROR_MSG', DOUBLER_FORM_POINTS_MAX);
-                       } elseif ($probe_nickname) {
-                               // Cannot resolv nickname -> userid
-                               define('__ERROR_MSG', DOUBLER_FORM_404_NICKNAME);
+               // Is the data valid?
+               if (!isValidUserData()) {
+                       // Output message that the userid is not okay
+                       displayMessage('{--DOUBLER_USERID_INVALID--}');
+               } // END - if
+
+               // Remove any dots and unwanted chars from the points
+               setPostRequestElement('points', bigintval(round(convertCommaToDot(postRequestElement('points')))));
+
+               // Probe for enough points
+               $probe_points = ((postRequestElement('points') >= getDoublerMin()) && (postRequestElement('points') <= getDoublerMax()));
+
+               // Check all together
+               if ((isValidUserData()) && (getUserData('password') == generateHash(postRequestElement('password'), substr(getUserData('password'), 0, -40))) && (getUserData('status') == 'CONFIRMED') && ($probe_points)) {
+                       // Nickname resolved to a unique userid or direct userid entered by the member
+                       $GLOBALS['local_doubler_userid'] = getUserData('userid');
+
+                       // Calulcate points
+                       $points = getTotalPoints(getUserData('userid'));
+
+                       // So let's continue with probing his points amount
+                       if (($points - getConfig('doubler_left') - postRequestElement('points') * getDoublerCharge() / 100) >= 0) {
+                               // Enough points are left so let's continue with the doubling process
+                               // Create doubling "account" with *DOUBLED* points
+                               sqlQueryEscaped("INSERT INTO `{?_MYSQL_PREFIX?}_doubler` (`userid`, `refid`, `points`, `remote_ip`, `timemark`, `completed`, `is_ref`) VALUES (%s,%s,%s,'%s', UNIX_TIMESTAMP(), 'N','N')",
+                                       array(
+                                               getUserData('userid'),
+                                               convertZeroToNull(determineReferralId()),
+                                               bigintval(postRequestElement('points') * 2),
+                                               determineRealRemoteAddress()
+                                       ), __FILE__, __LINE__);
+
+                               // Subtract entered points and ignore return status
+                               subtractPoints('doubler', getUserData('userid'), postRequestElement('points'));
+
+                               // Add points to "total payed" including charge
+                               $points = postRequestElement('points') - postRequestElement('points') * getDoublerCharge() / 100;
+                               updateConfiguration('doubler_points', $points, '+');
+                               incrementConfigEntry('doubler_points', $points);
+
+                               // Add second line for the referral but only when userid != refid
+                               if ((isValidId(determineReferralId())) && (determineReferralId() != getUserData('userid'))) {
+                                       // Okay add a refid line and apply refid percents
+                                       sqlQueryEscaped("INSERT INTO `{?_MYSQL_PREFIX?}_doubler` (`userid`, `refid`, `points`, `remote_ip`, `timemark`, `completed`, `is_ref`) VALUES (%s,0,%s,'%s',UNIX_TIMESTAMP(),'N','Y')",
+                                               array(
+                                                       convertZeroToNull(determineReferralId()),
+                                                       (postRequestElement('points') * 2 * getDoublerRef() / 100),
+                                                       determineRealRemoteAddress()
+                                               ), __FILE__, __LINE__);
+
+                                       // And that's why we don't want to you more than one referral level of doubler-points. ^^^
+                               } // END - if
+
+                               // Update usage counter
+                               updateConfiguration('doubler_counter', 1, '+');
+
+                               // Set constant
+                               $content['message'] = loadTemplate('doubler_reflink', TRUE, postRequestElement('userid'));
                        } else {
-                               // Wrong password or account not found
-                               define('__ERROR_MSG', DOUBLER_FORM_404_MEMBER);
+                               // Not enougth points left
+                               $content['message'] = '{--DOUBLER_FORM_NO_POINTS_LEFT--}';
                        }
-               } elseif (empty($_POST['userid'])) {
-                       // Login not entered
-                       define('__ERROR_MSG', DOUBLER_FORM_404_LOGIN);
-               } elseif (empty($_POST['pass'])) {
-                       // Password not entered
-                       define('__ERROR_MSG', DOUBLER_FORM_404_PASSWORD);
-               } elseif (empty($_POST['points'])) {
-                       // points not entered
-                       define('__ERROR_MSG', DOUBLER_FORM_404_POINTS);
+               } elseif (getUserData('status') == 'CONFIRMED') {
+                       // Account is unconfirmed!
+                       $content['message'] = '{--DOUBLER_FORM_WRONG_PASS--}';
+               } elseif (getUserData('status') == 'UNCONFIRMED') {
+                       // Account is unconfirmed!
+                       $content['message'] = '{--DOUBLER_FORM_STATUS_UNCONFIRMED--}';
+               } elseif (getUserData('status') == 'LOCKED') {
+                       // Account is locked by admin / holiday!
+                       $content['message'] = '{--DOUBLER_FORM_STATUS_LOCKED--}';
+               } elseif (postRequestElement('points') < getDoublerMin()) {
+                       // Not enougth points entered
+                       $content['message'] = '{--DOUBLER_FORM_POINTS_MIN--}';
+               } elseif (postRequestElement('points') > getDoublerMax()) {
+                       // Too much points entered
+                       $content['message'] = '{--DOUBLER_FORM_POINTS_MAX--}';
+               } elseif (isNicknameUsed(postRequestElement('userid'))) {
+                       // Cannot resolv nickname -> userid
+                       $content['message'] = '{--DOUBLER_FORM_404_NICKNAME--}';
+               } else {
+                       // Wrong password or account not found
+                       $content['message'] = '{--DOUBLER_FORM_404_MEMBER--}';
                }
+       } elseif (!isPostRequestElementSet('userid')) {
+               // Login not entered
+               $content['message'] = '{--DOUBLER_FORM_404_LOGIN--}';
+       } elseif (!isPostRequestElementSet('password')) {
+               // Password not entered
+               $content['message'] = '{--DOUBLER_FORM_404_PASSWORD--}';
+       } elseif (!isPostRequestElementSet('points')) {
+               // points not entered
+               $content['message'] = '{--DOUBLER_FORM_404_POINTS--}';
        }
+} // END - if (isFormSet())
 
-       // Set messages to nothing
-       if (!defined('__DOUBLER_MSG')) define('__DOUBLER_MSG', "");
-       if (!defined('__ERROR_MSG'))   define('__ERROR_MSG'  , "");
-
-       // Shall I check for points immediately?
-       if ($_CONFIG['doubler_send_mode'] == "DIRECT") require(PATH."inc/doubler_send.php");
-
-       // Output header
-       include(PATH."inc/header.php");
-
-       // Banner in text
-       define('__DOUBLER_BANNER', LOAD_TEMPLATE("doubler_banner", true));
-
-       // Load header/footer templates
-       define('__DOUBLER_HEADER', LOAD_TEMPLATE("doubler_header", true));
-       define('__DOUBLER_FOOTER', LOAD_TEMPLATE("doubler_footer", true));
-
-       if (!empty($uid)) {
-               // Transfer userid/nickname to constant
-               define('__REFID', $uid);
-       } else {
-               // Transfer userid/nickname to constant
-               define('__REFID', $GLOBALS['refid']);
-       }
-
-       // Percent values etc.
-       define('__CHARGE_VALUE', TRANSLATE_COMMA($_CONFIG['doubler_charge'] * 100));
-       define('__REF_VALUE'   , TRANSLATE_COMMA($_CONFIG['doubler_ref'] * 100));
-       define('__TOTAL_VALUE' , TRANSLATE_COMMA($_CONFIG['doubler_points']));
-       define('__MIN_VALUE'   , TRANSLATE_COMMA($_CONFIG['doubler_min']));
-       define('__MAX_VALUE'   , TRANSLATE_COMMA($_CONFIG['doubler_max']));
-
-       // Text "Enter login"
-       if (EXT_IS_ACTIVE("nickname")) {
-               // Choose login/nickname
-               define('DOUBLER_ENTER_LOGIN', GUEST_ENTER_LOGIN_NICKNAME);
-       } else {
-               // Simple login ID
-               define('DOUBLER_ENTER_LOGIN', GUEST_ENTER_LOGIN);
-       }
-
-       // Which mail-send-mode did the admin setup?
-       switch ($_CONFIG['doubler_send_mode'])
-       {
-       case "DIRECT":
-               define('DOUBLER_PAYOUT_TIME', DOUBLER_PAYOUT_TIME_DIRECT);
-               break;
+// Shall I check for points immediately?
+if (getDoublerSendMode() == 'DIRECT') {
+       loadInclude('inc/mails/doubler_mails.php');
+} // END - if
 
-       case "RESET":
-               define('DOUBLER_PAYOUT_TIME', DOUBLER_PAYOUT_TIME_RESET);
-               break;
-       }
+// Output header
+loadIncludeOnce('inc/header.php');
 
-       // Generate table with already payed out doubles
-       define('__DOUBLER_PAYOUT_HISTORY', DOUBLER_GENERATE_TABLE("0", "Y", "N", "DESC"));
+if (isValidUserData()) {
+       // Transfer userid/nickname to constant
+       $content['refid'] = getUserData('userid');
+} else {
+       // Transfer userid/nickname to constant
+       $content['refid'] = determineReferralId();
+}
 
-       // Generate timemark
-       define('__TIMEOUT_MARK', CREATE_FANCY_TIME($_CONFIG['doubler_timeout']));
+// Text "Enter login"
+if (isExtensionActive('nickname')) {
+       // Choose login/nickname
+       $content['enter_login'] = '{--GUEST_ENTER_LOGIN_NICKNAME--}';
+} else {
+       // Simple login id
+       $content['enter_login'] = '{--GUEST_ENTER_LOGIN--}';
+}
 
-       // Usage counter
-       define('__DOUBLER_COUNTER', $_CONFIG['doubler_counter']);
+// Which mail-send-mode did the admin setup?
+$content['payout_time'] = '{--DOUBLER_PAYOUT_TIME_' . getDoublerSendMode() . '--}';
 
-       // Points left to doubler
-       define('__LEFT_VALUE', TRANSLATE_COMMA(DOUBLER_GET_TOTAL_POINTS_LEFT()));
+// Generate table with already payed out doubles
+$content['payout_history'] = generateDoublerTable(0, 'Y', 'N', 'DESC');
 
-       // Output neccessary form for this
-       LOAD_TEMPLATE("doubler_index");
+// Output neccessary form for this
+loadTemplate('doubler_index', FALSE, $content);
 
-       // Output footer
-       include(PATH."inc/footer.php");
-} else {
-       // You have to configure first!
-       LOAD_URL("install.php");
-}
+// Output footer
+loadIncludeOnce('inc/footer.php');
 
-// Really all done here... ;-)
+// [EOF]
 ?>