Transcript(Runtime Revolution) の日本語で使える、基本的なCGI スクリプトです。サンプルはこちら

このスクリプトはRunRevファイルのメインスタックのスクリプトエディターに、ライブラリーとして収納して
HTML のフォームから CGI テキストファイル(.cgi)
#! revolution
on startup
 library "eAddressSearchLib.rev"
end startup
で引き出しています。

日本語は文字化け防止のため、SJIS をURL エンコードで使用しています。
(場合によってはユニコード使用も可能ですが、行程がSJIS よりも増えます)
URL エンコードを、デスクトップでデコードして普通に読める状態にするのは、
Runtime Revolution のフィールドに文字列を入れて、以下のスクリプトを使います。
set the unicodeText of fld 2 to uniEncode(urlDecode(fld 1), japanese)
上はフィールド1にエンコード文字列を入れて、フィールド2にデコードをセット
反対にRunRev のフィールドにある日本語をSJIS のURL エンコードにするのは
put urlEncode(uniDecode(the unicodeText of fld 1, "Japanese"))

大事なデータは、万一サーバーが侵入されてもスクリプトやデータが変えられないように
set the password of this stack to "myLock"
で、ライブラリーファイルをロックして、プロテクトしておくことができます。


ファイル「eAddressSearchLib.rev」に、ライブラリーとして収納したスクリプトは以下です。


-- コンスタントにしたパスは、それぞれサーバーの環境で変わります。
constant kStack = "/Library/WebServer/tData/eAddressSearchLib.rev" -- MacOSX での例
constant kActionAdress = "http://mydomain.com/cgi-bin/rev/eAddressSearchLib.cgi" -- CGIファイル
constant kReturnAddress = "http://mydomain.com/eAddressSearchLib.html" -- フォームがあるHTML ファイル
constant kPassword = "my-pass"

-- バックグラウンド、テキストなどのカラー設定
constant kBgColor="white", kText="#333333", kAlink="#aaaaaa", kVlink="#333333", kLink="#333333"
constant kTextAlignC="<center>", kTextAlignEndC="</center>"
constant kTextAlignL="<left>", kTextAlignEndL ="</left>"

-- 日本語は文字化け防止、検索のため、SJIS をURL エンコードで使用。便宜上頭に kl と付けています。
constant klJpTitle="E%83%81%81%5B%83%8B%83A%83h%83%8C%83X%93o%98%5E%92%A0"
constant klWrongPassword = "%82%F1%81%5B%82%F1%81A%83p%83X%83%8F%81%5B%83h%82%AA%88%E1%82%C1%82%C4%82%A2%82%DC%82%B7%82%CB%81B"
constant klClickNreturn = "%83u%83%89%83E%83U%81%5B%82%CC%96%EE%88%F3%82%F0%83N%83%8A%83b%83N%82%B5%82%C4%81A%91O%82%CC%83y%81%5B%83W%82%C9%96%DF%82%C1%82%C4%82%AD%82%BE%82%B3%82%A2%81B"
constant klPutTheName = "%96%BC%91O%82%F0%8BL%93%FC%82%B5%82%C4%82%AD%82%BE%82%B3%82%A2%81B"
constant klNoMarks = "%8D%ED%8F%9C%82%B7%82%E9%96%BC%91O%82%C9%83%60%83F%83b%83N%83%7D%81%5B%83N%82%F0%95t%82%AF%82%C4%82%AD%82%BE%82%B3%82%A2%81B"
constant klNameIsAlready = "%82%B7%82%C5%82%C9%93o%98%5E%8D%CF%82%DD%82%C5%82%B7%81B"
constant klEmail = "E%83%81%81%5B%83%8B%81F"
constant klName = "%96%BC%91O%81F"
constant klSpace = "%81%40%81%40"
constant klRemoveFromList ="%96%BC%95%EB%82%A9%82%E7%8D%ED%8F%9C%82%B7%82%E9"
constant klPasuwado= "%83p%83X%83%8F%81%5B%83h%81F"
constant klSendDataBtn = "%91%97%82%E9", klShowList="%83%8A%83X%83g%82%F0%8C%A9%82%E9", klSearchList ="%8C%9F%8D%F5"
constant klRetrunToHtmlBtn = "%96%BC%95%EB%83f%81%5B%83%5E%8F%91%82%AB%8D%9E%82%DD%82%C9%96%DF%82%E9"
constant klTsearchResult = "%8C%9F%8D%F5%82%CC%8C%8B%89%CA%82%CD%81F"

