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