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