class_WernisApi.php

Go to the documentation of this file.
00001 <?php
00024 class WernisApi extends BaseFrameworkSystem {
00028         private static $apiUrl = "http://www.wds66.com/api/";
00029 
00033         private $wernis_amount = 0;
00034 
00038         private $w_id = 0;
00039 
00043         private $w_md5 = "";
00044 
00048         private $w_nick = "";
00049 
00053         private $w_amount = 0;
00054 
00058         private $statusArray = array();
00059 
00065         protected function __construct () {
00066                 // Call parent constructor
00067                 parent::__construct(__CLASS__);
00068 
00069                 // Clean up a little
00070                 $this->removeNumberFormaters();
00071                 $this->removeSystemArray();
00072         }
00073 
00080         public final static function createWernisApi (array $cfg) {
00081                 // Create a new instance
00082                 $apiInstance = new WernisApi();
00083 
00084                 // Fix missing
00085                 if (!isset($cfg['api_url'])) $cfg['api_url'] = self::$apiUrl;
00086 
00087                 // Konfiguration uebertragen
00088                 $apiInstance->setCoonfigArray($cfg);
00089 
00090                 // Return the instance
00091                 return $apiInstance;
00092         }
00093 
00101         public function setUser ($w_id, $w_pwd) {
00102                 // Set username (id)
00103                 $this->w_id = $w_id;
00104 
00105                 // Hash clear password and set it
00106                 $this->w_md5 = md5($w_pwd);
00107         }
00108 
00109         /************************************************
00110          * The following methods are not yet rewritten! *
00111          ************************************************/
00112 
00113         public function einziehen ($amount) {
00114                 // amount auf Gueltigkeit pruefen
00115                 $amount = isset($amount) ? $amount+0 : 0;
00116 
00117                 if ($amount < $this->config['mineinsatz']) {
00118                         $this->setStatusMessage('low_stakes', sprintf("Dein Einsatz muss mindestens %d Wernis betragen.", $this->config['mineinsatz']));
00119                         return false;
00120                 }
00121 
00122                 // Abfrage senden
00123                 return $this->executeWithdraw($amount);
00124         }
00125 
00126         public function verschicken ($amount) {
00127                 // amount auf Gueltigkeit pruefen
00128                 $amount = isset($amount) ? $amount+0 : 0;
00129 
00130                 if ($amount < $this->config['mineinsatz']) {
00131                         $this->setStatusMessage('low_stakes', sprintf("Dein Einsatz muss mindestens %d Wernis betragen.", $this->config['mineinsatz']));
00132                         return false;
00133                 }
00134 
00135                 // Abfrage senden
00136                 return $this->executePayout($amount);
00137         }
00138 
00139         // Script abbrechen mit Zurueck-Buttom
00140         public function ende () {
00141                 global $_CONFIG;
00142                 include "templates/zurueck.html";
00143                 include "templates/fuss.html";
00144                 die();
00145         }
00146 
00147         // Fehlermeldung ausgeben und beenden
00148         public function error () {
00149                 print "<div class=\"fehler\">Fehler im Spiel: ".$this->getErrorMessage()."<div><br />\n";
00150                 $this->ende();
00151         }
00152 
00153         // Sets a status message and code
00154         public function setStatusMessage ($msg, $status) {
00155                 $this->statusArray['message'] = $msg;
00156                 $this->statusArray['status'] = $status;
00157         }
00158 
00159         // Get the status message
00160         public function getErrorMessage () {
00161                 if (isset($this->statusArray['message'])) {
00162                         // Use raw message
00163                         return $this->statusArray['message'];
00164                 } else {
00165                         // Fall-back to status
00166                         return sprintf("Fehler-Code <u>%s</u> ist keiner Nachricht zugewiesen.", $this->getErrorCode());
00167                 }
00168         }
00169 
00170         // Get the status code
00171         public function getErrorCode () {
00172                 if (isset($this->statusArray['status'])) {
00173                         // Use raw message
00174                         return $this->statusArray['status'];
00175                 } else {
00176                         // Something bad happend
00177                         return 'unknown';
00178                 }
00179         }
00180 
00181         // Sends out a request to the API and returns it's result
00182         private function sendRequest ($scriptName, array $requestData =  array()) {
00183                 // Is the requestData an array?
00184                 if (!is_array($requestData)) {
00185                         // Then abort here!
00186                         return array(
00187                                 'status'  => "failed_general",
00188                                 'message' => "API-Daten in <strong>config</strong> sind ung&uuml;ltig!"
00189                         );
00190                 }
00191 
00192                 // Is the API id and MD5 hash there?
00193                 if ((empty($this->config['wernis_api_id'])) || (empty($this->config['wernis_api_key']))) {
00194                         // Abort here...
00195                         return array(
00196                                 'status'  => "failed_general",
00197                                 'message' => "API-Daten in config.php sind leer!"
00198                         );
00199                 }
00200 
00201                 // Construct the request string
00202                 $requestString = $this->config['api_url'] . $scriptName."?api_id=".$this->config['wernis_api_id']."&api_key=".$this->config['wernis_api_key'];
00203                 foreach ($requestData as $key => $value) {
00204                         $requestString .= "&".$key."=".$value;
00205                 }
00206 
00207                 // Get the raw response from the lower function
00208                 $response = $this->sendRawRequest($requestString);
00209 
00210                 // Check the response header if all is fine
00211                 if (strpos($response[0], "200") === false) {
00212                         // Something bad happend... :(
00213                         return array(
00214                                 'status'  => "request_error",
00215                                 'message' => sprintf("Servermeldung <u>%s</u> von WDS66-API erhalten.", $response[0])
00216                         );
00217                 }
00218 
00219                 // All (maybe) fine so remove the response header from server
00220                 $response = $response[(count($response) - 1)];
00221 
00222                 // Prepare the returning result for higher functions
00223                 if (substr($response, 0, 1) == "&") {
00224                         // Remove the leading & (which can be used in Flash)
00225                         $response = substr($response, 1);
00226                 }
00227 
00228                 // Bring back the response
00229                 $data = explode("=", $response);
00230 
00231                 // Default return array (should not stay empty)
00232                 $return = array();
00233 
00234                 // We use only the first two entries (which shall be fine)
00235                 if ($data[0] === "error") {
00236                         // The request has failed... :(
00237                         switch ($data[1]) {
00238                                 case "404": // Invalid API ID
00239                                 case "AUTH": // Authorization has failed
00240                                         $return = array(
00241                                                 'status'  => "auth_failed",
00242                                                 'message' => "API-Daten scheinen nicht zu stimmen! (Access Denied)"
00243                                         );
00244                                         break;
00245 
00246                                 case "LOCKED": // User account is locked!
00247                                 case "PASS":   // Bad passphrase entered
00248                                 case "USER":   // Missing account or invalid password
00249                                         $return = array(
00250                                                 'status'  => "user_failed",
00251                                                 'message' => "Dein eingegebener WDS66-Username stimmt nicht, ist gesperrt oder du hast ein falsches Passwort eingegeben."
00252                                         );
00253                                         break;
00254 
00255                                 case "OWN": // Transfer to own account
00256                                         $return = array(
00257                                                 'status'  => "own_failed",
00258                                                 'message' => "Du darfst dein eigenes Spiel leider nicht spielen."
00259                                         );
00260                                         break;
00261 
00262                                 case "AMOUNT": // Amount is depleted
00263                                         $return = array(
00264                                                 'status'  => "amount_failed",
00265                                                 'message' => "Dein Guthaben reicht nicht aus, um das Spiel zu spielen."
00266                                         );
00267                                         break;
00268 
00269                                 case "AMOUNT-SEND": // API amount is depleted
00270                                         $return = array(
00271                                                 'status'  => "api_amount_failed",
00272                                                 'message' => "Nicht gen&uuml;gend Guthaben auf dem API-Account."
00273                                         );
00274                                         break;
00275 
00276                                 default: // Unknown error (maybe new?)
00277                                         $return = array(
00278                                                 'status'  => "request_failed",
00279                                                 'message' => sprintf("Unbekannter Fehler <u>%s</u> von API erhalten.", $data[1])
00280                                         );
00281                                         break;
00282                         }
00283                 } else {
00284                         // All fine here
00285                         $return = array(
00286                                 'status'   => "OK",
00287                                 'response' => $response
00288                         );
00289                 }
00290 
00291                 // Return the result
00292                 return $return;
00293         }
00294 
00295         // Widthdraw this amount
00296         private function executeWithdraw ($amount) {
00297                 // First all fails...
00298                 $result = false;
00299 
00300                 // Prepare the purpose
00301                 $purpose = "\"Bube oder Dame\"-Einsatz gesetzt.";
00302 
00303                 // Prepare the request data
00304                 $requestData = array(
00305                         'sub_request'   => "receive",
00306                         't_uid'                 => $this->w_id,
00307                         't_md5'                 => $this->w_md5,
00308                         'r_uid'                 => (int)$this->config['wernis_refid'],
00309                         'amount'                => (int)$amount,
00310                         'purpose'               => urlencode(base64_encode($purpose))
00311                 );
00312 
00313                 // Return the result from the lower functions
00314                 $return = $this->sendRequest("book.php", $requestData);
00315 
00316                 if ($return['status'] == "OK") {
00317                         // All fine!
00318                         $result = true;
00319                 } else {
00320                         // Status failture text
00321                         $this->setStatusMessage($return['message'], $return['status']);
00322                 }
00323 
00324                 // Return result
00325                 return $result;
00326         }
00327 
00328         // Payout this amount
00329         private function executePayout ($amount) {
00330                 // First all fails...
00331                 $result = false;
00332 
00333                 // Prepare the purpose
00334                 $purpose = "\"Bube oder Dame\"-Gewinn erhalten.";
00335 
00336                 // Prepare the request data
00337                 $requestData = array(
00338                         'sub_request'   => "send",
00339                         't_uid'                 => $this->w_id,
00340                         't_md5'                 => $this->w_md5,
00341                         'r_uid'                 => (int)$this->config['wernis_refid'],
00342                         'amount'                => (int)$amount,
00343                         'purpose'               => urlencode(base64_encode($purpose))
00344                 );
00345 
00346                 // Return the result from the lower functions
00347                 $return = $this->sendRequest("book.php", $requestData);
00348 
00349                 if ($return['status'] == "OK") {
00350                         // All fine!
00351                         $result = true;
00352                 } else {
00353                         // Status failture text
00354                         $this->setStatusMessage($return['message'], $return['status']);
00355                 }
00356 
00357                 // Return result
00358                 return $result;
00359         }
00360 
00361         // Send raw GET request
00362         private function sendRawRequest ($script) {
00363                 // Use the hostname from script URL as new hostname
00364                 $url = substr($script, 7);
00365                 $extract = explode("/", $url);
00366 
00367                 // Done extracting the URL :)
00368                 $url = $extract[0];
00369 
00370                 // Extract host name
00371                 $host = str_replace("http://", "", $url);
00372                 if (ereg("/", $host)) $host = substr($host, 0, strpos($host, "/"));
00373 
00374                 // Generate relative URL
00375                 $script = substr($script, (strlen($url) + 7));
00376                 if (substr($script, 0, 1) == "/") $script = substr($script, 1);
00377 
00378                 // Open connection
00379                 $fp = @fsockopen($host, 80, $errno, $errdesc, 30);
00380                 if (!$fp) {
00381                         // Failed!
00382                         return array("", "", "");
00383                 }
00384 
00385                 // Generate request header
00386                 $request  = "GET /".trim($script)." HTTP/1.0\r\n";
00387                 $request .= "Host: ".$host."\r\n";
00388                 $request .= sprintf("User-Agent: Bube oder Dame / 1.0 by Quix0r [Spieler: %d]\r\n\r\n", $this->w_id);
00389 
00390                 // Initialize array
00391                 $response = array();
00392 
00393                 // Write request
00394                 fputs($fp, $request);
00395 
00396                 // Read response
00397                 while(!feof($fp)) {
00398                         $response[] = trim(fgets($fp, 1024));
00399                 }
00400 
00401                 // Close socket
00402                 fclose($fp);
00403 
00404                 // Was the request successfull?
00405                 if ((!ereg("200 OK", $response[0])) && (empty($response[0]))) {
00406                         // Not found / access forbidden
00407                         $response = array("", "", "");
00408                 }
00409 
00410                 // Return response
00411                 return $response;
00412         }
00413 }
00414 
00415 // [EOF]
00416 ?>

Generated on Mon Dec 8 01:06:46 2008 for Ship-Simulator by  doxygen 1.5.6