3 * Class for connecting to the Wernis-Portal at http://www.wds66.com
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, this is free software
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class WernisApi extends BaseFrameworkSystem {
28 private static $apiUrl = "http://www.wds66.com/api/";
33 private $wernis_amount = 0;
41 * API Wernis password (not account password!)
46 * Nickname of the user
51 * Wernis amount of the user
53 private $w_amount = 0;
56 * Array with status informations
58 private $statusArray = array();
61 * Protected constructor
65 protected function __construct () {
66 // Call parent constructor
67 parent::__construct(__CLASS__);
70 $this->removeNumberFormaters();
71 $this->removeSystemArray();
75 * Creates an instance of this API class
77 * @param $cfg Configuration array
78 * @return $apiInstance An instance of this API class
80 public final static function createWernisApi (array $cfg) {
81 // Create a new instance
82 $apiInstance = new WernisApi();
85 if (!isset($cfg['api_url'])) $cfg['api_url'] = self::$apiUrl;
87 // Konfiguration uebertragen
88 $apiInstance->setCoonfigArray($cfg);
90 // Return the instance
95 * Setter for gamer data
97 * @param $w_id Username (id) of the gamer
98 * @param $w_pwd Clear password of the gamer
101 public function setUser ($w_id, $w_pwd) {
105 // Hash clear password and set it
106 $this->w_md5 = md5($w_pwd);
109 /************************************************
110 * The following methods are not yet rewritten! *
111 ************************************************/
113 public function einziehen ($amount) {
114 // amount auf Gueltigkeit pruefen
115 $amount = isset($amount) ? $amount+0 : 0;
117 if ($amount < $this->config['mineinsatz']) {
118 $this->setStatusMessage('low_stakes', sprintf("Dein Einsatz muss mindestens %d Wernis betragen.", $this->config['mineinsatz']));
123 return $this->executeWithdraw($amount);
126 public function verschicken ($amount) {
127 // amount auf Gueltigkeit pruefen
128 $amount = isset($amount) ? $amount+0 : 0;
130 if ($amount < $this->config['mineinsatz']) {
131 $this->setStatusMessage('low_stakes', sprintf("Dein Einsatz muss mindestens %d Wernis betragen.", $this->config['mineinsatz']));
136 return $this->executePayout($amount);
139 // Script abbrechen mit Zurueck-Buttom
140 public function ende () {
142 include "templates/zurueck.html";
143 include "templates/fuss.html";
147 // Fehlermeldung ausgeben und beenden
148 public function error () {
149 print "<div class=\"fehler\">Fehler im Spiel: ".$this->getErrorMessage()."<div><br />\n";
153 // Sets a status message and code
154 public function setStatusMessage ($msg, $status) {
155 $this->statusArray['message'] = $msg;
156 $this->statusArray['status'] = $status;
159 // Get the status message
160 public function getErrorMessage () {
161 if (isset($this->statusArray['message'])) {
163 return $this->statusArray['message'];
165 // Fall-back to status
166 return sprintf("Fehler-Code <u>%s</u> ist keiner Nachricht zugewiesen.", $this->getErrorCode());
170 // Get the status code
171 public function getErrorCode () {
172 if (isset($this->statusArray['status'])) {
174 return $this->statusArray['status'];
176 // Something bad happend
181 // Sends out a request to the API and returns it's result
182 private function sendRequest ($scriptName, array $requestData = array()) {
183 // Is the requestData an array?
184 if (!is_array($requestData)) {
187 'status' => "failed_general",
188 'message' => "API-Daten in <strong>config</strong> sind ungültig!"
192 // Is the API id and MD5 hash there?
193 if ((empty($this->config['wernis_api_id'])) || (empty($this->config['wernis_api_key']))) {
196 'status' => "failed_general",
197 'message' => "API-Daten in config.php sind leer!"
201 // Construct the request string
202 $requestString = $this->config['api_url'] . $scriptName."?api_id=".$this->config['wernis_api_id']."&api_key=".$this->config['wernis_api_key'];
203 foreach ($requestData as $key=>$value) {
204 $requestString .= "&".$key."=".$value;
207 // Get the raw response from the lower function
208 $response = $this->sendRawRequest($requestString);
210 // Check the response header if all is fine
211 if (strpos($response[0], "200") === false) {
212 // Something bad happend... :(
214 'status' => "request_error",
215 'message' => sprintf("Servermeldung <u>%s</u> von WDS66-API erhalten.", $response[0])
219 // All (maybe) fine so remove the response header from server
220 $response = $response[(count($response) - 1)];
222 // Prepare the returning result for higher functions
223 if (substr($response, 0, 1) == "&") {
224 // Remove the leading & (which can be used in Flash)
225 $response = substr($response, 1);
228 // Bring back the response
229 $data = explode("=", $response);
231 // Default return array (should not stay empty)
234 // We use only the first two entries (which shall be fine)
235 if ($data[0] === "error") {
236 // The request has failed... :(
238 case "404": // Invalid API ID
239 case "AUTH": // Authorization has failed
241 'status' => "auth_failed",
242 'message' => "API-Daten scheinen nicht zu stimmen! (Access Denied)"
246 case "LOCKED": // User account is locked!
247 case "PASS": // Bad passphrase entered
248 case "USER": // Missing account or invalid password
250 'status' => "user_failed",
251 'message' => "Dein eingegebener WDS66-Username stimmt nicht, ist gesperrt oder du hast ein falsches Passwort eingegeben."
255 case "OWN": // Transfer to own account
257 'status' => "own_failed",
258 'message' => "Du darfst dein eigenes Spiel leider nicht spielen."
262 case "AMOUNT": // Amount is depleted
264 'status' => "amount_failed",
265 'message' => "Dein Guthaben reicht nicht aus, um das Spiel zu spielen."
269 case "AMOUNT-SEND": // API amount is depleted
271 'status' => "api_amount_failed",
272 'message' => "Nicht genügend Guthaben auf dem API-Account."
276 default: // Unknown error (maybe new?)
278 'status' => "request_failed",
279 'message' => sprintf("Unbekannter Fehler <u>%s</u> von API erhalten.", $data[1])
287 'response' => $response
295 // Widthdraw this amount
296 private function executeWithdraw ($amount) {
297 // First all fails...
300 // Prepare the purpose
301 $purpose = "\"Bube oder Dame\"-Einsatz gesetzt.";
303 // Prepare the request data
304 $requestData = array(
305 'sub_request' => "receive",
306 't_uid' => $this->w_id,
307 't_md5' => $this->w_md5,
308 'r_uid' => (int)$this->config['wernis_refid'],
309 'amount' => (int)$amount,
310 'purpose' => urlencode(base64_encode($purpose))
313 // Return the result from the lower functions
314 $return = $this->sendRequest("book.php", $requestData);
316 if ($return['status'] == "OK") {
320 // Status failture text
321 $this->setStatusMessage($return['message'], $return['status']);
328 // Payout this amount
329 private function executePayout ($amount) {
330 // First all fails...
333 // Prepare the purpose
334 $purpose = "\"Bube oder Dame\"-Gewinn erhalten.";
336 // Prepare the request data
337 $requestData = array(
338 'sub_request' => "send",
339 't_uid' => $this->w_id,
340 't_md5' => $this->w_md5,
341 'r_uid' => (int)$this->config['wernis_refid'],
342 'amount' => (int)$amount,
343 'purpose' => urlencode(base64_encode($purpose))
346 // Return the result from the lower functions
347 $return = $this->sendRequest("book.php", $requestData);
349 if ($return['status'] == "OK") {
353 // Status failture text
354 $this->setStatusMessage($return['message'], $return['status']);
361 // Send raw GET request
362 private function sendRawRequest ($script) {
363 // Use the hostname from script URL as new hostname
364 $url = substr($script, 7);
365 $extract = explode("/", $url);
367 // Done extracting the URL :)
371 $host = str_replace("http://", "", $url);
372 if (ereg("/", $host)) $host = substr($host, 0, strpos($host, "/"));
374 // Generate relative URL
375 $script = substr($script, (strlen($url) + 7));
376 if (substr($script, 0, 1) == "/") $script = substr($script, 1);
379 $fp = @fsockopen($host, 80, $errno, $errdesc, 30);
382 return array("", "", "");
385 // Generate request header
386 $request = "GET /".trim($script)." HTTP/1.0\r\n";
387 $request .= "Host: ".$host."\r\n";
388 $request .= sprintf("User-Agent: Bube oder Dame / 1.0 by Quix0r [Spieler: %d]\r\n\r\n", $this->w_id);
394 fputs($fp, $request);
398 $response[] = trim(fgets($fp, 1024));
404 // Was the request successfull?
405 if ((!ereg("200 OK", $response[0])) && (empty($response[0]))) {
406 // Not found / access forbidden
407 $response = array("", "", "");