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