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