//簡単投票機
//author:Hiroto Hariu
//since:2005/02/23
define("FILEPATH","./votedat/");
$columns = array(
"面白い","馬鹿","つまらない","興味深い","その通り","無理がある","ちょっと変");
$page = "";
if(isset($_GET['page'])){
$page = $_GET['page'];
}
if(isset($_POST['page'])){
$page = $_POST['page'];
}
if($page == ""){
$page = "default";
}
$noForm = 0;
$msg = "";
$data = text_read(FILEPATH.urlencode($page));
if(isset($_POST['vote'])){
$index = (int)$_POST['vote'];
if($data[$index]['ip']==$_SERVER['REMOTE_ADDR']){
$msg = "連続投稿っぽいですよ
";
$noForm = 1;
}else{
$data = text_read(FILEPATH.urlencode($page));
if($index>=0 && $index < count($columns)){
$data[$index]['vote']++;
$data[$index]['ip']=$_SERVER['REMOTE_ADDR'];
}
text_write(FILEPATH.urlencode($page),$data);
$msg = "投票完了しました
ありがとうございます。
";
$noForm = 1;
}
}
//データからリスト作成
if(!isset($data)){
$data = text_read(FILEPATH.urlencode($page));
}
require("template.php");
//以下サブルーチン
//テキストファイルをパースして、配列にして返す。
function text_read($filename){
if(file_exists($filename)){
return str2array(join("",file($filename)));
}else{
return array();
}
}
//配列をテキストファイルにして書き込み。
function text_write($filename,$array){
$fh = fopen($filename, 'w');
flock($fh,LOCK_EX);
fwrite($fh, array2str($array));
flock($fh,LOCK_UN);
fclose($fh);
//chmod($filename,0666);
}
function array2str($array,$nestedlevel=0){
$result = "";
if(is_array($array)){
foreach($array as $k => $v){
$result.="<".urlencode($k).">".array2str($v,$nestedlevel+1)."".urlencode($k).">";
}
}else{
$result = urlencode($array);
}
return $result;
}
function str2array($str){
$result = array();
$source = ereg_replace("\r|\n|\ ","",$str);
//$current = 0;
$key = "";
$current = 0;
$max = strlen($source);
$finished = false;
$loop = 0;
$loopmax = 32000;
while(!$finished){
$tag = ereg_replace("^<([^>]*)>.*","\\1",$source);
//print htmlspecialchars($tag)."--\n";
if($tag!=$source){
//終了タグ取得
//$source = ereg_replace("<$tag>","",$source);
$source = substr($source,strlen("<$tag>"));
$endtagpos = strpos($source,"$tag>");
if($endtagpos!=-1){
$value = substr($source,0,$endtagpos);
//print "->".htmlspecialchars($value)."--\n";
if(ereg("^<",$value)){
$result[urldecode($tag)]=str2array($value);
}else{
$result[urldecode($tag)]=urldecode($value);
}
$source = substr($source,$endtagpos+strlen("$tag>"));
$loop++;
if($loop > $loopmax){ $finished = true;}
}else{
print "PARSE ERROR!!";
exit();
}
}else{
$finished = true;
}
}
return $result;
}
?>