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 - 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 // 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 = SQL_QUERY('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 = SQL_FETCHARRAY($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=' . makeNullToZero($refid) . ' - CALLING getRefbackPercents()');
74 $content['refback_percents'] = getRefbackPercents($refid, $userid);
75 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',refid=' . makeNullToZero($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!)
85 `{?_MYSQL_PREFIX?}_user_refs`
87 `refback_points`=`refback_points`+%s
96 ), __FUNCTION__, __LINE__);
98 // Something must have been updated
99 if (SQL_HASZEROAFFECTED()) {
100 // Please report this bug
101 debug_report_bug(__FUNCTION__, __LINE__, 'Nothing has been updated in user_refs,userid=' . $userid . ',refid=' . makeNullToZero($refid) . ',refbackPoints=' . $refbackPoints);
104 // Add points again, but only directly
105 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'refbackPoints=' . $refbackPoints . ',userid=' . $userid . ',ref=' . $ref . ',refid=' . makeNullToZero($refid) . ' - REFBACK-UPDATE! (' . SQL_AFFECTEDROWS() . ')');
106 addPointsThroughReferalSystem(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 SQL_FREERESULT($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 referal
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 ((!isValidUserId($userid)) || (!isValidUserId($refid))) {
140 // userid and/or refid is invalid
141 debug_report_bug(__FUNCTION__, __LINE__, 'userid=' . makeNullToZero($userid) . ', refid=' . makeNullToZero($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 = SQL_QUERY_ESC("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 (SQL_NUMROWS($result) == 1) {
159 list($GLOBALS['refback_percents'][$userid][$refid]) = SQL_FETCHROW($result);
160 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',refid=' . $refid . ',percents=' . $GLOBALS['refback_percents'][$userid][$refid]);
162 // Please report all findings
163 debug_report_bug(__FUNCTION__, __LINE__, 'userid=' . $userid . ',refid=' . $refid . ' - No entry found. :-(');
167 SQL_FREERESULT($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=' . makeNullToZero($refid) . ',level=' . $level . ' - ENTERED!');
182 $result = SQL_QUERY_ESC("SELECT
185 `{?_MYSQL_PREFIX?}_user_refs`
191 array($refid, $level), __FUNCTION__, __LINE__);
194 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'numRows=' . SQL_NUMROWS($result));
195 if (!SQL_HASZERONUMS($result)) {
197 while ($content = SQL_FETCHARRAY($result)) {
198 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'refid=' . makeNullToZero($refid) . ',level=' . $level . ',userid=' . $content['userid']);
199 $userIds[] = $content['userid'];
204 SQL_FREERESULT($result);
207 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'refid=' . makeNullToZero($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 = SQL_QUERY_ESC("SELECT
221 r.`refback_percents`,
227 d.`refid` AS `level_refid`
229 `{?_MYSQL_PREFIX?}_user_refs` AS r
231 `{?_MYSQL_PREFIX?}_user_data` AS d
239 array(bigintval($userid), bigintval($level)), __FUNCTION__, __LINE__);
241 // Are there some entries?
242 if (!SQL_HASZERONUMS($result)) {
244 while ($row = SQL_FETCHARRAY($result)) {
246 $row['click_rate'] = '0';
248 // Does he have received some mails?
249 if ($row['emails_received'] > 0) {
250 // Calculate click rate
251 $row['click_rate'] = ($row['mails_confirmed'] / $row['emails_received'] * 100);
255 $refs[$row['id']] = $row;
260 SQL_FREERESULT($result);
263 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ',refs()=' . count($refs) . ' - EXIT!');
267 // "Getter" for refback entry
268 function getArrayFromUserRefbackData ($id) {
269 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'id=' . $id . ' - ENTERED!');
270 // Default data is nothing
274 $id = bigintval($id);
276 // Get entry from database
277 $result = SQL_QUERY_ESC("SELECT `id`, `userid`, `refid`, `refback_percents`, `level`, `refback_points` FROM `{?_MYSQL_PREFIX?}_user_refs` WHERE `id`=%s AND `userid`=%s LIMIT 1",
278 array($id, getMemberId()), __FUNCTION__, __LINE__);
280 // Is there an entry?
281 if (SQL_NUMROWS($result) == 1) {
283 $data = SQL_FETCHARRAY($result);
287 SQL_FREERESULT($result);
290 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'id=' . $id . ',data()=' . count($data) . ' - EXIT!');
294 // Update refback percents (but with some sanity-checks!
295 function updateMemberRefbackPercents ($id, $percents, $note) {
296 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'id=' . $id . ',percents=' . $percents . ' - ENTERED!');
297 // Default status is failed
298 $status['message'] = 'MEMBER_REFBACK_ERROR_GENERAL';
300 // Do some sanity-checks
303 $status['message'] = 'MEMBER_REFBACK_ERROR_NO_MEMBER';
305 } elseif (''.$id.'' != ''.($id + 0).'') {
307 $status['message'] = 'MEMBER_REFBACK_ERROR_INVALID_ID_NUMBER';
309 } elseif (($percents < 0) || ($percents > 100)) {
310 // Percentage is not valid!
311 $status['message'] = 'MEMBER_REFBACK_ERROR_INVALID_PERCENTAGE';
316 $dummy = getArrayFromUserRefbackData($id);
319 if (count($dummy) == 0) {
320 // id does not belong to user!
321 $status['message'] = 'MEMBER_REFBACK_ERROR_ID_MISMATCH';
325 // Revert german commata
326 $percents = convertCommaToDot($percents);
329 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_refs` SET `refback_percents`='%s' WHERE `id`=%s AND `userid`=%s LIMIT 1",
334 ), __FUNCTION__, __LINE__);
337 if (SQL_HASZEROAFFECTED()) {
338 // Entry not updated!
339 $status['message'] = 'MEMBER_REFBACK_ERROR_NOT_UPDATED';
343 // Prepare email content
344 $dummy['refback_percents'] = $percents;
345 $dummy['refback_note'] = $note;
347 // Load member email template
348 $mail = loadEmailTemplate('member_refback', $dummy, $dummy['refid']);
350 // Send email to user
351 sendEmail($dummy['refid'], '{--MEMBER_REFBACK_SUBJECT--}', $mail);
353 // Send admin notification
354 sendAdminNotification('{--ADMIN_REFBACK_SUBJECT--}', 'admin_refback', $dummy, $dummy['refid']);
357 $status['ok'] = true;
359 // Return status array
360 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'id=' . $id . ',percents=' . $percents . ' - EXIT!');
364 // Checks wether refback is enabled
365 function isRefbackEnabled () {
367 if (!isset($GLOBALS[__FUNCTION__])) {
369 $GLOBALS[__FUNCTION__] = (getConfig('refback_enabled') == 'Y');
373 return $GLOBALS[__FUNCTION__];