From 35e102fe570f040ffb392655a7ef9a736d161969 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Wilfried=20G=C3=B6esgens?= Date: Sun, 4 Feb 2007 20:28:18 +0000 Subject: [PATCH] * added debug protocol flag * added nice output of our raw talk to the server if the above flag is set * make more use of the php array manipulation functions and tokenizers * examine the output of some commands more complete --- ctdlphp/{config.php => config_ctdlclient.php} | 3 +- ctdlphp/ctdlheader.php | 1 + ctdlphp/ctdlprotocol.php | 304 +++++++++++------- ctdlphp/ctdlsession.php | 18 +- ctdlphp/sessionproxy.php | 2 +- ctdlphp/sitestyle.css | 18 ++ 6 files changed, 219 insertions(+), 127 deletions(-) rename ctdlphp/{config.php => config_ctdlclient.php} (67%) diff --git a/ctdlphp/config.php b/ctdlphp/config_ctdlclient.php similarity index 67% rename from ctdlphp/config.php rename to ctdlphp/config_ctdlclient.php index ff8b76744..ed65575dc 100644 --- a/ctdlphp/config.php +++ b/ctdlphp/config_ctdlclient.php @@ -6,5 +6,6 @@ define('CITADEL_HOSTNAME',"127.0.0.1"); #make it 0 to use unix domain sockets define('CITADEL_TCP_PORTNO','504'); - +#do you want to see the server conversation for exploring the protocol? +define('CITADEL_DEBUG_CITPROTO',1); ?> \ No newline at end of file diff --git a/ctdlphp/ctdlheader.php b/ctdlphp/ctdlheader.php index 9e8dfde61..43c0f335b 100644 --- a/ctdlphp/ctdlheader.php +++ b/ctdlphp/ctdlheader.php @@ -14,6 +14,7 @@ // pages in the system then only have to include ctdlheader.php (since it // is required) and they get the others automatically. // +include "config.php"; include "ctdlsession.php"; include "ctdlprotocol.php"; include "ctdlelements.php"; diff --git a/ctdlphp/ctdlprotocol.php b/ctdlphp/ctdlprotocol.php index 49aa4e47e..ad7c6707c 100644 --- a/ctdlphp/ctdlprotocol.php +++ b/ctdlphp/ctdlprotocol.php @@ -1,21 +1,25 @@ -// This program is released under the terms of the GNU General Public License. - - +// One program is released under the terms of the GNU General Public License. +include "config_ctdlclient.php"; // // serv_gets() -- generic function to read one line of text from the server // -function serv_gets() { +function serv_gets($readblock=FALSE) { global $clientsocket; $buf = fgets($clientsocket, 4096); // Read line $buf = substr($buf, 0, (strlen($buf)-1) ); // strip trailing LF + if (CITADEL_DEBUG_CITPROTO == 1) { + if (!$readblock) printf ("
"); + printf($buf); + if (!$readblock) printf ("
"); + else printf ("
"); + } return $buf; } @@ -28,8 +32,29 @@ function serv_puts($buf) { fwrite($clientsocket, $buf . "\n", (strlen($buf)+1) ); fflush($clientsocket); + if (CITADEL_DEBUG_CITPROTO == 1) + printf ("
".$buf."
"); } +function read_array() { + $nLines = 0; + if (CITADEL_DEBUG_CITPROTO == 1) + printf ("
"); + $buf = serv_gets(TRUE); + $ret = array(); + while (strcasecmp($buf, "000")){ + array_push($ret, $buf); + $buf = serv_gets(TRUE); + $nLines++; + } + if (CITADEL_DEBUG_CITPROTO == 1){ + echo "read ".$nLines." lines from the server."; + printf ("
"); + } + return $ret; +} + + // // text_to_server() -- sends a block of text to the server. Assumes that @@ -52,12 +77,12 @@ function text_to_server($thetext, $convert_to_html) { } // Either mode ... send it to the server now - $this_line = strtok($thetext, "\n"); - while ($this_line !== FALSE) { - $this_line = trim($this_line, "\n\r"); - if ($this_line == "000") $this_line = "-000" ; - serv_puts($this_line); - $this_line = strtok("\n"); + $one_line = strtok($thetext, "\n"); + while ($one_line !== FALSE) { + $one_line = trim($one_line, "\n\r"); + if ($one_line == "000") $one_line = "-000" ; + serv_puts($one_line); + $one_line = strtok("\n"); } serv_puts("000"); // Tell the server we're done... @@ -67,20 +92,22 @@ function text_to_server($thetext, $convert_to_html) { } - - // -// Identify ourselves to the Citadel server (do this once after connection) +// Identify ourselves to the Citadel server (do one once after connection) // -function ctdl_iden() { +function ctdl_iden($client_info) { global $clientsocket; + if (count($client_info) != 5) + die("ctdl_iden takes 5 arguments!"); // Identify client and hostname - serv_puts("IDEN 0|8|001|PHP web client|" . $_SERVER['REMOTE_ADDR'] ); + serv_puts("IDEN ".implode('|', $client_info)); $buf = serv_gets(); +} +function ctdl_MessageFormatsPrefered($formatlist){ // Also express our message format preferences - serv_puts("MSGP text/html|text/plain"); + serv_puts("MSGP ".implode("|", $formatlist)); $buf = serv_gets(); } @@ -151,26 +178,15 @@ function create_new_user($user, $pass) { function become_logged_in($server_parms) { $_SESSION["logged_in"] = 1; - $tok = strtok($server_parms, "|"); - if ($tok) $thisline["username"] = $tok; - - $tok = strtok("|"); - if ($tok) $thisline["axlevel"] = $tok; - - $tok = strtok("|"); - if ($tok) $thisline["calls"] = $tok; - - $tok = strtok("|"); - if ($tok) $thisline["posts"] = $tok; - - $tok = strtok("|"); - if ($tok) $thisline["userflags"] = $tok; - - $tok = strtok("|"); - if ($tok) $thisline["usernum"] = $tok; - - $tok = strtok("|"); - if ($tok) $thisline["lastcall"] = $tok; + $tokens = explode("|", $server_parms); + + $oneline["username"] = $tokens[0]; + $oneline["axlevel"] = $tokens[1]; + $oneline["calls"] = $tokens[2]; + $oneline["posts"] = $tokens[3]; + $oneline["userflags"] = $tokens[4]; + $oneline["usernum"] = $tokens[5]; + $oneline["lastcall"] = $tokens[6]; ctdl_goto("_BASEROOM_"); } @@ -183,27 +199,33 @@ function become_logged_in($server_parms) { // function ctdl_get_serv_info() { serv_puts("INFO"); - $buf = serv_gets(); - if (substr($buf, 0, 1) == "1") { - $i = 0; - do { - $buf = serv_gets(); - if ($i == 1) $_SESSION["serv_nodename"] = $buf; - if ($i == 2) $_SESSION["serv_humannode"] = $buf; - if ($i == 3) $_SESSION["serv_fqdn"] = $buf; - if ($i == 4) $_SESSION["serv_software"] = $buf; - if ($i == 6) $_SESSION["serv_city"] = $buf; - if ($i == 7) $_SESSION["serv_sysadmin"] = $buf; - $i = $i + 1; - } while (strcasecmp($buf, "000")); + $reply = read_array(); + if ((count($reply) == 18) && + substr($reply[0], 0, 1) == "1") { + $server_info=array(); + $server_info["serv_nodename"] = $reply[1]; + $server_info["serv_humannode"] = $reply[2]; + $server_info["serv_fqdn"] = $reply[3]; + $server_info["serv_software"] = $reply[4]; + $server_info["serv_city"] = $reply[6]; + $server_info["serv_sysadmin"] = $reply[7]; + if (CITADEL_DEBUG_CITPROTO == 1) + { + echo "
";
+			print_r($server_info);
+			echo "
"; + } + return $server_info; } + else + die ("didn't understand the reply to the INFO command"); } // // Display a system banner. (Returns completed HTML.) -// (This is probably temporary because it outputs more or less finalized +// (One is probably temporary because it outputs more or less finalized // markup. For now it's just usable.) // function ctdl_mesg($msgname) { @@ -212,16 +234,14 @@ function ctdl_mesg($msgname) { $msgtext = "
\n"; serv_puts("MESG " . $msgname); - $response = serv_gets(); + $response = read_array(); - if (substr($response, 0, 1) == "1") { - while (strcmp($buf = serv_gets(), "000")) { - $msgtext .= "" . htmlspecialchars($buf) - . "
\n" ; - } + if (substr($response[0], 0, 1) == "1") { + array_shift($response); // throw away the status code. + $msgtext .= "" . implode( "
\n" ,$response); } else { - $msgtext .= "" . substr($response, 4) . "
\n"; + $msgtext .= "" . substr($response[0], 4) . "
\n"; } $msgtext .= "
\n"; @@ -244,29 +264,36 @@ function ctdl_rwho() { $all_lines = array(); $num_lines = 0; + + $responses = read_array(); + foreach ($responses as $response) { + $tokens = explode("|", $response); + $oneline = array(); + + $oneline["session"] = $tokens[0]; + $oneline["user"] = $tokens[1]; + $oneline["room"] = $tokens[2]; + $oneline["host"] = $tokens[3]; + $oneline["client"] = $tokens[4]; + $oneline["idlesince"] = $tokens[5]; + $oneline["lastcmd"] = $tokens[6]; + $oneline["flags"] = $tokens[7]; + $oneline["realname"] = $tokens[8]; + $oneline["realroom"] = $tokens[9]; + $oneline["realhostname"] = $tokens[10]; + $oneline["registered"] = $tokens[11]; - while (strcmp($buf = serv_gets(), "000")) { - - $thisline = array(); - - $tok = strtok($buf, "|"); - if ($tok) $thisline["session"] = $tok; + // IGnore the rest of the fields for now. + if (CITADEL_DEBUG_CITPROTO == 1) + { + echo "
";
+			print_r($oneline);
+			echo "
"; - $tok = strtok("|"); - if ($tok) $thisline["user"] = $tok; - - $tok = strtok("|"); - if ($tok) $thisline["room"] = $tok; - - $tok = strtok("|"); - if ($tok) $thisline["host"] = $tok; - - $tok = strtok("|"); - if ($tok) $thisline["client"] = $tok; + } - // IGnore the rest of the fields for now. - $num_lines = array_push($all_lines, $thisline); + $num_lines = array_push($all_lines, $oneline); } return array($num_lines, $all_lines); @@ -282,13 +309,44 @@ function ctdl_goto($to_where) { serv_puts("GOTO " . $to_where); $response = serv_gets(); - if (substr($response, 0, 1) == "2") { - $_SESSION["room"] = strtok(substr($response, 4), "|"); - return array(TRUE, substr($response, 0, 3)); + $results = explode ("|", $response); + $status_room = array_shift($results); + $status = substr($status_room, 0, 3); + if (substr($status, 0, 1) == "2") { + $room = substr($status_room, 4); + array_unshift($results, $room); + $room_state=array( + "state" => TRUE, + "statereply" => $status, + "roomname" => $results[ 0], + "nunreadmsg" => $results[ 1], + "nmessages" => $results[ 2], + "rinfopresent" => $results[ 3], + "flags" => $results[ 4], + "msgidmax" => $results[ 5], + "msgidreadmax" => $results[ 6], + "ismailroom" => $results[ 7], + "isroomaide" => $results[ 8], + "nnewmessages" => $results[ 9], + "floorid" => $results[10], + "viewselected" => $results[11], + "defaultview" => $results[12], + "istrashcan" => $results[13]); + + $_SESSION["room"] = $room; + if (CITADEL_DEBUG_CITPROTO == 1) + { + echo "
";
+			print_r($room_state);
+			echo "
"; + + } + + return $room_state; } else { - return array(FALSE, substr($response, 0, 3)); + return array("state" => FALSE, "statereply" => $status); } } @@ -302,45 +360,41 @@ function ctdl_knrooms() { global $clientsocket; serv_puts("LKRA"); - $response = serv_gets(); + $results = read_array(); - if (substr($response, 0, 1) != "1") { + if (substr($results[0], 0, 1) != "1") { return array(0, NULL); } - + array_shift($results); $all_lines = array(); $num_lines = 0; - while (strcmp($buf = serv_gets(), "000")) { - - $thisline = array(); + foreach ($results as $result){ + $oneline = array(); + $tokens = explode("|",$result); - $tok = strtok($buf, "|"); - if ($tok) $thisline["name"] = $tok; + $oneline["name"] = $tokens[0]; + $oneline["flags"] = $tokens[1]; + $oneline["floor"] = $tokens[2]; + $oneline["order"] = $tokens[3]; + $oneline["flags2"] = $tokens[4]; + $oneline["access"] = $tokens[5]; - $tok = strtok("|"); - if ($tok) $thisline["flags"] = $tok; - - $tok = strtok("|"); - if ($tok) $thisline["floor"] = $tok; - - $tok = strtok("|"); - if ($tok) $thisline["order"] = $tok; - - $tok = strtok("|"); - if ($tok) $thisline["flags2"] = $tok; - - $tok = strtok("|"); - if ($tok) $thisline["access"] = $tok; - - if ($thisline["access"] & 8) { - $thisline["hasnewmsgs"] = TRUE; + if ($oneline["access"] & 8) { + $oneline["hasnewmsgs"] = TRUE; } else { - $thisline["hasnewmsgs"] = FALSE; + $oneline["hasnewmsgs"] = FALSE; } - $num_lines = array_push($all_lines, $thisline); + if (CITADEL_DEBUG_CITPROTO == 1) + { + echo "
";
+			print_r($oneline);
+			echo "
"; + + } + $num_lines = array_push($all_lines, $oneline); } return array($num_lines, $all_lines); @@ -349,27 +403,28 @@ function ctdl_knrooms() { // -// Fetch the list of messages in this room. +// Fetch the list of messages in one room. // Returns: count, response, message array // function ctdl_msgs($mode, $count) { global $clientsocket; serv_puts("MSGS " . $mode . "|" . $count); - $response = serv_gets(); + $responses = read_array(); + print_r($responses); + + $response = array_shift($responses); + $num_msgs = count($responses); if (substr($response, 0, 1) != "1") { return array(0, substr($response, 4), NULL); } - $msgs = array(); - $num_msgs = 0; - - while (strcmp($buf = serv_gets(), "000")) { - $num_msgs = array_push($msgs, $buf); + if (CITADEL_DEBUG_CITPROTO == 1) + { + printf("found ".$num_msgs." messages."); } - - return array($num_msgs, substr($response, 4), $msgs); + return array($num_msgs, $response, $responses); } @@ -378,17 +433,24 @@ function ctdl_fetch_message($msgnum) { global $clientsocket; serv_puts("MSG4 " . $msgnum); - $response = serv_gets(); + + if (CITADEL_DEBUG_CITPROTO == 1) + printf ("
"); + $response = serv_gets(TRUE); if (substr($response, 0, 1) != "1") { return array(FALSE, substr($response, 4), NULL); } $fields = array(); - while (strcmp($buf = serv_gets(), "000")) { + while (strcmp($buf = serv_gets(TRUE), "000")) { if (substr($buf, 0, 4) == "text") { + if (CITADEL_DEBUG_CITPROTO == 1) + printf ("
\n

