Exemple #1
0
            echo "<font color='#FF0000'>error</font>";
            exit;
        } else {
        }
    }
}
if (empty($picName)) {
    $imgpath = "./image/auto.png";
} else {
    $imgpath = $picName;
}
require_once 'xmlHandler.php';
// create the chatroom xml file handler
$xmlh = new xmlHandler("chatroom.xml");
if (!$xmlh->fileExist()) {
    header("Location: error.html");
    exit;
}
$xmlh->openFile();
// get the 'users' element as the current element
$users_element = $xmlh->getElement("users");
// create a 'user' element for each user
$user_element = $xmlh->addElement($users_element, "user");
// add the name
$xmlh->setAttribute($user_element, "name", $_POST["name"]);
$xmlh->setAttribute($user_element, "pic", $imgpath);
$xmlh->saveFile();
// set the name to the cookie
setcookie("name", $_POST["name"]);
// Cookie done, redirect to client.php (to avoid reloading of page from the client)
header("Location: client.php");
require_once 'xmlHandler.php';
if (!isset($_COOKIE["name"])) {
    header("Location: error.html");
    exit;
}
// create the chatroom xml file handler
$xmlh = new xmlHandler("chatroom.xml");
if (!$xmlh->fileExist()) {
    header("Location: error.html");
    exit;
}
// get the name from the cookie
$name = $_COOKIE["name"];
$xmlh->openFile();
// get the users element
$users_node = $xmlh->getElement("users");
// get all user nodes
$users_array = $xmlh->getChildNodes("user");
if ($users_array != null) {
    // delete the current user from the users element
    foreach ($users_array as $user) {
        $username = $xmlh->getAttribute($user, "name");
        if ($username == $name) {
            $xmlh->removeElement($users_node, $user);
        }
    }
}
$xmlh->saveFile();
// clear the cookie
setcookie("name", "");
header("Location: login.html");
    header("Location: error.html");
    return;
}
// get the name from cookie
$name = $_COOKIE["name"];
$color = $_POST["color"];
// get the message content
$message = $_POST["message"];
if (trim($message) == "") {
    $message = "__EMPTY__";
}
require_once 'xmlHandler.php';
// create the chatroom xml file handler
$xmlh = new xmlHandler("chatroom.xml");
if (!$xmlh->fileExist()) {
    header("Location: error.html");
    exit;
}
// create the following DOM tree structure for a message and add it to the chatroom XML file
$xmlh->openFile();
// get the 'messages' element as the current element
$messages_element = $xmlh->getElement("messages");
// create a 'message' element for each message
$message_element = $xmlh->addElement($messages_element, "message");
// add the name
$xmlh->setAttribute($message_element, "name", $name);
$xmlh->setAttribute($message_element, "color", $color);
// add the content of the message
$xmlh->addText($message_element, $message);
$xmlh->saveFile();
header("Location: client.php");