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 - 2012 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 static final 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 for ($idx = (count($response) - 1); $idx > 1; $idx--) {
222 $line = trim($response[$idx]);
229 // Prepare the returning result for higher functions
230 if (substr($response, 0, 1) == '&') {
231 // Remove the leading & (which can be used in Flash)
232 $response = substr($response, 1);
235 // Bring back the response
236 $data = explode('=', $response);
238 // Default return array (should not stay empty)
241 // We use only the first two entries (which shall be fine)
242 if ($data[0] === "error") {
243 // The request has failed... :(
245 case "404": // Invalid API ID
246 case "AUTH": // Authorization has failed
248 'status' => "auth_failed",
249 'message' => "API-Daten scheinen nicht zu stimmen! (Access Denied)"
253 case "LOCKED": // User account is locked!
254 case "PASS": // Bad passphrase entered
255 case "USER": // Missing account or invalid password
257 'status' => "user_failed",
258 'message' => "Dein eingegebener WDS66-Username stimmt nicht, ist gesperrt oder du hast ein falsches Passwort eingegeben."
262 case "OWN": // Transfer to own account
264 'status' => "own_failed",
265 'message' => "Du darfst dein eigenes Spiel leider nicht spielen."
269 case "AMOUNT": // Amount is depleted
271 'status' => "amount_failed",
272 'message' => "Dein Guthaben reicht nicht aus, um das Spiel zu spielen."
276 case "AMOUNT-SEND": // API amount is depleted
278 'status' => "api_amount_failed",
279 'message' => "Nicht genügend Guthaben auf dem API-Account."
283 default: // Unknown error (maybe new?)
285 'status' => "request_failed",
286 'message' => sprintf("Unbekannter Fehler <u>%s</u> von API erhalten.", $data[1])
293 'status' => $this->statusOkay,
294 'response' => $response
302 // Widthdraw this amount
303 private function executeWithdraw ($amount) {
304 // First all fails...
307 // Prepare the purpose
308 $purpose = "\"Bube oder Dame\"-Einsatz gesetzt.";
310 // Prepare the request data
311 $requestData = array(
312 'sub_request' => 'receive',
313 't_uid' => $this->w_id,
314 't_md5' => $this->w_md5,
315 'r_uid' => (int)$this->config['wernis_refid'],
316 'amount' => (int)$amount,
317 'purpose' => urlencode(base64_encode($purpose))
320 // Return the result from the lower functions
321 $return = $this->sendRequest("book.php", $requestData);
323 if ($return['status'] == $this->statusOkay) {
327 // Status failture text
328 $this->setStatusMessage($return['message'], $return['status']);
335 // Payout this amount
336 private function executePayout ($amount) {
337 // First all fails...
340 // Prepare the purpose
341 $purpose = "\"Bube oder Dame\"-Gewinn erhalten.";
343 // Prepare the request data
344 $requestData = array(
345 'sub_request' => 'send',
346 't_uid' => $this->w_id,
347 't_md5' => $this->w_md5,
348 'r_uid' => (int)$this->config['wernis_refid'],
349 'amount' => (int)$amount,
350 'purpose' => urlencode(base64_encode($purpose))
353 // Return the result from the lower functions
354 $return = $this->sendRequest("book.php", $requestData);
356 if ($return['status'] == $this->statusOkay) {
360 // Status failture text
361 $this->setStatusMessage($return['message'], $return['status']);
368 // Send raw GET request
369 private function sendRawRequest ($script) {
370 // Use the hostname from script URL as new hostname
371 $url = substr($script, 7);
372 $extract = explode('/', $url);
374 // Done extracting the URL :)
378 $host = str_replace("http://", '', $url);
379 if (ereg('/', $host)) $host = substr($host, 0, strpos($host, '/'));
381 // Generate relative URL
382 $script = substr($script, (strlen($url) + 7));
383 if (substr($script, 0, 1) == '/') $script = substr($script, 1);
386 $fp = @fsockopen($host, 80, $errno, $errdesc, 30);
389 return array('', '', '');
392 // Generate request header
393 $request = "GET /".trim($script)." HTTP/1.0\r\n";
394 $request .= "Host: ".$host."\r\n";
395 $request .= sprintf("User-Agent: Bube oder Dame / 1.0 by Quix0r [Spieler: %d]\r\n\r\n", $this->w_id);
401 fputs($fp, $request);
405 $response[] = trim(fgets($fp, 1024));
411 // Was the request successfull?
412 if ((!ereg("200 OK", $response[0])) && (empty($response[0]))) {
413 // Not found / access forbidden
414 $response = array('', '', '');