掲示板
#! /usr/bin/python # coding: Shift_JIS import cgi import fcntl import time import re #================================================ 設定 FILE = './dat/bbs.dat' # 書き込み保存ファイル MAX = 100 # 保存件数 VIEW = 10 # 1ページの表示件数 TEXTMAX = 1000 # 本文文字数制限(全角) NAMEMAX = 10 # 名前文字数制限(全角) TITLEMAX = 20 # 題名文字数制限(全角) PASSWORD = '1234' # 削除用パスワード #================================================ 関数定義 def read_log(): log = [] f = open(FILE) fcntl.flock(f.fileno(), fcntl.LOCK_SH) for line in f: log.append(line.rstrip("\n")) fcntl.flock(f.fileno(), fcntl.LOCK_UN) f.close() return log def write_log(): tm = time.time() data = "\t".join([str(tm), FORM['name'], FORM['title'], FORM['text'], ""]) LOG.insert(0, data) while len(LOG) > MAX: LOG.pop() f = open(FILE, "w") fcntl.flock(f.fileno(), fcntl.LOCK_EX) f.write("\n".join(LOG)) fcntl.flock(f.fileno(), fcntl.LOCK_UN) f.close() def delete_log(): no = int(FORM['del']) del LOG[no] f = open(FILE, "w") fcntl.flock(f.fileno(), fcntl.LOCK_EX) f.write("\n".join(LOG)) fcntl.flock(f.fileno(), fcntl.LOCK_UN) f.close() def print_log(): page = int(FORM['page']) begin = page * VIEW end = begin + VIEW loglen = len(LOG) if end > loglen: end = loglen for i in range(begin, end): tm, name, title, text, hid = re.split("\t", LOG[i], 4) if name == "": name = "名無し" if title == "": title = "無題" print """ <input type="radio" name="del" value="%0d"> 【%s】%s<br> %s <div align=\"right\">%s</div> <hr> """ % (i, title, name, text, time.ctime(float(tm))) print "<center>" if begin > 0: print """<a href="?page=%d">≪前ページ</a>""" % (page - 1) if end < loglen: print """<a href="?page=%d">次ページ≫</a>""" % (page + 1) print "</center>" def get_form(): CGI = cgi.FieldStorage() form = {} for k in ['mode', 'pass', 'name', 'title', 'text']: form[k] = "" for k in ['page']: form[k] = "0" for k in CGI.keys(): form[k] = CGI.getvalue(k, "") form[k] = cgi.escape(form[k]) form[k] = re.sub("\x0D\x0A\s*", "<br>", form[k]) form[k] = re.sub("\s+", " ", form[k]) return form #================================================ メインプログラム FORM = get_form() LOG = read_log() #サンプルにつき書き込み停止 #if FORM['mode'] == "write"\ # and FORM['text'] != ""\ # and len(FORM['name']) <= NAMEMAX * 2\ # and len(FORM['title']) <= TITLEMAX * 2\ # and len(FORM['text']) <= TEXTMAX * 2: # write_log() #elif FORM['mode'] == "delete" and FORM['pass'] == PASSWORD: # delete_log() print "Content-type: text/html; charset=Shift_JIS" print """ <html> <head> <title>掲示板</title> </head> <body bgcolor="#ffccff"> <h1 style="margin:2px;font-size:15px;letter-spacing:50px">掲示板</h1> <hr> <form action="" method="POST"> 名前:<input type="text" name="name" size="50" maxlength="%d" value="%s"><br> <br> 題名:<input type="text" name="title" size="50" maxlength="%d"><br> <br> 本文: <small>※全角%d文字以内</small><br> <textarea name="text" rows="10" cols="80"></textarea><br> <br> <input type="hidden" name="mode" value="write"> <input type="submit" value=" 書き込み "><br> </form> <form action="" method="POST"> <hr> """ % (NAMEMAX, FORM['name'], TITLEMAX, TEXTMAX) print_log() print """ <div align="right"> <input type="password" name="pass" value="%s" size="4"> <input type="hidden" name="mode" value="delete"> <input type="hidden" name="page" value="%s"> <input type="submit" value="削除"> </div> </form> </body> </html> """ % (FORM['pass'], FORM['page'])
〔 実行する 〕