Extension ext-coupon continued, a lot improvements applied:
[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')) && (isBegActiveEnabled()) && ((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 `userid`, `email`, `birth_year`
68 FROM
69         `{?_MYSQL_PREFIX?}_user_data`
70 WHERE
71         `status`='CONFIRMED' AND
72         `birth_day`=%s AND
73         `birth_month`=%s AND
74         `birthday_sent` < (UNIX_TIMESTAMP() - ({?ONE_DAY?} * 364))
75         ".$lastOnline."
76 ORDER BY
77         `userid` ASC",
78         array($day, $month), __FILE__, __LINE__);
79
80 if (!SQL_HASZERONUMS($result_birthday)) {
81         // Start sending out birthday mails
82         while ($content = SQL_FETCHARRAY($result_birthday)) {
83                 // Calculate own timestamp for birthday and today
84                 $bd  = $content['birth_year'] + 12 * $month + 365 * $day;
85                 $now = $year  + 12 * $month + 365 * $day;
86
87                 // Simply subtract both values and you got the age... :)
88                 $age = $now - $bd;
89
90                 if (getConfig('birthday_points') > 0) {
91                         // Add more entries to the array
92                         $content['age']    = $age;
93                         $content['check']  = '';
94
95                         // @TODO 4 is hard-coded here, should we move it out in config?
96                         for ($idx = '0'; $idx < 4; $idx++) {
97                                 $content['check'] .= generateRandomCode('8', mt_rand(0, $month . $day), $content['userid'], ($age * ($idx + 1)));
98                         } // END - for
99
100                         // Insert row into database
101                         SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_birthday` (`userid`, `points`, `chk_value`) VALUES (%s,{?birthday_points?},'%s' )",
102                                 array(
103                                         bigintval($content['userid']),
104                                         $content['check']
105                                 ), __FILE__, __LINE__);
106
107                         // Load email template with confirmation link
108                         $message = loadEmailTemplate('member_birthday_confirm', $content, bigintval($content['userid']));
109                 } else {
110                         // Load default email template and fill in the age
111                         $message = loadEmailTemplate('member_birthday', $age, $content['userid']);
112                 }
113
114                 // Send email
115                 sendEmail($content['email'], '{--MEMBER_HAPPY_BIRTHDAY_SUBJECT--}', $message);
116
117                 // Remember him that he has received a birthday mail
118                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `birthday_sent`=UNIX_TIMESTAMP() WHERE `userid`=%s LIMIT 1",
119                         array(bigintval($content['userid'])), __FILE__, __LINE__);
120         } // END - while
121
122         // Free memory
123         SQL_FREERESULT($result_birthday);
124 } // END - if
125
126 // Debug line
127 //* DEBUG: */ logDebugMessage(__FILE__, __LINE__, 'Daily reset ended.');
128
129 // [EOF]
130 ?>