Wednesday, February 13, 2008

Add Cookie manipulation to WebSession Class

To add the getCookieVal function and the setCookie subroutine to Jake Howlett's WebSession class, add them and the escape and unescape functions at the end of the class.



Class WebSession
Public session As Notessession
Public database As NotesDatabase
...



Sub return(URL As String)
If Left(URL, 1)<>"/" Then
URL = "/"+URL
End If

Print "Location: " + URL
End Sub

' insert functions and subroutines here

' escape function is the same
Function escape(strIn As String) As String
Dim strAllowed As String
Dim i As Integer
Dim strChar As String
Dim strReturn As String
...
escape = strReturn
End Function


' unescape function is the same
Function unescape(strIn As String) As String
Dim i As Integer
Dim strChar As String
Dim strReturn As String
...
unescape = strReturn
End Function


' getCookieValfunction is the same
Function getCookieVal(thisname As String) As String
Dim cookie As String
Dim prefix As String
...
getCookieVal = unescape(Mid$(cookie, begin+Len(prefix), ending - begin -Len(prefix)+1))
End Function


' the setCookie sub needs a small adjustment where the database path (dbpath) is calculated
Sub setCookie(compName As String, compValue As String, adjYear As Integer, adjHour As Integer)
Dim thistime As NotesDateTime
Dim gmttime As NotesDateTime
Dim gmtvar As Variant
Dim cooktime As String
Set thistime = New NotesDateTime("")
Call thistime.SetNow
If adjHour <> 0 Then
Call thistime.AdjustHour(adjHour)
End If
If adjYear <> 0 Then
Call thistime.AdjustYear(adjYear)
End If
Set gmttime = New NotesDateTime(Left$(thistime.GMTTime, Len(thistime.GMTTime)-4))
gmtvar = gmttime.LSLocalTime
cooktime = Format(gmtvar, "dddd")+ ", " + Format(gmtvar, "dd")+ "-" + Format(gmtvar, "mmm") + "-" + Format(gmtvar, "yyyy") +_
" " + Format(gmtvar, "hh:mm:ss") + " GMT"
compName = escape(compName)
compValue = escape(compValue)
Dim dbpath As String
dbpath = Strleft(Lcase(Me.document.Path_Info_Decoded(0)), ".nsf") + ".nsf"+ "/"
Print "Set-Cookie: " + compName + "=" + compValue + "; expires=" + cooktime + "; path=" + dbpath + ";"
End Sub

End Class

Monday, February 4, 2008

setCookie and getCookie using LotusScript

These are two functions that can be used in LotusScript agents to set and get cookie values.

setCookie
The technique used to set a cookie value prints a header code. You need to have set the Content-Type with,

Print "Content-Type:text/html"

getCookieVal
The getCookieVal function uses the value obtained from doc.http_cookie(0) to obtain the value of the cookie name. For example, doc.http_cookie(0) may give you

test=thistest; user_name=Dietrich%20Willing

The getCookieVal function will give 'thistest' if the parameter you used was 'test'

So, here are the functions. I provide the escape and unescape functions, as they are used to ensure all characters can be stored and read.

Function getCookieVal( cookie As String, thisname As String) As String
Dim prefix As String
prefix = thisname + "="
Dim begin As Integer
Dim ending As Integer
begin = Instr(cookie, "; " + prefix)
If begin = 0 Then
begin = Instr(cookie, prefix)
If begin = 0 Then
getCookieVal = ""
Exit Function
End If
Else
begin = begin +2
End If
ending = Instr(begin, cookie, ";") - 1
If ending = -1 Then
ending = Len(cookie)
End If
getCookieVal = unescape(Mid$(cookie, begin+Len(prefix), ending - begin -Len(prefix)+1))
End Function

Sub setCookie(doc As NotesDocument, compName As String, compValue As String, adjYear As Integer, adjHour As Integer)
' uses a print statement to set the cookie
' uses adjYear and adjHour to set the expiry time of the cookie.
' If the cookie is needed for the one particular session set adjHour to 1, then the cookie is active for an hour
' If you want to expire a cookie, use a negative number for either adjYear or adjHour.
Dim thistime As NotesDateTime
Dim gmttime As NotesDateTime
Dim gmtvar As Variant
Dim cooktime As String
Set thistime = New NotesDateTime("")
Call thistime.SetNow
If adjHour <> 0 Then
Call thistime.AdjustHour(adjHour)
End If
If adjYear <> 0 Then
Call thistime.AdjustYear(adjYear)
End If
Set gmttime = New NotesDateTime(Left$(thistime.GMTTime, Len(thistime.GMTTime)-4))
gmtvar = gmttime.LSLocalTime
cooktime = Format(gmtvar, "dddd")+ ", " + Format(gmtvar, "dd")+ "-" + Format(gmtvar, "mmm") + "-" + Format(gmtvar, "yyyy") +_
" " + Format(gmtvar, "hh:mm:ss") + " GMT"
compName = escape(compName)
compValue = escape(compValue)
Dim dbpath As String
dbpath = Strleft(Lcase(doc.Path_Info_Decoded(0)), ".nsf") + ".nsf"+ "/"
Print "Set-Cookie: " + compName + "=" + compValue + "; expires=" + cooktime + "; path=" + dbpath + ";"
End Sub

Function escape(strIn As String) As String
Dim strAllowed As String
Dim i As Integer
Dim strChar As String
Dim strReturn As String

strAllowed = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" & "@/.*-_"
i = 1
strReturn = ""
While Not (i > Len(strIn))
strChar = Mid$(strIn, i, 1)
If Instr(1, strAllowed, strChar) > 0 Then
strReturn = strReturn & strChar
Else
strReturn = strReturn & "%" & Hex$(Asc(strChar))
End If
i = i + 1
Wend
escape = strReturn
End Function

Function unescape(strIn As String) As String
Dim i As Integer
Dim strChar As String
Dim strReturn As String

i = 1
strReturn = ""
While Not (i > Len(strIn))
strChar = Mid$(strIn, i, 1)
If Not strChar = "%" Then
strReturn = strReturn & strChar
Else
i = i + 1
strChar = "&H" & Mid$(strIn, i, 2)
strReturn = strReturn & Chr$(Val(strChar))
i = i + 1
End If
i = i + 1
Wend
unescape = strReturn
End Function


These functions and subs can be incorporated into Codestore's WebSession class. I'll show that tomorrow.



This LotusScript was converted to HTML using the ls2html routine,
provided by Julian Robichaux at nsftools.com.