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 * -------------------------------------------------------------------- *
18 * Copyright (c) 2003 - 2009 by Roland Haeder *
19 * Copyright (c) 2009 - 2012 by Mailer Developer Team *
20 * For more information visit: http://mxchange.org *
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. *
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. *
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, *
36 ************************************************************************/
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
43 // Sets a status message and code
44 function WERNIS_STATUS_MESSAGE ($message, $status) {
45 $GLOBALS['wernis_data']['message'] = $message;
46 $GLOBALS['wernis_data']['status'] = $status;
49 // Get the status message
50 function GET_WERNIS_ERROR_MESSAGE () {
51 if (isset($GLOBALS['wernis_data']['message'])) {
53 return $GLOBALS['wernis_data']['message'];
54 } elseif (isset($GLOBALS['wernis_data']['status'])) {
55 // Fall-back to status
56 return '{%message,WERNIS_ERROR_STATUS=' . $GLOBALS['wernis_data']['status'] . '%}';
58 // Something bad happend
59 return '{--WERNIS_UNKNOWN_ERROR--}';
63 // Get the status code
64 function GET_WERNIS_ERROR_CODE () {
65 if (isset($GLOBALS['wernis_data']['status'])) {
67 return $GLOBALS['wernis_data']['status'];
69 // Something bad happend
70 return '{--WERNIS_UNKNOWN_ERROR--}';
74 // Sends out a request to the API and returns it's result
75 function WERNIS_SEND_REQUEST ($scriptName, $requestData = array()) {
76 // Is the requestData an array?
77 if (!is_array($requestData)) {
80 'status' => 'failed_general',
81 'message' => '{--WERNIS_API_REQUEST_DATA_INVALID--}'
85 // Is the API id and MD5 hash there?
86 if ((getWernisApiId() == '') || (getWernisApiMd5() == '')) {
89 'status' => 'failed_general',
90 'message' => '{--WERNIS_API_REQUEST_DATA_MISSING--}'
94 // Add more request data
95 $requestData['api_id'] = getWernisApiId();
96 $requestData['api_key'] = getWernisApiMd5();
98 // Is a purpose there?
99 if (isset($requestData['purpose'])) {
101 eval('$purpose = "' . doFinalCompilation($requestData['purpose'], FALSE) . '";');
103 // Prepare the purpose, it needs encoding
104 $requestData['purpose'] = encodeString($purpose);
107 // Construct the request string
108 $requestString = getWernisApiUrl() . $scriptName;
110 // Get the raw response from the lower function
111 $response = sendPostRequest($requestString, $requestData);
113 // Check the response header if all is fine
114 if (!isInString('200', $response[0])) {
115 // Something bad happend... :(
117 'status' => 'request_error',
118 'message' => '{%message,WERNIS_API_REQUEST_ERROR=' . $response[0] . '%}'
122 // All (maybe) fine so remove the response header from server
123 $responseLine = '*INVALID*';
124 for ($idx = (count($response) - 1); $idx > 1; $idx--) {
125 $line = trim($response[$idx]);
127 $responseLine = $line;
132 // Is the response leaded by a & symbol?
133 if (substr($responseLine, 0, 1) != '&') {
134 // Something badly happened on server-side
136 'status' => 'request_problem',
137 'message' => sprintf(getMessage('WERNIS_API_REQUEST_PROBLEM'), $response[0], secureString($responseLine))
141 // Remove the leading & (which can be used in Flash)
142 $responseLine = substr($responseLine, 1);
144 // Bring back the response
145 $data = explode('=', $responseLine);
147 // Default return array (should not stay empty)
150 // We use only the first two entries (which shall be fine)
151 if ($data[0] === 'error') {
152 // The request has failed... :(
154 case '404': // Invalid API id
155 case 'AUTH': // Authorization has failed
157 'status' => 'auth_failed',
158 'message' => '{--WERNIS_API_REQUEST_FAILED_AUTH--}'
162 case 'LOCKED': // User account is locked!
163 case 'PASS': // Bad passphrase entered
164 case 'USER': // Missing account or invalid password
166 'status' => 'user_failed',
167 'message' => '{--WERNIS_API_REQUEST_FAILED_USER--}'
171 case 'OWN': // Transfer to own account
173 'status' => 'own_failed',
174 'message' => '{--WERNIS_API_REQUEST_FAILED_OWN--}'
178 case 'AMOUNT': // Amount is depleted
180 'status' => 'amount_failed',
181 'message' => '{--WERNIS_API_REQUEST_FAILED_AMOUNT--}'
185 case 'AMOUNT-SEND': // API amount is depleted
187 'status' => 'api_amount_failed',
188 'message' => '{--WERNIS_API_REQUEST_FAILED_API_AMOUNT--}'
192 default: // Unknown error (maybe new?)
193 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Unknown error %s from WDS66 API received.", $data[1]));
195 'status' => 'request_failed',
196 'message' => '{%message,WERNIS_API_REQUEST_FAILED=' . $data[1] . '%}'
204 'response' => $responseLine
212 // Tests the function by calling balance.php on the API
213 function WERNIS_TEST_API () {
214 // Result is always failed
217 // Return the result from the lower functions
218 $return = WERNIS_SEND_REQUEST('balance.php');
220 // Did it went smoothly?
221 if ($return['status'] == 'OK') {
225 // Status failure text
226 WERNIS_STATUS_MESSAGE($return['message'], $return['status']);
233 // Widthdraw this amount
234 function WERNIS_EXECUTE_WITHDRAW ($wdsId, $userMd5, $amount) {
235 // Is the sponsor extension installed?
236 if (!isWernisWithdrawActive()) {
237 if (!isExtensionActive('sponsor')) {
240 } elseif (!isSponsor()) {
241 // No sponsor, not allowed to withdraw!
246 // Default is failed attempt
249 // Prepare the request data
250 $requestData = array(
251 'sub_request' => 'receive',
252 't_uid' => bigintval($wdsId),
254 'r_uid' => getWernisRefid(),
255 'amount' => bigintval($amount),
256 'purpose' => getMaskedMessage('WERNIS_API_PURPOSE_WITHDRAW', getMemberId())
259 // Return the result from the lower functions
260 $return = WERNIS_SEND_REQUEST('book.php', $requestData);
262 if ($return['status'] == 'OK') {
267 WERNIS_LOG_TRANSFER($wdsId, $amount, 'WITHDRAW');
269 // Status failure text
270 WERNIS_STATUS_MESSAGE($return['message'], $return['status']);
273 WERNIS_LOG_TRANSFER($wdsId, $amount, 'FAILED', $return['message'], $return['status']);
281 // Payout this amount
282 function WERNIS_EXECUTE_PAYOUT ($wdsId, $amount) {
283 // Default is failed attempt
286 // Prepare the request data
287 $requestData = array(
288 'sub_request' => 'send',
289 't_uid' => getWernisRefid(),
290 't_md5' => getWernisPassMd5(),
291 'r_uid' => bigintval($wdsId),
292 'amount' => bigintval($amount),
293 'purpose' => getMaskedMessage('WERNIS_API_PURPOSE_PAYOUT', getMemberId())
296 // Return the result from the lower functions
297 $return = WERNIS_SEND_REQUEST('book.php', $requestData);
299 if ($return['status'] == 'OK') {
304 WERNIS_LOG_TRANSFER($wdsId, $amount, 'PAYOUT');
306 // Status failure text
307 WERNIS_STATUS_MESSAGE($return['message'], $return['status']);
310 WERNIS_LOG_TRANSFER($wdsId, $amount, 'FAILED', $return['message'], $return['status']);
317 // Translate the status IN/OUT
318 function translateWernisTransferStatus ($status) {
319 // Default status is unknown
320 $return = '{%message,WERNIS_STATUS_UNKNWOWN=' . $status . '%}';
322 // Construct message id
323 $messageId = 'WERNIS_STATUS_' . $status;
326 if (isMessageIdValid($messageId)) {
327 // Then use it as message string
328 $return = '{--' . $messageId . '--}';
336 function WERNIS_LOG_TRANSFER ($wdsId, $amount, $type = 'FAILED', $message = '', $status = '') {
337 // Register this wernis movement
338 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 (%s, %s, %s, UNIX_TIMESTAMP(), '%s', '%s', '%s')",
346 ), __FUNCTION__, __LINE__);
349 // Take fees and factor
350 function WERNIS_TAKE_FEE ($points, $mode) {
351 // Payout or withdraw are allowed modes!
352 //* DEBUG: */ debugOutput('mode=' . $mode . ',points=' . $points);
353 if (!in_array($mode, array('payout', 'withdraw'))) {
354 // Log error and abort
355 logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . getMemberId() . ',mode=' . $mode . ',points=' . $points . ' - unknown mode detected.');
359 // Is there a percentage or fixed fee?
360 if (getConfig('wernis_' . $mode . '_fee_percent') > 0) {
362 $points -= $points * getConfig('wernis_'.$mode.'_fee_percent') / 100;
363 } elseif (getConfig('wernis_' . $mode . '_fee_fix') > 0) {
365 $points -= getConfig('wernis_' . $mode . '_fee_fix');
368 // Divide/multiply the factor
369 if ($mode == 'payout') {
371 $points = $points / getWernisPayoutFactor();
373 // Multiply for withdraw
374 $points = $points * getWernisWithdrawFactor();
378 //* DEBUG: */ debugOutput('mode=' . $mode . ',points=' . $points);
382 // Add withdraw fees and factor
383 function WERNIS_ADD_WITHDRAW_FEE ($points) {
384 // Is there a percentage or fixed fee?
385 if (getWernisWithdrawFeePercent() > 0) {
387 $points += $points * getWernisWithdrawFeePercent() / 100;
388 } elseif (getWernisWithdrawFeeFix() > 0) {
390 $points += getWernisWithdrawFeeFix();
397 //-----------------------------------------------------------------------------
399 //-----------------------------------------------------------------------------
401 // Wrapper function for 'wernis_refid'
402 function getWernisRefid () {
404 if (!isset($GLOBALS[__FUNCTION__])) {
406 $GLOBALS[__FUNCTION__] = getConfig('wernis_refid');
410 return $GLOBALS[__FUNCTION__];
413 // Wrapper function for 'wernis_pass_md5'
414 function getWernisPassMd5 () {
416 if (!isset($GLOBALS[__FUNCTION__])) {
418 $GLOBALS[__FUNCTION__] = getConfig('wernis_pass_md5');
422 return $GLOBALS[__FUNCTION__];
425 // Wrapper function for 'wernis_api_id'
426 function getWernisApiId () {
428 if (!isset($GLOBALS[__FUNCTION__])) {
430 $GLOBALS[__FUNCTION__] = getConfig('wernis_api_id');
434 return $GLOBALS[__FUNCTION__];
437 // Wrapper function for 'wernis_api_md5'
438 function getWernisApiMd5 () {
440 if (!isset($GLOBALS[__FUNCTION__])) {
442 $GLOBALS[__FUNCTION__] = getConfig('wernis_api_md5');
446 return $GLOBALS[__FUNCTION__];
449 // Wrapper function for 'wernis_api_url'
450 function getWernisApiUrl () {
452 if (!isset($GLOBALS[__FUNCTION__])) {
454 $GLOBALS[__FUNCTION__] = getConfig('wernis_api_url');
458 return $GLOBALS[__FUNCTION__];
461 // Wrapper function for 'wernis_withdraw_active'
462 function getWernisWithdrawActive () {
464 if (!isset($GLOBALS[__FUNCTION__])) {
466 $GLOBALS[__FUNCTION__] = getConfig('wernis_withdraw_active');
470 return $GLOBALS[__FUNCTION__];
473 // Wrapper function for 'wernis_payout_active'
474 function getWernisPayoutActive () {
476 if (!isset($GLOBALS[__FUNCTION__])) {
478 $GLOBALS[__FUNCTION__] = getConfig('wernis_payout_active');
482 return $GLOBALS[__FUNCTION__];
485 // Wrapper function for 'wernis_withdraw_active'
486 function isWernisWithdrawActive () {
488 if (!isset($GLOBALS[__FUNCTION__])) {
490 $GLOBALS[__FUNCTION__] = (getConfig('wernis_withdraw_active') == 'Y');
494 return $GLOBALS[__FUNCTION__];
497 // Wrapper function for 'wernis_payout_active'
498 function isWernisPayoutActive () {
500 if (!isset($GLOBALS[__FUNCTION__])) {
502 $GLOBALS[__FUNCTION__] = (getConfig('wernis_payout_active') == 'Y');
506 return $GLOBALS[__FUNCTION__];
509 // Wrapper function for 'wernis_withdraw_factor'
510 function getWernisWithdrawFactor () {
512 if (!isset($GLOBALS[__FUNCTION__])) {
514 $GLOBALS[__FUNCTION__] = getConfig('wernis_withdraw_factor');
518 return $GLOBALS[__FUNCTION__];
521 // Wrapper function for 'wernis_payout_factor'
522 function getWernisPayoutFactor () {
524 if (!isset($GLOBALS[__FUNCTION__])) {
526 $GLOBALS[__FUNCTION__] = getConfig('wernis_payout_factor');
530 return $GLOBALS[__FUNCTION__];
533 // Wrapper function for 'wernis_withdraw_fee_percent'
534 function getWernisWithdrawFeePercent () {
536 if (!isset($GLOBALS[__FUNCTION__])) {
538 $GLOBALS[__FUNCTION__] = getConfig('wernis_withdraw_fee_percent');
542 return $GLOBALS[__FUNCTION__];
545 // Wrapper function for 'wernis_withdraw_fee_fix'
546 function getWernisWithdrawFeeFix () {
548 if (!isset($GLOBALS[__FUNCTION__])) {
550 $GLOBALS[__FUNCTION__] = getConfig('wernis_withdraw_fee_fix');
554 return $GLOBALS[__FUNCTION__];