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