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