Added some wrappers to speed-up things
[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 - 2013 by Mailer Developer Team                   *
20  * For more information visit: http://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 '{%message,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 = sendHttpPostRequest($requestString, $requestData);
112
113         // Check the response header if all is fine
114         if (!isHttpStatusOkay($response[0])) {
115                 // Something bad happend... :(
116                 return array(
117                         'status'  => 'request_error',
118                         'message' => '{%message,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' => '{%message,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         // Only as admin
215         assert(isAdmin());
216
217         // Result is always failed
218         $result = FALSE;
219
220         // Prepare the request data
221         $requestData = array(
222                 't_uid'       => getWernisRefid(),
223                 't_md5'       => getWernisPassMd5()
224         );
225
226         // Return the result from the lower functions
227         $return = WERNIS_SEND_REQUEST('balance.php', $requestData);
228
229         // Did it went smoothly?
230         if ($return['status'] == 'OK') {
231                 // All fine!
232                 $result = TRUE;
233         } else {
234                 // Status failure text
235                 WERNIS_STATUS_MESSAGE($return['message'], $return['status']);
236         }
237
238         // Return result
239         return $result;
240 }
241
242 // Widthdraw this amount
243 function WERNIS_EXECUTE_WITHDRAW ($wdsId, $userMd5, $amount) {
244         // Is the sponsor extension installed?
245         if (!isWernisWithdrawActive()) {
246                 if (!isExtensionActive('sponsor')) {
247                         // No, abort here
248                         return FALSE;
249                 } elseif (!isSponsor()) {
250                         // No sponsor, not allowed to withdraw!
251                         return FALSE;
252                 }
253         } // END - if
254
255         // Default is failed attempt
256         $result = FALSE;
257
258         // Prepare the request data
259         $requestData = array(
260                 'sub_request' => 'receive',
261                 't_uid'       => bigintval($wdsId),
262                 't_md5'       => $userMd5,
263                 'r_uid'       => getWernisRefid(),
264                 'amount'      => bigintval($amount),
265                 'purpose'     => getMaskedMessage('WERNIS_API_PURPOSE_WITHDRAW', getMemberId())
266         );
267
268         // Return the result from the lower functions
269         $return = WERNIS_SEND_REQUEST('book.php', $requestData);
270
271         if ($return['status'] == 'OK') {
272                 // All fine!
273                 $result = TRUE;
274
275                 // Log the transfer
276                 WERNIS_LOG_TRANSFER($wdsId, $amount, 'WITHDRAW');
277         } else {
278                 // Status failure text
279                 WERNIS_STATUS_MESSAGE($return['message'], $return['status']);
280
281                 // Log the transfer
282                 WERNIS_LOG_TRANSFER($wdsId, $amount, 'FAILED', $return['message'], $return['status']);
283         }
284
285         // Return result
286         return $result;
287 }
288
289
290 // Payout this amount
291 function WERNIS_EXECUTE_PAYOUT ($wdsId, $amount) {
292         // Default is failed attempt
293         $result = FALSE;
294
295         // Prepare the request data
296         $requestData = array(
297                 'sub_request' => 'send',
298                 't_uid'       => getWernisRefid(),
299                 't_md5'       => getWernisPassMd5(),
300                 'r_uid'       => bigintval($wdsId),
301                 'amount'      => bigintval($amount),
302                 'purpose'     => getMaskedMessage('WERNIS_API_PURPOSE_PAYOUT', getMemberId())
303         );
304
305         // Return the result from the lower functions
306         $return = WERNIS_SEND_REQUEST('book.php', $requestData);
307
308         if ($return['status'] == 'OK') {
309                 // All fine!
310                 $result = TRUE;
311
312                 // Log the transfer
313                 WERNIS_LOG_TRANSFER($wdsId, $amount, 'PAYOUT');
314         } else {
315                 // Status failure text
316                 WERNIS_STATUS_MESSAGE($return['message'], $return['status']);
317
318                 // Log the transfer
319                 WERNIS_LOG_TRANSFER($wdsId, $amount, 'FAILED', $return['message'], $return['status']);
320         }
321
322         // Return result
323         return $result;
324 }
325
326 // Translate the status IN/OUT
327 function translateWernisTransferStatus ($status) {
328         // Default status is unknown
329         $return = '{%message,WERNIS_STATUS_UNKNWOWN=' . $status . '%}';
330
331         // Construct message id
332         $messageId = 'WERNIS_STATUS_' . $status;
333
334         // Is it there?
335         if (isMessageIdValid($messageId)) {
336                 // Then use it as message string
337                 $return = '{--' . $messageId . '--}';
338         } // END - if
339
340         // Return the status
341         return $return;
342 }
343
344 // Log the transfer
345 function WERNIS_LOG_TRANSFER ($wdsId, $amount, $type = 'FAILED', $message = '', $status = '') {
346         // Register this wernis movement
347         sqlQueryEscaped("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')",
348                 array(
349                         getMemberId(),
350                         bigintval($wdsId),
351                         bigintval($amount),
352                         $type,
353                         $message,
354                         $status
355                 ), __FUNCTION__, __LINE__);
356 }
357
358 // Take fees and factor
359 function WERNIS_TAKE_FEE ($points, $mode) {
360         // Payout or withdraw are allowed modes!
361         //* DEBUG: */ debugOutput('mode=' . $mode . ',points=' . $points);
362         if (!in_array($mode, array('payout', 'withdraw'))) {
363                 // Log error and abort
364                 logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . getMemberId() . ',mode=' . $mode . ',points=' . $points . ' - unknown mode detected.');
365                 return FALSE;
366         } // END - if
367
368         // Is there a percentage or fixed fee?
369         if (getConfig('wernis_' . $mode . '_fee_percent') > 0) {
370                 // Percentage fee
371                 $points -= $points * getConfig('wernis_'.$mode.'_fee_percent') / 100;
372         } elseif (getConfig('wernis_' . $mode . '_fee_fix') > 0) {
373                 // Fixed fee
374                 $points -= getConfig('wernis_' . $mode . '_fee_fix');
375         }
376
377         // Divide/multiply the factor
378         if ($mode == 'payout') {
379                 // Divide for payout
380                 $points = $points / getWernisPayoutFactor();
381         } else {
382                 // Multiply for withdraw
383                 $points = $points * getWernisWithdrawFactor();
384         }
385
386         // Return value
387         //* DEBUG: */ debugOutput('mode=' . $mode . ',points=' . $points);
388         return $points;
389 }
390
391 // Add withdraw fees and factor
392 function WERNIS_ADD_WITHDRAW_FEE ($points) {
393         // Is there a percentage or fixed fee?
394         if (getWernisWithdrawFeePercent() > 0) {
395                 // Percentage fee
396                 $points += $points * getWernisWithdrawFeePercent() / 100;
397         } elseif (getWernisWithdrawFeeFix() > 0) {
398                 // Fixed fee
399                 $points += getWernisWithdrawFeeFix();
400         }
401
402         // Return value
403         return $points;
404 }
405
406 //-----------------------------------------------------------------------------
407 //                             Wrapper functions
408 //-----------------------------------------------------------------------------
409
410 // Wrapper function for 'wernis_refid'
411 function getWernisRefid () {
412         // Is there cache?
413         if (!isset($GLOBALS[__FUNCTION__])) {
414                 // Get config entry
415                 $GLOBALS[__FUNCTION__] = getConfig('wernis_refid');
416         } // END - if
417
418         // Return cache
419         return $GLOBALS[__FUNCTION__];
420 }
421
422 // Wrapper function for 'wernis_pass_md5'
423 function getWernisPassMd5 () {
424         // Is there cache?
425         if (!isset($GLOBALS[__FUNCTION__])) {
426                 // Get config entry
427                 $GLOBALS[__FUNCTION__] = getConfig('wernis_pass_md5');
428         } // END - if
429
430         // Return cache
431         return $GLOBALS[__FUNCTION__];
432 }
433
434 // Wrapper function for 'wernis_api_id'
435 function getWernisApiId () {
436         // Is there cache?
437         if (!isset($GLOBALS[__FUNCTION__])) {
438                 // Get config entry
439                 $GLOBALS[__FUNCTION__] = getConfig('wernis_api_id');
440         } // END - if
441
442         // Return cache
443         return $GLOBALS[__FUNCTION__];
444 }
445
446 // Wrapper function for 'wernis_api_md5'
447 function getWernisApiMd5 () {
448         // Is there cache?
449         if (!isset($GLOBALS[__FUNCTION__])) {
450                 // Get config entry
451                 $GLOBALS[__FUNCTION__] = getConfig('wernis_api_md5');
452         } // END - if
453
454         // Return cache
455         return $GLOBALS[__FUNCTION__];
456 }
457
458 // Wrapper function for 'wernis_api_url'
459 function getWernisApiUrl () {
460         // Is there cache?
461         if (!isset($GLOBALS[__FUNCTION__])) {
462                 // Get config entry
463                 $GLOBALS[__FUNCTION__] = getConfig('wernis_api_url');
464         } // END - if
465
466         // Return cache
467         return $GLOBALS[__FUNCTION__];
468 }
469
470 // Wrapper function for 'wernis_withdraw_active'
471 function getWernisWithdrawActive () {
472         // Is there cache?
473         if (!isset($GLOBALS[__FUNCTION__])) {
474                 // Get config entry
475                 $GLOBALS[__FUNCTION__] = getConfig('wernis_withdraw_active');
476         } // END - if
477
478         // Return cache
479         return $GLOBALS[__FUNCTION__];
480 }
481
482 // Wrapper function for 'wernis_payout_active'
483 function getWernisPayoutActive () {
484         // Is there cache?
485         if (!isset($GLOBALS[__FUNCTION__])) {
486                 // Get config entry
487                 $GLOBALS[__FUNCTION__] = getConfig('wernis_payout_active');
488         } // END - if
489
490         // Return cache
491         return $GLOBALS[__FUNCTION__];
492 }
493
494 // Wrapper function for 'wernis_withdraw_active'
495 function isWernisWithdrawActive () {
496         // Is there cache?
497         if (!isset($GLOBALS[__FUNCTION__])) {
498                 // Get config entry
499                 $GLOBALS[__FUNCTION__] = (getConfig('wernis_withdraw_active') == 'Y');
500         } // END - if
501
502         // Return cache
503         return $GLOBALS[__FUNCTION__];
504 }
505
506 // Wrapper function for 'wernis_payout_active'
507 function isWernisPayoutActive () {
508         // Is there cache?
509         if (!isset($GLOBALS[__FUNCTION__])) {
510                 // Get config entry
511                 $GLOBALS[__FUNCTION__] = (getConfig('wernis_payout_active') == 'Y');
512         } // END - if
513
514         // Return cache
515         return $GLOBALS[__FUNCTION__];
516 }
517
518 // Wrapper function for 'wernis_withdraw_factor'
519 function getWernisWithdrawFactor () {
520         // Is there cache?
521         if (!isset($GLOBALS[__FUNCTION__])) {
522                 // Get config entry
523                 $GLOBALS[__FUNCTION__] = getConfig('wernis_withdraw_factor');
524         } // END - if
525
526         // Return cache
527         return $GLOBALS[__FUNCTION__];
528 }
529
530 // Wrapper function for 'wernis_payout_factor'
531 function getWernisPayoutFactor () {
532         // Is there cache?
533         if (!isset($GLOBALS[__FUNCTION__])) {
534                 // Get config entry
535                 $GLOBALS[__FUNCTION__] = getConfig('wernis_payout_factor');
536         } // END - if
537
538         // Return cache
539         return $GLOBALS[__FUNCTION__];
540 }
541
542 // Wrapper function for 'wernis_withdraw_fee_percent'
543 function getWernisWithdrawFeePercent () {
544         // Is there cache?
545         if (!isset($GLOBALS[__FUNCTION__])) {
546                 // Get config entry
547                 $GLOBALS[__FUNCTION__] = getConfig('wernis_withdraw_fee_percent');
548         } // END - if
549
550         // Return cache
551         return $GLOBALS[__FUNCTION__];
552 }
553
554 // Wrapper function for 'wernis_withdraw_fee_fix'
555 function getWernisWithdrawFeeFix () {
556         // Is there cache?
557         if (!isset($GLOBALS[__FUNCTION__])) {
558                 // Get config entry
559                 $GLOBALS[__FUNCTION__] = getConfig('wernis_withdraw_fee_fix');
560         } // END - if
561
562         // Return cache
563         return $GLOBALS[__FUNCTION__];
564 }
565
566 // Wrapper function for 'wernis_payout_fee_percent'
567 function getWernisPayoutFeePercent () {
568         // Is there cache?
569         if (!isset($GLOBALS[__FUNCTION__])) {
570                 // Get config entry
571                 $GLOBALS[__FUNCTION__] = getConfig('wernis_payout_fee_percent');
572         } // END - if
573
574         // Return cache
575         return $GLOBALS[__FUNCTION__];
576 }
577
578 // Wrapper function for 'wernis_payout_fee_fix'
579 function getWernisPayoutFeeFix () {
580         // Is there cache?
581         if (!isset($GLOBALS[__FUNCTION__])) {
582                 // Get config entry
583                 $GLOBALS[__FUNCTION__] = getConfig('wernis_payout_fee_fix');
584         } // END - if
585
586         // Return cache
587         return $GLOBALS[__FUNCTION__];
588 }
589
590 // Wrapper function for 'wernis_min_payout'
591 function getWernisMinPayout () {
592         // Is there cache?
593         if (!isset($GLOBALS[__FUNCTION__])) {
594                 // Get config entry
595                 $GLOBALS[__FUNCTION__] = getConfig('wernis_min_payout');
596         } // END - if
597
598         // Return cache
599         return $GLOBALS[__FUNCTION__];
600 }
601
602 // Wrapper function for 'wernis_min_withdraw'
603 function getWernisMinWithdraw () {
604         // Is there cache?
605         if (!isset($GLOBALS[__FUNCTION__])) {
606                 // Get config entry
607                 $GLOBALS[__FUNCTION__] = getConfig('wernis_min_withdraw');
608         } // END - if
609
610         // Return cache
611         return $GLOBALS[__FUNCTION__];
612 }
613
614 // [EOF]
615 ?>