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 * Needs to be in all Files and every File needs "svn propset *
18 * svn:keywords Date Revision" (autoprobset!) at least!!!!!! *
19 * -------------------------------------------------------------------- *
20 * Copyright (c) 2003 - 2009 by Roland Haeder *
21 * Copyright (c) 2009, 2010 by Mailer Developer Team *
22 * For more information visit: http://www.mxchange.org *
24 * This program is free software; you can redistribute it and/or modify *
25 * it under the terms of the GNU General Public License as published by *
26 * the Free Software Foundation; either version 2 of the License, or *
27 * (at your option) any later version. *
29 * This program is distributed in the hope that it will be useful, *
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
32 * GNU General Public License for more details. *
34 * You should have received a copy of the GNU General Public License *
35 * along with this program; if not, write to the Free Software *
36 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
38 ************************************************************************/
40 // Some security stuff...
41 if (!defined('__SECURITY')) {
45 // Payback refback for refid and reduce it for current user
46 function addRefbackPoints ($userid, $ref, $points, $ref_points) {
47 //* DEBUG: */ print("----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n");
49 $GLOBALS['refback_depth'][$userid][$ref] = $GLOBALS['ref_level'];
55 $result_refs = SQL_QUERY('SELECT `level`, `percents` FROM `{?_MYSQL_PREFIX?}_refdepths` WHERE `level` > 0 ORDER BY `level` ASC',
56 __FUNCTION__, __LINE__);
58 // "Walk" through all level
59 while ($content = SQL_FETCHARRAY($result_refs)) {
61 // @TODO Try to rewrite the following unset()
62 unset($GLOBALS['ref_level']);
64 // "Walk" through all refids
65 //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):userid={$userid},ref={$ref},level={$content['level']},points={$points}<br />");
66 foreach (getArrayFromRefbackLevel($userid, $content['level']) as $refid) {
67 // Skip level zero or if both are the same
68 if ($userid == $refid) continue;
70 // Get refback percents
71 $percents = getRefbackPercents($userid, $refid);
72 //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):userid={$userid},ref={$ref},refid={$refid},points={$points},percents={$percents}<br />");
74 // Some percents given?
76 //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):points={$points},perc={$content['percents']},percents={$percents},level={$content['level']}<br />");
77 // Calculate points for refback
78 $refback = $points * ($content['percents'] / 100) * ($percents / 100);
80 // Update refback table ('refid' and 'userid' must be exchanged!)
81 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_refs` SET `points`=`points`+%s WHERE `userid`=%s AND `refid`=%s LIMIT 1",
82 array($refback, $refid, $userid), __FUNCTION__, __LINE__);
83 //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):refback={$refback},userid={$userid},refid={$refid} - UPDATE! (".SQL_AFFECTEDROWS().")<br />");
85 // Add points again, but only directly
86 //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):refback={$refback}<br />");
87 addPointsDirectly(sprintf("refback:%s", $refid), $userid, $refback);
89 // Reduce points if refid is found
91 // Reduce points here!
92 $return = $points * ($content['percents'] / 100) - $refback;
93 //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):points={$return} - REDUCED<br />");
100 SQL_FREERESULT($result_refs);
103 $GLOBALS['ref_level'] = $GLOBALS['refback_depth'][$userid][$ref];
106 //* DEBUG: */ print("</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />");
110 // "Getter" for refback percents
111 function getRefbackPercents ($userid, $ref) {
112 //* DEBUG: */ print("----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n");
113 //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):userid={$userid},ref={$ref}<br />");
114 // Skip identical ids
115 if ($userid == $ref) return 0;
118 if (!isset($GLOBALS['refback_percents'][$userid][$ref])) {
120 $GLOBALS['refback_percents'][$userid][$ref] = '0';
122 // Get percents from database
123 $result = SQL_QUERY_ESC("SELECT `refback` FROM `{?_MYSQL_PREFIX?}_user_refs` WHERE `userid`=%s AND `refid`=%s LIMIT 1",
124 array($ref, $userid), __FUNCTION__, __LINE__);
126 // Entry found? (Should be!)
127 if (SQL_NUMROWS($result) == 1) {
129 list($GLOBALS['refback_percents'][$userid][$ref]) = SQL_FETCHROW($result);
130 //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):percents=".$GLOBALS['refback_percents'][$userid][$ref]."<br />");
133 //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>): NO ENTRY FOUND! :-(<br />");
134 logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',ref=' . $ref . ' - No entry found! :-(');
138 SQL_FREERESULT($result);
142 //* DEBUG: */ print("</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />");
143 return $GLOBALS['refback_percents'][$userid][$ref];
146 // "Getter" for userid array which will return only one entry
147 function getArrayFromRefbackLevel ($rid, $level) {
148 //* DEBUG: */ print("----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n");
149 //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):rid={$rid},level={$level}<br />");
154 $result = SQL_QUERY_ESC("SELECT
157 `{?_MYSQL_PREFIX?}_user_refs`
159 `refid`=%s AND `level`=%s
162 array($rid, $level), __FUNCTION__, __LINE__);
165 //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):numRows=".SQL_NUMROWS($result)."<br />");
166 if (SQL_NUMROWS($result) > 0) {
168 while ($content = SQL_FETCHARRAY($result)) {
169 //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):rid={$rid},level={$level},userid={$content['userid']}<br />");
170 $userIds[] = $content['userid'];
175 SQL_FREERESULT($result);
178 //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):rid={$rid},count()=".count($userIds)."<br />");
179 //* DEBUG: */ print("</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />");
183 // Update "refback table"
184 function updateRefbackTable ($userid) {
185 //* DEBUG: */ print("----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n");
186 // Make it sure referal level zero (member him-/herself) is at least selected
187 if (empty($GLOBALS['refback_level'])) $GLOBALS['refback_level'] = 1;
188 //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):userid={$userid},level={$GLOBALS['refback_level']}<br />");
191 $GLOBALS['refback_refid'][$GLOBALS['refback_level']] = '0';
192 $old = '0'; $minus = '0';
194 // Check for his referal
195 $result_refid = SQL_QUERY_ESC("SELECT `userid` FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `refid`=%s ORDER BY `userid` ASC",
196 array(bigintval($userid)), __FUNCTION__, __LINE__);
198 // When no entry was updated then we have to create it here
199 //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):found=".SQL_NUMROWS($result_refid)."<br />");
200 if (SQL_NUMROWS($result_refid) > 0) {
202 while (list($GLOBALS['refback_refid'][$GLOBALS['refback_level']]) = SQL_FETCHROW($result_refid)) {
204 $GLOBALS['refback_userid'][$GLOBALS['refback_refid'][$GLOBALS['refback_level']]] = $userid;
207 //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):userid={$userid},ref={$GLOBALS['refback_refid'][$GLOBALS['refback_level']]}<br />");
208 if (($GLOBALS['refback_refid'][$GLOBALS['refback_level']] > 0) && ($GLOBALS['refback_refid'][$GLOBALS['refback_level']] != $userid) && (isset($GLOBALS['refback_refid'][$GLOBALS['refback_level']]))) {
210 if ((isset($GLOBALS['refback_refid'][$GLOBALS['refback_level']-1])) && (isset($GLOBALS['refback_userid'][$GLOBALS['refback_refid'][$GLOBALS['refback_level']-1]]))) {
213 $userid = $GLOBALS['refback_userid'][$GLOBALS['refback_refid'][$GLOBALS['refback_level']-1]];
215 //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):userid={$userid},old={$old},level={$GLOBALS['refback_level']},ref={$GLOBALS['refback_refid'][$GLOBALS['refback_level']]} - NEW UID!<br />");
219 $result = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_user_refs` WHERE `userid`=%s AND `level`='%s' AND `refid`=%s LIMIT 1",
220 array(bigintval($userid), $GLOBALS['refback_level'], bigintval($GLOBALS['refback_refid'][$GLOBALS['refback_level']])), __FUNCTION__, __LINE__);
222 // Do we have no entry?
223 //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):userid={$userid},level={$GLOBALS['refback_level']},ref={$GLOBALS['refback_refid'][$GLOBALS['refback_level']]},minus={$minus},numRows=".SQL_NUMROWS($result)." - FOUND!<br />");
224 if (SQL_HASZERONUMS($result)) {
226 //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):userid={$userid},level={$GLOBALS['refback_level']},ref={$GLOBALS['refback_refid'][$GLOBALS['refback_level']]} - ADD!<br />");
227 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_refs` (`userid`,`level`,`refid`) VALUES (%s,%s,%s)",
228 array(bigintval($userid), $GLOBALS['refback_level'], bigintval($GLOBALS['refback_refid'][$GLOBALS['refback_level']])), __FUNCTION__, __LINE__);
230 // Move to next referal level and count his counter one up!
231 $GLOBALS['refback_level']++; updateRefbackTable($GLOBALS['refback_refid'][($GLOBALS['refback_level'] - 1)]);
234 // Do we have another level here?
235 if ((($GLOBALS['refback_level']-$minus) > 0) && ($old > 0)) {
239 // Shall we add this as well?
240 $result = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_user_refs` WHERE `userid`=%s AND `level`='%s' AND `refid`=%s LIMIT 1",
241 array(bigintval($userid), ($GLOBALS['refback_level']-$minus), bigintval($GLOBALS['refback_refid'][$GLOBALS['refback_level']])), __FUNCTION__, __LINE__);
243 // Do we have no entry?
244 //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):userid={$userid},level=".($GLOBALS['refback_level']-$minus).",ref={$GLOBALS['refback_refid'][$GLOBALS['refback_level']]},numRows=".SQL_NUMROWS($result)." - BACK!<br />");
245 if (SQL_HASZERONUMS($result)) {
247 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_refs` (`userid`,`level`,`refid`) VALUES (%s,%s,%s)",
248 array(bigintval($userid), ($GLOBALS['refback_level']-$minus), bigintval($GLOBALS['refback_refid'][$GLOBALS['refback_level']])), __FUNCTION__, __LINE__);
249 //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):userid={$userid},level=".($GLOBALS['refback_level']-$minus).",ref={$GLOBALS['refback_refid'][$GLOBALS['refback_level']]} - RETURNED!<br />");
256 SQL_FREERESULT($result_refid);
259 // When he has a referal...
260 if ((($GLOBALS['refback_refid'][$GLOBALS['refback_level']] == $userid) || ($GLOBALS['refback_refid'][$GLOBALS['refback_level']] == '0')) && (isExtensionInstalledAndNewer('cache', '0.1.2')) && (!isset($GLOBALS['refback_cached']))) {
262 //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):userid={$userid},ref={$GLOBALS['refback_refid'][$GLOBALS['refback_level']]} - CACHE!<br />");
263 if ($GLOBALS['cache_instance']->loadCacheFile('refback')) $GLOBALS['cache_instance']->removeCacheFile();
264 $GLOBALS['refback_cached'] = 1;
268 $GLOBALS['refback_level']--;
269 //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):userid={$userid},level={$GLOBALS['refback_level']} - LEVEL!<br />");
272 if (!isset($GLOBALS['refback_refid'][$GLOBALS['refback_level']-1])) $GLOBALS['refback_refid'][$GLOBALS['refback_level']-1] = '0';
273 //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):userid={$userid},level={$GLOBALS['refback_level']},ref={$GLOBALS['refback_refid'][$GLOBALS['refback_level']-1]} - BACK!<br />");
274 //* DEBUG: */ print("</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />");
277 // "Getter" for array for user refs in given level
278 function getArrayFromRefbackUserRefs ($userid, $level) {
279 //* DEBUG: */ print("----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n");
280 // Default is no refs and no nickname
284 // Do we have nickname extension installed?
285 if (isExtensionActive('nickname')) {
286 $add = ", d.nickname";
289 // Get refs from database
290 $result = SQL_QUERY_ESC("SELECT
297 d.refid AS level_refid
299 `{?_MYSQL_PREFIX?}_user_refs` AS r
301 `{?_MYSQL_PREFIX?}_user_data` AS d
305 r.userid=%s AND r.level=%s
308 array(bigintval($userid), bigintval($level)), __FUNCTION__, __LINE__);
310 // Are there some entries?
311 if (SQL_NUMROWS($result) > 0) {
313 while ($row = SQL_FETCHARRAY($result)) {
315 $refs[$row['id']] = $row;
320 SQL_FREERESULT($result);
323 //* DEBUG: */ print("</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />");
327 // "Getter" for refback entry
328 function getArrayFromUserRefbackData ($id) {
329 //* DEBUG: */ print("----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n");
330 // Default data is nothing
334 $id = bigintval($id);
336 // Get entry from database
337 $result = SQL_QUERY_ESC("SELECT `id`, `userid`, `refid`, `refback`, `level`, `points` FROM `{?_MYSQL_PREFIX?}_user_refs` WHERE `id`=%s AND `userid`=%s LIMIT 1",
338 array($id, getMemberId()), __FUNCTION__, __LINE__);
340 // Is there an entry?
341 if (SQL_NUMROWS($result) == 1) {
343 $data = SQL_FETCHARRAY($result);
347 SQL_FREERESULT($result);
350 //* DEBUG: */ print("</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />");
354 // Update refback percents (but with some sanity-checks!
355 function updateMemberRefbackPercents ($id, $percents) {
356 //* DEBUG: */ print("----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n");
357 // Default status is failed
358 $status['message'] = '{--MEMBER_REFBACK_ERROR_GENERAL--}';
360 // Do some sanity-checks
363 $status['message'] = '{--MEMBER_REFBACK_ERROR_NO_MEMBER--}';
365 } elseif (''.$id.'' != ''.($id + 0).'') {
367 $status['message'] = '{--MEMBER_REFBACK_ERROR_INVALID_ID_NUMBER--}';
369 } elseif (($percents < 0) || ($percents > 100)) {
370 // Percentage is not valid!
371 $status['message'] = '{--MEMBER_REFBACK_ERROR_INVALID_PERCENTAGE--}';
376 $dummy = getArrayFromUserRefbackData($id);
379 if (count($dummy) == 0) {
380 // id does not belong to user!
381 $status['message'] = '{--MEMBER_REFBACK_ERROR_ID_MISMATCH--}';
385 // Revert german commata
386 $percents = convertCommaToDot($percents);
389 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_refs` SET `refback`='%s' WHERE `id`=%s AND `userid`=%s LIMIT 1",
390 array($percents, $id, getMemberId()), __FUNCTION__, __LINE__);
393 if (SQL_AFFECTEDROWS() < 1) {
394 // Entry not updated!
395 $status['message'] = '{--MEMBER_REFBACK_ERROR_NOT_UPDATED--}';
399 // Prepare email content
400 $dummy['percents'] = $percents;
402 // Load member email template
403 $mail = loadEmailTemplate('member_refback', $dummy, $dummy['refid']);
405 // Send email to user
406 sendEmail($dummy['refid'], '{--MEMBER_REFBACK_SUBJECT--}', $mail);
408 // Send admin notification
409 sendAdminNotification(getMessage('ADMIN_REFBACK_SUBJECT'), 'admin_refback', $dummy, getMemberId());
412 $status['ok'] = true;
414 // Return status array
415 //* DEBUG: */ print("</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />");