<?php i18n_http_output("SJIS"); ?>
<?php 
    
/*    PHP Test#06 : Tree BBS
        Programmed by FUKAYA Takashi 2001.01.13-



        Params
            Submit        コマンド

            txtName        入力内容
            txtMail
            txtMsg
            txtUrl
            txtPass

            prmNodeId    入力内容を接続するノード名

            prmResid    返信対象レコードID(これから投稿)

            prmViewid    表示対象レコードID
            prmTreeid    表示対象ツリーID


        Record(key name)
            #            記事#
            last#        ツリー階層ごとの記事数
            key#        #番目の投稿のkey名
            lastkey        key数
            

        Record field format    (tab区切り)
            no
            exist
            date
            ipadr
            name
            mail
            title
            msg
            url
            pass

        インデント量についてメモ

            KEY(データ本体)  B インデント  説明
            ---------------- - ----------- ------- -----
            1                2 +
            1_1              2  .+           一つ前のレコードの B値-1 だけ字下げ(この場合+1)
            1_1_1            1   .+        一つ前のレコードの B値-1 だけ字下げ(この場合+1)
            1_1_1_1          0    +        一つ前のレコードの B値-1 だけ字下げ(この場合+-0)
            1_1_2            1   +
            1_1_2_1          1   +
            1_1_2_1_1        0   +
            1_2              1  +          一つ前のレコードの B値-1 だけ字下げ(この場合+1)
            1_2_1            1  +
            1_2_1_1          1  +
            1_2_1_1_1        0  +

            2                1 +
            2_1              0  +

            3                1 +
            3_1              3  +
            3_1_1            0  ..+        一つ前のレコードの B値-1 だけ字下げ(この場合+2)
            3_1_2            1   +
            3_1_2_1          0   +
            3_1_3            0  +

            (Bは各階層の最大値=枝の数,末端の枝数は0になる)
    */

    
include "ftsstp.php3";
    include 
"treedbmclass.php3";
    
    
// ==================== PHP DEFINES ====================
    
    
$scriptname $PHP_SELF;    // "treebbs.php3";
    
$dbname "testtree.dbm";
    
$jsname "testtree.js";

    
// ==================== PHP FUNCTIONS ====================

    // 変数一覧を表示(デバッグ用)
    
function prtvars() {
        echo 
"<HR>\n";
        echo 
"<B>変数一覧 (" count($GLOBALS) . ")</B><BR>\n";
        
reset$GLOBALS );
        while( list( 
$key$value ) = each$GLOBALS ) ) {
               echo 
"$key = $value<br>\n";
        }
         echo 
"<HR>\n";
    } 

    
// デバッグメール送信
    
function debugmail($msg) {
        
mail"fukaya@sunquest.co.jp" "[PHPDEBUG]TREEBBS (" getenv(REMOTE_ADDR) . ")" $msg "\n" $ret );
    }


    
// 投稿記事表示
    
function prtarticle($key,$data,$level,$mode='') {
        global 
$scriptname;

        
$sp = ($level) * 30 20;        // ツリーの字下げ量
        
if ( $level==$head '●'; else $head '○';

        
$no $data['no'];
        
$exist $data['exist'];
        
$date $data['date'];
        
$ipadr $data['ipadr'];
        
$name $data['name'];        if ( $name=="" $name $mail;
                                    if ( 
$name=="" $name "(誰か)";
        
$mail $data['mail'];
        if ( 
$mail == "" ) {
            
$nametag $name;
        }else{
            
$nametag "<a href='mailto:$mail'>$name</a>";
        }

        
$title $data['title'];    if ( $title=="" $title "(何か)";
        
$msg $data['msg'];
        
$url $data['url'];
        
$pass $data['pass'];

        echo 
"<a name='$no'></a>";
        echo 
"<table width='100%' cellspacing='0' cellpadding='2'>\n";
        echo 
"  <tr> \n";
        echo 
"    <td align='left' width='$sp'><div align='right'>$head</div></td>\n";
        echo 
"    <td bordercolor='#0000FF'><font color='#33FF33'>[$no] <b>$title <font color='#CCCCCC'>- $nametag </font></b> - $date</font></td>\n";
        echo 
"  </tr>\n";
        echo 
"  <tr>\n";
        echo 
"    <td width='$sp'> </td>\n";
        echo 
"    <td bgcolor='#333333'><font size='-1'>";
        
$msgs explode"\n"$msg ) ;
        for ( 
$i=0$i<count($msgs); $i++ ) echo str_replace(" ","&nbsp;",htmlspecialchars($msgs[$i])) . "<br>";
        echo 
"    </font></td>\n";
        echo 
"  </tr>\n";
        if ( 
$url ) {
            echo 
"  <tr> \n";
            echo 
"    <td width='$sp' valign='top'><div align='right'><font size='-1' color='#CCCCFF'></font></div></td>\n";
            echo 
"    <td bgcolor='#000000'><font size='-1' color='#CCCCFF'>URL: <a href='$url'>$url</a></font></td>\n";
            echo 
"  </tr>\n";
        }
        if ( 
$mode != "res" ) {
            echo 
"  <tr> \n";
            echo 
"    <td width='$sp'> <div align='right'></div></td>\n";
            echo 
"    <td> \n";
            echo 
"      <div align='right'><font size='-1' color='#FF0000'><b><a href='$scriptname?prmResid=$key'><font color='#ff0000'>返事を書く</font></a><!--  削除 --></b></font></div>\n";
            echo 
"    </td>\n";
            echo 
"  </tr>\n";
        }
        echo 
"</table>\n";
    }

    
// 新規記事表示 JavaScript作成
    
function makenewjs($maxno=8) {
        global 
$dbm,$jsname,$scriptname;
        
$expsrc="";
        
$jsb "document.write ('";
        
$jse "');\n";

        
$expsrc .= $jsb '<font size="-2" color="#CCCCCC">■ 最近の投稿</font><br>' $jse;

        
$lastkey=$dbm->fetch('lastkey' );
        
$listno=0;
        while ( 
$lastkey>&& $listno<$maxno ) {
            
$key $dbm->fetch('key' $lastkey);
            
$dbm->fetchR($key,$data);
            if (
$data['exist']>0) {

                
$expsrc .= $jsb '<a href="';
                
$expsrc .= $scriptname '#' $data['no'];
                
$expsrc .= '"><font color="#00cc00"><font size="-2">▼ [' $data['no'] . '] ';
                
$expsrc .= $data['title'] . '</a> <font color="#808080">- ' $data['name'] . '</font> - ';
                
$expsrc .= $data['date'] . '</font></font><BR>' $jse;
                
$listno++;
            }            
            
$lastkey--;
        }
        
$fp fopen $jsname  "wb");
        
fputs$fp i18n_convert($expsrc,"SJIS") );
        
fclose$fp );
    }




    
// ==================== PHP STARTUP CODE ====================

    
$dbm = new TreeDBMClass($dbname );
    if ( 
$dbm->colnum == ) {
        
$dbm->defrecord"no,exist,date,ipadr,name,mail,title,msg,url,pass" );
    }

    if ( 
$Submit == "投稿" ) {
        
// 投稿
        
$data = array();
        
$data['no']=$dbm->fetch("lastkey")+1;
        
$data['exist']=1;
        
$data['date']=date("Y/m/d H:i:s",time());
        
$data['ipadr']=getenv(REMOTE_ADDR);
        
$data['name']=$txtName;
        
$data['mail']=$txtMail;
        
$data['title']=$txtTitle;
        
$data['msg']=$txtMsg;
        if ( 
$txtUrl == 'http://' $txtUrl='';
        
$data['url']=$txtUrl;
        
$data['pass']=$txtPass;

        if ( 
$txtMsg != "" ) {

            if ( 
$dbm->fetch('lastmsg') != $txtMsg ) {
                
// 投稿登録
                
$dbm->insertLeafR$prmNodeId $data );
                
$dbm->replace'lastmsg' $txtMsg );

                
$msg "投稿を受け付けました(No." $data['no'] . ")";
            } else {
                
$msg "同一内容のため登録されませんでした";
            }
        }else{
                
$msg "本文が空白のため登録されませんでした";
        }

        
makenewjs(4);

        
// Cookieを設定
        
setcookie"cookie_treebbs_name",$txtName time()+86400*);
        
setcookie"cookie_treebbs_mail",$txtMail time()+86400*);
        
$cookie_treebbs_name $txtName;
        
$cookie_treebbs_mail $txtMail;

        
$debugmsg  "msg:" $dbm->margeData($data) . "\n";
        
$debugmsg .= "result:" $msg "\n";
        
debugmail$debugmsg );
    }



?>







<html lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
<META content=F/T,深谷崇,PHP,PHP3,ツリー掲示板,SSTP name=keywords>
<META content=PHP+DBMを使用したツリー形式掲示板。 name=description>
<link rev="made" href="mailto:fukaya@dokokano.com">
<title>PHP TEST#06 : Tree BBS</title>
</head>

