PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/MinecraftInterface.class.php

https://github.com/leemcd56/Minecraft-PHP-Client-2
PHP | 135 lines | 84 code | 14 blank | 37 comment | 10 complexity | 8ffbc5684dbf7330b3e6b9da09065364 MD5 | raw file
Possible License(s): WTFPL
  1. <?php
  2. /*
  3. -
  4. / \
  5. / \
  6. / MINECRAFT \
  7. / PHP \
  8. |\ CLIENT /|
  9. |. \ 2 / .|
  10. | .. \ / .. |
  11. | .. | .. |
  12. | .. | .. |
  13. \ | /
  14. \ | /
  15. \ | /
  16. \ | /
  17. by @shoghicp
  18. DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  19. Version 2, December 2004
  20. Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
  21. Everyone is permitted to copy and distribute verbatim or modified
  22. copies of this license document, and changing it is allowed as long
  23. as the name is changed.
  24. DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  25. TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
  26. 0. You just DO WHAT THE FUCK YOU WANT TO.
  27. */
  28. class MinecraftInterface{
  29. var $pstruct, $name, $server, $protocol;
  30. function __construct($server, $protocol = CURRENT_PROTOCOL, $port = 25565, $listen = false){
  31. $this->server = new Socket($server, $port, (bool) $listen);
  32. $this->protocol = (int) $protocol;
  33. require("pstruct/".$this->protocol.".php");
  34. require("pstruct/packetName.php");
  35. $this->pstruct = $pstruct;
  36. $this->name = $packetName;
  37. }
  38. public function close(){
  39. return $this->server->close();
  40. }
  41. protected function getStruct($pid){
  42. if(isset($this->pstruct[$pid])){
  43. return $this->pstruct[$pid];
  44. }
  45. return false;
  46. }
  47. protected function writeDump($pid, $raw, $data, $origin = "client"){
  48. if(LOG === true and DEBUG >= 2){
  49. $p = "[".microtime(true)."] [".($origin === "client" ? "CLIENT->SERVER":"SERVER->CLIENT")."]: ".$this->name[$pid]." (0x".Utils::strToHex(chr($pid)).") [lenght ".strlen($raw)."]".PHP_EOL;
  50. $p .= Utils::hexdump($raw);
  51. if(is_array($data)){
  52. foreach($data as $i => $d){
  53. $p .= $i ." => ".(!is_array($d) ? $this->pstruct[$pid][$i]."(".(($this->pstruct[$pid][$i] === "byteArray" or $this->pstruct[$pid][$i] === "newChunkArray" or $this->pstruct[$pid][$i] === "chunkArray" or $this->pstruct[$pid][$i] === "chunkInfo" or $this->pstruct[$pid][$i] === "multiblockArray" or $this->pstruct[$pid][$i] === "newMultiblockArray") ? Utils::strToHex($d):$d).")":$this->pstruct[$pid][$i]."(***)").PHP_EOL;
  54. }
  55. }
  56. $p .= PHP_EOL;
  57. logg($p, "packets", false);
  58. }
  59. }
  60. public function readPacket($mode = false){
  61. if($this->server->connected === false){
  62. return array("pid" => 0xff, "data" => array(0 => "Connection error", 1 => true));
  63. }
  64. $pid = $this->server->read(1, $mode);
  65. if($pid == ""){
  66. return false;
  67. }
  68. $pid = ord($pid);
  69. $struct = $this->getStruct($pid);
  70. if($struct === false){
  71. $this->server->unblock();
  72. $p = "[".microtime(true)."] [SERVER->CLIENT]: Error, bad packet id 0x".Utils::strToHex(chr($pid)).PHP_EOL;
  73. $p .= Utils::hexdump(chr($pid).$this->server->read(1024, true));
  74. $p .= PHP_EOL . "--------------- (1024 byte max extract) ----------" .PHP_EOL;
  75. logg($p, "packets", true, 3);
  76. $this->buffer = "";
  77. $this->server->receive("\xff".Utils::writeString("Bad packet id 0x".Utils::strToHex(chr($pid))));
  78. $this->writePacket(0xff, array(0 => "Bad packet id 0x".Utils::strToHex(chr($pid))));
  79. return array("pid" => 0xff, "data" => array(0 => "Bad packet id 0x".Utils::strToHex(chr($pid))));
  80. }
  81. $packet = new Packet($pid, $struct, $this->server);
  82. $packet->protocol = $this->protocol;
  83. $packet->parse();
  84. $this->writeDump($pid, $packet->raw, $packet->data, "server");
  85. return array("pid" => $pid, "data" => $packet->data, "raw" => $packet->raw);
  86. }
  87. public function writePacket($pid, $data = array(), $raw = false){
  88. $struct = $this->getStruct($pid);
  89. if($this->protocol >= 32){
  90. if($pid === 0x01){
  91. $struct = array();
  92. }elseif($pid === 0x09){
  93. $struct = array();
  94. }
  95. }
  96. if($raw === false){
  97. $packet = new Packet($pid, $struct);
  98. $packet->protocol = $this->protocol;
  99. $packet->data = $data;
  100. $packet->create();
  101. $write = $this->server->write($packet->raw);
  102. $this->writeDump($pid, $packet->raw, $data, "client");
  103. }else{
  104. $write = $this->server->write($data);
  105. $this->writeDump($pid, $data, false, "client");
  106. }
  107. return true;
  108. }
  109. }
  110. ?>