Code rewritings, load base config improved and many minor fixes...
[mailer.git] / 0.2.1 / inc / doubler_send.php
1 <?php\r
2 /************************************************************************\r
3  * MXChange v0.2.1                                    Start: 02/16/2005 *\r
4  * ===============                              Last change: 01/21/2006 *\r
5  *                                                                      *\r
6  * -------------------------------------------------------------------- *\r
7  * File              : doubler_send.php                                 *\r
8  * -------------------------------------------------------------------- *\r
9  * Short description : Send's out mails for doubled points              *\r
10  * -------------------------------------------------------------------- *\r
11  * Kurzbeschreibung  : Sendet Mails bei vergueteter Verdoppelung aus    *\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 \r
41 // Initialize variables\r
42 $jackpot = "0"; $user = "0";\r
43 \r
44 // Get total points of the doubler itself\r
45 $DOUBLER_POINTS = DOUBLER_GET_TOTAL_POINTS_LEFT();\r
46 if ($DOUBLER_POINTS == 0)\r
47 {\r
48         // Exit here to prevent some SQL errors (SQL_QUERY_ESC doen't insert zeros! We need to fix this...)\r
49         return;\r
50 }\r
51 \r
52 // If not currently doubled set it to zero\r
53 unset($_GET['DOUBLER_UID']);\r
54 unset($_POST['DOUBLER_UID']);\r
55 unset($_COOKIE['DOUBLER_UID']);\r
56 if (empty($DOUBLER_UID)) $DOUBLER_UID = "0";\r
57 \r
58 // Check for doubles which we can pay out\r
59 $min = bigintval($CONFIG['doubler_min'] * 2);\r
60 $result_total = SQL_QUERY_ESC("SELECT DISTINCT d.id, d.userid, d.points, d.remote_ip, d.timemark\r
61 FROM "._MYSQL_PREFIX."_doubler AS d\r
62 LEFT JOIN "._MYSQL_PREFIX."_user_data AS u\r
63 ON d.userid=u.userid\r
64 WHERE u.status='CONFIRMED' AND d.points <= %s AND d.points >= %s AND d.completed='N' AND d.is_ref='N'\r
65 ORDER BY d.timemark", array($DOUBLER_POINTS, $min), __FILE__, __LINE__);\r
66 \r
67 // Check for accounts with limitation\r
68 $result_main = SQL_QUERY_ESC("SELECT DISTINCT d.id, d.userid, d.points, d.remote_ip, d.timemark\r
69 FROM "._MYSQL_PREFIX."_doubler AS d\r
70 LEFT JOIN "._MYSQL_PREFIX."_user_data AS u\r
71 ON d.userid=u.userid\r
72 WHERE u.status='CONFIRMED' AND d.points <= %s AND d.points >= %s AND d.completed='N' AND d.is_ref='N'\r
73 ORDER BY d.timemark\r
74 LIMIT %s", array($DOUBLER_POINTS, $min, $CONFIG['doubler_max_sent']), __FILE__, __LINE__);\r
75 \r
76 if (((SQL_NUMROWS($result_total) > 0) && ($CONFIG['doubler_sent_all'] == "Y")) || ((SQL_NUMROWS($result_main) == $CONFIG['doubler_group_sent']) && ($CONFIG['doubler_sent_all'] == "N")))\r
77 {\r
78         // Switch to matching SQL resource\r
79         $result_load = $result_main;\r
80         if ((SQL_NUMROWS($result_total) > 0) && ($CONFIG['doubler_sent_all'] == "Y")) $result_load = $result_total;\r
81 \r
82         // At least one account was found\r
83         while(list($id, $uid, $points, $ip, $time) = SQL_FETCHROW($result_load))\r
84         {\r
85                 // Only double when points are enougth!\r
86                 if ($DOUBLER_POINTS >= $points)\r
87                 {\r
88                         // Check for his ref points\r
89                         $result_ref = SQL_QUERY_ESC("SELECT SUM(points) FROM "._MYSQL_PREFIX."_doubler WHERE refid=%d AND completed='N' AND is_ref='Y'",\r
90                          array(bigintval($uid)), __FILE__, __LINE__);\r
91                         list($ref) = SQL_FETCHROW($result_ref);\r
92 \r
93                         // Free memory\r
94                         SQL_FREERESULT($result_ref);\r
95 \r
96                         // Zero refid when empty (might be helpful!)\r
97                         if (empty($ref)) $ref = 0;\r
98                         if (($ref > 0) && ($DOUBLER_UID == $uid) && (!empty($ref)))\r
99                         {\r
100                                 // Referral points found so add them and set line(s) to completed='Y'\r
101                                 $points += $ref;\r
102                                 $result_ref = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_doubler SET completed='Y' WHERE refid=%d AND completed='N' AND is_ref='Y'",\r
103                                  array(bigintval($uid)), __FILE__, __LINE__);\r
104                         }\r
105                          else\r
106                         {\r
107                                 // No referral points found\r
108                                 $ref = "0";\r
109                         }\r
110 \r
111                         // Exclude webmaster from doubling...\r
112                         if ($uid != $CONFIG['doubler_uid'])\r
113                         {\r
114                                 // Add points\r
115                                 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_points SET points=points+%s WHERE userid=%d AND ref_depth='0' LIMIT 1",\r
116                                  array($points, bigintval($uid)), __FILE__, __LINE__);\r
117 \r
118                                 // Update mediadata as well\r
119                                 if (GET_EXT_VERSION("mediadata") >= "0.0.4")\r
120                                 {\r
121                                         // Update database\r
122                                         MEDIA_UPDATE_ENTRY(array("total_points"), "add", $points);\r
123                                 }\r
124                         }\r
125 \r
126                         // Set entry as "payed"\r
127                         $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_doubler SET completed='Y' WHERE id=%d LIMIT 1",\r
128                          array(bigintval($id)), __FILE__, __LINE__);\r
129 \r
130                         $OK = false;\r
131                         // Check for jackpot inclusion in doubling process\r
132                         if (($jackpot > 0) && ($jackpot >= $points) && ($CONFIG['doubler_jackpot'] == "Y"))\r
133                         {\r
134                                 // Subtract points from jackpot\r
135                                 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_jackpot SET points=points-%s WHERE ok='ok' LIMIT 1",\r
136                                  array($points), __FILE__, __LINE__);\r
137                                 $jackpot -= $jackpot;\r
138 \r
139                                 // Okay, done!\r
140                                 $OK = true;\r
141                         }\r
142 \r
143                         // Exclude also webmaster's ID in taking points from webmaster's account\r
144                         if (($user > 0) && ($user >= $points) && (!$OK) && ($CONFIG['doubler_uid'] > 0) && ($uid != $CONFIG['doubler_uid']))\r
145                         {\r
146                                 // Add points to used points\r
147                                 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_data SET used_points=used_points+%s WHERE userid='' LIMIT 1",\r
148                                  array($points, $CONFIG['doubler_uid']), __FILE__, __LINE__);\r
149 \r
150                                 // Update mediadata as well\r
151                                 if (GET_EXT_VERSION("mediadata") >= "0.0.4")\r
152                                 {\r
153                                         // Update database\r
154                                         MEDIA_UPDATE_ENTRY(array("total_points"), "sub", $points);\r
155                                 }\r
156 \r
157                                 // Okay, done!\r
158                                 $OK = true;\r
159                         }\r
160 \r
161                         // Update doubler's account only when others are not updated\r
162                         if (!$OK)\r
163                         {\r
164                                 // Add points to used doubler points\r
165                                 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_config SET doubler_used=doubler_used+%s WHERE config='0' LIMIT 1",\r
166                                  array($points), __FILE__, __LINE__);\r
167 \r
168                                 // Destroy cache\r
169                                 if (GET_EXT_VERSION("cache") >= "0.1.2")\r
170                                 {\r
171                                         if ($CACHE->cache_file("config", true)) $CACHE->cache_destroy();\r
172                                 }\r
173                         }\r
174 \r
175                         // Update variables to prevent errors\r
176                         $CONFIG['doubler_used'] += $points;\r
177                         $DOUBLER_POINTS -= $points;\r
178 \r
179                         // Prepare array\r
180                         $content = array(\r
181                                 // Doubler transmission ID\r
182                                 'id'     => $id,\r
183                                 // Doubled points\r
184                                 'points' => TRANSLATE_COMMA($points),\r
185                                 // Timemark\r
186                                 'when'   => MAKE_DATETIME($time, "2"),\r
187                                 // IP number when the member submitted the doubling form\r
188                                 'ip'     => $ip,\r
189                         );\r
190 \r
191                         // Load mail template and send mail away...\r
192                         $msg = LOAD_EMAIL_TEMPLATE("member_doubler", $content, $uid);\r
193                         SEND_EMAIL($uid, DOUBLER_MEMBER_SUBJECT, $msg);\r
194                 }\r
195         }\r
196 }\r
197 \r
198 // Free memory\r
199 SQL_FREERESULT($result_total);\r
200 SQL_FREERESULT($result_main);\r
201 \r
202 //\r
203 ?>\r