A lot has been rewritten, ext-teams added, ext-forced continued:
[mailer.git] / inc / daily / daily_birthday.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 10/24/2009 *
4  * ===================                          Last change: 06/20/2010 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : daily_birthday.php                               *
8  * -------------------------------------------------------------------- *
9  * Short description : Sends out birthday mails on reset                *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Versendet Geburtstagsmails beim Reset            *
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 } elseif ((!isHtmlOutputMode()) || (!isResetModeEnabled())) {
42         // Do not execute when script is in CSS mode
43         return;
44 } elseif (!isExtensionActive('birthday')) {
45         // Extension not active/installed
46         logDebugMessage(__FILE__, __LINE__, 'Not resetting, needed extension disabled.');
47         return;
48 }
49
50 // Debug line
51 //* DEBUG: */ logDebugMessage(__FILE__, __LINE__, 'Daily reset started.');
52
53 // Get current day (01 to 31), month (01 to 12) and year (4-digits year)
54 $day   = getDay();
55 $month = getMonth();
56 $year  = getYear();
57
58 // Init variable
59 $lastOnline = '';
60
61 // Shall I include only active members?
62 if ((getConfig('birthday_active')  == 'Y') && (isExtensionActive('autopurge')) && ((getApInactiveSince() > 0))) {
63         $add = ' AND (UNIX_TIMESTAMP() - `last_online`) < {?ap_inactive_since?}';
64 } // END - if
65
66 // Only confirmed members shall receive birthday mails...
67 $result_birthday = SQL_QUERY_ESC("SELECT
68         `userid`,`email`,`birth_year`
69 FROM
70         `{?_MYSQL_PREFIX?}_user_data`
71 WHERE
72         `status`='CONFIRMED' AND
73         `birth_day`=%s AND
74         `birth_month`=%s AND
75         `birthday_sent` < (UNIX_TIMESTAMP() - ({?ONE_DAY?} * 364))
76         ".$lastOnline."
77 ORDER BY
78         `userid` ASC",
79         array($day, $month), __FILE__, __LINE__);
80
81 if (!SQL_HASZERONUMS($result_birthday)) {
82         // Start sending out birthday mails
83         while ($content = SQL_FETCHARRAY($result_birthday)) {
84                 // Calculate own timestamp for birthday and today
85                 $bd  = $content['birth_year'] + 12 * $month + 365 * $day;
86                 $now = $year  + 12 * $month + 365 * $day;
87
88                 // Simply subtract both values and you got the age... :)
89                 $age = $now - $bd;
90
91                 if (getConfig('birthday_points') > 0) {
92                         // Add more entries to the array
93                         $content['age']    = $age;
94                         $content['check']  = '';
95
96                         // @TODO 4 is hard-coded here, should we move it out in config?
97                         for ($idx = '0'; $idx < 4; $idx++) {
98                                 $content['check'] .= generateRandomCode('8', mt_rand(0, $month . $day), $content['userid'], ($age * ($idx + 1)));
99                         } // END - for
100
101                         // Insert row into database
102                         SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_birthday` (`userid`,`points`,`chk_value`) VALUES (%s,{?birthday_points?},'%s' )",
103                                 array(
104                                         bigintval($content['userid']),
105                                         $content['check']
106                                 ), __FILE__, __LINE__);
107
108                         // Load email template with confirmation link
109                         $message = loadEmailTemplate('member_birthday_confirm', $content, bigintval($content['userid']));
110                 } else {
111                         // Load default email template and fill in the age
112                         $message = loadEmailTemplate('member_birthday', $age, $content['userid']);
113                 }
114
115                 // Send email
116                 sendEmail($content['userid'], '{--MEMBER_HAPPY_BIRTHDAY_SUBJECT--}', $message);
117
118                 // Remember him that he has received a birthday mail
119                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `birthday_sent`=UNIX_TIMESTAMP() WHERE `userid`=%s LIMIT 1",
120                         array(bigintval($content['userid'])), __FILE__, __LINE__);
121         } // END - while
122
123         // Free memory
124         SQL_FREERESULT($result_birthday);
125 } // END - if
126
127 // Debug line
128 //* DEBUG: */ logDebugMessage(__FILE__, __LINE__, 'Daily reset ended.');
129
130 // [EOF]
131 ?>