[PHP] MobilniPlatby.cz [1.0.0-beta2]

Message
Autor
Uživatelský avatar
zdenda204
Příspěvky: 3617
Věk: 36
Registrován: 23 kvě 2012, 21:38
Reputation: 0

Re: [PHP] MobilniPlatby.cz [1.0.0-beta2]

#61 Příspěvekod zdenda204 » 28 srp 2014, 19:57

Využij funkci FakaHedy :)
Jinak to vpodstatě snad ani možné není, protože ti FH neumožní server spustit pod wrapperem, či jak se tomu nadává (Jakoby aplikace ještě nad samotným craftbukkitem, protože bez této aplikace ho vypneš, ale následně už vzdáleně nezapneš.)
Přijímám zakázky na skripty/pluginy v PHP/Javě za malý poplatky! A kup mi sušenku :) Obrázek Obrázek
⇨Projekt Market⇦

Rutherther
Příspěvky: 14
Registrován: 06 črc 2014, 21:43
Reputation: 0

Re: [PHP] MobilniPlatby.cz [1.0.0-beta2]

#62 Příspěvekod Rutherther » 28 srp 2014, 20:00

Aha a na jiném hostingu taky ne? Nebudu na cizím fóru dělat reklamu proto nezmiňuji a navíc se nejedná o konkrétní hosting spíše obecně.

Edit: neslo by ze mam treba spigot a rcon by v 1h odpoledne poslal /restart? A 5m predtim /say za 5 minut probíhá plánovaný restart serveru?

Uživatelský avatar
zdenda204
Příspěvky: 3617
Věk: 36
Registrován: 23 kvě 2012, 21:38
Reputation: 0

Re: [PHP] MobilniPlatby.cz [1.0.0-beta2]

#63 Příspěvekod zdenda204 » 28 srp 2014, 20:27

Ajo, to jsem opomněl :) Jo, to by šlo :) Zapomněl jsem na tuhle funkci.
Každopádně kdyžtak založ téma v OffTopicu ;)
Přijímám zakázky na skripty/pluginy v PHP/Javě za malý poplatky! A kup mi sušenku :) Obrázek Obrázek
⇨Projekt Market⇦

Uživatelský avatar
Mysteria
Příspěvky: 9122
Registrován: 26 pro 2009, 15:40
Reputation: 7
Bydliště: Česká republika
Kontaktovat uživatele:

Re: [PHP] MobilniPlatby.cz [1.0.0-beta2]

#64 Příspěvekod Mysteria » 28 srp 2014, 20:28

Ty nevidíš na mým profilu, vlevo dole jak jsou ty ikonky ikonku SZ (Soukromá Zpráva) nebo by to mělo být i u mých příspěvků dole (Profil a vedle Soukromá Zpráva tlačítko). Jinak k otázce, ano šlo by to, za předpokladu, že máš webhosting, který ti umožní v určitou dobu pustit script (jmenuje se to většinou cron).

TheMikeee
Příspěvky: 83
Registrován: 11 dub 2014, 15:18
Reputation: 0

Re: [PHP] MobilniPlatby.cz [1.0.0-beta2]

#65 Příspěvekod TheMikeee » 30 srp 2014, 21:43

Parse error: syntax error, unexpected ''say Uzivatel :NICK: si prave ' (T_CONSTANT_ENCAPSED_STRING), expecting ']' in /data/web/virtuals/54413/virtual/www/skripty/mobilni-platby.php on line 29
mi napíše keď otvorím stránku a v emulátore mi to ukazuje toto: Váš skript vrací odpověď v nesprávném formátu. Správný formát odpovědi naleznete v technické specifikaci.

Uživatelský avatar
zdenda204
Příspěvky: 3617
Věk: 36
Registrován: 23 kvě 2012, 21:38
Reputation: 0

Re: [PHP] MobilniPlatby.cz [1.0.0-beta2]

#66 Příspěvekod zdenda204 » 30 srp 2014, 21:49

