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