beae7944a22e7d32b1c66ee299641e55687683f1
[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 - 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 } elseif ((!isHtmlOutputMode()) || (!isDailyResetEnabled())) {
42         // Do not execute when script is in non-HTML mode
43         return;
44 } elseif (!isExtensionActive('birthday')) {
45         // Extension not active/installed
46         if (isDebugModeEnabled()) logDebugMessage(__FILE__, __LINE__, 'Not resetting, needed extension ext-birthday 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 variables
59 $lastOnline = '';
60 $excludeSql = '';
61
62 // Shall I include only active members?
63 if ((getConfig('birthday_active')  == 'Y') && (isExtensionActive('autopurge')) && ((getApInactiveSince() > 0))) {
64         $excludeSql = ' AND (UNIX_TIMESTAMP() - `d`.`last_online`) < {?ap_inactive_since?}';
65 } // END - if
66
67 // Is ext-holiday installed?
68 // @TODO Rewrite these if() blocks to a filter
69 if (isExtensionActive('holiday')) {
70         // Exclude those as well
71         $excludeSql .= " AND `d`.`holiday_active`='N'";
72 } // END - if
73
74 // Only confirmed members shall receive birthday mails...
75 $result_birthday = SQL_QUERY_ESC("SELECT
76         `d`.`userid`,
77         `d`.`email`,
78         `d`.`birth_year`
79 FROM
80         `{?_MYSQL_PREFIX?}_user_data` AS `d`
81 WHERE
82         `d`.`status`='CONFIRMED'
83         " . runFilterChain('user_exclusion_sql', $excludeSql) . " AND
84         `d`.`birth_day`=%s AND
85         `d`.`birth_month`=%s AND
86         `d`.`birthday_sent` < (UNIX_TIMESTAMP() - ({?ONE_DAY?} * 364))
87         " . $lastOnline . "
88 ORDER BY
89         `d`.`userid` ASC",
90         array($day, $month), __FILE__, __LINE__);
91
92 if (!SQL_HASZERONUMS($result_birthday)) {
93         // Start sending out birthday mails
94         while ($content = SQL_FETCHARRAY($result_birthday)) {
95                 // Calculate own timestamp for birthday and today
96                 $bd  = $content['birth_year'] + 12 * $month + 365 * $day;
97                 $now = $year  + 12 * $month + 365 * $day;
98
99                 // Simply subtract both values and you got the age... :)
100                 $age = $now - $bd;
101
102                 if (getBirthdayPoints() > 0) {
103                         // Add more entries to the array
104                         $content['age']    = $age;
105                         $content['check']  = '';
106
107                         // @TODO 4 is hard-coded here, should we move it out in config?
108                         for ($idx = 0; $idx < 4; $idx++) {
109                                 $content['check'] .= generateRandomCode('8', mt_rand(0, $month . $day), $content['userid'], ($age * ($idx + 1)));
110                         } // END - for
111
112                         // Insert row into database
113                         SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_birthday` (`userid`, `points`, `chk_value`) VALUES (%s,{?birthday_points?},'%s' )",
114                                 array(
115                                         bigintval($content['userid']),
116                                         $content['check']
117                                 ), __FILE__, __LINE__);
118
119                         // Load email template with confirmation link
120                         $message = loadEmailTemplate('member_birthday_confirm', $content, bigintval($content['userid']));
121                 } else {
122                         // Load default email template and fill in the age
123                         $message = loadEmailTemplate('member_birthday', $content, $content['userid']);
124                 }
125
126                 // Send email
127                 sendEmail($content['userid'], '{--MEMBER_HAPPY_BIRTHDAY_SUBJECT--}', $message);
128
129                 // Remember him that he has received a birthday mail
130                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `birthday_sent`=UNIX_TIMESTAMP() WHERE `userid`=%s LIMIT 1",
131                         array(bigintval($content['userid'])), __FILE__, __LINE__);
132         } // END - while
133
134         // Free memory
135         SQL_FREERESULT($result_birthday);
136 } // END - if
137
138 // Debug line
139 //* DEBUG: */ logDebugMessage(__FILE__, __LINE__, 'Daily reset ended.');
140
141 // [EOF]
142 ?>