Also reset (aka daily reset scripts) renamed. The naming 'reset' came from the time...
[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, 2010 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         logDebugMessage(__FILE__, __LINE__, 'Not resetting, needed extension disabled.');
46         return;
47 }
48
49 // Debug line
50 //* DEBUG: */ logDebugMessage(__FILE__, __LINE__, 'Daily reset started.');
51
52 // Get current day (01 to 31), month (01 to 12) and year (4-digits year)
53 $day   = getDay();
54 $month = getMonth();
55 $year  = getYear();
56
57 // Shall I include only active members?
58 $add = '%s'; $value = '';
59 if ((getConfig('birthday_active')) && (isExtensionActive('autopurge')) && (getConfig('autopurge_inactive') == 'Y') && (getApInactiveSince() > 0)) {
60         $add = " AND `last_online` >= (UNIX_TIMESTAMP() - {?ap_inactive_since?})";
61 } // END - if
62
63 // Only confirmed members shall receive birthday mails...
64 $result_birthday = SQL_QUERY_ESC("SELECT `userid`, `email`, `birth_year`
65 FROM
66         `{?_MYSQL_PREFIX?}_user_data`
67 WHERE
68         `status`='CONFIRMED' AND
69         `birth_day`=%s AND
70         `birth_month`=%s AND
71         `birthday_sent` < (UNIX_TIMESTAMP() - ({?ONE_DAY?} * 364))
72         ".$add."
73 ORDER BY
74         `userid` ASC",
75         array($day, $month, $value), __FILE__, __LINE__);
76
77 if (!SQL_HASZERONUMS($result_birthday)) {
78         // Start sending out birthday mails
79         while ($content = SQL_FETCHARRAY($result_birthday)) {
80                 // Calculate own timestamp for birthday and today
81                 $bd  = $content['birth_year'] + 12 * $month + 365 * $day;
82                 $now = $year  + 12 * $month + 365 * $day;
83
84                 // Simply subtract both values and you got the age... :)
85                 $age = $now - $bd;
86
87                 if (getConfig('birthday_points') > 0) {
88                         // Add more entries to the array
89                         $content['age']    = $age;
90                         $content['check']  = '';
91
92                         // @TODO 4 is hard-coded here, should we move it out in config?
93                         for ($idx = '0'; $idx < 4; $idx++) {
94                                 $content['check'] .= generateRandomCode('8', mt_rand(0, $month . $day), $content['userid'], ($age * ($idx + 1)));
95                         } // END - for
96
97                         // Insert row into database
98                         SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_birthday` (`userid`, `points`, `chk_value`) VALUES (%s,{?birthday_points?},'%s' )",
99                                 array(
100                                         bigintval($content['userid']),
101                                         $content['check']
102                                 ), __FILE__, __LINE__);
103
104                         // Load email template with confirmation link
105                         $message = loadEmailTemplate('member_birthday_confirm', $content, bigintval($content['userid']));
106                 } else {
107                         // Load default email template and fill in the age
108                         $message = loadEmailTemplate('member_birthday', $age, $content['userid']);
109                 }
110
111                 // Send email
112                 sendEmail($content['email'], '{--MEMBER_HAPPY_BIRTHDAY_SUBJECT--}', $message);
113
114                 // Remember him that he has received a birthday mail
115                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `birthday_sent`=UNIX_TIMESTAMP() WHERE `userid`=%s LIMIT 1",
116                         array(bigintval($content['userid'])), __FILE__, __LINE__);
117         } // END - while
118
119         // Free memory
120         SQL_FREERESULT($result_birthday);
121 } // END - if
122
123 // Debug line
124 //* DEBUG: */ logDebugMessage(__FILE__, __LINE__, 'Daily reset ended.');
125
126 // [EOF]
127 ?>