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