SHOW:
|
|
- or go back to the newest paste.
1 | <?php | |
2 | // stat.php - returns server status for a CSRV chat server | |
3 | // customize for fit your needs | |
4 | // -Patrick | |
5 | ||
6 | /* THIS IS HOW SOCKETS ARE USED [for reference] | |
7 | * $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); | |
8 | * $result = socket_connect($socket, $address, $service_port); | |
9 | * socket_write($socket, $in, strlen($in)); | |
10 | * $out = socket_read($socket, 2048); | |
11 | */ | |
12 | ||
13 | // some variables we'll need to connect to the server | |
14 | $addr = $_GET['addr'] or "127.0.0.1"; // address of the server to connect to | |
15 | $port = $_GET['port'] or 2303; // port to use when connecting | |
16 | $port = 2303; | |
17 | ||
18 | $user = $_GET['user'] or // the user used to connect to the server | |
19 | die("ERROR: need to specify a user\n"); | |
20 | $pass = $_GET['pass'] or // the user's password | |
21 | die("ERROR: need to specify a pass\n"); | |
22 | ||
23 | // TODO: specify which stats to get | |
24 | ||
25 | // some useful functions for talking to the server | |
26 | // connect to server | |
27 | function connect($addr, $port) { | |
28 | $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); | |
29 | if(!$socket) { | |
30 | die("ERROR: can't make a socket\n"); | |
31 | } | |
32 | if(!socket_connect($socket, $addr, $port)) { | |
33 | die("ERROR: con't connect to server\n"); | |
34 | } | |
35 | return $socket; | |
36 | } | |
37 | ||
38 | // send data to the server | |
39 | function send($socket, $text) { | |
40 | socket_write($socket, $text, strlen($text)); | |
41 | } | |
42 | ||
43 | // read from server | |
44 | function read($socket) { | |
45 | return socket_read($socket, 2048); | |
46 | } | |
47 | ||
48 | function verify($socket) { | |
49 | $response = read($socket); | |
50 | if(rtrim($response) != "WELCOME") { | |
51 | echo "ERROR: wrong response from server\n"; | |
52 | die($response); | |
53 | } | |
54 | return true; | |
55 | } | |
56 | ||
57 | // log in to server | |
58 | function login($socket, $user, $pass) { | |
59 | // send the server which username we're using | |
60 | send($socket, "USER ".$user."\r\n"); | |
61 | $response = read($socket); | |
62 | if(rtrim($response) != ("STATUS USER ".$user)) { | |
63 | echo "ERROR: wrong response to USER\n"; | |
64 | die($response); | |
65 | } | |
66 | send($socket, "PASS ".$pass."\r\n"); | |
67 | $response = read($socket); | |
68 | if(rtrim($response) != ("STATUS LOGIN !".$user)) { | |
69 | die("ERROR: user can't be logged in\n"); | |
70 | } | |
71 | } | |
72 | ||
73 | function logout($socket) { | |
74 | send($socket, "QUIT\r\n"); | |
75 | } | |
76 | ||
77 | // close connection | |
78 | function close_connection($socket) { | |
79 | socket_close($socket); | |
80 | } | |
81 | ||
82 | // actual code, this is where we use these functions | |
83 | // establish a connection | |
84 | $conn = connect($addr, $port); | |
85 | // verify the connection | |
86 | verify($conn); | |
87 | // log in | |
88 | login($conn, $user, $pass); | |
89 | ||
90 | // send some stat command | |
91 | send($conn, "USERLIST\r\n"); | |
92 | echo read($conn); | |
93 | ||
94 | // close the connection | |
95 | close_connection($conn); | |
96 | ?> |