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