A všichni supporti při nástupu dostali věšteckou kouli.

Dodej tvůj kód.
Přijímám zakázky na skripty/pluginy v PHP/Javě za malý poplatky! A kup mi sušenku :) Obrázek Obrázek
⇨Projekt Market⇦

TheMikeee
Příspěvky: 83
Registrován: 11 dub 2014, 15:18
Reputation: 0

Re: [PHP] MobilniPlatby.cz [1.0.0-beta2]

#67 Příspěvekod TheMikeee » 30 srp 2014, 21:53

Spoiler: zobrazit

Kód: Vybrat vše

<?php
/**
 * Název: MobilniPlatby.cz - PHP script pro zaslání RCON příkazů na MineCraft server po odeslání prémiové SMS
 * Autor: Mysteria - http://www.mysty.cz
 * Verze: 1.0.0-beta2
 * Poslední update: 27. 7. 2014
 * Více informací & download: http://forum.fakaheda.eu/viewtopic.php?f=85&t=20682#p134652
 */

$settings = (object)[ // Nastavení serverového RCONu
      'ip'       => 'ss',
      'port'       => ,ss
      'password'    => 'ss'
];

$messages = (object)[ // Nastavení chybových SMS zpráv
      'sms_bad_price'    => 'Litujeme, ale poslali jste SMS za :REALPRICE:Kc misto SMS za :PRICE:Kc.',
      'sms_not_found'    => 'Litujeme, ale poslali jste SMS, ktera nebyla automaticky rozpoznana. Kontaktujte prosim administratory.'
];

$levels = [ // Nastavení SMS levelů
   [
      'price'       => 99, // Cena SMSky
      'text'       => 'SMARTCRAFT :NICK:', // Text SMSky, :NICK: bude nahrazen skutečným nickem uvedeným v SMS, musí být unikátní v rámci všech platebních levelů
      'answer'    => 'Dekujeme za zakoupeni VIP.', // Odpověď na správně zaslanou SMS
      'commands'    => [ // RCON příkazy vykonané po úspěšném zpracování SMSky, :NICK: bude nahrazen skutečným nickem uvedeným v SMS
         'pex user :NICK: group set VIP',
         'economy give :NICK: 12500000'
         'say Uzivatel :NICK: si prave zakoupil VIP.'
      ]
   ], [
      'price'       => 149,
      'text'       => 'SCRAFT :NICK:',
      'answer'    => 'Dekujeme za zakoupeni ExtraVIP.',
      'commands'    => [
         'pex user :NICK: group set ExtraVIP',
         'economy give :NICK: 25000000'
         'say Uzivatel :NICK: si prave zakoupil ExtraVIP.'
      ]
   ], [
      'price'       => 299,
      'text'       => 'SCT :NICK:',
      'answer'    => 'Dekujeme za zakoupeni TrialOP.',
      'commands'    => [
         'pex user :NICK: group set TrialOP',
         'economy give :NICK: 50000000'
         'say Uzivatel :NICK: si prave zakoupil TrialOP.'
      ]
   ]
];

