Double question mark fixed
[mailer.git] / inc / libs / wernis_functions.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                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  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
17  * -------------------------------------------------------------------- *
18  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
19  * Copyright (c) 2009 - 2011 by Mailer Developer Team                   *
20  * For more information visit: http://www.mxchange.org                  *
21  *                                                                      *
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.                                  *
26  *                                                                      *
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.                         *
31  *                                                                      *
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,               *
35  * MA  02110-1301  USA                                                  *
36  ************************************************************************/
37
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
40         die();
41 } // END - if
42
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;
47 }
48
49 // Get the status message
50 function GET_WERNIS_ERROR_MESSAGE () {
51         if (isset($GLOBALS['wernis_data']['message'])) {
52                 // Use raw message
53                 return $GLOBALS['wernis_data']['message'];
54         } elseif (isset($GLOBALS['wernis_data']['status'])) {
55                 // Fall-back to status
56                 return getMaskedMessage('WERNIS_ERROR_STATUS', $GLOBALS['wernis_data']['status']);
57         } else {
58                 // Something bad happend
59                 return '{--WERNIS_UNKNOWN_ERROR--}';
60         }
61 }
62
63 // Get the status code
64 function GET_WERNIS_ERROR_CODE () {
65         if (isset($GLOBALS['wernis_data']['status'])) {
66                 // Use raw message
67                 return $GLOBALS['wernis_data']['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 ((getWernisApiId() == '') || (getWernisApiMd5() == '')) {
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']  = getWernisApiId();
96         $requestData['api_key'] = getWernisApiMd5();
97
98         // Is a purpose there?
99         if (isset($requestData['purpose'])) {
100                 // Eval the purpose
101                 eval('$purpose = "' . doFinalCompilation($requestData['purpose'], false) . '";');
102
103                 // Prepare the purpose, it needs encoding
104                 $requestData['purpose'] = encodeString($purpose);
105         } // END - if
106
107         // Construct the request string
108         $requestString = getWernisApiUrl() . $scriptName;
109
110         // Get the raw response from the lower function
111         $response = sendPostRequest($requestString, $requestData);
112
113         // Check the response header if all is fine
114         if (strpos($response[0], '200') === false) {
115                 // Something bad happend... :(
116                 return array(
117                         'status'  => 'request_error',
118                         'message' => getMaskedMessage('WERNIS_API_REQUEST_ERROR', $response[0])
119                 );
120         } // END - if
121
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]);
126                 if (!empty($line)) {
127                         $responseLine = $line;
128                         break;
129                 } // END - if
130         } // END - for
131
132         // Is the response leaded by a & symbol?
133         if (substr($responseLine, 0, 1) != '&') {
134                 // Something badly happened on server-side
135                 return array(
136                         'status'  => 'request_problem',
137                         'message' => sprintf(getMessage('WERNIS_API_REQUEST_PROBLEM'), $response[0], secureString($responseLine))
138                 );
139         } // END - if
140
141         // Remove the leading & (which can be used in Flash)
142         $responseLine = substr($responseLine, 1);
143
144         // Bring back the response
145         $data = explode('=', $responseLine);
146
147         // Default return array (should not stay empty)
148         $return = array();
149
150         // We use only the first two entries (which shall be fine)
151         if ($data[0] === 'error') {
152                 // The request has failed... :(
153                 switch ($data[1]) {
154                         case '404': // Invalid API id
155                         case 'AUTH': // Authorization has failed
156                                 $return = array(
157                                         'status'  => 'auth_failed',
158                                         'message' => '{--WERNIS_API_REQUEST_FAILED_AUTH--}'
159                                 );
160                                 break;
161
162                         case 'LOCKED': // User account is locked!
163                         case 'PASS': // Bad passphrase entered
164                         case 'USER': // Missing account or invalid password
165                                 $return = array(
166                                         'status'  => 'user_failed',
167                                         'message' => '{--WERNIS_API_REQUEST_FAILED_USER--}'
168                                 );
169                                 break;
170
171                         case 'OWN': // Transfer to own account
172                                 $return = array(
173                                         'status'  => 'own_failed',
174                                         'message' => '{--WERNIS_API_REQUEST_FAILED_OWN--}'
175                                 );
176                                 break;
177
178                         case 'AMOUNT': // Amount is depleted
179                                 $return = array(
180                                         'status'  => 'amount_failed',
181                                         'message' => '{--WERNIS_API_REQUEST_FAILED_AMOUNT--}'
182                                 );
183                                 break;
184
185                         case 'AMOUNT-SEND': // API amount is depleted
186                                 $return = array(
187                                         'status'  => 'api_amount_failed',
188                                         'message' => '{--WERNIS_API_REQUEST_FAILED_API_AMOUNT--}'
189                                 );
190                                 break;
191
192                         default: // Unknown error (maybe new?)
193                                 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Unknown error %s from WDS66 API received.", $data[1]));
194                                 $return = array(
195                                         'status'  => 'request_failed',
196                                         'message' => getMaskedMessage('WERNIS_API_REQUEST_FAILED', $data[1])
197                                 );
198                                 break;
199                 }
200         } else {
201                 // All fine here
202                 $return = array(
203                         'status'   => 'OK',
204                         'response' => $responseLine
205                 );
206         }
207
208         // Return the result
209         return $return;
210 }
211
212 // Tests the function by calling balance.php on the API
213 function WERNIS_TEST_API () {
214         // Result is always failed
215         $result = false;
216
217         // Return the result from the lower functions
218         $return = WERNIS_SEND_REQUEST('balance.php');
219
220         // Did it went smoothly?
221         if ($return['status'] == 'OK') {
222                 // All fine!
223                 $result = true;
224         } else {
225                 // Status failture text
226                 WERNIS_STATUS_MESSAGE($return['message'], $return['status']);
227         }
228
229         // Return result
230         return $result;
231 }
232
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')) {
238                         // No, abort here
239                         return false;
240                 } elseif (!isSponsor()) {
241                         // No sponsor, not allowed to withdraw!
242                         return false;
243                 }
244         } // END - if
245
246         // Default is failed attempt
247         $result = false;
248
249         // Prepare the request data
250         $requestData = array(
251                 'sub_request' => 'receive',
252                 't_uid'       => bigintval($wdsId),
253                 't_md5'       => $userMd5,
254                 'r_uid'       => getWernisRefid(),
255                 'amount'      => bigintval($amount),
256                 'purpose'     => getMaskedMessage('WERNIS_API_PURPOSE_WITHDRAW', getMemberId())
257         );
258
259         // Return the result from the lower functions
260         $return = WERNIS_SEND_REQUEST('book.php', $requestData);
261
262         if ($return['status'] == 'OK') {
263                 // All fine!
264                 $result = true;
265
266                 // Log the transfer
267                 WERNIS_LOG_TRANSFER($wdsId, $amount, 'IN');
268         } else {
269                 // Status failture text
270                 WERNIS_STATUS_MESSAGE($return['message'], $return['status']);
271
272                 // Log the transfer
273                 WERNIS_LOG_TRANSFER($wdsId, $amount, 'FAILED', $return['message'], $return['status']);
274         }
275
276         // Return result
277         return $result;
278 }
279
280
281 // Payout this amount
282 function WERNIS_EXECUTE_PAYOUT ($wdsId, $amount) {
283         // Default is failed attempt
284         $result = false;
285
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())
294         );
295
296         // Return the result from the lower functions
297         $return = WERNIS_SEND_REQUEST('book.php', $requestData);
298
299         if ($return['status'] == 'OK') {
300                 // All fine!
301                 $result = true;
302
303                 // Log the transfer
304                 WERNIS_LOG_TRANSFER($wdsId, $amount, 'OUT');
305         } else {
306                 // Status failture text
307                 WERNIS_STATUS_MESSAGE($return['message'], $return['status']);
308
309                 // Log the transfer
310                 WERNIS_LOG_TRANSFER($wdsId, $amount, 'FAILED', $return['message'], $return['status']);
311         }
312
313         // Return result
314         return $result;
315 }
316
317 // Translate the status IN/OUT
318 function translateWernisTransferStatus ($status) {
319         // Default status
320         $return = getMaskedMessage('WERNIS_STATUS_UNKNWOWN', $status);
321
322         // Which status?
323         switch ($status) {
324                 case 'IN': // Withdraw
325                         $return = '{--WERNIS_STATUS_WITHDRAW--}';
326                         break;
327
328                 case 'OUT': // Payout
329                         $return = '{--WERNIS_STATUS_PAYOUT--}';
330                         break;
331
332                 case 'FAILED': // Payout
333                         $return = '{--WERNIS_STATUS_FAILED--}';
334                         break;
335         } // END - switch
336
337         // Return the status
338         return $return;
339 }
340
341 // Log the transfer
342 function WERNIS_LOG_TRANSFER ($wdsId, $amount, $type = 'FAILED', $message = '', $status = '') {
343         // Register this wernis movement
344         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')",
345                 array(
346                         getMemberId(),
347                         bigintval($wdsId),
348                         bigintval($amount),
349                         $type,
350                         $message,
351                         $status
352                 ), __FUNCTION__, __LINE__);
353 }
354
355 // Take fees and factor
356 function WERNIS_TAKE_FEE ($points, $mode) {
357         // Payout or withdraw are allowed modes!
358         //* DEBUG: */ debugOutput('mode='.$mode.',points='.$points);
359         if (!in_array($mode, array('payout', 'withdraw'))) {
360                 // Log error and abort
361                 logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . getMemberId() . ',mode=' . $mode . ',points=' . $points);
362                 return false;
363         } // END - if
364
365         // Is there a percentage or fixed fee?
366         if (getConfig('wernis_' . $mode . '_fee_percent') > 0) {
367                 // Percentage fee
368                 $points -= $points * getConfig('wernis_'.$mode.'_fee_percent') / 100;
369         } elseif (getConfig('wernis_' . $mode . '_fee_fix') > 0) {
370                 // Fixed fee
371                 $points -= getConfig('wernis_' . $mode . '_fee_fix');
372         }
373
374         // Divide/multiply the factor
375         if ($mode == 'payout') {
376                 // Divide for payout
377                 $points = $points / getWernisPayoutFactor();
378         } else {
379                 // Multiply for withdraw
380                 $points = $points * getWernisWithdrawFactor();
381         }
382
383         // Return value
384         //* DEBUG: */ debugOutput('mode=' . $mode . ',points=' . $points);
385         return $points;
386 }
387
388 // Add withdraw fees and factor
389 function WERNIS_ADD_WITHDRAW_FEE ($points) {
390         // Is there a percentage or fixed fee?
391         if (getWernisWithdrawFeePercent() > 0) {
392                 // Percentage fee
393                 $points += $points * getWernisWithdrawFeePercent() / 100;
394         } elseif (getWernisWithdrawFeeFix() > 0) {
395                 // Fixed fee
396                 $points += getWernisWithdrawFeeFix();
397         }
398
399         // Return value
400         return $points;
401 }
402
403 // ---------------------------------------------------------------------------
404 //                             Wrapper functions
405 // ---------------------------------------------------------------------------
406
407 // Wrapper function for 'wernis_refid'
408 function getWernisRefid () {
409         // Do we have cache?
410         if (!isset($GLOBALS[__FUNCTION__])) {
411                 // Get config entry
412                 $GLOBALS[__FUNCTION__] = getConfig('wernis_refid');
413         } // END - if
414
415         // Return cache
416         return $GLOBALS[__FUNCTION__];
417 }
418
419 // Wrapper function for 'wernis_pass_md5'
420 function getWernisPassMd5 () {
421         // Do we have cache?
422         if (!isset($GLOBALS[__FUNCTION__])) {
423                 // Get config entry
424                 $GLOBALS[__FUNCTION__] = getConfig('wernis_pass_md5');
425         } // END - if
426
427         // Return cache
428         return $GLOBALS[__FUNCTION__];
429 }
430
431 // Wrapper function for 'wernis_api_id'
432 function getWernisApiId () {
433         // Do we have cache?
434         if (!isset($GLOBALS[__FUNCTION__])) {
435                 // Get config entry
436                 $GLOBALS[__FUNCTION__] = getConfig('wernis_api_id');
437         } // END - if
438
439         // Return cache
440         return $GLOBALS[__FUNCTION__];
441 }
442
443 // Wrapper function for 'wernis_api_md5'
444 function getWernisApiMd5 () {
445         // Do we have cache?
446         if (!isset($GLOBALS[__FUNCTION__])) {
447                 // Get config entry
448                 $GLOBALS[__FUNCTION__] = getConfig('wernis_api_md5');
449         } // END - if
450
451         // Return cache
452         return $GLOBALS[__FUNCTION__];
453 }
454
455 // Wrapper function for 'wernis_api_url'
456 function getWernisApiUrl () {
457         // Do we have cache?
458         if (!isset($GLOBALS[__FUNCTION__])) {
459                 // Get config entry
460                 $GLOBALS[__FUNCTION__] = getConfig('wernis_api_url');
461         } // END - if
462
463         // Return cache
464         return $GLOBALS[__FUNCTION__];
465 }
466
467 // Wrapper function for 'wernis_withdraw_active'
468 function getWernisWithdrawActive () {
469         // Do we have cache?
470         if (!isset($GLOBALS[__FUNCTION__])) {
471                 // Get config entry
472                 $GLOBALS[__FUNCTION__] = getConfig('wernis_withdraw_active');
473         } // END - if
474
475         // Return cache
476         return $GLOBALS[__FUNCTION__];
477 }
478
479 // Wrapper function for 'wernis_payout_active'
480 function getWernisPayoutActive () {
481         // Do we have cache?
482         if (!isset($GLOBALS[__FUNCTION__])) {
483                 // Get config entry
484                 $GLOBALS[__FUNCTION__] = getConfig('wernis_payout_active');
485         } // END - if
486
487         // Return cache
488         return $GLOBALS[__FUNCTION__];
489 }
490
491 // Wrapper function for 'wernis_withdraw_active'
492 function isWernisWithdrawActive () {
493         // Do we have cache?
494         if (!isset($GLOBALS[__FUNCTION__])) {
495                 // Get config entry
496                 $GLOBALS[__FUNCTION__] = (getConfig('wernis_withdraw_active') == 'Y');
497         } // END - if
498
499         // Return cache
500         return $GLOBALS[__FUNCTION__];
501 }
502
503 // Wrapper function for 'wernis_payout_active'
504 function isWernisPayoutActive () {
505         // Do we have cache?
506         if (!isset($GLOBALS[__FUNCTION__])) {
507                 // Get config entry
508                 $GLOBALS[__FUNCTION__] = (getConfig('wernis_payout_active') == 'Y');
509         } // END - if
510
511         // Return cache
512         return $GLOBALS[__FUNCTION__];
513 }
514
515 // Wrapper function for 'wernis_withdraw_factor'
516 function getWernisWithdrawFactor () {
517         // Do we have cache?
518         if (!isset($GLOBALS[__FUNCTION__])) {
519                 // Get config entry
520                 $GLOBALS[__FUNCTION__] = getConfig('wernis_withdraw_factor');
521         } // END - if
522
523         // Return cache
524         return $GLOBALS[__FUNCTION__];
525 }
526
527 // Wrapper function for 'wernis_payout_factor'
528 function getWernisPayoutFactor () {
529         // Do we have cache?
530         if (!isset($GLOBALS[__FUNCTION__])) {
531                 // Get config entry
532                 $GLOBALS[__FUNCTION__] = getConfig('wernis_payout_factor');
533         } // END - if
534
535         // Return cache
536         return $GLOBALS[__FUNCTION__];
537 }
538
539 // Wrapper function for 'wernis_withdraw_fee_percent'
540 function getWernisWithdrawFeePercent () {
541         // Do we have cache?
542         if (!isset($GLOBALS[__FUNCTION__])) {
543                 // Get config entry
544                 $GLOBALS[__FUNCTION__] = getConfig('wernis_withdraw_fee_percent');
545         } // END - if
546
547         // Return cache
548         return $GLOBALS[__FUNCTION__];
549 }
550
551 // Wrapper function for 'wernis_withdraw_fee_fix'
552 function getWernisWithdrawFeeFix () {
553         // Do we have cache?
554         if (!isset($GLOBALS[__FUNCTION__])) {
555                 // Get config entry
556                 $GLOBALS[__FUNCTION__] = getConfig('wernis_withdraw_fee_fix');
557         } // END - if
558
559         // Return cache
560         return $GLOBALS[__FUNCTION__];
561 }
562
563 // [EOF]
564 ?>