4114845e9cdbc75227cfd28a2c180e353228fd21
[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 (ereg(basename(__FILE__), $_SERVER['PHP_SELF'])) {
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         global $WERNIS;
43         $WERNIS['message'] = $msg;
44         $WERNIS['status'] = $status;
45 }
46
47 // Get the status message
48 function GET_WERNIS_ERROR_MESSAGE () {
49         global $WERNIS;
50         if (isset($WERNIS['message'])) {
51                 // Use raw message
52                 return $WERNIS['message'];
53         } elseif (isset($WERNIS['status'])) {
54                 // Fall-back to status
55                 return sprintf(WERNIS_ERROR_STATUS, $WERNIS['status']);
56         } else {
57                 // Something bad happend
58                 return WERNIS_UNKNOWN_ERROR;
59         }
60 }
61
62 // Get the status code
63 function GET_WERNIS_ERROR_CODE () {
64         global $WERNIS;
65         if (isset($WERNIS['status'])) {
66                 // Use raw message
67                 return $WERNIS['status'];
68         } else {
69                 // Something bad happend
70                 return WERNIS_UNKNOWN_ERROR;
71         }
72 }
73
74 // Sends out a request to the API and returns it's result
75 function WERNIS_SEND_REQUEST ($scriptName, $requestData =  array()) {
76         global $_CONFIG;
77
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 ((empty($_CONFIG['wernis_api_id'])) || (empty($_CONFIG['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']  = bigintval($_CONFIG['wernis_api_id']);
98         $requestData['api_key'] = $_CONFIG['wernis_api_key'];
99
100         // Construct the request string
101         $requestString = $_CONFIG['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                                 $return = array(
174                                         'status'  => "request_failed",
175                                         'message' => sprintf(WERNIS_API_REQUEST_FAILED, $data[1])
176                                 );
177                                 break;
178                 }
179         } else {
180                 // All fine here
181                 $return = array(
182                         'status'   => "OK",
183                         'response' => $response
184                 );
185         }
186
187         // Return the result
188         return $return;
189 }
190
191 // Tests the function by calling balance.php on the API
192 function WERNIS_TEST_API () {
193         // Get config first
194         global $_CONFIG;
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         global $_CONFIG;
215         $result = false;
216
217         // Prepare the purpose
218         $eval = "\$purpose = \"".COMPILE_CODE(WERNIS_API_PURPOSE_WITHDRAW)."\";";
219         eval($eval);
220
221         // Prepare the request data
222         $requestData = array(
223                 'sub_request'   => "receive",
224                 't_uid'                 => bigintval($wdsId),
225                 't_md5'                 => $userMd5,
226                 'r_uid'                 => $_CONFIG['wernis_refid'],
227                 'amount'                => bigintval($amount),
228                 'purpose'               => urlencode(base64_encode($purpose))
229         );
230
231         // Return the result from the lower functions
232         $return = WERNIS_SEND_REQUEST("book.php", $requestData);
233
234         if ($return['status'] == "OK") {
235                 // All fine!
236                 $result = true;
237
238                 // Log the transfer
239                 WERNIS_LOG_TRANSFER($wdsId, $amount, 'IN');
240         } else {
241                 // Status failture text
242                 WERNIS_STATUS_MESSAGE($return['message'], $return['status']);
243
244                 // Log the transfer
245                 WERNIS_LOG_TRANSFER($wdsId, $amount, 'FAILED', $return['message'], $return['status']);
246         }
247
248         // Return result
249         return $result;
250 }
251
252
253 // Payout this amount
254 function WERNIS_EXECUTE_PAYOUT ($wdsId, $userMd5, $amount) {
255         global $_CONFIG;
256         $result = false;
257
258         // Prepare the purpose
259         $eval = "\$purpose = \"".COMPILE_CODE(WERNIS_API_PURPOSE_PAYOUT)."\";";
260         eval($eval);
261
262         // Prepare the request data
263         $requestData = array(
264                 'sub_request'   => "send",
265                 't_uid'                 => bigintval($wdsId),
266                 't_md5'                 => $userMd5,
267                 'r_uid'                 => $_CONFIG['wernis_refid'],
268                 'amount'                => bigintval($amount),
269                 'purpose'               => urlencode(base64_encode($purpose))
270         );
271
272         // Return the result from the lower functions
273         $return = WERNIS_SEND_REQUEST("book.php", $requestData);
274
275         if ($return['status'] == "OK") {
276                 // All fine!
277                 $result = true;
278
279                 // Log the transfer
280                 WERNIS_LOG_TRANSFER($wdsId, $amount, 'OUT');
281         } else {
282                 // Status failture text
283                 WERNIS_STATUS_MESSAGE($return['message'], $return['status']);
284
285                 // Log the transfer
286                 WERNIS_LOG_TRANSFER($wdsId, $amount, 'FAILED', $return['message'], $return['status']);
287         }
288
289         // Return result
290         return $result;
291 }
292
293 // Translate the status IN/OUT
294 function WERNIS_TRANSFER_STATUS ($status) {
295         // Default status
296         $return = sprintf(WERNIS_STATUS_UNKNWOWN, $status);
297         switch ($status) {
298                 case "IN": // Withdraw
299                         $return = WERNIS_STATUS_WITHDRAW;
300                         break;
301
302                 case "OUT": // Payout
303                         $return = WERNIS_STATUS_PAYOUT;
304                         break;
305
306                 case "FAILED": // Payout
307                         $return = WERNIS_STATUS_FAILED;
308                         break;
309         }
310
311         // Return the status
312         return $return;
313 }
314
315 function WERNIS_LOG_TRANSFER ($wdsId, $amount, $type = 'FAILED', $message = null, $status = null) {
316         // Register this wernis movement
317         $result = 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')",
318                 array($GLOBALS['userid'], bigintval($wdsId), bigintval($amount), $type, $message, $status), __FILE__, __LINE__);
319 }
320
321 //
322 ?>