/* Do not make any changes below this line if you don't know what are you doing! */
$rcon = new Rcon($settings->ip, $settings->port, $settings->password, 3);
if (!$rcon->connect()) exit('Cannot connect to server RCON. Please check your settings.');
$url = (object)filter_input_array(INPUT_GET);
try {
   if (isset($url->shortcode)) {
      $url->sms = trim($url->sms);
      if (mb_strlen($url->shortcode) === 7) {
         $realPrice = (int)substr($url->shortcode, -2, 2);
         foreach ($levels as $level) {
            $level = (object)$level;
            if ($level->price >= 100) continue;
            $realNick = explode(' ', $url->sms)[array_search(':NICK:', explode(' ', $level->text))];
            if (strtolower($url->sms) === strtolower(str_replace(':NICK:', $realNick, $level->text))) {
               if ($realPrice === $level->price) {
                  foreach ($level->commands as $command) $rcon->send_command(str_replace(':NICK:', $realNick, $command));
                  File::write('./mobilni-platby.log', date('d. m. Y H:i:s', time()) . " => [SUCCESS] SMS zpráva s obsahem '$url->sms' s cenou {$realPrice}Kč byla úspěšně zpracována." . PHP_EOL);
                  header('Content-type:text/plain');
                  header('Content-length:' . mb_strlen($level->answer));
                  exit($level->answer);
               } else {
                  File::write('./mobilni-platby.log', date('d. m. Y H:i:s', time()) . " => [!ERROR!] SMS zpráva s obsahem '$url->sms' s cenou {$realPrice}Kč nebyla zpracována, protože měla mít cenu {$level->price}Kč." . PHP_EOL);
                  $answer = str_replace(':PRICE:', $level->price, str_replace(':REALPRICE:', $realPrice, $messages->sms_bad_price));
                  header('Content-type:text/plain');
                  header('Content-length:' . mb_strlen($answer));
                  exit($answer);
               }
            }
         }
         File::write('./mobilni-platby.log', date('d. m. Y H:i:s', time()) . " => [!ERROR!] SMS zpráva s obsahem '$url->sms' s cenou {$realPrice}Kč nebyla zpracována, protože nebyla rozpoznána." . PHP_EOL);
         header('Content-type:text/plain');
         header('Content-length:' . mb_strlen($messages->sms_not_found));
         exit($messages->sms_not_found);
      } else {
         foreach ($levels as $level) {
            $level = (object)$level;
            if ($level->price <= 99) continue;
            $realNick = explode(' ', $url->sms)[array_search(':NICK:', explode(' ', $level->text))];
            if (strtolower($url->sms) === strtolower(str_replace(':NICK:', $realNick, $level->text))) {
               $file = "./database/$url->id.txt";
               $commands = NULL;
               foreach ($level->commands as $command) $commands .= str_replace(':NICK:', $realNick, $command) . ';;;';
               File::write($file, $commands);
               $level->answer .= ";90333$level->price";
               File::write('./mobilni-platby.log', date('d. m. Y H:i:s', time()) . " => [SUCCESS] SMS zpráva ID $url->id s obsahem '$url->sms' s cenou {$level->price}Kč byla úspěšně zpracována." . PHP_EOL);
               header('Content-type:text/plain');
               header('Content-length:' . mb_strlen($level->answer));
               exit($level->answer);
            }
         }
         File::write('./mobilni-platby.log', date('d. m. Y H:i:s', time()) . " => [!ERROR!] SMS zpráva ID $url->id s obsahem '$url->sms' s cenou {$level->price}Kč nebyla zpracována, protože nebyla rozpoznána." . PHP_EOL);
         header('Content-type:text/plain');
         header('Content-length:' . mb_strlen($messages->sms_not_found));
         exit("$messages->sms_not_found;FREE90333199");
      }
   } elseif (isset($url->request)) {
      if ($url->status === 'DELIVERED') {
         $file = "./database/$url->request.txt";
         if (is_array($commands = explode(';;;', File::read($file)))) foreach ($commands as $command) $rcon->send_command($command);
         File::remove($file);
         File::write('./mobilni-platby.log', date('d. m. Y H:i:s', time()) . " => [SUCCESS] Doručenka SMS zprávy ID $url->request byla úspěšně zpracována." . PHP_EOL);
      } else File::write('./mobilni-platby.log', date('d. m. Y H:i:s', time()) . " => [!ERROR!] Doručenka SMS zprávy ID $url->request nebyla zpracována, protože přišla se statusem $url->status ($url->message)." . PHP_EOL);
      header('HTTP/1.1 204 No Content');
      exit;
   }
} catch (\FileException $e) { exit($e->getMessage()); }
class File {
   public static function read($source, $mode = 'r') {
      if (file_exists($source)) {
         if ($file = fopen($source, $mode)) {
            if ($content = fread($file, filesize($source))) {
               if (fclose($file)) {
                  return $content;
               } else throw new \FileException("Cannot close file $source.");
            } else throw new \FileException("Cannot read file $source.");
         } else throw new \FileException("Cannot open file $source.");
      } else throw new \FileException("Cannot find file $source.");
   }

