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, 2009 Core Developer Team
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();
63 private $statusOkay = 'OK';
66 * Protected constructor
70 protected function __construct () {
71 // Call parent constructor
72 parent::__construct(__CLASS__);
76 * Creates an instance of this API class
78 * @param $cfg Configuration array
79 * @return $apiInstance An instance of this API class
81 public final static function createWernisApi (array $cfg) {
82 // Create a new instance
83 $apiInstance = new WernisApi();
86 if (!isset($cfg['api_url'])) $cfg['api_url'] = self::$apiUrl;
88 // Konfiguration uebertragen
89 $apiInstance->setCoonfigArray($cfg);
91 // Return the instance
96 * Setter for gamer data
98 * @param $w_id Username (id) of the gamer
99 * @param $w_pwd Clear password of the gamer
102 public function setUser ($w_id, $w_pwd) {
106 // Hash clear password and set it
107 $this->w_md5 = md5($w_pwd);
110 /************************************************
111 * The following methods are not yet rewritten! *
112 ************************************************/
114 public function einziehen ($amount) {
115 // amount auf Gueltigkeit pruefen
116 $amount = isset($amount) ? $amount+0 : 0;
118 if ($amount < $this->config['mineinsatz']) {
119 $this->setStatusMessage('low_stakes', sprintf("Dein Einsatz muss mindestens %d Wernis betragen.", $this->config['mineinsatz']));
124 return $this->executeWithdraw($amount);
127 public function verschicken ($amount) {
128 // amount auf Gueltigkeit pruefen
129 $amount = isset($amount) ? $amount+0 : 0;
131 if ($amount < $this->config['mineinsatz']) {
132 $this->setStatusMessage('low_stakes', sprintf("Dein Einsatz muss mindestens %d Wernis betragen.", $this->config['mineinsatz']));
137 return $this->executePayout($amount);
140 // Script abbrechen mit Zurueck-Buttom
141 public function ende () {
143 include "templates/zurueck.html";
144 include "templates/fuss.html";
148 // Fehlermeldung ausgeben und beenden
149 public function error () {
150 print "<div class=\"fehler\">Fehler im Spiel: ".$this->getErrorMessage()."<div><br />\n";
154 // Sets a status message and code
155 public function setStatusMessage ($msg, $status) {
156 $this->statusArray['message'] = $msg;
157 $this->statusArray['status'] = $status;
160 // Get the status message
161 public function getErrorMessage () {
162 if (isset($this->statusArray['message'])) {
164 return $this->statusArray['message'];
166 // Fall-back to status
167 return sprintf("Fehler-Code <u>%s</u> ist keiner Nachricht zugewiesen.", $this->getErrorCode());
171 // Get the status code
172 public function getErrorCode () {
173 if (isset($this->statusArray['status'])) {
175 return $this->statusArray['status'];
177 // Something bad happend
182 // Sends out a request to the API and returns it's result
183 private function sendRequest ($scriptName, array $requestData = array()) {
184 // Is the requestData an array?
185 if (!is_array($requestData)) {
188 'status' => "failed_general",
189 'message' => "API-Daten in <strong>config</strong> sind ungültig!"
193 // Is the API id and MD5 hash there?
194 if ((empty($this->config['wernis_api_id'])) || (empty($this->config['wernis_api_key']))) {
197 'status' => "failed_general",
198 'message' => "API-Daten in config.php sind leer!"
202 // Construct the request string
203 $requestString = $this->config['api_url'] . $scriptName."?api_id=".$this->config['wernis_api_id']."&api_key=".$this->config['wernis_api_key'];
204 foreach ($requestData as $key => $value) {
205 $requestString .= '&' . $key . '=' . $value;
208 // Get the raw response from the lower function
209 $response = $this->sendRawRequest($requestString);
211 // Check the response header if all is fine
212 if (strpos($response[0], '200') === false) {
213 // Something bad happend... :(
215 'status' => "request_error",
216 'message' => sprintf("Servermeldung <u>%s</u> von WDS66-API erhalten.", $response[0])
220 // All (maybe) fine so remove the response header from server
221 $response = $response[(count($response) - 1)];
223 // Prepare the returning result for higher functions
224 if (substr($response, 0, 1) == '&') {
225 // Remove the leading & (which can be used in Flash)
226 $response = substr($response, 1);
229 // Bring back the response
230 $data = explode('=', $response);
232 // Default return array (should not stay empty)
235 // We use only the first two entries (which shall be fine)
236 if ($data[0] === "error") {
237 // The request has failed... :(
239 case "404": // Invalid API ID
240 case "AUTH": // Authorization has failed
242 'status' => "auth_failed",
243 'message' => "API-Daten scheinen nicht zu stimmen! (Access Denied)"
247 case "LOCKED": // User account is locked!
248 case "PASS": // Bad passphrase entered
249 case "USER": // Missing account or invalid password
251 'status' => "user_failed",
252 'message' => "Dein eingegebener WDS66-Username stimmt nicht, ist gesperrt oder du hast ein falsches Passwort eingegeben."
256 case "OWN": // Transfer to own account
258 'status' => "own_failed",
259 'message' => "Du darfst dein eigenes Spiel leider nicht spielen."
263 case "AMOUNT": // Amount is depleted
265 'status' => "amount_failed",
266 'message' => "Dein Guthaben reicht nicht aus, um das Spiel zu spielen."
270 case "AMOUNT-SEND": // API amount is depleted
272 'status' => "api_amount_failed",
273 'message' => "Nicht genügend Guthaben auf dem API-Account."
277 default: // Unknown error (maybe new?)
279 'status' => "request_failed",
280 'message' => sprintf("Unbekannter Fehler <u>%s</u> von API erhalten.", $data[1])
287 'status' => $this->statusOkay,
288 'response' => $response
296 // Widthdraw this amount
297 private function executeWithdraw ($amount) {
298 // First all fails...
301 // Prepare the purpose
302 $purpose = "\"Bube oder Dame\"-Einsatz gesetzt.";
304 // Prepare the request data
305 $requestData = array(
306 'sub_request' => 'receive',
307 't_uid' => $this->w_id,
308 't_md5' => $this->w_md5,
309 'r_uid' => (int)$this->config['wernis_refid'],
310 'amount' => (int)$amount,
311 'purpose' => urlencode(base64_encode($purpose))
314 // Return the result from the lower functions
315 $return = $this->sendRequest("book.php", $requestData);
317 if ($return['status'] == $this->statusOkay) {
321 // Status failture text
322 $this->setStatusMessage($return['message'], $return['status']);
329 // Payout this amount
330 private function executePayout ($amount) {
331 // First all fails...
334 // Prepare the purpose
335 $purpose = "\"Bube oder Dame\"-Gewinn erhalten.";
337 // Prepare the request data
338 $requestData = array(
339 'sub_request' => 'send',
340 't_uid' => $this->w_id,
341 't_md5' => $this->w_md5,
342 'r_uid' => (int)$this->config['wernis_refid'],
343 'amount' => (int)$amount,
344 'purpose' => urlencode(base64_encode($purpose))
347 // Return the result from the lower functions
348 $return = $this->sendRequest("book.php", $requestData);
350 if ($return['status'] == $this->statusOkay) {
354 // Status failture text
355 $this->setStatusMessage($return['message'], $return['status']);
362 // Send raw GET request
363 private function sendRawRequest ($script) {
364 // Use the hostname from script URL as new hostname
365 $url = substr($script, 7);
366 $extract = explode('/', $url);
368 // Done extracting the URL :)
372 $host = str_replace("http://", '', $url);
373 if (ereg('/', $host)) $host = substr($host, 0, strpos($host, '/'));
375 // Generate relative URL
376 $script = substr($script, (strlen($url) + 7));
377 if (substr($script, 0, 1) == '/') $script = substr($script, 1);
380 $fp = @fsockopen($host, 80, $errno, $errdesc, 30);
383 return array('', '', '');
386 // Generate request header
387 $request = "GET /".trim($script)." HTTP/1.0\r\n";
388 $request .= "Host: ".$host."\r\n";
389 $request .= sprintf("User-Agent: Bube oder Dame / 1.0 by Quix0r [Spieler: %d]\r\n\r\n", $this->w_id);
395 fputs($fp, $request);
399 $response[] = trim(fgets($fp, 1024));
405 // Was the request successfull?
406 if ((!ereg("200 OK", $response[0])) && (empty($response[0]))) {
407 // Not found / access forbidden
408 $response = array('', '', '');