First batch of removal of the headers needed for revision-functions.php
[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  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
14  * Copyright (c) 2009 - 2013 by Mailer Developer Team                   *
15  * For more information visit: http://mxchange.org                      *
16  *                                                                      *
17  * This program is free software; you can redistribute it and/or modify *
18  * it under the terms of the GNU General Public License as published by *
19  * the Free Software Foundation; either version 2 of the License, or    *
20  * (at your option) any later version.                                  *
21  *                                                                      *
22  * This program is distributed in the hope that it will be useful,      *
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
25  * GNU General Public License for more details.                         *
26  *                                                                      *
27  * You should have received a copy of the GNU General Public License    *
28  * along with this program; if not, write to the Free Software          *
29  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
30  * MA  02110-1301  USA                                                  *
31  ************************************************************************/
32
33 // Some security stuff...
34 if (!defined('__SECURITY')) {
35         die();
36 } elseif ((!isHtmlOutputMode()) || (!isDailyResetEnabled())) {
37         // Do not execute when script is in non-HTML mode
38         return;
39 } elseif (!isExtensionActive('birthday')) {
40         // Extension not active/installed
41         if (isDebugModeEnabled()) logDebugMessage(__FILE__, __LINE__, 'Not resetting, needed extension ext-birthday disabled.');
42         return;
43 }
44
45 // Debug line
46 //* DEBUG: */ logDebugMessage(__FILE__, __LINE__, 'Daily reset started.');
47
48 // Get current day (01 to 31), month (01 to 12) and year (4-digits year)
49 $day   = getDay();
50 $month = getMonth();
51 $year  = getYear();
52
53 // Init variables
54 $lastOnline = '';
55 $excludeSql = '';
56
57 // Shall I include only active members?
58 // @TODO Move this out to an extension
59 if ((getConfig('birthday_active')  == 'Y') && (isExtensionActive('autopurge')) && ((getApInactiveSince() > 0))) {
60         $excludeSql = ' AND (UNIX_TIMESTAMP() - `d`.`last_online`) < {?ap_inactive_since?}';
61 } // END - if
62
63 // Only confirmed members shall receive birthday mails...
64 $result_birthday = sqlQueryEscaped("SELECT
65         `d`.`userid`,
66         `d`.`email`,
67         `d`.`birth_year`
68 FROM
69         `{?_MYSQL_PREFIX?}_user_data` AS `d`
70 WHERE
71         `d`.`status`='CONFIRMED'
72         " . runFilterChain('user_exclusion_sql', ' ' . $excludeSql) . " AND
73         `d`.`birth_day`=%s AND
74         `d`.`birth_month`=%s AND
75         `d`.`birthday_sent` < (UNIX_TIMESTAMP() - ({?ONE_DAY?} * 364))
76         " . $lastOnline . "
77 ORDER BY
78         `d`.`userid` ASC",
79         array($day, $month), __FILE__, __LINE__);
80
81 if (!ifSqlHasZeroNums($result_birthday)) {
82         // Start sending out birthday mails
83         while ($content = sqlFetchArray($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 (getBirthdayPoints() > 0) {
92                         // Add more entries to the array
93                         $content['age']    = $age;
94                         $content['check']  = '';
95
96                         // Generate long random code
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                         sqlQueryEscaped("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', $content, $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                 sqlQueryEscaped("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         sqlFreeResult($result_birthday);
125 } // END - if
126
127 // Debug line
128 //* DEBUG: */ logDebugMessage(__FILE__, __LINE__, 'Daily reset ended.');
129
130 // [EOF]
131 ?>