e665277234890a9f6feb7218f93b080a7049c9c1
[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         }
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         }
95
96         // Construct the request string
97         $requestString = $_CONFIG['wernis_api_url'] . $scriptName."?api_id=".$_CONFIG['wernis_api_id']."&api_key=".$_CONFIG['wernis_api_md5'];
98         foreach ($requestData as $key=>$value) {
99                 $requestString .= "&".$key."=".$value;
100         }
101
102         // Get the raw response from the lower function
103         $response = MXCHANGE_OPEN($requestString);
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(WERNIS_API_REQUEST_ERROR, $response[0])
111                 );
112         }
113
114         // All (maybe) fine so remove the response header from server
115         $response = $response[(count($response) - 1)];
116
117         // Prepare the returning result for higher functions
118         if (substr($response, 0, 1) == "&") {
119                 // Remove the leading & (which can be used in Flash)
120                 $response = substr($response, 1);
121         }
122
123         // Bring back the response
124         $data = explode("=", $response);
125
126         // Default return array (should not stay empty)
127         $return = array();
128
129         // We use only the first two entries (which shall be fine)
130         if ($data[0] === "error") {
131                 // The request has failed... :(
132                 switch ($data[1]) {
133                         case "404": // Invalid API ID
134                         case "AUTH": // Authorization has failed
135                                 $return = array(
136                                         'status'  => "auth_failed",
137                                         'message' => WERNIS_API_REQUEST_FAILED_AUTH
138                                 );
139                                 break;
140
141                         case "LOCKED": // User account is locked!
142                         case "PASS": // Bad passphrase entered
143                         case "USER": // Missing account or invalid password
144                                 $return = array(
145                                         'status'  => "user_failed",
146                                         'message' => WERNIS_API_REQUEST_FAILED_USER
147                                 );
148                                 break;
149
150                         case "OWN": // Transfer to own account
151                                 $return = array(
152                                         'status'  => "own_failed",
153                                         'message' => WERNIS_API_REQUEST_FAILED_OWN
154                                 );
155                                 break;
156
157                         case "AMOUNT": // Amount is depleted
158                                 $return = array(
159                                         'status'  => "amount_failed",
160                                         'message' => WERNIS_API_REQUEST_FAILED_AMOUNT
161                                 );
162                                 break;
163
164                         case "AMOUNT-SEND": // API amount is depleted
165                                 $return = array(
166                                         'status'  => "api_amount_failed",
167                                         'message' => WERNIS_API_REQUEST_FAILED_API_AMOUNT
168                                 );
169                                 break;
170
171                         default: // Unknown error (maybe new?)
172                                 $return = array(
173                                         'status'  => "request_failed",
174                                         'message' => sprintf(WERNIS_API_REQUEST_FAILED, $data[1])
175                                 );
176                                 break;
177                 }
178         } else {
179                 // All fine here
180                 $return = array(
181                         'status'   => "OK",
182                         'response' => $response
183                 );
184         }
185
186         // Return the result
187         return $return;
188 }
189
190 // Tests the function by calling balance.php on the API
191 function WERNIS_TEST_API () {
192         // Get config first
193         global $_CONFIG;
194         $result = false;
195
196         // Return the result from the lower functions
197         $return = WERNIS_SEND_REQUEST("balance.php");
198
199         if ($return['status'] == "OK") {
200                 // All fine!
201                 $result = true;
202         } else {
203                 // Status failture text
204                 WERNIS_STATUS_MESSAGE($return['message'], $return['status']);
205         }
206
207         // Return result
208         return $result;
209 }
210
211 // Widthdraw this amount
212 function WERNIS_EXECUTE_WITHDRAW ($wdsId, $userMd5, $amount) {
213         global $_CONFIG;
214         $result = false;
215
216         // Prepare the purpose
217         $eval = "\$purpose = \"".COMPILE_CODE(WERNIS_API_PURPOSE_WITHDRAW)."\";";
218         eval($eval);
219
220         // Prepare the request data
221         $requestData = array(
222                 'sub_request'   => "receive",
223                 't_uid'                 => bigintval($wdsId),
224                 't_md5'                 => $userMd5,
225                 'r_uid'                 => $_CONFIG['wernis_refid'],
226                 'amount'                => bigintval($amount),
227                 'purpose'               => urlencode(base64_encode($purpose))
228         );
229
230         // Return the result from the lower functions
231         $return = WERNIS_SEND_REQUEST("book.php", $requestData);
232
233         if ($return['status'] == "OK") {
234                 // All fine!
235                 $result = true;
236
237                 // Log the transfer
238                 WERNIS_LOG_TRANSFER($wdsId, $amount, 'IN');
239         } else {
240                 // Status failture text
241                 WERNIS_STATUS_MESSAGE($return['message'], $return['status']);
242
243                 // Log the transfer
244                 WERNIS_LOG_TRANSFER($wdsId, $amount, 'FAILED', $return['message'], $return['status']);
245         }
246
247         // Return result
248         return $result;
249 }
250
251
252 // Payout this amount
253 function WERNIS_EXECUTE_PAYOUT ($wdsId, $userMd5, $amount) {
254         global $_CONFIG;
255         $result = false;
256
257         // Prepare the purpose
258         $eval = "\$purpose = \"".COMPILE_CODE(WERNIS_API_PURPOSE_PAYOUT)."\";";
259         eval($eval);
260
261         // Prepare the request data
262         $requestData = array(
263                 'sub_request'   => "send",
264                 't_uid'                 => bigintval($wdsId),
265                 't_md5'                 => $userMd5,
266                 'r_uid'                 => $_CONFIG['wernis_refid'],
267                 'amount'                => bigintval($amount),
268                 'purpose'               => urlencode(base64_encode($purpose))
269         );
270
271         // Return the result from the lower functions
272         $return = WERNIS_SEND_REQUEST("book.php", $requestData);
273
274         if ($return['status'] == "OK") {
275                 // All fine!
276                 $result = true;
277
278                 // Log the transfer
279                 WERNIS_LOG_TRANSFER($wdsId, $amount, 'OUT');
280         } else {
281                 // Status failture text
282                 WERNIS_STATUS_MESSAGE($return['message'], $return['status']);
283
284                 // Log the transfer
285                 WERNIS_LOG_TRANSFER($wdsId, $amount, 'FAILED', $return['message'], $return['status']);
286         }
287
288         // Return result
289         return $result;
290 }
291
292 // Translate the status IN/OUT
293 function WERNIS_TRANSFER_STATUS ($status) {
294         // Default status
295         $return = sprintf(WERNIS_STATUS_UNKNWOWN, $status);
296         switch ($status) {
297                 case "IN": // Withdraw
298                         $return = WERNIS_STATUS_WITHDRAW;
299                         break;
300
301                 case "OUT": // Payout
302                         $return = WERNIS_STATUS_PAYOUT;
303                         break;
304
305                 case "FAILED": // Payout
306                         $return = WERNIS_STATUS_FAILED;
307                         break;
308         }
309
310         // Return the status
311         return $return;
312 }
313
314 function WERNIS_LOG_TRANSFER ($wdsId, $amount, $type = 'FAILED', $message = null, $status = null) {
315         // Register this wernis movement
316         $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')",
317                 array($GLOBALS['userid'], bigintval($wdsId), bigintval($amount), $type, $message, $status), __FILE__, __LINE__);
318 }
319
320 //
321 ?>