2 /************************************************************************
3 * Mailer v0.2.1-FINAL Start: 09/28/2008 *
4 * =================== Last change: 09/28/2008 *
6 * -------------------------------------------------------------------- *
7 * File : refback_functions.php *
8 * -------------------------------------------------------------------- *
9 * Short description : Functions for refback extension *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Funktionen fuer Refback-Erweiterung *
12 * -------------------------------------------------------------------- *
15 * $Tag:: 0.2.1-FINAL $ *
17 * -------------------------------------------------------------------- *
18 * Copyright (c) 2003 - 2009 by Roland Haeder *
19 * Copyright (c) 2009 - 2015 by Mailer Developer Team *
20 * For more information visit: http://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 // Payback refback for refid and reduce it for current user
44 function addRefbackPoints ($userid, $ref, $points, $ref_points) {
45 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',ref=' . $ref . ',points=' . $points . ',ref_points=' . $ref_points . ' - ENTERED!');
47 $GLOBALS['refback_depth'][$userid][$ref] = $GLOBALS['ref_level'];
50 $return = $ref_points;
53 $result_refs = sqlQuery('SELECT `level`, `percents` FROM `{?_MYSQL_PREFIX?}_refdepths` WHERE `level` > 0 ORDER BY `level` ASC',
54 __FUNCTION__, __LINE__);
56 // "Walk" through all level
57 while ($content = sqlFetchArray($result_refs)) {
58 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'level=' . $content['level'] . ',percents=' . $content['percents'] . ' - LOOP START!');
63 $refids = getArrayFromRefbackLevel($userid, $content['level']);
65 // "Walk" through all refids
66 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',ref=' . $ref . ',level=' . $content['level'] . ',percents=' . $content['percents'] . ',points=' . $points . ' - LOOP START!');
67 foreach ($refids as $refid) {
69 * Get refback percents, do not swap $refid and $userid by the next
70 * call. If you do so, you will check the wrong direction, $userid
71 * is always bigger than $refid.
73 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',refid=' . convertNullToZero($refid) . ' - CALLING getRefbackPercents()');
74 $content['refback_percents'] = getRefbackPercents($refid, $userid);
75 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',refid=' . convertNullToZero($refid) . ',refback_percents=' . $content['refback_percents']);
77 // Some percents given?
78 if ($content['refback_percents'] > 0) {
79 // Calculate points for refback
80 $refbackPoints = $points * ($content['percents'] / 100) * ($content['refback_percents'] / 100);
81 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'points=' . $points . ',percents=' . $content['percents'] . ',refback_percents=' . $content['refback_percents'] . ',level=' . $content['level'] . ',refback_points=' . $refbackPoints);
83 // Update refback table ('refid' and 'userid' must be exchanged!)
84 sqlQueryEscaped('UPDATE
85 `{?_MYSQL_PREFIX?}_user_refs`
87 `refback_points`=`refback_points`+%s
96 ), __FUNCTION__, __LINE__);
98 // Something must have been updated
99 if (ifSqlHasZeroAffectedRows()) {
100 // Please report this bug
101 reportBug(__FUNCTION__, __LINE__, 'Nothing has been updated in user_refs,userid=' . $userid . ',refid=' . convertNullToZero($refid) . ',refbackPoints=' . $refbackPoints);
104 // Add points again, but only directly
105 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'refbackPoints=' . $refbackPoints . ',userid=' . $userid . ',ref=' . $ref . ',refid=' . convertNullToZero($refid) . ' - REFBACK-UPDATE! (' . sqlAffectedRows() . ')');
106 addPointsThroughReferralSystem(sprintf('refback:%s', $refid), $userid, $refbackPoints);
108 // Reduce points if refid is found
109 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',refid=' . $refid . ',ref=' . $ref . ' - REDUCING?');
110 if ($refid == $ref) {
111 // Reduce points here!
112 $return = $points * ($content['percents'] / 100) - $refbackPoints;
113 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'return=' . $return . ' - REDUCED!');
120 sqlFreeResult($result_refs);
123 $GLOBALS['ref_level'] = $GLOBALS['refback_depth'][$userid][$ref];
126 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',ref=' . $ref . ',points=' . $points . ',ref_points=' . $ref_points . ',return=' . $return . ' - EXIT!');
131 * "Getter" for refback percents
133 * @param $userid "Current" user's userid
134 * @param $refid $userid has this userid as referral
135 * @return $percents Refback percents $userid has given to $refid
137 function getRefbackPercents ($userid, $refid) {
138 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',refid=' . $refid . ' - ENTERED!');
139 if ((!isValidId($userid)) || (!isValidId($refid))) {
140 // userid and/or refid is invalid
141 reportBug(__FUNCTION__, __LINE__, 'userid=' . convertNullToZero($userid) . ', refid=' . convertNullToZero($refid) . ' - Both must be > 0');
142 } elseif ($userid == $refid) {
143 // Skip identical ids
148 if (!isset($GLOBALS['refback_percents'][$userid][$refid])) {
150 $GLOBALS['refback_percents'][$userid][$refid] = '0';
152 // Get percents from database
153 $result = sqlQueryEscaped("SELECT `refback_percents` FROM `{?_MYSQL_PREFIX?}_user_refs` WHERE `userid`=%s AND `refid`=%s LIMIT 1",
154 array($userid, $refid), __FUNCTION__, __LINE__);
156 // Entry found? (Should be!)
157 if (sqlNumRows($result) == 1) {
159 list($GLOBALS['refback_percents'][$userid][$refid]) = sqlFetchRow($result);
160 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',refid=' . $refid . ',percents=' . $GLOBALS['refback_percents'][$userid][$refid]);
162 // Please report all findings
163 reportBug(__FUNCTION__, __LINE__, 'userid=' . $userid . ',refid=' . $refid . ' - No entry found. :-(');
167 sqlFreeResult($result);
171 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',refid=' . $refid . ',refback_percents=' . $GLOBALS['refback_percents'][$userid][$refid] . ' - EXIT!');
172 return $GLOBALS['refback_percents'][$userid][$refid];
175 // "Getter" for userid array which will return only one entry
176 function getArrayFromRefbackLevel ($refid, $level) {
177 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'refid=' . convertNullToZero($refid) . ',level=' . $level . ' - ENTERED!');
182 $result = sqlQueryEscaped("SELECT
185 `{?_MYSQL_PREFIX?}_user_refs`
191 array($refid, $level), __FUNCTION__, __LINE__);
194 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'numRows=' . sqlNumRows($result));
195 if (!ifSqlHasZeroNums($result)) {
197 while ($content = sqlFetchArray($result)) {
198 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'refid=' . convertNullToZero($refid) . ',level=' . $level . ',userid=' . $content['userid']);
199 array_push($userIds, $content['userid']);
204 sqlFreeResult($result);
207 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'refid=' . convertNullToZero($refid) . ',userIds()=' . count($userIds) . ' - EXIT!');
211 // "Getter" for array for user refs in given level
212 function getArrayFromRefbackUserRefs ($userid, $level) {
213 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ' - ENTERED!');
214 // Default is no refs and no nickname
217 // Get refs from database
218 $result = sqlQueryEscaped("SELECT
221 `r`.`refback_percents`,
222 `r`.`refback_points`,
225 `d`.`mails_confirmed`,
226 `d`.`emails_received`,
227 `d`.`refid` AS `level_refid`
229 `{?_MYSQL_PREFIX?}_user_refs` AS `r`
231 `{?_MYSQL_PREFIX?}_user_data` AS `d`
233 `r`.`refid`=`d`.`userid`
239 array(bigintval($userid), bigintval($level)), __FUNCTION__, __LINE__);
241 // Are there some entries?
242 if (!ifSqlHasZeroNums($result)) {
244 while ($row = sqlFetchArray($result)) {
245 // Calculate click rate
246 $row['click_rate'] = calculatePercentageRate($row['mails_confirmed'], $row['emails_received']);
249 $refs[$row['id']] = $row;
254 sqlFreeResult($result);
257 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ',refs()=' . count($refs) . ' - EXIT!');
261 // "Getter" for refback entry
262 function getArrayFromUserRefbackData ($id) {
263 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'id=' . $id . ' - ENTERED!');
264 // Default data is nothing
268 $id = bigintval($id);
270 // Get entry from database
271 $result = sqlQueryEscaped("SELECT `id`, `userid`, `refid`, `refback_percents`, `level`, `refback_points` FROM `{?_MYSQL_PREFIX?}_user_refs` WHERE `id`=%s AND `userid`=%s LIMIT 1",
272 array($id, getMemberId()), __FUNCTION__, __LINE__);
274 // Is there an entry?
275 if (sqlNumRows($result) == 1) {
277 $data = sqlFetchArray($result);
281 sqlFreeResult($result);
284 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'id=' . $id . ',data()=' . count($data) . ' - EXIT!');
288 // Update refback percents (but with some sanity-checks!
289 function updateMemberRefbackPercents ($id, $percents, $note) {
290 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'id=' . $id . ',percents=' . $percents . ' - ENTERED!');
291 // Default status is failed
292 $status['message'] = 'MEMBER_REFBACK_ERROR_GENERAL';
294 // Do some sanity-checks
297 $status['message'] = 'MEMBER_REFBACK_ERROR_NO_MEMBER';
299 } elseif (''.$id.'' != ''.($id + 0).'') {
301 $status['message'] = 'MEMBER_REFBACK_ERROR_INVALID_ID_NUMBER';
303 } elseif (($percents < 0) || ($percents > 100)) {
304 // Percentage is not valid!
305 $status['message'] = 'MEMBER_REFBACK_ERROR_INVALID_PERCENTAGE';
310 $content = getArrayFromUserRefbackData($id);
313 if (!isFilledArray($content)) {
314 // id does not belong to user!
315 $status['message'] = 'MEMBER_REFBACK_ERROR_ID_MISMATCH';
319 // Revert german commata
320 $percents = convertCommaToDot($percents);
323 sqlQueryEscaped("UPDATE `{?_MYSQL_PREFIX?}_user_refs` SET `refback_percents`='%s' WHERE `id`=%s AND `userid`=%s LIMIT 1",
328 ), __FUNCTION__, __LINE__);
331 if (ifSqlHasZeroAffectedRows()) {
332 // Entry not updated!
333 $status['message'] = 'MEMBER_REFBACK_ERROR_NOT_UPDATED';
337 // Prepare email content
338 $content['refback_percents'] = $percents;
339 $content['refback_notice'] = $note;
341 // Load member email template
342 $mail = loadEmailTemplate('member_refback', $content, $content['refid']);
344 // Send email to user
345 sendEmail($content['refid'], '{--MEMBER_REFBACK_SUBJECT--}', $mail);
347 // Send admin notification
348 sendAdminNotification('{--ADMIN_REFBACK_SUBJECT--}', 'admin_refback', $content, $content['refid']);
351 $status['ok'] = TRUE;
353 // Return status array
354 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'id=' . $id . ',percents=' . $percents . ' - EXIT!');
358 // ----------------------------------------------------------------------------
359 // Wrapper functions for configuration entries
360 // ----------------------------------------------------------------------------
362 // Getter for refback_enabled
363 function getRefbackEnabled () {
365 if (!isset($GLOBALS[__FUNCTION__])) {
367 $GLOBALS[__FUNCTION__] = getConfig('refback_enabled');
371 return $GLOBALS[__FUNCTION__];
374 // Checks whether refback is enabled
375 function isRefbackEnabled () {
377 if (!isset($GLOBALS[__FUNCTION__])) {
379 $GLOBALS[__FUNCTION__] = (getRefbackEnabled() == 'Y');
383 return $GLOBALS[__FUNCTION__];
386 // Getter for refback_max_perc
387 function getRefbackMaxPerc () {
389 if (!isset($GLOBALS[__FUNCTION__])) {
391 $GLOBALS[__FUNCTION__] = getConfig('refback_max_perc');
395 return $GLOBALS[__FUNCTION__];
398 // Getter for refback_min_perc
399 function getRefbackMinPerc () {
401 if (!isset($GLOBALS[__FUNCTION__])) {
403 $GLOBALS[__FUNCTION__] = getConfig('refback_min_perc');
407 return $GLOBALS[__FUNCTION__];