Wernis extension can now contact the API
[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 // Sends out a request to the API and returns it's result
41 function WERNIS_SEND_REQUEST ($scriptName, $requestData =  array()) {
42         global $CONFIG;
43
44         // Is the requestData an array?
45         if (!is_array($requestData)) {
46                 // Then abort here!
47                 return array(
48                         'status'  => "failed_general",
49                         'message' => WERNIS_API_REQUEST_DATA_INVALID
50                 );
51         }
52
53         // Is the API id and MD5 hash there?
54         if ((empty($CONFIG['wernis_api_id'])) || (empty($CONFIG['wernis_api_md5']))) {
55                 // Abort here...
56                 return array(
57                         'status'  => "failed_general",
58                         'message' => WERNIS_API_REQUEST_DATA_MISSING
59                 );
60         }
61
62         // Construct the request string
63         $requestString = $CONFIG['wernis_api_url'] . $scriptName."?api_id=".$CONFIG['wernis_api_id']."&api_key=".$CONFIG['wernis_api_md5'];
64         foreach ($requestData as $key=>$value) {
65                 $requestString .= "&".$key."=".$value;
66         }
67
68         // Get the raw response from the lower function
69         $response = MXCHANGE_OPEN($requestString);
70
71         // Check the response header if all is fine
72         if (strpos($response[0], "200") === false) {
73                 // Something bad happend... :(
74                 return array(
75                         'status'  => "request_eror",
76                         'message' => sprintf(WERNIS_API_REQUEST_ERROR, $response[0])
77                 );
78         }
79
80         // All (maybe) fine so remove the response header from server
81         $response = $response[(count($response) - 1)];
82
83         // Prepare the returning result for higher functions
84         if (substr($response, 0, 1) == "&") {
85                 // Remove the leading & (which can be used in Flash)
86                 $response = substr($response, 1);
87         }
88
89         // Bring back the response
90         $data = explode("=", $response);
91
92         // Default return array (should not stay empty)
93         $return = array();
94
95         // We use only the first two entries (which shall be fine)
96         if ($data[0] == "error") {
97                 // The request has failed... :(
98                 switch ($data[1]) {
99                         case "AUTH": // Authorization has failed
100                                 $return = array(
101                                         'status'  => "auth_failed",
102                                         'message' => WERNIS_API_REQUEST_FAILED_AUTH
103                                 );
104                                 break;
105
106                         default: // Unknown error (maybe new?)
107                                 $return = array(
108                                         'status'  => "request_failed",
109                                         'message' => sprintf(WERNIS_API_REQUEST_FAILED, $data[1])
110                                 );
111                                 break;
112                 }
113         } else {
114                 // All fine here
115                 $return = array(
116                         'status'   => "OK",
117                         'response' => $response
118                 );
119         }
120
121         // Return the result
122         return $return;
123 }
124
125 // Tests the function by calling balance.php on the API
126 function WERNIS_TEST_API () {
127         // Get config first
128         global $CONFIG;
129
130         // Prepare the request to the API
131         $return = WERNIS_SEND_REQUEST("balance.php");
132
133         die("<pre>".print_r($return, true)."</pre>");
134 }
135
136 //
137 ?>