Extension wernis now should fully work again, withdraw was broken
[mailer.git] / inc / libs / wernis_functions.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 10/19/2003 *
4  * ===============                              Last change: 08/12/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : what-points.php                                  *
8  * -------------------------------------------------------------------- *
9  * Short description : All your collected points...                     *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Alle Ihrer gesammelten Punkte                    *
12  * -------------------------------------------------------------------- *
13  *                                                                      *
14  * -------------------------------------------------------------------- *
15  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
16  * For more information visit: http://www.mxchange.org                  *
17  *                                                                      *
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.                                  *
22  *                                                                      *
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.                         *
27  *                                                                      *
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,               *
31  * MA  02110-1301  USA                                                  *
32  ************************************************************************/
33
34 // Some security stuff...
35 if (!defined('__SECURITY')) {
36         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
37         require($INC);
38 }
39
40 // Sets a status message and code
41 function WERNIS_STATUS_MESSAGE ($msg, $status) {
42         global $WERNIS;
43         $WERNIS['message'] = $msg;
44         $WERNIS['status'] = $status;
45 }
46
47 // Get the status message
48 function GET_WERNIS_ERROR_MESSAGE () {
49         global $WERNIS;
50         if (isset($WERNIS['message'])) {
51                 // Use raw message
52                 return $WERNIS['message'];
53         } elseif (isset($WERNIS['status'])) {
54                 // Fall-back to status
55                 return sprintf(WERNIS_ERROR_STATUS, $WERNIS['status']);
56         } else {
57                 // Something bad happend
58                 return WERNIS_UNKNOWN_ERROR;
59         }
60 }
61
62 // Get the status code
63 function GET_WERNIS_ERROR_CODE () {
64         global $WERNIS;
65         if (isset($WERNIS['status'])) {
66                 // Use raw message
67                 return $WERNIS['status'];
68         } else {
69                 // Something bad happend
70                 return WERNIS_UNKNOWN_ERROR;
71         }
72 }
73
74 // Sends out a request to the API and returns it's result
75 function WERNIS_SEND_REQUEST ($scriptName, $requestData =  array()) {
76         global $_CONFIG;
77
78         // Is the requestData an array?
79         if (!is_array($requestData)) {
80                 // Then abort here!
81                 return array(
82                         'status'  => "failed_general",
83                         'message' => WERNIS_API_REQUEST_DATA_INVALID
84                 );
85         } // END - if
86
87         // Is the API id and MD5 hash there?
88         if ((empty($_CONFIG['wernis_api_id'])) || (empty($_CONFIG['wernis_api_md5']))) {
89                 // Abort here...
90                 return array(
91                         'status'  => "failed_general",
92                         'message' => WERNIS_API_REQUEST_DATA_MISSING
93                 );
94         } // END - if
95
96         // Add more request data
97         $requestData['api_id']  = bigintval($_CONFIG['wernis_api_id']);
98         $requestData['api_key'] = $_CONFIG['wernis_api_md5'];
99
100         // Construct the request string
101         $requestString = $_CONFIG['wernis_api_url'] . $scriptName;
102
103         // Get the raw response from the lower function
104         $response = POST_URL($requestString, $requestData);
105
106         // Check the response header if all is fine
107         if (strpos($response[0], "200") === false) {
108                 // Something bad happend... :(
109                 return array(
110                         'status'  => "request_error",
111                         'message' => sprintf(WERNIS_API_REQUEST_ERROR, $response[0])
112                 );
113         } // END - if
114
115         // All (maybe) fine so remove the response header from server
116         $response = $response[(count($response) - 1)];
117
118         // Prepare the returning result for higher functions
119         if (substr($response, 0, 1) == "&") {
120                 // Remove the leading & (which can be used in Flash)
121                 $response = substr($response, 1);
122         } // END - if
123
124         // Bring back the response
125         $data = explode("=", $response);
126
127         // Default return array (should not stay empty)
128         $return = array();
129
130         // We use only the first two entries (which shall be fine)
131         if ($data[0] === "error") {
132                 // The request has failed... :(
133                 switch ($data[1]) {
134                         case "404": // Invalid API ID
135                         case "AUTH": // Authorization has failed
136                                 $return = array(
137                                         'status'  => "auth_failed",
138                                         'message' => WERNIS_API_REQUEST_FAILED_AUTH
139                                 );
140                                 break;
141
142                         case "LOCKED": // User account is locked!
143                         case "PASS": // Bad passphrase entered
144                         case "USER": // Missing account or invalid password
145                                 $return = array(
146                                         'status'  => "user_failed",
147                                         'message' => WERNIS_API_REQUEST_FAILED_USER
148                                 );
149                                 break;
150
151                         case "OWN": // Transfer to own account
152                                 $return = array(
153                                         'status'  => "own_failed",
154                                         'message' => WERNIS_API_REQUEST_FAILED_OWN
155                                 );
156                                 break;
157
158                         case "AMOUNT": // Amount is depleted
159                                 $return = array(
160                                         'status'  => "amount_failed",
161                                         'message' => WERNIS_API_REQUEST_FAILED_AMOUNT
162                                 );
163                                 break;
164
165                         case "AMOUNT-SEND": // API amount is depleted
166                                 $return = array(
167                                         'status'  => "api_amount_failed",
168                                         'message' => WERNIS_API_REQUEST_FAILED_API_AMOUNT
169                                 );
170                                 break;
171
172                         default: // Unknown error (maybe new?)
173                                 DEBUG_LOG(__FILE__, __LINE__, sprintf("Unknown error %s from WDS66 API received.", $data[1]));
174                                 $return = array(
175                                         'status'  => "request_failed",
176                                         'message' => sprintf(WERNIS_API_REQUEST_FAILED, $data[1])
177                                 );
178                                 break;
179                 }
180         } else {
181                 // All fine here
182                 $return = array(
183                         'status'   => "OK",
184                         'response' => $response
185                 );
186         }
187
188         // Return the result
189         return $return;
190 }
191
192 // Tests the function by calling balance.php on the API
193 function WERNIS_TEST_API () {
194         // Get config first
195         global $_CONFIG;
196         $result = false;
197
198         // Return the result from the lower functions
199         $return = WERNIS_SEND_REQUEST("balance.php");
200
201         if ($return['status'] == "OK") {
202                 // All fine!
203                 $result = true;
204         } else {
205                 // Status failture text
206                 WERNIS_STATUS_MESSAGE($return['message'], $return['status']);
207         }
208
209         // Return result
210         return $result;
211 }
212
213 // Widthdraw this amount
214 function WERNIS_EXECUTE_WITHDRAW ($wdsId, $userMd5, $amount) {
215         global $_CONFIG;
216
217         // Is the sponsor extension installed?
218         if ($_CONFIG['wernis_withdraw_active'] == "N") {
219                 if (!EXT_IS_ACTIVE("sponsor")) {
220                         // No, abort here
221                         return false;
222                 } elseif (!IS_SPONSOR()) {
223                         // No sponsor, not allowed to withdraw!
224                         return false;
225                 }
226         } // END - if
227
228         // Default is failed attempt
229         $result = false;
230
231         // Prepare the purpose
232         $eval = "\$purpose = \"".COMPILE_CODE(sprintf(WERNIS_API_PURPOSE_WITHDRAW, $GLOBALS['userid']))."\";";
233         eval($eval);
234
235         // Prepare the request data
236         $requestData = array(
237                 'sub_request'   => "receive",
238                 't_uid'                 => bigintval($wdsId),
239                 't_md5'                 => $userMd5,
240                 'r_uid'                 => $_CONFIG['wernis_refid'],
241                 'amount'                => bigintval($amount),
242                 'purpose'               => urlencode(base64_encode($purpose))
243         );
244
245         // Return the result from the lower functions
246         $return = WERNIS_SEND_REQUEST("book.php", $requestData);
247
248         if ($return['status'] == "OK") {
249                 // All fine!
250                 $result = true;
251
252                 // Log the transfer
253                 WERNIS_LOG_TRANSFER($wdsId, $amount, 'IN');
254         } else {
255                 // Status failture text
256                 WERNIS_STATUS_MESSAGE($return['message'], $return['status']);
257
258                 // Log the transfer
259                 WERNIS_LOG_TRANSFER($wdsId, $amount, 'FAILED', $return['message'], $return['status']);
260         }
261
262         // Return result
263         return $result;
264 }
265
266
267 // Payout this amount
268 function WERNIS_EXECUTE_PAYOUT ($wdsId, $amount) {
269         global $_CONFIG;
270
271         // Default is failed attempt
272         $result = false;
273
274         // Prepare the purpose
275         $eval = "\$purpose = \"".COMPILE_CODE(sprintf(WERNIS_API_PURPOSE_PAYOUT, $GLOBALS['userid']))."\";";
276         eval($eval);
277
278         // Prepare the request data
279         $requestData = array(
280                 'sub_request'   => "send",
281                 't_uid'                 => $_CONFIG['wernis_refid'],
282                 't_md5'                 => $_CONFIG['wernis_pass_md5'],
283                 'r_uid'                 => bigintval($wdsId),
284                 'amount'                => bigintval($amount),
285                 'purpose'               => urlencode(base64_encode($purpose))
286         );
287
288         // Return the result from the lower functions
289         $return = WERNIS_SEND_REQUEST("book.php", $requestData);
290
291         if ($return['status'] == "OK") {
292                 // All fine!
293                 $result = true;
294
295                 // Log the transfer
296                 WERNIS_LOG_TRANSFER($wdsId, $amount, 'OUT');
297         } else {
298                 // Status failture text
299                 WERNIS_STATUS_MESSAGE($return['message'], $return['status']);
300
301                 // Log the transfer
302                 WERNIS_LOG_TRANSFER($wdsId, $amount, 'FAILED', $return['message'], $return['status']);
303         }
304
305         // Return result
306         return $result;
307 }
308
309 // Translate the status IN/OUT
310 function WERNIS_TRANSFER_STATUS ($status) {
311         // Default status
312         $return = sprintf(WERNIS_STATUS_UNKNWOWN, $status);
313         switch ($status) {
314                 case "IN": // Withdraw
315                         $return = WERNIS_STATUS_WITHDRAW;
316                         break;
317
318                 case "OUT": // Payout
319                         $return = WERNIS_STATUS_PAYOUT;
320                         break;
321
322                 case "FAILED": // Payout
323                         $return = WERNIS_STATUS_FAILED;
324                         break;
325         }
326
327         // Return the status
328         return $return;
329 }
330
331 // Log the transfer
332 function WERNIS_LOG_TRANSFER ($wdsId, $amount, $type = 'FAILED', $message = "", $status = "") {
333         // Register this wernis movement
334         $result = 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')",
335                 array($GLOBALS['userid'], bigintval($wdsId), bigintval($amount), $type, $message, $status), __FILE__, __LINE__);
336 }
337
338 // Take fees and factor
339 function WERNIS_TAKE_FEE ($points, $mode) {
340         global $_CONFIG;
341
342         // Payout or withdraw are allowed modes!
343         //* DEBUG: */ echo "mode={$mode},points={$points}<br />\n";
344         if (!in_array($mode, array('payout', 'withdraw'))) {
345                 // Log error and abort
346                 DEBUG_LOG(__FUNCTION__, __LINE__, "uid={$GLOBALS['userid']},mode={$mode},points={$points}");
347                 return false;
348         } // END - if
349
350         // Is there a percentage or fixed fee?
351         if ($_CONFIG['wernis_'.$mode.'_fee_percent'] > 0) {
352                 // Percentage fee
353                 $points -= $points * $_CONFIG['wernis_'.$mode.'_fee_percent'] / 100;
354         } elseif ($_CONFIG['wernis_'.$mode.'_fee_fix'] > 0) {
355                 // Fixed fee
356                 $points -= $_CONFIG['wernis_'.$mode.'_fee_fix'];
357         }
358
359         // Divide/multiply the factor
360         if ($mode == "payout") {
361                 // Divide for payout
362                 $points = $points / $_CONFIG['wernis_payout_factor'];
363         } else {
364                 // Multiply for withdraw
365                 $points = $points * $_CONFIG['wernis_withdraw_factor'];
366         }
367
368         // Return value
369         //* DEBUG: */ echo "mode={$mode},points={$points}<br />\n";
370         return $points;
371 }
372
373 // Add withdraw fees and factor
374 function WERNIS_ADD_WITHDRAW_FEE ($points) {
375         global $_CONFIG;
376
377         // Is there a percentage or fixed fee?
378         if ($_CONFIG['wernis_withdraw_fee_percent'] > 0) {
379                 // Percentage fee
380                 $points += $points * $_CONFIG['wernis_withdraw_fee_percent'] / 100;
381         } elseif ($_CONFIG['wernis_withdraw_fee_fix'] > 0) {
382                 // Fixed fee
383                 $points += $_CONFIG['wernis_withdraw_fee_fix'];
384         }
385
386         // Return value
387         return $points;
388 }
389
390 // Add all fees to the array
391 function WERNIS_ADD_FEES_TO_ARRAY (&$array) {
392         global $_CONFIG;
393
394         // Is the array an array? ;-)
395         if (!is_array($array)) {
396                 // Log error and return
397                 DEBUG_LOG(__FUNCTION__, __LINE__, " Type ".gettype($array)." != array.");
398                 return;
399         } // END - if
400
401         // Add both factors
402         $array['payout_factor']        = TRANSLATE_COMMA($_CONFIG['wernis_payout_factor']);
403         $array['withdraw_factor']      = TRANSLATE_COMMA($_CONFIG['wernis_withdraw_factor']);
404
405         // Add all fees
406         $array['payout_fee_percent']   = TRANSLATE_COMMA($_CONFIG['wernis_payout_fee_percent']);
407         $array['withdraw_fee_percent'] = TRANSLATE_COMMA($_CONFIG['wernis_withdraw_fee_percent']);
408         $array['payout_fee_fix']       = TRANSLATE_COMMA($_CONFIG['wernis_payout_fee_fix']);
409         $array['withdraw_fee_fix']     = TRANSLATE_COMMA($_CONFIG['wernis_withdraw_fee_fix']);
410 }
411
412 //
413 ?>