0cb27530e046cbb8c623a0283a358daf1c706524
[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         // Is the requestData an array?
77         if (!is_array($requestData)) {
78                 // Then abort here!
79                 return array(
80                         'status'  => "failed_general",
81                         'message' => WERNIS_API_REQUEST_DATA_INVALID
82                 );
83         } // END - if
84
85         // Is the API id and MD5 hash there?
86         if ((getConfig('wernis_api_id') == "") || (getConfig('wernis_api_md5') == "")) {
87                 // Abort here...
88                 return array(
89                         'status'  => "failed_general",
90                         'message' => WERNIS_API_REQUEST_DATA_MISSING
91                 );
92         } // END - if
93
94         // Add more request data
95         $requestData['api_id']  = bigintval(getConfig('wernis_api_id'));
96         $requestData['api_key'] = getConfig('wernis_api_md5');
97
98         // Construct the request string
99         $requestString = getConfig('wernis_api_url') . $scriptName;
100
101         // Get the raw response from the lower function
102         $response = POST_URL($requestString, $requestData);
103
104         // Check the response header if all is fine
105         if (strpos($response[0], "200") === false) {
106                 // Something bad happend... :(
107                 return array(
108                         'status'  => "request_error",
109                         'message' => sprintf(WERNIS_API_REQUEST_ERROR, $response[0])
110                 );
111         } // END - if
112
113         // All (maybe) fine so remove the response header from server
114         $response = $response[(count($response) - 1)];
115
116         // Prepare the returning result for higher functions
117         if (substr($response, 0, 1) == "&") {
118                 // Remove the leading & (which can be used in Flash)
119                 $response = substr($response, 1);
120         } // END - if
121
122         // Bring back the response
123         $data = explode("=", $response);
124
125         // Default return array (should not stay empty)
126         $return = array();
127
128         // We use only the first two entries (which shall be fine)
129         if ($data[0] === "error") {
130                 // The request has failed... :(
131                 switch ($data[1]) {
132                         case "404": // Invalid API ID
133                         case "AUTH": // Authorization has failed
134                                 $return = array(
135                                         'status'  => "auth_failed",
136                                         'message' => WERNIS_API_REQUEST_FAILED_AUTH
137                                 );
138                                 break;
139
140                         case "LOCKED": // User account is locked!
141                         case "PASS": // Bad passphrase entered
142                         case "USER": // Missing account or invalid password
143                                 $return = array(
144                                         'status'  => "user_failed",
145                                         'message' => WERNIS_API_REQUEST_FAILED_USER
146                                 );
147                                 break;
148
149                         case "OWN": // Transfer to own account
150                                 $return = array(
151                                         'status'  => "own_failed",
152                                         'message' => WERNIS_API_REQUEST_FAILED_OWN
153                                 );
154                                 break;
155
156                         case "AMOUNT": // Amount is depleted
157                                 $return = array(
158                                         'status'  => "amount_failed",
159                                         'message' => WERNIS_API_REQUEST_FAILED_AMOUNT
160                                 );
161                                 break;
162
163                         case "AMOUNT-SEND": // API amount is depleted
164                                 $return = array(
165                                         'status'  => "api_amount_failed",
166                                         'message' => WERNIS_API_REQUEST_FAILED_API_AMOUNT
167                                 );
168                                 break;
169
170                         default: // Unknown error (maybe new?)
171                                 DEBUG_LOG(__FUNCTION__, __LINE__, sprintf("Unknown error %s from WDS66 API received.", $data[1]));
172                                 $return = array(
173                                         'status'  => "request_failed",
174                                         'message' => sprintf(WERNIS_API_REQUEST_FAILED, $data[1])
175                                 );
176                                 break;
177                 }
178         } else {
179                 // All fine here
180                 $return = array(
181                         'status'   => "OK",
182                         'response' => $response
183                 );
184         }
185
186         // Return the result
187         return $return;
188 }
189
190 // Tests the function by calling balance.php on the API
191 function WERNIS_TEST_API () {
192         // Result is always failed
193         $result = false;
194
195         // Return the result from the lower functions
196         $return = WERNIS_SEND_REQUEST("balance.php");
197
198         if ($return['status'] == "OK") {
199                 // All fine!
200                 $result = true;
201         } else {
202                 // Status failture text
203                 WERNIS_STATUS_MESSAGE($return['message'], $return['status']);
204         }
205
206         // Return result
207         return $result;
208 }
209
210 // Widthdraw this amount
211 function WERNIS_EXECUTE_WITHDRAW ($wdsId, $userMd5, $amount) {
212         // Is the sponsor extension installed?
213         if (getConfig('wernis_withdraw_active') != "Y") {
214                 if (!EXT_IS_ACTIVE("sponsor")) {
215                         // No, abort here
216                         return false;
217                 } elseif (!IS_SPONSOR()) {
218                         // No sponsor, not allowed to withdraw!
219                         return false;
220                 }
221         } // END - if
222
223         // Default is failed attempt
224         $result = false;
225
226         // Prepare the purpose
227         $eval = "\$purpose = \"".COMPILE_CODE(sprintf(WERNIS_API_PURPOSE_WITHDRAW, $GLOBALS['userid']))."\";";
228         eval($eval);
229
230         // Prepare the request data
231         $requestData = array(
232                 'sub_request'   => "receive",
233                 't_uid'                 => bigintval($wdsId),
234                 't_md5'                 => $userMd5,
235                 'r_uid'                 => getConfig('wernis_refid'),
236                 'amount'                => bigintval($amount),
237                 'purpose'               => encodeString($purpose, false)
238         );
239
240         // Return the result from the lower functions
241         $return = WERNIS_SEND_REQUEST("book.php", $requestData);
242
243         if ($return['status'] == "OK") {
244                 // All fine!
245                 $result = true;
246
247                 // Log the transfer
248                 WERNIS_LOG_TRANSFER($wdsId, $amount, 'IN');
249         } else {
250                 // Status failture text
251                 WERNIS_STATUS_MESSAGE($return['message'], $return['status']);
252
253                 // Log the transfer
254                 WERNIS_LOG_TRANSFER($wdsId, $amount, 'FAILED', $return['message'], $return['status']);
255         }
256
257         // Return result
258         return $result;
259 }
260
261
262 // Payout this amount
263 function WERNIS_EXECUTE_PAYOUT ($wdsId, $amount) {
264         // Default is failed attempt
265         $result = false;
266
267         // Prepare the purpose
268         $eval = "\$purpose = \"".COMPILE_CODE(sprintf(getMessage('WERNIS_API_PURPOSE_PAYOUT'), $GLOBALS['userid']))."\";";
269         eval($eval);
270
271         // Prepare the request data
272         $requestData = array(
273                 'sub_request'   => "send",
274                 't_uid'                 => getConfig('wernis_refid'),
275                 't_md5'                 => getConfig('wernis_pass_md5'),
276                 'r_uid'                 => bigintval($wdsId),
277                 'amount'                => bigintval($amount),
278                 'purpose'               => encodeString($purpose, false)
279         );
280
281         // Return the result from the lower functions
282         $return = WERNIS_SEND_REQUEST("book.php", $requestData);
283
284         if ($return['status'] == "OK") {
285                 // All fine!
286                 $result = true;
287
288                 // Log the transfer
289                 WERNIS_LOG_TRANSFER($wdsId, $amount, 'OUT');
290         } else {
291                 // Status failture text
292                 WERNIS_STATUS_MESSAGE($return['message'], $return['status']);
293
294                 // Log the transfer
295                 WERNIS_LOG_TRANSFER($wdsId, $amount, 'FAILED', $return['message'], $return['status']);
296         }
297
298         // Return result
299         return $result;
300 }
301
302 // Translate the status IN/OUT
303 function WERNIS_TRANSFER_STATUS ($status) {
304         // Default status
305         $return = sprintf(WERNIS_STATUS_UNKNWOWN, $status);
306         switch ($status) {
307                 case "IN": // Withdraw
308                         $return = WERNIS_STATUS_WITHDRAW;
309                         break;
310
311                 case "OUT": // Payout
312                         $return = WERNIS_STATUS_PAYOUT;
313                         break;
314
315                 case "FAILED": // Payout
316                         $return = WERNIS_STATUS_FAILED;
317                         break;
318         }
319
320         // Return the status
321         return $return;
322 }
323
324 // Log the transfer
325 function WERNIS_LOG_TRANSFER ($wdsId, $amount, $type = 'FAILED', $message = "", $status = "") {
326         // Register this wernis movement
327         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')",
328                 array($GLOBALS['userid'], bigintval($wdsId), bigintval($amount), $type, $message, $status), __FILE__, __LINE__);
329 }
330
331 // Take fees and factor
332 function WERNIS_TAKE_FEE ($points, $mode) {
333         // Payout or withdraw are allowed modes!
334         //* DEBUG: */ echo "mode={$mode},points={$points}<br />\n";
335         if (!in_array($mode, array('payout', 'withdraw'))) {
336                 // Log error and abort
337                 DEBUG_LOG(__FUNCTION__, __LINE__, "uid={$GLOBALS['userid']},mode={$mode},points={$points}");
338                 return false;
339         } // END - if
340
341         // Is there a percentage or fixed fee?
342         if (getConfig('wernis_'.$mode.'_fee_percent') > 0) {
343                 // Percentage fee
344                 $points -= $points * getConfig('wernis_'.$mode.'_fee_percent') / 100;
345         } elseif (getConfig('wernis_'.$mode.'_fee_fix') > 0) {
346                 // Fixed fee
347                 $points -= getConfig('wernis_'.$mode.'_fee_fix');
348         }
349
350         // Divide/multiply the factor
351         if ($mode == "payout") {
352                 // Divide for payout
353                 $points = $points / getConfig('wernis_payout_factor');
354         } else {
355                 // Multiply for withdraw
356                 $points = $points * getConfig('wernis_withdraw_factor');
357         }
358
359         // Return value
360         //* DEBUG: */ echo "mode={$mode},points={$points}<br />\n";
361         return $points;
362 }
363
364 // Add withdraw fees and factor
365 function WERNIS_ADD_WITHDRAW_FEE ($points) {
366         // Is there a percentage or fixed fee?
367         if (getConfig('wernis_withdraw_fee_percent') > 0) {
368                 // Percentage fee
369                 $points += $points * getConfig('wernis_withdraw_fee_percent') / 100;
370         } elseif (getConfig('wernis_withdraw_fee_fix') > 0) {
371                 // Fixed fee
372                 $points += getConfig('wernis_withdraw_fee_fix');
373         }
374
375         // Return value
376         return $points;
377 }
378
379 // Add all fees to the array
380 function WERNIS_ADD_FEES_TO_ARRAY (&$array) {
381         // Is the array an array? ;-)
382         if (!is_array($array)) {
383                 // Log error and return
384                 DEBUG_LOG(__FUNCTION__, __LINE__, " Type ".gettype($array)." != array.");
385                 return;
386         } // END - if
387
388         // Add both factors
389         $array['payout_factor']        = TRANSLATE_COMMA(getConfig('wernis_payout_factor'));
390         $array['withdraw_factor']      = TRANSLATE_COMMA(getConfig('wernis_withdraw_factor'));
391
392         // Add all fees
393         $array['payout_fee_percent']   = TRANSLATE_COMMA(getConfig('wernis_payout_fee_percent'));
394         $array['withdraw_fee_percent'] = TRANSLATE_COMMA(getConfig('wernis_withdraw_fee_percent'));
395         $array['payout_fee_fix']       = TRANSLATE_COMMA(getConfig('wernis_payout_fee_fix'));
396         $array['withdraw_fee_fix']     = TRANSLATE_COMMA(getConfig('wernis_withdraw_fee_fix'));
397 }
398
399 //
400 ?>