-- ライブラリースタックはここから
on librarystack
-- HTML ファイルのフォームから送られてきた文字列「$QUERY_STRING」をアレイに
put $QUERY_STRING into theTerms
put theTerms into theTermsArray
split theTermsArray by "&" and "="
put "noMarks" into tCheckBox
put "" into buffer

-- パスワードのチェック
put urlDecode(theTermsArray["password"]) into tPass
if tPass is not kPassword then
put sjishtml(kTextAlignC, kTextAlignEndC) after buffer -- center
put "<br><br><p class=head>" &urlDecode(klWrongPassword)&"</p>" &\
"<p class=body>" &urlDecode(klClickNreturn) &"</p>" after buffer
put "</body> </center></html>" after buffer
put "Content-Type: text/html" & cr & cr
put buffer
exit librarystack  -- パスワードが違っていたらライブラリースタックから抜ける
end if

--どのサブミットボタンがクリックされたか?
put theTermsArray["dataNameSend"] after tBtnName
put theTermsArray["showList"] after tBtnName
put theTermsArray["sendDelete"] after tBtnName
put theTermsArray["searchList"] after tBtnName

-- サブミットボタンの振り分け
switch
case (tBtnName=klSendDataBtn) or (tBtnName=klShowList)
if theTermsArray["showList"] <> klShowList then

-- 名簿から削除するマークが付いていたら
if theTerms contains "checkboxName" then
put theTerms into tMarked
split tMarked by "&"
repeat for each element tMarkedCard in tMarked
put urlDecode(tMarkedCard) & return after tCardNames
end repeat
open stack kStack
put tCardNames into tLines
repeat for each line tLine in tCardNames
if tLine contains "checkboxName=" then
replace "checkboxName=" with "" in tLine
delete cd tLine of this stack
end if
end repeat
-- 削除した後ファイルをSave
save stack kStack
close stack kStack
put "deleted" into tCheckBox -- 削除したら"noMarks" から"deleted" に変える
end if

-- フォームから名簿に書き込みが送られた
if tCheckBox = "NoMarks" then
put theTermsArray["name"] into tName
put theTermsArray["tel"] into tTel -- 注:Eメールは telとなっていて未修整

-- フォームから送られた名前がカラの場合
if tName is "" then
put sjishtml(kTextAlignC, kTextAlignEndC) after buffer -- center
if theTermsArray["sendDelete"] is empty then
put "<br><br><p class=head>"& urlDecode(klPutTheName) &"</p>" after buffer
else
put "<br><br><p class=head>"& urlDecode(klNoMarks) &"</p>" after buffer
end if
put "<p class=body>" & urlDecode(klClickNreturn)& "</p>" after buffer
put "</body> </center></html>" after buffer
put "Content-Type: text/html" & cr & cr
put buffer
exit librarystack -- ライブラリースタックから抜ける
end if

-- スタックを開いて名前がすでに登録済みかチェック
open stack kStack
-- Is there the name in the list?
put the cardnames of this stack into tCdNames
if lineOffset (tName&tTel, tCdNames) >0 then -- 登録済みの場合
put sjishtml(kTextAlignC, kTextAlignEndC) after buffer -- center
put "<br><br><p class=head> " & urlDecode(klNameIsAlready) & "</p>" after buffer
put "<p class=body>" & urlDecode(klClickNreturn)& "</p>" after buffer
put "</body> </center></html>" after buffer
put "Content-Type: text/html" & cr & cr
put buffer
close stack kStack
exit librarystack -- ライブラリースタックから抜ける
end if

-- 条件をクリアしたら新しいカードを作成
open last cd
create cd
set the name of this cd to tName&tTel -- 新しいカードに「名前+Eメール」の名前を付ける

-- 新しいカードにデータを書き込み。
-- データはスタックに収納するこの時点ではURLデコードしない。
set the cName of this cd to tName
set the cTel of this cd to tTel
-- 必ず保存して、スタックをクローズ
save stack kStack
close stack kStack
end if
end if

