dc3d0681602bcc0dfe336b5705a4346d7145e95e
[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", __FILE__, __LINE__);
51
52         // "Walk" through all level
53         while (list($level, $perc) = SQL_FETCHROW($result_refs)) {
54                 // Reset ref depths
55                 unset($GLOBALS['ref_level']);
56
57                 // "Walk" through all refids
58                 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},ref={$ref},level={$level},points={$points}<br />\n";
59                 foreach (GET_REFBACK_USERID_ARRAY($uid, $level) as $refid) {
60                         // Skip level zero or if both are the same
61                         if ($uid == $refid) continue;
62
63                         // Get refback percents
64                         $percents = GET_REFBACK_PERCENTS($uid, $refid);
65                         //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},ref={$ref},refid={$refid},points={$points},percents={$percents}<br />\n";
66
67                         // Some percents given?
68                         if ($percents > 0) {
69                                 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):points={$points},perc={$perc},percents={$percents},level={$level}<br />\n";
70                                 // Calculate points for refback
71                                 $refback = $points * ($perc / 100) * ($percents / 100);
72
73                                 // Update refback table ("refid" and "uid" must be exchanged!)
74                                 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_user_refs` SET points=points+%s WHERE userid=%s AND refid=%s LIMIT 1",
75                                         array($refback, $refid, $uid), __FILE__, __LINE__);
76                                 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):refback={$refback},uid={$uid},refid={$refid} - UPDATE! (".SQL_AFFECTEDROWS().")<br />\n";
77
78                                 // Add points again, but only directly
79                                 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):refback={$refback}<br />\n";
80                                 ADD_POINTS_REFSYSTEM(sprintf("refback:%s", $refid), $uid, $refback, false, "0", false, "direct");
81
82                                 // Reduce points if refid is found
83                                 if ($refid == $ref) {
84                                         // Reduce¬†points here!
85                                         $return = $points * ($perc / 100) - $refback;
86                                         //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):points={$return} - REDUCED<br />\n";
87                                 } // END - if
88                         } // END - if
89                 } // END foreach
90         } // END - while
91
92         // Free result
93         SQL_FREERESULT($result_refs);
94
95         // Restore ref depth
96         $GLOBALS['ref_level'] = $GLOBALS['cache_array']['depth'][$uid][$ref];
97
98         // Return them
99         //* DEBUG: */ print "</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />\n";
100         return $return;
101 }
102
103 // "Getter" for refback percents
104 function GET_REFBACK_PERCENTS ($uid, $ref) {
105         //* DEBUG: */ print "----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n";
106         //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},ref={$ref}<br />\n";
107         // Skip identical ids
108         if ($uid == $ref) return 0;
109
110         // Default is zero
111         $percents = 0;
112
113         // Get percents from database
114         $result = SQL_QUERY_ESC("SELECT refback FROM `{!_MYSQL_PREFIX!}_user_refs` WHERE userid=%s AND refid=%s LIMIT 1",
115                 array($ref, $uid), __FILE__, __LINE__);
116
117         // Entry found? (Should be!)
118         if (SQL_NUMROWS($result) == 1) {
119                 // Fetch percents
120                 list($percents) = SQL_FETCHROW($result);
121                 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):percents={$percents}<br />\n";
122         } else {
123                 // Debug log
124                 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>): NO ENTRY FOUND! :-(<br />\n";
125                 DEBUG_LOG(__FUNCTION__, __LINE__, " uid={$uid},ref={$ref} - No entry found! :-(");
126         }
127
128         // Free result
129         SQL_FREERESULT($result);
130
131         // Return percents
132         //* DEBUG: */ print "</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />\n";
133         return $percents;
134 }
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
169 // Update "refback table"
170 function UPDATE_REFBACK_TABLE($uid) {
171         //* DEBUG: */ print "----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n";
172         // Make it sure referal level zero (member him-/herself) is at least selected
173         if (empty($GLOBALS['cache_array']['back_level'])) $GLOBALS['cache_array']['back_level'] = 1;
174         //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},level={$GLOBALS['cache_array']['back_level']}<br />\n";
175
176         // Init refid
177         $GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']] = 0;
178         $old = 0; $minus = 0;
179
180         // Check for his referal
181         $result_refid = SQL_QUERY_ESC("SELECT userid FROM `{!_MYSQL_PREFIX!}_user_data` WHERE refid=%s ORDER BY userid ASC",
182                 array(bigintval($uid)), __FILE__, __LINE__);
183
184         // When no entry was updated then we have to create it here
185         //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):found=".SQL_NUMROWS($result_refid)."<br />\n";
186         if (SQL_NUMROWS($result_refid) > 0) {
187                 // Load all refids
188                 while (list($GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']]) = SQL_FETCHROW($result_refid)) {
189                         // Remmber userid
190                         $GLOBALS['cache_array']['up_userid'][$GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']]] = $uid;
191
192                         //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},ref={$GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']]}<br />\n";
193                         // Refid set?
194                         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']]))) {
195                                 // New userid?
196                                 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]]))) {
197                                         // New userid!
198                                         $old = $uid;
199                                         $uid = $GLOBALS['cache_array']['up_userid'][$GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']-1]];
200                                         $minus = 1;
201                                         //* 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";
202                                 } // END - if
203
204                                 // Check existence
205                                 $result = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_user_refs` WHERE userid=%s AND level='%s' AND refid=%s LIMIT 1",
206                                         array(bigintval($uid), $GLOBALS['cache_array']['back_level'], bigintval($GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']])), __FILE__, __LINE__);
207
208                                 // Do we have no entry?
209                                 //* 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";
210                                 if (SQL_NUMROWS($result) == 0) {
211                                         // Insert this level
212                                         //* 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";
213                                         SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_user_refs` (userid,level,refid) VALUES (%s,%s,%s)",
214                                                 array(bigintval($uid), $GLOBALS['cache_array']['back_level'], bigintval($GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']])), __FILE__, __LINE__);
215
216                                         // Move to next referal level and count his counter one up!
217                                         $GLOBALS['cache_array']['back_level']++; UPDATE_REFBACK_TABLE($GLOBALS['cache_array']['up_refid'][($GLOBALS['cache_array']['back_level'] - 1)]);
218                                 } // END - if
219
220                                 // Do we have another level here?
221                                 if ((($GLOBALS['cache_array']['back_level']-$minus) > 0) && ($old > 0)) {
222                                         // Restore old one
223                                         $uid = $old;
224
225                                         // Shall we add this as well?
226                                         $result = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_user_refs` WHERE userid=%s AND level='%s' AND refid=%s LIMIT 1",
227                                                 array(bigintval($uid), ($GLOBALS['cache_array']['back_level']-$minus), bigintval($GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']])), __FILE__, __LINE__);
228
229                                         // Do we have no entry?
230                                         //* 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";
231                                         if (SQL_NUMROWS($result) == 0) {
232                                                 // Insert this level
233                                                 SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_user_refs` (userid,level,refid) VALUES (%s,%s,%s)",
234                                                         array(bigintval($uid), ($GLOBALS['cache_array']['back_level']-$minus), bigintval($GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']])), __FILE__, __LINE__);
235                                                 //* 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";
236                                         } // END - if
237                                 } // END - if
238                         } // END - if
239                 } // END - while
240
241                 // Free memory
242                 SQL_FREERESULT($result_refid);
243         } // END - if
244
245         // When he has a referal...
246         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']))) {
247                 // Remove cache here
248                 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},ref={$GLOBALS['cache_array']['up_refid'][$GLOBALS['cache_array']['back_level']]} - CACHE!<br />\n";
249                 if ($GLOBALS['cache_instance']->loadCacheFile("refback")) $GLOBALS['cache_instance']->destroyCacheFile();
250                 $GLOBALS['cache_array']['back_cached'] = 1;
251         } // END - if
252
253         // "Walk" back here
254         $GLOBALS['cache_array']['back_level']--;
255         //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},level={$GLOBALS['cache_array']['back_level']} - LEVEL!<br />\n";
256
257         // Fix empty refid
258         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;
259         //* 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";
260         //* DEBUG: */ print "</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />\n";
261 }
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 extension 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
301 // "Getter" for refback entry
302 function GET_USER_REF_ENTRY ($id) {
303         //* DEBUG: */ print "----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n";
304         // Default data is nothing
305         $data = array();
306
307         // Secure id number
308         $id = bigintval($id);
309
310         // Get entry from database
311         $result = SQL_QUERY_ESC("SELECT id, refid, refback, level FROM `{!_MYSQL_PREFIX!}_user_refs` WHERE id=%s AND userid=%s LIMIT 1",
312                 array($id, $GLOBALS['userid']), __FILE__, __LINE__);
313
314         // Is there an entry?
315         if (SQL_NUMROWS($result) == 1) {
316                 // Fetch data
317                 $data = SQL_FETCHARRAY($result);
318         } // END - if
319
320         // Free result
321         SQL_FREERESULT($result);
322
323         // Return entry
324         //* DEBUG: */ print "</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />\n";
325         return $data;
326 }
327
328 // Update refback percents (but with some sanity-checks!
329 function REFBACK_CHANGE_MEMBER_PERCENTS ($id, $percents) {
330         //* DEBUG: */ print "----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n";
331         // Default status is failed
332         $status['message'] = getMessage('MEMBER_REFBACK_ERROR_GENERAL');
333
334         // Do some sanity-checks
335         if (!IS_MEMBER()) {
336                 // No member!
337                 $status['message'] = getMessage('MEMBER_REFBACK_ERROR_NO_MEMBER');
338                 return $status;
339         } elseif ("".$id."" != "".($id + 0)."") {
340                 // No number!
341                 $status['message'] = getMessage('MEMBER_REFBACK_ERROR_INVALID_ID_NUMBER');
342                 return $status;
343         } elseif (($percents < 0) || ($percents > 100)) {
344                 // Percentage is not valid!
345                 $status['message'] = getMessage('MEMBER_REFBACK_ERROR_INVALID_PERCENTAGE');
346                 return $status;
347         }
348
349         // Get ref entry
350         $dummy = GET_USER_REF_ENTRY($id);
351
352         // Is this valid?
353         if (count($dummy) == 0) {
354                 // ID does not belong to user!
355                 $status['message'] = getMessage('MEMBER_REFBACK_ERROR_ID_MISMATCH');
356                 return $status;
357         } // END - if
358
359         // Revert german commata
360         $percents = REVERT_COMMA($percents);
361
362         // Update entry
363         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_user_refs` SET refback='%s' WHERE id=%s AND userid=%s LIMIT 1",
364                 array($percents, $id, $GLOBALS['userid']), __FILE__, __LINE__);
365
366         // Entry updated?
367         if (SQL_AFFECTEDROWS() < 1) {
368                 // Entry not updated!
369                 $status['message'] = getMessage('MEMBER_REFBACK_ERROR_NOT_UPDATED');
370                 return $status;
371         } // END - if
372
373         // Prepare email content
374         $content = array(
375                 'percents' => TRANSLATE_COMMA($percents),
376                 'refid'    => $dummy['refid'],
377                 'level'    => $dummy['level']
378         );
379
380         // Load member email template
381         $mail = LOAD_EMAIL_TEMPLATE("member_refback", $content, $GLOBALS['userid']);
382
383         // Send email to user
384         SEND_EMAIL($dummy['refid'], getMessage('MEMBER_REFBACK_SUBJECT'), $mail);
385
386         // Send admin notification
387         SEND_ADMIN_NOTIFICATION(getMessage('ADMIN_REFBACK_SUBJECT'), "admin_refback", $content, $GLOBALS['userid']);
388
389         // All fine!
390         $status['ok'] = true;
391
392         // Return status array
393         //* DEBUG: */ print "</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />\n";
394         return $status;
395 }
396 //
397 ?>