Wernis extension can now contact the API
[mailer.git] / inc / libs / wernis_functions.php
index c96ac9e968aa1edde34e72b8571a3f1c88be4303..8dff531395a50d113505ea7b95e4acf584e499c0 100644 (file)
  ************************************************************************/
 
 // Some security stuff...
-if (ereg(basename(__FILE__), $_SERVER['PHP_SELF']))
-{
+if (ereg(basename(__FILE__), $_SERVER['PHP_SELF'])) {
        $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
        require($INC);
 }
 
+// Sends out a request to the API and returns it's result
+function WERNIS_SEND_REQUEST ($scriptName, $requestData =  array()) {
+       global $CONFIG;
+
+       // Is the requestData an array?
+       if (!is_array($requestData)) {
+               // Then abort here!
+               return array(
+                       'status'  => "failed_general",
+                       'message' => WERNIS_API_REQUEST_DATA_INVALID
+               );
+       }
+
+       // Is the API id and MD5 hash there?
+       if ((empty($CONFIG['wernis_api_id'])) || (empty($CONFIG['wernis_api_md5']))) {
+               // Abort here...
+               return array(
+                       'status'  => "failed_general",
+                       'message' => WERNIS_API_REQUEST_DATA_MISSING
+               );
+       }
+
+       // Construct the request string
+       $requestString = $CONFIG['wernis_api_url'] . $scriptName."?api_id=".$CONFIG['wernis_api_id']."&api_key=".$CONFIG['wernis_api_md5'];
+       foreach ($requestData as $key=>$value) {
+               $requestString .= "&".$key."=".$value;
+       }
+
+       // Get the raw response from the lower function
+       $response = MXCHANGE_OPEN($requestString);
+
+       // Check the response header if all is fine
+       if (strpos($response[0], "200") === false) {
+               // Something bad happend... :(
+               return array(
+                       'status'  => "request_eror",
+                       'message' => sprintf(WERNIS_API_REQUEST_ERROR, $response[0])
+               );
+       }
+
+       // All (maybe) fine so remove the response header from server
+       $response = $response[(count($response) - 1)];
+
+       // Prepare the returning result for higher functions
+       if (substr($response, 0, 1) == "&") {
+               // Remove the leading & (which can be used in Flash)
+               $response = substr($response, 1);
+       }
+
+       // Bring back the response
+       $data = explode("=", $response);
+
+       // Default return array (should not stay empty)
+       $return = array();
+
+       // We use only the first two entries (which shall be fine)
+       if ($data[0] == "error") {
+               // The request has failed... :(
+               switch ($data[1]) {
+                       case "AUTH": // Authorization has failed
+                               $return = array(
+                                       'status'  => "auth_failed",
+                                       'message' => WERNIS_API_REQUEST_FAILED_AUTH
+                               );
+                               break;
+
+                       default: // Unknown error (maybe new?)
+                               $return = array(
+                                       'status'  => "request_failed",
+                                       'message' => sprintf(WERNIS_API_REQUEST_FAILED, $data[1])
+                               );
+                               break;
+               }
+       } else {
+               // All fine here
+               $return = array(
+                       'status'   => "OK",
+                       'response' => $response
+               );
+       }
+
+       // Return the result
+       return $return;
+}
+
+// Tests the function by calling balance.php on the API
+function WERNIS_TEST_API () {
+       // Get config first
+       global $CONFIG;
+
+       // Prepare the request to the API
+       $return = WERNIS_SEND_REQUEST("balance.php");
+
+       die("<pre>".print_r($return, true)."</pre>");
+}
+
 //
 ?>