6e5c9112ba2130e48541d207817a57214236657a
[mailer.git] / inc / libs / refback_functions.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 09/28/2008 *
4  * ===============                              Last change: 09/28/2008 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : refback_functions.php                            *
8  * -------------------------------------------------------------------- *
9  * Short description : Functions for refback extension                  *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Funktionen fuer Refback-Erweiterung              *
12  * -------------------------------------------------------------------- *
13  *                                                                      *
14  * -------------------------------------------------------------------- *
15  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
16  * For more information visit: http://www.mxchange.org                  *
17  *                                                                      *
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.                                  *
22  *                                                                      *
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.                         *
27  *                                                                      *
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,               *
31  * MA  02110-1301  USA                                                  *
32  ************************************************************************/
33
34 // Some security stuff...
35 if (!defined('__SECURITY')) {
36         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
37         require($INC);
38 }
39
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;
44
45         // Back ref depths
46         $cacheArray['depth'][$uid][$ref] = $DEPTH; 
47
48         // Init points
49         $return = $points;
50
51         // Get all ref levels
52         $result_refs = SQL_QUERY("SELECT level, percents FROM "._MYSQL_PREFIX."_refdepths WHERE level > 0 ORDER BY level ASC", __FILE__, __LINE__);
53
54         // "Walk" through all level
55         while (list($level, $perc) = SQL_FETCHROW($result_refs)) {
56                 // Reset ref depths
57                 $DEPTH = -1;
58
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;
64
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";
68
69                         // Some percents given?
70                         if ($percents > 0) {
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);
74
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";
79
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");
83
84                                 // Reduce points if refid is found
85                                 if ($refid == $ref) {
86                                         // Reduce¬†points here!
87                                         $return = $points * ($perc / 100) - $refback;
88                                         //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):points={$return} - REDUCED<br />\n";
89                                 } // END - if
90                         } // END - if
91                 } // END foreach
92         } // END - while
93
94         // Free result
95         SQL_FREERESULT($result_refs);
96
97         // Restore ref depth
98         $DEPTH = $cacheArray['depth'][$uid][$ref];
99
100         // Return them
101         //* DEBUG: */ print "</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />\n";
102         return $return;
103 }
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;
110
111         // Default is zero
112         $percents = 0;
113
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__);
117
118         // Entry found? (Should be!)
119         if (SQL_NUMROWS($result) == 1) {
120                 // Fetch percents
121                 list($percents) = SQL_FETCHROW($result);
122                 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):percents={$percents}<br />\n";
123         } else {
124                 // Debug log
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! :-(");
127         }
128
129         // Free result
130         SQL_FREERESULT($result);
131
132         // Return percents
133         //* DEBUG: */ print "</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />\n";
134         return $percents;
135 }
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";
140         // Init userids
141         $userIds = array();
142
143         // Look for all
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__);
149
150         // Entries found?
151         //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):numRows=".SQL_NUMROWS($result)."<br />\n";
152         if (SQL_NUMROWS($result) > 0) {
153                 // Add all
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;
157                 } // END - while
158         } // END - if
159
160         // Free result
161         SQL_FREERESULT($result);
162
163         // Return array
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";
166         return $userIds;
167 }
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;
172
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";
176
177         // Init refid
178         $cacheArray['up_refid'][$cacheArray['back_level']] = 0;
179         $old = 0; $minus = 0;
180
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__);
184
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) {
188                 // Load all refids
189                 while(list($cacheArray['up_refid'][$cacheArray['back_level']]) = SQL_FETCHROW($result_refid)) {
190                         // Remmber userid
191                         $cacheArray['up_userid'][$cacheArray['up_refid'][$cacheArray['back_level']]] = $uid;
192
193                         //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},ref={$cacheArray['up_refid'][$cacheArray['back_level']]}<br />\n";
194                         // Refid set?
195                         if (($cacheArray['up_refid'][$cacheArray['back_level']] > 0) && ($cacheArray['up_refid'][$cacheArray['back_level']] != $uid) && (!empty($cacheArray['up_refid'][$cacheArray['back_level']]))) {
196                                 // New userid?
197                                 if ((isset($cacheArray['up_refid'][$cacheArray['back_level']-1])) && (isset($cacheArray['up_userid'][$cacheArray['up_refid'][$cacheArray['back_level']-1]]))) {
198                                         // New userid!
199                                         $old = $uid;
200                                         $uid = $cacheArray['up_userid'][$cacheArray['up_refid'][$cacheArray['back_level']-1]];
201                                         $minus = 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";
203                                 } // END - if
204
205                                 // Check existence
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__);
208
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) {
212                                         // Insert this level
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__);
216
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)]);
219                                 } // END - if
220
221                                 // Do we have another level here?
222                                 if ((($cacheArray['back_level']-$minus) > 0) && ($old > 0)) {
223                                         // Restore old one
224                                         $uid = $old;
225
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__);
229
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) {
233                                                 // Insert this level
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";
237                                         } // END - if
238                                 } // END - if
239                         } // END - if
240                 } // END - while
241
242                 // Free memory
243                 SQL_FREERESULT($result_refid);
244         } // END - if
245
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']))) {
248                 // Remove cache here
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;
252         } // END - if
253
254         // "Walk" back here
255         $cacheArray['back_level']--;
256         //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},level={$cacheArray['back_level']} - LEVEL!<br />\n";
257
258         // Fix empty refid
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";
262 }
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
267         $ADD = "";
268         $refs = array();
269
270         // Do we have nickname installed?
271         if (EXT_IS_ACTIVE("nickname")) {
272                 $ADD = ", d.nickname";
273         } // END - if
274
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
279 ON r.refid=d.userid
280 WHERE r.userid=%s AND r.level=%s
281 ORDER BY r.refid ASC",
282                 array(bigintval($uid), bigintval($level)), __FILE__, __LINE__);
283
284         // Are there some entries?
285         if (SQL_NUMROWS($result) > 0) {
286                 // Fetch all entries
287                 while ($row = SQL_FETCHARRAY($result)) {
288                         // Add row
289                         $refs[$row['id']] = $row;
290                 } // END - while
291         } // END - if
292
293         // Free result
294         SQL_FREERESULT($result);
295
296         // Return result
297         //* DEBUG: */ print "</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />\n";
298         return $refs;
299 }
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
304         $data = array();
305
306         // Secure id number
307         $id = bigintval($id);
308
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__);
312
313         // Is there an entry?
314         if (SQL_NUMROWS($result) == 1) {
315                 // Fetch data
316                 $data = SQL_FETCHARRAY($result);
317         } // END - if
318
319         // Free result
320         SQL_FREERESULT($result);
321
322         // Return entry
323         //* DEBUG: */ print "</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />\n";
324         return $data;
325 }
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;
331
332         // Do some sanity-checks
333         if (!IS_MEMBER()) {
334                 // No member!
335                 $status['message'] = MEMBER_REFBACK_ERROR_NO_MEMBER;
336                 return $status;
337         } elseif ("".$id."" != "".bigintval($id)."") {
338                 // No number!
339                 $status['message'] = MEMBER_REFBACK_ERROR_INVALID_ID_NUMBER;
340                 return $status;
341         } elseif (($percents < 0) || ($percents > 100)) {
342                 // Percentage is not valid!
343                 $status['message'] = MEMBER_REFBACK_ERROR_INVALID_PERCENTAGE;
344                 return $status;
345         }
346
347         // Get ref entry
348         $dummy = GET_USER_REF_ENTRY($id);
349
350         // Is this valid?
351         if (count($dummy) == 0) {
352                 // ID does not belong to user!
353                 $status['message'] = MEMBER_REFBACK_ERROR_ID_MISMATCH;
354                 return $status;
355         } // END - if
356
357         // Revert german commata
358         $percents = REVERT_COMMA($percents);
359
360         // Update entry
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__);
363
364         // Entry updated?
365         if (SQL_AFFECTEDROWS() < 1) {
366                 // Entry not updated!
367                 $status['message'] = MEMBER_REFBACK_ERROR_NOT_UPDATED;;
368                 return $status;
369         } // END - if
370
371         // Prepare email content
372         $content = array(
373                 'percents' => TRANSLATE_COMMA($percents),
374                 'refid'    => $dummy['refid'],
375                 'level'    => $dummy['level']
376         );
377
378         // Load member email template
379         $mail = LOAD_EMAIL_TEMPLATE("member_refback", $content, $GLOBALS['userid']);
380
381         // Send email to user
382         SEND_EMAIL($dummy['refid'], MEMBER_REFBACK_SUBJECT, $mail);
383
384         // Send admin notification
385         SEND_ADMIN_NOTIFICATION(ADMIN_REFBACK_SUBJECT, "admin_refback", $content, $GLOBALS['userid']);
386
387         // All fine!
388         $status['ok'] = true;
389
390         // Return status array
391         //* DEBUG: */ print "</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />\n";
392         return $status;
393 }
394 //
395 ?>