Message Body Follows

"); // We're in the text body. New loop here. $fields["text"] = ctdl_msg4_from_server(); + if (CITADEL_DEBUG_CITPROTO == 1) + printf ("
"); return array(TRUE, substr($response, 4), $fields); } else { @@ -400,7 +462,7 @@ function ctdl_fetch_message($msgnum) { return array(FALSE, substr($response, 4), $fields); } -// Support function for ctdl_fetch_message(). This handles the text body +// Support function for ctdl_fetch_message(). This handles the text body // portion of the message, converting various formats to HTML as // appropriate. function ctdl_msg4_from_server() { @@ -410,7 +472,7 @@ function ctdl_msg4_from_server() { $in_body = FALSE; $previous_line = ""; - while (strcmp($buf = serv_gets(), "000")) { + while (strcmp($buf = serv_gets(TRUE), "000")) { if ($in_body == FALSE) { if (strlen($buf) == 0) { $in_body = TRUE; diff --git a/ctdlphp/ctdlsession.php b/ctdlphp/ctdlsession.php index 996975d7a..db56974ef 100644 --- a/ctdlphp/ctdlsession.php +++ b/ctdlphp/ctdlsession.php @@ -62,9 +62,15 @@ function establish_citadel_session() { } // At this point we have a good connection to Citadel. - - ctdl_iden(); // Identify client - + $identity=array( + "DevelNr" => '0', + "ClientID" => '8', + "VersionNumber" => '001', + "ClientInfoString" => 'PHP web client|', + "Remote Address" => $_SERVER['REMOTE_ADDR'] ); + + ctdl_iden($identity); // Identify client + ctdl_MessageFormatsPrefered(array("text/html","text/plain")); if ($_SESSION["username"]) { login_existing_user( $_SESSION["username"], @@ -81,7 +87,11 @@ function establish_citadel_session() { } if (!isset($_SESSION["serv_humannode"])) { - ctdl_get_serv_info(); + $server_info = ctdl_get_serv_info(); + print_r($server_info); + $keys = array_keys($server_info); + foreach ($keys as $key) + $_SESSION[$key] = $server_info[$key]; } // If the user is trying to call up any page other than diff --git a/ctdlphp/sessionproxy.php b/ctdlphp/sessionproxy.php index 2e59e81df..ee919ca99 100755 --- a/ctdlphp/sessionproxy.php +++ b/ctdlphp/sessionproxy.php @@ -1,7 +1,7 @@ #!/usr/bin/php -q #Menu {width:8em;} font-size:small; text-indent: 0; } + +.ctdldbgRead { + color:#ff0000; + background-color:#eee; + border:1px dashed #999; + margin: 15px; + font-size:small; + text-indent: 0; +} + +.ctdldbgWrite { + color:#0000ff; + background-color:#eee; + border:1px dashed #999; + margin: 15px; + font-size:small; + text-indent: 0; +} -- 2.30.2