A lot while() conditions rewritten to SQL_FETCHARRAY(), see bug #107, @TODO tags...
[mailer.git] / inc / mails / bonus_mails.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 11/22/2005 *
4  * ===============                              Last change: 11/22/2005 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : bonus_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("bonus")) {
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 (bonus_ral_notify ";
51 switch (getConfig('bonus_active'))
52 {
53 case "Y": // Active rallye is activated
54         if (getConfig('bonus_en_notify') == "Y") {
55                 // Okay, let's check for member accounts
56                 $sql .= "= 0 OR (bonus_ral_notify > 0 AND bonus_ral_en_notify < bonus_ral_di_notify)";
57                 $MODE = "en";
58         } else {
59                 // Do not notify!
60                 $sql = "";
61         }
62         break;
63
64 case "N": // Active rallye is deactivated
65         if (getConfig('bonus_di_notify') == "Y") {
66                 // Okay, let's check for member accounts
67                 $sql .= " > 0 AND bonus_ral_di_notify < bonus_ral_en_notify";
68                 $MODE = "di";
69         } else {
70                 // Do not notify!
71                 $sql = "";
72         }
73         break;
74 }
75
76 if (!empty($sql)) {
77         // The SQL command needs to be finisched here (only confirmed accounts!)
78         $sql .= ") AND `status`='CONFIRMED' ORDER BY last_online ASC";
79
80         // Normal notification mails or bonus mails?
81         $sentBonusMails = ((getConfig('bonus_notify_points') > 0) && ($MODE == "en") && (EXT_IS_ACTIVE("bonus")));
82
83         // Generate subject line
84         $SUBJECT = constant('BONUS_RALLYE_'.strtoupper($MODE).'_NOTIFY');
85
86         // Load message body for bonus mails
87         $MSG = LOAD_EMAIL_TEMPLATE("bonus_en_notify_body", "", "{PER}uid{PER}");
88         $RECEIVER = ""; $UIDs = array();
89
90         // Check for accounts to be notified
91         $result_main = SQL_QUERY($sql, __FILE__, __LINE__);
92         if (SQL_NUMROWS($result_main) > 0) {
93                 // Okay lets notify all users!
94                 while ($content = SQL_FETCHARRAY($result_main)) {
95                         // Update account
96                         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_user_data`
97 SET bonus_ral_notify='%s', bonus_ral_%s_notify='%s'
98 WHERE userid=%s
99 LIMIT 1",
100                                 array(time(), $MODE, time(), $content['userid']), __FILE__, __LINE__);
101
102                         // Load email template and send it to the user!
103                         if ($sentBonusMails === true) {
104                                 // Add userid to queue
105                                 $UIDs[] = $content['userid'];
106                         } else {
107                                 // Send normal notification mail to the members
108                                 $MSG = LOAD_EMAIL_TEMPLATE("bonus_".$MODE."_notify", array(), $content['userid']);
109                                 SEND_EMAIL($content['email'], $SUBJECT, $MSG);
110                         }
111                 } // END - while
112
113                 // Shall I send out bonus mails?
114                 if ($sentBonusMails === true) {
115                         // Okay, make array to string
116                         $RECEIVER = implode(";", $UIDs);
117
118                         // Prepare URL
119                         $URL = "modules.php?module=index&amp;what=login";
120
121                         // Insert mail
122                         ADD_BONUS_MAIL_TO_QUEUE($SUBJECT, $MSG, $RECEIVER, getConfig('bonus_notify_points'), getConfig('bonus_notify_wait'), $URL, 0, "normal", SQL_NUMROWS($result_main));
123                 } // END - if
124         } // END - if
125
126         // Free memory
127         SQL_FREERESULT($result_main);
128 } // END - if
129
130 //
131 ?>