5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
9 * Class for connecting to the Wernis-Portal at http://www.wds66.com
11 * @author Roland Haeder <webmaster@shipsimu.org>
13 <<<<<<< HEAD:framework/main/third_party/api/wernisportal/class_WernisApi.php
14 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
16 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
17 >>>>>>> Some updates::inc/main/third_party/api/wernisportal/class_WernisApi.php
18 * @license GNU GPL 3.0 or any newer version
19 * @link http://www.shipsimu.org
20 * @todo Out-dated since 0.6-BETA
22 * This program is free software: you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation, either version 3 of the License, or
25 * (at your option) any later version.
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with this program. If not, see <http://www.gnu.org/licenses/>.
35 class WernisApi extends BaseFrameworkSystem {
39 private static $apiUrl = 'https://www.wds66.com/api/';
44 private $wernis_amount = 0;
52 * API Wernis password (not account password!)
57 * Nickname of the user
62 * Wernis amount of the user
64 private $w_amount = 0;
67 * Array with status informations
69 private $statusArray = array();
74 private $statusOkay = 'OK';
77 * Protected constructor
81 protected function __construct () {
82 // Call parent constructor
83 parent::__construct(__CLASS__);
87 * Creates an instance of this API class
89 * @param $cfg Configuration array
90 * @return $apiInstance An instance of this API class
92 public static final function createWernisApi (array $cfg) {
93 // Create a new instance
94 $apiInstance = new WernisApi();
97 if (!isset($cfg['api_url'])) $cfg['api_url'] = self::$apiUrl;
99 // Konfiguration uebertragen
100 $apiInstance->setCoonfigArray($cfg);
102 // Return the instance
107 * Setter for gamer data
109 * @param $w_id Username (id) of the gamer
110 * @param $w_pwd Clear password of the gamer
113 public function setUser ($w_id, $w_pwd) {
117 // Hash clear password and set it
118 $this->w_md5 = md5($w_pwd);
121 /************************************************
122 * The following methods are not yet rewritten! *
123 ************************************************/
125 public function einziehen ($amount) {
126 // amount auf Gueltigkeit pruefen
127 $amount = isset($amount) ? $amount+0 : 0;
129 if ($amount < $this->config['mineinsatz']) {
130 $this->setStatusMessage('low_stakes', sprintf('Dein Einsatz muss mindestens %d Wernis betragen.', $this->config['mineinsatz']));
135 return $this->executeWithdraw($amount);
138 public function verschicken ($amount) {
139 // amount auf Gueltigkeit pruefen
140 $amount = isset($amount) ? $amount+0 : 0;
142 if ($amount < $this->config['mineinsatz']) {
143 $this->setStatusMessage('low_stakes', sprintf('Dein Einsatz muss mindestens %d Wernis betragen.', $this->config['mineinsatz']));
148 return $this->executePayout($amount);
151 // Script abbrechen mit Zurueck-Buttom
152 public function ende () {
154 include 'templates/zurueck.html';
155 include 'templates/fuss.html';
159 // Fehlermeldung ausgeben und beenden
160 public function error () {
161 print "<div class=\"fehler\">Fehler im Spiel: ".$this->getErrorMessage()."<div><br />\n";
165 // Sets a status message and code
166 public function setStatusMessage ($msg, $status) {
167 $this->statusArray['message'] = $msg;
168 $this->statusArray['status'] = $status;
171 // Get the status message
172 public function getErrorMessage () {
173 if (isset($this->statusArray['message'])) {
175 return $this->statusArray['message'];
177 // Fall-back to status
178 return sprintf('Fehler-Code <u>%s</u> ist keiner Nachricht zugewiesen.', $this->getErrorCode());
182 // Get the status code
183 public function getErrorCode () {
184 if (isset($this->statusArray['status'])) {
186 return $this->statusArray['status'];
188 // Something bad happend
193 // Sends out a request to the API and returns it's result
194 private function sendRequest ($scriptName, array $requestData = array()) {
195 // Is the requestData an array?
196 if (!is_array($requestData)) {
199 'status' => 'failed_general',
200 'message' => 'API-Daten in <strong>config</strong> sind ungültig!'
204 // Is the API id and MD5 hash there?
205 if ((empty($this->config['wernis_api_id'])) || (empty($this->config['wernis_api_key']))) {
208 'status' => 'failed_general',
209 'message' => 'API-Daten in config.php sind leer!'
213 // Construct the request string
214 $requestString = $this->config['api_url'] . $scriptName . '?api_id=' . $this->config['wernis_api_id'] . '&api_key='.$this->config['wernis_api_key'];
215 foreach ($requestData as $key => $value) {
216 $requestString .= '&' . $key . '=' . $value;
219 // Get the raw response from the lower function
220 $response = $this->sendRawRequest($requestString);
222 // Check the response header if all is fine
223 if (strpos($response[0], '200') === false) {
224 // Something bad happend... :(
226 'status' => 'request_error',
227 'message' => sprintf('Servermeldung <u>%s</u> von WDS66-API erhalten.', $response[0])
231 // All (maybe) fine so remove the response header from server
232 for ($idx = (count($response) - 1); $idx > 1; $idx--) {
233 $line = trim($response[$idx]);
240 // Prepare the returning result for higher functions
241 if (substr($response, 0, 1) == '&') {
242 // Remove the leading & (which can be used in Flash)
243 $response = substr($response, 1);
246 // Bring back the response
247 $data = explode('=', $response);
249 // Default return array (should not stay empty)
252 // We use only the first two entries (which shall be fine)
253 if ($data[0] === 'error') {
254 // The request has failed... :(
256 case '404': // Invalid API ID
257 case 'AUTH': // Authorization has failed
259 'status' => 'auth_failed',
260 'message' => 'API-Daten scheinen nicht zu stimmen! (Access Denied)'
264 case 'LOCKED': // User account is locked!
265 case 'PASS': // Bad passphrase entered
266 case 'USER': // Missing account or invalid password
268 'status' => 'user_failed',
269 'message' => 'Dein eingegebener WDS66-Username stimmt nicht, ist gesperrt oder du hast ein falsches Passwort eingegeben.'
273 case 'OWN': // Transfer to own account
275 'status' => 'own_failed',
276 'message' => 'Du darfst dein eigenes Spiel leider nicht spielen.'
280 case 'AMOUNT': // Amount is depleted
282 'status' => 'amount_failed',
283 'message' => 'Dein Guthaben reicht nicht aus, um das Spiel zu spielen.'
287 case 'AMOUNT-SEND': // API amount is depleted
289 'status' => 'api_amount_failed',
290 'message' => 'Nicht genügend Guthaben auf dem API-Account.'
294 default: // Unknown error (maybe new?)
296 'status' => 'request_failed',
297 'message' => sprintf('Unbekannter Fehler <u>%s</u> von API erhalten.', $data[1])
304 'status' => $this->statusOkay,
305 'response' => $response
313 // Widthdraw this amount
314 private function executeWithdraw ($amount) {
315 // First all fails...
318 // Prepare the purpose
319 $purpose = "\"Bube oder Dame\"-Einsatz gesetzt.";
321 // Prepare the request data
322 $requestData = array(
323 'sub_request' => 'receive',
324 't_uid' => $this->w_id,
325 't_md5' => $this->w_md5,
326 'r_uid' => (int) $this->config['wernis_refid'],
327 'amount' => (int) $amount,
328 'purpose' => urlencode(base64_encode($purpose))
331 // Return the result from the lower functions
332 $return = $this->sendRequest('book.php', $requestData);
334 if ($return['status'] == $this->statusOkay) {
338 // Status failture text
339 $this->setStatusMessage($return['message'], $return['status']);
346 // Payout this amount
347 private function executePayout ($amount) {
348 // First all fails...
351 // Prepare the purpose
352 $purpose = "\"Bube oder Dame\"-Gewinn erhalten.";
354 // Prepare the request data
355 $requestData = array(
356 'sub_request' => 'send',
357 't_uid' => $this->w_id,
358 't_md5' => $this->w_md5,
359 'r_uid' => (int) $this->config['wernis_refid'],
360 'amount' => (int) $amount,
361 'purpose' => urlencode(base64_encode($purpose))
364 // Return the result from the lower functions
365 $return = $this->sendRequest("book.php", $requestData);
367 if ($return['status'] == $this->statusOkay) {
371 // Status failture text
372 $this->setStatusMessage($return['message'], $return['status']);
379 // Send raw GET request
380 private function sendRawRequest ($script) {
381 // Use the hostname from script URL as new hostname
382 $url = substr($script, 7);
383 $extract = explode('/', $url);
385 // Done extracting the URL :)
389 $host = str_replace('http://', '', $url);
390 if (ereg('/', $host)) $host = substr($host, 0, strpos($host, '/'));
392 // Generate relative URL
393 $script = substr($script, (strlen($url) + 7));
394 if (substr($script, 0, 1) == '/') $script = substr($script, 1);
397 $fp = @fsockopen($host, 80, $errno, $errdesc, 30);
400 return array('', '', '');
403 // Generate request header
404 $request = "GET /" . trim($script) . " HTTP/1.0\r\n";
405 $request .= "Host: " . $host . "\r\n";
406 $request .= sprintf("User-Agent: WernisApi/1.0 by Quix0r [Spieler: %d]\r\n\r\n", $this->w_id);
412 fputs($fp, $request);
416 array_push($response, trim(fgets($fp, 1024)));
422 // Was the request successfull?
423 if ((!ereg('200 OK', $response[0])) && (empty($response[0]))) {
424 // Not found / access forbidden
425 $response = array('', '', '');