<body bgcolor="#000000" text="#ffffff" topmargin="0" link="#9999FF" vlink="#FF66FF" alink="#CCCCFF">

<!-- 本文 : タイトル部分 --> 

<div align="center"> 

    
  <table align="center" background="bgCir01.gif" width="100%" border="0" cellspacing="0" >
    <tr> 
      <td height="3" bgcolor="#ff0000" colspan="3" ></td>
    </tr>
    <tr align="center"> 
      <td width="50" > </td>
      <td > <font size="-1" color="#ff0000"><font size="+3" color="#FFFFFF"> 
        <font size="+2">PHP TEST#06</font> : Tree BBS TEST#1</font> <br>
        <font color="#FFFFFF">PHP+DBMを使用したツリー形式掲示板。<br>
          <?php $tm time();  echo "(ただいまの時刻は " date("Y/m/d H:i:s",$tm) . ")";  ?> 
          </font> 
        </font> </td>
      <td width="50" valign="top" ><a href="./"><br>
        戻る</a></td>
    </tr>
    <tr> 
      <td height="3" bgcolor="#ff0000" colspan="3" ></td>
    </tr>
  </table>


</div>


<!-- 本文 --> 

<div align="left">

    <?php
//        $dbm->dump();
//        $dbm->dumpRecords();

//        echo "stat = " . $dbm->insertLeaf("","ROOT") . "<BR>";
//        echo "stat = " . $dbm->insertLeaf("","ROOT#2"). "<BR>";
//        echo "stat = " . $dbm->insertLeaf("1","LEAF#2"). "<BR>";

//        $dbm->dumpRecords();
        
        
$keys = array();
        
$dbm->makeListRev($keys);

//        $cnt = count($keys);
//        for ( $i=0; $i<$cnt; $i++ ) {
//            echo "keys[$i]=$keys[$i] <BR>\n";
//        }

    
?>

    <br>
        <div align='center'><font size='+1' color='#e0e0ff'>
            BBSプログラム自体のテストです。<br>
            開発の都合でメッセージ内容を全削除する場合があります。<br>
            BBSとしては、<a href='sptbbs.php3'>サポートなどBBS</a>を用意しましたのでこちらをご利用ください。<br>
        </font>
    
        <br>



    <?php
        $resTitle 
"";
        if ( 
$prmResid != "" ) {
            
// ● 返信対象記事
             
$dbm->fetchR($prmResid,$data);
            if (
$data) {
            
//    echo "fetchR ; " . $dbm->margeData( $data );
                
prtarticle($prmResid$data $dbm->getNodeLevel($keys[$i]),"res");

                
$resTitle $data['title'];
                if ( 
substr$resTitle 0,) != "Re:" $resTitle "Re:" $resTitle;
            }else{
                
$prmResid "";
            }
        }
        echo 
"<br>\n";
    
?>



    <!-- ▼ 投稿フォーム開始 -->
    <form method="post" action="<?php echo $scriptname?>">
    

    <div align="center"> 
      <table width="90%" border="0" cellspacing="0" cellpadding="0">
        <tr bgcolor="#666666"> 
          <td> 
            <div align="center"><?php if ( $prmResid=="" ) echo "新規投稿"; else echo "メッセージへの返信"?></div>
          </td>
        </tr>
        <tr bgcolor="#333333"> 
          <td><font size="-1"><b>お名前 </b> 
            <input type="text" name="txtName"  <?php echo "value='" $cookie_treebbs_name "'"?>>
            </font> <font size="-1" color="#c0c0c0"> メールアドレス <input type="text" name="txtMail"   <?php echo "value='" $cookie_treebbs_mail "'"?>>
            <!--  削除パスワード <input type="text" name="txtPass" size="4"> -->
            </font></td>
        </tr>
        <tr bgcolor="#333333" valign="top"> 
          <td><font size="-1"><b>タイトル  <input type="text" name="txtTitle"  size="60" <?php echo "value='" $resTitle "'"?>></b></font></td>
        </tr>
        <tr bgcolor="#333333" valign="top"> 
          <td><font size="-1"><b>▼ 本文<br>
            </b> 
            <textarea name="txtMsg" cols="80" rows="6"></textarea>
            </font></td>
        </tr>
        <tr bgcolor="#333333"> 
          <td> <font size="-1"> </font> <font size="-1">
            <div align="center"></div>
            </font> <font size="-1" color="#c0c0c0"> 関連URL 
            <input type="text" name="txtUrl" size="70" value='http://'>
            <br>
            
            </font><br>
            <div align="center"> 
              <input type="submit" name="Submit" value="投稿"> <input type="submit" name="Submit" value="キャンセル">
            </div>
          </td>
        </tr>
      </table>
    </div>

    <br>

    <input type="hidden" name="prmNodeId" <?php echo "value=$prmResid"?> >

    </form>
    <!-- ▲ フォーム終了 -->

    <?php
    
