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