   public static function write($destination, $content, $mode = 'a') {
      if (!is_dir(dirname($destination))) if (!mkdir(dirname($destination), 0777, TRUE)) throw new \FileException("Cannot create path $destination.");
      if ($file = fopen($destination, $mode)) {
         if (fwrite($file, $content)) {
            if (fclose($file)) {
               return TRUE;
            } else throw new \FileException("Cannot close file $destination.");
         } else throw new \FileException("Cannot write file $destination.");
      } else throw new \FileException("Cannot find file $destination.");
   }

   public static function remove($source) {
      if (file_exists($source)) {
         if (unlink($source)) {
            return TRUE;
         } else throw new \FileException("Cannot remove file $source.");
      } else throw new \FileException("Cannot find file $source.");
   }
}
class FileException extends \Exception { }
class Rcon {
   private $host;
   private $port;
   private $password;
   private $timeout;
   private $socket;
   private $authorized;
   private $last_response;

   const PACKET_AUTHORIZE = 5;
   const PACKET_COMMAND = 6;
   const SERVERDATA_AUTH = 3;
   const SERVERDATA_AUTH_RESPONSE = 2;
   const SERVERDATA_EXECCOMMAND = 2;
   const SERVERDATA_RESPONSE_VALUE = 0;

   public function __construct($host, $port, $password, $timeout) {
      $this->host = $host;
      $this->port = $port;
      $this->password = $password;
      $this->timeout = $timeout;
   }

   public function get_response() {
      return $this->last_response;
   }

   public function connect() {

      $this->socket = fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);

      if (!$this->socket) {
         $this->last_response = $errstr;
         return false;
      }

      //set timeout
      stream_set_timeout($this->socket, 3, 0);

      //authorize
      $auth = $this->authorize();

      if ($auth) {
         return true;
      }

      return false;
   }

   public function disconnect() {
      if ($this->socket) {
         fclose($this->socket);
      }
   }

   public function is_connected() {
      return $this->authorized;
   }

   public function send_command($command) {
      if (!$this->is_connected()) return false;

      // send command packet.
      $this->write_packet(Rcon::PACKET_COMMAND, Rcon::SERVERDATA_EXECCOMMAND, $command);

      // get response.
      $response_packet = $this->read_packet();
      if ($response_packet['id'] == Rcon::PACKET_COMMAND) {
         if ($response_packet['type'] == Rcon::SERVERDATA_RESPONSE_VALUE) {
            $this->last_response = $response_packet['body'];
            return $response_packet['body'];
         }
      }

      return false;
   }

   private function authorize() {
      $this->write_packet(Rcon::PACKET_AUTHORIZE, Rcon::SERVERDATA_AUTH, $this->password);
      $response_packet = $this->read_packet();

      if ($response_packet['type'] == Rcon::SERVERDATA_AUTH_RESPONSE) {
         if ($response_packet['id'] == Rcon::PACKET_AUTHORIZE) {
            $this->authorized = true;
            return true;
         }
      }

      $this->disconnect();
      return false;
   }

   /**
    * Writes a packet to the socket stream..
    */
   private function write_packet($packet_id, $packet_type, $packet_body) {
      /*
        Size         32-bit little-endian Signed Integer       Varies, see below.
        ID            32-bit little-endian Signed Integer      Varies, see below.
        Type         32-bit little-endian Signed Integer      Varies, see below.
        Body         Null-terminated ASCII String         Varies, see below.
        Empty String   Null-terminated ASCII String         0x00
       */

      //create packet
      $packet = pack("VV", $packet_id, $packet_type);
      $packet = $packet . $packet_body . "\x00";
      $packet = $packet . "\x00";

      // get packet size.
      $packet_size = strlen($packet);

      // attach size to packet.
      $packet = pack("V", $packet_size) . $packet;

      // write packet.
      fwrite($this->socket, $packet, strlen($packet));
   }

   private function read_packet() {
      //get packet size.
      $size_data = fread($this->socket, 4);
      $size_pack = unpack("V1size", $size_data);
      $size = $size_pack['size'];

      // if size is > 4096, the response will be in multiple packets.
      // this needs to be address. get more info about multi-packet responses
      // from the RCON protocol specification at
      // https://developer.valvesoftware.com/wiki/Source_RCON_Protocol
      // currently, this script does not support multi-packet responses.

      $packet_data = fread($this->socket, $size);
      $packet_pack = unpack("V1id/V1type/a*body", $packet_data);

      return $packet_pack;
   }
}

