New wrapper function introduced, more EL rewrites:
[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  * 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  * Copyright (c) 2009, 2010 by Mailer Developer Team                    *
22  * For more information visit: http://www.mxchange.org                  *
23  *                                                                      *
24  * This program is free software; you can redistribute it and/or modify *
25  * it under the terms of the GNU General Public License as published by *
26  * the Free Software Foundation; either version 2 of the License, or    *
27  * (at your option) any later version.                                  *
28  *                                                                      *
29  * This program is distributed in the hope that it will be useful,      *
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
32  * GNU General Public License for more details.                         *
33  *                                                                      *
34  * You should have received a copy of the GNU General Public License    *
35  * along with this program; if not, write to the Free Software          *
36  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
37  * MA  02110-1301  USA                                                  *
38  ************************************************************************/
39
40 // Some security stuff...
41 if (!defined('__SECURITY')) {
42         die();
43 } // END - if
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 getMaskedMessage('WERNIS_ERROR_STATUS', $GLOBALS['wernis_data']['status']);
59         } else {
60                 // Something bad happend
61                 return '{--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 '{--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' => '{--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' => '{--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' => getMaskedMessage('WERNIS_API_REQUEST_ERROR', $response[0])
112                 );
113         } // END - if
114
115         // All (maybe) fine so remove the response header from server
116         $responseLine = '*INVALID*';
117         for ($idx = (count($response) - 1); $idx > 1; $idx--) {
118                 $line = trim($response[$idx]);
119                 if (!empty($line)) {
120                         $responseLine = $line;
121                         break;
122                 } // END - if
123         } // END - for
124
125         // Is the response leaded by a & symbol?
126         if (substr($responseLine, 0, 1) != '&') {
127                 // Something badly happened on server-side
128                 return array(
129                         'status'  => 'request_problem',
130                         'message' => sprintf(getMessage('WERNIS_API_REQUEST_PROBLEM'), $response[0], secureString($responseLine))
131                 );
132         } // END - if
133
134         // Remove the leading & (which can be used in Flash)
135         $responseLine = substr($responseLine, 1);
136
137         // Bring back the response
138         $data = explode('=', $responseLine);
139
140         // Default return array (should not stay empty)
141         $return = array();
142
143         // We use only the first two entries (which shall be fine)
144         if ($data[0] === 'error') {
145                 // The request has failed... :(
146                 switch ($data[1]) {
147                         case '404': // Invalid API id
148                         case 'AUTH': // Authorization has failed
149                                 $return = array(
150                                         'status'  => 'auth_failed',
151                                         'message' => '{--WERNIS_API_REQUEST_FAILED_AUTH--}'
152                                 );
153                                 break;
154
155                         case 'LOCKED': // User account is locked!
156                         case 'PASS': // Bad passphrase entered
157                         case 'USER': // Missing account or invalid password
158                                 $return = array(
159                                         'status'  => 'user_failed',
160                                         'message' => '{--WERNIS_API_REQUEST_FAILED_USER--}'
161                                 );
162                                 break;
163
164                         case 'OWN': // Transfer to own account
165                                 $return = array(
166                                         'status'  => 'own_failed',
167                                         'message' => '{--WERNIS_API_REQUEST_FAILED_OWN--}'
168                                 );
169                                 break;
170
171                         case 'AMOUNT': // Amount is depleted
172                                 $return = array(
173                                         'status'  => 'amount_failed',
174                                         'message' => '{--WERNIS_API_REQUEST_FAILED_AMOUNT--}'
175                                 );
176                                 break;
177
178                         case 'AMOUNT-SEND': // API amount is depleted
179                                 $return = array(
180                                         'status'  => 'api_amount_failed',
181                                         'message' => '{--WERNIS_API_REQUEST_FAILED_API_AMOUNT--}'
182                                 );
183                                 break;
184
185                         default: // Unknown error (maybe new?)
186                                 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Unknown error %s from WDS66 API received.", $data[1]));
187                                 $return = array(
188                                         'status'  => 'request_failed',
189                                         'message' => getMaskedMessage('WERNIS_API_REQUEST_FAILED', $data[1])
190                                 );
191                                 break;
192                 }
193         } else {
194                 // All fine here
195                 $return = array(
196                         'status'   => 'OK',
197                         'response' => $responseLine
198                 );
199         }
200
201         // Return the result
202         return $return;
203 }
204
205 // Tests the function by calling balance.php on the API
206 function WERNIS_TEST_API () {
207         // Result is always failed
208         $result = false;
209
210         // Return the result from the lower functions
211         $return = WERNIS_SEND_REQUEST('balance.php');
212
213         if ($return['status'] == 'OK') {
214                 // All fine!
215                 $result = true;
216         } else {
217                 // Status failture text
218                 WERNIS_STATUS_MESSAGE($return['message'], $return['status']);
219         }
220
221         // Return result
222         return $result;
223 }
224
225 // Widthdraw this amount
226 function WERNIS_EXECUTE_WITHDRAW ($wdsId, $userMd5, $amount) {
227         // Is the sponsor extension installed?
228         if (getConfig('wernis_withdraw_active') != 'Y') {
229                 if (!isExtensionActive('sponsor')) {
230                         // No, abort here
231                         return false;
232                 } elseif (!isSponsor()) {
233                         // No sponsor, not allowed to withdraw!
234                         return false;
235                 }
236         } // END - if
237
238         // Default is failed attempt
239         $result = false;
240
241         // Prepare the purpose
242         eval('$purpose = "' . preCompileCode(getMaskedMessage('WERNIS_API_PURPOSE_WITHDRAW', getMemberId())) . '";');
243
244         // Prepare the request data
245         $requestData = array(
246                 'sub_request' => 'receive',
247                 't_uid'       => bigintval($wdsId),
248                 't_md5'       => $userMd5,
249                 'r_uid'       => getConfig('wernis_refid'),
250                 'amount'      => bigintval($amount),
251                 'purpose'     => encodeString($purpose, false)
252         );
253
254         // Return the result from the lower functions
255         $return = WERNIS_SEND_REQUEST('book.php', $requestData);
256
257         if ($return['status'] == 'OK') {
258                 // All fine!
259                 $result = true;
260
261                 // Log the transfer
262                 WERNIS_LOG_TRANSFER($wdsId, $amount, 'IN');
263         } else {
264                 // Status failture text
265                 WERNIS_STATUS_MESSAGE($return['message'], $return['status']);
266
267                 // Log the transfer
268                 WERNIS_LOG_TRANSFER($wdsId, $amount, 'FAILED', $return['message'], $return['status']);
269         }
270
271         // Return result
272         return $result;
273 }
274
275
276 // Payout this amount
277 function WERNIS_EXECUTE_PAYOUT ($wdsId, $amount) {
278         // Default is failed attempt
279         $result = false;
280
281         // Prepare the purpose
282         eval('$purpose = "' . preCompileCode(getMaskedMessage('WERNIS_API_PURPOSE_PAYOUT', getMemberId())) . '";');
283
284         // Prepare the request data
285         $requestData = array(
286                 'sub_request' => 'send',
287                 't_uid'       => getConfig('wernis_refid'),
288                 't_md5'       => getConfig('wernis_pass_md5'),
289                 'r_uid'       => bigintval($wdsId),
290                 'amount'      => bigintval($amount),
291                 'purpose'     => encodeString($purpose, false)
292         );
293
294         // Return the result from the lower functions
295         $return = WERNIS_SEND_REQUEST('book.php', $requestData);
296
297         if ($return['status'] == 'OK') {
298                 // All fine!
299                 $result = true;
300
301                 // Log the transfer
302                 WERNIS_LOG_TRANSFER($wdsId, $amount, 'OUT');
303         } else {
304                 // Status failture text
305                 WERNIS_STATUS_MESSAGE($return['message'], $return['status']);
306
307                 // Log the transfer
308                 WERNIS_LOG_TRANSFER($wdsId, $amount, 'FAILED', $return['message'], $return['status']);
309         }
310
311         // Return result
312         return $result;
313 }
314
315 // Translate the status IN/OUT
316 function translateWernisTransferStatus ($status) {
317         // Default status
318         $return = getMaskedMessage('WERNIS_STATUS_UNKNWOWN', $status);
319         switch ($status) {
320                 case 'IN': // Withdraw
321                         $return = '{--WERNIS_STATUS_WITHDRAW--}';
322                         break;
323
324                 case 'OUT': // Payout
325                         $return = '{--WERNIS_STATUS_PAYOUT--}';
326                         break;
327
328                 case 'FAILED': // Payout
329                         $return = '{--WERNIS_STATUS_FAILED--}';
330                         break;
331         }
332
333         // Return the status
334         return $return;
335 }
336
337 // Log the transfer
338 function WERNIS_LOG_TRANSFER ($wdsId, $amount, $type = 'FAILED', $message = '', $status = '') {
339         // Register this wernis movement
340         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')",
341                 array(getMemberId(), bigintval($wdsId), bigintval($amount), $type, $message, $status), __FUNCTION__, __LINE__);
342 }
343
344 // Take fees and factor
345 function WERNIS_TAKE_FEE ($points, $mode) {
346         // Payout or withdraw are allowed modes!
347         //* DEBUG: */ debugOutput('mode='.$mode.',points='.$points);
348         if (!in_array($mode, array('payout', 'withdraw'))) {
349                 // Log error and abort
350                 logDebugMessage(__FUNCTION__, __LINE__, "userid={getMemberId()},mode={$mode},points={$points}");
351                 return false;
352         } // END - if
353
354         // Is there a percentage or fixed fee?
355         if (getConfig('wernis_'.$mode.'_fee_percent') > 0) {
356                 // Percentage fee
357                 $points -= $points * getConfig('wernis_'.$mode.'_fee_percent') / 100;
358         } elseif (getConfig('wernis_'.$mode.'_fee_fix') > 0) {
359                 // Fixed fee
360                 $points -= getConfig('wernis_'.$mode.'_fee_fix');
361         }
362
363         // Divide/multiply the factor
364         if ($mode == 'payout') {
365                 // Divide for payout
366                 $points = $points / getConfig('wernis_payout_factor');
367         } else {
368                 // Multiply for withdraw
369                 $points = $points * getConfig('wernis_withdraw_factor');
370         }
371
372         // Return value
373         //* DEBUG: */ debugOutput('mode='.$mode.',points='.$points);
374         return $points;
375 }
376
377 // Add withdraw fees and factor
378 function WERNIS_ADD_WITHDRAW_FEE ($points) {
379         // Is there a percentage or fixed fee?
380         if (getConfig('wernis_withdraw_fee_percent') > 0) {
381                 // Percentage fee
382                 $points += $points * getConfig('wernis_withdraw_fee_percent') / 100;
383         } elseif (getConfig('wernis_withdraw_fee_fix') > 0) {
384                 // Fixed fee
385                 $points += getConfig('wernis_withdraw_fee_fix');
386         }
387
388         // Return value
389         return $points;
390 }
391
392 // [EOF]
393 ?>