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