]> git.mxchange.org Git - mailer.git/blob - inc/modules/member/what-wernis.php
More improved SQL queries
[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         } elseif (empty($_POST['wds66_password'])) {
205                 // Nothing entered in WDS66 password
206                 LOAD_TEMPLATE("admin_settings_saved", false, WERNIS_MEMBER_EMPTY_PASSWORD);
207         } elseif (empty($_POST['amount'])) {
208                 // Nothing entered in amount
209                 LOAD_TEMPLATE("admin_settings_saved", false, WERNIS_MEMBER_EMPTY_AMOUNT);
210         } elseif ($_POST['wds66_id'] != bigintval($_POST['wds66_id'])) {
211                 // Only numbers in account ID!
212                 LOAD_TEMPLATE("admin_settings_saved", false, WERNIS_MEMBER_INVALID_USERNAME);
213         } elseif ($_POST['amount'] != bigintval($_POST['amount'])) {
214                 // Only numbers in amount!
215                 LOAD_TEMPLATE("admin_settings_saved", false, WERNIS_MEMBER_INVALID_AMOUNT);
216         } else {
217                 // Check input data depending on the mode and execute the requested mode
218                 switch ($_GET['mode']) {
219                         case "withdraw": // Widthdraws WDS66 -> This exchange
220                                 if ($_POST['amount'] < getConfig('wernis_min_withdraw')) {
221                                         // Not enougth entered!
222                                         LOAD_TEMPLATE("admin_settings_saved", false, sprintf(WERNIS_MEMBER_AMOUNT_SMALLER_MIN, bigintval(getConfig('wernis_min_withdraw'))));
223                                 } else {
224                                         // All is fine here so do the withdraw
225                                         $success = WERNIS_EXECUTE_WITHDRAW($_POST['wds66_id'], md5($_POST['wds66_password']), $_POST['amount']);
226                                         if ($success) {
227                                                 // Add it to this amount
228                                                 unset($GLOBALS['ref_level']);
229                                                 ADD_POINTS_REFSYSTEM("wernis_withdraw", $GLOBALS['userid'], bigintval($_POST['amount']), false, 0, false, "direct");
230
231                                                 // Update the user data as well..
232                                                 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_user_data` SET `wernis_userid`=%s WHERE userid=%s LIMIT 1",
233                                                         array(bigintval($_POST['wds66_id']), $GLOBALS['userid']), __FILE__, __LINE__);
234
235                                                 // All done!
236                                                 LOAD_TEMPLATE("admin_settings_saved", false, WERNIS_MEMBER_WITHDRAW_DONE);
237                                                 return;
238                                         } elseif ((GET_WERNIS_ERROR_CODE() == "user_failed") || (GET_WERNIS_ERROR_CODE() == "own_failed") || (GET_WERNIS_ERROR_CODE() == "amount_failed")) {
239                                                 // Wrong login data
240                                                 LOAD_TEMPLATE("admin_settings_saved", false, GET_WERNIS_ERROR_MESSAGE());
241                                         } else {
242                                                 // Something went wrong
243                                                 LOAD_TEMPLATE("admin_settings_saved", false, sprintf(WERNIS_MEMBER_WITHDRAW_FAILED, GET_WERNIS_ERROR_MESSAGE()));
244                                         }
245                                 }
246                                 break;
247
248                         case "payout": // Payout this exchange -> WDS66
249                                 if ($_POST['amount'] < getConfig('wernis_min_payout')) {
250                                         // Not enougth entered!
251                                         LOAD_TEMPLATE("admin_settings_saved", false, sprintf(WERNIS_MEMBER_AMOUNT_SMALLER_MIN, bigintval(getConfig('wernis_min_payout'))));
252                                 } elseif ($_POST['amount'] > $points) {
253                                         // Not enougth points left!
254                                         LOAD_TEMPLATE("admin_settings_saved", false, sprintf(WERNIS_MEMBER_PAYOUT_POINTS_DEPLETED, bigintval($_POST['amount']), bigintval($points)));
255                                 } else {
256                                         // All is fine here so do the withdraw
257                                         $success = WERNIS_EXECUTE_PAYOUT($_POST['wds66_id'], $_POST['amount']);
258                                         if ($success) {
259                                                 // Sub points
260                                                 SUB_POINTS("wernis_payout", $GLOBALS['userid'], $_POST['amount']);
261
262                                                 // Update WDS66 id
263                                                 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_user_data` SET `wernis_userid`=%s WHERE userid=%s LIMIT 1",
264                                                         array(bigintval($_POST['wds66_id']), $GLOBALS['userid']), __FILE__, __LINE__);
265
266                                                 // All done!
267                                                 LOAD_TEMPLATE("admin_settings_saved", false, WERNIS_MEMBER_PAYOUT_DONE);
268                                                 return;
269                                         } 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")) {
270                                                 // Wrong login data
271                                                 LOAD_TEMPLATE("admin_settings_saved", false, GET_WERNIS_ERROR_MESSAGE());
272                                         } else {
273                                                 // Something went wrong
274                                                 LOAD_TEMPLATE("admin_settings_saved", false, sprintf(WERNIS_MEMBER_WITHDRAW_FAILED, GET_WERNIS_ERROR_MESSAGE()));
275                                         }
276                                 }
277                                 break;
278
279                         default: // Invalid mode!
280                                 DEBUG_LOG(__FILE__, __LINE__, sprintf("Invalid mode %s detected.", $_GET['mode']));
281                                 LOAD_TEMPLATE("admin_settings_saved", false, sprintf(WERNIS_MEMBER_MODE_INVALID, SQL_ESCAPE($_GET['mode'])));
282                                 return;
283                 }
284         }
285 } // END - if
286
287 // Prepare mode for template name
288 $mode = sprintf("member_wernis_mode_%s", SQL_ESCAPE($_GET['mode']));
289
290 // Load the template
291 LOAD_TEMPLATE($mode, false, $content);
292
293 //
294 ?>