]> code.citadel.org Git - citadel.git/blobdiff - ctdlphp/ctdlprotocol.php
* Cleaned up the pages in preparation for handing it off to the extremely
[citadel.git] / ctdlphp / ctdlprotocol.php
index 2a6545187f5e6ff8bd0895d1460fc2120e161b07..2fd38d4f3a16dae766565b65b1bfd3af10d1f0d8 100644 (file)
@@ -30,43 +30,113 @@ function serv_puts($buf) {
        fwrite($clientsocket, $buf . "\n", (strlen($buf)+1) );
 }
 
+//
+// login_existing_user() -- attempt to login using a supplied username/password
+// Returns an array with two variables:
+// 0. TRUE or FALSE to determine success or failure
+// 1. String error message (if relevant)
+//
+function login_existing_user($user, $pass) {
+       global $clientsocket;
+
+       serv_puts("USER " . $user);
+       $resp = serv_gets();
+       if (substr($resp, 0, 1) != "3") {
+               return array(FALSE, substr($resp, 4));
+       }
+
+       serv_puts("PASS " . $pass);
+       $resp = serv_gets();
+       if (substr($resp, 0, 1) != "2") {
+               return array(FALSE, substr($resp, 4));
+       }
+
+       $_SESSION["username"] = $user;
+       $_SESSION["password"] = $pass;
+       become_logged_in(substr($resp, 4));
+
+       return array(TRUE, "Login successful.  Have fun.");
+}
+
+
+//
+// create_new_user() -- attempt to create a new user 
+//                      using a supplied username/password
+// Returns an array with two variables:
+// 0. TRUE or FALSE to determine success or failure
+// 1. String error message (if relevant)
+//
+function create_new_user($user, $pass) {
+       global $clientsocket;
+
+       serv_puts("NEWU " . $user);
+       $resp = serv_gets();
+       if (substr($resp, 0, 1) != "2") {
+               return array(FALSE, substr($resp, 4));
+       }
+
+       serv_puts("SETP " . $pass);
+       $resp = serv_gets();
+       if (substr($resp, 0, 1) != "2") {
+               return array(FALSE, substr($resp, 4));
+       }
+
+       $_SESSION["username"] = $user;
+       $_SESSION["password"] = $pass;
+       become_logged_in(substr($resp, 4));
+
+       return array(TRUE, "Login successful.  Have fun.");
+}
+
+
+//
+// Code common to both existing-user and new-user logins
+//
+function become_logged_in($server_parms) {
+       $_SESSION["logged_in"] = 1;
+}
+
+
 
 //
 // Learn all sorts of interesting things about the Citadel server to
 // which we are connected.
 //
 function ctdl_get_serv_info() {
-       global $serv_humannode;
-       global $serv_software;
-
        serv_puts("INFO");
-       serv_gets($buf);
+       $buf = serv_gets();
        if (substr($buf, 0, 1) == "1") {
                $i = 0;
                do {
                        $buf = serv_gets();
-                       if ($i == 2) $serv_humannode = $buf;
-                       if ($i == 4) $serv_software = $buf;
+                       if ($i == 2) $_SESSION["serv_humannode"] = $buf;
+                       if ($i == 4) $_SESSION["serv_software"] = $buf;
                        $i = $i + 1;
-               } while ($buf != "000");
+               } while (strcasecmp($buf, "000"));
        }
 
 }
 
 
-
+//
+// Temporary function to verify communication with the Citadel server.
+//
 function test_for_echo() {
-
        global $clientsocket, $session;
 
        $command = "ECHO Video vertigo ... test for echo.";
-
        serv_puts($command);
        $response = serv_gets();
        echo $response, "<BR>";
        flush();
 }
 
+
+//
+// Display a system banner.
+// (This is probably temporary because it outputs more or less finalized
+// markup.  For now it's just usable.)
+//
 function ctdl_mesg($msgname) {
        global $clientsocket;
 
@@ -77,10 +147,10 @@ function ctdl_mesg($msgname) {
                echo "<DIV ALIGN=CENTER>\n";
                do {
                        $buf = serv_gets();
-                       if ($buf != "000") {
+                       if (strcasecmp($buf, "000")) {
                                echo "<TT>", $buf, "</TT><BR>\n" ;
                        }
-               } while ($buf != "000");
+               } while (strcasecmp($buf, "000"));
                echo "</DIV>\n";
        }
        else {
@@ -88,4 +158,5 @@ function ctdl_mesg($msgname) {
        }
 }
 
+
 ?>