Merge branch 'contrib' into 0.2.1-FINAL
[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 - 2013 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 // @TODO Move this out to an extension
64 if ((getConfig('birthday_active')  == 'Y') && (isExtensionActive('autopurge')) && ((getApInactiveSince() > 0))) {
65         $excludeSql = ' AND (UNIX_TIMESTAMP() - `d`.`last_online`) < {?ap_inactive_since?}';
66 } // END - if
67
68 // Only confirmed members shall receive birthday mails...
69 $result_birthday = sqlQueryEscaped("SELECT
70         `d`.`userid`,
71         `d`.`email`,
72         `d`.`birth_year`
73 FROM
74         `{?_MYSQL_PREFIX?}_user_data` AS `d`
75 WHERE
76         `d`.`status`='CONFIRMED'
77         " . runFilterChain('user_exclusion_sql', ' ' . $excludeSql) . " AND
78         `d`.`birth_day`=%s AND
79         `d`.`birth_month`=%s AND
80         `d`.`birthday_sent` < (UNIX_TIMESTAMP() - ({?ONE_DAY?} * 364))
81         " . $lastOnline . "
82 ORDER BY
83         `d`.`userid` ASC",
84         array($day, $month), __FILE__, __LINE__);
85
86 if (!ifSqlHasZeroNums($result_birthday)) {
87         // Start sending out birthday mails
88         while ($content = sqlFetchArray($result_birthday)) {
89                 // Calculate own timestamp for birthday and today
90                 $bd  = $content['birth_year'] + 12 * $month + 365 * $day;
91                 $now = $year  + 12 * $month + 365 * $day;
92
93                 // Simply subtract both values and you got the age... :)
94                 $age = $now - $bd;
95
96                 if (getBirthdayPoints() > 0) {
97                         // Add more entries to the array
98                         $content['age']    = $age;
99                         $content['check']  = '';
100
101                         // Generate long random code
102                         for ($idx = 0; $idx < 4; $idx++) {
103                                 $content['check'] .= generateRandomCode('8', mt_rand(0, $month . $day), $content['userid'], ($age * ($idx + 1)));
104                         } // END - for
105
106                         // Insert row into database
107                         sqlQueryEscaped("INSERT INTO `{?_MYSQL_PREFIX?}_user_birthday` (`userid`, `points`, `chk_value`) VALUES (%s,{?birthday_points?},'%s' )",
108                                 array(
109                                         bigintval($content['userid']),
110                                         $content['check']
111                                 ), __FILE__, __LINE__);
112
113                         // Load email template with confirmation link
114                         $message = loadEmailTemplate('member_birthday_confirm', $content, bigintval($content['userid']));
115                 } else {
116                         // Load default email template and fill in the age
117                         $message = loadEmailTemplate('member_birthday', $content, $content['userid']);
118                 }
119
120                 // Send email
121                 sendEmail($content['userid'], '{--MEMBER_HAPPY_BIRTHDAY_SUBJECT--}', $message);
122
123                 // Remember him that he has received a birthday mail
124                 sqlQueryEscaped("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `birthday_sent`=UNIX_TIMESTAMP() WHERE `userid`=%s LIMIT 1",
125                         array(bigintval($content['userid'])), __FILE__, __LINE__);
126         } // END - while
127
128         // Free memory
129         sqlFreeResult($result_birthday);
130 } // END - if
131
132 // Debug line
133 //* DEBUG: */ logDebugMessage(__FILE__, __LINE__, 'Daily reset ended.');
134
135 // [EOF]
136 ?>