Wernis extension added
[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 (ereg(basename(__FILE__), $_SERVER['PHP_SELF'])) {
36         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
37         require($INC);
38 } elseif (!IS_LOGGED_IN()) {
39         // User is not logged in
40         LOAD_URL("modules.php?module=index");
41 } elseif ((!EXT_IS_ACTIVE("wernis")) && (!IS_ADMIN())) {
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", basename(__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 }
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
65         // Get WDS66 id
66         $result = SQL_QUERY_ESC("SELECT wernis_userid FROM "._MYSQL_PREFIX."_user_data WHERE userid=%d 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         }
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` = %d 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                 // Mode chooser! ;-)
109                 $_GET['mode'] = "choose";
110         }
111 } elseif ($_GET['mode'] == "pay") {
112         // Get total points and check if the user can request a payout
113         $points = GET_TOTAL_DATA($GLOBALS['userid'], "user_points", "points") - GET_TOTAL_DATA($GLOBALS['userid'], "user_data", "used_points");
114
115         // No dots here...
116         $points = explode(".", $points);
117         $points = bigintval($points[0]);
118
119         // Is this enougth for a payout?
120         if ($points < $_CONFIG['wernis_min_payout']) {
121                 // No, then abort here
122                 LOAD_TEMPLATE("admin_settings_saved", false, sprintf(WERNIS_MEMBER_MIN_PAYOUT_PAYOUT, TRANSLATE_COMMA($_CONFIG['wernis_min_payout'])));
123                 return;
124         }
125
126         // Add points to content array
127         $content['points']              = $points;
128         $content['min_points']  = TRANSLATE_COMMA($_CONFIG['wernis_min_payout']);
129
130         // Get WDS66 id
131         $content['wds66_id'] = "";
132         $result = SQL_QUERY_ESC("SELECT wernis_userid FROM "._MYSQL_PREFIX."_user_data WHERE userid=%d LIMIT 1",
133                 array($GLOBALS['userid']), __FILE__, __LINE__);
134
135         // Are there some entries?
136         if (SQL_NUMROWS($result) == 1) {
137                 // Fetch ID
138                 list($content['wds66_id']) = SQL_FETCHROW($result);
139         }
140
141         // Free result
142         SQL_FREERESULT($result);
143 } elseif ($_GET['mode'] == "withdraw") {
144         // Get total points for just displaying them
145         $points = GET_TOTAL_DATA($GLOBALS['userid'], "user_points", "points") - GET_TOTAL_DATA($GLOBALS['userid'], "user_data", "used_points");
146
147         // Prepare data for the template
148         $content['points']              = TRANSLATE_COMMA($points);
149         $content['min_points']  = TRANSLATE_COMMA($_CONFIG['wernis_min_withdraw']);
150         $content['wds66_id']    = "";
151
152         // Get WDS66 id
153         $result = SQL_QUERY_ESC("SELECT wernis_userid FROM "._MYSQL_PREFIX."_user_data WHERE userid=%d LIMIT 1",
154                 array($GLOBALS['userid']), __FILE__, __LINE__);
155
156         // Are there some entries?
157         if (SQL_NUMROWS($result) == 1) {
158                 // Fetch ID
159                 list($content['wds66_id']) = SQL_FETCHROW($result);
160         }
161
162         // Free result
163         SQL_FREERESULT($result);
164 } else {
165         // Invalid mode!
166         LOAD_TEMPLATE("admin_settings_saved", false, sprintf(WERNIS_MEMBER_MODE_INVALID, SQL_ESCAPE($_GET['mode'])));
167         return;
168 }
169
170 // Is the formular sent?
171 if ((isset($_POST['ok'])) && (isset($_GET['mode']))) {
172         // Check input data depending on the mode and execute the requested mode
173         switch ($_GET['mode']) {
174                 case "withdraw": // Widthdraws WDS66 -> This exchange
175                         // Is the user ID and password set?
176                         if (empty($_POST['wds66_id'])) {
177                                 // Nothing entered in WDS66 user ID
178                                 LOAD_TEMPLATE("admin_settings_saved", false, WERNIS_MEMBER_EMPTY_USERNAME);
179                                 OUTPUT_HTML("<br />");
180                         } elseif (empty($_POST['wds66_password'])) {
181                                 // Nothing entered in WDS66 password
182                                 LOAD_TEMPLATE("admin_settings_saved", false, WERNIS_MEMBER_EMPTY_PASSWORD);
183                                 OUTPUT_HTML("<br />");
184                         } elseif (empty($_POST['amount'])) {
185                                 // Nothing entered in amount
186                                 LOAD_TEMPLATE("admin_settings_saved", false, WERNIS_MEMBER_EMPTY_AMOUNT);
187                                 OUTPUT_HTML("<br />");
188                         } elseif ($_POST['wds66_id'] != bigintval($_POST['wds66_id'])) {
189                                 // Only numbers in account ID!
190                                 LOAD_TEMPLATE("admin_settings_saved", false, WERNIS_MEMBER_INVALID_USERNAME);
191                                 OUTPUT_HTML("<br />");
192                         } elseif ($_POST['amount'] != bigintval($_POST['amount'])) {
193                                 // Only numbers in amount!
194                                 LOAD_TEMPLATE("admin_settings_saved", false, WERNIS_MEMBER_INVALID_AMOUNT);
195                                 OUTPUT_HTML("<br />");
196                         } elseif ($_POST['amount'] < $_CONFIG['wernis_min_withdraw']) {
197                                 // Not enougth entered!
198                                 LOAD_TEMPLATE("admin_settings_saved", false, sprintf(WERNIS_MEMBER_AMOUNT_SMALLER_MIN, bigintval($_CONFIG['wernis_min_withdraw'])));
199                                 OUTPUT_HTML("<br />");
200                         } else {
201                                 // All is fine here so do the withdraw
202                                 $success = WERNIS_EXECUTE_WITHDRAW($_POST['wds66_id'], md5($_POST['wds66_password']), $_POST['amount']);
203                                 if ($success) {
204                                         // Add it to this amount
205                                         $DEPTH = 0;
206                                         ADD_POINTS_REFSYSTEM($GLOBALS['userid'], bigintval($_POST['amount']), false, 0, false, "direct");
207
208                                         // Update the user data as well..
209                                         $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_data SET `wernis_userid`=%d WHERE userid=%d LIMIT 1",
210                                                 array(bigintval($_POST['wds66_id']), $GLOBALS['userid']), __FILE__, __LINE__);
211
212                                         // All done!
213                                         LOAD_TEMPLATE("admin_settings_saved", false, WERNIS_MEMBER_WITHDRAW_DONE);
214                                         return;
215                                 } elseif ((GET_WERNIS_ERROR_CODE() == "user_failed") || (GET_WERNIS_ERROR_CODE() == "own_failed") || (GET_WERNIS_ERROR_CODE() == "amount_failed")) {
216                                         // Wrong login data
217                                         LOAD_TEMPLATE("admin_settings_saved", false, GET_WERNIS_ERROR_MESSAGE());
218                                         OUTPUT_HTML("<br />");
219                                 } else {
220                                         // Something went wrong
221                                         LOAD_TEMPLATE("admin_settings_saved", false, sprintf(WERNIS_MEMBER_WITHDRAW_FAILED, GET_WERNIS_ERROR_MESSAGE()));
222                                         OUTPUT_HTML("<br />");
223                                 }
224                         }
225                         break;
226
227                 case "pay": // Payout this exchange -> WDS66
228                         // Is the user ID and password set?
229                         if (empty($_POST['wds66_id'])) {
230                                 // Nothing entered in WDS66 user ID
231                                 LOAD_TEMPLATE("admin_settings_saved", false, WERNIS_MEMBER_EMPTY_USERNAME);
232                                 OUTPUT_HTML("<br />");
233                         } elseif (empty($_POST['wds66_password'])) {
234                                 // Nothing entered in WDS66 password
235                                 LOAD_TEMPLATE("admin_settings_saved", false, WERNIS_MEMBER_EMPTY_PASSWORD);
236                                 OUTPUT_HTML("<br />");
237                         } elseif (empty($_POST['amount'])) {
238                                 // Nothing entered in amount
239                                 LOAD_TEMPLATE("admin_settings_saved", false, WERNIS_MEMBER_EMPTY_AMOUNT);
240                                 OUTPUT_HTML("<br />");
241                         } elseif ($_POST['wds66_id'] != bigintval($_POST['wds66_id'])) {
242                                 // Only numbers in account ID!
243                                 LOAD_TEMPLATE("admin_settings_saved", false, WERNIS_MEMBER_INVALID_USERNAME);
244                                 OUTPUT_HTML("<br />");
245                         } elseif ($_POST['amount'] != bigintval($_POST['amount'])) {
246                                 // Only numbers in amount!
247                                 LOAD_TEMPLATE("admin_settings_saved", false, WERNIS_MEMBER_INVALID_AMOUNT);
248                                 OUTPUT_HTML("<br />");
249                         } elseif ($_POST['amount'] < $_CONFIG['wernis_min_payout']) {
250                                 // Not enougth entered!
251                                 LOAD_TEMPLATE("admin_settings_saved", false, sprintf(WERNIS_MEMBER_AMOUNT_SMALLER_MIN, bigintval($_CONFIG['wernis_min_payout'])));
252                                 OUTPUT_HTML("<br />");
253                         } elseif ($_POST['amount'] > $points) {
254                                 // Not enougth points left!
255                                 LOAD_TEMPLATE("admin_settings_saved", false, sprintf(WERNIS_MEMBER_PAYOUT_POINTS_DEPLETED, bigintval($_POST['amount']), bigintval($points)));
256                                 OUTPUT_HTML("<br />");
257                         } else {
258                                 // All is fine here so do the withdraw
259                                 $success = WERNIS_EXECUTE_PAYOUT($_POST['wds66_id'], md5($_POST['wds66_password']), $_POST['amount']);
260                                 if ($success) {
261                                         // Default is locked!
262                                         $locked = true;
263
264                                         // Shall I "pay" the referral points imidiately?
265                                         if ($_CONFIG['ref_payout'] == "0") {
266                                                 // Yes, "pay" it now
267                                                 $locked = false;
268                                         }
269
270                                         // Remove the points from the account
271                                         $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_data SET `used_points` = `used_points` + %d, `wernis_userid`=%d WHERE userid=%d LIMIT 1",
272                                                 array(bigintval($_POST['amount']), bigintval($_POST['wds66_id']), $GLOBALS['userid']), __FILE__, __LINE__);
273
274                                         // All done!
275                                         LOAD_TEMPLATE("admin_settings_saved", false, WERNIS_MEMBER_PAYOUT_DONE);
276                                         return;
277                                 } 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")) {
278                                         // Wrong login data
279                                         LOAD_TEMPLATE("admin_settings_saved", false, GET_WERNIS_ERROR_MESSAGE());
280                                         OUTPUT_HTML("<br />");
281                                 } else {
282                                         // Something went wrong
283                                         LOAD_TEMPLATE("admin_settings_saved", false, sprintf(WERNIS_MEMBER_WITHDRAW_FAILED, GET_WERNIS_ERROR_MESSAGE()));
284                                         OUTPUT_HTML("<br />");
285                                 }
286                         }
287                         break;
288
289                 default: // Invalid mode!
290                         LOAD_TEMPLATE("admin_settings_saved", false, sprintf(WERNIS_MEMBER_MODE_INVALID, SQL_ESCAPE($_GET['mode'])));
291                         return;
292         }
293 }
294
295 // Prepare mode for template name
296 $mode = sprintf("member_wernis_mode_%s", SQL_ESCAPE($_GET['mode']));
297
298 // Load the template
299 LOAD_TEMPLATE($mode, false, $content);
300
301 //
302 ?>