Missing SVN properties set
[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  * 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         $responseLine = '*INVALID*';
116         for ($idx = (count($response) - 1); $idx > 1; $idx--) {
117                 $line = trim($response[$idx]);
118                 if (!empty($line)) {
119                         $responseLine = $line;
120                         break;
121                 } // END - if
122         } // END - for
123
124         // Is the response leaded by a & symbol?
125         if (substr($responseLine, 0, 1) != '&') {
126                 // Something badly happened on server-side
127                 return array(
128                         'status'  => 'request_problem',
129                         'message' => sprintf(getMessage('WERNIS_API_REQUEST_PROBLEM'), $response[0], secureString($responseLine))
130                 );
131         } // END - if
132
133         // Remove the leading & (which can be used in Flash)
134         $responseLine = substr($responseLine, 1);
135
136         // Bring back the response
137         $data = explode('=', $responseLine);
138
139         // Default return array (should not stay empty)
140         $return = array();
141
142         // We use only the first two entries (which shall be fine)
143         if ($data[0] === 'error') {
144                 // The request has failed... :(
145                 switch ($data[1]) {
146                         case '404': // Invalid API id
147                         case 'AUTH': // Authorization has failed
148                                 $return = array(
149                                         'status'  => 'auth_failed',
150                                         'message' => getMessage('WERNIS_API_REQUEST_FAILED_AUTH')
151                                 );
152                                 break;
153
154                         case 'LOCKED': // User account is locked!
155                         case 'PASS': // Bad passphrase entered
156                         case 'USER': // Missing account or invalid password
157                                 $return = array(
158                                         'status'  => 'user_failed',
159                                         'message' => getMessage('WERNIS_API_REQUEST_FAILED_USER')
160                                 );
161                                 break;
162
163                         case 'OWN': // Transfer to own account
164                                 $return = array(
165                                         'status'  => 'own_failed',
166                                         'message' => getMessage('WERNIS_API_REQUEST_FAILED_OWN')
167                                 );
168                                 break;
169
170                         case 'AMOUNT': // Amount is depleted
171                                 $return = array(
172                                         'status'  => 'amount_failed',
173                                         'message' => getMessage('WERNIS_API_REQUEST_FAILED_AMOUNT')
174                                 );
175                                 break;
176
177                         case 'AMOUNT-SEND': // API amount is depleted
178                                 $return = array(
179                                         'status'  => 'api_amount_failed',
180                                         'message' => getMessage('WERNIS_API_REQUEST_FAILED_API_AMOUNT')
181                                 );
182                                 break;
183
184                         default: // Unknown error (maybe new?)
185                                 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Unknown error %s from WDS66 API received.", $data[1]));
186                                 $return = array(
187                                         'status'  => 'request_failed',
188                                         'message' => sprintf(getMessage('WERNIS_API_REQUEST_FAILED'), $data[1])
189                                 );
190                                 break;
191                 }
192         } else {
193                 // All fine here
194                 $return = array(
195                         'status'   => 'OK',
196                         'response' => $responseLine
197                 );
198         }
199
200         // Return the result
201         return $return;
202 }
203
204 // Tests the function by calling balance.php on the API
205 function WERNIS_TEST_API () {
206         // Result is always failed
207         $result = false;
208
209         // Return the result from the lower functions
210         $return = WERNIS_SEND_REQUEST('balance.php');
211
212         if ($return['status'] == 'OK') {
213                 // All fine!
214                 $result = true;
215         } else {
216                 // Status failture text
217                 WERNIS_STATUS_MESSAGE($return['message'], $return['status']);
218         }
219
220         // Return result
221         return $result;
222 }
223
224 // Widthdraw this amount
225 function WERNIS_EXECUTE_WITHDRAW ($wdsId, $userMd5, $amount) {
226         // Is the sponsor extension installed?
227         if (getConfig('wernis_withdraw_active') != 'Y') {
228                 if (!isExtensionActive('sponsor')) {
229                         // No, abort here
230                         return false;
231                 } elseif (!isSponsor()) {
232                         // No sponsor, not allowed to withdraw!
233                         return false;
234                 }
235         } // END - if
236
237         // Default is failed attempt
238         $result = false;
239
240         // Prepare the purpose
241         eval("\$purpose = \"".compileRawCode(sprintf(getMessage('WERNIS_API_PURPOSE_WITHDRAW'), getMemberId()))."\";");
242
243         // Prepare the request data
244         $requestData = array(
245                 'sub_request' => 'receive',
246                 't_uid'       => bigintval($wdsId),
247                 't_md5'       => $userMd5,
248                 'r_uid'       => getConfig('wernis_refid'),
249                 'amount'      => bigintval($amount),
250                 'purpose'     => encodeString($purpose, false)
251         );
252
253         // Return the result from the lower functions
254         $return = WERNIS_SEND_REQUEST('book.php', $requestData);
255
256         if ($return['status'] == 'OK') {
257                 // All fine!
258                 $result = true;
259
260                 // Log the transfer
261                 WERNIS_LOG_TRANSFER($wdsId, $amount, 'IN');
262         } else {
263                 // Status failture text
264                 WERNIS_STATUS_MESSAGE($return['message'], $return['status']);
265
266                 // Log the transfer
267                 WERNIS_LOG_TRANSFER($wdsId, $amount, 'FAILED', $return['message'], $return['status']);
268         }
269
270         // Return result
271         return $result;
272 }
273
274
275 // Payout this amount
276 function WERNIS_EXECUTE_PAYOUT ($wdsId, $amount) {
277         // Default is failed attempt
278         $result = false;
279
280         // Prepare the purpose
281         eval("\$purpose = \"".compileRawCode(sprintf(getMessage('WERNIS_API_PURPOSE_PAYOUT'), getMemberId()))."\";");
282
283         // Prepare the request data
284         $requestData = array(
285                 'sub_request' => 'send',
286                 't_uid'       => getConfig('wernis_refid'),
287                 't_md5'       => getConfig('wernis_pass_md5'),
288                 'r_uid'       => bigintval($wdsId),
289                 'amount'      => bigintval($amount),
290                 'purpose'     => encodeString($purpose, false)
291         );
292
293         // Return the result from the lower functions
294         $return = WERNIS_SEND_REQUEST('book.php', $requestData);
295
296         if ($return['status'] == 'OK') {
297                 // All fine!
298                 $result = true;
299
300                 // Log the transfer
301                 WERNIS_LOG_TRANSFER($wdsId, $amount, 'OUT');
302         } else {
303                 // Status failture text
304                 WERNIS_STATUS_MESSAGE($return['message'], $return['status']);
305
306                 // Log the transfer
307                 WERNIS_LOG_TRANSFER($wdsId, $amount, 'FAILED', $return['message'], $return['status']);
308         }
309
310         // Return result
311         return $result;
312 }
313
314 // Translate the status IN/OUT
315 function WERNIS_TRANSFER_STATUS ($status) {
316         // Default status
317         $return = sprintf(getMessage('WERNIS_STATUS_UNKNWOWN'), $status);
318         switch ($status) {
319                 case 'IN': // Withdraw
320                         $return = getMessage('WERNIS_STATUS_WITHDRAW');
321                         break;
322
323                 case 'OUT': // Payout
324                         $return = getMessage('WERNIS_STATUS_PAYOUT');
325                         break;
326
327                 case 'FAILED': // Payout
328                         $return = getMessage('WERNIS_STATUS_FAILED');
329                         break;
330         }
331
332         // Return the status
333         return $return;
334 }
335
336 // Log the transfer
337 function WERNIS_LOG_TRANSFER ($wdsId, $amount, $type = 'FAILED', $message = '', $status = '') {
338         // Register this wernis movement
339         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')",
340                 array(getMemberId(), bigintval($wdsId), bigintval($amount), $type, $message, $status), __FUNCTION__, __LINE__);
341 }
342
343 // Take fees and factor
344 function WERNIS_TAKE_FEE ($points, $mode) {
345         // Payout or withdraw are allowed modes!
346         //* DEBUG: */ outputHtml("mode={$mode},points={$points}<br />");
347         if (!in_array($mode, array('payout', 'withdraw'))) {
348                 // Log error and abort
349                 logDebugMessage(__FUNCTION__, __LINE__, "userid={getMemberId()},mode={$mode},points={$points}");
350                 return false;
351         } // END - if
352
353         // Is there a percentage or fixed fee?
354         if (getConfig('wernis_'.$mode.'_fee_percent') > 0) {
355                 // Percentage fee
356                 $points -= $points * getConfig('wernis_'.$mode.'_fee_percent') / 100;
357         } elseif (getConfig('wernis_'.$mode.'_fee_fix') > 0) {
358                 // Fixed fee
359                 $points -= getConfig('wernis_'.$mode.'_fee_fix');
360         }
361
362         // Divide/multiply the factor
363         if ($mode == 'payout') {
364                 // Divide for payout
365                 $points = $points / getConfig('wernis_payout_factor');
366         } else {
367                 // Multiply for withdraw
368                 $points = $points * getConfig('wernis_withdraw_factor');
369         }
370
371         // Return value
372         //* DEBUG: */ outputHtml("mode={$mode},points={$points}<br />");
373         return $points;
374 }
375
376 // Add withdraw fees and factor
377 function WERNIS_ADD_WITHDRAW_FEE ($points) {
378         // Is there a percentage or fixed fee?
379         if (getConfig('wernis_withdraw_fee_percent') > 0) {
380                 // Percentage fee
381                 $points += $points * getConfig('wernis_withdraw_fee_percent') / 100;
382         } elseif (getConfig('wernis_withdraw_fee_fix') > 0) {
383                 // Fixed fee
384                 $points += getConfig('wernis_withdraw_fee_fix');
385         }
386
387         // Return value
388         return $points;
389 }
390
391 // Add all fees to the array
392 function WERNIS_ADD_FEES_TO_ARRAY (&$array) {
393         // Is the array an array? ;-)
394         if (!is_array($array)) {
395                 // Log error and return
396                 logDebugMessage(__FUNCTION__, __LINE__, 'Type ' . gettype($array) . ' != array.');
397                 return;
398         } // END - if
399
400         // Add both factors
401         $array['payout_factor']        = translateComma(getConfig('wernis_payout_factor'));
402         $array['withdraw_factor']      = translateComma(getConfig('wernis_withdraw_factor'));
403
404         // Add all fees
405         $array['payout_fee_percent']   = translateComma(getConfig('wernis_payout_fee_percent'));
406         $array['withdraw_fee_percent'] = translateComma(getConfig('wernis_withdraw_fee_percent'));
407         $array['payout_fee_fix']       = translateComma(getConfig('wernis_payout_fee_fix'));
408         $array['withdraw_fee_fix']     = translateComma(getConfig('wernis_withdraw_fee_fix'));
409 }
410
411 //
412 ?>