// ● メッセージ
        
if ( $msg != ""  ) {
            echo 
"<div align='center'><font size='+2' color='#ff0000'>$msg</font></dev><br><br>";
        }
    
?>

    <div align='right'>
    <?php
    
// ● 最新記事一覧
    
if ( $prmResid == ""  ) {
    
?>
    <font size="-1" color="#CCCCCC">■ 最近の投稿</font><br>
    <?php
        $lastkey
=$dbm->fetch('lastkey' );
        
$listno=0;
        while ( 
$lastkey>&& $listno<) {
            
            
$key $dbm->fetch('key' $lastkey);
            
$dbm->fetchR($key,$data);
            
// echo "key=$key level=$level branch=$branch indent=$indent<BR>\n";
            
if ($data['exist']>0) {
    
?>
     <a href="#<?php echo $data['no']; ?>"><font color="#22cc22" size="-1">▼ [<?php echo $data['no']; ?><?php echo $data['title']; ?></font></a> <font color="#aaaaaa" size="-1">- <?php echo $data['name']; ?></font><font color="#22cc22" size="-1"> - <?php echo $data['date']; ?>  </font><BR>
    <?php
                $listno
++;
            }            
            
$lastkey--;
        }
    }
    
?>
    </div>


    <br><br>



    <div align='left'>
    <?php
    
// ● 記事一覧

        
if ( $prmResid == ""  ) {

            
$indent=0;
            
$branch=0;
            
$cnt count($keys);
            for ( 
$i=0$i<$cnt$i++ ) {
                
$key $keys[$i];
                
$indent += ($branch-1);

                
$level$dbm->getNodeLevel($key);
                
$branch$dbm->fetch('last' $key);

                if ( 
$level == ) { $indent=0$branch++; }
            
//    echo "keys[$i]=$keys[$i] <BR>\n";

                
$dbm->fetchR($key,$data);
            
//    echo "fetchR ; " . $dbm->margeData( $data );
            //    if ($data['exist']>0) prtarticle( $key,$data , $dbm->getNodeLevel($key));
            //    echo "key=$key level=$level branch=$branch indent=$indent<BR>\n";
                
if ($data['exist']>0prtarticle$key,$data ,$indent+1);
            }
        }

    
?>
    </div>

    <br>


<!-- 本文 : フッター--> 
    
    <table width="100%" border="0">
        <tr>
            <td>
                
          <div align="right"><a href="./">戻る</a></div>
            </td>
        </tr>

    </table>
    
    <br>
    




<!-- 本文終端 --> 


<?php 

    
if ( $debug  prtvars();
    if ( 
$src ) {
        echo 
"phps作成 結果:" copy"treebbs.php3" "treebbs.phps" )  . "," copy"treedbmclass.php3" "treedbmclass.phps" );
    } 
?>

</div>



<BR>

<div>
<table align="center" background="bgCir01.gif" width="100%" border="0" cellspacing="0" >
        <tr><td height="3" bgcolor="#ff0000" ></td></tr>
        <tr>
      <td align="right" > <font size="-1" color="#ff0000"> <a href="http://www.dokokano.com/ftlabo/"><img src="linkFTLabo01.gif" align="right" height="31" width="88" hspace="0" vspace="0" border="0"></a> 
        PHP TEST#06 : Tree BBS [2001/01/13]  <BR>
            Programmed by FUKAYA Takashi(F/T)  <BR><BR>
            </font>
        </td></tr>

        </table>
        <div align="right">
            <SCRIPT Language="JavaScript">
            <!--
            document.write("<img src='http://www.dokokano.com/cgi-bin/ftcount.cgi?dokokanoFTLaboTest06:"+document.referrer+"' height='1' width='32'");
            // -->
            </SCRIPT>
        </div>
</div>

<?php
    
// ==================== PHP TARMINATE CODE ====================
    
$dbm->close();

?>

</div></body>
</html>