Tons of rewrites (SQL queries), surfbar nearly finished (working: surfing with static...
[mailer.git] / inc / mails / birthday_mails.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 05/23/2004 *
4  * ===============                              Last change: 11/22/2005 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : birthday_mails.php                               *
8  * -------------------------------------------------------------------- *
9  * Short description : Sends out birthday mails                         *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Versendet Geburtstagsmails                       *
12  * -------------------------------------------------------------------- *
13  *                                                                      *
14  * -------------------------------------------------------------------- *
15  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
16  * For more information visit: http://www.mxchange.org                  *
17  *                                                                      *
18  * This program is free software; you can redistribute it and/or modify *
19  * it under the terms of the GNU General Public License as published by *
20  * the Free Software Foundation; either version 2 of the License, or    *
21  * (at your option) any later version.                                  *
22  *                                                                      *
23  * This program is distributed in the hope that it will be useful,      *
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
26  * GNU General Public License for more details.                         *
27  *                                                                      *
28  * You should have received a copy of the GNU General Public License    *
29  * along with this program; if not, write to the Free Software          *
30  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
31  * MA  02110-1301  USA                                                  *
32  ************************************************************************/
33
34 // Some security stuff...
35 if (ereg(basename(__FILE__), $_SERVER['PHP_SELF']))
36 {
37         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
38         require($INC);
39 }
40  elseif ((!EXT_IS_ACTIVE("birthday")) && (!IS_ADMIN()))
41 {
42         ADD_FATAL(EXTENSION_PROBLEM_EXT_INACTIVE, "birthday");
43         return;
44 }
45
46 // Do not execute when script is in CSS mode
47 if ($CSS == 1) return;
48
49 // Get current day (01 to 31), month (01 to 12) and year (4-digits year)
50 $DAY   = date("d", time());
51 $MONTH = date("m", time());
52 $YEAR  = date('Y', time());
53
54 // Shall I include only active members?
55 $ADD = "%s"; $VALUE = "";
56 if (($_CONFIG['birthday_active']) && (EXT_IS_ACTIVE("autopurge")) && ($_CONFIG['ap_in_since'] > 0) && ($_CONFIG['ap_inactive'] == "Y"))
57 {
58         $ADD = " AND last_online >= %d";
59         $VALUE = bigintval(time() - $_CONFIG['ap_in_since']);
60 }
61
62 // Only confirmed members shall receive birthday mails...
63 $result_birthday = SQL_QUERY_ESC("SELECT userid, email, birth_year
64 FROM "._MYSQL_PREFIX."_user_data
65 WHERE status='CONFIRMED' AND birth_day=%s AND birth_month=%s AND birthday_sent < ".(time() - (ONE_DAY*364)).$ADD."
66 ORDER BY userid",
67  array($DAY, $MONTH, $VALUE), __FILE__, __LINE__);
68
69 if (SQL_NUMROWS($result_birthday) > 0)
70 {
71         // Start sending out birthday mails
72         while (list($uid, $email, $byear) = SQL_FETCHROW($result_birthday))
73         {
74                 // Calculate own timestamp for birthday and today
75                 $BD  = $byear + 12*$MONTH + 365*$DAY;
76                 $NOW = $YEAR  + 12*$MONTH + 365*$DAY;
77
78                 // Simply subtract both values and you got the age... :)
79                 $AGE = $NOW - $BD;
80
81                 if ($_CONFIG['birthday_points'] > 0)
82                 {
83                         // Prepare array for loading template
84                         $content = array(
85                                 'age'    => $AGE,
86                                 'points' => $_CONFIG['birthday_points'],
87                                 'check'  => "",
88                         );
89                         for ($idx = 0; $idx < 4; $idx++)
90                         {
91                                 $content['check'] .= GEN_RANDOM_CODE("8", rand(0, "$MONTH$DAY"), $uid, ($AGE*($idx+1)));
92                         }
93
94                         // Insert row into database
95                         $result_insert = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_user_birthday (userid, points, chk_value) VALUES ('%s', '%s', '%s' )",
96                          array(bigintval($uid), $_CONFIG['birthday_points'], $content['check']), __FILE__, __LINE__);
97
98                         // Load email template with confirmation link
99                         $msg = LOAD_EMAIL_TEMPLATE("member_birthday_confirm", $content, bigintval($uid));
100                 }
101                  else
102                 {
103                         // Load default email template and fill in the age
104                         $msg = LOAD_EMAIL_TEMPLATE("member_birthday", $AGE, $uid);
105                 }
106
107                 // Send email
108                 SEND_EMAIL($email, HAPPY_BIRTHDAY, $msg);
109
110                 // Remember him that he has received a birthday mail
111                 $result_bd = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_data SET birthday_sent=UNIX_TIMESTAMP() WHERE userid=%s LIMIT 1",
112                  array(bigintval($uid)), __FILE__, __LINE__);
113         }
114
115         // Free memory
116         SQL_FREERESULT($result);
117 }
118
119 //
120 ?>