fname = $filename ; $this->mainitem = array() ; $this->loopitem = array() ; $this->loopname = "" ; $this->loopone = "" ; if( $icode == "" ) { $this->kconv = 0 ; $this->kcin = "EUC-JP" ; $this->kcout = "SJIS" ; } else { $this->kconv = 1 ; $this->kcin = $icode ; $this->kcout = $ocode ; } } // デバッグモードフラグ設定 function dbgmode( $dbgflag ) { $this->dbg = $dbgflag ; } // 漢字の変換設定 // $conv 0:変換しない 1:変換する // $incode , $outcode : "EUC-JP" , "SJIS" , "JIS" function set_kcode( $conv , $incode = "EUC-JP" , $outcode = "SJIS" ) { if ( $conv ) { $this->kconv = 1 ; $this->kcin = $incode ; $this->kcout = $outcode ; } else { $this->kconv = 0 ; } } // テンプレートファイル名の変更 function set_fname( $filename ) { $this->fname = $filename ; } // 置換用変数の追加 function assign( $name , $value ) { if ( $this->loopname == "" ) { $this->mainitem[ "%".$name."%" ] = $value ; } else { $this->loopone[ "%".$name."%" ] = $value ; } if ( $this->dbg ) { echo "\n" ; } } // ループ名設定 function loopset( $lpname ) { // loopnext()を行う前にloopset()を呼び出すと // loopnext()以降に設定した値を忘れるので要注意。 if ( $lpname == "" ) { $this->loopname = "" ; return ; } $this->loopname = $lpname ; $this->loopone = array() ; if ( $this->dbg ) { echo "\n" ; } } // ループの繰り返し function loopnext() { // assign()でデータをセットしたら最後に必ずloopnext()を // 呼び出すようにする事。 if ( $this->loopname != "" ) { $this->loopitem[ $this->loopname ][] = $this->loopone ; $this->loopone = array() ; if ( $this->dbg ) { echo "\n\n" ; } } else { if ( $this->dbg ) { echo "\n\n" ; } } } // コード変換と出力 function flush( $outvar = 0 ) { $loopmode = "" ; // main時は""。それ以外はloopタグのloop名 $loopbuf = array() ; // loop中のhtmlデータと置換キー ob_start() ; if ( $this->dbg ) { echo "\n\n" ; } if ( !(@ $fp = fopen( $this->fname , "r" ) ) ) { echo "\nFile Open Error!!\n" ; $contents = ob_get_contents() ; ob_end_clean() ; echo $contents ; exit ; } $outflg = 1 ; while ( !feof( $fp ) ) { $lbuf = fgets( $fp, 16384 ) ; if ( $loopmode == "" ) { // main if ( $outflg ) { // 出力可である if ( eregi( "" , $lbuf , $regs ) ) { if ( $this->dbg ) { echo "\n\n" ; } $loopmode = $regs[1] ; continue ; } elseif ( eregi( "" , $lbuf , $regs ) ) { if( ! isset( $this->mainitem[ $regs[1] ] ) ) { $outflg = 0 ; } if ( $this->dbg ) { echo "\n\n" ; } } elseif ( eregi( "" , $lbuf , $regs ) ) { continue ; // 何もしない } else { $this->tmplchgline( &$lbuf , &$this->mainitem ) ; echo $lbuf ; continue ; } } else { // 出力不可 if ( eregi( "" , $lbuf , $regs ) ) { $outflg = 1 ; } continue ; } } else { // loopが設定してある。 if ( eregi( "" , $lbuf , $regs ) ) { // 全ての出力(loopの終了) if ( $this->dbg ) { echo "\n\n" ; } for ( $i = 0 ; is_array( $this->loopitem[$loopmode][$i] ) ; $i++ ) { $outflg = 1 ; for ( $j = 0 ; $loopbuf[$j] ; $j++ ) { if( $loopbuf[$j][0] == -1 ) { if ( isset( $this->loopitem[$loopmode][$i][$loopbuf[$j][2]] ) ) { $outflg = 1 ; } else { $outflg = 0 ; } continue ; } if( $loopbuf[$j][0] == -2 ) { $outflg = 1 ; continue ; } if ( ! $outflg ) { continue ; } $this->tmpllpchgline( &$otmp , &$loopbuf[$j] , &$this->loopitem[$loopmode][$i] ) ; echo $otmp ; } } $loopmode = "" ; $loopbuf = array() ; continue ; } // ループ内コンテンツを検索キーを探しながら溜める $mcco = preg_match_all( "/%[_\-a-z][_\-\.0-9a-z]*%/" , $lbuf , $regs ) ; if( $mcco == 0 ) { if ( eregi( "" , $lbuf , $dummy ) ) { $mcco = -2 ; } } if( $mcco == 1 ) { if ( eregi( "" , $lbuf , $dummy ) ) { $mcco = -1 ; } } $loopbuf[] = array_merge( array ( $mcco , $lbuf ) , $regs[0] ) ; continue ; } } fclose( $fp ) ; $contents = ob_get_contents() ; ob_end_clean() ; if( $outvar ) { if ( $this->kconv ) { return( mb_convert_encoding( &$contents , $this->kcout , $this->kcin ) ) ; } else { return( $contents ) ; } } else { if ( $this->kconv ) { echo mb_convert_encoding( &$contents , $this->kcout , $this->kcin ) ; } else { echo $contents ; } } } // local-function // 1行分の置換を行う。 // tmplchgline( string 置換される文字列 , array 置換データ ) // Return値 : ture:置換部分あり false:無し // 置換される文字列が、置換されて戻る。 function tmplchgline( $lbuf , $chgstrs ) { if( $mcco = preg_match_all( "/%[_\-a-z][_\-\.0-9a-z]*%/" , $lbuf , $regs , PREG_SET_ORDER ) ) { for( $i = 0 ; $i < $mcco ; $i++ ) { if ( isset( $chgstrs[$regs[$i][0]] ) ) { $otmp = str_replace( $regs[$i][0] , $chgstrs[$regs[$i][0]] , $lbuf ) ; $lbuf = $otmp ; } } return true ; } else { return false ; } } // ループ内の1行の置換を行う。 // tmpllpchgline( string 置換後文字列 , array 置換元データ // , array 置換データ ) function tmpllpchgline( $obuf , $lpbuf , $chgstrs ) { $chgco = $lpbuf[0] + 2 ; $obuf = $lpbuf[1] ; for ( $k = 2 ; $k < $chgco ; $k++ ) { if ( isset( $chgstrs[ $lpbuf[$k] ] ) ) { $xtmp = str_replace( $lpbuf[$k] , $chgstrs[ $lpbuf[$k] ] , $obuf ) ; $obuf = $xtmp ; } } } } // endof Tmpl-class ?>