Wernis API now reconizes failed API responses
[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  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
17  * Needs to be in all Files and every File needs "svn propset           *
18  * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
19  * -------------------------------------------------------------------- *
20  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
21  * For more information visit: http://www.mxchange.org                  *
22  *                                                                      *
23  * This program is free software; you can redistribute it and/or modify *
24  * it under the terms of the GNU General Public License as published by *
25  * the Free Software Foundation; either version 2 of the License, or    *
26  * (at your option) any later version.                                  *
27  *                                                                      *
28  * This program is distributed in the hope that it will be useful,      *
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
31  * GNU General Public License for more details.                         *
32  *                                                                      *
33  * You should have received a copy of the GNU General Public License    *
34  * along with this program; if not, write to the Free Software          *
35  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
36  * MA  02110-1301  USA                                                  *
37  ************************************************************************/
38
39 // Some security stuff...
40 if (!defined('__SECURITY')) {
41         die();
42 } elseif (!isMember()) {
43         // User is not logged in
44         redirectToIndexMemberOnlyModule();
45 }
46
47 // Add description as navigation point
48 addMenuDescription('member', __FILE__);
49
50 if ((!isExtensionActive('wernis')) && (!isAdmin())) {
51         loadTemplate('admin_settings_saved', false, generateExtensionInactiveNotInstalledMessage('wernis'));
52         return;
53 } // END - if
54
55 // Check if the admin has entered the data
56 if ((getConfig('wernis_api_id') == '') || (getConfig('wernis_api_md5') == '')) {
57         // Something important is missing...
58         loadTemplate('admin_settings_saved', false, getMessage('WERNIS_MEMBER_API_DATA_MISSING'));
59         return;
60 } // END - if
61
62 // Init the content array and points
63 $content = array(); $points = false;
64
65 // Is the mode set (withdraw or payout)
66 if ((!isGetRequestElementSet('mode')) || (getRequestElement('mode') == 'choose')) {
67         // Let the user choose what he wants to do
68         $content['refid']    = getConfig(('wernis_refid'));
69         $content['wds66_id'] = 0;
70
71         // Get WDS66 id
72         $result = SQL_QUERY_ESC("SELECT wernis_userid FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `userid`=%s LIMIT 1",
73                 array(getUserId()), __FILE__, __LINE__);
74
75         // Are there some entries?
76         if (SQL_NUMROWS($result) == 1) {
77                 // Fetch ID
78                 list($content['wds66_id']) = SQL_FETCHROW($result);
79         } // END - if
80
81         // Free result
82         SQL_FREERESULT($result);
83
84         // Is there an ID?
85         if ((!empty($content['wds66_id'])) && (!isGetRequestElementSet('mode'))) {
86                 // Then use an other "mode"
87                 setRequestGetElement('mode', 'list');
88
89                 // And load all rows!
90                 $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",
91                         array(getUserId()), __FILE__, __LINE__);
92
93                 // Load all rows
94                 $content['rows'] = ''; $SW = 2;
95                 while ($data = SQL_FETCHARRAY($result)) {
96                         // Prepare data for output
97                         $rowContent = array(
98                                 'stamp'    => generateDateTime($data['wernis_timestamp'], 2),
99                                 'points'   => translateComma($data['wernis_amount']),
100                                 'acc'      => bigintval($data['wernis_account']),
101                                 'status'   => WERNIS_TRANSFER_STATUS($data['wernis_type']),
102                                 'raw_type' => strtolower($data['wernis_type']),
103                                 'sw'       => $SW,
104                         );
105
106                         // Load row template
107                         $content['rows'] .= loadTemplate('member_wernis_mode_list_row', true, $rowContent);
108                         $SW = 3 - $SW;
109                 }
110
111                 // Free result
112                 SQL_FREERESULT($result);
113         } else {
114                 // Default links are not active!
115                 $content['payout_link']   = '<em>{--WERNIS_MEMBER_PAYOUT_DISABLED--}</em>';
116                 $content['withdraw_link'] = '<em>{--WERNIS_MEMBER_WITHDRAW_DISABLED--}</em>';
117
118                 // Is the payout mode active?
119                 if (getConfig('wernis_payout_active') == 'Y') {
120                         // Add link
121                         $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>";
122                 } // END - if
123
124                 // Is the withdraw mode active?
125                 if (getConfig('wernis_withdraw_active') == 'Y') {
126                         // Add link
127                         $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>";
128                 } // END - if
129
130                 // Mode chooser! ;-)
131                 setRequestGetElement('mode', 'choose');
132         }
133 } elseif ((getRequestElement('mode') == 'payout') && (getConfig('wernis_payout_active') == 'Y')) {
134         // Get total points and check if the user can request a payout
135         $points = countSumTotalData(getUserId(), 'user_points', 'points') - countSumTotalData(getUserId(), 'user_data', 'used_points');
136
137         // Remove the registration fee
138         $points = $points - getConfig('points_register');
139
140         // Is there a percentage or fixed fee?
141         $points = WERNIS_TAKE_FEE($points, 'payout');
142
143         // Is this enougth for a payout?
144         if ($points < getConfig('wernis_min_payout')) {
145                 // No, then abort here
146                 loadTemplate('admin_settings_saved', false, sprintf(
147                         getMessage('WERNIS_MEMBER_MIN_PAYOUT'),
148                         translateComma(getConfig('wernis_min_payout')),
149                         translateComma($points)
150                 ));
151                 return;
152         } // END - if
153
154         // No dots here...
155         $points = explode('.', $points);
156         $points = bigintval($points[0]);
157
158         // Add points to content array
159         $content['points']     = translateComma($points);
160         $content['min_points'] = translateComma(getConfig('wernis_min_payout'));
161
162         // Add fees to array
163         WERNIS_ADD_FEES_TO_ARRAY($content);
164
165         // Get WDS66 id
166         $content['wds66_id'] = '';
167         $result = SQL_QUERY_ESC("SELECT `wernis_userid` FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `userid`=%s LIMIT 1",
168                 array(getUserId()), __FILE__, __LINE__);
169
170         // Are there some entries?
171         if (SQL_NUMROWS($result) == 1) {
172                 // Fetch ID
173                 list($content['wds66_id']) = SQL_FETCHROW($result);
174         } // END - if
175
176         // Free result
177         SQL_FREERESULT($result);
178 } elseif ((getRequestElement('mode') == 'withdraw') && (getConfig('wernis_withdraw_active') == 'Y')) {
179         // Get total points for just displaying them
180         $points = countSumTotalData(getUserId(), 'user_points', 'points') - countSumTotalData(getUserId(), 'user_data', 'used_points');
181
182         // Prepare data for the template
183         $content['points']     = translateComma($points);
184         $content['min_points'] = translateComma(getConfig('wernis_min_withdraw'));
185         $content['wds66_id']   = '';
186
187         // Add fees to array
188         WERNIS_ADD_FEES_TO_ARRAY($content);
189
190         // Get WDS66 id
191         $result = SQL_QUERY_ESC("SELECT `wernis_userid` FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `userid`=%s LIMIT 1",
192                 array(getUserId()), __FILE__, __LINE__);
193
194         // Are there some entries?
195         if (SQL_NUMROWS($result) == 1) {
196                 // Fetch ID
197                 list($content['wds66_id']) = SQL_FETCHROW($result);
198         } // END - if
199
200         // Free result
201         SQL_FREERESULT($result);
202 } else {
203         // Invalid mode!
204         loadTemplate('admin_settings_saved', false, sprintf(getMessage('WERNIS_MEMBER_MODE_INVALID'), getRequestElement('mode')));
205         return;
206 }
207
208 // Is the formular sent?
209 if ((isFormSent()) && (isGetRequestElementSet('mode'))) {
210         // Is the user ID and password set?
211         if (!isPostRequestElementSet(('wds66_id'))) {
212                 // Nothing entered in WDS66 user ID
213                 loadTemplate('admin_settings_saved', false, getMessage('WERNIS_MEMBER_EMPTY_USERNAME'));
214         } elseif (!isPostRequestElementSet(('wds66_password'))) {
215                 // Nothing entered in WDS66 password
216                 loadTemplate('admin_settings_saved', false, getMessage('WERNIS_MEMBER_EMPTY_PASSWORD'));
217         } elseif (!isPostRequestElementSet(('amount'))) {
218                 // Nothing entered in amount
219                 loadTemplate('admin_settings_saved', false, getMessage('WERNIS_MEMBER_EMPTY_AMOUNT'));
220         } elseif (postRequestElement('wds66_id') != bigintval(postRequestElement('wds66_id'))) {
221                 // Only numbers in account ID!
222                 loadTemplate('admin_settings_saved', false, getMessage('WERNIS_MEMBER_INVALID_USERNAME'));
223         } elseif (postRequestElement('amount') != bigintval(postRequestElement('amount'))) {
224                 // Only numbers in amount!
225                 loadTemplate('admin_settings_saved', false, getMessage('WERNIS_MEMBER_INVALID_AMOUNT'));
226         } else {
227                 // Check input data depending on the mode and execute the requested mode
228                 switch (getRequestElement('mode')) {
229                         case 'withdraw': // Widthdraws WDS66 -> This exchange
230                                 if (postRequestElement('amount') < getConfig('wernis_min_withdraw')) {
231                                         // Not enougth entered!
232                                         loadTemplate('admin_settings_saved', false, sprintf(getMessage('WERNIS_MEMBER_AMOUNT_SMALLER_MIN'), translateComma(getConfig('wernis_min_withdraw'))));
233                                 } else {
234                                         // All is fine here so do the withdraw
235                                         $success = WERNIS_EXECUTE_WITHDRAW(postRequestElement('wds66_id'), md5(postRequestElement('wds66_password')), postRequestElement('amount'));
236                                         if ($success === true) {
237                                                 // Add it to this amount
238                                                 addPointsDirectly('wernis_withdraw', getUserId(), bigintval(postRequestElement('amount')));
239
240                                                 // Update the user data as well..
241                                                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `wernis_userid`=%s WHERE `userid`=%s LIMIT 1",
242                                                         array(bigintval(postRequestElement('wds66_id')), getUserId()), __FILE__, __LINE__);
243
244                                                 // All done!
245                                                 loadTemplate('admin_settings_saved', false, getMessage('WERNIS_MEMBER_WITHDRAW_DONE'));
246                                                 return;
247                                         } elseif ((GET_WERNIS_ERROR_CODE() == 'user_failed') || (GET_WERNIS_ERROR_CODE() == 'own_failed') || (GET_WERNIS_ERROR_CODE() == 'amount_failed')) {
248                                                 // Wrong login data
249                                                 loadTemplate('admin_settings_saved', false, GET_WERNIS_ERROR_MESSAGE());
250                                         } else {
251                                                 // Something went wrong
252                                                 loadTemplate('admin_settings_saved', false, sprintf(getMessage('WERNIS_MEMBER_WITHDRAW_FAILED'), GET_WERNIS_ERROR_MESSAGE()));
253                                         }
254                                 }
255                                 break;
256
257                         case 'payout': // Payout this exchange -> WDS66
258                                 if (postRequestElement('amount') < getConfig('wernis_min_payout')) {
259                                         // Not enougth entered!
260                                         loadTemplate('admin_settings_saved', false, sprintf(getMessage('WERNIS_MEMBER_AMOUNT_SMALLER_MIN'), translateComma(getConfig('wernis_min_payout'))));
261                                 } elseif (postRequestElement('amount') > $points) {
262                                         // Not enougth points left!
263                                         loadTemplate('admin_settings_saved', false, sprintf(getMessage('WERNIS_MEMBER_PAYOUT_POINTS_DEPLETED'), bigintval(postRequestElement('amount')), bigintval($points)));
264                                 } else {
265                                         // All is fine here so do the withdraw
266                                         $success = WERNIS_EXECUTE_PAYOUT(postRequestElement('wds66_id'), postRequestElement('amount'));
267                                         if ($success === true) {
268                                                 // Sub points
269                                                 subtractPoints('wernis_payout', getUserId(), postRequestElement('amount'));
270
271                                                 // Update WDS66 id
272                                                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `wernis_userid`=%s WHERE `userid`=%s LIMIT 1",
273                                                         array(bigintval(postRequestElement('wds66_id')), getUserId()), __FILE__, __LINE__);
274
275                                                 // All done!
276                                                 loadTemplate('admin_settings_saved', false, getMessage('WERNIS_MEMBER_PAYOUT_DONE'));
277                                                 return;
278                                         } 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')) {
279                                                 // Wrong login data
280                                                 loadTemplate('admin_settings_saved', false, GET_WERNIS_ERROR_MESSAGE());
281                                         } else {
282                                                 // Something went wrong
283                                                 loadTemplate('admin_settings_saved', false, sprintf(getMessage('WERNIS_MEMBER_WITHDRAW_FAILED'), GET_WERNIS_ERROR_MESSAGE()));
284                                         }
285                                 }
286                                 break;
287
288                         default: // Invalid mode!
289                                 logDebugMessage(__FILE__, __LINE__, sprintf("Invalid mode %s detected.", getRequestElement('mode')));
290                                 loadTemplate('admin_settings_saved', false, sprintf(getMessage('WERNIS_MEMBER_MODE_INVALID'), getRequestElement('mode')));
291                                 return;
292                 }
293         }
294 } // END - if
295
296 // Prepare mode for template name
297 $mode = sprintf("member_wernis_mode_%s", getRequestElement('mode'));
298
299 // Load the template
300 loadTemplate($mode, false, $content);
301
302 // [EOF]
303 ?>