Unnessarry addslashes() and SQL_ESCAPE() removed, some added, some bigintval() added
[mailer.git] / inc / modules / member / what-payout.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 04/12/2004 *
4  * ================                             Last change: 12/01/2008 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : what-payout.php                                  *
8  * -------------------------------------------------------------------- *
9  * Short description : Payout requests                                  *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Auszahlungsanfragen                              *
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         // Don't call this directly!
37         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
38         require($INC);
39 } elseif (!IS_MEMBER()) {
40         // Not logged in
41         LOAD_URL("modules.php?module=index");
42 } elseif ((!EXT_IS_ACTIVE("payout")) && (!IS_ADMIN())) {
43         // Extension "payout" is not active
44         ADD_FATAL(EXTENSION_PROBLEM_EXT_INACTIVE, "payout");
45         return;
46 }
47
48 // Add description as navigation point
49 ADD_DESCR("member", __FILE__);
50
51 $result_depths = SQL_QUERY("SELECT level, percents FROM "._MYSQL_PREFIX."_refdepths ORDER BY level", __FILE__, __LINE__);
52 $TPTS = 0;
53 while (list($lvl, $per) = SQL_FETCHROW($result_depths))
54 {
55         // Load referal points
56         $result_points = SQL_QUERY_ESC("SELECT points FROM "._MYSQL_PREFIX."_user_points WHERE userid=%s AND ref_depth=%d LIMIT 1",
57          array($GLOBALS['userid'], bigintval($lvl)), __FILE__, __LINE__);
58         if (SQL_NUMROWS($result_points) == 1)
59         {
60                 list($points) = SQL_FETCHROW($result_points);
61                 SQL_FREERESULT($result_points);
62                 $TPTS += $points;
63         }
64 }
65
66 // Free memory
67 SQL_FREERESULT($result_depths);
68
69 // Get used points
70 $USED = GET_TOTAL_DATA($GLOBALS['userid'], "user_data", "used_points");
71
72 // Translate point into comma
73 $TPTS = TRANSLATE_COMMA($TPTS - $USED);
74
75 // Sanity check...
76 if (empty($TPTS)) $TPTS = "0.00000";
77
78 if (empty($_GET['payout']))
79 {
80         // Load payout types
81         $result = SQL_QUERY_ESC("SELECT id, type, rate, min_points, allow_url
82 FROM "._MYSQL_PREFIX."_payout_types
83 WHERE %s >= min_points
84 ORDER BY type", array(REVERT_COMMA($TPTS)), __FILE__, __LINE__);
85         if (SQL_NUMROWS($result) > 0)
86         {
87                 // Free memory
88                 SQL_FREERESULT($result);
89
90                 // Check for his payouts
91                 $result_payouts = SQL_QUERY_ESC("SELECT DISTINCT p.id, p.payout_total, p.target_account, p.target_bank, t.type, p.payout_timestamp, p.status, t.allow_url AS allow, p.target_url AS url, p.link_text AS alt, p.banner_url AS bannerm
92 FROM "._MYSQL_PREFIX."_user_payouts AS p
93 LEFT JOIN "._MYSQL_PREFIX."_payout_types AS t
94 ON p.payout_id = t.id
95 WHERE p.userid = %s
96 ORDER BY p.payout_timestamp DESC",
97                  array($GLOBALS['userid']), __FILE__, __LINE__);
98                 if (SQL_NUMROWS($result_payouts) > 0)
99                 {
100                         // List all his requests
101                         $SW = 2; $OUT = "";
102                         while (list($pid, $total, $account, $bank, $type, $tstamp, $status, $allow, $url, $alt, $banner) = SQL_FETCHROW($result_payouts))
103                         {
104                                 // Translate status
105                                 $status = constant('PAYOUT_MEMBER_STATUS_'.strtoupper($status).'');
106                                 $status = "<FONT class=\"member_failed\">".$status."</FONT>";
107
108                                 // Nothing entered must be secured in member/what-payputs.php !
109                                 if ($allow == "Y")
110                                 {
111                                         // Banner/Textlink views/clicks request
112                                         if (!empty($banner))
113                                         {
114                                                 // Banner
115                                                 $account = "<IMG src=\"".$banner."\" alt=\"".$alt."\" title=\"".$alt."\" border=\"0\">";
116                                         }
117                                          else
118                                         {
119                                                 // Textlink
120                                                 $account = $alt;
121                                         }
122                                         $bank = "<A href=\"".DEREFERER($url)."\" target=\"_blank\">".CLICK_HERE."</A>";
123                                 }
124                                  else
125                                 {
126                                         // e-currency payout request
127                                         if (empty($account)) $account = "---";
128                                         if (empty($bank))    $bank    = "---";
129                                 }
130
131                                 // Prepare data for the template
132                                 $content = array(
133                                         'sw'     => $SW,
134                                         'acc'    => $account,
135                                         'points' => TRANSLATE_COMMA($total)." ".COMPILE_CODE($type),
136                                         'bank'   => $bank,
137                                         'stamp'  => MAKE_DATETIME($tstamp, "2"),
138                                         'status' => $status
139                                 );
140                                 // Load row template and switch colors
141                                 $OUT .= LOAD_TEMPLATE("member_payout_row", true, $content);
142                                 $SW = 3 - $SW;
143                         }
144
145                         // Remember rows in constant
146                         define('__PAYOUT_ROWS', $OUT);
147
148                         // Load template
149                         LOAD_TEMPLATE("member_payout");
150                 }
151
152                 // Free memory
153                 SQL_FREERESULT($result_payouts);
154
155                 // Output payout list
156                 PAYOUT_OUTPUT_PAYOUT_LIST($TPTS);
157         }
158 }
159  else
160 {
161         // Chedk if he can get paid by selected type
162         $result = SQL_QUERY_ESC("SELECT type, rate, min_points, allow_url FROM "._MYSQL_PREFIX."_payout_types WHERE id=%s LIMIT 1",
163          array(bigintval($_GET['payout'])), __FILE__, __LINE__);
164
165         if (SQL_NUMROWS($result) == 1)
166         {
167                 // ID is valid
168                 list($type, $rate, $min, $allow) = SQL_FETCHROW($result);
169                 SQL_FREERESULT($result);
170
171                 // Calculate maximum value
172                 $max = round($TPTS * $rate - 0.5);
173
174                 // Calulcate points from submitted amount
175                 $PAYOUT = 0;
176                 if (!empty($_POST['payout']))
177                 {
178                         $PAYOUT  = bigintval($_POST['payout']) / $rate;
179                         $PAY_MAX = $max / $rate;
180                 }
181
182                 // Move variables into constants for templates
183                 define('PAYOUT_MAX_VALUE' , $max);
184                 define('PAYOUT_TYPE_VALUE', COMPILE_CODE($type));
185
186                 if (REVERT_COMMA($TPTS) >= $min)
187                 {
188                         // Ok, he can get be paid
189                         if ((isset($_POST['ok'])) && ($PAYOUT <= $PAY_MAX) && ($PAYOUT >= $min))
190                         {
191                                 // Calculate exact value
192                                 define('PAYOUT_POINTS_VALUE', $PAYOUT);
193
194                                 // Subtract points from member's account
195                                 SUB_POINTS($GLOBALS['userid'], $PAYOUT);
196
197                                 // Add entry to his tranfer history
198                                 if ($allow == "Y")
199                                 {
200                                         // Banner/textlink ordered
201                                         $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_user_payouts (userid, payout_total, payout_id, payout_timestamp, status, target_url, link_text, banner_url)
202 VALUES (%s,%s,%s, UNIX_TIMESTAMP(), 'NEW','%s','%s','%s')",
203  array(
204         $GLOBALS['userid'],
205         bigintval($_POST['payout']),
206         bigintval($_GET['payout']),
207         $_POST['turl'],
208         $_POST['alt'],
209         $_POST['banner']
210 ), __FILE__, __LINE__);
211
212                                         // Load templates
213                                         $msg_mem = LOAD_EMAIL_TEMPLATE("member_payout_request_banner", array(), $GLOBALS['userid']);
214                                         if (GET_EXT_VERSION("admins") >= "0.4.1")
215                                         {
216                                                 $adm_tpl = "admin_payout_request_banner";
217                                         }
218                                          else
219                                         {
220                                                 $msg_adm = LOAD_EMAIL_TEMPLATE("admin_payout_request_banner", array(), $GLOBALS['userid']);
221                                         }
222                                 }
223                                  else
224                                 {
225                                         // e-currency payout requested
226                                         $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_user_payouts (userid, payout_total, target_account, target_bank, payout_id, payout_timestamp, status, password)
227 VALUES (%s,%s,%s,'%s',%s, UNIX_TIMESTAMP(), 'NEW','%s')",
228  array(
229         $GLOBALS['userid'],
230         bigintval($_POST['payout']),
231         bigintval($_POST['account']),
232         $_POST['bank'],
233         bigintval($_GET['payout']),
234         $_POST['pass']
235 ), __FILE__, __LINE__);
236
237                                         // Load templates
238                                         $msg_mem = LOAD_EMAIL_TEMPLATE("member_payout_request", array(), $GLOBALS['userid']);
239                                         $msg_adm = LOAD_EMAIL_TEMPLATE("admin_payout_request", array(), $GLOBALS['userid']);
240                                         $admin_tpl = "";
241                                         if (GET_EXT_VERSION("admins") >= "0.4.1")
242                                         {
243                                                 $admin_tpl = "admin_payout_request";
244                                         }
245                                 }
246
247                                 // Generate task
248                                 $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_task_system (assigned_admin, status, task_type, subject, text, task_created, userid)
249 VALUES (0, 'NEW','PAYOUT_REQUEST','[payout:] ".PAYOUT_REQUEST_ADMIN."','%s', UNIX_TIMESTAMP(), %s)",
250  array(
251         $msg_adm,
252         $GLOBALS['userid']
253 ), __FILE__, __LINE__);
254
255                                 // Send out mails
256                                 SEND_EMAIL($GLOBALS['userid'], PAYOUT_REQUEST_MEMBER, $msg_mem);
257
258                                 // To admin(s)
259                                 SEND_ADMIN_NOTIFICATION(PAYOUT_REQUEST_ADMIN, $admin_tpl, array(), $GLOBALS['userid']);
260
261                                 // Load template and output it
262                                 LOAD_TEMPLATE("admin_settings_saved", false, PAYOUT_REQUEST_SENT);
263                         }
264                          elseif ($allow == "Y")
265                         {
266                                 // Generate banner order form
267                                 LOAD_TEMPLATE("member_payout_form_banner");
268                         }
269                          else
270                         {
271                                 // Generate normal form
272                                 LOAD_TEMPLATE("member_payout_form");
273                         }
274                 }
275                  else
276                 {
277                         // Not enougth points
278                         LOAD_TEMPLATE("admin_settings_saved", false, PAYOUT_POINTS_NOT_ENOUGTH);
279                 }
280         }
281          else
282         {
283                 // ID is invalid
284                 LOAD_TEMPLATE("admin_settings_saved", false, PAYOUT_ID_INVALID);
285         }
286 }
287 //
288 ?>