]> git.mxchange.org Git - mailer.git/blob - inc/referal-functions.php
Code moved around (referal-functions.php added)
[mailer.git] / inc / referal-functions.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 07/12/2011 *
4  * ===================                          Last change: 07/12/2011 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : referal-functions.php                            *
8  * -------------------------------------------------------------------- *
9  * Short description : All referal system functions                     *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Alle zum Referal-System gehoerenden Funktionen   *
12  * -------------------------------------------------------------------- *
13  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
17  * -------------------------------------------------------------------- *
18  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
19  * Copyright (c) 2009 - 2011 by Mailer Developer Team                   *
20  * For more information visit: http://www.mxchange.org                  *
21  *                                                                      *
22  * This program is free software; you can redistribute it and/or modify *
23  * it under the terms of the GNU General Public License as published by *
24  * the Free Software Foundation; either version 2 of the License, or    *
25  * (at your option) any later version.                                  *
26  *                                                                      *
27  * This program is distributed in the hope that it will be useful,      *
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
30  * GNU General Public License for more details.                         *
31  *                                                                      *
32  * You should have received a copy of the GNU General Public License    *
33  * along with this program; if not, write to the Free Software          *
34  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
35  * MA  02110-1301  USA                                                  *
36  ************************************************************************/
37
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
40         die();
41 } // END - if
42
43 // Initializes the referal system
44 function initReferalSystem () {
45         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ' Referal system initialized!');
46         $GLOBALS['ref_level']  = NULL;
47         $GLOBALS['ref_system'] = true;
48 }
49
50 // Getter fro ref level percents
51 function getReferalLevelPercents ($level) {
52         // Default is zero
53         $data['percents'] = '0';
54
55         // Do we have cache?
56         if ((isset($GLOBALS['cache_array']['refdepths']['level'])) && (isExtensionActive('cache'))) {
57                 // First look for level
58                 $key = array_search($level, $GLOBALS['cache_array']['refdepths']['level']);
59                 if ($key !== false) {
60                         // Entry found
61                         $data['percents'] = $GLOBALS['cache_array']['refdepths']['percents'][$key];
62
63                         // Count cache hit
64                         incrementStatsEntry('cache_hits');
65                 } // END - if
66         } elseif (!isExtensionActive('cache')) {
67                 // Get referal data
68                 $result_level = SQL_QUERY_ESC("SELECT `percents` FROM `{?_MYSQL_PREFIX?}_refdepths` WHERE `level`=%s LIMIT 1",
69                         array(bigintval($level)), __FUNCTION__, __LINE__);
70
71                 // Entry found?
72                 if (SQL_NUMROWS($result_level) == 1) {
73                         // Get percents
74                         $data = SQL_FETCHARRAY($result_level);
75                 } // END - if
76
77                 // Free result
78                 SQL_FREERESULT($result_level);
79         }
80
81         // Return percent
82         return $data['percents'];
83 }
84
85 /**
86  *
87  * Dynamic referal and points system, can also send mails!
88  *
89  * subject       = Subject line, write in lower-case letters and underscore is allowed
90  * userid        = Referal id wich should receive...
91  * points        = ... xxx points
92  * refid         = inc/modules/guest/what-confirm.php need this
93  * locked        = Shall I pay it to normal (false) or locked (true) points ammount?
94  */
95 function addPointsThroughReferalSystem ($subject, $userid, $points, $refid = '0') {
96         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'subject=' . $subject . ',userid=' . $userid . ',points=' . $points . ',refid=' . $refid . ' - ENTERED!');
97         // By default nothing has been added
98         $added = false;
99
100         // Determine payment method and notification
101         $paymentMethod = strtoupper(getPaymentMethodFromSubject($subject));
102         $sendNotify    = isPaymentRecipientNotificationEnabled($subject);
103
104         // When $userid = '0' add points to jackpot
105         if (($userid == '0') && ($paymentMethod == 'DIRECT') && (isExtensionActive('jackpot'))) {
106                 // Add points to jackpot only in DIRECT mode
107                 return addPointsToJackpot($points);
108         } // END - if
109
110         // Check user account
111         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',points=' . $points);
112         if (fetchUserData($userid)) {
113                 // Determine wether the user has some mails to click before he/she gets the points
114                 $locked = ifUserPointsLocked($userid);
115
116                 // Detect database column
117                 $pointsColumn = determinePointsColumnFromSubjectLocked($subject, $locked);
118
119                 // This is the user and his ref
120                 $GLOBALS['cache_array']['add_userid'][getUserData('refid')] = $userid;
121
122                 // Get percents
123                 $per = getReferalLevelPercents($GLOBALS['ref_level']);
124                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid='.$userid.',points='.$points.',depth='.$GLOBALS['ref_level'].',per='.$per.',mode='.$paymentMethod);
125
126                 // Some percents found?
127                 if ($per > 0) {
128                         // Calculate new points
129                         $ref_points = $points * $per / 100;
130                         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid='.$userid.',points='.$points.',per='.$per.',depth='.$GLOBALS['ref_level'].',ref_points='.$ref_points);
131
132                         // Pay refback here if level > 0 and in ref-mode
133                         if ((isExtensionActive('refback')) && ($GLOBALS['ref_level'] > 0) && ($per < 100) && ($paymentMethod == 'REFERAL') && (isset($GLOBALS['cache_array']['add_userid'][$userid]))) {
134                                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid='.$userid.',data='.$GLOBALS['cache_array']['add_userid'][$userid].',ref_points='.$ref_points.',depth='.$GLOBALS['ref_level'].' - BEFORE!');
135                                 $ref_points = addRefbackPoints($GLOBALS['cache_array']['add_userid'][$userid], $userid, $points, $ref_points);
136                                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid='.$userid.',data='.$GLOBALS['cache_array']['add_userid'][$userid].',ref_points='.$ref_points.',depth='.$GLOBALS['ref_level'].' - AFTER!');
137                         } // END - if
138
139                         // Update points...
140                         if (is_null($GLOBALS['ref_level'])) {
141                                 // Level NULL (self)
142                                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_points` SET `%s`=`%s`+%s WHERE `userid`=%s AND `ref_depth` IS NULL LIMIT 1",
143                                         array(
144                                                 $pointsColumn,
145                                                 $pointsColumn,
146                                                 $ref_points,
147                                                 bigintval($userid)
148                                         ), __FUNCTION__, __LINE__);
149                         } else {
150                                 // Level 1+
151                                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_points` SET `%s`=`%s`+%s WHERE `userid`=%s AND `ref_depth`=%s LIMIT 1",
152                                         array(
153                                                 $pointsColumn,
154                                                 $pointsColumn,
155                                                 $ref_points,
156                                                 bigintval($userid),
157                                                 bigintval($GLOBALS['ref_level'])
158                                         ), __FUNCTION__, __LINE__);
159                         }
160                         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'pointsColumn='.$pointsColumn.',ref_points='.$ref_points.',userid='.$userid.',depth='.$GLOBALS['ref_level'].',mode='.$paymentMethod.' - UPDATE! ('.SQL_AFFECTEDROWS().')');
161
162                         // No entry updated?
163                         if (SQL_HASZEROAFFECTED()) {
164                                 // First ref in this level! :-)
165                                 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_points` (`userid`, `ref_depth`, `%s`) VALUES (%s, %s, %s)",
166                                         array(
167                                                 $pointsColumn,
168                                                 bigintval($userid),
169                                                 makeZeroToNull($GLOBALS['ref_level']),
170                                                 $ref_points
171                                         ), __FUNCTION__, __LINE__);
172                                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'data='.$pointsColumn.',ref_points='.$ref_points.',userid='.$userid.',depth='.$GLOBALS['ref_level'].',mode='.$paymentMethod.' - INSERTED! ('.SQL_AFFECTEDROWS().')');
173                         } // END - if
174
175                         // Check affected rows
176                         $added = SQL_AFFECTEDROWS();
177                         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'added='.intval($added));
178
179                         // Prepare data for the filter
180                         $filterData = array(
181                                 'subject'    => $subject,
182                                 'userid'     => $userid,
183                                 'points'     => $points,
184                                 'ref_points' => $ref_points,
185                                 'column'     => $pointsColumn,
186                                 'notify'     => $sendNotify,
187                                 'refid'      => $refid,
188                                 'locked'     => $locked,
189                                 'mode'       => 'add',
190                                 'add_mode'   => $paymentMethod,
191                                 'added'      => $added
192                         );
193
194                         // Filter it now
195                         $filterData = runFilterChain('post_add_points', $filterData);
196
197                         // Extract $added
198                         $added = $filterData['added'];
199                         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'added='.intval($added));
200
201                         // Points updated, maybe I shall send him an email?
202                         if (($sendNotify === true) && (isValidUserId(getUserData('refid'))) && ($locked === false)) {
203                                 // Prepare content
204                                 $content = array(
205                                         'percents' => $per,
206                                         'level'    => bigintval($GLOBALS['ref_level']),
207                                         'points'   => $ref_points,
208                                 );
209
210                                 // Load email template
211                                 $message = loadEmailTemplate('guest_user_confirmed_referal', $content, bigintval($userid));
212
213                                 // Send email
214                                 sendEmail($userid, '{--THANX_REFERAL_ONE_SUBJECT--}', $message);
215                         } elseif (($sendNotify === true) && (!isValidUserId(getUserData('refid'))) && ($locked === false) && ($paymentMethod == 'DIRECT')) {
216                                 // Prepare content
217                                 $content = array(
218                                         'reason'  => '{--REASON_DIRECT_PAYMENT--}',
219                                         'subject' => $subject,
220                                         'points'  => $ref_points
221                                 );
222
223                                 // Load message
224                                 $message = loadEmailTemplate('member_add_points', $content, $userid);
225
226                                 // And sent it away
227                                 sendEmail($userid, '{--DIRECT_PAYMENT_SUBJECT--}', $message);
228                                 if (!isGetRequestParameterSet('mid')) {
229                                         // Output message to admin
230                                         displayMessage('{--ADMIN_POINTS_ADDED--}');
231                                 } // END - if
232                         }
233
234                         // Increase referal level
235                         $GLOBALS['ref_level']++;
236                         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ' Referal level increased, ref_level=' . $GLOBALS['ref_level']);
237
238                         // Maybe there's another ref?
239                         if ((isValidUserId(getUserData('refid'))) && ($points > 0) && (getUserData('refid') != $userid) && ($paymentMethod == 'REFERAL')) {
240                                 // Then let's credit him here...
241                                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',refid=' . getUserData('refid') . ',points=' . $points . ',ref_points=' . $ref_points . ' - ADVANCE!');
242                                 $added = ($added && addPointsThroughReferalSystem(sprintf("%s_ref:%s", $subject, $GLOBALS['ref_level']), getUserData('refid'), $points, getUserData('refid')));
243                         } // END - if
244                 } // END - if
245         } // END - if
246
247         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'subject=' . $subject . ',userid=' . $userid . ',points=' . $points . ',sendNotify=' . intval($sendNotify) . ',refid=' . $refid . ',paymentMethod=' . $paymentMethod . ' - EXIT!');
248         return $added;
249 }
250
251 // Updates the referal counter
252 function updateReferalCounter ($userid) {
253         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ' - ENTERED!');
254         // Init referal id
255         $ref = NULL;
256
257         // Check for his referal
258         if (fetchUserData($userid)) {
259                 // Get it
260                 $ref = getUserData('refid');
261                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',ref=' . makeZeroToNull($ref) . ' - FETCHED!');
262         } // END - if
263
264         // Init entries
265         if (empty($GLOBALS['cache_array']['ref_level'][$userid])) {
266                 $GLOBALS['cache_array']['ref_level'][$userid] = NULL;
267         } // END - if
268         if (empty($GLOBALS['cache_array']['ref_level'][$ref])) {
269                 $GLOBALS['cache_array']['ref_level'][$ref] = NULL;
270         } // END - if
271
272         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',ref=' . makeZeroToNull($ref));
273
274         // When he has a referal...
275         if (($ref > 0) && ($ref != $userid)) {
276                 // Move to next referal level and count his counter one up
277                 $GLOBALS['cache_array']['ref_level'][$ref] = $GLOBALS['cache_array']['ref_level'][$userid] + 1;
278                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',ref(' . $ref . ')=' . $GLOBALS['cache_array']['ref_level'][$ref] . ' - ADVANCED!');
279
280                 // Update counter
281                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_refsystem` SET `counter`=`counter`+1 WHERE `userid`=%s AND `level`=%s LIMIT 1",
282                         array(
283                                 bigintval($ref),
284                                 bigintval($GLOBALS['cache_array']['ref_level'][$ref])
285                         ), __FUNCTION__, __LINE__);
286
287                 // When no entry was updated then we have to create it here
288                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ref=' . $ref . ',level=' . $GLOBALS['cache_array']['ref_level'][$ref] . ',updated=' . SQL_AFFECTEDROWS());
289                 if (SQL_HASZEROAFFECTED()) {
290                         // First count!
291                         SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_refsystem` (`userid`, `level`, `counter`) VALUES (%s,%s,1)",
292                                 array(
293                                         bigintval($ref),
294                                         makeZeroToNull($GLOBALS['cache_array']['ref_level'][$ref])
295                                 ), __FUNCTION__, __LINE__);
296                         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ref=' . $ref . ',level=' . $GLOBALS['cache_array']['ref_level'][$ref] . ',SQL_AFFECTEDROWS()=' . SQL_AFFECTEDROWS());
297                 } // END - if
298
299                 // Advance to next level
300                 updateReferalCounter($ref);
301         } elseif ((($ref == $userid) || ($ref == '0')) && (isExtensionInstalledAndNewer('cache', '0.1.2'))) {
302                 // Remove cache here
303                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ref=' . makeZeroToNull($ref) . ' - CACHE!');
304                 rebuildCache('refsystem', 'refsystem');
305         }
306
307         // Handle refback here if extension is installed
308         // @TODO Rewrite this to a filter
309         if (isExtensionActive('refback')) {
310                 updateRefbackTable($userid);
311         } // END - if
312         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',ref=' . makeZeroToNull($ref) . ',level=' . makeZeroToNull($GLOBALS['cache_array']['ref_level'][$ref]) . ' - EXIT!');
313 }
314
315 // Subtract points from database and mediadata cache
316 function subtractPoints ($subject, $userid, $points) {
317         // Add points to used points
318         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `used_points`=`used_points`+%s WHERE `userid`=%s LIMIT 1",
319                 array(
320                         $points,
321                         bigintval($userid)
322                 ), __FUNCTION__, __LINE__);
323
324         // Prepare filter data
325         $filterData = array(
326                 'subject' => $subject,
327                 'userid'  => $userid,
328                 'points'  => $points,
329                 'mode'    => 'sub',
330                 'added'   => (!SQL_HASZEROAFFECTED())
331         );
332
333         // Insert booking record
334         $filterData = runFilterChain('post_sub_points', $filterData);
335
336         // Return result
337         return $filterData['added'];
338 }
339 // "Getter" for array for user refs and points in given level
340 function getUserReferalPoints ($userid, $level) {
341         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ' - ENTERED!');
342         // Default is no refs and no nickname
343         $refs = array();
344
345         // Get refs from database
346         $result = SQL_QUERY_ESC("SELECT
347         ur.`id`, ur.`refid`, ud.`status`, ud.`last_online`, ud.`mails_confirmed`, ud.`emails_received`
348 FROM
349         `{?_MYSQL_PREFIX?}_user_refs` AS ur
350 LEFT JOIN
351         `{?_MYSQL_PREFIX?}_user_points` AS up
352 ON
353         ur.refid=up.userid AND
354         (ur.level=0 OR ur.level IS NULL)
355 LEFT JOIN
356         `{?_MYSQL_PREFIX?}_user_data` AS ud
357 ON
358         ur.`refid`=ud.`userid`
359 WHERE
360         ur.`userid`=%s AND
361         ur.`level`=%s
362 ORDER BY
363         ur.`refid` ASC",
364                 array(
365                         bigintval($userid),
366                         bigintval($level)
367                 ), __FUNCTION__, __LINE__);
368
369         // Are there some entries?
370         if (!SQL_HASZERONUMS($result)) {
371                 // Fetch all entries
372                 while ($row = SQL_FETCHARRAY($result)) {
373                         // Get total points of this user
374                         $row['points'] = getTotalPoints($row['refid']);
375
376                         // Get unconfirmed mails
377                         $row['unconfirmed']  = countSumTotalData($row['refid'], 'user_links', 'id', 'userid', true);
378
379                         // Init click rate with zero
380                         $row['click_rate'] = '0';
381
382                         // Is at least one mail received?
383                         if ($row['emails_received'] > 0) {
384                                 // Calculate click rate
385                                 $row['click_rate'] = ($row['mails_confirmed'] / $row['emails_received'] * 100);
386                         } // END - if
387
388                         // Activity is 'active' by default because if autopurge is not installed
389                         $row['activity'] = '{--MEMBER_ACTIVITY_ACTIVE--}';
390
391                         // Is autopurge installed and the user inactive?
392                         if ((isExtensionActive('autopurge')) && ((time() - getApInactiveSince()) >= $row['last_online']))  {
393                                 // Inactive user!
394                                 $row['activity'] = '{--MEMBER_ACTIVITY_INACTIVE--}';
395                         } // END - if
396
397                         // Remove some entries
398                         unset($row['mails_confirmed']);
399                         unset($row['emails_received']);
400                         unset($row['last_online']);
401
402                         // Add row
403                         $refs[$row['id']] = $row;
404                 } // END - while
405         } // END - if
406
407         // Free result
408         SQL_FREERESULT($result);
409
410         // Return result
411         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ' - EXIT!');
412         return $refs;
413 }
414
415 // Get points data for given subject
416 function getPointsDataArrayFromSubject ($subject) {
417         // Extension sql_patches must be up-to-date
418         if (isExtensionInstalledAndOlder('sql_patches', '0.8.2')) {
419                 // Please update ext-sql_patches
420                 debug_report_bug(__FUNCTION__, __LINE__, 'sql_patches is out-dated. Please update to at least 0.8.2 to continue. subject=' . $subject);
421         } // END - if
422
423         // Remove any double-dot from it
424         $subjectArray = explode(':', $subject);
425         $subject = $subjectArray[0];
426         unset($subjectArray);
427
428         // If we have cache, shortcut it here
429         if (isset($GLOBALS['cache_array']['points_data'][$subject])) {
430                 // Return it
431                 return $GLOBALS['cache_array']['points_data'][$subject];
432         } // END - if
433
434         // Now checkout the entry in database table
435         $result = SQL_QUERY_ESC("SELECT `id`, `subject`, `column_name`, `locked_mode`, `payment_method`, `notify_recipient` FROM `{?_MYSQL_PREFIX?}_points_data` WHERE `subject`='%s' LIMIT 1",
436                 array($subject), __FUNCTION__, __LINE__);
437
438         // Do we have an entry?
439         if (SQL_NUMROWS($result) == 1) {
440                 // Then load it
441                 $pointsData = SQL_FETCHARRAY($result);
442
443                 // Add all entries to our cache array
444                 foreach ($pointsData as $key=>$value) {
445                         $GLOBALS['cache_array']['points_data'][$subject][$key] = $value;
446                 } // END - foreach
447         } else {
448                 // Register this automatically
449                 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_points_data` (`subject`, `column_name`, `locked_mode`, `payment_method`, `notify_recipient`) VALUES ('%s','points','LOCKED','REFERAL','N')",
450                         array($subject), __FUNCTION__, __LINE__);
451
452                 // Re-request it
453                 return getPointsDataArrayFromSubject($subject);
454         }
455
456         // Free result
457         SQL_FREERESULT($result);
458
459         // Return it
460         return $GLOBALS['cache_array']['points_data'][$subject];
461 }
462
463 // Determines the right points column name for given subject and 'locked'
464 function getPointsColumnNameFromSubjectLocked ($subject, $isLocked) {
465         // Get the points_data entry
466         $pointsData = getPointsDataArrayFromSubject($subject);
467
468         // Regular points by default
469         $columnName = $pointsData['column_name'];
470
471         // Are the points locked?
472         if (($isLocked === true) && ($pointsData['locked_mode'] == 'LOCKED')) {
473                 // Locked points, so prefix it
474                 $columnName = 'locked_' . $pointsData['column_name'];
475         } // END - if
476
477         // Return the result
478         return $columnName;
479 }
480
481 // Determines the payment method for given extension and 'locked'
482 function getPaymentMethodFromSubject ($subject) {
483         // Get the points_data entry
484         $pointsData = getPointsDataArrayFromSubject($subject);
485
486         // Regular points by default
487         $paymentMethod = $pointsData['payment_method'];
488
489         // Return the result
490         return $paymentMethod;
491 }
492
493 // Checks wether notification of points recipient is enabled
494 function isPaymentRecipientNotificationEnabled ($subject) {
495         // Get the points_data entry
496         $pointsData = getPointsDataArrayFromSubject($subject);
497
498         // Is it enabled?
499         $isEnabled = ($pointsData['notify_recipient'] == 'Y');
500
501         // Return the result
502         return $isEnabled;
503 }
504
505 // [EOF]
506 ?>