2 /************************************************************************
3 * Mailer v0.2.1-FINAL Start: 07/12/2011 *
4 * =================== Last change: 07/12/2011 *
6 * -------------------------------------------------------------------- *
7 * File : referal-functions.php *
8 * -------------------------------------------------------------------- *
9 * Short description : All referal system functions *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Alle zum Referal-System gehoerenden Funktionen *
12 * -------------------------------------------------------------------- *
15 * $Tag:: 0.2.1-FINAL $ *
17 * -------------------------------------------------------------------- *
18 * Copyright (c) 2003 - 2009 by Roland Haeder *
19 * Copyright (c) 2009 - 2011 by Mailer Developer Team *
20 * For more information visit: http://www.mxchange.org *
22 * This program is free software; you can redistribute it and/or modify *
23 * it under the terms of the GNU General Public License as published by *
24 * the Free Software Foundation; either version 2 of the License, or *
25 * (at your option) any later version. *
27 * This program is distributed in the hope that it will be useful, *
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
30 * GNU General Public License for more details. *
32 * You should have received a copy of the GNU General Public License *
33 * along with this program; if not, write to the Free Software *
34 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
36 ************************************************************************/
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
43 // Initializes the referal system
44 function initReferalSystem () {
45 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ' Referal system initialized!');
46 $GLOBALS['ref_level'] = NULL;
47 $GLOBALS['ref_system'] = true;
50 // Getter fro ref level percents
51 function getReferalLevelPercents ($level) {
53 $data['percents'] = '0';
56 if ((isset($GLOBALS['cache_array']['refdepths']['level'])) && (isExtensionActive('cache'))) {
57 // First look for level
58 $key = array_search($level, $GLOBALS['cache_array']['refdepths']['level']);
61 $data['percents'] = $GLOBALS['cache_array']['refdepths']['percents'][$key];
64 incrementStatsEntry('cache_hits');
66 } elseif (!isExtensionActive('cache')) {
68 $result_level = SQL_QUERY_ESC("SELECT `percents` FROM `{?_MYSQL_PREFIX?}_refdepths` WHERE `level`=%s LIMIT 1",
69 array(bigintval($level)), __FUNCTION__, __LINE__);
72 if (SQL_NUMROWS($result_level) == 1) {
74 $data = SQL_FETCHARRAY($result_level);
78 SQL_FREERESULT($result_level);
82 return $data['percents'];
87 * Dynamic referal and points system, can also send mails!
89 * subject = Subject line, write in lower-case letters and underscore is allowed
90 * userid = Referal id wich should receive...
91 * points = ... xxx points
92 * refid = inc/modules/guest/what-confirm.php need this
93 * locked = Shall I pay it to normal (false) or locked (true) points ammount?
95 function addPointsThroughReferalSystem ($subject, $userid, $points, $refid = NULL) {
96 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'subject=' . $subject . ',userid=' . $userid . ',points=' . $points . ',refid=' . $refid . ' - ENTERED!');
97 // By default nothing has been added
100 // Determine payment method and notification
101 $paymentMethod = strtoupper(getPaymentMethodFromSubject($subject));
102 $sendNotify = isPaymentRecipientNotificationEnabled($subject);
104 // When $userid = '0' add points to jackpot
105 if (($userid == '0') && ($paymentMethod == 'DIRECT') && (isExtensionActive('jackpot'))) {
106 // Add points to jackpot only in DIRECT mode
107 return addPointsToJackpot($points);
110 // Check user account
111 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',points=' . $points);
112 if (fetchUserData($userid)) {
113 // Determine wether the user has some mails to click before he/she gets the points
114 $locked = ifUserPointsLocked($userid);
116 // Detect database column
117 $pointsColumn = determinePointsColumnFromSubjectLocked($subject, $locked);
119 // This is the user and his ref
120 $GLOBALS['cache_array']['add_userid'][getUserData('refid')] = $userid;
123 $per = getReferalLevelPercents($GLOBALS['ref_level']);
124 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid='.$userid.',points='.$points.',depth='.$GLOBALS['ref_level'].',per='.$per.',mode='.$paymentMethod);
126 // Some percents found?
128 // Calculate new points
129 $ref_points = $points * $per / 100;
130 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid='.$userid.',points='.$points.',per='.$per.',depth='.$GLOBALS['ref_level'].',ref_points='.$ref_points);
132 // Pay refback here if level > 0 and in ref-mode
133 if ((isExtensionActive('refback')) && ($GLOBALS['ref_level'] > 0) && ($per < 100) && ($paymentMethod == 'REFERAL') && (isset($GLOBALS['cache_array']['add_userid'][$userid]))) {
134 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid='.$userid.',data='.$GLOBALS['cache_array']['add_userid'][$userid].',ref_points='.$ref_points.',depth='.$GLOBALS['ref_level'].' - BEFORE!');
135 $ref_points = addRefbackPoints($GLOBALS['cache_array']['add_userid'][$userid], $userid, $points, $ref_points);
136 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid='.$userid.',data='.$GLOBALS['cache_array']['add_userid'][$userid].',ref_points='.$ref_points.',depth='.$GLOBALS['ref_level'].' - AFTER!');
140 if (is_null($GLOBALS['ref_level'])) {
142 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_points` SET `%s`=`%s`+%s WHERE `userid`=%s AND `ref_depth` IS NULL LIMIT 1",
148 ), __FUNCTION__, __LINE__);
151 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_points` SET `%s`=`%s`+%s WHERE `userid`=%s AND `ref_depth`=%s LIMIT 1",
157 bigintval($GLOBALS['ref_level'])
158 ), __FUNCTION__, __LINE__);
160 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'pointsColumn='.$pointsColumn.',ref_points='.$ref_points.',userid='.$userid.',depth='.$GLOBALS['ref_level'].',mode='.$paymentMethod.' - UPDATE! ('.SQL_AFFECTEDROWS().')');
163 if (SQL_HASZEROAFFECTED()) {
164 // First ref in this level! :-)
165 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_points` (`userid`, `ref_depth`, `%s`) VALUES (%s, %s, %s)",
169 makeZeroToNull($GLOBALS['ref_level']),
171 ), __FUNCTION__, __LINE__);
172 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'data='.$pointsColumn.',ref_points='.$ref_points.',userid='.$userid.',depth='.$GLOBALS['ref_level'].',mode='.$paymentMethod.' - INSERTED! ('.SQL_AFFECTEDROWS().')');
175 // Check affected rows
176 $added = SQL_AFFECTEDROWS();
177 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'added='.intval($added));
179 // Prepare data for the filter
181 'subject' => $subject,
184 'ref_points' => $ref_points,
185 'column' => $pointsColumn,
186 'notify' => $sendNotify,
190 'add_mode' => $paymentMethod,
195 $filterData = runFilterChain('post_add_points', $filterData);
198 $added = $filterData['added'];
199 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'added='.intval($added));
201 // Points updated, maybe I shall send him an email?
202 if (($sendNotify === true) && (isValidUserId(getUserData('refid'))) && ($locked === false)) {
206 'level' => bigintval($GLOBALS['ref_level']),
207 'points' => $ref_points,
210 // Load email template
211 $message = loadEmailTemplate('guest_user_confirmed_referal', $content, bigintval($userid));
214 sendEmail($userid, '{--THANX_REFERAL_ONE_SUBJECT--}', $message);
215 } elseif (($sendNotify === true) && (!isValidUserId(getUserData('refid'))) && ($locked === false) && ($paymentMethod == 'DIRECT')) {
218 'reason' => '{--REASON_DIRECT_PAYMENT--}',
219 'subject' => $subject,
220 'points' => $ref_points
224 $message = loadEmailTemplate('member_add_points', $content, $userid);
227 sendEmail($userid, '{--DIRECT_PAYMENT_SUBJECT--}', $message);
228 if (!isGetRequestParameterSet('mid')) {
229 // Output message to admin
230 displayMessage('{--ADMIN_POINTS_ADDED--}');
234 // Increase referal level
235 $GLOBALS['ref_level']++;
236 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ' Referal level increased, ref_level=' . $GLOBALS['ref_level']);
238 // Maybe there's another ref?
239 if ((isValidUserId(getUserData('refid'))) && ($points > 0) && (getUserData('refid') != $userid) && ($paymentMethod == 'REFERAL')) {
240 // Then let's credit him here...
241 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',refid=' . getUserData('refid') . ',points=' . $points . ',ref_points=' . $ref_points . ' - ADVANCE!');
242 $added = ($added && addPointsThroughReferalSystem(sprintf("%s_ref:%s", $subject, $GLOBALS['ref_level']), getUserData('refid'), $points, getUserData('refid')));
247 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'subject=' . $subject . ',userid=' . $userid . ',points=' . $points . ',sendNotify=' . intval($sendNotify) . ',refid=' . $refid . ',paymentMethod=' . $paymentMethod . ' - EXIT!');
251 // Updates the referal counter
252 function updateReferalCounter ($userid) {
253 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ' - ENTERED!');
257 // Check for his referal
258 if (fetchUserData($userid)) {
260 $ref = getUserData('refid');
261 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',ref=' . makeZeroToNull($ref) . ' - FETCHED!');
265 if (empty($GLOBALS['cache_array']['ref_level'][$userid])) {
266 $GLOBALS['cache_array']['ref_level'][$userid] = NULL;
268 if (empty($GLOBALS['cache_array']['ref_level'][$ref])) {
269 $GLOBALS['cache_array']['ref_level'][$ref] = NULL;
272 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',ref=' . makeZeroToNull($ref));
274 // When he has a referal...
275 if (($ref > 0) && ($ref != $userid)) {
276 // Move to next referal level and count his counter one up
277 $GLOBALS['cache_array']['ref_level'][$ref] = $GLOBALS['cache_array']['ref_level'][$userid] + 1;
278 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',ref(' . $ref . ')=' . $GLOBALS['cache_array']['ref_level'][$ref] . ' - ADVANCED!');
281 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_refsystem` SET `counter`=`counter`+1 WHERE `userid`=%s AND `level`=%s LIMIT 1",
284 bigintval($GLOBALS['cache_array']['ref_level'][$ref])
285 ), __FUNCTION__, __LINE__);
287 // When no entry was updated then we have to create it here
288 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ref=' . $ref . ',level=' . $GLOBALS['cache_array']['ref_level'][$ref] . ',updated=' . SQL_AFFECTEDROWS());
289 if (SQL_HASZEROAFFECTED()) {
291 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_refsystem` (`userid`, `level`, `counter`) VALUES (%s,%s,1)",
294 makeZeroToNull($GLOBALS['cache_array']['ref_level'][$ref])
295 ), __FUNCTION__, __LINE__);
296 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ref=' . $ref . ',level=' . $GLOBALS['cache_array']['ref_level'][$ref] . ',SQL_AFFECTEDROWS()=' . SQL_AFFECTEDROWS());
299 // Advance to next level
300 updateReferalCounter($ref);
301 } elseif ((($ref == $userid) || ($ref == '0')) && (isExtensionInstalledAndNewer('cache', '0.1.2'))) {
303 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ref=' . makeZeroToNull($ref) . ' - CACHE!');
304 rebuildCache('refsystem', 'refsystem');
307 // Update the referal table
308 updateReferalTable($userid);
310 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',ref=' . makeZeroToNull($ref) . ',level=' . makeZeroToNull($GLOBALS['cache_array']['ref_level'][$ref]) . ' - EXIT!');
313 // Subtract points from database and mediadata cache
314 function subtractPoints ($subject, $userid, $points) {
315 // Add points to used points
316 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `used_points`=`used_points`+%s WHERE `userid`=%s LIMIT 1",
320 ), __FUNCTION__, __LINE__);
322 // Prepare filter data
324 'subject' => $subject,
328 'added' => (!SQL_HASZEROAFFECTED())
331 // Insert booking record
332 $filterData = runFilterChain('post_sub_points', $filterData);
335 return $filterData['added'];
337 // "Getter" for array for user refs and points in given level
338 function getUserReferalPoints ($userid, $level) {
339 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ' - ENTERED!');
340 // Default is no refs and no nickname
343 // Get refs from database
344 $result = SQL_QUERY_ESC("SELECT
345 ur.`id`, ur.`refid`, ud.`status`, ud.`last_online`, ud.`mails_confirmed`, ud.`emails_received`
347 `{?_MYSQL_PREFIX?}_user_refs` AS `ur`
349 `{?_MYSQL_PREFIX?}_user_points` AS `up`
351 ur.`refid`=up.`userid` AND
352 (ur.`level`=0 OR ur.`level` IS NULL)
354 `{?_MYSQL_PREFIX?}_user_data` AS `ud`
356 ur.`refid`=ud.`userid`
365 ), __FUNCTION__, __LINE__);
367 // Are there some entries?
368 if (!SQL_HASZERONUMS($result)) {
370 while ($row = SQL_FETCHARRAY($result)) {
371 // Get total points of this user
372 $row['points'] = getTotalPoints($row['refid']);
374 // Get unconfirmed mails
375 $row['unconfirmed'] = getTotalUnconfirmedMails($row['refid']);
377 // Init click rate with zero
378 $row['click_rate'] = '0';
380 // Is at least one mail received?
381 if ($row['emails_received'] > 0) {
382 // Calculate click rate
383 $row['click_rate'] = ($row['mails_confirmed'] / $row['emails_received'] * 100);
386 // Activity is 'active' by default because if autopurge is not installed
387 $row['activity'] = '{--MEMBER_ACTIVITY_ACTIVE--}';
389 // Is autopurge installed and the user inactive?
390 if ((isExtensionActive('autopurge')) && ((time() - getApInactiveSince()) >= $row['last_online'])) {
392 $row['activity'] = '{--MEMBER_ACTIVITY_INACTIVE--}';
395 // Remove some entries
396 unset($row['mails_confirmed']);
397 unset($row['emails_received']);
398 unset($row['last_online']);
401 $refs[$row['id']] = $row;
406 SQL_FREERESULT($result);
409 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ' - EXIT!');
413 // Get points data for given subject
414 function getPointsDataArrayFromSubject ($subject) {
415 // Extension ext-sql_patches must be up-to-date
416 if (isExtensionInstalledAndOlder('sql_patches', '0.8.2')) {
417 // Please update ext-sql_patches
418 debug_report_bug(__FUNCTION__, __LINE__, 'sql_patches is out-dated. Please update to at least 0.8.2 to continue. subject=' . $subject);
421 // Remove any double-dot from it
422 $subjectArray = explode(':', $subject);
423 $subject = $subjectArray[0];
424 unset($subjectArray);
426 // If we have cache, shortcut it here
427 if (isset($GLOBALS['cache_array']['points_data'][$subject])) {
429 return $GLOBALS['cache_array']['points_data'][$subject];
432 // Now checkout the entry in database table
433 $result = SQL_QUERY_ESC("SELECT `id`, `subject`, `column_name`, `locked_mode`, `payment_method`, `notify_recipient` FROM `{?_MYSQL_PREFIX?}_points_data` WHERE `subject`='%s' LIMIT 1",
434 array($subject), __FUNCTION__, __LINE__);
436 // Do we have an entry?
437 if (SQL_NUMROWS($result) == 1) {
439 $pointsData = SQL_FETCHARRAY($result);
441 // Add all entries to our cache array
442 foreach ($pointsData as $key=>$value) {
443 $GLOBALS['cache_array']['points_data'][$subject][$key] = $value;
446 // Register this automatically
447 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_points_data` (`subject`, `column_name`, `locked_mode`, `payment_method`, `notify_recipient`) VALUES ('%s','points','LOCKED','REFERAL','N')",
448 array($subject), __FUNCTION__, __LINE__);
451 return getPointsDataArrayFromSubject($subject);
455 SQL_FREERESULT($result);
458 return $GLOBALS['cache_array']['points_data'][$subject];
461 // Determines the right points column name for given subject and 'locked'
462 function getPointsColumnNameFromSubjectLocked ($subject, $isLocked) {
463 // Get the points_data entry
464 $pointsData = getPointsDataArrayFromSubject($subject);
466 // Regular points by default
467 $columnName = $pointsData['column_name'];
469 // Are the points locked?
470 if (($isLocked === true) && ($pointsData['locked_mode'] == 'LOCKED')) {
471 // Locked points, so prefix it
472 $columnName = 'locked_' . $pointsData['column_name'];
479 // Determines the payment method for given extension and 'locked'
480 function getPaymentMethodFromSubject ($subject) {
481 // Get the points_data entry
482 $pointsData = getPointsDataArrayFromSubject($subject);
484 // Regular points by default
485 $paymentMethod = $pointsData['payment_method'];
488 return $paymentMethod;
491 // Checks wether notification of points recipient is enabled
492 function isPaymentRecipientNotificationEnabled ($subject) {
493 // Get the points_data entry
494 $pointsData = getPointsDataArrayFromSubject($subject);
497 $isEnabled = ($pointsData['notify_recipient'] == 'Y');
503 // Update "referal table"
504 function updateReferalTable ($userid) {
505 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ' - ENTERED!');
507 loadReferalTable($userid);
509 // Add missing level > 1
510 addMissingReferalLevels($userid);
512 // The last step is to flush all userid's entries to the database
513 flushReferalTableToDatabase($userid);
516 rebuildCache('refsystem', 'refsystem');
518 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ' - EXIT!');
521 // Loads all referals for given userid
522 function loadReferalTable ($userid) {
523 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ' - ENTERED!');
525 $GLOBALS['referal_refid'][$userid] = array();
527 // Get all level entries from the refsystem table
528 $result = SQL_QUERY_ESC('SELECT `level` FROM `{?_MYSQL_PREFIX?}_refsystem` WHERE `userid`=%s ORDER BY `level` ASC',
529 array($userid), __FUNCTION__, __LINE__);
531 // Do we have entries?
532 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',SQL_NUMROWS()=' . SQL_NUMROWS($result));
533 if (SQL_NUMROWS($result) > 0) {
534 // Then walk through all levels
535 while (list($level) = SQL_FETCHROW($result)) {
537 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level);
538 $GLOBALS['referal_refid'][$userid][$level] = array();
542 // Load all referals of this user
543 $result_refs = SQL_QUERY_ESC('SELECT `userid` FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `refid`=%s ORDER BY `userid` ASC',
544 array($userid), __FUNCTION__, __LINE__);
546 // Do we have entries?
547 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ',SQL_NUMROWS()=' . SQL_NUMROWS($result_refs));
548 if (SQL_NUMROWS($result_refs) > 0) {
549 // Then again walk through all
550 while (list($refid) = SQL_FETCHROW($result_refs)) {
552 $GLOBALS['referal_refid'][$userid][$level][] = $refid;
553 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ',refid=' . $refid);
554 // Load the refid's array as well
555 loadReferalTable($refid);
560 SQL_FREERESULT($result_refs);
566 SQL_FREERESULT($result);
567 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ' - EXIT!');
570 // Adds missing referal levels to the array
571 function addMissingReferalLevels ($userid) {
572 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ' - ENTERED!');
573 // If the array is gone, you have called this function without calling loadReferalTable()
574 if (!isset($GLOBALS['referal_refid'][$userid])) {
575 // Please fix your code
576 debug_report_bug(__FUNCTION__, __LINE__, 'Called without calling loadReferalTable() before! userid=' . $userid);
578 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',count()=' . count($GLOBALS['referal_refid'][$userid]));
580 // Now walk through the array, first levels
581 foreach ($GLOBALS['referal_refid'][$userid] as $level=>$levelArray) {
582 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ',count()=' . count($levelArray));
583 // Next are the users
584 foreach ($levelArray as $refid) {
585 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ',refid=' . $refid);
586 // Does the refid have an array?
587 if (isset($GLOBALS['referal_refid'][$refid])) {
588 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ',refid=' . $refid . ',count()=' . count($GLOBALS['referal_refid'][$refid]));
589 // Okay, then walk through here, too
590 foreach ($GLOBALS['referal_refid'][$refid] as $refLevel=>$refArray) {
591 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ',refid=' . $refid . ',refLevel=' . $refLevel . ',count()=' . count($refArray));
592 // Also walk through this one
593 foreach ($refArray as $refRefid) {
594 // Calculate new level
595 $newLevel = $level + $refLevel;
596 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ',refid=' . $refid . ',refLevel=' . $refLevel . ',refRefid=' . $refRefid . ',newLevel=' . $newLevel);
597 // Is the refRefid not in?
598 if ((!isset($GLOBALS['referal_refid'][$userid][$newLevel])) || (!in_array($refRefid, $GLOBALS['referal_refid'][$userid][$newLevel]))) {
599 // Then we must add this ref's refid to the userid's next level
600 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',newLevel=' . $newLevel . ',refRefid=' . $refRefid . ' - ADDED!');
601 $GLOBALS['referal_refid'][$userid][$newLevel][] = $refRefid;
608 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ' - EXIT!');
611 // Flush all entries for given userid to database
612 function flushReferalTableToDatabase ($userid) {
613 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ' - ENTERED!');
614 // If the array is gone, you have called this function without calling loadReferalTable()
615 if (!isset($GLOBALS['referal_refid'][$userid])) {
616 // Please fix your code
617 debug_report_bug(__FUNCTION__, __LINE__, 'Called without calling loadReferalTable() before! userid=' . $userid);
619 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',count()=' . count($GLOBALS['referal_refid'][$userid]));
621 // If no entries are there, skip this whole step
622 if (count($GLOBALS['referal_refid'][$userid]) == 0) {
624 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ' - ABORTING...');
629 $SQL = 'INSERT INTO `{?_MYSQL_PREFIX?}_user_refs` (`userid`,`level`,`refid`) VALUES ';
632 // Now walk through the array, first levels
633 foreach ($GLOBALS['referal_refid'][$userid] as $level=>$levelArray) {
634 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ',count()=' . count($levelArray));
635 // Next are the users
636 foreach ($levelArray as $refid) {
637 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ',refid=' . $refid);
638 // Query the user_refs table
639 list($count) = SQL_FETCHROW(SQL_QUERY_ESC('SELECT COUNT(`id`) AS `count` FROM `{?_MYSQL_PREFIX?}_user_refs` WHERE `userid`=%s AND `level`=%s AND `refid`=%s LIMIT 1',
644 ), __FUNCTION__, __LINE__));
646 // Do we have no entry?
648 // Then add it to the SQL
649 $SQL .= '(' . $userid . ',' . $level . ',' . $refid . '),';
651 // Some has been added, so execute the query
657 // Remove last comma from SQL
658 $SQL = substr($SQL, 0, -1);
661 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',SQL=' . $SQL);
662 if ($executeSql === true) {
663 SQL_QUERY($SQL, __FUNCTION__, __LINE__);
666 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ' - EXIT!');