~ubuntu-branches/ubuntu/hoary/moodle/hoary

« back to all changes in this revision

Viewing changes to mod/chat/gui_sockets/arscd.php

  • Committer: Bazaar Package Importer
  • Author(s): Isaac Clerencia
  • Date: 2004-12-29 00:49:52 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20041229004952-gliyqzpj2w3e7clx
Tags: 1.4.3-1
* Urgency high as upstream release fixes several security bugs
* New upstream release
* Write database creation errors and warn the user about it, 
closes: #285842, #285842

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/php4 -q
2
 
<?php
3
 
@set_time_limit (0);
4
 
set_magic_quotes_runtime(0);
5
 
 
6
 
include("../config.inc.php");
7
 
include("../functions.inc.php");
8
 
include("../filter.inc.php");
9
 
 
10
 
 
11
 
// Checking parameters
12
 
 
13
 
if ($argv[1] == "--help" OR $argv[1] == "-h" OR $argv[1] == "/?")
14
 
{
15
 
 echo "Starts the ARSC socket server on port {$arsc_parameters["socketserver_port"]}.";
16
 
 echo "\n\n";
17
 
 echo "Usage: arscd.php [-v|-l|--help]\n\n";
18
 
 echo "Example:\n";
19
 
 echo "  arscd.php -l=/var/log/arsc\n\n";
20
 
 echo "Options:\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";
24
 
 echo "\n";
25
 
 echo "For bug reporting, please visit:";
26
 
 echo "<URL:http://manuel.kiessling.net/projects/software/arsc/bugs/>.\n";
27
 
 die();
28
 
}
29
 
$arsc_logdir = false;
30
 
if (ereg("-l=", $argv[1]))
31
 
{
32
 
 if (is_dir(str_replace("-l=", "", $argv[1])))
33
 
 {
34
 
  $arsc_logdir = str_replace("-l=", "", $argv[1]);
35
 
  echo "Logging into '".$arsc_logdir."'\n";
36
 
 }
37
 
 else
38
 
 {
39
 
  die ("Cannot log into '".str_replace("-l=", "", $argv[1])."'. Aborting.\n");
40
 
 }
41
 
}
42
 
 
43
 
 
44
 
// Creating socket
45
 
 
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");
50
 
 
51
 
socket_set_nonblock($arsc_listen_socket);
52
 
 
53
 
$arsc_connected_clients = 0;
54
 
$arsc_connections = array();
55
 
$arsc_connection_info = array();
56
 
$arsc_sid = array();
57
 
 
58
 
echo date("[Y-m-d H:i:s]")." {SOCK} Started ARSC server listening on port ".$arsc_parameters["socketserver_port"].".\n";
59
 
 
60
 
while(1) // Handling connections in a neverending loop
61
 
{
62
 
 $arsc_socket_set = array_merge($arsc_listen_socket, $arsc_connections);
63
 
 if(socket_select($arsc_socket_set, $a = NULL, $b = NULL, 0, 0))
64
 
 {
65
 
  foreach($arsc_connections as $arsc_connection)
66
 
  {
67
 
   if(!($arsc_connection == $arsc_listen_socket))
68
 
   {
69
 
    foreach($arsc_connection_info as $arsc_num => $arsc_info)
70
 
    {
71
 
     if($arsc_connection == $arsc_info['handle'])
72
 
     {
73
 
      if ($arsc_sid[$arsc_num] == "")
74
 
      {
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)
78
 
       {
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] <> "")
83
 
        {
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);
93
 
        }
94
 
        else
95
 
        {
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]);
102
 
         flush();
103
 
        }
104
 
       }
105
 
      }
106
 
      else
107
 
      {
108
 
       $arsc_newmessages = arsc_getmessages($arsc_sid[$arsc_num]);
109
 
       if ($arsc_newmessages <> "")
110
 
       {
111
 
        if (!arsc_socket_write($arsc_connection, $arsc_newmessages))
112
 
        {
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]);
124
 
         flush();
125
 
        }
126
 
       }
127
 
       else
128
 
       {
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]);
140
 
        flush();
141
 
       }
