Skip to content

Commit b130fcb

Browse files
Navarr BarnierNavarr Barnier
Navarr Barnier
authored and
Navarr Barnier
committed
Composer Support
* Create xPaw Namespace * Use PSR-4 autoloading standard
1 parent 51f1848 commit b130fcb

9 files changed

+151
-115
lines changed

Diff for: .gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor/
2+
composer.lock
3+
# Composer.lock should normally be included but is not recommended for libraries

Diff for: README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ Minecraft implements [Source RCON protocol](https://developer.valvesoftware.com/
1818
## Example
1919
```php
2020
<?php
21-
require __DIR__ . '/MinecraftQuery.class.php';
21+
require __DIR__ . '/src/MinecraftQuery.php';
22+
require __DIR__ . '/src/MinecraftQueryException.php';
23+
24+
use xPaw\MinecraftQuery;
2225

2326
$Query = new MinecraftQuery( );
2427

Diff for: composer.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "xpaw/php-minecraft-query",
3+
"description": "A Minecraft Server query handler for php",
4+
"license": "CC BY-NC-SA 3.0",
5+
"keywords": ["minecraft"],
6+
"require": {
7+
"php": ">=5.3.0"
8+
},
9+
"autoload": {
10+
"psr-4": { "xPaw\\": "src" }
11+
}
12+
}
+43-45
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
<?php
2-
class MinecraftPingException extends Exception
3-
{
4-
//
5-
}
2+
3+
namespace xPaw;
64

75
class MinecraftPing
86
{
@@ -27,104 +25,104 @@ class MinecraftPing
2725
* echo '<img width="64" height="64" src="' . Str_Replace( "\n", "", $Info[ 'favicon' ] ) . '">';
2826
*
2927
*/
30-
28+
3129
private $Socket;
3230
private $ServerAddress;
3331
private $ServerPort;
3432
private $Timeout;
35-
33+
3634
public function __construct( $Address, $Port = 25565, $Timeout = 2 )
3735
{
3836
$this->ServerAddress = $Address;
3937
$this->ServerPort = (int)$Port;
4038
$this->Timeout = (int)$Timeout;
41-
39+
4240
$this->Connect( );
4341
}
44-
42+
4543
public function __destruct( )
4644
{
4745
$this->Close( );
4846
}
49-
47+
5048
public function Close( )
5149
{
5250
if( $this->Socket !== null )
5351
{
5452
fclose( $this->Socket );
55-
53+
5654
$this->Socket = null;
5755
}
5856
}
59-
57+
6058
public function Connect( )
6159
{
6260
$connectTimeout = $this->Timeout;
6361
$this->Socket = @fsockopen( $this->ServerAddress, $this->ServerPort, $errno, $errstr, $connectTimeout );
64-
62+
6563
if( !$this->Socket )
6664
{
6765
throw new MinecraftPingException( "Failed to connect or create a socket: $errno ($errstr)" );
6866
}
69-
67+
7068
// Set Read/Write timeout
7169
stream_set_timeout( $this->Socket, $this->Timeout );
7270
}
73-
71+
7472
public function Query( )
7573
{
7674
$TimeStart = microtime(true); // for read timeout purposes
77-
75+
7876
// See http://wiki.vg/Protocol (Status Ping)
7977
$Data = "\x00"; // packet ID = 0 (varint)
80-
78+
8179
$Data .= "\x04"; // Protocol version (varint)
8280
$Data .= Pack( 'c', StrLen( $this->ServerAddress ) ) . $this->ServerAddress; // Server (varint len + UTF-8 addr)
8381
$Data .= Pack( 'n', $this->ServerPort ); // Server port (unsigned short)
8482
$Data .= "\x01"; // Next state: status (varint)
85-
83+
8684
$Data = Pack( 'c', StrLen( $Data ) ) . $Data; // prepend length of packet ID + data
87-
85+
8886
fwrite( $this->Socket, $Data ); // handshake
8987
fwrite( $this->Socket, "\x01\x00" ); // status ping
90-
88+
9189
$Length = $this->ReadVarInt( ); // full packet length
92-
90+
9391
if( $Length < 10 )
9492
{
9593
return FALSE;
9694
}
97-
95+
9896
fgetc( $this->Socket ); // packet type, in server ping it's 0
99-
97+
10098
$Length = $this->ReadVarInt( ); // string length
101-
99+
102100
$Data = "";
103101
do
104102
{
105103
if (microtime(true) - $TimeStart > $this->Timeout)
106104
{
107105
throw new MinecraftPingException( 'Server read timed out' );
108106
}
109-
107+
110108
$Remainder = $Length - StrLen( $Data );
111109
$block = fread( $this->Socket, $Remainder ); // and finally the json string
112110
// abort if there is no progress
113111
if (!$block)
114112
{
115113
throw new MinecraftPingException( 'Server returned too few data' );
116114
}
117-
115+
118116
$Data .= $block;
119117
} while( StrLen($Data) < $Length );
120-
118+
121119
if( $Data === FALSE )
122120
{
123121
throw new MinecraftPingException( 'Server didn\'t return any data' );
124122
}
125-
123+
126124
$Data = JSON_Decode( $Data, true );
127-
125+
128126
if( JSON_Last_Error( ) !== JSON_ERROR_NONE )
129127
{
130128
if( Function_Exists( 'json_last_error_msg' ) )
@@ -135,32 +133,32 @@ public function Query( )
135133
{
136134
throw new MinecraftPingException( 'JSON parsing failed' );
137135
}
138-
136+
139137
return FALSE;
140138
}
141-
139+
142140
return $Data;
143141
}
144-
142+
145143
public function QueryOldPre17( )
146144
{
147145
fwrite( $this->Socket, "\xFE\x01" );
148146
$Data = fread( $this->Socket, 512 );
149147
$Len = StrLen( $Data );
150-
148+
151149
if( $Len < 4 || $Data[ 0 ] !== "\xFF" )
152150
{
153151
return FALSE;
154152
}
155-
153+
156154
$Data = SubStr( $Data, 3 ); // Strip packet header (kick message packet and short length)
157155
$Data = iconv( 'UTF-16BE', 'UTF-8', $Data );
158-
156+
159157
// Are we dealing with Minecraft 1.4+ server?
160158
if( $Data[ 1 ] === "\xA7" && $Data[ 2 ] === "\x31" )
161159
{
162160
$Data = Explode( "\x00", $Data );
163-
161+
164162
return Array(
165163
'HostName' => $Data[ 3 ],
166164
'Players' => IntVal( $Data[ 4 ] ),
@@ -169,9 +167,9 @@ public function QueryOldPre17( )
169167
'Version' => $Data[ 2 ]
170168
);
171169
}
172-
170+
173171
$Data = Explode( "\xA7", $Data );
174-
172+
175173
return Array(
176174
'HostName' => SubStr( $Data[ 0 ], 0, -1 ),
177175
'Players' => isset( $Data[ 1 ] ) ? IntVal( $Data[ 1 ] ) : 0,
@@ -180,36 +178,36 @@ public function QueryOldPre17( )
180178
'Version' => '1.3'
181179
);
182180
}
183-
181+
184182
private function ReadVarInt( )
185183
{
186184
$i = 0;
187185
$j = 0;
188-
186+
189187
while( true )
190188
{
191189
$k = @fgetc( $this->Socket );
192-
190+
193191
if( $k === FALSE )
194192
{
195193
return 0;
196194
}
197-
195+
198196
$k = Ord( $k );
199-
197+
200198
$i |= ( $k & 0x7F ) << $j++ * 7;
201-
199+
202200
if( $j > 5 )
203201
{
204202
throw new MinecraftPingException( 'VarInt too big' );
205203
}
206-
204+
207205
if( ( $k & 0x80 ) != 128 )
208206
{
209207
break;
210208
}
211209
}
212-
210+
213211
return $i;
214212
}
215213
}

Diff for: src/MinecraftPingException.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace xPaw;
4+
5+
class MinecraftPingException extends \Exception
6+
{
7+
// Exception thrown by MinecraftPing class
8+
}

0 commit comments

Comments
 (0)