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 = '0') {
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 // Handle refback here if extension is installed
308 // @TODO Rewrite this to a filter
309 if (isExtensionActive('refback')) {
310 updateRefbackTable($userid);
312 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',ref=' . makeZeroToNull($ref) . ',level=' . makeZeroToNull($GLOBALS['cache_array']['ref_level'][$ref]) . ' - EXIT!');
315 // Subtract points from database and mediadata cache
316 function subtractPoints ($subject, $userid, $points) {
317 // Add points to used points
318 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `used_points`=`used_points`+%s WHERE `userid`=%s LIMIT 1",
322 ), __FUNCTION__, __LINE__);
324 // Prepare filter data
326 'subject' => $subject,
330 'added' => (!SQL_HASZEROAFFECTED())
333 // Insert booking record
334 $filterData = runFilterChain('post_sub_points', $filterData);
337 return $filterData['added'];
339 // "Getter" for array for user refs and points in given level
340 function getUserReferalPoints ($userid, $level) {
341 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ' - ENTERED!');
342 // Default is no refs and no nickname
345 // Get refs from database
346 $result = SQL_QUERY_ESC("SELECT
347 ur.`id`, ur.`refid`, ud.`status`, ud.`last_online`, ud.`mails_confirmed`, ud.`emails_received`
349 `{?_MYSQL_PREFIX?}_user_refs` AS ur
351 `{?_MYSQL_PREFIX?}_user_points` AS up
353 ur.refid=up.userid AND
354 (ur.level=0 OR ur.level IS NULL)
356 `{?_MYSQL_PREFIX?}_user_data` AS ud
358 ur.`refid`=ud.`userid`
367 ), __FUNCTION__, __LINE__);
369 // Are there some entries?
370 if (!SQL_HASZERONUMS($result)) {
372 while ($row = SQL_FETCHARRAY($result)) {
373 // Get total points of this user
374 $row['points'] = getTotalPoints($row['refid']);
376 // Get unconfirmed mails
377 $row['unconfirmed'] = countSumTotalData($row['refid'], 'user_links', 'id', 'userid', true);
379 // Init click rate with zero
380 $row['click_rate'] = '0';
382 // Is at least one mail received?
383 if ($row['emails_received'] > 0) {
384 // Calculate click rate
385 $row['click_rate'] = ($row['mails_confirmed'] / $row['emails_received'] * 100);
388 // Activity is 'active' by default because if autopurge is not installed
389 $row['activity'] = '{--MEMBER_ACTIVITY_ACTIVE--}';
391 // Is autopurge installed and the user inactive?
392 if ((isExtensionActive('autopurge')) && ((time() - getApInactiveSince()) >= $row['last_online'])) {
394 $row['activity'] = '{--MEMBER_ACTIVITY_INACTIVE--}';
397 // Remove some entries
398 unset($row['mails_confirmed']);
399 unset($row['emails_received']);
400 unset($row['last_online']);
403 $refs[$row['id']] = $row;
408 SQL_FREERESULT($result);
411 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ' - EXIT!');
415 // Get points data for given subject
416 function getPointsDataArrayFromSubject ($subject) {
417 // Extension sql_patches must be up-to-date
418 if (isExtensionInstalledAndOlder('sql_patches', '0.8.2')) {
419 // Please update ext-sql_patches
420 debug_report_bug(__FUNCTION__, __LINE__, 'sql_patches is out-dated. Please update to at least 0.8.2 to continue. subject=' . $subject);
423 // Remove any double-dot from it
424 $subjectArray = explode(':', $subject);
425 $subject = $subjectArray[0];
426 unset($subjectArray);
428 // If we have cache, shortcut it here
429 if (isset($GLOBALS['cache_array']['points_data'][$subject])) {
431 return $GLOBALS['cache_array']['points_data'][$subject];
434 // Now checkout the entry in database table
435 $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",
436 array($subject), __FUNCTION__, __LINE__);
438 // Do we have an entry?
439 if (SQL_NUMROWS($result) == 1) {
441 $pointsData = SQL_FETCHARRAY($result);
443 // Add all entries to our cache array
444 foreach ($pointsData as $key=>$value) {
445 $GLOBALS['cache_array']['points_data'][$subject][$key] = $value;
448 // Register this automatically
449 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_points_data` (`subject`, `column_name`, `locked_mode`, `payment_method`, `notify_recipient`) VALUES ('%s','points','LOCKED','REFERAL','N')",
450 array($subject), __FUNCTION__, __LINE__);
453 return getPointsDataArrayFromSubject($subject);
457 SQL_FREERESULT($result);
460 return $GLOBALS['cache_array']['points_data'][$subject];
463 // Determines the right points column name for given subject and 'locked'
464 function getPointsColumnNameFromSubjectLocked ($subject, $isLocked) {
465 // Get the points_data entry
466 $pointsData = getPointsDataArrayFromSubject($subject);
468 // Regular points by default
469 $columnName = $pointsData['column_name'];
471 // Are the points locked?
472 if (($isLocked === true) && ($pointsData['locked_mode'] == 'LOCKED')) {
473 // Locked points, so prefix it
474 $columnName = 'locked_' . $pointsData['column_name'];
481 // Determines the payment method for given extension and 'locked'
482 function getPaymentMethodFromSubject ($subject) {
483 // Get the points_data entry
484 $pointsData = getPointsDataArrayFromSubject($subject);
486 // Regular points by default
487 $paymentMethod = $pointsData['payment_method'];
490 return $paymentMethod;
493 // Checks wether notification of points recipient is enabled
494 function isPaymentRecipientNotificationEnabled ($subject) {
495 // Get the points_data entry
496 $pointsData = getPointsDataArrayFromSubject($subject);
499 $isEnabled = ($pointsData['notify_recipient'] == 'Y');