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 * -------------------------------------------------------------------- *
14 * -------------------------------------------------------------------- *
15 * Copyright (c) 2003 - 2008 by Roland Haeder *
16 * For more information visit: http://www.mxchange.org *
18 * This program is free software; you can redistribute it and/or modify *
19 * it under the terms of the GNU General Public License as published by *
20 * the Free Software Foundation; either version 2 of the License, or *
21 * (at your option) any later version. *
23 * This program is distributed in the hope that it will be useful, *
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
26 * GNU General Public License for more details. *
28 * You should have received a copy of the GNU General Public License *
29 * along with this program; if not, write to the Free Software *
30 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
32 ************************************************************************/
34 // Some security stuff...
35 if (!defined('__SECURITY')) {
36 $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
40 // Sets a status message and code
41 function WERNIS_STATUS_MESSAGE ($msg, $status) {
42 $GLOBALS['wernis_data']['message'] = $msg;
43 $GLOBALS['wernis_data']['status'] = $status;
46 // Get the status message
47 function GET_WERNIS_ERROR_MESSAGE () {
48 if (isset($GLOBALS['wernis_data']['message'])) {
50 return $GLOBALS['wernis_data']['message'];
51 } elseif (isset($GLOBALS['wernis_data']['status'])) {
52 // Fall-back to status
53 return sprintf(WERNIS_ERROR_STATUS, $GLOBALS['wernis_data']['status']);
55 // Something bad happend
56 return WERNIS_UNKNOWN_ERROR;
60 // Get the status code
61 function GET_WERNIS_ERROR_CODE () {
62 if (isset($GLOBALS['wernis_data']['status'])) {
64 return $GLOBALS['wernis_data']['status'];
66 // Something bad happend
67 return WERNIS_UNKNOWN_ERROR;
71 // Sends out a request to the API and returns it's result
72 function WERNIS_SEND_REQUEST ($scriptName, $requestData = array()) {
73 // Is the requestData an array?
74 if (!is_array($requestData)) {
77 'status' => "failed_general",
78 'message' => WERNIS_API_REQUEST_DATA_INVALID
82 // Is the API id and MD5 hash there?
83 if ((getConfig('wernis_api_id') == "") || (getConfig('wernis_api_md5') == "")) {
86 'status' => "failed_general",
87 'message' => WERNIS_API_REQUEST_DATA_MISSING
91 // Add more request data
92 $requestData['api_id'] = getConfig(('wernis_api_id'));
93 $requestData['api_key'] = getConfig('wernis_api_md5');
95 // Construct the request string
96 $requestString = getConfig('wernis_api_url') . $scriptName;
98 // Get the raw response from the lower function
99 $response = POST_URL($requestString, $requestData);
101 // Check the response header if all is fine
102 if (strpos($response[0], "200") === false) {
103 // Something bad happend... :(
105 'status' => "request_error",
106 'message' => sprintf(WERNIS_API_REQUEST_ERROR, $response[0])
110 // All (maybe) fine so remove the response header from server
111 $response = $response[(count($response) - 1)];
113 // Prepare the returning result for higher functions
114 if (substr($response, 0, 1) == "&") {
115 // Remove the leading & (which can be used in Flash)
116 $response = substr($response, 1);
119 // Bring back the response
120 $data = explode("=", $response);
122 // Default return array (should not stay empty)
125 // We use only the first two entries (which shall be fine)
126 if ($data[0] === "error") {
127 // The request has failed... :(
129 case "404": // Invalid API ID
130 case "AUTH": // Authorization has failed
132 'status' => "auth_failed",
133 'message' => WERNIS_API_REQUEST_FAILED_AUTH
137 case "LOCKED": // User account is locked!
138 case "PASS": // Bad passphrase entered
139 case "USER": // Missing account or invalid password
141 'status' => "user_failed",
142 'message' => WERNIS_API_REQUEST_FAILED_USER
146 case "OWN": // Transfer to own account
148 'status' => "own_failed",
149 'message' => WERNIS_API_REQUEST_FAILED_OWN
153 case "AMOUNT": // Amount is depleted
155 'status' => "amount_failed",
156 'message' => WERNIS_API_REQUEST_FAILED_AMOUNT
160 case "AMOUNT-SEND": // API amount is depleted
162 'status' => "api_amount_failed",
163 'message' => WERNIS_API_REQUEST_FAILED_API_AMOUNT
167 default: // Unknown error (maybe new?)
168 DEBUG_LOG(__FUNCTION__, __LINE__, sprintf("Unknown error %s from WDS66 API received.", $data[1]));
170 'status' => "request_failed",
171 'message' => sprintf(WERNIS_API_REQUEST_FAILED, $data[1])
179 'response' => $response
187 // Tests the function by calling balance.php on the API
188 function WERNIS_TEST_API () {
189 // Result is always failed
192 // Return the result from the lower functions
193 $return = WERNIS_SEND_REQUEST("balance.php");
195 if ($return['status'] == "OK") {
199 // Status failture text
200 WERNIS_STATUS_MESSAGE($return['message'], $return['status']);
207 // Widthdraw this amount
208 function WERNIS_EXECUTE_WITHDRAW ($wdsId, $userMd5, $amount) {
209 // Is the sponsor extension installed?
210 if (getConfig('wernis_withdraw_active') != "Y") {
211 if (!EXT_IS_ACTIVE("sponsor")) {
214 } elseif (!IS_SPONSOR()) {
215 // No sponsor, not allowed to withdraw!
220 // Default is failed attempt
223 // Prepare the purpose
224 $eval = "\$purpose = \"".COMPILE_CODE(sprintf(WERNIS_API_PURPOSE_WITHDRAW, $GLOBALS['userid']))."\";";
227 // Prepare the request data
228 $requestData = array(
229 'sub_request' => "receive",
230 't_uid' => bigintval($wdsId),
232 'r_uid' => getConfig('wernis_refid'),
233 'amount' => bigintval($amount),
234 'purpose' => encodeString($purpose, false)
237 // Return the result from the lower functions
238 $return = WERNIS_SEND_REQUEST("book.php", $requestData);
240 if ($return['status'] == "OK") {
245 WERNIS_LOG_TRANSFER($wdsId, $amount, 'IN');
247 // Status failture text
248 WERNIS_STATUS_MESSAGE($return['message'], $return['status']);
251 WERNIS_LOG_TRANSFER($wdsId, $amount, 'FAILED', $return['message'], $return['status']);
259 // Payout this amount
260 function WERNIS_EXECUTE_PAYOUT ($wdsId, $amount) {
261 // Default is failed attempt
264 // Prepare the purpose
265 $eval = "\$purpose = \"".COMPILE_CODE(sprintf(getMessage('WERNIS_API_PURPOSE_PAYOUT'), $GLOBALS['userid']))."\";";
268 // Prepare the request data
269 $requestData = array(
270 'sub_request' => "send",
271 't_uid' => getConfig('wernis_refid'),
272 't_md5' => getConfig('wernis_pass_md5'),
273 'r_uid' => bigintval($wdsId),
274 'amount' => bigintval($amount),
275 'purpose' => encodeString($purpose, false)
278 // Return the result from the lower functions
279 $return = WERNIS_SEND_REQUEST("book.php", $requestData);
281 if ($return['status'] == "OK") {
286 WERNIS_LOG_TRANSFER($wdsId, $amount, 'OUT');
288 // Status failture text
289 WERNIS_STATUS_MESSAGE($return['message'], $return['status']);
292 WERNIS_LOG_TRANSFER($wdsId, $amount, 'FAILED', $return['message'], $return['status']);
299 // Translate the status IN/OUT
300 function WERNIS_TRANSFER_STATUS ($status) {
302 $return = sprintf(WERNIS_STATUS_UNKNWOWN, $status);
304 case "IN": // Withdraw
305 $return = WERNIS_STATUS_WITHDRAW;
308 case "OUT": // Payout
309 $return = WERNIS_STATUS_PAYOUT;
312 case "FAILED": // Payout
313 $return = WERNIS_STATUS_FAILED;
322 function WERNIS_LOG_TRANSFER ($wdsId, $amount, $type = 'FAILED', $message = "", $status = "") {
323 // Register this wernis movement
324 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')",
325 array($GLOBALS['userid'], bigintval($wdsId), bigintval($amount), $type, $message, $status), __FUNCTION__, __LINE__);
328 // Take fees and factor
329 function WERNIS_TAKE_FEE ($points, $mode) {
330 // Payout or withdraw are allowed modes!
331 //* DEBUG: */ echo "mode={$mode},points={$points}<br />\n";
332 if (!in_array($mode, array('payout', 'withdraw'))) {
333 // Log error and abort
334 DEBUG_LOG(__FUNCTION__, __LINE__, "uid={$GLOBALS['userid']},mode={$mode},points={$points}");
338 // Is there a percentage or fixed fee?
339 if (getConfig('wernis_'.$mode.'_fee_percent') > 0) {
341 $points -= $points * getConfig('wernis_'.$mode.'_fee_percent') / 100;
342 } elseif (getConfig('wernis_'.$mode.'_fee_fix') > 0) {
344 $points -= getConfig('wernis_'.$mode.'_fee_fix');
347 // Divide/multiply the factor
348 if ($mode == "payout") {
350 $points = $points / getConfig('wernis_payout_factor');
352 // Multiply for withdraw
353 $points = $points * getConfig('wernis_withdraw_factor');
357 //* DEBUG: */ echo "mode={$mode},points={$points}<br />\n";
361 // Add withdraw fees and factor
362 function WERNIS_ADD_WITHDRAW_FEE ($points) {
363 // Is there a percentage or fixed fee?
364 if (getConfig('wernis_withdraw_fee_percent') > 0) {
366 $points += $points * getConfig('wernis_withdraw_fee_percent') / 100;
367 } elseif (getConfig('wernis_withdraw_fee_fix') > 0) {
369 $points += getConfig('wernis_withdraw_fee_fix');
376 // Add all fees to the array
377 function WERNIS_ADD_FEES_TO_ARRAY (&$array) {
378 // Is the array an array? ;-)
379 if (!is_array($array)) {
380 // Log error and return
381 DEBUG_LOG(__FUNCTION__, __LINE__, " Type ".gettype($array)." != array.");
386 $array['payout_factor'] = TRANSLATE_COMMA(getConfig('wernis_payout_factor'));
387 $array['withdraw_factor'] = TRANSLATE_COMMA(getConfig('wernis_withdraw_factor'));
390 $array['payout_fee_percent'] = TRANSLATE_COMMA(getConfig('wernis_payout_fee_percent'));
391 $array['withdraw_fee_percent'] = TRANSLATE_COMMA(getConfig('wernis_withdraw_fee_percent'));
392 $array['payout_fee_fix'] = TRANSLATE_COMMA(getConfig('wernis_payout_fee_fix'));
393 $array['withdraw_fee_fix'] = TRANSLATE_COMMA(getConfig('wernis_withdraw_fee_fix'));