2 /************************************************************************
3 * MXChange v0.2.1 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 * For more information visit: http://www.mxchange.org *
23 * This program is free software; you can redistribute it and/or modify *
24 * it under the terms of the GNU General Public License as published by *
25 * the Free Software Foundation; either version 2 of the License, or *
26 * (at your option) any later version. *
28 * This program is distributed in the hope that it will be useful, *
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
31 * GNU General Public License for more details. *
33 * You should have received a copy of the GNU General Public License *
34 * along with this program; if not, write to the Free Software *
35 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
37 ************************************************************************/
39 // Some security stuff...
40 if (!defined('__SECURITY')) {
44 // Sets a status message and code
45 function WERNIS_STATUS_MESSAGE ($message, $status) {
46 $GLOBALS['wernis_data']['message'] = $message;
47 $GLOBALS['wernis_data']['status'] = $status;
50 // Get the status message
51 function GET_WERNIS_ERROR_MESSAGE () {
52 if (isset($GLOBALS['wernis_data']['message'])) {
54 return $GLOBALS['wernis_data']['message'];
55 } elseif (isset($GLOBALS['wernis_data']['status'])) {
56 // Fall-back to status
57 return sprintf(getMessage('WERNIS_ERROR_STATUS'), $GLOBALS['wernis_data']['status']);
59 // Something bad happend
60 return getMessage('WERNIS_UNKNOWN_ERROR');
64 // Get the status code
65 function GET_WERNIS_ERROR_CODE () {
66 if (isset($GLOBALS['wernis_data']['status'])) {
68 return $GLOBALS['wernis_data']['status'];
70 // Something bad happend
71 return getMessage('WERNIS_UNKNOWN_ERROR');
75 // Sends out a request to the API and returns it's result
76 function WERNIS_SEND_REQUEST ($scriptName, $requestData = array()) {
77 // Is the requestData an array?
78 if (!is_array($requestData)) {
81 'status' => 'failed_general',
82 'message' => getMessage('WERNIS_API_REQUEST_DATA_INVALID')
86 // Is the API id and MD5 hash there?
87 if ((getConfig('wernis_api_id') == '') || (getConfig('wernis_api_md5') == '')) {
90 'status' => 'failed_general',
91 'message' => getMessage('WERNIS_API_REQUEST_DATA_MISSING')
95 // Add more request data
96 $requestData['api_id'] = getConfig('wernis_api_id');
97 $requestData['api_key'] = getConfig('wernis_api_md5');
99 // Construct the request string
100 $requestString = getConfig('wernis_api_url') . $scriptName;
102 // Get the raw response from the lower function
103 $response = sendPostRequest($requestString, $requestData);
105 // Check the response header if all is fine
106 if (strpos($response[0], '200') === false) {
107 // Something bad happend... :(
109 'status' => 'request_error',
110 'message' => sprintf(getMessage('WERNIS_API_REQUEST_ERROR'), $response[0])
114 // All (maybe) fine so remove the response header from server
115 $responseLine = '*INVALID*';
116 for ($idx = (count($response) - 1); $idx > 1; $idx--) {
117 $line = trim($response[$idx]);
119 $responseLine = $line;
124 // Is the response leaded by a & symbol?
125 if (substr($responseLine, 0, 1) != '&') {
126 // Something badly happened on server-side
128 'status' => 'request_problem',
129 'message' => sprintf(getMessage('WERNIS_API_REQUEST_PROBLEM'), $response[0], htmlentities($responseLine))
133 // Remove the leading & (which can be used in Flash)
134 $responseLine = substr($responseLine, 1);
136 // Bring back the response
137 $data = explode('=', $responseLine);
139 // Default return array (should not stay empty)
142 // We use only the first two entries (which shall be fine)
143 if ($data[0] === 'error') {
144 // The request has failed... :(
146 case '404': // Invalid API ID
147 case 'AUTH': // Authorization has failed
149 'status' => 'auth_failed',
150 'message' => getMessage('WERNIS_API_REQUEST_FAILED_AUTH')
154 case 'LOCKED': // User account is locked!
155 case 'PASS': // Bad passphrase entered
156 case 'USER': // Missing account or invalid password
158 'status' => 'user_failed',
159 'message' => getMessage('WERNIS_API_REQUEST_FAILED_USER')
163 case 'OWN': // Transfer to own account
165 'status' => 'own_failed',
166 'message' => getMessage('WERNIS_API_REQUEST_FAILED_OWN')
170 case 'AMOUNT': // Amount is depleted
172 'status' => 'amount_failed',
173 'message' => getMessage('WERNIS_API_REQUEST_FAILED_AMOUNT')
177 case 'AMOUNT-SEND': // API amount is depleted
179 'status' => 'api_amount_failed',
180 'message' => getMessage('WERNIS_API_REQUEST_FAILED_API_AMOUNT')
184 default: // Unknown error (maybe new?)
185 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Unknown error %s from WDS66 API received.", $data[1]));
187 'status' => 'request_failed',
188 'message' => sprintf(getMessage('WERNIS_API_REQUEST_FAILED'), $data[1])
196 'response' => $responseLine
204 // Tests the function by calling balance.php on the API
205 function WERNIS_TEST_API () {
206 // Result is always failed
209 // Return the result from the lower functions
210 $return = WERNIS_SEND_REQUEST('balance.php');
212 if ($return['status'] == 'OK') {
216 // Status failture text
217 WERNIS_STATUS_MESSAGE($return['message'], $return['status']);
224 // Widthdraw this amount
225 function WERNIS_EXECUTE_WITHDRAW ($wdsId, $userMd5, $amount) {
226 // Is the sponsor extension installed?
227 if (getConfig('wernis_withdraw_active') != 'Y') {
228 if (!isExtensionActive('sponsor')) {
231 } elseif (!isSponsor()) {
232 // No sponsor, not allowed to withdraw!
237 // Default is failed attempt
240 // Prepare the purpose
241 eval("\$purpose = \"".compileCode(sprintf(getMessage('WERNIS_API_PURPOSE_WITHDRAW'), getUserId()))."\";");
243 // Prepare the request data
244 $requestData = array(
245 'sub_request' => 'receive',
246 't_uid' => bigintval($wdsId),
248 'r_uid' => getConfig('wernis_refid'),
249 'amount' => bigintval($amount),
250 'purpose' => encodeString($purpose, false)
253 // Return the result from the lower functions
254 $return = WERNIS_SEND_REQUEST('book.php', $requestData);
256 if ($return['status'] == 'OK') {
261 WERNIS_LOG_TRANSFER($wdsId, $amount, 'IN');
263 // Status failture text
264 WERNIS_STATUS_MESSAGE($return['message'], $return['status']);
267 WERNIS_LOG_TRANSFER($wdsId, $amount, 'FAILED', $return['message'], $return['status']);
275 // Payout this amount
276 function WERNIS_EXECUTE_PAYOUT ($wdsId, $amount) {
277 // Default is failed attempt
280 // Prepare the purpose
281 eval("\$purpose = \"".compileCode(sprintf(getMessage('WERNIS_API_PURPOSE_PAYOUT'), getUserId()))."\";");
283 // Prepare the request data
284 $requestData = array(
285 'sub_request' => 'send',
286 't_uid' => getConfig('wernis_refid'),
287 't_md5' => getConfig('wernis_pass_md5'),
288 'r_uid' => bigintval($wdsId),
289 'amount' => bigintval($amount),
290 'purpose' => encodeString($purpose, false)
293 // Return the result from the lower functions
294 $return = WERNIS_SEND_REQUEST('book.php', $requestData);
296 if ($return['status'] == 'OK') {
301 WERNIS_LOG_TRANSFER($wdsId, $amount, 'OUT');
303 // Status failture text
304 WERNIS_STATUS_MESSAGE($return['message'], $return['status']);
307 WERNIS_LOG_TRANSFER($wdsId, $amount, 'FAILED', $return['message'], $return['status']);
314 // Translate the status IN/OUT
315 function WERNIS_TRANSFER_STATUS ($status) {
317 $return = sprintf(getMessage('WERNIS_STATUS_UNKNWOWN'), $status);
319 case 'IN': // Withdraw
320 $return = getMessage('WERNIS_STATUS_WITHDRAW');
323 case 'OUT': // Payout
324 $return = getMessage('WERNIS_STATUS_PAYOUT');
327 case 'FAILED': // Payout
328 $return = getMessage('WERNIS_STATUS_FAILED');
337 function WERNIS_LOG_TRANSFER ($wdsId, $amount, $type = 'FAILED', $message = '', $status = '') {
338 // Register this wernis movement
339 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')",
340 array(getUserId(), bigintval($wdsId), bigintval($amount), $type, $message, $status), __FUNCTION__, __LINE__);
343 // Take fees and factor
344 function WERNIS_TAKE_FEE ($points, $mode) {
345 // Payout or withdraw are allowed modes!
346 //* DEBUG: */ outputHtml("mode={$mode},points={$points}<br />");
347 if (!in_array($mode, array('payout', 'withdraw'))) {
348 // Log error and abort
349 logDebugMessage(__FUNCTION__, __LINE__, "userid={getUserId()},mode={$mode},points={$points}");
353 // Is there a percentage or fixed fee?
354 if (getConfig('wernis_'.$mode.'_fee_percent') > 0) {
356 $points -= $points * getConfig('wernis_'.$mode.'_fee_percent') / 100;
357 } elseif (getConfig('wernis_'.$mode.'_fee_fix') > 0) {
359 $points -= getConfig('wernis_'.$mode.'_fee_fix');
362 // Divide/multiply the factor
363 if ($mode == 'payout') {
365 $points = $points / getConfig('wernis_payout_factor');
367 // Multiply for withdraw
368 $points = $points * getConfig('wernis_withdraw_factor');
372 //* DEBUG: */ outputHtml("mode={$mode},points={$points}<br />");
376 // Add withdraw fees and factor
377 function WERNIS_ADD_WITHDRAW_FEE ($points) {
378 // Is there a percentage or fixed fee?
379 if (getConfig('wernis_withdraw_fee_percent') > 0) {
381 $points += $points * getConfig('wernis_withdraw_fee_percent') / 100;
382 } elseif (getConfig('wernis_withdraw_fee_fix') > 0) {
384 $points += getConfig('wernis_withdraw_fee_fix');
391 // Add all fees to the array
392 function WERNIS_ADD_FEES_TO_ARRAY (&$array) {
393 // Is the array an array? ;-)
394 if (!is_array($array)) {
395 // Log error and return
396 logDebugMessage(__FUNCTION__, __LINE__, 'Type ' . gettype($array) . ' != array.');
401 $array['payout_factor'] = translateComma(getConfig('wernis_payout_factor'));
402 $array['withdraw_factor'] = translateComma(getConfig('wernis_withdraw_factor'));
405 $array['payout_fee_percent'] = translateComma(getConfig('wernis_payout_fee_percent'));
406 $array['withdraw_fee_percent'] = translateComma(getConfig('wernis_withdraw_fee_percent'));
407 $array['payout_fee_fix'] = translateComma(getConfig('wernis_payout_fee_fix'));
408 $array['withdraw_fee_fix'] = translateComma(getConfig('wernis_withdraw_fee_fix'));