5 // Import framework stuff
6 use CoreFramework\Object\BaseFrameworkSystem;
9 * Class for connecting to the Wernis-Portal at http://www.wds66.com
11 * @author Roland Haeder <webmaster@shipsimu.org>
13 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
14 * @license GNU GPL 3.0 or any newer version
15 * @link http://www.shipsimu.org
16 * @todo Out-dated since 0.6-BETA
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 class WernisApi extends BaseFrameworkSystem {
35 private static $apiUrl = 'http://www.wds66.com/api/';
40 private $wernis_amount = 0;
48 * API Wernis password (not account password!)
53 * Nickname of the user
58 * Wernis amount of the user
60 private $w_amount = 0;
63 * Array with status informations
65 private $statusArray = array();
70 private $statusOkay = 'OK';
73 * Protected constructor
77 protected function __construct () {
78 // Call parent constructor
79 parent::__construct(__CLASS__);
83 * Creates an instance of this API class
85 * @param $cfg Configuration array
86 * @return $apiInstance An instance of this API class
88 public static final function createWernisApi (array $cfg) {
89 // Create a new instance
90 $apiInstance = new WernisApi();
93 if (!isset($cfg['api_url'])) $cfg['api_url'] = self::$apiUrl;
95 // Konfiguration uebertragen
96 $apiInstance->setCoonfigArray($cfg);
98 // Return the instance
103 * Setter for gamer data
105 * @param $w_id Username (id) of the gamer
106 * @param $w_pwd Clear password of the gamer
109 public function setUser ($w_id, $w_pwd) {
113 // Hash clear password and set it
114 $this->w_md5 = md5($w_pwd);
117 /************************************************
118 * The following methods are not yet rewritten! *
119 ************************************************/
121 public function einziehen ($amount) {
122 // amount auf Gueltigkeit pruefen
123 $amount = isset($amount) ? $amount+0 : 0;
125 if ($amount < $this->config['mineinsatz']) {
126 $this->setStatusMessage('low_stakes', sprintf('Dein Einsatz muss mindestens %d Wernis betragen.', $this->config['mineinsatz']));
131 return $this->executeWithdraw($amount);
134 public function verschicken ($amount) {
135 // amount auf Gueltigkeit pruefen
136 $amount = isset($amount) ? $amount+0 : 0;
138 if ($amount < $this->config['mineinsatz']) {
139 $this->setStatusMessage('low_stakes', sprintf('Dein Einsatz muss mindestens %d Wernis betragen.', $this->config['mineinsatz']));
144 return $this->executePayout($amount);
147 // Script abbrechen mit Zurueck-Buttom
148 public function ende () {
150 include 'templates/zurueck.html';
151 include 'templates/fuss.html';
155 // Fehlermeldung ausgeben und beenden
156 public function error () {
157 print "<div class=\"fehler\">Fehler im Spiel: ".$this->getErrorMessage()."<div><br />\n";
161 // Sets a status message and code
162 public function setStatusMessage ($msg, $status) {
163 $this->statusArray['message'] = $msg;
164 $this->statusArray['status'] = $status;
167 // Get the status message
168 public function getErrorMessage () {
169 if (isset($this->statusArray['message'])) {
171 return $this->statusArray['message'];
173 // Fall-back to status
174 return sprintf('Fehler-Code <u>%s</u> ist keiner Nachricht zugewiesen.', $this->getErrorCode());
178 // Get the status code
179 public function getErrorCode () {
180 if (isset($this->statusArray['status'])) {
182 return $this->statusArray['status'];
184 // Something bad happend
189 // Sends out a request to the API and returns it's result
190 private function sendRequest ($scriptName, array $requestData = array()) {
191 // Is the requestData an array?
192 if (!is_array($requestData)) {
195 'status' => 'failed_general',
196 'message' => 'API-Daten in <strong>config</strong> sind ungültig!'
200 // Is the API id and MD5 hash there?
201 if ((empty($this->config['wernis_api_id'])) || (empty($this->config['wernis_api_key']))) {
204 'status' => 'failed_general',
205 'message' => 'API-Daten in config.php sind leer!'
209 // Construct the request string
210 $requestString = $this->config['api_url'] . $scriptName . '?api_id=' . $this->config['wernis_api_id'] . '&api_key='.$this->config['wernis_api_key'];
211 foreach ($requestData as $key => $value) {
212 $requestString .= '&' . $key . '=' . $value;
215 // Get the raw response from the lower function
216 $response = $this->sendRawRequest($requestString);
218 // Check the response header if all is fine
219 if (strpos($response[0], '200') === FALSE) {
220 // Something bad happend... :(
222 'status' => 'request_error',
223 'message' => sprintf('Servermeldung <u>%s</u> von WDS66-API erhalten.', $response[0])
227 // All (maybe) fine so remove the response header from server
228 for ($idx = (count($response) - 1); $idx > 1; $idx--) {
229 $line = trim($response[$idx]);
236 // Prepare the returning result for higher functions
237 if (substr($response, 0, 1) == '&') {
238 // Remove the leading & (which can be used in Flash)
239 $response = substr($response, 1);
242 // Bring back the response
243 $data = explode('=', $response);
245 // Default return array (should not stay empty)
248 // We use only the first two entries (which shall be fine)
249 if ($data[0] === 'error') {
250 // The request has failed... :(
252 case '404': // Invalid API ID
253 case 'AUTH': // Authorization has failed
255 'status' => 'auth_failed',
256 'message' => 'API-Daten scheinen nicht zu stimmen! (Access Denied)'
260 case 'LOCKED': // User account is locked!
261 case 'PASS': // Bad passphrase entered
262 case 'USER': // Missing account or invalid password
264 'status' => 'user_failed',
265 'message' => 'Dein eingegebener WDS66-Username stimmt nicht, ist gesperrt oder du hast ein falsches Passwort eingegeben.'
269 case 'OWN': // Transfer to own account
271 'status' => 'own_failed',
272 'message' => 'Du darfst dein eigenes Spiel leider nicht spielen.'
276 case 'AMOUNT': // Amount is depleted
278 'status' => 'amount_failed',
279 'message' => 'Dein Guthaben reicht nicht aus, um das Spiel zu spielen.'
283 case 'AMOUNT-SEND': // API amount is depleted
285 'status' => 'api_amount_failed',
286 'message' => 'Nicht genügend Guthaben auf dem API-Account.'
290 default: // Unknown error (maybe new?)
292 'status' => 'request_failed',
293 'message' => sprintf('Unbekannter Fehler <u>%s</u> von API erhalten.', $data[1])
300 'status' => $this->statusOkay,
301 'response' => $response
309 // Widthdraw this amount
310 private function executeWithdraw ($amount) {
311 // First all fails...
314 // Prepare the purpose
315 $purpose = "\"Bube oder Dame\"-Einsatz gesetzt.";
317 // Prepare the request data
318 $requestData = array(
319 'sub_request' => 'receive',
320 't_uid' => $this->w_id,
321 't_md5' => $this->w_md5,
322 'r_uid' => (int) $this->config['wernis_refid'],
323 'amount' => (int) $amount,
324 'purpose' => urlencode(base64_encode($purpose))
327 // Return the result from the lower functions
328 $return = $this->sendRequest('book.php', $requestData);
330 if ($return['status'] == $this->statusOkay) {
334 // Status failture text
335 $this->setStatusMessage($return['message'], $return['status']);
342 // Payout this amount
343 private function executePayout ($amount) {
344 // First all fails...
347 // Prepare the purpose
348 $purpose = "\"Bube oder Dame\"-Gewinn erhalten.";
350 // Prepare the request data
351 $requestData = array(
352 'sub_request' => 'send',
353 't_uid' => $this->w_id,
354 't_md5' => $this->w_md5,
355 'r_uid' => (int) $this->config['wernis_refid'],
356 'amount' => (int) $amount,
357 'purpose' => urlencode(base64_encode($purpose))
360 // Return the result from the lower functions
361 $return = $this->sendRequest("book.php", $requestData);
363 if ($return['status'] == $this->statusOkay) {
367 // Status failture text
368 $this->setStatusMessage($return['message'], $return['status']);
375 // Send raw GET request
376 private function sendRawRequest ($script) {
377 // Use the hostname from script URL as new hostname
378 $url = substr($script, 7);
379 $extract = explode('/', $url);
381 // Done extracting the URL :)
385 $host = str_replace('http://', '', $url);
386 if (ereg('/', $host)) $host = substr($host, 0, strpos($host, '/'));
388 // Generate relative URL
389 $script = substr($script, (strlen($url) + 7));
390 if (substr($script, 0, 1) == '/') $script = substr($script, 1);
393 $fp = @fsockopen($host, 80, $errno, $errdesc, 30);
396 return array('', '', '');
399 // Generate request header
400 $request = "GET /" . trim($script) . " HTTP/1.0\r\n";
401 $request .= "Host: " . $host . "\r\n";
402 $request .= sprintf("User-Agent: WernisApi/1.0 by Quix0r [Spieler: %d]\r\n\r\n", $this->w_id);
408 fputs($fp, $request);
412 array_push($response, trim(fgets($fp, 1024)));
418 // Was the request successfull?
419 if ((!ereg('200 OK', $response[0])) && (empty($response[0]))) {
420 // Not found / access forbidden
421 $response = array('', '', '');