Login procedure rewritten to filters (internal TODO)
[mailer.git] / inc / libs / bonus_functions.php
index 55aba4995f949096c806a830686e695ac2d2e772..af551a1b82a40a81f378f4a6dd58682ce3a8244c 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /************************************************************************
- * MXChange v0.2.1                                    Start: 11/10/2004 *
- * ===============                              Last change: 03/18/2005 *
+ * Mailer v0.2.1-FINAL                                Start: 11/10/2004 *
+ * ===================                          Last change: 03/18/2005 *
  *                                                                      *
  * -------------------------------------------------------------------- *
  * File              : bonus_functions.php                              *
@@ -17,7 +17,7 @@
  * Needs to be in all Files and every File needs "svn propset           *
  * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
  * -------------------------------------------------------------------- *
- * Copyright (c) 2003 - 2008 by Roland Haeder                           *
+ * Copyright (c) 2003 - 2009 by Roland Haeder                           *
  * For more information visit: http://www.mxchange.org                  *
  *                                                                      *
  * This program is free software; you can redistribute it and/or modify *
 
 // Some security stuff...
 if (!defined('__SECURITY')) {
-       $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), '/inc') + 4) . '/security.php';
-       require($INC);
-}
+       die();
+} // END - if
 
 // This function must be run *BEFORE* a link is removed from table 'mxchange_user_links' !
-function BONUS_ADD_TURBO_POINTS ($mid, $uid, $type) {
+function addTurboBonus ($mid, $userid, $type) {
        // Shall we add bonus points?
        if (getConfig('bonus_active') != 'Y') return false;
 
        // Init variables
-       $sql = ''; $bonys = 0; $mail = 0; $column = '';
+       $sql = ''; $bonus = '0'; $mail = '0'; $column = '';
 
        // Select SQL command
-       switch ($type)
-       {
+       switch ($type) {
                case 'bonusid':
                        $column = 'bonus_id';
                        $bonus = $mid;
@@ -64,7 +62,7 @@ function BONUS_ADD_TURBO_POINTS ($mid, $uid, $type) {
                        break;
 
                default:
-                       DEBUG_LOG(__FUNCTION__, __LINE__, sprintf("Invalid type %s detected.", $type));
+                       logDebugMessage(__FUNCTION__, __LINE__, sprintf("Invalid type %s detected.", $type));
                        break;
        }
 
@@ -75,7 +73,7 @@ function BONUS_ADD_TURBO_POINTS ($mid, $uid, $type) {
        } // END - if
 
        // Check for entry
-       $rank = GET_TOTAL_DATA($uid, 'bonus_turbo', 'id', 'userid', true, sprintf(" AND `%s`=%s", $column, $mid)) + 1;
+       $rank = countSumTotalData($mid, 'bonus_turbo', 'id', $column, true) + 1;
 
        // Which rank?
        if ($rank == 1) {
@@ -94,103 +92,149 @@ function BONUS_ADD_TURBO_POINTS ($mid, $uid, $type) {
        }
 
        // Add points to his account directly
-       SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_user_data` SET `turbo_bonus`=`turbo_bonus`+%s WHERE `userid`=%s LIMIT 1",
-               array(bigintval($uid), $points), __FUNCTION__, __LINE__);
+       SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `turbo_bonus`=`turbo_bonus`+%s WHERE `userid`=%s LIMIT 1",
+               array(
+                       bigintval($userid),
+                       $points
+               ), __FUNCTION__, __LINE__);
 
        // Rember this whole data for displaying ranking list
-       SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_bonus_turbo` (`userid`, `mail_id`, `bonus_id`, `level`, `points`, `timemark`) VALUES (%s, %s, %s, %s, %s, UNIX_TIMESTAMP())",
+       SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_bonus_turbo` (`userid`, `mail_id`, `bonus_id`, `level`, `points`, `timemark`) VALUES (%s, %s, %s, %s, %s, UNIX_TIMESTAMP())",
                array(
-                       bigintval($uid),
+                       bigintval($userid),
                        bigintval($mail),
                        bigintval($bonus),
                        $rank,
                        $points
                ), __FUNCTION__, __LINE__);
 
-       if ((GET_EXT_VERSION('bonus') >= '0.3.5') && (getConfig('bonus_mode') != "ADD") && ($points > 0)) BONUS_POINTS_HANDLER($points);
+       if ((getExtensionVersion('bonus') >= '0.3.5') && (getConfig('bonus_mode') != "ADD") && ($points > 0)) handleBonusPoints($points);
 }
+
 //
-function BONUS_MAKE_RANK_ROWS ($data, $type, $uid) {
-       $self = false; $OUT = '';
+function addBonusRanks ($data, $type, $userid) {
+       // Init variables
+       $self = false; $OUT = ''; $GLOBALS['ranking_content'] = array();
+
+       // Clear rankings by default
+       $GLOBALS['ranking_content']['rankings'] = '';
 
        // How many ranks do we have?
        $ranks = count(explode(';', getConfig('turbo_rates'))) + 1;
 
        // Load current user's data
-       $result = SQL_QUERY_ESC("SELECT `level`, `points`, `timemark` FROM `{!_MYSQL_PREFIX!}_bonus_turbo` WHERE `%s`=%s AND `userid`=%s LIMIT 1",
-               array($type, $data, $uid), __FUNCTION__, __LINE__);
+       $result = SQL_QUERY_ESC("SELECT `level`, `points`, `timemark` FROM `{?_MYSQL_PREFIX?}_bonus_turbo` WHERE `%s`=%s AND `userid`=%s LIMIT 1",
+               array(
+                       $type,
+                       $data,
+                       $userid
+               ), __FUNCTION__, __LINE__);
+
+       // Entry found?
        if (SQL_NUMROWS($result) == 1) {
                // Load data
-               list($rank, $points, $mark) = SQL_FETCHROW($result);
+               $GLOBALS['ranking_content'] = merge_array($GLOBALS['ranking_content'], SQL_FETCHARRAY($result));
 
                // Remember all values for later use
-               $self  = true; $points = translateComma($points);
+               $self  = true;
 
                // Transfer data to template
-               define('__YR_LEVEL' , $rank);
-               define('__YR_POINTS', $points);
-               define('__YR_TMARK' , generateDateTime($mark, '1'));
+               $GLOBALS['ranking_content']['yr_level']  = $GLOBALS['ranking_content']['level'];
+               $GLOBALS['ranking_content']['yr_points'] = translateComma($GLOBALS['ranking_content']['points']);
+               $GLOBALS['ranking_content']['yr_tmark']  = generateDateTime($GLOBALS['ranking_content']['timemark'], 1);
 
                // Load template
-               define('__YOUR_RANKING_LINE', LOAD_TEMPLATE("show_bonus_yr", true));
+               $GLOBALS['ranking_content']['own'] = loadTemplate('show_bonus_yr', true, $GLOBALS['ranking_content']);
        } // END - if
 
        // Load rankings
-       $result = SQL_QUERY_ESC("SELECT `id` FROM `{!_MYSQL_PREFIX!}_bonus_turbo` WHERE %s=%s ORDER BY level LIMIT %s",
-       array($type, $data, getConfig('bonus_lines')), __FUNCTION__, __LINE__);
+       $result = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_bonus_turbo` WHERE %s=%s ORDER BY `level` ASC LIMIT {?bonus_lines?}",
+               array($type, $data), __FUNCTION__, __LINE__);
        if (SQL_NUMROWS($result) > 0) {
                // Start generating the ranking list
                $max = SQL_NUMROWS($result);
 
+               // Init variables
                $SW = 2;
+
+               // Output all ranks (levels)
                for ($rank = 1; $rank <= $max; $rank++) {
                        // Load data
-                       $result_users = SQL_QUERY_ESC("SELECT userid, points, timemark FROM `{!_MYSQL_PREFIX!}_bonus_turbo` WHERE %s=%s AND level=%s LIMIT 1",
-                       array($type, $data, $rank), __FUNCTION__, __LINE__);
+                       $result_users = SQL_QUERY_ESC("SELECT
+       `userid`, `points`
+FROM
+       `{?_MYSQL_PREFIX?}_bonus_turbo`
+WHERE
+       `%s`=%s AND
+       `level`=%s
+LIMIT 1",
+                               array($type, $data, $rank), __FUNCTION__, __LINE__);
 
                        // Nothing found by default
-                       $userid = '---'; $points = '---';
+                       $rows['userid'] = '---';
+                       $rows['points'] = '---';
 
                        // Are you one of them?
                        if (SQL_NUMROWS($result_users) == 1) {
                                // Load data
-                               list($userid, $points, $mark) = SQL_FETCHROW($result_users);
+                               $rows = merge_array($rows, SQL_FETCHARRAY($result_users));
+
+                               // Is ext-nickname active?
+                               if (isExtensionActive('nickname')) {
+                                       // Then get the nickname
+                                       $nick = getNickname($rows['userid']);
+
+                                       // Is it not empty? Then use it
+                                       if (!empty($nick)) $rows['userid'] = $nick;
+                               } // END - if
 
                                // Translate comma
-                               $points = translateComma($points);
+                               $rows['points'] = translateComma($rows['points']);
                        } // END - if
 
+                       // Free result
+                       SQL_FREERESULT($result_users);
+
+                       // Add more
+                       $rows['rank'] = $rank;
+                       $rows['sw']   = $SW;
+
                        // Output row
                        $OUT .= "<tr>
-  <td class=\"bonus_rank_".$rank." bottom2 switch_sw".$SW."\">&nbsp;".$rank.".</td>
-  <td class=\"bonus_rank_".$rank." bottom2 switch_sw".$SW."\" align=\"center\">".$userid."</td>
-  <td class=\"bonus_rank_".$rank." bottom2 switch_sw".$SW."\" align=\"center\">".$points."</td>
+  <td class=\"bonus_rank_".$rows['rank']." bottom switch_sw".$rows['sw']."\">&nbsp;".$rows['rank'].".</td>
+  <td class=\"bonus_rank_".$rows['rank']." bottom switch_sw".$rows['sw']."\" align=\"center\">".$rows['userid']."</td>
+  <td class=\"bonus_rank_".$rows['rank']." bottom switch_sw".$rows['sw']."\" align=\"center\">".$rows['points']."</td>
 </tr>\n";
+
+                       // Switch color
                        $SW = 3 - $SW;
                } // END - for
 
-               if (!$self) {
+               if ($self === false) {
                        // If current user was not found set constant
-                       define('__YOUR_RANKING_LINE', BONUS_RANK_YOU_ARE_NOT_FOUND);
+                       // @TODO Try to find a way for rewriting this constant
+                       $GLOBALS['ranking_content']['rankings'] = getMessage('BONUS_RANK_YOU_ARE_404');
                } // END - if
        } else {
                // No entries found!
                $OUT = "<tr>
-  <td colspan=\"3\" align=\"center\" height=\"30\" class=\"bottom2\">
+  <td colspan=\"3\" align=\"center\" height=\"30\" class=\"bottom\">
     <div class=\"guest_failed\">".sprintf(getMessage('BONUS_NO_RANKS'), $data)."</div>
   </td>
 </tr>\n";
-               define('__YOUR_RANKING_LINE', '');
        }
+
+       // Retutn content
        return $OUT;
 }
+
 //
-function BONUS_POINTS_HANDLER ($mode) {
+function handleBonusPoints ($mode) {
        // Shall we add bonus points?
        if (getConfig('bonus_active') != 'Y') return;
 
        // Switch to jackpot-mode when no UID is supplied but userid-mode is selected
-       if ((getConfig('bonus_mode') == 'UID') && (getConfig('bonus_uid') == '0')) {
+       if ((getConfig('bonus_mode') == 'UID') && (getConfig('bonus_userid') == '0') && (isExtensionActive('jackpot'))) {
                // Update database & config
                updateConfiguration('bonus_mode', 'JACKPOT');
        } // END - if
@@ -203,43 +247,87 @@ function BONUS_POINTS_HANDLER ($mode) {
                $points = $mode;
        }
 
+       // Check his amount first
+       $total = countSumTotalData(getConfig('bonus_userid'), 'user_points', 'points') - countSumTotalData(getConfig('bonus_userid'), 'user_data', 'used_points');
+
        // Subtract points from...
-       switch (getConfig('bonus_mode'))
-       {
+       switch (getConfig('bonus_mode')) {
                case 'JACKPOT': // ... jackpot
-                       if ((SUB_JACKPOT($points) == -1) && (getConfig('bonus_uid') > 0)) {
-                               // Check points amount first...
-                               $total = GET_TOTAL_DATA(getConfig('bonus_uid'), 'user_points', 'points') - GET_TOTAL_DATA(getConfig('bonus_uid'), 'user_data', 'used_points');
+                       if ((isExtensionActive('jackpot')) && (subtractPointsFromJackpot($points) == -1) && (getConfig('bonus_userid') > 0)) {
                                if ($total >= $points) {
                                        // Subtract points from userid's account
-                                       SUB_POINTS('bonus_payout_jackpot', getConfig('bonus_uid'), $points);
+                                       subtractPointsFromJackpot('bonus_payout_jackpot', getConfig('bonus_userid'), $points);
                                } // END - if
                        } // END - if
                        break;
 
                case 'UID': // ... userid's account
-                       // Check his amount first
-                       $total = GET_TOTAL_DATA(getConfig('bonus_uid'), 'user_points', 'points') - GET_TOTAL_DATA(getConfig('bonus_uid'), 'user_data', 'used_points');
                        if ($total >= $points) {
                                // Subtract points from userid's account
-                               SUB_POINTS('bonus_payout_uid', getConfig('bonus_uid'), $points);
-                       } else {
+                               subtractPoints('bonus_payout_userid', getConfig('bonus_userid'), $points);
+                       } elseif (isExtensionActive('jackpot')) {
                                // Try to subtract from jackpot
-                               $dummy = SUB_JACKPOT($points);
+                               $dummy = subtractPointsFromJackpot($points);
                        }
                        break;
-       }
+       } // END - switch
 }
 
-//
-function BONUS_PURGE_EXPIRED_TURBO_BONUS() {
+// Purges expired fast-click bonus entries
+function purgeExpiredTurboBonus() {
        // Remove entries
-       $result = SQL_QUERY("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_bonus_turbo` WHERE timemark < (UNIX_TIMESTAMP() - ".getConfig('bonus_timeout').")", __FUNCTION__, __LINE__);
+       $result = SQL_QUERY("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_bonus_turbo` WHERE `timemark` < (UNIX_TIMESTAMP() - {?bonus_timeout?})", __FUNCTION__, __LINE__);
+
        if (SQL_AFFECTEDROWS() > 0) {
                // Send out email to admin
-               sendAdminNotification(AUTOPURGE_ADMIN_TURBO_SUBJECT, 'admin_autopurge_turbo', SQL_AFFECTEDROWS(), '');
+               sendAdminNotification(getMessage('AUTOPURGE_ADMIN_TURBO_SUBJECT'), 'admin_autopurge_turbo', SQL_AFFECTEDROWS(), '');
        } // END - if
 }
 
-//
+///////////////////////////////////////////////////////////////////////////////
+//                              Only filter functions
+///////////////////////////////////////////////////////////////////////////////
+
+// Filter for adding login bonus to the user's account
+function FILTER_ADD_LOGIN_BONUS () {
+       // Is the user data valid?
+       if (!isMember()) {
+               // Do only run for logged in members
+               debug_report_bug('Please only run this filter for logged in users.');
+       } // END - if
+
+       // Bonus is not given by default ;-)
+       $bonus = false;
+       if ((isExtensionInstalledAndNewer('sql_patches', '0.2.8')) && (getConfig('bonus_active') == 'Y') && (getConfig('bonus_login_yn') == 'Y')) {
+               // Update last login if far enougth away
+               // @TODO This query isn't right, it will only update if the user was for a longer time away!
+               SQL_QUERY_ESC("UPDATE
+       `{?_MYSQL_PREFIX?}_user_data`
+SET
+       `last_login`=UNIX_TIMESTAMP()
+WHERE
+       `userid`=%s AND
+       `last_login` < (UNIX_TIMESTAMP() - {?login_timeout?})
+LIMIT 1",
+                       array(
+                               getMemberId()
+                       ), __FILE__, __LINE__
+               );
+
+               // Updated entry?
+               $bonus = (SQL_AFFECTEDROWS() == 1);
+       } // END - if
+
+       if (($bonus === true) && (getRequestElement('mode') == 'bonus')) {
+               // Output message with added points
+               $GLOBALS['message'] .= "<div class=\"tiny\">
+  ".sprintf(getMessage('BONUS_LOGIN_BONUS_ADDED'), translateComma(getConfig('login_bonus')))."
+</div>";
+       } elseif (isExtensionActive('bonus')) {
+               // No login bonus added!
+               $GLOBALS['message'] .= "<div class=\"member_failed\">{--BONUS_LOGIN_BONUS_NOT_ADDED--}</div>";
+       }
+}
+
+// [EOF]
 ?>