Some more wrappers added, code cleanups:
[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, '{--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['wernis_userid'] = '0';
70
71         // Is there an id?
72         if ((!empty($content['wernis_userid'])) && (!isGetRequestParameterSet('mode'))) {
73                 // Then use an other "mode"
74                 setGetRequestParameter('mode', 'list');
75
76                 // And load all rows!
77                 $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",
78                         array(getMemberId()), __FILE__, __LINE__);
79
80                 // Load all rows
81                 $content['rows'] = '';
82                 while ($data = SQL_FETCHARRAY($result)) {
83                         // Prepare data for output
84                         $rowContent = array(
85                                 'wernis_timestamp' => generateDateTime($data['wernis_timestamp'], 2),
86                                 'wernis_amount'    => $data['wernis_amount'],
87                                 'wernis_account'   => $data['wernis_account'],
88                                 'wernis_type'      => $data['wernis_type'],
89                         );
90
91                         // Load row template
92                         $content['rows'] .= loadTemplate('member_wernis_mode_list_row', true, $rowContent);
93                 } // END - while
94
95                 // Free result
96                 SQL_FREERESULT($result);
97         } else {
98                 // Default links are not active!
99                 $content['payout_link']   = '<em>{--MEMBER_WERNIS_PAYOUT_DISABLED--}</em>';
100                 $content['withdraw_link'] = '<em>{--MEMBER_WERNIS_WITHDRAW_DISABLED--}</em>';
101
102                 // Is the payout mode active?
103                 if (getConfig('wernis_payout_active') == 'Y') {
104                         // Add link
105                         $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>';
106                 } // END - if
107
108                 // Is the withdraw mode active?
109                 if (getConfig('wernis_withdraw_active') == 'Y') {
110                         // Add link
111                         $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>';
112                 } // END - if
113
114                 // Mode chooser! ;-)
115                 setGetRequestParameter('mode', 'choose');
116         }
117 } elseif ((getRequestParameter('mode') == 'payout') && (getConfig('wernis_payout_active') == 'Y')) {
118         // Get user's total points and remove the registration fee
119         $points = getTotalPoints(getMemberId()) - getPointsRegister();
120
121         // Is there a percentage or fixed fee?
122         $points = WERNIS_TAKE_FEE($points, 'payout');
123
124         // Is this enougth for a payout?
125         if ($points < getConfig('wernis_min_payout')) {
126                 // No, then abort here
127                 loadTemplate('admin_settings_saved', false, sprintf(
128                         getMessage('MEMBER_WERNIS_MIN_PAYOUT'),
129                         translateComma(getConfig('wernis_min_payout')),
130                         translateComma($points)
131                 ));
132                 return;
133         } // END - if
134
135         // No dots here...
136         $points = explode('.', $points);
137         $points = bigintval($points[0]);
138
139         // Add points to content array
140         $content['points']     = $points;
141
142         // Get WDS66 username
143         $content['wernis_userid'] = getUserData('wernis_userid');
144 } elseif ((getRequestParameter('mode') == 'withdraw') && (getConfig('wernis_withdraw_active') == 'Y')) {
145         // Prepare data for the template
146         $content['points']        = getTotalPoints(getMemberId());
147         $content['wernis_userid'] = '';
148
149         // Fetch id
150         $content['wernis_userid'] = getUserData('wernis_userid');
151 } else {
152         // Invalid mode!
153         loadTemplate('admin_settings_saved', false, getMaskedMessage('MEMBER_WERNIS_MODE_INVALID', getRequestParameter('mode')));
154         return;
155 }
156
157 // Is the formular sent?
158 if ((isFormSent()) && (isGetRequestParameterSet('mode'))) {
159         // Is the user id and password set?
160         if (!isPostRequestParameterSet('wernis_userid')) {
161                 // Nothing entered in WDS66 user id
162                 loadTemplate('admin_settings_saved', false, '{--MEMBER_WERNIS_EMPTY_USERNAME--}');
163         } elseif (!isPostRequestParameterSet('wds66_password')) {
164                 // Nothing entered in WDS66 password
165                 loadTemplate('admin_settings_saved', false, '{--MEMBER_WERNIS_EMPTY_PASSWORD--}');
166         } elseif (!isPostRequestParameterSet('amount')) {
167                 // Nothing entered in amount
168                 loadTemplate('admin_settings_saved', false, '{--MEMBER_WERNIS_EMPTY_AMOUNT--}');
169         } elseif (postRequestParameter('wernis_userid') != bigintval(postRequestParameter('wernis_userid'))) {
170                 // Only numbers in account id!
171                 loadTemplate('admin_settings_saved', false, '{--MEMBER_WERNIS_INVALID_USERNAME');
172         } elseif (postRequestParameter('amount') != bigintval(postRequestParameter('amount'))) {
173                 // Only numbers in amount!
174                 loadTemplate('admin_settings_saved', false, '{--MEMBER_WERNIS_INVALID_AMOUNT--}');
175         } else {
176                 // Check input data depending on the mode and execute the requested mode
177                 switch (getRequestParameter('mode')) {
178                         case 'withdraw': // Widthdraws WDS66 -> This exchange
179                                 if (postRequestParameter('amount') < getConfig('wernis_min_withdraw')) {
180                                         // Not enougth entered!
181                                         loadTemplate('admin_settings_saved', false, '{--MEMBER_WERNIS_WITHDRAW_AMOUNT_SMALLER_MIN--}');
182                                 } else {
183                                         // All is fine here so do the withdraw
184                                         $success = WERNIS_EXECUTE_WITHDRAW(postRequestParameter('wernis_userid'), md5(postRequestParameter('wds66_password')), postRequestParameter('amount'));
185                                         if ($success === true) {
186                                                 // Add it to this amount
187                                                 addPointsDirectly('wernis_withdraw', getMemberId(), bigintval(postRequestParameter('amount')));
188
189                                                 // Update the user data as well..
190                                                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `wernis_userid`=%s WHERE `userid`=%s LIMIT 1",
191                                                         array(bigintval(postRequestParameter('wernis_userid')), getMemberId()), __FILE__, __LINE__);
192
193                                                 // All done!
194                                                 loadTemplate('admin_settings_saved', false, '{--MEMBER_WERNIS_WITHDRAW_DONE--}');
195                                                 return;
196                                         } elseif ((GET_WERNIS_ERROR_CODE() == 'user_failed') || (GET_WERNIS_ERROR_CODE() == 'own_failed') || (GET_WERNIS_ERROR_CODE() == 'amount_failed')) {
197                                                 // Wrong login data
198                                                 loadTemplate('admin_settings_saved', false, GET_WERNIS_ERROR_MESSAGE());
199                                         } else {
200                                                 // Something went wrong
201                                                 loadTemplate('admin_settings_saved', false, getMaskedMessage('MEMBER_WERNIS_WITHDRAW_FAILED', GET_WERNIS_ERROR_MESSAGE()));
202                                         }
203                                 }
204                                 break;
205
206                         case 'payout': // Payout this exchange -> WDS66
207                                 if (postRequestParameter('amount') < getConfig('wernis_min_payout')) {
208                                         // Not enougth entered!
209                                         loadTemplate('admin_settings_saved', false, '{--MEMBER_WERNIS_PAYOUT_AMOUNT_SMALLER_MIN--}');
210                                 } elseif (postRequestParameter('amount') > $points) {
211                                         // Not enougth points left!
212                                         loadTemplate('admin_settings_saved', false, sprintf(getMessage('MEMBER_WERNIS_PAYOUT_POINTS_DEPLETED'), bigintval(postRequestParameter('amount')), bigintval($points)));
213                                 } else {
214                                         // All is fine here so do the withdraw
215                                         $success = WERNIS_EXECUTE_PAYOUT(postRequestParameter('wernis_userid'), postRequestParameter('amount'));
216                                         if ($success === true) {
217                                                 // Sub points
218                                                 subtractPoints('wernis_payout', getMemberId(), postRequestParameter('amount'));
219
220                                                 // Update WDS66 id
221                                                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `wernis_userid`=%s WHERE `userid`=%s LIMIT 1",
222                                                         array(bigintval(postRequestParameter('wernis_userid')), getMemberId()), __FILE__, __LINE__);
223
224                                                 // All done!
225                                                 loadTemplate('admin_settings_saved', false, '{--MEMBER_WERNIS_PAYOUT_DONE--}');
226                                                 return;
227                                         } 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')) {
228                                                 // Wrong login data
229                                                 loadTemplate('admin_settings_saved', false, GET_WERNIS_ERROR_MESSAGE());
230                                         } else {
231                                                 // Something went wrong
232                                                 loadTemplate('admin_settings_saved', false, getMaskedMessage('MEMBER_WERNIS_WITHDRAW_FAILED', GET_WERNIS_ERROR_MESSAGE()));
233                                         }
234                                 }
235                                 break;
236
237                         default: // Invalid mode!
238                                 logDebugMessage(__FILE__, __LINE__, sprintf("Invalid mode %s detected.", getRequestParameter('mode')));
239                                 loadTemplate('admin_settings_saved', false, getMaskedMessage('MEMBER_WERNIS_MODE_INVALID', getRequestParameter('mode')));
240                                 return;
241                 } // END - switch
242         }
243 } // END - if
244
245 // Prepare mode for template name
246 $mode = sprintf("member_wernis_mode_%s", getRequestParameter('mode'));
247
248 // Load the template
249 loadTemplate($mode, false, $content);
250
251 // [EOF]
252 ?>