d8d2185b8808f60b98185fd8904d567f74eaea03
[mailer.git] / inc / reset / reset_birthday.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 10/24/2009 *
4  * ===============                              Last change: 10/24/2009 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : reset_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  * Needs to be in all Files and every File needs "svn propset           *
18  * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
19  * -------------------------------------------------------------------- *
20  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
21  * For more information visit: http://www.mxchange.org                  *
22  *                                                                      *
23  * This program is free software; you can redistribute it and/or modify *
24  * it under the terms of the GNU General Public License as published by *
25  * the Free Software Foundation; either version 2 of the License, or    *
26  * (at your option) any later version.                                  *
27  *                                                                      *
28  * This program is distributed in the hope that it will be useful,      *
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
31  * GNU General Public License for more details.                         *
32  *                                                                      *
33  * You should have received a copy of the GNU General Public License    *
34  * along with this program; if not, write to the Free Software          *
35  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
36  * MA  02110-1301  USA                                                  *
37  ************************************************************************/
38
39 // Some security stuff...
40 if (!defined('__SECURITY')) {
41         die();
42 } elseif (!isExtensionActive('birthday')) {
43         return;
44 }
45
46 // Do not execute when script is in CSS mode
47 if ((getOutputMode() != 0) || (!isResetModeEnabled())) return;
48 //* DEBUG: */ outputHtml(basename(__FILE__)."<br />");
49
50 // Get current day (01 to 31), month (01 to 12) and year (4-digits year)
51 $day   = date('d', time());
52 $month = date('m', time());
53 $year  = date('Y', time());
54
55 // Shall I include only active members?
56 $add = "%s"; $value = '';
57 if ((getConfig('birthday_active')) && (isExtensionActive('autopurge')) && (getConfig('autopurge_inactive') == 'Y') && (getConfig('ap_inactive_since') > 0)) {
58         $add = " AND `last_online` >= (UNIX_TIMESTAMP() - %s)";
59         $value = getConfig('ap_inactive_since');
60 } // END - if
61
62 // Only confirmed members shall receive birthday mails...
63 $result_birthday = SQL_QUERY_ESC("SELECT `userid`, `email`, `birth_year`
64 FROM
65         `{?_MYSQL_PREFIX?}_user_data`
66 WHERE
67         `status`='CONFIRMED' AND
68         `birth_day`=%s AND
69         `birth_month`=%s AND
70         `birthday_sent` < (UNIX_TIMESTAMP() - ".(getConfig('ONE_DAY') * 364).")
71         ".$add."
72 ORDER BY
73         `userid` ASC",
74         array($day, $month, $value), __FILE__, __LINE__);
75
76 if (SQL_NUMROWS($result_birthday) > 0) {
77         // Start sending out birthday mails
78         while ($content = SQL_FETCHARRAY($result_birthday)) {
79                 // Calculate own timestamp for birthday and today
80                 $bd  = $content['birth_year'] + 12 * $month + 365 * $day;
81                 $now = $year  + 12 * $month + 365 * $day;
82
83                 // Simply subtract both values and you got the age... :)
84                 $age = $now - $bd;
85
86                 if (getConfig('birthday_points') > 0) {
87                         // Add more entries to the array
88                         $content['age']    = $age;
89                         $content['points'] = translateComma(getConfig('birthday_points'));
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','%s','%s' )",
99                                 array(bigintval($content['userid']), getConfig('birthday_points'), $content['check']), __FILE__, __LINE__);
100
101                         // Load email template with confirmation link
102                         $message = loadEmailTemplate('member_birthday_confirm', $content, bigintval($content['userid']));
103                 } else {
104                         // Load default email template and fill in the age
105                         $message = loadEmailTemplate('member_birthday', $age, $content['userid']);
106                 }
107
108                 // Send email
109                 sendEmail($content['email'], getMessage('HAPPY_BIRTHDAY'), $message);
110
111                 // Remember him that he has received a birthday mail
112                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `birthday_sent`=UNIX_TIMESTAMP() WHERE `userid`=%s LIMIT 1",
113                         array(bigintval($content['userid'])), __FILE__, __LINE__);
114         } // END - while
115
116         // Free memory
117         SQL_FREERESULT($result);
118 }
119
120 // [EOF]
121 ?>