Fixes for broken order page and themes
[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  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
17  * Needs to be in all Files and every File needs "svn propset           *
18  * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
19  * -------------------------------------------------------------------- *
20  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
21  * For more information visit: http://www.mxchange.org                  *
22  *                                                                      *
23  * This program is free software; you can redistribute it and/or modify *
24  * it under the terms of the GNU General Public License as published by *
25  * the Free Software Foundation; either version 2 of the License, or    *
26  * (at your option) any later version.                                  *
27  *                                                                      *
28  * This program is distributed in the hope that it will be useful,      *
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
31  * GNU General Public License for more details.                         *
32  *                                                                      *
33  * You should have received a copy of the GNU General Public License    *
34  * along with this program; if not, write to the Free Software          *
35  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
36  * MA  02110-1301  USA                                                  *
37  ************************************************************************/
38
39 // Some security stuff...
40 if (!defined('__SECURITY')) {
41         die();
42 }
43
44 // Sets a status message and code
45 function WERNIS_STATUS_MESSAGE ($message, $status) {
46         $GLOBALS['wernis_data']['message'] = $message;
47         $GLOBALS['wernis_data']['status']  = $status;
48 }
49
50 // Get the status message
51 function GET_WERNIS_ERROR_MESSAGE () {
52         if (isset($GLOBALS['wernis_data']['message'])) {
53                 // Use raw message
54                 return $GLOBALS['wernis_data']['message'];
55         } elseif (isset($GLOBALS['wernis_data']['status'])) {
56                 // Fall-back to status
57                 return sprintf(getMessage('WERNIS_ERROR_STATUS'), $GLOBALS['wernis_data']['status']);
58         } else {
59                 // Something bad happend
60                 return getMessage('WERNIS_UNKNOWN_ERROR');
61         }
62 }
63
64 // Get the status code
65 function GET_WERNIS_ERROR_CODE () {
66         if (isset($GLOBALS['wernis_data']['status'])) {
67                 // Use raw message
68                 return $GLOBALS['wernis_data']['status'];
69         } else {
70                 // Something bad happend
71                 return getMessage('WERNIS_UNKNOWN_ERROR');
72         }
73 }
74
75 // Sends out a request to the API and returns it's result
76 function WERNIS_SEND_REQUEST ($scriptName, $requestData =  array()) {
77         // Is the requestData an array?
78         if (!is_array($requestData)) {
79                 // Then abort here!
80                 return array(
81                         'status'  => 'failed_general',
82                         'message' => getMessage('WERNIS_API_REQUEST_DATA_INVALID')
83                 );
84         } // END - if
85
86         // Is the API id and MD5 hash there?
87         if ((getConfig('wernis_api_id') == '') || (getConfig('wernis_api_md5') == '')) {
88                 // Abort here...
89                 return array(
90                         'status'  => 'failed_general',
91                         'message' => getMessage('WERNIS_API_REQUEST_DATA_MISSING')
92                 );
93         } // END - if
94
95         // Add more request data
96         $requestData['api_id']  = getConfig('wernis_api_id');
97         $requestData['api_key'] = getConfig('wernis_api_md5');
98
99         // Construct the request string
100         $requestString = getConfig('wernis_api_url') . $scriptName;
101
102         // Get the raw response from the lower function
103         $response = sendPostRequest($requestString, $requestData);
104
105         // Check the response header if all is fine
106         if (strpos($response[0], '200') === false) {
107                 // Something bad happend... :(
108                 return array(
109                         'status'  => 'request_error',
110                         'message' => sprintf(getMessage('WERNIS_API_REQUEST_ERROR'), $response[0])
111                 );
112         } // END - if
113
114         // All (maybe) fine so remove the response header from server
115         for ($idx = (count($response) - 1); $idx > 1; $idx--) {
116                 $line = trim($response[$idx]);
117                 if (!empty($line)) {
118                         $response = $line;
119                         break;
120                 }
121         }
122
123         // Prepare the returning result for higher functions
124         if (substr($response, 0, 1) == '&') {
125                 // Remove the leading & (which can be used in Flash)
126                 $response = substr($response, 1);
127         } // END - if
128
129         // Bring back the response
130         $data = explode('=', $response);
131
132         // Default return array (should not stay empty)
133         $return = array();
134
135         // We use only the first two entries (which shall be fine)
136         if ($data[0] === 'error') {
137                 // The request has failed... :(
138                 switch ($data[1]) {
139                         case '404': // Invalid API ID
140                         case 'AUTH': // Authorization has failed
141                                 $return = array(
142                                         'status'  => 'auth_failed',
143                                         'message' => getMessage('WERNIS_API_REQUEST_FAILED_AUTH')
144                                 );
145                                 break;
146
147                         case 'LOCKED': // User account is locked!
148                         case 'PASS': // Bad passphrase entered
149                         case 'USER': // Missing account or invalid password
150                                 $return = array(
151                                         'status'  => 'user_failed',
152                                         'message' => getMessage('WERNIS_API_REQUEST_FAILED_USER')
153                                 );
154                                 break;
155
156                         case 'OWN': // Transfer to own account
157                                 $return = array(
158                                         'status'  => 'own_failed',
159                                         'message' => getMessage('WERNIS_API_REQUEST_FAILED_OWN')
160                                 );
161                                 break;
162
163                         case 'AMOUNT': // Amount is depleted
164                                 $return = array(
165                                         'status'  => 'amount_failed',
166                                         'message' => getMessage('WERNIS_API_REQUEST_FAILED_AMOUNT')
167                                 );
168                                 break;
169
170                         case 'AMOUNT-SEND': // API amount is depleted
171                                 $return = array(
172                                         'status'  => 'api_amount_failed',
173                                         'message' => getMessage('WERNIS_API_REQUEST_FAILED_API_AMOUNT')
174                                 );
175                                 break;
176
177                         default: // Unknown error (maybe new?)
178                                 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Unknown error %s from WDS66 API received.", $data[1]));
179                                 $return = array(
180                                         'status'  => 'request_failed',
181                                         'message' => sprintf(getMessage('WERNIS_API_REQUEST_FAILED'), $data[1])
182                                 );
183                                 break;
184                 }
185         } else {
186                 // All fine here
187                 $return = array(
188                         'status'   => 'OK',
189                         'response' => $response
190                 );
191         }
192
193         // Return the result
194         return $return;
195 }
196
197 // Tests the function by calling balance.php on the API
198 function WERNIS_TEST_API () {
199         // Result is always failed
200         $result = false;
201
202         // Return the result from the lower functions
203         $return = WERNIS_SEND_REQUEST('balance.php');
204
205         if ($return['status'] == 'OK') {
206                 // All fine!
207                 $result = true;
208         } else {
209                 // Status failture text
210                 WERNIS_STATUS_MESSAGE($return['message'], $return['status']);
211         }
212
213         // Return result
214         return $result;
215 }
216
217 // Widthdraw this amount
218 function WERNIS_EXECUTE_WITHDRAW ($wdsId, $userMd5, $amount) {
219         // Is the sponsor extension installed?
220         if (getConfig('wernis_withdraw_active') != 'Y') {
221                 if (!isExtensionActive('sponsor')) {
222                         // No, abort here
223                         return false;
224                 } elseif (!isSponsor()) {
225                         // No sponsor, not allowed to withdraw!
226                         return false;
227                 }
228         } // END - if
229
230         // Default is failed attempt
231         $result = false;
232
233         // Prepare the purpose
234         eval("\$purpose = \"".compileCode(sprintf(getMessage('WERNIS_API_PURPOSE_WITHDRAW'), getUserId()))."\";");
235
236         // Prepare the request data
237         $requestData = array(
238                 'sub_request' => 'receive',
239                 't_userid'    => bigintval($wdsId),
240                 't_md5'       => $userMd5,
241                 'r_userid'    => getConfig('wernis_refid'),
242                 'amount'      => bigintval($amount),
243                 'purpose'     => encodeString($purpose, false)
244         );
245
246         // Return the result from the lower functions
247         $return = WERNIS_SEND_REQUEST('book.php', $requestData);
248
249         if ($return['status'] == 'OK') {
250                 // All fine!
251                 $result = true;
252
253                 // Log the transfer
254                 WERNIS_LOG_TRANSFER($wdsId, $amount, 'IN');
255         } else {
256                 // Status failture text
257                 WERNIS_STATUS_MESSAGE($return['message'], $return['status']);
258
259                 // Log the transfer
260                 WERNIS_LOG_TRANSFER($wdsId, $amount, 'FAILED', $return['message'], $return['status']);
261         }
262
263         // Return result
264         return $result;
265 }
266
267
268 // Payout this amount
269 function WERNIS_EXECUTE_PAYOUT ($wdsId, $amount) {
270         // Default is failed attempt
271         $result = false;
272
273         // Prepare the purpose
274         eval("\$purpose = \"".compileCode(sprintf(getMessage('WERNIS_API_PURPOSE_PAYOUT'), getUserId()))."\";");
275
276         // Prepare the request data
277         $requestData = array(
278                 'sub_request' => 'send',
279                 't_userid'    => getConfig('wernis_refid'),
280                 't_md5'       => getConfig('wernis_pass_md5'),
281                 'r_userid'    => bigintval($wdsId),
282                 'amount'      => bigintval($amount),
283                 'purpose'     => encodeString($purpose, false)
284         );
285
286         // Return the result from the lower functions
287         $return = WERNIS_SEND_REQUEST('book.php', $requestData);
288
289         if ($return['status'] == 'OK') {
290                 // All fine!
291                 $result = true;
292
293                 // Log the transfer
294                 WERNIS_LOG_TRANSFER($wdsId, $amount, 'OUT');
295         } else {
296                 // Status failture text
297                 WERNIS_STATUS_MESSAGE($return['message'], $return['status']);
298
299                 // Log the transfer
300                 WERNIS_LOG_TRANSFER($wdsId, $amount, 'FAILED', $return['message'], $return['status']);
301         }
302
303         // Return result
304         return $result;
305 }
306
307 // Translate the status IN/OUT
308 function WERNIS_TRANSFER_STATUS ($status) {
309         // Default status
310         $return = sprintf(getMessage('WERNIS_STATUS_UNKNWOWN'), $status);
311         switch ($status) {
312                 case 'IN': // Withdraw
313                         $return = getMessage('WERNIS_STATUS_WITHDRAW');
314                         break;
315
316                 case 'OUT': // Payout
317                         $return = getMessage('WERNIS_STATUS_PAYOUT');
318                         break;
319
320                 case 'FAILED': // Payout
321                         $return = getMessage('WERNIS_STATUS_FAILED');
322                         break;
323         }
324
325         // Return the status
326         return $return;
327 }
328
329 // Log the transfer
330 function WERNIS_LOG_TRANSFER ($wdsId, $amount, $type = 'FAILED', $message = '', $status = '') {
331         // Register this wernis movement
332         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')",
333                 array(getUserId(), bigintval($wdsId), bigintval($amount), $type, $message, $status), __FUNCTION__, __LINE__);
334 }
335
336 // Take fees and factor
337 function WERNIS_TAKE_FEE ($points, $mode) {
338         // Payout or withdraw are allowed modes!
339         //* DEBUG: */ outputHtml("mode={$mode},points={$points}<br />");
340         if (!in_array($mode, array('payout', 'withdraw'))) {
341                 // Log error and abort
342                 logDebugMessage(__FUNCTION__, __LINE__, "userid={getUserId()},mode={$mode},points={$points}");
343                 return false;
344         } // END - if
345
346         // Is there a percentage or fixed fee?
347         if (getConfig('wernis_'.$mode.'_fee_percent') > 0) {
348                 // Percentage fee
349                 $points -= $points * getConfig('wernis_'.$mode.'_fee_percent') / 100;
350         } elseif (getConfig('wernis_'.$mode.'_fee_fix') > 0) {
351                 // Fixed fee
352                 $points -= getConfig('wernis_'.$mode.'_fee_fix');
353         }
354
355         // Divide/multiply the factor
356         if ($mode == 'payout') {
357                 // Divide for payout
358                 $points = $points / getConfig('wernis_payout_factor');
359         } else {
360                 // Multiply for withdraw
361                 $points = $points * getConfig('wernis_withdraw_factor');
362         }
363
364         // Return value
365         //* DEBUG: */ outputHtml("mode={$mode},points={$points}<br />");
366         return $points;
367 }
368
369 // Add withdraw fees and factor
370 function WERNIS_ADD_WITHDRAW_FEE ($points) {
371         // Is there a percentage or fixed fee?
372         if (getConfig('wernis_withdraw_fee_percent') > 0) {
373                 // Percentage fee
374                 $points += $points * getConfig('wernis_withdraw_fee_percent') / 100;
375         } elseif (getConfig('wernis_withdraw_fee_fix') > 0) {
376                 // Fixed fee
377                 $points += getConfig('wernis_withdraw_fee_fix');
378         }
379
380         // Return value
381         return $points;
382 }
383
384 // Add all fees to the array
385 function WERNIS_ADD_FEES_TO_ARRAY (&$array) {
386         // Is the array an array? ;-)
387         if (!is_array($array)) {
388                 // Log error and return
389                 logDebugMessage(__FUNCTION__, __LINE__, 'Type ' . gettype($array) . ' != array.');
390                 return;
391         } // END - if
392
393         // Add both factors
394         $array['payout_factor']        = translateComma(getConfig('wernis_payout_factor'));
395         $array['withdraw_factor']      = translateComma(getConfig('wernis_withdraw_factor'));
396
397         // Add all fees
398         $array['payout_fee_percent']   = translateComma(getConfig('wernis_payout_fee_percent'));
399         $array['withdraw_fee_percent'] = translateComma(getConfig('wernis_withdraw_fee_percent'));
400         $array['payout_fee_fix']       = translateComma(getConfig('wernis_payout_fee_fix'));
401         $array['withdraw_fee_fix']     = translateComma(getConfig('wernis_withdraw_fee_fix'));
402 }
403
404 //
405 ?>