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";
44 $GLOBALS['cache_array']['depth'][$uid][$ref] = $GLOBALS['ref_level'];
50 $result_refs = SQL_QUERY("SELECT level, percents FROM `{!_MYSQL_PREFIX!}_refdepths` WHERE level > 0 ORDER BY level ASC",
51 __FUNCTION__, __LINE__);
53 // "Walk" through all level
54 while ($content = SQL_FETCHARRAY($result_refs)) {
56 unset($GLOBALS['ref_level']);
58 // "Walk" through all refids
59 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},ref={$ref},level={$content['level']},points={$points}<br />\n";
60 foreach (GET_REFBACK_USERID_ARRAY($uid, $content['level']) as $refid) {
61 // Skip level zero or if both are the same
62 if ($uid == $refid) continue;
64 // Get refback percents
65 $percents = GET_REFBACK_PERCENTS($uid, $refid);
66 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},ref={$ref},refid={$refid},points={$points},percents={$percents}<br />\n";
68 // Some percents given?
70 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):points={$points},perc={$content['percents']},percents={$percents},level={$content['level']}<br />\n";
71 // Calculate points for refback
72 $refback = $points * ($content['percents'] / 100) * ($percents / 100);
74 // Update refback table ("refid" and "uid" must be exchanged!)
75 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_user_refs` SET points=points+%s WHERE userid=%s AND refid=%s LIMIT 1",
76 array($refback, $refid, $uid), __FUNCTION__, __LINE__);
77 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):refback={$refback},uid={$uid},refid={$refid} - UPDATE! (".SQL_AFFECTEDROWS().")<br />\n";
79 // Add points again, but only directly
80 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):refback={$refback}<br />\n";
81 ADD_POINTS_REFSYSTEM(sprintf("refback:%s", $refid), $uid, $refback, false, "0", false, "direct");
83 // Reduce points if refid is found
85 // Reduce points here!
86 $return = $points * ($content['percents'] / 100) - $refback;
87 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):points={$return} - REDUCED<br />\n";
94 SQL_FREERESULT($result_refs);
97 $GLOBALS['ref_level'] = $GLOBALS['cache_array']['depth'][$uid][$ref];
100 //* 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), __FUNCTION__, __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__, __LINE__, " uid={$uid},ref={$ref} - No entry found! :-(");
130 SQL_FREERESULT($result);
133 //* DEBUG: */ print "</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />\n";
137 // "Getter" for userid array which will return only one entry
138 function GET_REFBACK_USERID_ARRAY ($rid, $level) {
139 //* DEBUG: */ print "----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n";
140 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):rid={$rid},level={$level}<br />\n";
145 $result = SQL_QUERY_ESC("SELECT userid
146 FROM `{!_MYSQL_PREFIX!}_user_refs`
147 WHERE refid=%s AND level=%s
148 ORDER BY userid ASC",
149 array($rid,$level), __FUNCTION__, __LINE__);
152 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):numRows=".SQL_NUMROWS($result)."<br />\n";
153 if (SQL_NUMROWS($result) > 0) {
155 while ($content = SQL_FETCHARRAY($result)) {
156 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):rid={$rid},level={$level},userid={$content['userid']}<br />\n";
157 $userIds[] = $content['userid'];
162 SQL_FREERESULT($result);
165 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):rid={$rid},count()=".count($userIds)."<br />\n";
166 //* DEBUG: */ print "</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />\n";
170 // Update "refback table"
171 function UPDATE_REFBACK_TABLE ($uid) {
172 //* DEBUG: */ print "----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n";
173 // Make it sure referal level zero (member him-/herself) is at least selected
174 if (empty($GLOBALS['cache_array']['back_level'])) $GLOBALS['cache_array']['back_level'] = 1;
175 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},level={$GLOBALS['cache_array']['back_level']}<br />\n";
178 $GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']] = 0;
179 $old = 0; $minus = 0;
181 // Check for his referal
182 $result_refid = SQL_QUERY_ESC("SELECT userid FROM `{!_MYSQL_PREFIX!}_user_data` WHERE refid=%s ORDER BY userid ASC",
183 array(bigintval($uid)), __FUNCTION__, __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($GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']]) = SQL_FETCHROW($result_refid)) {
191 $GLOBALS['cache_array']['up_userid'][$GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']]] = $uid;
193 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},ref={$GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']]}<br />\n";
195 if (($GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']] > 0) && ($GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']] != $uid) && (isset($GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']]))) {
197 if ((isset($GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']-1])) && (isset($GLOBALS['cache_array']['up_userid'][$GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']-1]]))) {
200 $uid = $GLOBALS['cache_array']['up_userid'][$GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']-1]];
202 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},old={$old},level={$GLOBALS['cache_array']['back_level']},ref={$GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['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), $GLOBALS['cache_array']['back_level'], bigintval($GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']])), __FUNCTION__, __LINE__);
209 // Do we have no entry?
210 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},level={$GLOBALS['cache_array']['back_level']},ref={$GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['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={$GLOBALS['cache_array']['back_level']},ref={$GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']]} - ADD!<br />\n";
214 SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_user_refs` (userid,level,refid) VALUES (%s,%s,%s)",
215 array(bigintval($uid), $GLOBALS['cache_array']['back_level'], bigintval($GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']])), __FUNCTION__, __LINE__);
217 // Move to next referal level and count his counter one up!
218 $GLOBALS['cache_array']['back_level']++; UPDATE_REFBACK_TABLE($GLOBALS['cache_array']['up_refid'][($GLOBALS['cache_array']['back_level'] - 1)]);
221 // Do we have another level here?
222 if ((($GLOBALS['cache_array']['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), ($GLOBALS['cache_array']['back_level']-$minus), bigintval($GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']])), __FUNCTION__, __LINE__);
230 // Do we have no entry?
231 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},level=".($GLOBALS['cache_array']['back_level']-$minus).",ref={$GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']]},numRows=".SQL_NUMROWS($result)." - BACK!<br />\n";
232 if (SQL_NUMROWS($result) == 0) {
234 SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_user_refs` (userid,level,refid) VALUES (%s,%s,%s)",
235 array(bigintval($uid), ($GLOBALS['cache_array']['back_level']-$minus), bigintval($GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']])), __FUNCTION__, __LINE__);
236 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},level=".($GLOBALS['cache_array']['back_level']-$minus).",ref={$GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']]} - RETURNED!<br />\n";
243 SQL_FREERESULT($result_refid);
246 // When he has a referal...
247 if ((($GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']] == $uid) || ($GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']] == 0)) && (GET_EXT_VERSION("cache") >= "0.1.2") && (!isset($GLOBALS['cache_array']['back_cached']))) {
249 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},ref={$GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']]} - CACHE!<br />\n";
250 if ($GLOBALS['cache_instance']->loadCacheFile("refback")) $GLOBALS['cache_instance']->destroyCacheFile();
251 $GLOBALS['cache_array']['back_cached'] = 1;
255 $GLOBALS['cache_array']['back_level']--;
256 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},level={$GLOBALS['cache_array']['back_level']} - LEVEL!<br />\n";
259 if (!isset($GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']-1])) $GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']-1] = 0;
260 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},level={$GLOBALS['cache_array']['back_level']},ref={$GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']-1]} - BACK!<br />\n";
261 //* DEBUG: */ print "</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />\n";
264 // "Getter" for array for user refs in given level
265 function GET_USER_REFS ($uid, $level) {
266 //* DEBUG: */ print "----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n";
267 // Default is no refs and no nickname
271 // Do we have nickname extension installed?
272 if (EXT_IS_ACTIVE("nickname")) {
273 $ADD = ", d.nickname";
276 // Get refs from database
277 $result = SQL_QUERY_ESC("SELECT r.id, r.refid, r.refback, r.points, d.status".$ADD."
278 FROM `{!_MYSQL_PREFIX!}_user_refs` AS r
279 LEFT JOIN `{!_MYSQL_PREFIX!}_user_data` AS d
281 WHERE r.userid=%s AND r.level=%s
282 ORDER BY r.refid ASC",
283 array(bigintval($uid), bigintval($level)), __FUNCTION__, __LINE__);
285 // Are there some entries?
286 if (SQL_NUMROWS($result) > 0) {
288 while ($row = SQL_FETCHARRAY($result)) {
290 $refs[$row['id']] = $row;
295 SQL_FREERESULT($result);
298 //* DEBUG: */ print "</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />\n";
302 // "Getter" for refback entry
303 function GET_USER_REF_ENTRY ($id) {
304 //* DEBUG: */ print "----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n";
305 // Default data is nothing
309 $id = bigintval($id);
311 // Get entry from database
312 $result = SQL_QUERY_ESC("SELECT id, refid, refback, level FROM `{!_MYSQL_PREFIX!}_user_refs` WHERE id=%s AND userid=%s LIMIT 1",
313 array($id, $GLOBALS['userid']), __FUNCTION__, __LINE__);
315 // Is there an entry?
316 if (SQL_NUMROWS($result) == 1) {
318 $data = SQL_FETCHARRAY($result);
322 SQL_FREERESULT($result);
325 //* DEBUG: */ print "</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />\n";
329 // Update refback percents (but with some sanity-checks!
330 function REFBACK_CHANGE_MEMBER_PERCENTS ($id, $percents) {
331 //* DEBUG: */ print "----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n";
332 // Default status is failed
333 $status['message'] = getMessage('MEMBER_REFBACK_ERROR_GENERAL');
335 // Do some sanity-checks
338 $status['message'] = getMessage('MEMBER_REFBACK_ERROR_NO_MEMBER');
340 } elseif ("".$id."" != "".($id + 0)."") {
342 $status['message'] = getMessage('MEMBER_REFBACK_ERROR_INVALID_ID_NUMBER');
344 } elseif (($percents < 0) || ($percents > 100)) {
345 // Percentage is not valid!
346 $status['message'] = getMessage('MEMBER_REFBACK_ERROR_INVALID_PERCENTAGE');
351 $dummy = GET_USER_REF_ENTRY($id);
354 if (count($dummy) == 0) {
355 // ID does not belong to user!
356 $status['message'] = getMessage('MEMBER_REFBACK_ERROR_ID_MISMATCH');
360 // Revert german commata
361 $percents = REVERT_COMMA($percents);
364 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_user_refs` SET refback='%s' WHERE id=%s AND userid=%s LIMIT 1",
365 array($percents, $id, $GLOBALS['userid']), __FUNCTION__, __LINE__);
368 if (SQL_AFFECTEDROWS() < 1) {
369 // Entry not updated!
370 $status['message'] = getMessage('MEMBER_REFBACK_ERROR_NOT_UPDATED');
374 // Prepare email content
376 'percents' => TRANSLATE_COMMA($percents),
377 'refid' => $dummy['refid'],
378 'level' => $dummy['level']
381 // Load member email template
382 $mail = LOAD_EMAIL_TEMPLATE("member_refback", $content, $GLOBALS['userid']);
384 // Send email to user
385 SEND_EMAIL($dummy['refid'], getMessage('MEMBER_REFBACK_SUBJECT'), $mail);
387 // Send admin notification
388 SEND_ADMIN_NOTIFICATION(getMessage('ADMIN_REFBACK_SUBJECT'), "admin_refback", $content, $GLOBALS['userid']);
391 $status['ok'] = true;
393 // Return status array
394 //* DEBUG: */ print "</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />\n";