A lot has been rewritten, ext-teams added, ext-forced continued:
[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 - 2011 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         displayMessage('{%pipe,generateExtensionInactiveNotInstalledMessage=wernis%}');
51         return;
52 } // END - if
53
54 // Check if the admin has entered the data
55 if ((getWernisApiId() == '') || (getWernisApiMd5() == '')) {
56         // Something important is missing...
57         displayMessage('{--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 (isWernisWithdrawActive()) {
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                 displayMessage('{%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') && (isWernisWithdrawActive())) {
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         displayMessage('{%message,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                 displayMessage('{--MEMBER_WERNIS_EMPTY_USERNAME--}');
154         } elseif (!isPostRequestParameterSet('wds66_password')) {
155                 // Nothing entered in WDS66 password
156                 displayMessage('{--MEMBER_WERNIS_EMPTY_PASSWORD--}');
157         } elseif (!isPostRequestParameterSet('amount')) {
158                 // Nothing entered in amount
159                 displayMessage('{--MEMBER_WERNIS_EMPTY_AMOUNT--}');
160         } elseif (postRequestParameter('wernis_userid') != bigintval(postRequestParameter('wernis_userid'))) {
161                 // Only numbers in account id!
162                 displayMessage('{--MEMBER_WERNIS_INVALID_USERNAME');
163         } elseif (postRequestParameter('amount') != bigintval(postRequestParameter('amount'))) {
164                 // Only numbers in amount!
165                 displayMessage('{--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                                         displayMessage('{--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                                                 initReferalSystem();
179                                                 addPointsThroughReferalSystem('wernis_withdraw', getMemberId(), bigintval(postRequestParameter('amount')));
180
181                                                 // Update the user data as well..
182                                                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `wernis_userid`=%s WHERE `userid`=%s LIMIT 1",
183                                                         array(bigintval(postRequestParameter('wernis_userid')), getMemberId()), __FILE__, __LINE__);
184
185                                                 // All done!
186                                                 displayMessage('{--MEMBER_WERNIS_WITHDRAW_DONE--}');
187                                                 return;
188                                         } elseif ((GET_WERNIS_ERROR_CODE() == 'user_failed') || (GET_WERNIS_ERROR_CODE() == 'own_failed') || (GET_WERNIS_ERROR_CODE() == 'amount_failed')) {
189                                                 // Wrong login data
190                                                 displayMessage(GET_WERNIS_ERROR_MESSAGE());
191                                         } else {
192                                                 // Something went wrong
193                                                 displayMessage('{--MEMBER_WERNIS_WITHDRAW_FAILED--}');
194                                         }
195                                 }
196                                 break;
197
198                         case 'payout': // Payout this exchange -> WDS66
199                                 if (postRequestParameter('amount') < getConfig('wernis_min_payout')) {
200                                         // Not enougth entered!
201                                         displayMessage('{--MEMBER_WERNIS_PAYOUT_AMOUNT_SMALLER_MIN--}');
202                                 } elseif (postRequestParameter('amount') > $points) {
203                                         // Not enougth points left!
204                                         displayMessage(sprintf(getMessage('MEMBER_WERNIS_PAYOUT_POINTS_DEPLETED'), bigintval(postRequestParameter('amount')), bigintval($points)));
205                                 } else {
206                                         // All is fine here so do the withdraw
207                                         $success = WERNIS_EXECUTE_PAYOUT(postRequestParameter('wernis_userid'), postRequestParameter('amount'));
208                                         if ($success === true) {
209                                                 // Sub points and ignore return status
210                                                 subtractPoints('wernis_payout', getMemberId(), postRequestParameter('amount'));
211
212                                                 // Update WDS66 id
213                                                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `wernis_userid`=%s WHERE `userid`=%s LIMIT 1",
214                                                         array(bigintval(postRequestParameter('wernis_userid')), getMemberId()), __FILE__, __LINE__);
215
216                                                 // All done!
217                                                 displayMessage('{--MEMBER_WERNIS_PAYOUT_DONE--}');
218                                                 return;
219                                         } 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')) {
220                                                 // Wrong login data
221                                                 displayMessage(GET_WERNIS_ERROR_MESSAGE());
222                                         } else {
223                                                 // Something went wrong
224                                                 displayMessage('{--MEMBER_WERNIS_PAYOUT_FAILED--}');
225                                         }
226                                 }
227                                 break;
228
229                         default: // Invalid mode!
230                                 logDebugMessage(__FILE__, __LINE__, sprintf("Invalid mode %s detected.", getRequestParameter('mode')));
231                                 displayMessage('{%message,MEMBER_WERNIS_MODE_INVALID=' . getRequestParameter('mode') . '%}');
232                                 return;
233                 } // END - switch
234         }
235 } // END - if
236
237 // Prepare mode for template name
238 $mode = sprintf("member_wernis_mode_%s", getRequestParameter('mode'));
239
240 // Load the template
241 loadTemplate($mode, false, $content);
242
243 // [EOF]
244 ?>