- return $ret;
-}
-// "Getter fro ref level percents
-function GET_REF_LEVEL_PERCENTS ($level) {
- global $cacheInstance, $cacheArray;
-
- // Default is zero
- $per = 0;
-
- // Do we have cache?
- if ((isset($cacheArray['ref_depths']['level'])) && (EXT_IS_ACTIVE("cache"))) {
- // First look for level
- $key = array_search($level, $cacheArray['ref_depths']['level']);
- if ($key !== false) {
- // Entry found!
- $per = $cacheArray['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)), __FILE__, __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, $cacheArray;
-
- // 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)), __FILE__, __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);
- $cacheArray['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($cacheArray['add_uid'][$uid]))) {
- //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},data={$cacheArray['add_uid'][$uid]},ref_points={$ref_points},depth={$GLOBALS['ref_level']} - BEFORE!<br />\n";
- $ref_points = ADD_REFBACK_POINTS($cacheArray['add_uid'][$uid], $uid, $points, $ref_points);
- //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},data={$cacheArray['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'])), __FILE__, __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), __FILE__, __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' => 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, SUBJECT_DIRECT_PAYMENT, $msg);
- if (!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
- } // END - if
- } // END - if
-
- // Free result
- SQL_FREERESULT($result_user);
- //* DEBUG: */ print "</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />\n";
-}
-//
-function UPDATE_REF_COUNTER ($uid) {
- global $cacheArray, $cacheInstance;
-
- // Make it sure referal level zero (member him-/herself) is at least selected
- if (empty($cacheArray['ref_level'][$uid])) $cacheArray['ref_level'][$uid] = 1;
- //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},level={$cacheArray['ref_level'][$uid]}<br />\n";
-
- // Update counter
- SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_refsystem` SET counter=counter+1 WHERE userid=%s AND level='%s' LIMIT 1",
- array(bigintval($uid), $cacheArray['ref_level'][$uid]), __FILE__, __LINE__);
-
- // When no entry was updated then we have to create it here
- //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):updated=".SQL_AFFECTEDROWS()."<br />\n";
- if (SQL_AFFECTEDROWS() < 1) {
- // First count!
- SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_refsystem` (userid, level, counter) VALUES (%s,%s,1)",
- array(bigintval($uid), $cacheArray['ref_level'][$uid]), __FILE__, __LINE__);
- //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid}<br />\n";
- } // END - if
-
- // Check for his referal
- $result = SQL_QUERY_ESC("SELECT refid FROM `{!_MYSQL_PREFIX!}_user_data` WHERE userid=%s LIMIT 1",
- array(bigintval($uid)), __FILE__, __LINE__);
-
- // Load refid
- list($ref) = SQL_FETCHROW($result);
-
- // Free memory
- SQL_FREERESULT($result);
- //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},ref={$ref}<br />\n";
-
- // When he has a referal...
- if (($ref > 0) && ($ref != $uid)) {
- // Move to next referal level and count his counter one up!
- //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):ref={$ref} - ADVANCE!<br />\n";
- $cacheArray['ref_level'][$uid]++; UPDATE_REF_COUNTER($ref);
- } elseif ((($ref == $uid) || ($ref == 0)) && (GET_EXT_VERSION("cache") >= "0.1.2")) {
- // Remove cache here
- //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):ref={$ref} - CACHE!<br />\n";
- REBUILD_CACHE("refsystem", "refsystem");
- }
-
- // "Walk" back here
- $cacheArray['ref_level'][$uid]--;
-
- // Handle refback here if extension is installed
- if (EXT_IS_ACTIVE("refback")) {
- UPDATE_REFBACK_table($uid);
- } // END - if