4
set_magic_quotes_runtime(0);
6
include("../config.inc.php");
7
include("../functions.inc.php");
8
include("../filter.inc.php");
11
// Checking parameters
13
if ($argv[1] == "--help" OR $argv[1] == "-h" OR $argv[1] == "/?")
15
echo "Starts the ARSC socket server on port {$arsc_parameters["socketserver_port"]}.";
17
echo "Usage: arscd.php [-v|-l|--help]\n\n";
19
echo " arscd.php -l=/var/log/arsc\n\n";
21
echo " -v Verbose mode, print every message every user receives to STDOUT\n";
22
echo " -l=DIR Write message logfiles for every room into DIR (creates file for every room)\n";
23
echo " -h, --help Show this help\n";
25
echo "For bug reporting, please visit:";
26
echo "<URL:http://manuel.kiessling.net/projects/software/arsc/bugs/>.\n";
30
if (ereg("-l=", $argv[1]))
32
if (is_dir(str_replace("-l=", "", $argv[1])))
34
$arsc_logdir = str_replace("-l=", "", $argv[1]);
35
echo "Logging into '".$arsc_logdir."'\n";
39
die ("Cannot log into '".str_replace("-l=", "", $argv[1])."'. Aborting.\n");
46
if(false === ($arsc_listen_socket = socket_create_listen((string)$arsc_parameters["socketserver_port"], $arsc_parameters["socketserver_maximumusers"])))
47
die("Couldn't create listening socket on port {$arsc_parameters["socketserver_port"]}.\n");
48
if(false === socket_setopt($arsc_listen_socket, SOL_SOCKET, SO_REUSEADDR, 1))
49
die("Couldn't set socket option\n");
51
socket_set_nonblock($arsc_listen_socket);
53
$arsc_connected_clients = 0;
54
$arsc_connections = array();
55
$arsc_connection_info = array();
58
echo date("[Y-m-d H:i:s]")." {SOCK} Started ARSC server listening on port ".$arsc_parameters["socketserver_port"].".\n";
60
while(1) // Handling connections in a neverending loop
62
$arsc_socket_set = array_merge($arsc_listen_socket, $arsc_connections);
63
if(socket_select($arsc_socket_set, $a = NULL, $b = NULL, 0, 0))
65
foreach($arsc_connections as $arsc_connection)
67
if(!($arsc_connection == $arsc_listen_socket))
69
foreach($arsc_connection_info as $arsc_num => $arsc_info)
71
if($arsc_connection == $arsc_info['handle'])
73
if ($arsc_sid[$arsc_num] == "")
75
$arsc_read_socket = array($arsc_connection);
76
$arsc_socket_changed = socket_select($arsc_read_socket, $write = NULL, $except = NULL, 0, 0);
77
if ($arsc_socket_changed > 0)
79
$received_data = socket_read($arsc_connection, 100);
80
ereg("arsc_sid=(.*) HTTP", $received_data, $a);
81
$arsc_sid[$arsc_num] = $a[1];
82
if ($arsc_sid[$arsc_num] <> "")
84
$arsc_my = arsc_getdatafromsid($arsc_sid[$arsc_num]);
85
echo date("[Y-m-d H:i:s]")." {ARSC} #$arsc_num | Connection is an ARSC client (SID $arsc_sid[$arsc_num], nickname {$arsc_my["user"]}, room {$arsc_my["room"]})\n";
86
arsc_socket_write($arsc_connection, $arsc_parameters["htmlhead_js"]);
87
$arsc_sendtime = date("H:i:s");
88
$arsc_timeid = arsc_microtime();
89
@include("../shared/language/".$arsc_my["language"].".inc.php");
90
$arsc_message = "/msg ".$arsc_my["user"]." ".$arsc_lang["welcome"];
91
$arsc_message = arsc_filter_posting("System", $arsc_sendtime, $arsc_message, $arsc_my["room"], 0);
92
arsc_socket_write($arsc_connection, $arsc_message);
96
$arsc_sid[$arsc_num] = "-1";
97
echo date("[Y-m-d H:i:s]")." {SOCK} #$arsc_num | Connection is invalid\n";
98
$arsc_text = "You don't seem to be a valid ARSC client. Connection closed.";
99
arsc_socket_write($arsc_connection, $arsc_text);
100
unset($arsc_connections[$arsc_num]);
101
unset($arsc_connection_info[$arsc_num]);
108
$arsc_newmessages = arsc_getmessages($arsc_sid[$arsc_num]);
109
if ($arsc_newmessages <> "")
111
if (!arsc_socket_write($arsc_connection, $arsc_newmessages))
113
$arsc_user = $arsc_my["user"];
114
$arsc_room = $arsc_my["room"];
115
$arsc_nice_room = arsc_nice_room($arsc_room);
116
$arsc_timeid = arsc_microtime();
117
$arsc_sendtime = date("H:i:s");
118
mysql_query("DELETE from arsc_users WHERE sid = '{$arsc_sid[$arsc_num]}'");
119
mysql_query("INSERT into arsc_room_$arsc_room (message, user, sendtime, timeid) VALUES ('arsc_user_quit~~$arsc_user~~$arsc_nice_room', 'System', '$arsc_sendtime', '$arsc_timeid')");
120
echo date("[Y-m-d H:i:s]")." {SOCK} #$arsc_num | Client #$arsc_num {$arsc_connection_info[$arsc_num]['address']}:{$arsc_connection_info[$arsc_num]['port']} disconnected\n";
121
echo date("[Y-m-d H:i:s]")." {ARSC} #$arsc_num | Cannot reach user (SID $arsc_sid[$arsc_num], nickname {$arsc_my["user"]}, room {$arsc_my["room"]})\n";
122
unset($arsc_connections[$arsc_num]);
123
unset($arsc_connection_info[$arsc_num]);
129
$arsc_user = $arsc_my["user"];
130
$arsc_room = $arsc_my["room"];
131
$arsc_nice_room = arsc_nice_room($arsc_room);
132
$arsc_timeid = arsc_microtime();
133
$arsc_sendtime = date("H:i:s");
134
mysql_query("DELETE from arsc_users WHERE sid = '{$arsc_sid[$arsc_num]}'");
135
mysql_query("INSERT into arsc_room_$arsc_room (message, user, sendtime, timeid) VALUES ('arsc_user_quit~~$arsc_user~~$arsc_nice_room', 'System', '$arsc_sendtime', '$arsc_timeid')");
136
echo date("[Y-m-d H:i:s]")." {ARSC} #$arsc_num | User no longer known to ARSC (SID was $arsc_sid[$arsc_num])\n";
137
echo date("[Y-m-d H:i:s]")." {SOCK} #$arsc_num | Client {$arsc_connection_info[$arsc_num]['address']}:{$arsc_connection_info[$arsc_num]['port']} disconnected\n";
138
unset($arsc_connections[$arsc_num]);
139
unset($arsc_connection_info[$arsc_num]);
147
// A new client connected
148
if($arsc_connection_info[$arsc_connected_clients]['handle'] = @socket_accept($arsc_listen_socket))
150
$arsc_connections[] = $arsc_connection_info[$arsc_connected_clients]['handle'];
151
socket_getpeername($arsc_connection_info[$arsc_connected_clients]['handle'], &$arsc_connection_info[$arsc_connected_clients]['address'], &$arsc_connection_info[$arsc_connected_clients]['port']);
152
echo date("[Y-m-d H:i:s]")." {SOCK} #$arsc_connected_clients | Connection from {$arsc_connection_info[$arsc_connected_clients]['address']} on port {$arsc_connection_info[$arsc_connected_clients]['port']}\n";
154
$arsc_connected_clients++;
157
usleep($arsc_parameters["socketserver_refresh"]);
163
function arsc_getmessages($arsc_sid)
170
$arsc_lastlogmessage,
173
$arsc_sid = str_replace("/", "", $arsc_sid);
175
if (($arsc_my = arsc_getdatafromsid($arsc_sid)) <> FALSE)
177
$arsc_room = $arsc_my["room"];
178
if ($arsc_my["level"] < 0)
180
include("../shared/language/".$arsc_my["language"].".inc.php");
181
switch($arsc_my["level"])
183
case "-1": mysql_query("DELETE from arsc_users WHERE sid = '$arsc_sid'");
184
return arsc_filter_posting("System", date("H:i:s"), "<font size=\"4\"><b>".$arsc_lang["youwerekicked"]."</b></font>", $arsc_room, 0);
190
$arsc_posting = " \n ";
191
include("../shared/language/".$arsc_my["language"].".inc.php");
192
$arsc_result = mysql_query("SELECT lastmessageping from arsc_users WHERE sid = '$arsc_sid'");
193
$arsc_b = mysql_fetch_array($arsc_result);
194
if ($arsc_b["lastmessageping"] == "0")
196
$arsc_lastmessageping = arsc_microtime();
197
mysql_query("UPDATE arsc_users SET lastmessageping = '$arsc_lastmessageping' WHERE sid = '$arsc_sid'");
201
$arsc_lastmessageping = $arsc_b["lastmessageping"];
202
$arsc_result = mysql_query("SELECT message, user, flag_ripped, sendtime, timeid from arsc_room_$arsc_room WHERE timeid > '$arsc_lastmessageping' ORDER BY timeid ASC, id ASC");
203
while ($arsc_a = mysql_fetch_array($arsc_result))
205
$arsc_posting .= arsc_filter_posting($arsc_a["user"], $arsc_a["sendtime"], $arsc_a["message"], $arsc_room, $arsc_a["flag_ripped"]);
206
$arsc_lastmessageping = $arsc_a["timeid"];
207
if ($argv[1] == "-v")
209
echo date("[Y-m-d H:i:s]")." {MESG} #$arsc_num | Room: $arsc_room | User: {$arsc_a["user"]} | Sendtime: {$arsc_a["sendtime"]} | Message: {$arsc_a["message"]}\n";
213
$arsc_logmessage = "[".date("Y-m-d")."] [{$arsc_a["sendtime"]}] {$arsc_a["user"]}: {$arsc_a["message"]}\n";
214
if ($arsc_lastlogmessage != $arsc_logmessage)
216
$arsc_lastlogmessage = $arsc_logmessage;
217
$arsc_logresource = "fp_".$arsc_room;
218
if(is_resource($$arsc_logresource))
220
fputs($$arsc_logresource, $arsc_logmessage);
224
if ($$arsc_logresource = fopen($arsc_logdir."/".$arsc_room.".log", "a"))
226
fputs($$arsc_logresource, $arsc_logmessage);
230
echo "Error: cannot open logfile '".$arsc_logdir."/".$arsc_room.".log', disable logging.\n";
231
$arsc_logdir = false;
238
mysql_query("UPDATE arsc_users SET lastping = '$arsc_ping', lastmessageping = '$arsc_lastmessageping' WHERE sid = '$arsc_sid'");
240
return $arsc_posting;
247
function arsc_socket_write(&$connection, $text)
250
$check_socket = array($connection);
251
$socket_changed = socket_select($read = NULL, $check_socket, $except = NULL, 0, 0);
252
if ($socket_changed > 0)
255
@socket_write($connection, $text, strlen($text));
259
echo date("[Y-m-d H:i:s]")." {SOCK} Socket not ready for write, closing connection.\n";