2 /************************************************************************
3 * Mailer v0.2.1-FINAL Start: 10/19/2003 *
4 * =================== Last change: 08/12/2004 *
6 * -------------------------------------------------------------------- *
7 * File : what-points.php *
8 * -------------------------------------------------------------------- *
9 * Short description : All your collected points... *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Alle Ihrer gesammelten Punkte *
12 * -------------------------------------------------------------------- *
15 * $Tag:: 0.2.1-FINAL $ *
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 *
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. *
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. *
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, *
38 ************************************************************************/
40 // Some security stuff...
41 if (!defined('__SECURITY')) {
45 // Sets a status message and code
46 function WERNIS_STATUS_MESSAGE ($message, $status) {
47 $GLOBALS['wernis_data']['message'] = $message;
48 $GLOBALS['wernis_data']['status'] = $status;
51 // Get the status message
52 function GET_WERNIS_ERROR_MESSAGE () {
53 if (isset($GLOBALS['wernis_data']['message'])) {
55 return $GLOBALS['wernis_data']['message'];
56 } elseif (isset($GLOBALS['wernis_data']['status'])) {
57 // Fall-back to status
58 return getMaskedMessage('WERNIS_ERROR_STATUS', $GLOBALS['wernis_data']['status']);
60 // Something bad happend
61 return '{--WERNIS_UNKNOWN_ERROR--}';
65 // Get the status code
66 function GET_WERNIS_ERROR_CODE () {
67 if (isset($GLOBALS['wernis_data']['status'])) {
69 return $GLOBALS['wernis_data']['status'];
71 // Something bad happend
72 return '{--WERNIS_UNKNOWN_ERROR--}';
76 // Sends out a request to the API and returns it's result
77 function WERNIS_SEND_REQUEST ($scriptName, $requestData = array()) {
78 // Is the requestData an array?
79 if (!is_array($requestData)) {
82 'status' => 'failed_general',
83 'message' => '{--WERNIS_API_REQUEST_DATA_INVALID--}'
87 // Is the API id and MD5 hash there?
88 if ((getConfig('wernis_api_id') == '') || (getConfig('wernis_api_md5') == '')) {
91 'status' => 'failed_general',
92 'message' => '{--WERNIS_API_REQUEST_DATA_MISSING--}'
96 // Add more request data
97 $requestData['api_id'] = getConfig('wernis_api_id');
98 $requestData['api_key'] = getConfig('wernis_api_md5');
100 // Is a purpose there?
101 if (isset($requestData['purpose'])) {
103 eval('$purpose = "' . doFinalCompilation($requestData['purpose'], false) . '";');
105 // Prepare the purpose, it needs encoding
106 $requestData['purpose'] = encodeString($purpose);
109 // Construct the request string
110 $requestString = getConfig('wernis_api_url') . $scriptName;
112 // Get the raw response from the lower function
113 $response = sendPostRequest($requestString, $requestData);
115 // Check the response header if all is fine
116 if (strpos($response[0], '200') === false) {
117 // Something bad happend... :(
119 'status' => 'request_error',
120 'message' => getMaskedMessage('WERNIS_API_REQUEST_ERROR', $response[0])
124 // All (maybe) fine so remove the response header from server
125 $responseLine = '*INVALID*';
126 for ($idx = (count($response) - 1); $idx > 1; $idx--) {
127 $line = trim($response[$idx]);
129 $responseLine = $line;
134 // Is the response leaded by a & symbol?
135 if (substr($responseLine, 0, 1) != '&') {
136 // Something badly happened on server-side
138 'status' => 'request_problem',
139 'message' => sprintf(getMessage('WERNIS_API_REQUEST_PROBLEM'), $response[0], secureString($responseLine))
143 // Remove the leading & (which can be used in Flash)
144 $responseLine = substr($responseLine, 1);
146 // Bring back the response
147 $data = explode('=', $responseLine);
149 // Default return array (should not stay empty)
152 // We use only the first two entries (which shall be fine)
153 if ($data[0] === 'error') {
154 // The request has failed... :(
156 case '404': // Invalid API id
157 case 'AUTH': // Authorization has failed
159 'status' => 'auth_failed',
160 'message' => '{--WERNIS_API_REQUEST_FAILED_AUTH--}'
164 case 'LOCKED': // User account is locked!
165 case 'PASS': // Bad passphrase entered
166 case 'USER': // Missing account or invalid password
168 'status' => 'user_failed',
169 'message' => '{--WERNIS_API_REQUEST_FAILED_USER--}'
173 case 'OWN': // Transfer to own account
175 'status' => 'own_failed',
176 'message' => '{--WERNIS_API_REQUEST_FAILED_OWN--}'
180 case 'AMOUNT': // Amount is depleted
182 'status' => 'amount_failed',
183 'message' => '{--WERNIS_API_REQUEST_FAILED_AMOUNT--}'
187 case 'AMOUNT-SEND': // API amount is depleted
189 'status' => 'api_amount_failed',
190 'message' => '{--WERNIS_API_REQUEST_FAILED_API_AMOUNT--}'
194 default: // Unknown error (maybe new?)
195 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Unknown error %s from WDS66 API received.", $data[1]));
197 'status' => 'request_failed',
198 'message' => getMaskedMessage('WERNIS_API_REQUEST_FAILED', $data[1])
206 'response' => $responseLine
214 // Tests the function by calling balance.php on the API
215 function WERNIS_TEST_API () {
216 // Result is always failed
219 // Return the result from the lower functions
220 $return = WERNIS_SEND_REQUEST('balance.php');
222 // Did it went smoothly?
223 if ($return['status'] == 'OK') {
227 // Status failture text
228 WERNIS_STATUS_MESSAGE($return['message'], $return['status']);
235 // Widthdraw this amount
236 function WERNIS_EXECUTE_WITHDRAW ($wdsId, $userMd5, $amount) {
237 // Is the sponsor extension installed?
238 if (getConfig('wernis_withdraw_active') != 'Y') {
239 if (!isExtensionActive('sponsor')) {
242 } elseif (!isSponsor()) {
243 // No sponsor, not allowed to withdraw!
248 // Default is failed attempt
251 // Prepare the request data
252 $requestData = array(
253 'sub_request' => 'receive',
254 't_uid' => bigintval($wdsId),
256 'r_uid' => getConfig('wernis_refid'),
257 'amount' => bigintval($amount),
258 'purpose' => getMaskedMessage('WERNIS_API_PURPOSE_WITHDRAW', getMemberId())
261 // Return the result from the lower functions
262 $return = WERNIS_SEND_REQUEST('book.php', $requestData);
264 if ($return['status'] == 'OK') {
269 WERNIS_LOG_TRANSFER($wdsId, $amount, 'IN');
271 // Status failture text
272 WERNIS_STATUS_MESSAGE($return['message'], $return['status']);
275 WERNIS_LOG_TRANSFER($wdsId, $amount, 'FAILED', $return['message'], $return['status']);
283 // Payout this amount
284 function WERNIS_EXECUTE_PAYOUT ($wdsId, $amount) {
285 // Default is failed attempt
288 // Prepare the request data
289 $requestData = array(
290 'sub_request' => 'send',
291 't_uid' => getConfig('wernis_refid'),
292 't_md5' => getConfig('wernis_pass_md5'),
293 'r_uid' => bigintval($wdsId),
294 'amount' => bigintval($amount),
295 'purpose' => getMaskedMessage('WERNIS_API_PURPOSE_PAYOUT', getMemberId())
298 // Return the result from the lower functions
299 $return = WERNIS_SEND_REQUEST('book.php', $requestData);
301 if ($return['status'] == 'OK') {
306 WERNIS_LOG_TRANSFER($wdsId, $amount, 'OUT');
308 // Status failture text
309 WERNIS_STATUS_MESSAGE($return['message'], $return['status']);
312 WERNIS_LOG_TRANSFER($wdsId, $amount, 'FAILED', $return['message'], $return['status']);
319 // Translate the status IN/OUT
320 function translateWernisTransferStatus ($status) {
322 $return = getMaskedMessage('WERNIS_STATUS_UNKNWOWN', $status);
326 case 'IN': // Withdraw
327 $return = '{--WERNIS_STATUS_WITHDRAW--}';
330 case 'OUT': // Payout
331 $return = '{--WERNIS_STATUS_PAYOUT--}';
334 case 'FAILED': // Payout
335 $return = '{--WERNIS_STATUS_FAILED--}';
344 function WERNIS_LOG_TRANSFER ($wdsId, $amount, $type = 'FAILED', $message = '', $status = '') {
345 // Register this wernis movement
346 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_wernis` (`userid`,`wernis_account`,`wernis_amount`,`wernis_timestamp`,`wernis_type`,`wernis_api_message`,`wernis_api_status`) VALUES (%d, %d, %d, UNIX_TIMESTAMP(), '%s', '%s', '%s')",
347 array(getMemberId(), bigintval($wdsId), bigintval($amount), $type, $message, $status), __FUNCTION__, __LINE__);
350 // Take fees and factor
351 function WERNIS_TAKE_FEE ($points, $mode) {
352 // Payout or withdraw are allowed modes!
353 //* DEBUG: */ debugOutput('mode='.$mode.',points='.$points);
354 if (!in_array($mode, array('payout', 'withdraw'))) {
355 // Log error and abort
356 logDebugMessage(__FUNCTION__, __LINE__, "userid={getMemberId()},mode={$mode},points={$points}");
360 // Is there a percentage or fixed fee?
361 if (getConfig('wernis_'.$mode.'_fee_percent') > 0) {
363 $points -= $points * getConfig('wernis_'.$mode.'_fee_percent') / 100;
364 } elseif (getConfig('wernis_'.$mode.'_fee_fix') > 0) {
366 $points -= getConfig('wernis_'.$mode.'_fee_fix');
369 // Divide/multiply the factor
370 if ($mode == 'payout') {
372 $points = $points / getConfig('wernis_payout_factor');
374 // Multiply for withdraw
375 $points = $points * getConfig('wernis_withdraw_factor');
379 //* DEBUG: */ debugOutput('mode='.$mode.',points='.$points);
383 // Add withdraw fees and factor
384 function WERNIS_ADD_WITHDRAW_FEE ($points) {
385 // Is there a percentage or fixed fee?
386 if (getConfig('wernis_withdraw_fee_percent') > 0) {
388 $points += $points * getConfig('wernis_withdraw_fee_percent') / 100;
389 } elseif (getConfig('wernis_withdraw_fee_fix') > 0) {
391 $points += getConfig('wernis_withdraw_fee_fix');