Uživatelský avatar
Mysteria
Příspěvky: 9122
Registrován: 26 pro 2009, 15:40
Reputation: 7
Bydliště: Česká republika
Kontaktovat uživatele:

Re: [PHP] MobilniPlatby.cz [1.0.0-beta2]

#68 Příspěvekod Mysteria » 30 srp 2014, 22:17

Chybí ti čárky za:

Kód: Vybrat vše

'economy give :NICK: 12500000'
, má to být

Kód: Vybrat vše

'economy give :NICK: 12500000',
a chybí ti to u všech třech levelů.

TheMikeee
Příspěvky: 83
Registrován: 11 dub 2014, 15:18
Reputation: 0

Re: [PHP] MobilniPlatby.cz [1.0.0-beta2]

#69 Příspěvekod TheMikeee » 30 srp 2014, 22:38

Aj za say
Spoiler: zobrazit

Kód: Vybrat vše

Uzivatel :NICK: si prave zakoupil TrialOP

Má byť tá čiarka?

-- 30 srp 2014, 21:47 --

na linku to už funguje ale na emulátore to ukazuje toto:
Spoiler: zobrazit

Kód: Vybrat vše

Výsledek testu:   ERROR
Popis chyby:   Váš skript vrací odpověď v nesprávném formátu. Správný formát odpovědi naleznete v technické specifikaci.
Typ volaného skriptu:   Nový skript
Testovaná URL:   http://smartcraft.eu/skripty/mobilni-platby.php?timestamp=2014-08-30T22%3A44%3A10&phone=8555&sms=SM...
Http Status kód:   200
Content-Type:   text/html
Text odpovědi:   <br /> <b>Warning</b>: Cannot modify header information - headers already sent by (output started at /data/web/virtuals/54413/virtual/www/skripty/mobilni-platby.php:1) in <b>/data/web/virtuals/54413/virtual/www/skripty/mobilni-platby.php</b> on line <b>69</b><br /> <br /> <b>Warning</b>: Cannot modify header information - headers already sent by (output started at /data/web/virtuals/54413/virtu



Script code:
Spoiler: zobrazit

Kód: Vybrat vše

<?php
/**
 * Název: MobilniPlatby.cz - PHP script pro zaslání RCON příkazů na MineCraft server po odeslání prémiové SMS
 * Autor: Mysteria - http://www.mysty.cz
 * Verze: 1.0.0-beta2
 * Poslední update: 27. 7. 2014
 * Více informací & download: http://forum.fakaheda.eu/viewtopic.php?f=85&t=20682#p134652
 */

$settings = (object)[ // Nastavení serverového RCONu
      'ip'       => 'ss',
      'port'       => ss,
      'password'    => 'ss'
];

$messages = (object)[ // Nastavení chybových SMS zpráv
      'sms_bad_price'    => 'Litujeme, ale poslali jste SMS za :REALPRICE:Kc misto SMS za :PRICE:Kc.',
      'sms_not_found'    => 'Litujeme, ale poslali jste SMS, ktera nebyla automaticky rozpoznana. Kontaktujte prosim administratory.'
];

$levels = [ // Nastavení SMS levelů
   [
      'price'       => 99, // Cena SMSky
      'text'       => 'SMARTCRAFT :NICK:', // Text SMSky, :NICK: bude nahrazen skutečným nickem uvedeným v SMS, musí být unikátní v rámci všech platebních levelů
      'answer'    => 'Dekujeme za zakoupeni VIP.', // Odpověď na správně zaslanou SMS
      'commands'    => [ // RCON příkazy vykonané po úspěšném zpracování SMSky, :NICK: bude nahrazen skutečným nickem uvedeným v SMS
         'pex user :NICK: group set VIP',
         'economy give :NICK: 12500000',
         'say Uzivatel :NICK: si prave zakoupil VIP.'
      ]
   ], [
      'price'       => 149,
      'text'       => 'SCRAFT :NICK:',
      'answer'    => 'Dekujeme za zakoupeni ExtraVIP.',
      'commands'    => [
         'pex user :NICK: group set ExtraVIP',
         'economy give :NICK: 25000000',
         'say Uzivatel :NICK: si prave zakoupil ExtraVIP.'
      ]
   ], [
      'price'       => 299,
      'text'       => 'SCT :NICK:',
      'answer'    => 'Dekujeme za zakoupeni TrialOP.',
      'commands'    => [
         'pex user :NICK: group set TrialOP',
         'economy give :NICK: 50000000',
         'say Uzivatel :NICK: si prave zakoupil TrialOP.'
      ]
   ]
];

/* Do not make any changes below this line if you don't know what are you doing! */
$rcon = new Rcon($settings->ip, $settings->port, $settings->password, 3);
if (!$rcon->connect()) exit('Cannot connect to server RCON. Please check your settings.');
$url = (object)filter_input_array(INPUT_GET);
try {
   if (isset($url->shortcode)) {
      $url->sms = trim($url->sms);
      if (mb_strlen($url->shortcode) === 7) {
         $realPrice = (int)substr($url->shortcode, -2, 2);
         foreach ($levels as $level) {
            $level = (object)$level;
            if ($level->price >= 100) continue;
            $realNick = explode(' ', $url->sms)[array_search(':NICK:', explode(' ', $level->text))];
            if (strtolower($url->sms) === strtolower(str_replace(':NICK:', $realNick, $level->text))) {
               if ($realPrice === $level->price) {
                  foreach ($level->commands as $command) $rcon->send_command(str_replace(':NICK:', $realNick, $command));
                  File::write('./mobilni-platby.log', date('d. m. Y H:i:s', time()) . " => [SUCCESS] SMS zpráva s obsahem '$url->sms' s cenou {$realPrice}Kč byla úspěšně zpracována." . PHP_EOL);
                  header('Content-type:text/plain');
                  header('Content-length:' . mb_strlen($level->answer));
                  exit($level->answer);
               } else {
                  File::write('./mobilni-platby.log', date('d. m. Y H:i:s', time()) . " => [!ERROR!] SMS zpráva s obsahem '$url->sms' s cenou {$realPrice}Kč nebyla zpracována, protože měla mít cenu {$level->price}Kč." . PHP_EOL);
                  $answer = str_replace(':PRICE:', $level->price, str_replace(':REALPRICE:', $realPrice, $messages->sms_bad_price));
                  header('Content-type:text/plain');
                  header('Content-length:' . mb_strlen($answer));
                  exit($answer);
               }
            }
         }
         File::write('./mobilni-platby.log', date('d. m. Y H:i:s', time()) . " => [!ERROR!] SMS zpráva s obsahem '$url->sms' s cenou {$realPrice}Kč nebyla zpracována, protože nebyla rozpoznána." . PHP_EOL);
         header('Content-type:text/plain');
         header('Content-length:' . mb_strlen($messages->sms_not_found));
         exit($messages->sms_not_found);
      } else {
         foreach ($levels as $level) {
            $level = (object)$level;
            if ($level->price <= 99) continue;
            $realNick = explode(' ', $url->sms)[array_search(':NICK:', explode(' ', $level->text))];
            if (strtolower($url->sms) === strtolower(str_replace(':NICK:', $realNick, $level->text))) {
               $file = "./database/$url->id.txt";
               $commands = NULL;
               foreach ($level->commands as $command) $commands .= str_replace(':NICK:', $realNick, $command) . ';;;';
               File::write($file, $commands);
               $level->answer .= ";90333$level->price";
               File::write('./mobilni-platby.log', date('d. m. Y H:i:s', time()) . " => [SUCCESS] SMS zpráva ID $url->id s obsahem '$url->sms' s cenou {$level->price}Kč byla úspěšně zpracována." . PHP_EOL);
               header('Content-type:text/plain');
               header('Content-length:' . mb_strlen($level->answer));
               exit($level->answer);
            }
         }
         File::write('./mobilni-platby.log', date('d. m. Y H:i:s', time()) . " => [!ERROR!] SMS zpráva ID $url->id s obsahem '$url->sms' s cenou {$level->price}Kč nebyla zpracována, protože nebyla rozpoznána." . PHP_EOL);
         header('Content-type:text/plain');
         header('Content-length:' . mb_strlen($messages->sms_not_found));
         exit("$messages->sms_not_found;FREE90333199");
      }
   } elseif (isset($url->request)) {
      if ($url->status === 'DELIVERED') {
         $file = "./database/$url->request.txt";
         if (is_array($commands = explode(';;;', File::read($file)))) foreach ($commands as $command) $rcon->send_command($command);
         File::remove($file);
         File::write('./mobilni-platby.log', date('d. m. Y H:i:s', time()) . " => [SUCCESS] Doručenka SMS zprávy ID $url->request byla úspěšně zpracována." . PHP_EOL);
      } else File::write('./mobilni-platby.log', date('d. m. Y H:i:s', time()) . " => [!ERROR!] Doručenka SMS zprávy ID $url->request nebyla zpracována, protože přišla se statusem $url->status ($url->message)." . PHP_EOL);
      header('HTTP/1.1 204 No Content');
      exit;
   }
} catch (\FileException $e) { exit($e->getMessage()); }
class File {
   public static function read($source, $mode = 'r') {
      if (file_exists($source)) {
         if ($file = fopen($source, $mode)) {
            if ($content = fread($file, filesize($source))) {
               if (fclose($file)) {
                  return $content;
               } else throw new \FileException("Cannot close file $source.");
            } else throw new \FileException("Cannot read file $source.");
         } else throw new \FileException("Cannot open file $source.");
      } else throw new \FileException("Cannot find file $source.");
   }

   public static function write($destination, $content, $mode = 'a') {
      if (!is_dir(dirname($destination))) if (!mkdir(dirname($destination), 0777, TRUE)) throw new \FileException("Cannot create path $destination.");
      if ($file = fopen($destination, $mode)) {
         if (fwrite($file, $content)) {
            if (fclose($file)) {
               return TRUE;
            } else throw new \FileException("Cannot close file $destination.");
         } else throw new \FileException("Cannot write file $destination.");
      } else throw new \FileException("Cannot find file $destination.");
   }

   public static function remove($source) {
      if (file_exists($source)) {
         if (unlink($source)) {
            return TRUE;
         } else throw new \FileException("Cannot remove file $source.");
      } else throw new \FileException("Cannot find file $source.");
   }
}
class FileException extends \Exception { }
class Rcon {
   private $host;
   private $port;
   private $password;
   private $timeout;
   private $socket;
   private $authorized;
   private $last_response;

   const PACKET_AUTHORIZE = 5;
   const PACKET_COMMAND = 6;
   const SERVERDATA_AUTH = 3;
   const SERVERDATA_AUTH_RESPONSE = 2;
   const SERVERDATA_EXECCOMMAND = 2;
   const SERVERDATA_RESPONSE_VALUE = 0;

   public function __construct($host, $port, $password, $timeout) {
      $this->host = $host;
      $this->port = $port;
      $this->password = $password;
      $this->timeout = $timeout;
   }

   public function get_response() {
      return $this->last_response;
   }

   public function connect() {

      $this->socket = fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);

      if (!$this->socket) {
         $this->last_response = $errstr;
         return false;
      }

      //set timeout
      stream_set_timeout($this->socket, 3, 0);

      //authorize
      $auth = $this->authorize();

      if ($auth) {
         return true;
      }

      return false;
   }

   public function disconnect() {
      if ($this->socket) {
         fclose($this->socket);
      }
   }

   public function is_connected() {
      return $this->authorized;
   }

   public function send_command($command) {
      if (!$this->is_connected()) return false;

      // send command packet.
      $this->write_packet(Rcon::PACKET_COMMAND, Rcon::SERVERDATA_EXECCOMMAND, $command);

      // get response.
      $response_packet = $this->read_packet();
      if ($response_packet['id'] == Rcon::PACKET_COMMAND) {
         if ($response_packet['type'] == Rcon::SERVERDATA_RESPONSE_VALUE) {
            $this->last_response = $response_packet['body'];
            return $response_packet['body'];
         }
      }

      return false;
   }

   private function authorize() {
      $this->write_packet(Rcon::PACKET_AUTHORIZE, Rcon::SERVERDATA_AUTH, $this->password);
      $response_packet = $this->read_packet();

      if ($response_packet['type'] == Rcon::SERVERDATA_AUTH_RESPONSE) {
         if ($response_packet['id'] == Rcon::PACKET_AUTHORIZE) {
            $this->authorized = true;
            return true;
         }
      }

      $this->disconnect();
      return false;
   }

   /**
    * Writes a packet to the socket stream..
    */
   private function write_packet($packet_id, $packet_type, $packet_body) {
      /*
        Size         32-bit little-endian Signed Integer       Varies, see below.
        ID            32-bit little-endian Signed Integer      Varies, see below.
        Type         32-bit little-endian Signed Integer      Varies, see below.
        Body         Null-terminated ASCII String         Varies, see below.
        Empty String   Null-terminated ASCII String         0x00
       */

      //create packet
      $packet = pack("VV", $packet_id, $packet_type);
      $packet = $packet . $packet_body . "\x00";
      $packet = $packet . "\x00";

      // get packet size.
      $packet_size = strlen($packet);

      // attach size to packet.
      $packet = pack("V", $packet_size) . $packet;

      // write packet.
      fwrite($this->socket, $packet, strlen($packet));
   }

   private function read_packet() {
      //get packet size.
      $size_data = fread($this->socket, 4);
      $size_pack = unpack("V1size", $size_data);
      $size = $size_pack['size'];

      // if size is > 4096, the response will be in multiple packets.
      // this needs to be address. get more info about multi-packet responses
      // from the RCON protocol specification at
      // https://developer.valvesoftware.com/wiki/Source_RCON_Protocol
      // currently, this script does not support multi-packet responses.

      $packet_data = fread($this->socket, $size);
      $packet_pack = unpack("V1id/V1type/a*body", $packet_data);

      return $packet_pack;
   }
}

Uživatelský avatar
Mysteria
Příspěvky: 9122
Registrován: 26 pro 2009, 15:40
Reputation: 7
Bydliště: Česká republika
Kontaktovat uživatele:

Re: [PHP] MobilniPlatby.cz [1.0.0-beta2]

#70 Příspěvekod Mysteria » 30 srp 2014, 23:32

Zase tohle? Nemáš náhodou Wedos?


Zpět na „Návody“

Kdo je online

Uživatelé prohlížející si toto fórum: Žádní registrovaní uživatelé a 8 hostů