How to include a TCL script in a PHP page ?
Short answer : echo tclsh script.tcl
Here’s my Pluton code.
-
//Les scripts TCL s’exécutent dans _includesTCL et se voient passés en argument l’user_id et la session_id
-
echo
tclsh ../../$body $Utilisateur['user_id'] $_SESSION['ID']
; -
//Revenons maintenant à notre dossier / pour ajouter correctement notre footer
Pluton is the PHP engine developed on Espace Win and also used on Double V Network website. I’ve learned PHP coding it in 2001.
You’ll certainly need some libraries ; I’ve a TCL script initializing MySQL connection and another with user profiles functions. Let’s put all in a directory, chdir to it, and no need to worry about paths.
Of couse, after the script, we come back to our initial directory to avoid broke the end of our php app.
tclsh is a shell-like TCL interpreter, and is the backtick operator, executing the command inside and returning result. Of couse, tclsh must be in tour path. And you should consider use tclsh84 or tclsh85 instead. If I’ve written tclsh to get TCL 8.4 on Guenwhyvar, the FreeBSD Espace Win prod server and TCL 8.5 on Leviathan, my laptop.
We pass two arguments at our TCL script, the user id and session id of the logged user. I presume user_id is satisfactory but we’ll see in the future.
Oh, btw, our user_id is [lindex $argv 0] and session id [lindex $argv 1], tclsh putting our args in argv variable, lindex $list $n returning element $n of our list $list.
Inside TCL
Now, let’s see what I’ve coded in TCL :
[ www.espace-win.org/IRC/Projets | source code ]
This is a webpage viewing #Win projects. I had already the TCL output in SurfBoard so why recode it again in PHP ?
In core.tcl we’ve Pluton user profile functions and two interesting :
-
proc runningOnWindows {} {
-
regexp “.exe$” [info nameofexecutable]
-
}
-
proc isnumber {string} {
-
if {([string compare $string “”]) && (![regexp [^0-9] $string])} {return 1} {return 0}
-
}
MySQL.tcl opens a MySQL connection :
-
if [runningOnWindows] {
-
load fbsql.dll
-
} else {
-
load fbsql.so
-
}
-
sql connect localhost login pass
-
sql selectdb EspaceWin
Then Projets.tcl contains functions I’ve copied from my previous Su
-
proc projet.who {id {niveau “-1”}} {
-
#Liste les membres d’un projet ayant le niveau donné
-
#Si pas de niveau spécifié, liste 2 et 9 (manager et participants projets)
-
if {$niveau == -1} {
-
return [sql “SELECT u.username FROM Utilisateurs u, Projets_membres pm WHERE pm.project_id LIKE ‘$id’ AND (pm.niveau = ‘2’ OR pm.niveau = ‘9’) AND u.user_id = pm.user_id ORDER BY u.username ASC”]
-
} else {
-
return [sql “SELECT u.username FROM Utilisateurs u, Projets_membres pm WHERE pm.project_id LIKE ‘$id’ AND pm.niveau = ‘$niveau’ AND u.user_id = pm.user_id ORDER BY u.username ASC”]
-
}
-
}
Finally, here the extra functions in core.tcl to get a fully functionnal code 😉
-
################################################################################
-
# Fonctions utilisateurs, et de gestion du profil #
-
################################################################################
-
#Récupère l’username de $who
-
#$who : user_id ou username
-
proc getnom {who} {
-
getuserinfo [lectureID $who] username
-
}
-
#Récupère le champs $data du profil de $who
-
#$who : user_id ou username
-
#$data : nom du champs de la table Utilisateurs à récupérer
-
proc getuserinfo {who data} {
-
sql “SELECT $data FROM Utilisateurs WHERE user_id = ‘[lectureID $who]'”
-
}
-
#Met à jour le champs $data du profil de $who sur $valeur
-
#$who : user_id ou username
-
#$data : nom du champs de la table Utilisateurs à mettre à jour
-
#$valeur : nouvelle valeur de ce champs
-
proc setuserinfo {who data valeur} {
-
sql “UPDATE Utilisateurs SET $data = ‘[sqlfilt $valeur]’ WHERE user_id = [lectureID $who]”
-
}
-
#Récupère l’user_id de $who
-
#$who : user_id ou username
-
proc lectureID {who} {
-
if ![isnumber $who] {
-
sql “SELECT user_id FROM Utilisateurs WHERE username LIKE ‘$who'”
-
} else {
-
return $who
-
}
-
}
I hope you’ve catched return is not necessary in TCL, by default a function return the result returned by it last instruction (so getnom proc returns getuserinfo result). Eminently readable source code 🙂
Code highlighting is provided by GeSHi, very nice no ?
This is fully integrated in Pastebin (and our pastebin has an up to date GeSHi version with more languages than the original).