d08c83bc4b85c9f92d52ffd979c8fa0d96655d3e
[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         // Back ref depths
44         $GLOBALS['cache_array']['depth'][$uid][$ref] = $GLOBALS['ref_level']; 
45
46         // Init points
47         $return = $points;
48
49         // Get all ref levels
50         $result_refs = SQL_QUERY("SELECT level, percents FROM `{!_MYSQL_PREFIX!}_refdepths` WHERE level > 0 ORDER BY level ASC",
51                 __FUNCTION__, __LINE__);
52
53         // "Walk" through all level
54         while ($content = SQL_FETCHARRAY($result_refs)) {
55                 // Reset ref depths
56                 unset($GLOBALS['ref_level']);
57
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;
63
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";
67
68                         // Some percents given?
69                         if ($percents > 0) {
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);
73
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";
78
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");
82
83                                 // Reduce points if refid is found
84                                 if ($refid == $ref) {
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";
88                                 } // END - if
89                         } // END - if
90                 } // END foreach
91         } // END - while
92
93         // Free result
94         SQL_FREERESULT($result_refs);
95
96         // Restore ref depth
97         $GLOBALS['ref_level'] = $GLOBALS['cache_array']['depth'][$uid][$ref];
98
99         // Return them
100         //* DEBUG: */ print "</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />\n";
101         return $return;
102 }
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), __FUNCTION__, __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__, __LINE__, " 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
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";
141         // Init userids
142         $userIds = array();
143
144         // Look for all
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__);
150
151         // Entries found?
152         //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):numRows=".SQL_NUMROWS($result)."<br />\n";
153         if (SQL_NUMROWS($result) > 0) {
154                 // Add all
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'];
158                 } // END - while
159         } // END - if
160
161         // Free result
162         SQL_FREERESULT($result);
163
164         // Return array
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";
167         return $userIds;
168 }
169
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";
176
177         // Init refid
178         $GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']] = 0;
179         $old = 0; $minus = 0;
180
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__);
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($GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']]) = SQL_FETCHROW($result_refid)) {
190                         // Remmber userid
191                         $GLOBALS['cache_array']['up_userid'][$GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']]] = $uid;
192
193                         //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},ref={$GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']]}<br />\n";
194                         // Refid set?
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']]))) {
196                                 // New userid?
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]]))) {
198                                         // New userid!
199                                         $old = $uid;
200                                         $uid = $GLOBALS['cache_array']['up_userid'][$GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']-1]];
201                                         $minus = 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";
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), $GLOBALS['cache_array']['back_level'], bigintval($GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']])), __FUNCTION__, __LINE__);
208
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) {
212                                         // Insert this level
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__);
216
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)]);
219                                 } // END - if
220
221                                 // Do we have another level here?
222                                 if ((($GLOBALS['cache_array']['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), ($GLOBALS['cache_array']['back_level']-$minus), bigintval($GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']])), __FUNCTION__, __LINE__);
229
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) {
233                                                 // Insert this level
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";
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 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']))) {
248                 // Remove cache here
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;
252         } // END - if
253
254         // "Walk" back here
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";
257
258         // Fix empty refid
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";
262 }
263
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
268         $ADD = "";
269         $refs = array();
270
271         // Do we have nickname extension installed?
272         if (EXT_IS_ACTIVE("nickname")) {
273                 $ADD = ", d.nickname";
274         } // END - if
275
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
280 ON r.refid=d.userid
281 WHERE r.userid=%s AND r.level=%s
282 ORDER BY r.refid ASC",
283                 array(bigintval($uid), bigintval($level)), __FUNCTION__, __LINE__);
284
285         // Are there some entries?
286         if (SQL_NUMROWS($result) > 0) {
287                 // Fetch all entries
288                 while ($row = SQL_FETCHARRAY($result)) {
289                         // Add row
290                         $refs[$row['id']] = $row;
291                 } // END - while
292         } // END - if
293
294         // Free result
295         SQL_FREERESULT($result);
296
297         // Return result
298         //* DEBUG: */ print "</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />\n";
299         return $refs;
300 }
301
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
306         $data = array();
307
308         // Secure id number
309         $id = bigintval($id);
310
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__);
314
315         // Is there an entry?
316         if (SQL_NUMROWS($result) == 1) {
317                 // Fetch data
318                 $data = SQL_FETCHARRAY($result);
319         } // END - if
320
321         // Free result
322         SQL_FREERESULT($result);
323
324         // Return entry
325         //* DEBUG: */ print "</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />\n";
326         return $data;
327 }
328
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');
334
335         // Do some sanity-checks
336         if (!IS_MEMBER()) {
337                 // No member!
338                 $status['message'] = getMessage('MEMBER_REFBACK_ERROR_NO_MEMBER');
339                 return $status;
340         } elseif ("".$id."" != "".($id + 0)."") {
341                 // No number!
342                 $status['message'] = getMessage('MEMBER_REFBACK_ERROR_INVALID_ID_NUMBER');
343                 return $status;
344         } elseif (($percents < 0) || ($percents > 100)) {
345                 // Percentage is not valid!
346                 $status['message'] = getMessage('MEMBER_REFBACK_ERROR_INVALID_PERCENTAGE');
347                 return $status;
348         }
349
350         // Get ref entry
351         $dummy = GET_USER_REF_ENTRY($id);
352
353         // Is this valid?
354         if (count($dummy) == 0) {
355                 // ID does not belong to user!
356                 $status['message'] = getMessage('MEMBER_REFBACK_ERROR_ID_MISMATCH');
357                 return $status;
358         } // END - if
359
360         // Revert german commata
361         $percents = REVERT_COMMA($percents);
362
363         // Update entry
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__);
366
367         // Entry updated?
368         if (SQL_AFFECTEDROWS() < 1) {
369                 // Entry not updated!
370                 $status['message'] = getMessage('MEMBER_REFBACK_ERROR_NOT_UPDATED');
371                 return $status;
372         } // END - if
373
374         // Prepare email content
375         $content = array(
376                 'percents' => TRANSLATE_COMMA($percents),
377                 'refid'    => $dummy['refid'],
378                 'level'    => $dummy['level']
379         );
380
381         // Load member email template
382         $mail = LOAD_EMAIL_TEMPLATE("member_refback", $content, $GLOBALS['userid']);
383
384         // Send email to user
385         SEND_EMAIL($dummy['refid'], getMessage('MEMBER_REFBACK_SUBJECT'), $mail);
386
387         // Send admin notification
388         SEND_ADMIN_NOTIFICATION(getMessage('ADMIN_REFBACK_SUBJECT'), "admin_refback", $content, $GLOBALS['userid']);
389
390         // All fine!
391         $status['ok'] = true;
392
393         // Return status array
394         //* DEBUG: */ print "</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />\n";
395         return $status;
396 }
397 //
398 ?>