]> git.mxchange.org Git - mailer.git/blob - inc/libs/refback_functions.php
9305e2de50db77ad1f50bd1bb66aeae3e7533933
[mailer.git] / inc / libs / refback_functions.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                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  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
17  * Needs to be in all Files and every File needs "svn propset           *
18  * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
19  * -------------------------------------------------------------------- *
20  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
21  * Copyright (c) 2009, 2010 by Mailer Developer Team                    *
22  * For more information visit: http://www.mxchange.org                  *
23  *                                                                      *
24  * This program is free software; you can redistribute it and/or modify *
25  * it under the terms of the GNU General Public License as published by *
26  * the Free Software Foundation; either version 2 of the License, or    *
27  * (at your option) any later version.                                  *
28  *                                                                      *
29  * This program is distributed in the hope that it will be useful,      *
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
32  * GNU General Public License for more details.                         *
33  *                                                                      *
34  * You should have received a copy of the GNU General Public License    *
35  * along with this program; if not, write to the Free Software          *
36  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
37  * MA  02110-1301  USA                                                  *
38  ************************************************************************/
39
40 // Some security stuff...
41 if (!defined('__SECURITY')) {
42         die();
43 }
44
45 // Payback refback for refid and reduce it for current user
46 function addRefbackPoints ($userid, $ref, $points, $ref_points) {
47         //* DEBUG: */ print("----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n");
48         // Back ref depths
49         $GLOBALS['refback_depth'][$userid][$ref] = $GLOBALS['ref_level'];
50
51         // Init points
52         $return = $points;
53
54         // Get all ref levels
55         $result_refs = SQL_QUERY('SELECT `level`, `percents` FROM `{?_MYSQL_PREFIX?}_refdepths` WHERE `level` > 0 ORDER BY `level` ASC',
56                 __FUNCTION__, __LINE__);
57
58         // "Walk" through all level
59         while ($content = SQL_FETCHARRAY($result_refs)) {
60                 // Reset ref depths
61                 // @TODO Try to rewrite the following unset()
62                 unset($GLOBALS['ref_level']);
63
64                 // "Walk" through all refids
65                 //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):userid={$userid},ref={$ref},level={$content['level']},points={$points}<br />");
66                 foreach (getArrayFromRefbackLevel($userid, $content['level']) as $refid) {
67                         // Skip level zero or if both are the same
68                         if ($userid == $refid) continue;
69
70                         // Get refback percents
71                         $percents = getRefbackPercents($userid, $refid);
72                         //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):userid={$userid},ref={$ref},refid={$refid},points={$points},percents={$percents}<br />");
73
74                         // Some percents given?
75                         if ($percents > 0) {
76                                 //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):points={$points},perc={$content['percents']},percents={$percents},level={$content['level']}<br />");
77                                 // Calculate points for refback
78                                 $refback = $points * ($content['percents'] / 100) * ($percents / 100);
79
80                                 // Update refback table ('refid' and 'userid' must be exchanged!)
81                                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_refs` SET `points`=`points`+%s WHERE `userid`=%s AND `refid`=%s LIMIT 1",
82                                         array($refback, $refid, $userid), __FUNCTION__, __LINE__);
83                                 //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):refback={$refback},userid={$userid},refid={$refid} - UPDATE! (".SQL_AFFECTEDROWS().")<br />");
84
85                                 // Add points again, but only directly
86                                 //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):refback={$refback}<br />");
87                                 addPointsDirectly(sprintf("refback:%s", $refid), $userid, $refback);
88
89                                 // Reduce points if refid is found
90                                 if ($refid == $ref) {
91                                         // Reduce points here!
92                                         $return = $points * ($content['percents'] / 100) - $refback;
93                                         //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):points={$return} - REDUCED<br />");
94                                 } // END - if
95                         } // END - if
96                 } // END foreach
97         } // END - while
98
99         // Free result
100         SQL_FREERESULT($result_refs);
101
102         // Restore ref depth
103         $GLOBALS['ref_level'] = $GLOBALS['refback_depth'][$userid][$ref];
104
105         // Return them
106         //* DEBUG: */ print("</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />");
107         return $return;
108 }
109
110 // "Getter" for refback percents
111 function getRefbackPercents ($userid, $ref) {
112         //* DEBUG: */ print("----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n");
113         //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):userid={$userid},ref={$ref}<br />");
114         // Skip identical ids
115         if ($userid == $ref) return 0;
116
117         // Is it cached?
118         if (!isset($GLOBALS['refback_percents'][$userid][$ref])) {
119                 // Default is zero
120                 $GLOBALS['refback_percents'][$userid][$ref] = '0';
121
122                 // Get percents from database
123                 $result = SQL_QUERY_ESC("SELECT `refback` FROM `{?_MYSQL_PREFIX?}_user_refs` WHERE `userid`=%s AND `refid`=%s LIMIT 1",
124                         array($ref, $userid), __FUNCTION__, __LINE__);
125
126                 // Entry found? (Should be!)
127                 if (SQL_NUMROWS($result) == 1) {
128                         // Fetch percents
129                         list($GLOBALS['refback_percents'][$userid][$ref]) = SQL_FETCHROW($result);
130                         //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):percents=".$GLOBALS['refback_percents'][$userid][$ref]."<br />");
131                 } else {
132                         // Debug log
133                         //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>): NO ENTRY FOUND! :-(<br />");
134                         logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',ref=' . $ref . ' - No entry found! :-(');
135                 }
136
137                 // Free result
138                 SQL_FREERESULT($result);
139         } // END - if
140
141         // Return percents
142         //* DEBUG: */ print("</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />");
143         return $GLOBALS['refback_percents'][$userid][$ref];
144 }
145
146 // "Getter" for userid array which will return only one entry
147 function getArrayFromRefbackLevel ($rid, $level) {
148         //* DEBUG: */ print("----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n");
149         //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):rid={$rid},level={$level}<br />");
150         // Init userids
151         $userIds = array();
152
153         // Look for all
154         $result = SQL_QUERY_ESC("SELECT
155         `userid`
156 FROM
157         `{?_MYSQL_PREFIX?}_user_refs`
158 WHERE
159         `refid`=%s AND `level`=%s
160 ORDER BY
161         `userid` ASC",
162                 array($rid, $level), __FUNCTION__, __LINE__);
163
164         // Entries found?
165         //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):numRows=".SQL_NUMROWS($result)."<br />");
166         if (SQL_NUMROWS($result) > 0) {
167                 // Add all
168                 while ($content = SQL_FETCHARRAY($result)) {
169                         //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):rid={$rid},level={$level},userid={$content['userid']}<br />");
170                         $userIds[] = $content['userid'];
171                 } // END - while
172         } // END - if
173
174         // Free result
175         SQL_FREERESULT($result);
176
177         // Return array
178         //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):rid={$rid},count()=".count($userIds)."<br />");
179         //* DEBUG: */ print("</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />");
180         return $userIds;
181 }
182
183 // Update "refback table"
184 function updateRefbackTable ($userid) {
185         //* DEBUG: */ print("----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n");
186         // Make it sure referal level zero (member him-/herself) is at least selected
187         if (empty($GLOBALS['refback_level'])) $GLOBALS['refback_level'] = 1;
188         //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):userid={$userid},level={$GLOBALS['refback_level']}<br />");
189
190         // Init refid
191         $GLOBALS['refback_refid'][$GLOBALS['refback_level']] = '0';
192         $old = '0'; $minus = '0';
193
194         // Check for his referal
195         $result_refid = SQL_QUERY_ESC("SELECT `userid` FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `refid`=%s ORDER BY `userid` ASC",
196                 array(bigintval($userid)), __FUNCTION__, __LINE__);
197
198         // When no entry was updated then we have to create it here
199         //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):found=".SQL_NUMROWS($result_refid)."<br />");
200         if (SQL_NUMROWS($result_refid) > 0) {
201                 // Load all refids
202                 while (list($GLOBALS['refback_refid'][$GLOBALS['refback_level']]) = SQL_FETCHROW($result_refid)) {
203                         // Remmber userid
204                         $GLOBALS['refback_userid'][$GLOBALS['refback_refid'][$GLOBALS['refback_level']]] = $userid;
205
206                         // Refid set?
207                         //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):userid={$userid},ref={$GLOBALS['refback_refid'][$GLOBALS['refback_level']]}<br />");
208                         if (($GLOBALS['refback_refid'][$GLOBALS['refback_level']] > 0) && ($GLOBALS['refback_refid'][$GLOBALS['refback_level']] != $userid) && (isset($GLOBALS['refback_refid'][$GLOBALS['refback_level']]))) {
209                                 // New userid?
210                                 if ((isset($GLOBALS['refback_refid'][$GLOBALS['refback_level']-1])) && (isset($GLOBALS['refback_userid'][$GLOBALS['refback_refid'][$GLOBALS['refback_level']-1]]))) {
211                                         // New userid!
212                                         $old = $userid;
213                                         $userid = $GLOBALS['refback_userid'][$GLOBALS['refback_refid'][$GLOBALS['refback_level']-1]];
214                                         $minus = 1;
215                                         //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):userid={$userid},old={$old},level={$GLOBALS['refback_level']},ref={$GLOBALS['refback_refid'][$GLOBALS['refback_level']]} - NEW UID!<br />");
216                                 } // END - if
217
218                                 // Check existence
219                                 $result = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_user_refs` WHERE `userid`=%s AND `level`='%s' AND `refid`=%s LIMIT 1",
220                                         array(bigintval($userid), $GLOBALS['refback_level'], bigintval($GLOBALS['refback_refid'][$GLOBALS['refback_level']])), __FUNCTION__, __LINE__);
221
222                                 // Do we have no entry?
223                                 //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):userid={$userid},level={$GLOBALS['refback_level']},ref={$GLOBALS['refback_refid'][$GLOBALS['refback_level']]},minus={$minus},numRows=".SQL_NUMROWS($result)." - FOUND!<br />");
224                                 if (SQL_HASZERONUMS($result)) {
225                                         // Insert this level
226                                         //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):userid={$userid},level={$GLOBALS['refback_level']},ref={$GLOBALS['refback_refid'][$GLOBALS['refback_level']]} - ADD!<br />");
227                                         SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_refs` (`userid`,`level`,`refid`) VALUES (%s,%s,%s)",
228                                                 array(bigintval($userid), $GLOBALS['refback_level'], bigintval($GLOBALS['refback_refid'][$GLOBALS['refback_level']])), __FUNCTION__, __LINE__);
229
230                                         // Move to next referal level and count his counter one up!
231                                         $GLOBALS['refback_level']++; updateRefbackTable($GLOBALS['refback_refid'][($GLOBALS['refback_level'] - 1)]);
232                                 } // END - if
233
234                                 // Do we have another level here?
235                                 if ((($GLOBALS['refback_level']-$minus) > 0) && ($old > 0)) {
236                                         // Restore old one
237                                         $userid = $old;
238
239                                         // Shall we add this as well?
240                                         $result = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_user_refs` WHERE `userid`=%s AND `level`='%s' AND `refid`=%s LIMIT 1",
241                                                 array(bigintval($userid), ($GLOBALS['refback_level']-$minus), bigintval($GLOBALS['refback_refid'][$GLOBALS['refback_level']])), __FUNCTION__, __LINE__);
242
243                                         // Do we have no entry?
244                                         //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):userid={$userid},level=".($GLOBALS['refback_level']-$minus).",ref={$GLOBALS['refback_refid'][$GLOBALS['refback_level']]},numRows=".SQL_NUMROWS($result)." - BACK!<br />");
245                                         if (SQL_HASZERONUMS($result)) {
246                                                 // Insert this level
247                                                 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_refs` (`userid`,`level`,`refid`) VALUES (%s,%s,%s)",
248                                                         array(bigintval($userid), ($GLOBALS['refback_level']-$minus), bigintval($GLOBALS['refback_refid'][$GLOBALS['refback_level']])), __FUNCTION__, __LINE__);
249                                                 //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):userid={$userid},level=".($GLOBALS['refback_level']-$minus).",ref={$GLOBALS['refback_refid'][$GLOBALS['refback_level']]} - RETURNED!<br />");
250                                         } // END - if
251                                 } // END - if
252                         } // END - if
253                 } // END - while
254
255                 // Free memory
256                 SQL_FREERESULT($result_refid);
257         } // END - if
258
259         // When he has a referal...
260         if ((($GLOBALS['refback_refid'][$GLOBALS['refback_level']] == $userid) || ($GLOBALS['refback_refid'][$GLOBALS['refback_level']] == '0')) && (isExtensionInstalledAndNewer('cache', '0.1.2')) && (!isset($GLOBALS['refback_cached']))) {
261                 // Remove cache here
262                 //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):userid={$userid},ref={$GLOBALS['refback_refid'][$GLOBALS['refback_level']]} - CACHE!<br />");
263                 if ($GLOBALS['cache_instance']->loadCacheFile('refback')) $GLOBALS['cache_instance']->removeCacheFile();
264                 $GLOBALS['refback_cached'] = 1;
265         } // END - if
266
267         // "Walk" back here
268         $GLOBALS['refback_level']--;
269         //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):userid={$userid},level={$GLOBALS['refback_level']} - LEVEL!<br />");
270
271         // Fix empty refid
272         if (!isset($GLOBALS['refback_refid'][$GLOBALS['refback_level']-1])) $GLOBALS['refback_refid'][$GLOBALS['refback_level']-1] = '0';
273         //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):userid={$userid},level={$GLOBALS['refback_level']},ref={$GLOBALS['refback_refid'][$GLOBALS['refback_level']-1]} - BACK!<br />");
274         //* DEBUG: */ print("</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />");
275 }
276
277 // "Getter" for array for user refs in given level
278 function getArrayFromRefbackUserRefs ($userid, $level) {
279         //* DEBUG: */ print("----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n");
280         // Default is no refs and no nickname
281         $add = '';
282         $refs = array();
283
284         // Do we have nickname extension installed?
285         if (isExtensionActive('nickname')) {
286                 $add = ", d.nickname";
287         } // END - if
288
289         // Get refs from database
290         $result = SQL_QUERY_ESC("SELECT
291         r.id,
292         r.refid,
293         r.refback,
294         r.points,
295         d.status".$add.",
296         d.joined,
297         d.refid AS level_refid
298 FROM
299         `{?_MYSQL_PREFIX?}_user_refs` AS r
300 LEFT JOIN
301         `{?_MYSQL_PREFIX?}_user_data` AS d
302 ON
303         r.refid=d.userid
304 WHERE
305         r.userid=%s AND r.level=%s
306 ORDER BY
307         r.refid ASC",
308                 array(bigintval($userid), bigintval($level)), __FUNCTION__, __LINE__);
309
310         // Are there some entries?
311         if (SQL_NUMROWS($result) > 0) {
312                 // Fetch all entries
313                 while ($row = SQL_FETCHARRAY($result)) {
314                         // Add row
315                         $refs[$row['id']] = $row;
316                 } // END - while
317         } // END - if
318
319         // Free result
320         SQL_FREERESULT($result);
321
322         // Return result
323         //* DEBUG: */ print("</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />");
324         return $refs;
325 }
326
327 // "Getter" for refback entry
328 function getArrayFromUserRefbackData ($id) {
329         //* DEBUG: */ print("----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n");
330         // Default data is nothing
331         $data = array();
332
333         // Secure id number
334         $id = bigintval($id);
335
336         // Get entry from database
337         $result = SQL_QUERY_ESC("SELECT `id`, `userid`, `refid`, `refback`, `level`, `points` FROM `{?_MYSQL_PREFIX?}_user_refs` WHERE `id`=%s AND `userid`=%s LIMIT 1",
338                 array($id, getMemberId()), __FUNCTION__, __LINE__);
339
340         // Is there an entry?
341         if (SQL_NUMROWS($result) == 1) {
342                 // Fetch data
343                 $data = SQL_FETCHARRAY($result);
344         } // END - if
345
346         // Free result
347         SQL_FREERESULT($result);
348
349         // Return entry
350         //* DEBUG: */ print("</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />");
351         return $data;
352 }
353
354 // Update refback percents (but with some sanity-checks!
355 function updateMemberRefbackPercents ($id, $percents) {
356         //* DEBUG: */ print("----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n");
357         // Default status is failed
358         $status['message'] = '{--MEMBER_REFBACK_ERROR_GENERAL--}';
359
360         // Do some sanity-checks
361         if (!isMember()) {
362                 // No member!
363                 $status['message'] = '{--MEMBER_REFBACK_ERROR_NO_MEMBER--}';
364                 return $status;
365         } elseif (''.$id.'' != ''.($id + 0).'') {
366                 // No number!
367                 $status['message'] = '{--MEMBER_REFBACK_ERROR_INVALID_ID_NUMBER--}';
368                 return $status;
369         } elseif (($percents < 0) || ($percents > 100)) {
370                 // Percentage is not valid!
371                 $status['message'] = '{--MEMBER_REFBACK_ERROR_INVALID_PERCENTAGE--}';
372                 return $status;
373         }
374
375         // Get ref entry
376         $dummy = getArrayFromUserRefbackData($id);
377
378         // Is this valid?
379         if (count($dummy) == 0) {
380                 // id does not belong to user!
381                 $status['message'] = '{--MEMBER_REFBACK_ERROR_ID_MISMATCH--}';
382                 return $status;
383         } // END - if
384
385         // Revert german commata
386         $percents = convertCommaToDot($percents);
387
388         // Update entry
389         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_refs` SET `refback`='%s' WHERE `id`=%s AND `userid`=%s LIMIT 1",
390                 array($percents, $id, getMemberId()), __FUNCTION__, __LINE__);
391
392         // Entry updated?
393         if (SQL_AFFECTEDROWS() < 1) {
394                 // Entry not updated!
395                 $status['message'] = '{--MEMBER_REFBACK_ERROR_NOT_UPDATED--}';
396                 return $status;
397         } // END - if
398
399         // Prepare email content
400         $dummy['percents'] = $percents;
401
402         // Load member email template
403         $mail = loadEmailTemplate('member_refback', $dummy, $dummy['refid']);
404
405         // Send email to user
406         sendEmail($dummy['refid'], '{--MEMBER_REFBACK_SUBJECT--}', $mail);
407
408         // Send admin notification
409         sendAdminNotification(getMessage('ADMIN_REFBACK_SUBJECT'), 'admin_refback', $dummy, getMemberId());
410
411         // All fine!
412         $status['ok'] = true;
413
414         // Return status array
415         //* DEBUG: */ print("</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />");
416         return $status;
417 }
418
419 // [EOF]
420 ?>