142
 
      }
143
 
     }
144
 
    }
145
 
   }
146
 
  } 
147
 
  // A new client connected
148
 
  if($arsc_connection_info[$arsc_connected_clients]['handle'] = @socket_accept($arsc_listen_socket))
149
 
  {
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";
153
 
   flush();
154
 
   $arsc_connected_clients++;
155
 
  }
156
 
 }
157
 
 usleep($arsc_parameters["socketserver_refresh"]); 
158
 
}
159
 
 
160
 
 
161
 
// Message handling
162
 
 
163
 
function arsc_getmessages($arsc_sid)
164
 
{
165
 
 GLOBAL $arsc_my,
166
 
        $arsc_parameters,
167
 
        $arsc_lang,
168
 
        $argv,
169
 
        $arsc_logdir,
170
 
        $arsc_lastlogmessage,
171
 
        $arsc_num;
172
 
 
173
 
 $arsc_sid = str_replace("/", "", $arsc_sid);
174
 
 
175
 
 if (($arsc_my = arsc_getdatafromsid($arsc_sid)) <> FALSE)
176
 
 {
177
 
  $arsc_room = $arsc_my["room"];
178
 
  if ($arsc_my["level"] < 0)
179
 
  {
180
 
   include("../shared/language/".$arsc_my["language"].".inc.php");
181
 
   switch($arsc_my["level"])
182
 
   {
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);
185
 
               
186
 
   }
187
 
  }
188
 
  else
189
 
  {
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")
195
 
   {
196
 
    $arsc_lastmessageping = arsc_microtime();
197
 
    mysql_query("UPDATE arsc_users SET lastmessageping = '$arsc_lastmessageping' WHERE sid = '$arsc_sid'");
198
 
   }
199
 
   else
200
 
   {
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))
204
 
    {
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")
208
 
     {
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";
210
 
     }
211
 
     elseif($arsc_logdir)
212
 
     {
213
 
      $arsc_logmessage = "[".date("Y-m-d")."] [{$arsc_a["sendtime"]}] {$arsc_a["user"]}: {$arsc_a["message"]}\n";
214
 
      if ($arsc_lastlogmessage != $arsc_logmessage)
215
 
      {
216
 
       $arsc_lastlogmessage = $arsc_logmessage;
217
 
       $arsc_logresource = "fp_".$arsc_room;
218
 
       if(is_resource($$arsc_logresource))
219
 
       {
220
 
        fputs($$arsc_logresource, $arsc_logmessage);
221
 
       }
222
 
       else
223
 
       {
224
 
        if ($$arsc_logresource = fopen($arsc_logdir."/".$arsc_room.".log", "a"))
225
 
        {
226
 
         fputs($$arsc_logresource, $arsc_logmessage);
227
 
        }
228
 
        else
229
 
        {
230
 
         echo "Error: cannot open logfile '".$arsc_logdir."/".$arsc_room.".log', disable logging.\n";
231
 
         $arsc_logdir = false;
232
 
        }
233
 
       }
234
 
      }
235
 
     }
236
 
    }
237
 
    $arsc_ping = time();
238
 
    mysql_query("UPDATE arsc_users SET lastping = '$arsc_ping', lastmessageping = '$arsc_lastmessageping' WHERE sid = '$arsc_sid'");
239
 
   }
240
 
   return $arsc_posting;
241
 
  }
242
 
 }
243
 
}
244
 
 
245
 
 
246
 
// Helpers
247
 
function arsc_socket_write(&$connection, $text)
248
 
{
249
 
 $return = false;
250
 
 $check_socket = array($connection);
251
 
 $socket_changed = socket_select($read = NULL, $check_socket, $except = NULL, 0, 0);
252
 
 if ($socket_changed > 0)
253
 
 {
254
 
  $return = true;
255
 
  @socket_write($connection, $text, strlen($text));
256
 
 }
257
 
 else
258
 
 {
259
 
  echo date("[Y-m-d H:i:s]")." {SOCK} Socket not ready for write, closing connection.\n";
260
 
 }
261
 
 return $return;
262
 
}
263
 
 
264
 
?>