|
// Session class.
/**
* Manipulations with session, stored in xml.
* @param xmlNode XML node with single session information.
*/
function Session(xmlNode, markAccesssing) {
this.xmlNode = xmlNode;
this.markAccesssing = markAccesssing;
this.userExpirationTime = getSessionExpirationTime(); // seconds
this.getSessionID = Session_getSessionID;
this.getUserNick = Session_getUserNick;
this.setUserNick = Session_setUserNick;
this.getClientIP = Session_getClientIP;
this.setClientIP = Session_setClientIP;
this.doesExpired = Session_doesExpired;
this.markLastAccessTime = Session_markLastAccessTime;
this.setAttribte = Session_setAttribte;
this.getAttribte = Session_getAttribte;
}
/**
* Extracting session id of owned session.
* @return The session id.
*/
function Session_getSessionID() {
var id = this.getAttribte("ID");
this.markLastAccessTime();
return id;
}
/**
* Extracting user nick.
*/
function Session_getUserNick() {
var userNick = this.getAttribte("NAME");
this.markLastAccessTime();
return userNick;
}
/**
* Assigning new user nick.
*/
function Session_setUserNick(userNick) {
this.setAttribte("NAME", userNick);
this.markLastAccessTime();
}
/**
* Extracting client of this session IP address.
*/
function Session_getClientIP() {
var clientIP = this.getAttribte("CLIENTIP");
this.markLastAccessTime();
return clientIP;
}
/**
* Assigning client IP address.
*/
function Session_setClientIP(clientIP) {
this.setAttribte("CLIENTIP", clientIP);
this.markLastAccessTime();
}
/**
* Check record for not accessed time-out.
*/
function Session_doesExpired() {
var accessedValue = this.getAttribte("ACCESSED");
if(accessedValue == null) {
// Maybe the first time accessing.
return false;
}
var current = new Date();
var p = Number(accessedValue);
var expirationDiff = p + this.userExpirationTime*1000;
return (expirationDiff < current.valueOf());
}
/**
* Continue accessing this session.
*/
function Session_markLastAccessTime() {
if(this.markAccesssing) {
var currentTime = new Date();
var lastAccessTime = currentTime.valueOf();
this.setAttribte("ACCESSED", lastAccessTime);
}
}
/**
* Create or rename name-value pair of attribute.
* @param name the attribute name.
* @param value attribute value.
*/
function Session_setAttribte(name, value) {
var attributeNode = this.xmlNode.attributes.getNamedItem(name);
if(attributeNode != null) {
attributeNode.nodeValue = value;
}
else {
var xmlDoc = this.xmlNode.ownerDocument;
attributeNode = xmlDoc.createAttribute(name);
attributeNode.nodeValue = value;
this.xmlNode.attributes.setNamedItem(attributeNode);
}
}
/**
* Retraiving specified attribute value.
* @param name the attribute name.
* @return Attribute value.
*/
function Session_getAttribte(name) {
var xmlNode = this.xmlNode.attributes.getNamedItem(name);
if(xmlNode != null) {
return xmlNode.nodeValue;
}
else {
return null;
}
}
// Session class.
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here