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__);
75 $this->removeNumberFormaters();
76 $this->removeSystemArray();
80 * Creates an instance of this API class
82 * @param $cfg Configuration array
83 * @return $apiInstance An instance of this API class
85 public final static function createWernisApi (array $cfg) {
86 // Create a new instance
87 $apiInstance = new WernisApi();
90 if (!isset($cfg['api_url'])) $cfg['api_url'] = self::$apiUrl;
92 // Konfiguration uebertragen
93 $apiInstance->setCoonfigArray($cfg);
95 // Return the instance
100 * Setter for gamer data
102 * @param $w_id Username (id) of the gamer
103 * @param $w_pwd Clear password of the gamer
106 public function setUser ($w_id, $w_pwd) {
110 // Hash clear password and set it
111 $this->w_md5 = md5($w_pwd);
114 /************************************************
115 * The following methods are not yet rewritten! *
116 ************************************************/
118 public function einziehen ($amount) {
119 // amount auf Gueltigkeit pruefen
120 $amount = isset($amount) ? $amount+0 : 0;
122 if ($amount < $this->config['mineinsatz']) {
123 $this->setStatusMessage('low_stakes', sprintf("Dein Einsatz muss mindestens %d Wernis betragen.", $this->config['mineinsatz']));
128 return $this->executeWithdraw($amount);
131 public function verschicken ($amount) {
132 // amount auf Gueltigkeit pruefen
133 $amount = isset($amount) ? $amount+0 : 0;
135 if ($amount < $this->config['mineinsatz']) {
136 $this->setStatusMessage('low_stakes', sprintf("Dein Einsatz muss mindestens %d Wernis betragen.", $this->config['mineinsatz']));
141 return $this->executePayout($amount);
144 // Script abbrechen mit Zurueck-Buttom
145 public function ende () {
147 include "templates/zurueck.html";
148 include "templates/fuss.html";
152 // Fehlermeldung ausgeben und beenden
153 public function error () {
154 print "<div class=\"fehler\">Fehler im Spiel: ".$this->getErrorMessage()."<div><br />\n";
158 // Sets a status message and code
159 public function setStatusMessage ($msg, $status) {
160 $this->statusArray['message'] = $msg;
161 $this->statusArray['status'] = $status;
164 // Get the status message
165 public function getErrorMessage () {
166 if (isset($this->statusArray['message'])) {
168 return $this->statusArray['message'];
170 // Fall-back to status
171 return sprintf("Fehler-Code <u>%s</u> ist keiner Nachricht zugewiesen.", $this->getErrorCode());
175 // Get the status code
176 public function getErrorCode () {
177 if (isset($this->statusArray['status'])) {
179 return $this->statusArray['status'];
181 // Something bad happend
186 // Sends out a request to the API and returns it's result
187 private function sendRequest ($scriptName, array $requestData = array()) {
188 // Is the requestData an array?
189 if (!is_array($requestData)) {
192 'status' => "failed_general",
193 'message' => "API-Daten in <strong>config</strong> sind ungültig!"
197 // Is the API id and MD5 hash there?
198 if ((empty($this->config['wernis_api_id'])) || (empty($this->config['wernis_api_key']))) {
201 'status' => "failed_general",
202 'message' => "API-Daten in config.php sind leer!"
206 // Construct the request string
207 $requestString = $this->config['api_url'] . $scriptName."?api_id=".$this->config['wernis_api_id']."&api_key=".$this->config['wernis_api_key'];
208 foreach ($requestData as $key => $value) {
209 $requestString .= '&' . $key . '=' . $value;
212 // Get the raw response from the lower function
213 $response = $this->sendRawRequest($requestString);
215 // Check the response header if all is fine
216 if (strpos($response[0], '200') === false) {
217 // Something bad happend... :(
219 'status' => "request_error",
220 'message' => sprintf("Servermeldung <u>%s</u> von WDS66-API erhalten.", $response[0])
224 // All (maybe) fine so remove the response header from server
225 $response = $response[(count($response) - 1)];
227 // Prepare the returning result for higher functions
228 if (substr($response, 0, 1) == '&') {
229 // Remove the leading & (which can be used in Flash)
230 $response = substr($response, 1);
233 // Bring back the response
234 $data = explode('=', $response);
236 // Default return array (should not stay empty)
239 // We use only the first two entries (which shall be fine)
240 if ($data[0] === "error") {
241 // The request has failed... :(
243 case "404": // Invalid API ID
244 case "AUTH": // Authorization has failed
246 'status' => "auth_failed",
247 'message' => "API-Daten scheinen nicht zu stimmen! (Access Denied)"
251 case "LOCKED": // User account is locked!
252 case "PASS": // Bad passphrase entered
253 case "USER": // Missing account or invalid password
255 'status' => "user_failed",
256 'message' => "Dein eingegebener WDS66-Username stimmt nicht, ist gesperrt oder du hast ein falsches Passwort eingegeben."
260 case "OWN": // Transfer to own account
262 'status' => "own_failed",
263 'message' => "Du darfst dein eigenes Spiel leider nicht spielen."
267 case "AMOUNT": // Amount is depleted
269 'status' => "amount_failed",
270 'message' => "Dein Guthaben reicht nicht aus, um das Spiel zu spielen."
274 case "AMOUNT-SEND": // API amount is depleted
276 'status' => "api_amount_failed",
277 'message' => "Nicht genügend Guthaben auf dem API-Account."
281 default: // Unknown error (maybe new?)
283 'status' => "request_failed",
284 'message' => sprintf("Unbekannter Fehler <u>%s</u> von API erhalten.", $data[1])
291 'status' => $this->statusOkay,
292 'response' => $response
300 // Widthdraw this amount
301 private function executeWithdraw ($amount) {
302 // First all fails...
305 // Prepare the purpose
306 $purpose = "\"Bube oder Dame\"-Einsatz gesetzt.";
308 // Prepare the request data
309 $requestData = array(
310 'sub_request' => 'receive',
311 't_uid' => $this->w_id,
312 't_md5' => $this->w_md5,
313 'r_uid' => (int)$this->config['wernis_refid'],
314 'amount' => (int)$amount,
315 'purpose' => urlencode(base64_encode($purpose))
318 // Return the result from the lower functions
319 $return = $this->sendRequest("book.php", $requestData);
321 if ($return['status'] == $this->statusOkay) {
325 // Status failture text
326 $this->setStatusMessage($return['message'], $return['status']);
333 // Payout this amount
334 private function executePayout ($amount) {
335 // First all fails...
338 // Prepare the purpose
339 $purpose = "\"Bube oder Dame\"-Gewinn erhalten.";
341 // Prepare the request data
342 $requestData = array(
343 'sub_request' => 'send',
344 't_uid' => $this->w_id,
345 't_md5' => $this->w_md5,
346 'r_uid' => (int)$this->config['wernis_refid'],
347 'amount' => (int)$amount,
348 'purpose' => urlencode(base64_encode($purpose))
351 // Return the result from the lower functions
352 $return = $this->sendRequest("book.php", $requestData);
354 if ($return['status'] == $this->statusOkay) {
358 // Status failture text
359 $this->setStatusMessage($return['message'], $return['status']);
366 // Send raw GET request
367 private function sendRawRequest ($script) {
368 // Use the hostname from script URL as new hostname
369 $url = substr($script, 7);
370 $extract = explode('/', $url);
372 // Done extracting the URL :)
376 $host = str_replace("http://", '', $url);
377 if (ereg('/', $host)) $host = substr($host, 0, strpos($host, '/'));
379 // Generate relative URL
380 $script = substr($script, (strlen($url) + 7));
381 if (substr($script, 0, 1) == '/') $script = substr($script, 1);
384 $fp = @fsockopen($host, 80, $errno, $errdesc, 30);
387 return array('', '', '');
390 // Generate request header
391 $request = "GET /".trim($script)." HTTP/1.0\r\n";
392 $request .= "Host: ".$host."\r\n";
393 $request .= sprintf("User-Agent: Bube oder Dame / 1.0 by Quix0r [Spieler: %d]\r\n\r\n", $this->w_id);
399 fputs($fp, $request);
403 $response[] = trim(fgets($fp, 1024));
409 // Was the request successfull?
410 if ((!ereg("200 OK", $response[0])) && (empty($response[0]))) {
411 // Not found / access forbidden
412 $response = array('', '', '');