More improvements:
[mailer.git] / inc / libs / bonus_functions.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 11/10/2004 *
4  * ===================                          Last change: 03/18/2005 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : bonus_functions.php                              *
8  * -------------------------------------------------------------------- *
9  * Short description : Special functions for bonus extension            *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Spezielle Funktion fuer bonus-Erweiterung        *
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 - 2012 by Mailer Developer Team                   *
20  * For more information visit: http://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 // This function must be run *BEFORE* a link is removed from table 'mailer_user_links' !
44 function addTurboBonus ($mid, $userid, $type) {
45         // Shall we add bonus points?
46         if (!isBonusRallyeActive()) {
47                 return FALSE;
48         } // END - if
49
50         // Init variables
51         $sql = ''; $bonus = '0'; $mail = '0'; $column = '';
52
53         // Select SQL command
54         switch ($type) {
55                 case 'bonusid':
56                         $column = 'bonus_id';
57                         $bonus = $mid;
58                         break;
59
60                 case 'mailid' :
61                         $column = 'mail_id';
62                         $mail = $mid;
63                         break;
64
65                 default:
66                         logDebugMessage(__FUNCTION__, __LINE__, sprintf("Invalid type %s detected.", $type));
67                         break;
68         } // END - switch
69
70         // Is a column name set?
71         if (empty($column)) {
72                 // No, then abort here
73                 return FALSE;
74         } // END - if
75
76         // Check for entry
77         $rank = countSumTotalData($mid, 'bonus_turbo', 'id', $column, TRUE) + 1;
78
79         // Which rank?
80         if ($rank == 1) {
81                 // First rank!
82                 $points = getTurboBonus();
83         } else {
84                 // Anything else so let's explode all entered rank points
85                 $test = explode(';', getTurboRates());
86                 if (!empty($test[$rank - 2])) {
87                         // Level found
88                         $points = $test[$rank - 2];
89                 } else {
90                         // Level not found
91                         $points = '0.00000';
92                 }
93         }
94
95         // Add points to his account directly
96         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `turbo_bonus`=`turbo_bonus`+%s WHERE `userid`=%s LIMIT 1",
97                 array(
98                         $points,
99                         bigintval($userid)
100                 ), __FUNCTION__, __LINE__);
101
102         // Rember this whole data for displaying ranking list
103         SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_bonus_turbo` (`userid`, `mail_id`, `bonus_id`, `level`, `points`, `timemark`) VALUES (%s, %s, %s, %s, %s, UNIX_TIMESTAMP())",
104                 array(
105                         bigintval($userid),
106                         bigintval($mail),
107                         bigintval($bonus),
108                         $rank,
109                         $points
110                 ), __FUNCTION__, __LINE__);
111
112         if ((isExtensionInstalledAndNewer('bonus', '0.3.5')) && (getBonusMode() != 'ADD') && ($points > 0)) {
113                 handleBonusPoints($points, $userid);
114         } // END - if
115 }
116
117 //
118 function addBonusRanks ($data, $type, $userid) {
119         // Init variables
120         $self = FALSE; $OUT = ''; $GLOBALS['ranking_content'] = array();
121
122         // Clear rankings by default
123         $GLOBALS['ranking_content']['rankings'] = '';
124
125         // How many ranks do we have?
126         $ranks = count(explode(';', getTurboRates())) + 1;
127
128         // Load current user's data
129         $result = SQL_QUERY_ESC("SELECT `level`, `points`, `timemark` FROM `{?_MYSQL_PREFIX?}_bonus_turbo` WHERE `%s`=%s AND `userid`=%s LIMIT 1",
130                 array(
131                         $type,
132                         $data,
133                         bigintval($userid)
134                 ), __FUNCTION__, __LINE__);
135
136         // Entry found?
137         if (SQL_NUMROWS($result) == 1) {
138                 // Load data
139                 $GLOBALS['ranking_content'] = merge_array($GLOBALS['ranking_content'], SQL_FETCHARRAY($result));
140
141                 // Remember all values for later use
142                 $self  = TRUE;
143
144                 // Transfer data to template
145                 $GLOBALS['ranking_content']['timemark'] = generateDateTime($GLOBALS['ranking_content']['timemark'], 1);
146
147                 // Load template
148                 $GLOBALS['ranking_content']['own'] = loadTemplate('show_bonus_yr', TRUE, $GLOBALS['ranking_content']);
149         } // END - if
150
151         // Load rankings
152         $result = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_bonus_turbo` WHERE `%s`=%s ORDER BY `level` ASC LIMIT {?bonus_lines?}",
153                 array($type, $data), __FUNCTION__, __LINE__);
154         if (!SQL_HASZERONUMS($result)) {
155                 // Output all ranks (levels)
156                 for ($rank = 1; $rank <= SQL_NUMROWS($result); $rank++) {
157                         // Load data
158                         $result_users = SQL_QUERY_ESC("SELECT
159         `userid`,
160         `points`
161 FROM
162         `{?_MYSQL_PREFIX?}_bonus_turbo`
163 WHERE
164         `%s`=%s AND
165         `level`=%s
166 LIMIT 1",
167                                 array($type, $data, $rank), __FUNCTION__, __LINE__);
168
169                         // Nothing found by default
170                         $rows['userid'] = '---';
171                         $rows['points'] = '---';
172
173                         // Are you one of them?
174                         if (SQL_NUMROWS($result_users) == 1) {
175                                 // Load data
176                                 $rows = merge_array($rows, SQL_FETCHARRAY($result_users));
177                         } // END - if
178
179                         // Free result
180                         SQL_FREERESULT($result_users);
181
182                         // Add more
183                         $rows['rank'] = $rank;
184
185                         // Load row template
186                         $OUT .= loadTemplate('member_bonus_turbo_row', TRUE, $rows);
187                 } // END - for
188
189                 if ($self === FALSE) {
190                         // If current user was not found set constant
191                         $GLOBALS['ranking_content']['rankings'] = '{--MEMBER_BONUS_RANK_YOU_ARE_404--}';
192                 } // END - if
193         } else {
194                 // No entries found
195                 // @TODO Move this HTML to a template
196                 $OUT = '<tr>
197   <td colspan="3" align="center" height="30" class="bottom">
198     <div class="bad">{%message,MEMBER_BONUS_NO_RANKS=' . $data . '%}</div>
199   </td>
200 </tr>';
201         }
202
203         // Retutn content
204         return $OUT;
205 }
206
207 // Hanle any bonus points the given user shall become
208 function handleBonusPoints ($mode, $userid) {
209         // Shall we add bonus points?
210         if (!isBonusRallyeActive()) {
211                 return;
212         } // END - if
213
214         // Default is not working
215         $return = FALSE;
216
217         // Switch to jackpot-mode when no UID is supplied but userid-mode is selected
218         if ((getBonusMode() == 'UID') && ($userid == '0') && (isExtensionActive('jackpot'))) {
219                 // Update database & config
220                 updateConfiguration('bonus_mode', 'JACKPOT');
221         } // END - if
222
223         if ($mode == 'login_bonus') {
224                 // Login bonus detected
225                 $points = getLoginBonus();
226         } else {
227                 // Direct points supplied
228                 $points = $mode;
229         }
230
231         // Check his amount first
232         $total = getTotalPoints($userid);
233
234         // Subtract points from...
235         switch (getBonusMode()) {
236                 case 'ADD': // Only add them (no subtraction)
237                         // Ignored
238                         break;
239
240                 case 'JACKPOT': // ... jackpot
241                         if ((isExtensionActive('jackpot')) && (subtractPointsFromJackpot($points) === FALSE) && (isValidUserId($userid))) {
242                                 if ($total >= $points) {
243                                         // Subtract points from userid's account
244                                         $return = subtractPointsFromJackpot('bonus_payout_jackpot', $userid, $points);
245                                 } // END - if
246                         } // END - if
247                         break;
248
249                 case 'UID': // ... userid's account
250                         if ($total >= $points) {
251                                 // Subtract points from userid's account
252                                 $return = subtractPoints('bonus_payout_userid', $userid, $points);
253                         } elseif (isExtensionActive('jackpot')) {
254                                 // Try to subtract from jackpot
255                                 $return = subtractPointsFromJackpot($points);
256                         }
257                         break;
258
259                 default: // This should not happen
260                         reportBug(__FUNCTION__, __LINE__, 'Invalid bonus-mode ' . getBonusMode() . ' detected.');
261                         break;
262         } // END - switch
263
264         // Return status
265         return $return;
266 }
267
268 // Purges expired fast-click bonus entries
269 function purgeExpiredTurboBonus() {
270         // Remove entries
271         $result = SQL_QUERY('DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_bonus_turbo` WHERE (UNIX_TIMESTAMP() - `timemark`) >= {?bonus_timeout?}', __FUNCTION__, __LINE__);
272
273         if (!SQL_HASZEROAFFECTED()) {
274                 // Send out email to admin
275                 sendAdminNotification('{--ADMIN_AUTOPURGE_TURBO_SUBJECT--}', 'admin_autopurge_turbo', SQL_AFFECTEDROWS());
276         } // END - if
277 }
278
279 //-----------------------------------------------------------------------------
280 //                             Wrapper Functions
281 //-----------------------------------------------------------------------------
282
283 // Determines whether the "bonus rallye" is active
284 function isBonusRallyeActive () {
285         // Is there cache?
286         if (!isset($GLOBALS['bonus_rallye_active'])) {
287                 // Just determine it
288                 $GLOBALS['bonus_rallye_active'] = (getBonusActive() == 'Y');
289         } // END - if
290
291         // Return cache
292         return $GLOBALS['bonus_rallye_active'];
293 }
294
295 // Determines whether the "bonus new_member_notify" is active
296 function isBonusNewMemberNotifyEnabled () {
297         // Is there cache?
298         if (!isset($GLOBALS['bonus_new_member_notify_active'])) {
299                 // Just determine it
300                 $GLOBALS['bonus_new_member_notify_active'] = (getBonusNewMemberNotify() == 'Y');
301         } // END - if
302
303         // Return cache
304         return $GLOBALS['bonus_new_member_notify_active'];
305 }
306
307 // Getter for bonus_timeout
308 function getBonusTimeout () {
309         // Is there cache?
310         if (!isset($GLOBALS[__FUNCTION__])) {
311                 // Determine it
312                 $GLOBALS[__FUNCTION__] = getConfig('bonus_timeout');
313         } // END - if
314
315         // Return cache
316         return $GLOBALS[__FUNCTION__];
317 }
318
319 // Getter for bonus_mode
320 function getBonusMode () {
321         // Is there cache?
322         if (!isset($GLOBALS[__FUNCTION__])) {
323                 // Determine it
324                 $GLOBALS[__FUNCTION__] = getConfig('bonus_mode');
325         } // END - if
326
327         // Return cache
328         return $GLOBALS[__FUNCTION__];
329 }
330
331 // Getter for bonus_ranks
332 function getBonusRanks () {
333         // Is there cache?
334         if (!isset($GLOBALS[__FUNCTION__])) {
335                 // Determine it
336                 $GLOBALS[__FUNCTION__] = getConfig('bonus_ranks');
337         } // END - if
338
339         // Return cache
340         return $GLOBALS[__FUNCTION__];
341 }
342
343 // Getter for turbo_rates
344 function getTurboRates () {
345         // Is there cache?
346         if (!isset($GLOBALS[__FUNCTION__])) {
347                 // Determine it
348                 $GLOBALS[__FUNCTION__] = getConfig('turbo_rates');
349         } // END - if
350
351         // Return cache
352         return $GLOBALS[__FUNCTION__];
353 }
354
355 // Getter for login_timeout
356 function getLoginTimeout () {
357         // Is there cache?
358         if (!isset($GLOBALS[__FUNCTION__])) {
359                 // Determine it
360                 $GLOBALS[__FUNCTION__] = getConfig('login_timeout');
361         } // END - if
362
363         // Return cache
364         return $GLOBALS[__FUNCTION__];
365 }
366
367 // Getter for login_bonus
368 function getLoginBonus () {
369         // Is there cache?
370         if (!isset($GLOBALS[__FUNCTION__])) {
371                 // Determine it
372                 $GLOBALS[__FUNCTION__] = getConfig('login_bonus');
373         } // END - if
374
375         // Return cache
376         return $GLOBALS[__FUNCTION__];
377 }
378
379 // Getter for turbo_bonus
380 function getTurboBonus () {
381         // Is there cache?
382         if (!isset($GLOBALS[__FUNCTION__])) {
383                 // Determine it
384                 $GLOBALS[__FUNCTION__] = getConfig('turbo_bonus');
385         } // END - if
386
387         // Return cache
388         return $GLOBALS[__FUNCTION__];
389 }
390
391 // Getter for bonus_ref
392 function getBonusRef () {
393         // Is there cache?
394         if (!isset($GLOBALS[__FUNCTION__])) {
395                 // Determine it
396                 $GLOBALS[__FUNCTION__] = getConfig('bonus_ref');
397         } // END - if
398
399         // Return cache
400         return $GLOBALS[__FUNCTION__];
401 }
402
403 // Getter for bonus_userid
404 function getBonusUserid () {
405         // Is there cache?
406         if (!isset($GLOBALS[__FUNCTION__])) {
407                 // Determine it
408                 $GLOBALS[__FUNCTION__] = getConfig('bonus_userid');
409         } // END - if
410
411         // Return cache
412         return $GLOBALS[__FUNCTION__];
413 }
414
415 // Getter for bonus_active
416 function getBonusActive () {
417         // Is there cache?
418         if (!isset($GLOBALS[__FUNCTION__])) {
419                 // Determine it
420                 $GLOBALS[__FUNCTION__] = getConfig('bonus_active');
421         } // END - if
422
423         // Return cache
424         return $GLOBALS[__FUNCTION__];
425 }
426
427 // Getter for bonus_notify_points
428 function getBonusNotifyPoints () {
429         // Is there cache?
430         if (!isset($GLOBALS[__FUNCTION__])) {
431                 // Determine it
432                 $GLOBALS[__FUNCTION__] = getConfig('bonus_notify_points');
433         } // END - if
434
435         // Return cache
436         return $GLOBALS[__FUNCTION__];
437 }
438
439 // Getter for bonus_new_member_notify
440 function getBonusNewMemberNotify () {
441         // Is there cache?
442         if (!isset($GLOBALS[__FUNCTION__])) {
443                 // Determine it
444                 $GLOBALS[__FUNCTION__] = getConfig('bonus_new_member_notify');
445         } // END - if
446
447         // Return cache
448         return $GLOBALS[__FUNCTION__];
449 }
450
451 // Getter for bonus_stats
452 function getBonusStats () {
453         // Is there cache?
454         if (!isset($GLOBALS[__FUNCTION__])) {
455                 // Determine it
456                 $GLOBALS[__FUNCTION__] = getConfig('bonus_stats');
457         } // END - if
458
459         // Return cache
460         return $GLOBALS[__FUNCTION__];
461 }
462
463 // Getter for bonus_order
464 function getBonusOrder () {
465         // Is there cache?
466         if (!isset($GLOBALS[__FUNCTION__])) {
467                 // Determine it
468                 $GLOBALS[__FUNCTION__] = getConfig('bonus_order');
469         } // END - if
470
471         // Return cache
472         return $GLOBALS[__FUNCTION__];
473 }
474
475 // Getter for bonus_lines
476 function getBonusLines () {
477         // Is there cache?
478         if (!isset($GLOBALS[__FUNCTION__])) {
479                 // Determine it
480                 $GLOBALS[__FUNCTION__] = getConfig('bonus_lines');
481         } // END - if
482
483         // Return cache
484         return $GLOBALS[__FUNCTION__];
485 }
486
487 // Getter for bonus_include_own
488 function getBonusIncludeOwn () {
489         // Is there cache?
490         if (!isset($GLOBALS[__FUNCTION__])) {
491                 // Determine it
492                 $GLOBALS[__FUNCTION__] = getConfig('bonus_include_own');
493         } // END - if
494
495         // Return cache
496         return $GLOBALS[__FUNCTION__];
497 }
498
499 // Checks whether bonus_include_own is set to 'Y'
500 function isBonusIncludeOwnEnabled () {
501         // Is there cache?
502         if (!isset($GLOBALS[__FUNCTION__])) {
503                 // Determine it
504                 $GLOBALS[__FUNCTION__] = (getBonusIncludeOwn() == 'Y');
505         } // END - if
506
507         // Return cache
508         return $GLOBALS[__FUNCTION__];
509 }
510
511 // Getter for bonus_disable_notify
512 function getBonusDisableNotify () {
513         // Is there cache?
514         if (!isset($GLOBALS[__FUNCTION__])) {
515                 // Determine it
516                 $GLOBALS[__FUNCTION__] = getConfig('bonus_disable_notify');
517         } // END - if
518
519         // Return cache
520         return $GLOBALS[__FUNCTION__];
521 }
522
523 // Getter for bonus_enable_notify
524 function getBonusEnableNotify () {
525         // Is there cache?
526         if (!isset($GLOBALS[__FUNCTION__])) {
527                 // Determine it
528                 $GLOBALS[__FUNCTION__] = getConfig('bonus_enable_notify');
529         } // END - if
530
531         // Return cache
532         return $GLOBALS[__FUNCTION__];
533 }
534
535 // Getter for include_bonus_click
536 function getIncludeBonusClick () {
537         // Is there cache?
538         if (!isset($GLOBALS[__FUNCTION__])) {
539                 // Determine it
540                 $GLOBALS[__FUNCTION__] = getConfig('include_bonus_click');
541         } // END - if
542
543         // Return cache
544         return $GLOBALS[__FUNCTION__];
545 }
546
547 // Checks whether include_bonus_click is set to 'Y'
548 function isIncludeBonusClickEnabled () {
549         // Is there cache?
550         if (!isset($GLOBALS[__FUNCTION__])) {
551                 // Determine it
552                 $GLOBALS[__FUNCTION__] = (getIncludeBonusClick() == 'Y');
553         } // END - if
554
555         // Return cache
556         return $GLOBALS[__FUNCTION__];
557 }
558
559 // Getter for include_bonus_login
560 function getIncludeBonusLogin () {
561         // Is there cache?
562         if (!isset($GLOBALS[__FUNCTION__])) {
563                 // Determine it
564                 $GLOBALS[__FUNCTION__] = getConfig('include_bonus_login');
565         } // END - if
566
567         // Return cache
568         return $GLOBALS[__FUNCTION__];
569 }
570
571 // Checks whether include_bonus_login is set to 'Y'
572 function isIncludeBonusLoginEnabled () {
573         // Is there cache?
574         if (!isset($GLOBALS[__FUNCTION__])) {
575                 // Determine it
576                 $GLOBALS[__FUNCTION__] = (getIncludeBonusLogin() == 'Y');
577         } // END - if
578
579         // Return cache
580         return $GLOBALS[__FUNCTION__];
581 }
582
583 // Getter for include_bonus_order
584 function getIncludeBonusOrder () {
585         // Is there cache?
586         if (!isset($GLOBALS[__FUNCTION__])) {
587                 // Determine it
588                 $GLOBALS[__FUNCTION__] = getConfig('include_bonus_order');
589         } // END - if
590
591         // Return cache
592         return $GLOBALS[__FUNCTION__];
593 }
594
595 // Checks whether include_bonus_order is set to 'Y'
596 function isIncludeBonusOrderEnabled () {
597         // Is there cache?
598         if (!isset($GLOBALS[__FUNCTION__])) {
599                 // Determine it
600                 $GLOBALS[__FUNCTION__] = (getIncludeBonusOrder() == 'Y');
601         } // END - if
602
603         // Return cache
604         return $GLOBALS[__FUNCTION__];
605 }
606
607 // Getter for include_bonus_Ref
608 function getIncludeBonusRef () {
609         // Is there cache?
610         if (!isset($GLOBALS[__FUNCTION__])) {
611                 // Determine it
612                 $GLOBALS[__FUNCTION__] = getConfig('include_bonus_ref');
613         } // END - if
614
615         // Return cache
616         return $GLOBALS[__FUNCTION__];
617 }
618
619 // Checks whether include_bonus_ref is set to 'Y'
620 function isIncludeBonusRefEnabled () {
621         // Is there cache?
622         if (!isset($GLOBALS[__FUNCTION__])) {
623                 // Determine it
624                 $GLOBALS[__FUNCTION__] = (getIncludeBonusRef() == 'Y');
625         } // END - if
626
627         // Return cache
628         return $GLOBALS[__FUNCTION__];
629 }
630
631 // Getter for include_bonus_stats
632 function getIncludeBonusStats () {
633         // Is there cache?
634         if (!isset($GLOBALS[__FUNCTION__])) {
635                 // Determine it
636                 $GLOBALS[__FUNCTION__] = getConfig('include_bonus_stats');
637         } // END - if
638
639         // Return cache
640         return $GLOBALS[__FUNCTION__];
641 }
642
643 // Checks whether include_bonus_stats is set to 'Y'
644 function isIncludeBonusStatsEnabled () {
645         // Is there cache?
646         if (!isset($GLOBALS[__FUNCTION__])) {
647                 // Determine it
648                 $GLOBALS[__FUNCTION__] = (getIncludeBonusStats() == 'Y');
649         } // END - if
650
651         // Return cache
652         return $GLOBALS[__FUNCTION__];
653 }
654
655 // [EOF]
656 ?>