-- 書き出しのHTML ファイルの作成
put sjishtml(kTextAlignL, kTextAlignEndL) & returnAddress() after buffer
put "<form action="& quote & kActionAdress &quote& " method=get >" after buffer

-- スタックを開いて、カード2からデータを読み出し
open stack kStack
put the num of cds of this stack into tNumOfCds
repeat with i=2 to tNumOfCds
open cd i
put the cName of this cd into sjisName
put the cTel of this cd into sjisTel

-- HTML に書き出す日本語は URL デコードする
put "<p class=body>"&cr& urlDecode(klName) & urlDecode(sjisName) &\
urlDecode(klSpace) & urlDecode(klEmail) & urlDecode(sjisTel) & cr& \
"<br><input type=checkbox name=checkboxName value=" &\  -- 名簿から削除するチェックボックスを付ける
quote& sjisName&sjisTel &quote& "> " & urlDecode(klRemoveFromList) &"<br><br></p>" after buffer
end repeat

close stack kStack -- 必ずクローズする
put setPasswordForm() after buffer
put "</p></body> </left></html>" after buffer
put "Content-Type: text/html" & cr & cr
put buffer
break

-- サブミットが検索の場合はここから
case (tBtnName=klSearchList)
-- 検索する語は何か?
put theTermsArray["searchName"] into tSearchName
-- HTML を書き出し
put sjishtml(kTextAlignL, kTextAlignEndL) & returnAddress() after buffer
-- スタックを開いて
open stack kStack
-- スタックのすべてのカード名「名前+Eメール」を取得
put the cardnames of this stack into tCdNames
-- 検索する語が含まれていたら、データをURL デコードで書き出し。
repeat for each line tLine in tCdNames
if tLine contains tSearchName then
put urlDecode(the cName of cd tLine) & urlDecode(klSlash) after tSearch
put urlDecode(the cTel of cd tLine) &"<br><br>" after tSearch
end if
end repeat
-- 必ず保存して、スタックをクローズ
save stack kStack
close stack kStack

-- HTML ファイルを閉じる
put "<p class=body>"&urlDecode(klTsearchResult) &"<br><br>"&cr& \
tSearch &"</p></body> </left></html>" after buffer
put "Content-Type: text/html" & cr & cr
put buffer
break
end switch
end librarystack -- ライブラリー終わり


-- HTML ファイルを書き出すためのファンクション
function sjishtml kTextAlign, kTextAlignEnd
put "<html>"& cr&"<meta http-equiv=" &quote& "content-type" &quote& \
"content=" &quote & "text/html; charset=shift_jis" &quote & \
"><head><title>" &urlDecode(klJpTitle) &"</title>" &cr& cssTx() & cr& \
"</head>"& cr&"<body bgcolor="&quote & kBgColor &quote &" text="&quote &kText&quote & \
" alink="&quote & kAlink &quote &" vlink="&quote & kVlink &quote &" link="&quote & kLink &quote &">"& cr& \
kTextAlign & cr into tJpn
return tJpn
end sjishtml

-- スタイルシートのためのファンクション
function cssTx
return "<style type=" &quote & "text/css" &quote & "><!--" &cr& \
".head { color: #333; font-size: 24px; line-height: 32px; font-family: " &quote& "MS UI Gothic" &quote& " }" &cr& \
".body { color: #333; font-size: 12px; line-height: 14px; font-family: " &quote& "MS UI Gothic" &quote& "} " &cr& \
"--> </style>"
end cssTx

-- パスワードのフォーム
function setPasswordForm
return "<hr align=left width=300> "&cr&\
"<p class=body>"&cr& \
urlDecode(klPasuwado) & "<input type=password name=password size=29> <br>" & \
urlDecode(klRemoveFromList) & "<input type=submit name=sendDelete value=" &urlDecode(klSendDataBtn) & "></font></p><br></form>"
end setPasswordForm

-- 始めのフォームに戻るためのリンク
function returnAddress
return "<p class=body><a href=" & kReturnAddress &">" & urlDecode(klRetrunToHtmlBtn ) & "</a></p>"
end returnAddress