b5f4913e6d101925f93cd14872dee936468b17a5
[mailer.git] / inc / modules / member / what-wernis.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 04/12/2004 *
4  * ================                             Last change: 11/16/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : what-wernis.php                                  *
8  * -------------------------------------------------------------------- *
9  * Short description : Wernis 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         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
37         require($INC);
38 } elseif (!IS_MEMBER()) {
39         // User is not logged in
40         LOAD_URL("modules.php?module=index");
41 } elseif ((!EXT_IS_ACTIVE("wernis")) && (!IS_ADMIN())) {
42         addFatalMessage(EXTENSION_PROBLEM_EXT_INACTIVE, "wernis");
43         return;
44 }
45
46 // Add description as navigation point
47 ADD_DESCR("member", __FILE__);
48
49 // Check if the admin has entered the data
50 if ((getConfig('wernis_api_id') == "") || (getConfig('wernis_api_md5') == "")) {
51         // Something important is missing...
52         LOAD_TEMPLATE("admin_settings_saved", false, WERNIS_MEMBER_API_DATA_MISSING);
53         return;
54 } // END - if
55
56 // Init the content array and points
57 $content = array(); $points = false;
58
59 // Is the mode set (withdraw or payout)
60 if ((!isset($_GET['mode'])) || ($_GET['mode'] == "choose")) {
61         // Let the user choose what he wants to do
62         $content['refid']    = bigintval(getConfig('wernis_refid'));
63         $content['wds66_id'] = 0;
64
65         // Get WDS66 id
66         $result = SQL_QUERY_ESC("SELECT wernis_userid FROM `{!_MYSQL_PREFIX!}_user_data` WHERE userid=%s LIMIT 1",
67                 array($GLOBALS['userid']), __FILE__, __LINE__);
68
69         // Are there some entries?
70         if (SQL_NUMROWS($result) == 1) {
71                 // Fetch ID
72                 list($content['wds66_id']) = SQL_FETCHROW($result);
73         } // END - if
74
75         // Free result
76         SQL_FREERESULT($result);
77
78         // Is there an ID?
79         if ((!empty($content['wds66_id'])) && (!isset($_GET['mode']))) {
80                 // Then use an other "mode"
81                 $_GET['mode'] = "list";
82
83                 // And load all rows!
84                 $result = SQL_QUERY_ESC("SELECT `id`, `wernis_account`, `wernis_amount`, `wernis_timestamp`, `wernis_type` FROM `{!_MYSQL_PREFIX!}_user_wernis` WHERE `userid` = %s ORDER BY `wernis_timestamp` DESC",
85                         array($GLOBALS['userid']), __FILE__, __LINE__);
86
87                 // Load all rows
88                 $content['rows'] = ""; $SW = 2;
89                 while ($data = SQL_FETCHARRAY($result)) {
90                         // Prepare data for output
91                         $rowContent = array(
92                                 'stamp'    => MAKE_DATETIME($data['wernis_timestamp'], "2"),
93                                 'points'   => TRANSLATE_COMMA($data['wernis_amount']),
94                                 'acc'      => bigintval($data['wernis_account']),
95                                 'status'   => WERNIS_TRANSFER_STATUS($data['wernis_type']),
96                                 'raw_type' => strtolower($data['wernis_type']),
97                                 'sw'       => $SW,
98                         );
99
100                         // Load row template
101                         $content['rows'] .= LOAD_TEMPLATE("member_wernis_mode_list_row", true, $rowContent);
102                         $SW = 3 - $SW;
103                 }
104
105                 // Free result
106                 SQL_FREERESULT($result);
107         } else {
108                 // Default links are not active!
109                 $content['payout_link']   = "<em>".WERNIS_MEMBER_PAYOUT_DISABLED."</em>";
110                 $content['withdraw_link'] = "<em>".WERNIS_MEMBER_WITHDRAW_DISABLED."</em>";
111
112                 // Is the payout mode active?
113                 if (getConfig('wernis_payout_active') == "Y") {
114                         // Add link
115                         $content['payout_link'] = "<a class=\"menu_blur\" style=\"height: 40px\" href=\"{!URL!}/modules.php?module=login&amp;what=wernis&amp;mode=payout\"><div style=\"padding-top: 10px\">".MEMBER_WERNIS_MODE_PAYOUT."</div></a>";
116                 } // END - if
117
118                 // Is the withdraw mode active?
119                 if (getConfig('wernis_withdraw_active') == "Y") {
120                         // Add link
121                         $content['withdraw_link'] = "<a class=\"menu_blur\" style=\"height: 40px\" href=\"{!URL!}/modules.php?module=login&amp;what=wernis&amp;mode=withdraw\"><div style=\"padding-top: 10px\">".MEMBER_WERNIS_MODE_WITHDRAW."</div></a>";
122                 } // END - if
123
124                 // Mode chooser! ;-)
125                 $_GET['mode'] = "choose";
126         }
127 } elseif (($_GET['mode'] == "payout") && (getConfig('wernis_payout_active') == "Y")) {
128         // Get total points and check if the user can request a payout
129         $points = GET_TOTAL_DATA($GLOBALS['userid'], "user_points", "points") - GET_TOTAL_DATA($GLOBALS['userid'], "user_data", "used_points");
130
131         // Remove the registration fee
132         $points = $points - getConfig('points_register');
133
134         // Is there a percentage or fixed fee?
135         $points = WERNIS_TAKE_FEE($points, "payout");
136
137         // Is this enougth for a payout?
138         if ($points < getConfig('wernis_min_payout')) {
139                 // No, then abort here
140                 LOAD_TEMPLATE("admin_settings_saved", false, sprintf(WERNIS_MEMBER_MIN_PAYOUT, TRANSLATE_COMMA(getConfig('wernis_min_payout'))));
141                 return;
142         } // END - if
143
144         // No dots here...
145         $points = explode(".", $points);
146         $points = bigintval($points[0]);
147
148         // Add points to content array
149         $content['points']     = TRANSLATE_COMMA($points);
150         $content['min_points'] = TRANSLATE_COMMA(getConfig('wernis_min_payout'));
151
152         // Add fees to array
153         WERNIS_ADD_FEES_TO_ARRAY($content);
154
155         // Get WDS66 id
156         $content['wds66_id'] = "";
157         $result = SQL_QUERY_ESC("SELECT wernis_userid FROM `{!_MYSQL_PREFIX!}_user_data` WHERE userid=%s LIMIT 1",
158                 array($GLOBALS['userid']), __FILE__, __LINE__);
159
160         // Are there some entries?
161         if (SQL_NUMROWS($result) == 1) {
162                 // Fetch ID
163                 list($content['wds66_id']) = SQL_FETCHROW($result);
164         } // END - if
165
166         // Free result
167         SQL_FREERESULT($result);
168 } elseif (($_GET['mode'] == "withdraw") && (getConfig('wernis_withdraw_active') == "Y")) {
169         // Get total points for just displaying them
170         $points = GET_TOTAL_DATA($GLOBALS['userid'], "user_points", "points") - GET_TOTAL_DATA($GLOBALS['userid'], "user_data", "used_points");
171
172         // Prepare data for the template
173         $content['points']     = TRANSLATE_COMMA($points);
174         $content['min_points'] = TRANSLATE_COMMA(getConfig('wernis_min_withdraw'));
175         $content['wds66_id']   = "";
176
177         // Add fees to array
178         WERNIS_ADD_FEES_TO_ARRAY($content);
179
180         // Get WDS66 id
181         $result = SQL_QUERY_ESC("SELECT wernis_userid FROM `{!_MYSQL_PREFIX!}_user_data` WHERE userid=%s LIMIT 1",
182                 array($GLOBALS['userid']), __FILE__, __LINE__);
183
184         // Are there some entries?
185         if (SQL_NUMROWS($result) == 1) {
186                 // Fetch ID
187                 list($content['wds66_id']) = SQL_FETCHROW($result);
188         } // END - if
189
190         // Free result
191         SQL_FREERESULT($result);
192 } else {
193         // Invalid mode!
194         LOAD_TEMPLATE("admin_settings_saved", false, sprintf(WERNIS_MEMBER_MODE_INVALID, SQL_ESCAPE($_GET['mode'])));
195         return;
196 }
197
198 // Is the formular sent?
199 if ((isset($_POST['ok'])) && (isset($_GET['mode']))) {
200         // Is the user ID and password set?
201         if (empty($_POST['wds66_id'])) {
202                 // Nothing entered in WDS66 user ID
203                 LOAD_TEMPLATE("admin_settings_saved", false, WERNIS_MEMBER_EMPTY_USERNAME);
204                 OUTPUT_HTML("<br />");
205         } elseif (empty($_POST['wds66_password'])) {
206                 // Nothing entered in WDS66 password
207                 LOAD_TEMPLATE("admin_settings_saved", false, WERNIS_MEMBER_EMPTY_PASSWORD);
208                 OUTPUT_HTML("<br />");
209         } elseif (empty($_POST['amount'])) {
210                 // Nothing entered in amount
211                 LOAD_TEMPLATE("admin_settings_saved", false, WERNIS_MEMBER_EMPTY_AMOUNT);
212                 OUTPUT_HTML("<br />");
213         } elseif ($_POST['wds66_id'] != bigintval($_POST['wds66_id'])) {
214                 // Only numbers in account ID!
215                 LOAD_TEMPLATE("admin_settings_saved", false, WERNIS_MEMBER_INVALID_USERNAME);
216                 OUTPUT_HTML("<br />");
217         } elseif ($_POST['amount'] != bigintval($_POST['amount'])) {
218                 // Only numbers in amount!
219                 LOAD_TEMPLATE("admin_settings_saved", false, WERNIS_MEMBER_INVALID_AMOUNT);
220                 OUTPUT_HTML("<br />");
221         } else {
222                 // Check input data depending on the mode and execute the requested mode
223                 switch ($_GET['mode']) {
224                         case "withdraw": // Widthdraws WDS66 -> This exchange
225                                 if ($_POST['amount'] < getConfig('wernis_min_withdraw')) {
226                                         // Not enougth entered!
227                                         LOAD_TEMPLATE("admin_settings_saved", false, sprintf(WERNIS_MEMBER_AMOUNT_SMALLER_MIN, bigintval(getConfig('wernis_min_withdraw'))));
228                                         OUTPUT_HTML("<br />");
229                                 } else {
230                                         // All is fine here so do the withdraw
231                                         $success = WERNIS_EXECUTE_WITHDRAW($_POST['wds66_id'], md5($_POST['wds66_password']), $_POST['amount']);
232                                         if ($success) {
233                                                 // Add it to this amount
234                                                 unset($GLOBALS['ref_level']);
235                                                 ADD_POINTS_REFSYSTEM("wernis_withdraw", $GLOBALS['userid'], bigintval($_POST['amount']), false, 0, false, "direct");
236
237                                                 // Update the user data as well..
238                                                 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_user_data` SET `wernis_userid`=%s WHERE userid=%s LIMIT 1",
239                                                         array(bigintval($_POST['wds66_id']), $GLOBALS['userid']), __FILE__, __LINE__);
240
241                                                 // All done!
242                                                 LOAD_TEMPLATE("admin_settings_saved", false, WERNIS_MEMBER_WITHDRAW_DONE);
243                                                 return;
244                                         } elseif ((GET_WERNIS_ERROR_CODE() == "user_failed") || (GET_WERNIS_ERROR_CODE() == "own_failed") || (GET_WERNIS_ERROR_CODE() == "amount_failed")) {
245                                                 // Wrong login data
246                                                 LOAD_TEMPLATE("admin_settings_saved", false, GET_WERNIS_ERROR_MESSAGE());
247                                                 OUTPUT_HTML("<br />");
248                                         } else {
249                                                 // Something went wrong
250                                                 LOAD_TEMPLATE("admin_settings_saved", false, sprintf(WERNIS_MEMBER_WITHDRAW_FAILED, GET_WERNIS_ERROR_MESSAGE()));
251                                                 OUTPUT_HTML("<br />");
252                                         }
253                                 }
254                                 break;
255
256                         case "payout": // Payout this exchange -> WDS66
257                                 if ($_POST['amount'] < getConfig('wernis_min_payout')) {
258                                         // Not enougth entered!
259                                         LOAD_TEMPLATE("admin_settings_saved", false, sprintf(WERNIS_MEMBER_AMOUNT_SMALLER_MIN, bigintval(getConfig('wernis_min_payout'))));
260                                         OUTPUT_HTML("<br />");
261                                 } elseif ($_POST['amount'] > $points) {
262                                         // Not enougth points left!
263                                         LOAD_TEMPLATE("admin_settings_saved", false, sprintf(WERNIS_MEMBER_PAYOUT_POINTS_DEPLETED, bigintval($_POST['amount']), bigintval($points)));
264                                         OUTPUT_HTML("<br />");
265                                 } else {
266                                         // All is fine here so do the withdraw
267                                         $success = WERNIS_EXECUTE_PAYOUT($_POST['wds66_id'], $_POST['amount']);
268                                         if ($success) {
269                                                 // Sub points
270                                                 SUB_POINTS("wernis_payout", $GLOBALS['userid'], $_POST['amount']);
271
272                                                 // Update WDS66 id
273                                                 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_user_data` SET `wernis_userid`=%s WHERE userid=%s LIMIT 1",
274                                                         array(bigintval($_POST['wds66_id']), $GLOBALS['userid']), __FILE__, __LINE__);
275
276                                                 // All done!
277                                                 LOAD_TEMPLATE("admin_settings_saved", false, WERNIS_MEMBER_PAYOUT_DONE);
278                                                 return;
279                                         } elseif ((GET_WERNIS_ERROR_CODE() == "user_failed") || (GET_WERNIS_ERROR_CODE() == "own_failed") || (GET_WERNIS_ERROR_CODE() == "amount_failed") || (GET_WERNIS_ERROR_CODE() == "api_amount_failed")) {
280                                                 // Wrong login data
281                                                 LOAD_TEMPLATE("admin_settings_saved", false, GET_WERNIS_ERROR_MESSAGE());
282                                                 OUTPUT_HTML("<br />");
283                                         } else {
284                                                 // Something went wrong
285                                                 LOAD_TEMPLATE("admin_settings_saved", false, sprintf(WERNIS_MEMBER_WITHDRAW_FAILED, GET_WERNIS_ERROR_MESSAGE()));
286                                                 OUTPUT_HTML("<br />");
287                                         }
288                                 }
289                                 break;
290
291                         default: // Invalid mode!
292                                 DEBUG_LOG(__FILE__, __LINE__, sprintf("Invalid mode %s detected.", $_GET['mode']));
293                                 LOAD_TEMPLATE("admin_settings_saved", false, sprintf(WERNIS_MEMBER_MODE_INVALID, SQL_ESCAPE($_GET['mode'])));
294                                 return;
295                 }
296         }
297 } // END - if
298
299 // Prepare mode for template name
300 $mode = sprintf("member_wernis_mode_%s", SQL_ESCAPE($_GET['mode']));
301
302 // Load the template
303 LOAD_TEMPLATE($mode, false, $content);
304
305 //
306 ?>