The yearly copyright-update commit. 2009, 2010 are now copyrighted on the developer...
[mailer.git] / inc / modules / member / what-wernis.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                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  * Copyright (c) 2009, 2010 by Mailer Developer Team                    *
22  * For more information visit: http://www.mxchange.org                  *
23  *                                                                      *
24  * This program is free software; you can redistribute it and/or modify *
25  * it under the terms of the GNU General Public License as published by *
26  * the Free Software Foundation; either version 2 of the License, or    *
27  * (at your option) any later version.                                  *
28  *                                                                      *
29  * This program is distributed in the hope that it will be useful,      *
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
32  * GNU General Public License for more details.                         *
33  *                                                                      *
34  * You should have received a copy of the GNU General Public License    *
35  * along with this program; if not, write to the Free Software          *
36  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
37  * MA  02110-1301  USA                                                  *
38  ************************************************************************/
39
40 // Some security stuff...
41 if (!defined('__SECURITY')) {
42         die();
43 } elseif (!isMember()) {
44         // User is not logged in
45         redirectToIndexMemberOnlyModule();
46 }
47
48 // Add description as navigation point
49 addMenuDescription('member', __FILE__);
50
51 if ((!isExtensionActive('wernis')) && (!isAdmin())) {
52         loadTemplate('admin_settings_saved', false, generateExtensionInactiveNotInstalledMessage('wernis'));
53         return;
54 } // END - if
55
56 // Check if the admin has entered the data
57 if ((getConfig('wernis_api_id') == '') || (getConfig('wernis_api_md5') == '')) {
58         // Something important is missing...
59         loadTemplate('admin_settings_saved', false, getMessage('MEMBER_WERNIS_API_DATA_MISSING'));
60         return;
61 } // END - if
62
63 // Init the content array and points
64 $content = array(); $points = false;
65
66 // Is the mode set (withdraw or payout)
67 if ((!isGetRequestParameterSet('mode')) || (getRequestParameter('mode') == 'choose')) {
68         // Let the user choose what he wants to do
69         $content['refid']    = getConfig(('wernis_refid'));
70         $content['wernis_userid'] = '0';
71
72         // Is there an id?
73         if ((!empty($content['wernis_userid'])) && (!isGetRequestParameterSet('mode'))) {
74                 // Then use an other "mode"
75                 setGetRequestParameter('mode', 'list');
76
77                 // And load all rows!
78                 $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",
79                         array(getMemberId()), __FILE__, __LINE__);
80
81                 // Load all rows
82                 $content['rows'] = ''; $SW = 2;
83                 while ($data = SQL_FETCHARRAY($result)) {
84                         // Prepare data for output
85                         $rowContent = array(
86                                 'wernis_timestamp' => generateDateTime($data['wernis_timestamp'], 2),
87                                 'points'           => translateComma($data['wernis_amount']),
88                                 'wernis_account'   => bigintval($data['wernis_account']),
89                                 'status'           => WERNIS_TRANSFER_STATUS($data['wernis_type']),
90                                 'raw_type'         => strtolower($data['wernis_type']),
91                                 'sw'               => $SW,
92                         );
93
94                         // Load row template
95                         $content['rows'] .= loadTemplate('member_wernis_mode_list_row', true, $rowContent);
96                         $SW = 3 - $SW;
97                 }
98
99                 // Free result
100                 SQL_FREERESULT($result);
101         } else {
102                 // Default links are not active!
103                 $content['payout_link']   = '<em>{--MEMBER_WERNIS_PAYOUT_DISABLED--}</em>';
104                 $content['withdraw_link'] = '<em>{--MEMBER_WERNIS_WITHDRAW_DISABLED--}</em>';
105
106                 // Is the payout mode active?
107                 if (getConfig('wernis_payout_active') == 'Y') {
108                         // Add link
109                         $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>";
110                 } // END - if
111
112                 // Is the withdraw mode active?
113                 if (getConfig('wernis_withdraw_active') == 'Y') {
114                         // Add link
115                         $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>";
116                 } // END - if
117
118                 // Mode chooser! ;-)
119                 setGetRequestParameter('mode', 'choose');
120         }
121 } elseif ((getRequestParameter('mode') == 'payout') && (getConfig('wernis_payout_active') == 'Y')) {
122         // Get total points and check if the user can request a payout
123         $points = countSumTotalData(getMemberId(), 'user_points', 'points') - countSumTotalData(getMemberId(), 'user_data', 'used_points');
124
125         // Remove the registration fee
126         $points = $points - getConfig('points_register');
127
128         // Is there a percentage or fixed fee?
129         $points = WERNIS_TAKE_FEE($points, 'payout');
130
131         // Is this enougth for a payout?
132         if ($points < getConfig('wernis_min_payout')) {
133                 // No, then abort here
134                 loadTemplate('admin_settings_saved', false, sprintf(
135                         getMessage('MEMBER_WERNIS_MIN_PAYOUT'),
136                         translateComma(getConfig('wernis_min_payout')),
137                         translateComma($points)
138                 ));
139                 return;
140         } // END - if
141
142         // No dots here...
143         $points = explode('.', $points);
144         $points = bigintval($points[0]);
145
146         // Add points to content array
147         $content['points']     = translateComma($points);
148
149         // Get WDS66 username
150         $content['wernis_userid'] = getUserData('wernis_userid');
151 } elseif ((getRequestParameter('mode') == 'withdraw') && (getConfig('wernis_withdraw_active') == 'Y')) {
152         // Get total points for just displaying them
153         $points = countSumTotalData(getMemberId(), 'user_points', 'points') - countSumTotalData(getMemberId(), 'user_data', 'used_points');
154
155         // Prepare data for the template
156         $content['points']        = translateComma($points);
157         $content['wernis_userid'] = '';
158
159         // Add fees to array
160         WERNIS_ADD_FEES_TO_ARRAY($content);
161
162         // Fetch id
163         $content['wernis_userid'] = getUserData('wernis_userid');
164 } else {
165         // Invalid mode!
166         loadTemplate('admin_settings_saved', false, getMaskedMessage('MEMBER_WERNIS_MODE_INVALID', getRequestParameter('mode')));
167         return;
168 }
169
170 // Is the formular sent?
171 if ((isFormSent()) && (isGetRequestParameterSet('mode'))) {
172         // Is the user id and password set?
173         if (!isPostRequestParameterSet(('wernis_userid'))) {
174                 // Nothing entered in WDS66 user id
175                 loadTemplate('admin_settings_saved', false, getMessage('MEMBER_WERNIS_EMPTY_USERNAME'));
176         } elseif (!isPostRequestParameterSet(('wds66_password'))) {
177                 // Nothing entered in WDS66 password
178                 loadTemplate('admin_settings_saved', false, getMessage('MEMBER_WERNIS_EMPTY_PASSWORD'));
179         } elseif (!isPostRequestParameterSet(('amount'))) {
180                 // Nothing entered in amount
181                 loadTemplate('admin_settings_saved', false, getMessage('MEMBER_WERNIS_EMPTY_AMOUNT'));
182         } elseif (postRequestParameter('wernis_userid') != bigintval(postRequestParameter('wernis_userid'))) {
183                 // Only numbers in account id!
184                 loadTemplate('admin_settings_saved', false, getMessage('MEMBER_WERNIS_INVALID_USERNAME'));
185         } elseif (postRequestParameter('amount') != bigintval(postRequestParameter('amount'))) {
186                 // Only numbers in amount!
187                 loadTemplate('admin_settings_saved', false, getMessage('MEMBER_WERNIS_INVALID_AMOUNT'));
188         } else {
189                 // Check input data depending on the mode and execute the requested mode
190                 switch (getRequestParameter('mode')) {
191                         case 'withdraw': // Widthdraws WDS66 -> This exchange
192                                 if (postRequestParameter('amount') < getConfig('wernis_min_withdraw')) {
193                                         // Not enougth entered!
194                                         loadTemplate('admin_settings_saved', false, getMessage('MEMBER_WERNIS_WITHDRAW_AMOUNT_SMALLER_MIN'));
195                                 } else {
196                                         // All is fine here so do the withdraw
197                                         $success = WERNIS_EXECUTE_WITHDRAW(postRequestParameter('wernis_userid'), md5(postRequestParameter('wds66_password')), postRequestParameter('amount'));
198                                         if ($success === true) {
199                                                 // Add it to this amount
200                                                 addPointsDirectly('wernis_withdraw', getMemberId(), bigintval(postRequestParameter('amount')));
201
202                                                 // Update the user data as well..
203                                                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `wernis_userid`=%s WHERE `userid`=%s LIMIT 1",
204                                                         array(bigintval(postRequestParameter('wernis_userid')), getMemberId()), __FILE__, __LINE__);
205
206                                                 // All done!
207                                                 loadTemplate('admin_settings_saved', false, getMessage('MEMBER_WERNIS_WITHDRAW_DONE'));
208                                                 return;
209                                         } elseif ((GET_WERNIS_ERROR_CODE() == 'user_failed') || (GET_WERNIS_ERROR_CODE() == 'own_failed') || (GET_WERNIS_ERROR_CODE() == 'amount_failed')) {
210                                                 // Wrong login data
211                                                 loadTemplate('admin_settings_saved', false, GET_WERNIS_ERROR_MESSAGE());
212                                         } else {
213                                                 // Something went wrong
214                                                 loadTemplate('admin_settings_saved', false, getMaskedMessage('MEMBER_WERNIS_WITHDRAW_FAILED', GET_WERNIS_ERROR_MESSAGE()));
215                                         }
216                                 }
217                                 break;
218
219                         case 'payout': // Payout this exchange -> WDS66
220                                 if (postRequestParameter('amount') < getConfig('wernis_min_payout')) {
221                                         // Not enougth entered!
222                                         loadTemplate('admin_settings_saved', false, getMessage('MEMBER_WERNIS_PAYOUT_AMOUNT_SMALLER_MIN'));
223                                 } elseif (postRequestParameter('amount') > $points) {
224                                         // Not enougth points left!
225                                         loadTemplate('admin_settings_saved', false, sprintf(getMessage('MEMBER_WERNIS_PAYOUT_POINTS_DEPLETED'), bigintval(postRequestParameter('amount')), bigintval($points)));
226                                 } else {
227                                         // All is fine here so do the withdraw
228                                         $success = WERNIS_EXECUTE_PAYOUT(postRequestParameter('wernis_userid'), postRequestParameter('amount'));
229                                         if ($success === true) {
230                                                 // Sub points
231                                                 subtractPoints('wernis_payout', getMemberId(), postRequestParameter('amount'));
232
233                                                 // Update WDS66 id
234                                                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `wernis_userid`=%s WHERE `userid`=%s LIMIT 1",
235                                                         array(bigintval(postRequestParameter('wernis_userid')), getMemberId()), __FILE__, __LINE__);
236
237                                                 // All done!
238                                                 loadTemplate('admin_settings_saved', false, getMessage('MEMBER_WERNIS_PAYOUT_DONE'));
239                                                 return;
240                                         } 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')) {
241                                                 // Wrong login data
242                                                 loadTemplate('admin_settings_saved', false, GET_WERNIS_ERROR_MESSAGE());
243                                         } else {
244                                                 // Something went wrong
245                                                 loadTemplate('admin_settings_saved', false, getMaskedMessage('MEMBER_WERNIS_WITHDRAW_FAILED', GET_WERNIS_ERROR_MESSAGE()));
246                                         }
247                                 }
248                                 break;
249
250                         default: // Invalid mode!
251                                 logDebugMessage(__FILE__, __LINE__, sprintf("Invalid mode %s detected.", getRequestParameter('mode')));
252                                 loadTemplate('admin_settings_saved', false, getMaskedMessage('MEMBER_WERNIS_MODE_INVALID', getRequestParameter('mode')));
253                                 return;
254                 }
255         }
256 } // END - if
257
258 // Prepare mode for template name
259 $mode = sprintf("member_wernis_mode_%s", getRequestParameter('mode'));
260
261 // Load the template
262 loadTemplate($mode, false, $content);
263
264 // [EOF]
265 ?>