出来上がりプログラミング ver.1.0

<?php /* -*-java-*- */
/**
 * SEO対策として、/xx/index.html というページを生成するのが効果がある。
 * 簡単にたくさん作れるようにしたスクリプト。
 *
 * まだ途中です。残り
 * - 引数でiniファイルを指定しる
 * -- argv を使えば良いよ。
 *
 * - 文字コードをどうにかしないとな。
 */

/*
 * php-4.3.0 以上で動作
 */
$libPath = "./Lib";
set_include_path( $libPath.":/yopt/apache_php/lib/php:".get_include_path() );

/*
 * PHP設定
 */
ini_set('memory_limit', "15M" );
session_cache_expire( 1 );
ini_set('session.lifetime', 30 );
ini_set('session.gc_maxlifetime', 40 );

define( '_D_SEP_', DIRECTORY_SEPARATOR );

/*
 * 文字コード設定
 */
ini_set('output_handler', 'mb_output_handler');
mb_detect_order( "eucjp-win,sjis-win,UTF-8,ISO-2022-JP,ASCII");
mb_internal_encoding('eucJP-win');
mb_http_input('auto');
mb_http_output('sjis-win');
mb_language('Japanese'); // ISO-2022-JP/Base64

/*
 * コールするライブラリ
 */
require_once('IniParser.class.php');
require_once('TemplateComponent.class.php');

/*
 * エラー出力制御(開発用)
 */
ini_set( 'error_reporting', E_ALL);
ini_set( 'display_errors', 1);


/*
 * メイン部分
 */
$ini =& new IniParser('define.ini.txt');

$site = getcwd()."/".$ini->getValue('site');
$template_dir = $ini->getValue('template_dir');


$fields = $ini->getFields();
foreach ( $fields as $field ) {

    if ( $field != 'general' ) {

        $template = $ini->getValue('template',$field);
        $template = implode( _D_SEP_, array( $template_dir, $template ) );

        $els = $ini->getElements($field);
        foreach ( $els as $key=>$val ) {
            $val = mb_convert_encoding( $val, "SJIS", "auto" );
            if ( 'template' != $key ) {
                $dir = implode( _D_SEP_, array( $site, $field, $key ) );
                if ( dirIsHere( $dir )) {
                    $tc =& new TemplateComponent($template, array('value'=>$val));
                    $string = $tc->getString();

                    if ( !($fp=fopen($dir._D_SEP_.'index.html',"w"))) {
                        echo "err: cannot make ".$dir._D_SEP_."index.html\n";
                    }
                    else {
                        echo "done: make ".$dir._D_SEP_."index.html\n";
                        fwrite( $fp, $string, strlen($string) );
                        fclose($fp);
                    }
                }
            }
        }
        echo "$field done.\n";
    }
}
echo "finished SeoSite maker.\n";

/**
 * ディレクトリの存在確認
 * ディレクトリがなければ、mkdir()する
 *
 * @param string $dir_path
 * @return bool
 */
function dirIsHere($dir_path){

    if( is_dir($dir_path) ){
        return TRUE;
    }

    $path_ary = explode("/",$dir_path);
    $trash = array_shift($path_ary);

    $file_path = "";
    foreach( $path_ary as $t_path){
        if( $t_path == "" ){
            return FALSE;
        }
        $file_path = $file_path."/".$t_path;
        //    echo "<BR><FONT Color=Red>$file_path</Font><BR>";
        if( !is_dir($file_path) ){
            if( !mkdir($file_path,0775) ){
                echo "<BR><FONT Color=BLUE>Cann't create Dir: $file_ath</Font><BR>";
                return FALSE;
            }
        }
    }

    /*
     * ファイルシステムの状況を調べるのは、コストがかかるため
     * 一度、調べたものはキャッシュするという仕様があるらしい
     * そのため、いま作ったディレクトリを確認するには
     * キャッシュをクリアする必要がある。(らしい)
     */
    clearstatcache();
    if( is_dir($dir_path) ){
        return TRUE;
    }else{
        return FALSE;
    }
}
?>