- }
- }
-
- // Return status for sending routine
- return $ret;
-}
-
-// Calculate sum (default) or count records of given criteria
-function GET_TOTAL_DATA ($search, $tableName, $lookFor = "id", $whereStatement = 'userid', $countRows = false, $add = "") {
- $ret = 0;
- //* DEBUG: */ echo $search."/".$tableName."/".$lookFor."/".$whereStatement."/".$add."<br />\n";
- if ((empty($search)) && ($search != "0")) {
- // Count or sum whole table?
- if ($countRows === true) {
- // Count whole table
- $result = SQL_QUERY_ESC("SELECT COUNT(`%s`) FROM `{!_MYSQL_PREFIX!}_%s`".$add,
- array($lookFor, $tableName), __FUNCTION__, __LINE__);
- } else {
- // Sum whole table
- $result = SQL_QUERY_ESC("SELECT SUM(`%s`) FROM `{!_MYSQL_PREFIX!}_%s`".$add,
- array($lookFor, $tableName), __FUNCTION__, __LINE__);
- }
- } elseif (($countRows === true) || ($lookFor == 'userid')) {
- // Count rows
- //* DEBUG: */ echo "COUNT!<br />\n";
- $result = SQL_QUERY_ESC("SELECT COUNT(`%s`) FROM `{!_MYSQL_PREFIX!}_%s` WHERE `%s`='%s'".$add,
- array($lookFor, $tableName, $whereStatement, $search), __FUNCTION__, __LINE__);
- } else {
- // Add all rows
- //* DEBUG: */ echo "SUM!<br />\n";
- $result = SQL_QUERY_ESC("SELECT SUM(`%s`) FROM `{!_MYSQL_PREFIX!}_%s` WHERE `%s`='%s'".$add,
- array($lookFor, $tableName, $whereStatement, $search), __FUNCTION__, __LINE__);
- }
-
- // Load row
- list($ret) = SQL_FETCHROW($result);
-
- // Free result
- SQL_FREERESULT($result);
-
- // Fix empty values
- if ((empty($ret)) && ($lookFor != "counter") && ($lookFor != "id") && ($lookFor != 'userid')) {
- // Float number
- $ret = "0.00000";
- } elseif ("".$ret."" == "") {
- // Fix empty result
- $ret = "0";
- }
-
- // Return value
- return $ret;
-}
-// "Getter fro ref level percents
-function GET_REF_LEVEL_PERCENTS ($level) {
- // Default is zero
- $per = 0;
-
- // Do we have cache?
- if ((isset($GLOBALS['cache_array']['ref_depths']['level'])) && (EXT_IS_ACTIVE('cache'))) {
- // First look for level
- $key = array_search($level, $GLOBALS['cache_array']['ref_depths']['level']);
- if ($key !== false) {
- // Entry found!
- $per = $GLOBALS['cache_array']['ref_depths']['percents'][$key];
-
- // Count cache hit
- incrementConfigEntry('cache_hits');
- }
- } elseif (!EXT_IS_ACTIVE('cache')) {
- // Get referal data
- $result_lvl = SQL_QUERY_ESC("SELECT percents FROM `{!_MYSQL_PREFIX!}_refdepths` WHERE level='%s' LIMIT 1",
- array(bigintval($level)), __FUNCTION__, __LINE__);
-
- // Entry found?
- if (SQL_NUMROWS($result_lvl) == 1) {
- // Get percents
- list($per) = SQL_FETCHROW($result_lvl);
- } // END - if
-
- // Free result
- SQL_FREERESULT($result_lvl);
- }
-
- // Return percent
- return $per;
-}
-/**
- *
- * Dynamic referal system, can also send mails!
- *
- * subject = Subject line, write in lower-case letters and underscore is allowed
- * uid = Referal ID wich should receive...
- * points = ... xxx points
- * send_notify = shall I send the referal an email or not?
- * rid = inc/modules/guest/what-confirm.php need this (DEPRECATED???)
- * locked = Shall I pay it to normal (false) or locked (true) points ammount?
- * add_mode = Add points only to $uid or also refs? (WARNING! Changing "ref" to "direct"
- * for default value will cause no referal will get points ever!!!)
- */
-function ADD_POINTS_REFSYSTEM ($subject, $uid, $points, $send_notify=false, $rid="0", $locked=false, $add_mode="ref") {
- //* DEBUG: */ print "----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n";
- global $DATA;
-
- // Convert mode to lower-case
- $add_mode = strtolower($add_mode);
-
- // When $uid = 0 add points to jackpot
- if ($uid == "0") {
- // Add points to jackpot
- ADD_JACKPOT($points);
- return;
- } // END - if
-
- // Add booking record if extension is installed
- if (EXT_IS_ACTIVE('booking')) {
- // Add record
- ADD_BOOKING_RECORD($subject, $uid, $points, 'add');
- } // END - if
-
- // Count up referal depth
- if (!isset($GLOBALS['ref_level'])) {
- // Initialialize referal system
- //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>): Referal system initialized!<br />\n";
- $GLOBALS['ref_level'] = 0;
- } else {
- // Increase referal level
- $GLOBALS['ref_level']++;
- //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>): Referal level increased. DEPTH={$GLOBALS['ref_level']}<br />\n";
- }
-
- // Default is 'normal' points
- $data = "points";
-
- // Which points, locked or normal?
- if ($locked) $data = "locked_points";
-
- // Check user account
- $result_user = SQL_QUERY_ESC("SELECT refid, email FROM `{!_MYSQL_PREFIX!}_user_data` WHERE userid=%s AND `status`='CONFIRMED' LIMIT 1",
- array(bigintval($uid)), __FUNCTION__, __LINE__);
-
- //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},numRows=".SQL_NUMROWS($result_user).",points={$points}<br />\n";
- if (SQL_NUMROWS($result_user) == 1) {
- // This is the user and his ref
- list($ref, $email) = SQL_FETCHROW($result_user);
- $GLOBALS['cache_array']['add_uid'][$ref] = $uid;
-
- // Get percents
- $per = GET_REF_LEVEL_PERCENTS($GLOBALS['ref_level']);
- //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},points={$points},depth={$GLOBALS['ref_level']},per={$per},mode={$add_mode}<br />\n";
-
- // Some percents found?
- if ($per > 0) {
- // Calculate new points
- //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},points={$points},per={$per},depth={$GLOBALS['ref_level']}<br />\n";
- $ref_points = $points * $per / 100;
-
- // Pay refback here if level > 0 and in ref-mode
- if ((EXT_IS_ACTIVE('refback')) && ($GLOBALS['ref_level'] > 0) && ($per < 100) && ($add_mode == "ref") && (isset($GLOBALS['cache_array']['add_uid'][$uid]))) {
- //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},data={$GLOBALS['cache_array']['add_uid'][$uid]},ref_points={$ref_points},depth={$GLOBALS['ref_level']} - BEFORE!<br />\n";
- $ref_points = ADD_REFBACK_POINTS($GLOBALS['cache_array']['add_uid'][$uid], $uid, $points, $ref_points);
- //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},data={$GLOBALS['cache_array']['add_uid'][$uid]},ref_points={$ref_points},depth={$GLOBALS['ref_level']} - AFTER!<br />\n";
- } // END - if
-
- // Update points...
- SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_user_points` SET %s=%s+%s WHERE userid=%s AND ref_depth='%s' LIMIT 1",
- array($data, $data, $ref_points, bigintval($uid), bigintval($GLOBALS['ref_level'])), __FUNCTION__, __LINE__);
- //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):data={$data},ref_points={$ref_points},uid={$uid},depth={$GLOBALS['ref_level']},mode={$add_mode} - UPDATE! (".SQL_AFFECTEDROWS().")<br />\n";
-
- // No entry updated?
- if (SQL_AFFECTEDROWS() < 1) {
- // First ref in this level! :-)
- SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_user_points` (userid,ref_depth,%s) VALUES (%s,'%s',%s)",
- array($data, bigintval($uid), bigintval($GLOBALS['ref_level']), $ref_points), __FUNCTION__, __LINE__);
- //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):data={$data},ref_points={$ref_points},uid={$uid},depth={$GLOBALS['ref_level']},mode={$add_mode} - INSERTED! (".SQL_AFFECTEDROWS().")<br />\n";
- } // END - if
-
- // Update mediadata as well
- if (GET_EXT_VERSION('mediadata') >= '0.0.4') {
- // Update database
- MEDIA_UPDATE_ENTRY(array('total_points'), 'add', $ref_points);
- } // END - if
-
- // Points updated, maybe I shall send him an email?
- if (($send_notify) && ($ref > 0) && (!$locked)) {
- // Prepare content
- $content = array(
- 'percent' => $per,
- 'level' => bigintval($GLOBALS['ref_level']),
- 'points' => $ref_points,
- 'refid' => bigintval($ref)
- );
-
- // Load email template
- $msg = LOAD_EMAIL_TEMPLATE("confirm-referal", $content, bigintval($uid));
-
- SEND_EMAIL($email, THANX_REFERRAL_ONE, $msg);
- } elseif (($send_notify) && ($ref == 0) && (!$locked) && ($add_mode == "direct") && (!defined('__POINTS_VALUE'))) {
- // Direct payment shall be notified about
- define('__POINTS_VALUE', $ref_points);
-
- // Prepare content
- $content = array(
- 'text' => getMessage('REASON_DIRECT_PAYMENT'),
- 'points' => TRANSLATE_COMMA($ref_points)
- );
-
- // Load message
- $msg = LOAD_EMAIL_TEMPLATE("add-points", $content, $uid);
-
- // And sent it away
- SEND_EMAIL($email, getMessage('SUBJECT_DIRECT_PAYMENT'), $msg);
- if (!REQUEST_ISSET_GET(('mid'))) LOAD_TEMPLATE('admin_settings_saved', false, getMessage('ADMIN_POINTS_ADDED'));
- }
-
- // Maybe there's another ref?
- if (($ref > 0) && ($points > 0) && ($ref != $uid) && ($add_mode == "ref")) {
- // Then let's credit him here...
- //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},ref={$ref},points={$points} - ADVANCE!<br />\n";
- ADD_POINTS_REFSYSTEM(sprintf("%s_ref:%s", $subject, $GLOBALS['ref_level']), $ref, $points, $send_notify, $ref, $locked);
- } // END - if