3 * Class for connecting to the Wernis-Portal at http://www.wds66.com
5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2014 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.shipsimu.org
10 * @todo Out-dated since 0.6-BETA
12 * This program is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, either version 3 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 class WernisApi extends BaseFrameworkSystem {
29 private static $apiUrl = 'http://www.wds66.com/api/';
34 private $wernis_amount = 0;
42 * API Wernis password (not account password!)
47 * Nickname of the user
52 * Wernis amount of the user
54 private $w_amount = 0;
57 * Array with status informations
59 private $statusArray = array();
64 private $statusOkay = 'OK';
67 * Protected constructor
71 protected function __construct () {
72 // Call parent constructor
73 parent::__construct(__CLASS__);
77 * Creates an instance of this API class
79 * @param $cfg Configuration array
80 * @return $apiInstance An instance of this API class
82 public static final function createWernisApi (array $cfg) {
83 // Create a new instance
84 $apiInstance = new WernisApi();
87 if (!isset($cfg['api_url'])) $cfg['api_url'] = self::$apiUrl;
89 // Konfiguration uebertragen
90 $apiInstance->setCoonfigArray($cfg);
92 // Return the instance
97 * Setter for gamer data
99 * @param $w_id Username (id) of the gamer
100 * @param $w_pwd Clear password of the gamer
103 public function setUser ($w_id, $w_pwd) {
107 // Hash clear password and set it
108 $this->w_md5 = md5($w_pwd);
111 /************************************************
112 * The following methods are not yet rewritten! *
113 ************************************************/
115 public function einziehen ($amount) {
116 // amount auf Gueltigkeit pruefen
117 $amount = isset($amount) ? $amount+0 : 0;
119 if ($amount < $this->config['mineinsatz']) {
120 $this->setStatusMessage('low_stakes', sprintf('Dein Einsatz muss mindestens %d Wernis betragen.', $this->config['mineinsatz']));
125 return $this->executeWithdraw($amount);
128 public function verschicken ($amount) {
129 // amount auf Gueltigkeit pruefen
130 $amount = isset($amount) ? $amount+0 : 0;
132 if ($amount < $this->config['mineinsatz']) {
133 $this->setStatusMessage('low_stakes', sprintf('Dein Einsatz muss mindestens %d Wernis betragen.', $this->config['mineinsatz']));
138 return $this->executePayout($amount);
141 // Script abbrechen mit Zurueck-Buttom
142 public function ende () {
144 include 'templates/zurueck.html';
145 include 'templates/fuss.html';
149 // Fehlermeldung ausgeben und beenden
150 public function error () {
151 print "<div class=\"fehler\">Fehler im Spiel: ".$this->getErrorMessage()."<div><br />\n";
155 // Sets a status message and code
156 public function setStatusMessage ($msg, $status) {
157 $this->statusArray['message'] = $msg;
158 $this->statusArray['status'] = $status;
161 // Get the status message
162 public function getErrorMessage () {
163 if (isset($this->statusArray['message'])) {
165 return $this->statusArray['message'];
167 // Fall-back to status
168 return sprintf('Fehler-Code <u>%s</u> ist keiner Nachricht zugewiesen.', $this->getErrorCode());
172 // Get the status code
173 public function getErrorCode () {
174 if (isset($this->statusArray['status'])) {
176 return $this->statusArray['status'];
178 // Something bad happend
183 // Sends out a request to the API and returns it's result
184 private function sendRequest ($scriptName, array $requestData = array()) {
185 // Is the requestData an array?
186 if (!is_array($requestData)) {
189 'status' => 'failed_general',
190 'message' => 'API-Daten in <strong>config</strong> sind ungültig!'
194 // Is the API id and MD5 hash there?
195 if ((empty($this->config['wernis_api_id'])) || (empty($this->config['wernis_api_key']))) {
198 'status' => 'failed_general',
199 'message' => 'API-Daten in config.php sind leer!'
203 // Construct the request string
204 $requestString = $this->config['api_url'] . $scriptName . '?api_id=' . $this->config['wernis_api_id'] . '&api_key='.$this->config['wernis_api_key'];
205 foreach ($requestData as $key => $value) {
206 $requestString .= '&' . $key . '=' . $value;
209 // Get the raw response from the lower function
210 $response = $this->sendRawRequest($requestString);
212 // Check the response header if all is fine
213 if (strpos($response[0], '200') === FALSE) {
214 // Something bad happend... :(
216 'status' => 'request_error',
217 'message' => sprintf('Servermeldung <u>%s</u> von WDS66-API erhalten.', $response[0])
221 // All (maybe) fine so remove the response header from server
222 for ($idx = (count($response) - 1); $idx > 1; $idx--) {
223 $line = trim($response[$idx]);
230 // Prepare the returning result for higher functions
231 if (substr($response, 0, 1) == '&') {
232 // Remove the leading & (which can be used in Flash)
233 $response = substr($response, 1);
236 // Bring back the response
237 $data = explode('=', $response);
239 // Default return array (should not stay empty)
242 // We use only the first two entries (which shall be fine)
243 if ($data[0] === 'error') {
244 // The request has failed... :(
246 case '404': // Invalid API ID
247 case 'AUTH': // Authorization has failed
249 'status' => 'auth_failed',
250 'message' => 'API-Daten scheinen nicht zu stimmen! (Access Denied)'
254 case 'LOCKED': // User account is locked!
255 case 'PASS': // Bad passphrase entered
256 case 'USER': // Missing account or invalid password
258 'status' => 'user_failed',
259 'message' => 'Dein eingegebener WDS66-Username stimmt nicht, ist gesperrt oder du hast ein falsches Passwort eingegeben.'
263 case 'OWN': // Transfer to own account
265 'status' => 'own_failed',
266 'message' => 'Du darfst dein eigenes Spiel leider nicht spielen.'
270 case 'AMOUNT': // Amount is depleted
272 'status' => 'amount_failed',
273 'message' => 'Dein Guthaben reicht nicht aus, um das Spiel zu spielen.'
277 case 'AMOUNT-SEND': // API amount is depleted
279 'status' => 'api_amount_failed',
280 'message' => 'Nicht genügend Guthaben auf dem API-Account.'
284 default: // Unknown error (maybe new?)
286 'status' => 'request_failed',
287 'message' => sprintf('Unbekannter Fehler <u>%s</u> von API erhalten.', $data[1])
294 'status' => $this->statusOkay,
295 'response' => $response
303 // Widthdraw this amount
304 private function executeWithdraw ($amount) {
305 // First all fails...
308 // Prepare the purpose
309 $purpose = "\"Bube oder Dame\"-Einsatz gesetzt.";
311 // Prepare the request data
312 $requestData = array(
313 'sub_request' => 'receive',
314 't_uid' => $this->w_id,
315 't_md5' => $this->w_md5,
316 'r_uid' => (int) $this->config['wernis_refid'],
317 'amount' => (int) $amount,
318 'purpose' => urlencode(base64_encode($purpose))
321 // Return the result from the lower functions
322 $return = $this->sendRequest('book.php', $requestData);
324 if ($return['status'] == $this->statusOkay) {
328 // Status failture text
329 $this->setStatusMessage($return['message'], $return['status']);
336 // Payout this amount
337 private function executePayout ($amount) {
338 // First all fails...
341 // Prepare the purpose
342 $purpose = "\"Bube oder Dame\"-Gewinn erhalten.";
344 // Prepare the request data
345 $requestData = array(
346 'sub_request' => 'send',
347 't_uid' => $this->w_id,
348 't_md5' => $this->w_md5,
349 'r_uid' => (int) $this->config['wernis_refid'],
350 'amount' => (int) $amount,
351 'purpose' => urlencode(base64_encode($purpose))
354 // Return the result from the lower functions
355 $return = $this->sendRequest("book.php", $requestData);
357 if ($return['status'] == $this->statusOkay) {
361 // Status failture text
362 $this->setStatusMessage($return['message'], $return['status']);
369 // Send raw GET request
370 private function sendRawRequest ($script) {
371 // Use the hostname from script URL as new hostname
372 $url = substr($script, 7);
373 $extract = explode('/', $url);
375 // Done extracting the URL :)
379 $host = str_replace('http://', '', $url);
380 if (ereg('/', $host)) $host = substr($host, 0, strpos($host, '/'));
382 // Generate relative URL
383 $script = substr($script, (strlen($url) + 7));
384 if (substr($script, 0, 1) == '/') $script = substr($script, 1);
387 $fp = @fsockopen($host, 80, $errno, $errdesc, 30);
390 return array('', '', '');
393 // Generate request header
394 $request = "GET /" . trim($script) . " HTTP/1.0\r\n";
395 $request .= "Host: " . $host . "\r\n";
396 $request .= sprintf("User-Agent: WernisApi/1.0 by Quix0r [Spieler: %d]\r\n\r\n", $this->w_id);
402 fputs($fp, $request);
406 array_push($response, trim(fgets($fp, 1024)));
412 // Was the request successfull?
413 if ((!ereg('200 OK', $response[0])) && (empty($response[0]))) {
414 // Not found / access forbidden
415 $response = array('', '', '');