A lot while() conditions rewritten to SQL_FETCHARRAY(), see bug #107, @TODO tags...
[mailer.git] / inc / mails / beg_mails.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 11/22/2005 *
4  * ===============                              Last change: 11/22/2005 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : beg_mails.php                                    *
8  * -------------------------------------------------------------------- *
9  * Short description : Sends out reminder mails                         *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Versendet Erinnerungsmails                       *
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 (!defined('__SECURITY')) {
36         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
37         require($INC);
38 } elseif (!EXT_IS_ACTIVE("beg")) {
39         return;
40 }
41
42 // Do not execute when script is in CSS mode
43 if ($GLOBALS['output_mode'] == 1) return;
44
45 // Create timemark from saved month
46 $mark = mktime(0, 0, 0, getConfig('last_month'), date("d", time()), date('Y', time()));
47 $sql = ""; $MODE = "";
48
49 // Shall I sent activation or deactivation mail?
50 $sql = "SELECT userid, email FROM `{!_MYSQL_PREFIX!}_user_data` WHERE (beg_ral_notify ";
51 switch (getConfig('beg_rallye'))
52 {
53 case "Y": // Begging rallye is activated
54         if (getConfig('beg_ral_en_notify') == "Y")
55         {
56                 // Okay, let's check for member accounts
57                 $sql .= "= 0 OR (beg_ral_notify > 0 AND beg_ral_en_notify < beg_ral_di_notify)";
58                 $MODE = "en";
59         }
60          else
61         {
62                 // Do not notify!
63                 $sql = "";
64         }
65         break;
66
67 case "N": // Begging rallye is deactivated
68         if (getConfig('beg_ral_di_notify') == "Y")
69         {
70                 // Okay, let's check for member accounts
71                 $sql .= " > 0 AND beg_ral_di_notify < beg_ral_en_notify";
72                 $MODE = "di";
73         }
74          else
75         {
76                 // Do not notify!
77                 $sql = "";
78         }
79         break;
80 }
81
82 if (!empty($sql)) {
83         // The SQL command needs to be finisched here (only confirmed accounts!)
84         $sql .= ") AND `status`='CONFIRMED' ORDER BY last_online ASC";
85
86         // Prepare data for the template
87         define('__BEG_MIN_POINTS' , TRANSLATE_COMMA(getConfig('beg_points')));
88         define('__BEG_MAX_POINTS' , TRANSLATE_COMMA(getConfig('beg_points_max')));
89         define('__BEG_MAX_WINNERS', round(getConfig('beg_ranks')));
90         if (getConfig('beg_ip_timeout') == 0)
91         {
92                 // No IP locking setuped!
93                 define('__BEG_IP_LOCKER', BEG_NO_LIMITATION);
94         }
95          else
96         {
97                 // Create timemark
98                 define('__BEG_IP_LOCKER', CREATE_FANCY_TIME(getConfig('beg_ip_timeout')));
99         }
100
101         // Check for accounts to be notified
102         $result_main = SQL_QUERY($sql, __FILE__, __LINE__);
103         if (SQL_NUMROWS($result_main) > 0) {
104                 // Normal notification mails or bonus mails?
105                 $sentBonusMails = ((getConfig('beg_notify_bonus') > 0) && ($MODE == "en") && (EXT_IS_ACTIVE("bonus")));
106
107                 // Generate subject line
108                 $SUBJECT = constant('BEG_RALLYE_'.strtoupper($MODE).'_NOTIFY');
109
110                 // Load message body for bonus mails
111                 $MSG = LOAD_EMAIL_TEMPLATE("beg_en_notify_body", "", "{PER}uid{PER}");
112                 $RECEIVER = ""; $UIDs = array();
113
114                 // Okay lets notify all users!
115                 while ($content = SQL_FETCHARRAY($result_main)) {
116                         // Update account
117                         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_user_data`
118 SET beg_ral_notify='%s', beg_ral_%s_notify='%s'
119 WHERE userid=%s
120 LIMIT 1",
121                                 array(time(), $MODE, time(), $content['userid']), __FILE__, __LINE__);
122
123                         // Load email template and send it to the user!
124                         if ($sentBonusMails === true) {
125                                 // Add userid to queue
126                                 $UIDs[] = $content['userid'];
127                         } else {
128                                 // Send normal notification mail to the members
129                                 $MSG = LOAD_EMAIL_TEMPLATE("beg_".$MODE."_notify", array(), $content['userid']);
130                                 SEND_EMAIL($content['email'], $SUBJECT, $MSG);
131                         }
132                 } // END - while
133
134                 // Shall I send out bonus mails?
135                 if ($sentBonusMails === true) {
136                         // Okay, make array to string
137                         $RECEIVER = implode(";", $UIDs);
138
139                         // Prepare URL
140                         $URL = "modules.php?module=index&amp;what=login";
141
142                         // Insert mail
143                         ADD_BONUS_MAIL_TO_QUEUE($SUBJECT, $MSG, $RECEIVER, getConfig('beg_notify_bonus'), getConfig('beg_notify_wait'), $URL, 0, "normal", SQL_NUMROWS($result_main));
144                 } // END - if
145         } // END - if
146
147         // Free memory
148         SQL_FREERESULT($result_main);
149 } // END - if
150
151 //
152 ?>