2 /************************************************************************
3 * MXChange v0.2.1 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 * -------------------------------------------------------------------- *
14 * -------------------------------------------------------------------- *
15 * Copyright (c) 2003 - 2008 by Roland Haeder *
16 * For more information visit: http://www.mxchange.org *
18 * This program is free software; you can redistribute it and/or modify *
19 * it under the terms of the GNU General Public License as published by *
20 * the Free Software Foundation; either version 2 of the License, or *
21 * (at your option) any later version. *
23 * This program is distributed in the hope that it will be useful, *
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
26 * GNU General Public License for more details. *
28 * You should have received a copy of the GNU General Public License *
29 * along with this program; if not, write to the Free Software *
30 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
32 ************************************************************************/
34 // Some security stuff...
35 if (!defined('__SECURITY')) {
36 $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
40 // Payback refback for refid and reduce it for current user
41 function ADD_REFBACK_POINTS ($uid, $ref, $points, $ref_points) {
42 //* DEBUG: */ print "----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n";
43 global $DEPTH, $cacheArray;
46 $cacheArray['depth'][$uid][$ref] = $DEPTH;
52 $result_refs = SQL_QUERY("SELECT level, percents FROM "._MYSQL_PREFIX."_refdepths WHERE level > 0 ORDER BY level ASC", __FILE__, __LINE__);
54 // "Walk" through all level
55 while (list($level, $perc) = SQL_FETCHROW($result_refs)) {
59 // "Walk through all refids
60 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},ref={$ref},level={$level},points={$points}<br />\n";
61 foreach (GET_REFBACK_USERID_ARRAY($uid, $level) as $refid) {
62 // Skip level zero or if both are the same
63 if ($uid == $refid) continue;
65 // Get refback percents
66 $percents = GET_REFBACK_PERCENTS($uid, $refid);
67 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},ref={$ref},refid={$refid},points={$points},percents={$percents}<br />\n";
69 // Some percents given?
71 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):points={$points},perc={$perc},percents={$percents},level={$level}<br />\n";
72 // Get points for refback
73 $refback = $points * ($perc / 100) * ($percents / 100);
75 // Update refback table ("refid" and "uid" must be exchanged!)
76 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_refs SET points=points+%s WHERE userid=%s AND refid=%s LIMIT 1",
77 array($refback, $refid, $uid), __FILE__, __LINE__);
78 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):refback={$refback},uid={$uid},refid={$refid} - UPDATE! (".SQL_AFFECTEDROWS().")<br />\n";
80 // Add points again, but only directly
81 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):refback={$refback}<br />\n";
82 ADD_POINTS_REFSYSTEM($uid, $refback, false, "0", false, "direct");
84 // Reduce points if refid is found
86 // Reduce points here!
87 $return = $points * ($perc / 100) - $refback;
88 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):points={$return} - REDUCED<br />\n";
95 SQL_FREERESULT($result_refs);
98 $DEPTH = $cacheArray['depth'][$uid][$ref];
101 //* DEBUG: */ print "</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />\n";
104 // "Getter" for refback percents
105 function GET_REFBACK_PERCENTS ($uid, $ref) {
106 //* DEBUG: */ print "----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n";
107 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},ref={$ref}<br />\n";
108 // Skip identical ids
109 if ($uid == $ref) return 0;
114 // Get percents from database
115 $result = SQL_QUERY_ESC("SELECT refback FROM "._MYSQL_PREFIX."_user_refs WHERE userid=%s AND refid=%s LIMIT 1",
116 array($ref, $uid), __FILE__, __LINE__);
118 // Entry found? (Should be!)
119 if (SQL_NUMROWS($result) == 1) {
121 list($percents) = SQL_FETCHROW($result);
122 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):percents={$percents}<br />\n";
125 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>): NO ENTRY FOUND! :-(<br />\n";
126 DEBUG_LOG(__FUNCTION__.": uid={$uid},ref={$ref} - No entry found! :-(");
130 SQL_FREERESULT($result);
133 //* DEBUG: */ print "</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />\n";
136 // "Getter" for userid array which will return only one entry
137 function GET_REFBACK_USERID_ARRAY ($rid, $level) {
138 //* DEBUG: */ print "----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n";
139 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):rid={$rid},level={$level}<br />\n";
144 $result = SQL_QUERY_ESC("SELECT userid
145 FROM "._MYSQL_PREFIX."_user_refs
146 WHERE refid=%s AND level=%s
147 ORDER BY userid ASC",
148 array($rid,$level), __FILE__, __LINE__);
151 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):numRows=".SQL_NUMROWS($result)."<br />\n";
152 if (SQL_NUMROWS($result) > 0) {
154 while (list($userid) = SQL_FETCHROW($result)) {
155 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):rid={$rid},level={$level},userid={$userid}<br />\n";
156 $userIds[] = $userid;
161 SQL_FREERESULT($result);
164 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):rid={$rid},count()=".count($userIds)."<br />\n";
165 //* DEBUG: */ print "</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />\n";
168 // Update "refback table"
169 function UPDATE_REFBACK_TABLE($uid) {
170 //* DEBUG: */ print "----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n";
171 global $cacheArray, $cacheInstance;
173 // Make it sure referral level zero (member him-/herself) is at least selected
174 if (empty($cacheArray['back_level'])) $cacheArray['back_level'] = 1;
175 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},level={$cacheArray['back_level']}<br />\n";
178 $cacheArray['up_refid'][$cacheArray['back_level']] = 0;
179 $old = 0; $minus = 0;
181 // Check for his referral
182 $result_refid = SQL_QUERY_ESC("SELECT userid FROM "._MYSQL_PREFIX."_user_data WHERE refid=%s ORDER BY userid ASC",
183 array(bigintval($uid)), __FILE__, __LINE__);
185 // When no entry was updated then we have to create it here
186 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):found=".SQL_NUMROWS($result_refid)."<br />\n";
187 if (SQL_NUMROWS($result_refid) > 0) {
189 while(list($cacheArray['up_refid'][$cacheArray['back_level']]) = SQL_FETCHROW($result_refid)) {
191 $cacheArray['up_userid'][$cacheArray['up_refid'][$cacheArray['back_level']]] = $uid;
193 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},ref={$cacheArray['up_refid'][$cacheArray['back_level']]}<br />\n";
195 if (($cacheArray['up_refid'][$cacheArray['back_level']] > 0) && ($cacheArray['up_refid'][$cacheArray['back_level']] != $uid) && (!empty($cacheArray['up_refid'][$cacheArray['back_level']]))) {
197 if ((isset($cacheArray['up_refid'][$cacheArray['back_level']-1])) && (isset($cacheArray['up_userid'][$cacheArray['up_refid'][$cacheArray['back_level']-1]]))) {
200 $uid = $cacheArray['up_userid'][$cacheArray['up_refid'][$cacheArray['back_level']-1]];
202 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},old={$old},level={$cacheArray['back_level']},ref={$cacheArray['up_refid'][$cacheArray['back_level']]} - NEW UID!<br />\n";
206 $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_user_refs WHERE userid=%s AND level='%s' AND refid=%s LIMIT 1",
207 array(bigintval($uid), $cacheArray['back_level'], bigintval($cacheArray['up_refid'][$cacheArray['back_level']])), __FILE__, __LINE__);
209 // Do we have no entry?
210 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},level={$cacheArray['back_level']},ref={$cacheArray['up_refid'][$cacheArray['back_level']]},minus={$minus},numRows=".SQL_NUMROWS($result)." - FOUND!<br />\n";
211 if (SQL_NUMROWS($result) == 0) {
213 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},level={$cacheArray['back_level']},ref={$cacheArray['up_refid'][$cacheArray['back_level']]} - ADD!<br />\n";
214 $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_user_refs (userid,level,refid) VALUES (%s,%s,%s)",
215 array(bigintval($uid), $cacheArray['back_level'], bigintval($cacheArray['up_refid'][$cacheArray['back_level']])), __FILE__, __LINE__);
217 // Move to next referral level and count his counter one up!
218 $cacheArray['back_level']++; UPDATE_REFBACK_TABLE($cacheArray['up_refid'][($cacheArray['back_level'] - 1)]);
221 // Do we have another level here?
222 if ((($cacheArray['back_level']-$minus) > 0) && ($old > 0)) {
226 // Shall we add this as well?
227 $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_user_refs WHERE userid=%s AND level='%s' AND refid=%s LIMIT 1",
228 array(bigintval($uid), ($cacheArray['back_level']-$minus), bigintval($cacheArray['up_refid'][$cacheArray['back_level']])), __FILE__, __LINE__);
230 // Do we have no entry?
231 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},level=".($cacheArray['back_level']-$minus).",ref={$cacheArray['up_refid'][$cacheArray['back_level']]},numRows=".SQL_NUMROWS($result)." - BACK!<br />\n";
232 if (SQL_NUMROWS($result) == 0) {
234 $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_user_refs (userid,level,refid) VALUES (%s,%s,%s)",
235 array(bigintval($uid), ($cacheArray['back_level']-$minus), bigintval($cacheArray['up_refid'][$cacheArray['back_level']])), __FILE__, __LINE__);
236 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},level=".($cacheArray['back_level']-$minus).",ref={$cacheArray['up_refid'][$cacheArray['back_level']]} - RETURNED!<br />\n";
243 SQL_FREERESULT($result_refid);
246 // When he has a referral...
247 if ((($cacheArray['up_refid'][$cacheArray['back_level']] == $uid) || ($cacheArray['up_refid'][$cacheArray['back_level']] == 0)) && (GET_EXT_VERSION("cache") >= "0.1.2") && (!isset($cacheArray['back_cached']))) {
249 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},ref={$cacheArray['up_refid'][$cacheArray['back_level']]} - CACHE!<br />\n";
250 if ($cacheInstance->cache_file("refback", true)) $cacheInstance->cache_destroy();
251 $cacheArray['back_cached'] = 1;
255 $cacheArray['back_level']--;
256 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},level={$cacheArray['back_level']} - LEVEL!<br />\n";
259 if (!isset($cacheArray['up_refid'][$cacheArray['back_level']-1])) $cacheArray['up_refid'][$cacheArray['back_level']-1] = 0;
260 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},level={$cacheArray['back_level']},ref={$cacheArray['up_refid'][$cacheArray['back_level']-1]} - BACK!<br />\n";
261 //* DEBUG: */ print "</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />\n";
263 // "Getter" for array for user refs in given level
264 function GET_USER_REFS ($uid, $level) {
265 //* DEBUG: */ print "----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n";
266 // Default is no refs and no nickname
270 // Do we have nickname installed?
271 if (EXT_IS_ACTIVE("nickname")) {
272 $ADD = ", d.nickname";
275 // Get refs from database
276 $result = SQL_QUERY_ESC("SELECT r.id, r.refid, r.refback, r.points, d.status".$ADD."
277 FROM "._MYSQL_PREFIX."_user_refs AS r
278 LEFT JOIN "._MYSQL_PREFIX."_user_data AS d
280 WHERE r.userid=%s AND r.level=%s
281 ORDER BY r.refid ASC",
282 array(bigintval($uid), bigintval($level)), __FILE__, __LINE__);
284 // Are there some entries?
285 if (SQL_NUMROWS($result) > 0) {
287 while ($row = SQL_FETCHARRAY($result)) {
289 $refs[$row['id']] = $row;
294 SQL_FREERESULT($result);
297 //* DEBUG: */ print "</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />\n";
300 // "Getter" for refback entry
301 function GET_USER_REF_ENTRY ($id) {
302 //* DEBUG: */ print "----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n";
303 // Default data is nothing
307 $id = bigintval($id);
309 // Get entry from database
310 $result = SQL_QUERY_ESC("SELECT id, refid, refback, level FROM "._MYSQL_PREFIX."_user_refs WHERE id=%s AND userid=%s LIMIT 1",
311 array($id, $GLOBALS['userid']), __FILE__, __LINE__);
313 // Is there an entry?
314 if (SQL_NUMROWS($result) == 1) {
316 $data = SQL_FETCHARRAY($result);
320 SQL_FREERESULT($result);
323 //* DEBUG: */ print "</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />\n";
326 // Update refback percents (but with some sanity-checks!
327 function REFBACK_CHANGE_MEMBER_PERCENTS ($id, $percents) {
328 //* DEBUG: */ print "----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n";
329 // Default status is failed
330 $status['message'] = MEMBER_REFBACK_ERROR_GENERAL;
332 // Do some sanity-checks
335 $status['message'] = MEMBER_REFBACK_ERROR_NO_MEMBER;
337 } elseif ("".$id."" != "".bigintval($id)."") {
339 $status['message'] = MEMBER_REFBACK_ERROR_INVALID_ID_NUMBER;
341 } elseif (($percents < 0) || ($percents > 100)) {
342 // Percentage is not valid!
343 $status['message'] = MEMBER_REFBACK_ERROR_INVALID_PERCENTAGE;
348 $dummy = GET_USER_REF_ENTRY($id);
351 if (count($dummy) == 0) {
352 // ID does not belong to user!
353 $status['message'] = MEMBER_REFBACK_ERROR_ID_MISMATCH;
357 // Revert german commata
358 $percents = REVERT_COMMA($percents);
361 SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_refs SET refback='%s' WHERE id=%s AND userid=%s LIMIT 1",
362 array($percents, $id, $GLOBALS['userid']), __FILE__, __LINE__);
365 if (SQL_AFFECTEDROWS() < 1) {
366 // Entry not updated!
367 $status['message'] = MEMBER_REFBACK_ERROR_NOT_UPDATED;;
371 // Prepare email content
373 'percents' => TRANSLATE_COMMA($percents),
374 'refid' => $dummy['refid'],
375 'level' => $dummy['level']
378 // Load member email template
379 $mail = LOAD_EMAIL_TEMPLATE("member_refback", $content, $GLOBALS['userid']);
381 // Send email to user
382 SEND_EMAIL($dummy['refid'], MEMBER_REFBACK_SUBJECT, $mail);
384 // Send admin notification
385 SEND_ADMIN_NOTIFICATION(ADMIN_REFBACK_SUBJECT, "admin_refback", $content, $GLOBALS['userid']);
388 $status['ok'] = true;
390 // Return status array
391 //* DEBUG: */ print "</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />\n";