A lot double-quotes rewritten to single-quotes, some redirect URLs fixed
[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  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
17  * Needs to be in all Files and every File needs "svn propset           *
18  * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
19  * -------------------------------------------------------------------- *
20  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
21  * For more information visit: http://www.mxchange.org                  *
22  *                                                                      *
23  * This program is free software; you can redistribute it and/or modify *
24  * it under the terms of the GNU General Public License as published by *
25  * the Free Software Foundation; either version 2 of the License, or    *
26  * (at your option) any later version.                                  *
27  *                                                                      *
28  * This program is distributed in the hope that it will be useful,      *
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
31  * GNU General Public License for more details.                         *
32  *                                                                      *
33  * You should have received a copy of the GNU General Public License    *
34  * along with this program; if not, write to the Free Software          *
35  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
36  * MA  02110-1301  USA                                                  *
37  ************************************************************************/
38
39 // Some security stuff...
40 if (!defined('__SECURITY')) {
41         // Don't call this directly!
42         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), '/inc') + 4) . '/security.php';
43         require($INC);
44 } elseif (!IS_MEMBER()) {
45         // Not logged in
46         LOAD_URL('modules.php?module=index');
47 } elseif ((!EXT_IS_ACTIVE('payout')) && (!IS_ADMIN())) {
48         addFatalMessage(__FILE__, __LINE__, getMessage('EXTENSION_PROBLEM_EXT_INACTIVE'), 'payout');
49         return;
50 }
51
52 // Add description as navigation point
53 ADD_DESCR('member', __FILE__);
54
55 $result_depths = SQL_QUERY("SELECT level, percents FROM `{!_MYSQL_PREFIX!}_refdepths` ORDER BY level", __FILE__, __LINE__);
56 $TPTS = 0;
57 while ($content = SQL_FETCHARRAY($result_depths)) {
58         // Load referal points
59         $result_points = SQL_QUERY_ESC("SELECT points FROM `{!_MYSQL_PREFIX_user_points!}` WHERE userid=%s AND ref_depth=%d LIMIT 1",
60                 array(getUserId(), bigintval($content['level'])), __FILE__, __LINE__);
61
62         // Entry found?
63         if (SQL_NUMROWS($result_points) == 1) {
64                 // Load points
65                 list($points) = SQL_FETCHROW($result_points);
66
67                 // Add them to total
68                 $TPTS += $points;
69         }
70
71         // Free result
72         SQL_FREERESULT($result_points);
73 }
74
75 // Free memory
76 SQL_FREERESULT($result_depths);
77
78 // Get used points
79 $USED = GET_TOTAL_DATA(getUserId(), "user_data", "used_points");
80
81 // Translate point into comma
82 $TPTS = TRANSLATE_COMMA($TPTS - $USED);
83
84 // Sanity check...
85 if (empty($TPTS)) $TPTS = "0.00000";
86
87 if (!REQUEST_ISSET_GET(('payout'))) {
88         // Load payout types
89         $result = SQL_QUERY_ESC("SELECT id, type, rate, min_points, allow_url
90 FROM `{!_MYSQL_PREFIX!}_payout_types`
91 WHERE %s >= min_points
92 ORDER BY type", array(REVERT_COMMA($TPTS)), __FILE__, __LINE__);
93         if (SQL_NUMROWS($result) > 0) {
94                 // Free memory
95                 SQL_FREERESULT($result);
96
97                 // Check for his payouts
98                 $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 banner
99 FROM `{!_MYSQL_PREFIX!}_user_payouts` AS p
100 LEFT JOIN `{!_MYSQL_PREFIX!}_payout_types` AS t
101 ON p.payout_id = t.id
102 WHERE p.userid = %s
103 ORDER BY p.payout_timestamp DESC",
104                  array(getUserId()), __FILE__, __LINE__);
105                 if (SQL_NUMROWS($result_payouts) > 0) {
106                         // List all his requests
107                         $OUT = ''; $SW = 2;
108                         while ($content = SQL_FETCHARRAY($result_payouts)) {
109                                 // Translate status
110                                 $content['status'] = getMessage('PAYOUT_MEMBER_STATUS_'.strtoupper($content['status']).'');
111                                 $content['status'] = "<div class=\"member_failed\">".$content['status']."</div>";
112
113                                 // Nothing entered must be secured in member/what-payputs.php !
114                                 if ($content['allow'] == 'Y') {
115                                         // Banner/Textlink views/clicks request
116                                         if (!empty($content['banner'])) {
117                                                 // Banner
118                                                 $content['target_account'] = "<img src=\"".$content['banner']."\" alt=\"".$content['alt']."\" title=\"".$content['alt']."\" border=\"0\" />";
119                                         } else {
120                                                 // Textlink
121                                                 $content['target_account'] = $content['alt'];
122                                         }
123                                         $content['target_bank'] = "<a href=\"".DEREFERER($content['url'])."\" target=\"_blank\">{--CLICK_HERE--}</a>";
124                                 } else {
125                                         // e-currency payout request
126                                         if (empty($content['target_account'])) $content['target_account'] = "---";
127                                         if (empty($content['target_bank']))    $content['target_bank']    = "---";
128                                 }
129
130                                 // Prepare data for the template
131                                 // @TODO Rewritings: acc->target_account,bank->target_bank in templates
132                                 $content = array(
133                                         'sw'     => $SW,
134                                         'acc'    => $content['target_account'],
135                                         'points' => TRANSLATE_COMMA($content['payout_total'])." ".COMPILE_CODE($content['type']),
136                                         'bank'   => $content['target_bank'],
137                                         'stamp'  => MAKE_DATETIME($content['payout_timestamp'], "2"),
138                                         'status' => $content['status']
139                                 );
140
141                                 // Load row template and switch colors
142                                 $OUT .= LOAD_TEMPLATE("member_payout_row", true, $content);
143                                 $SW = 3 - $SW;
144                         }
145
146                         // Remember rows in constant
147                         // @TODO Rewrite this constant
148                         define('__PAYOUT_ROWS', $OUT);
149
150                         // Load template
151                         LOAD_TEMPLATE("member_payout");
152                 }
153
154                 // Free memory
155                 SQL_FREERESULT($result_payouts);
156
157                 // Output payout list
158                 PAYOUT_OUTPUT_PAYOUT_LIST($TPTS);
159         }
160 } else {
161         // Chedk if he can get paid by selected type
162         $result = SQL_QUERY_ESC("SELECT type, rate, min_points, allow_url AS allow FROM `{!_MYSQL_PREFIX!}_payout_types` WHERE id=%s LIMIT 1",
163                 array(bigintval(REQUEST_GET('payout'))), __FILE__, __LINE__);
164
165         if (SQL_NUMROWS($result) == 1) {
166                 // ID is valid so load the data
167                 $content = SQL_FETCHARRAY($result);
168                 SQL_FREERESULT($result);
169
170                 // Calculate maximum value
171                 $max = round($TPTS * $content['rate'] - 0.5);
172
173                 // Calulcate points from submitted amount
174                 $PAYOUT = 0;
175                 if (REQUEST_ISSET_POST(('payout'))) {
176                         $PAYOUT  = bigintval(REQUEST_POST('payout')) / $content['rate'];
177                         $PAY_MAX = $max / $content['rate'];
178                 }
179
180                 // Move variables into constants for templates
181                 // @TODO Rewrite these constants
182                 define('PAYOUT_MAX_VALUE' , $max);
183                 define('PAYOUT_TYPE_VALUE', COMPILE_CODE($content['type']));
184
185                 if (REVERT_COMMA($TPTS) >= $content['min_points']) {
186                         // Ok, he can get be paid
187                         if ((IS_FORM_SENT()) && ($PAYOUT <= $PAY_MAX) && ($PAYOUT >= $content['min_points'])) {
188                                 // Calculate exact value
189                                 // @TODO Rewrite this constant
190                                 define('PAYOUT_POINTS_VALUE', $PAYOUT);
191
192                                 // Subtract points from member's account
193                                 SUB_POINTS('payout', getUserId(), $PAYOUT);
194
195                                 // Add entry to his tranfer history
196                                 if ($content['allow'] == 'Y') {
197                                         // Banner/textlink ordered
198                                         SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_user_payouts` (userid, payout_total, payout_id, payout_timestamp, status, target_url, link_text, banner_url)
199 VALUES (%s,%s,%s, UNIX_TIMESTAMP(), 'NEW','%s','%s','%s')",
200  array(
201         getUserId(),
202         bigintval(REQUEST_POST('payout')),
203         bigintval(REQUEST_GET('payout')),
204         REQUEST_POST('turl'),
205         REQUEST_POST('alt'),
206         REQUEST_POST('banner')
207 ), __FILE__, __LINE__);
208
209                                         // Load templates
210                                         $msg_mem = LOAD_EMAIL_TEMPLATE("member_payout_request_banner", array(), getUserId());
211                                         if (GET_EXT_VERSION('admins') >= '0.4.1') {
212                                                 $adm_tpl = "admin_payout_request_banner";
213                                         } else {
214                                                 $msg_adm = LOAD_EMAIL_TEMPLATE("admin_payout_request_banner", array(), getUserId());
215                                         }
216                                 } else {
217                                         // e-currency payout requested
218                                         SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_user_payouts` (userid, payout_total, target_account, target_bank, payout_id, payout_timestamp, status, password)
219 VALUES (%s,%s,%s,'%s',%s, UNIX_TIMESTAMP(), 'NEW','%s')",
220  array(
221         getUserId(),
222         bigintval(REQUEST_POST('payout')),
223         bigintval(REQUEST_POST('account')),
224         REQUEST_POST('bank'),
225         bigintval(REQUEST_GET('payout')),
226         REQUEST_POST('pass')
227 ), __FILE__, __LINE__);
228
229                                         // Load templates
230                                         $msg_mem = LOAD_EMAIL_TEMPLATE("member_payout_request", array(), getUserId());
231                                         $msg_adm = LOAD_EMAIL_TEMPLATE("admin_payout_request", array(), getUserId());
232                                         $admin_tpl = '';
233                                         if (GET_EXT_VERSION('admins') >= '0.4.1')
234                                         {
235                                                 $admin_tpl = "admin_payout_request";
236                                         }
237                                 }
238
239                                 // Generate task
240                                 SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_task_system` (assigned_admin, status, task_type, subject, text, task_created, userid)
241 VALUES (0, 'NEW','PAYOUT_REQUEST','[payout:] {--PAYOUT_REQUEST_ADMIN--}','%s', UNIX_TIMESTAMP(), %s)",
242  array(
243         $msg_adm,
244         getUserId()
245 ), __FILE__, __LINE__);
246
247                                 // Send out mails
248                                 SEND_EMAIL(getUserId(), getMessage('PAYOUT_REQUEST_MEMBER'), $msg_mem);
249
250                                 // To admin(s)
251                                 SEND_ADMIN_NOTIFICATION(getMessage('PAYOUT_REQUEST_ADMIN'), $admin_tpl, array(), getUserId());
252
253                                 // Load template and output it
254                                 LOAD_TEMPLATE('admin_settings_saved', false, getMessage('PAYOUT_REQUEST_SENT'));
255                         } elseif ($content['allow'] == 'Y') {
256                                 // Prepare content
257                                 $content = array(
258                                         'payout' => bigintval(REQUEST_GET('payout'))
259                                 );
260
261                                 // Generate banner order form
262                                 LOAD_TEMPLATE("member_payout_form_banner", false, $content);
263                         } else {
264                                 // Prepare content
265                                 $content = array(
266                                         'payout' => bigintval(REQUEST_GET('payout'))
267                                 );
268
269                                 // Generate normal form
270                                 LOAD_TEMPLATE("member_payout_form", false, $content);
271                         }
272                 } else {
273                         // Not enougth points
274                         LOAD_TEMPLATE('admin_settings_saved', false, getMessage('PAYOUT_POINTS_NOT_ENOUGTH'));
275                 }
276         } else {
277                 // ID is invalid
278                 LOAD_TEMPLATE('admin_settings_saved', false, getMessage('PAYOUT_ID_INVALID'));
279         }
280 }
281
282 //
283 ?>