OSDN Git Service

テンプレートエンジンの更新
authorroot <landhere@users.sourceforge.jp>
Sat, 25 Feb 2012 15:00:59 +0000 (00:00 +0900)
committerroot <landhere@users.sourceforge.jp>
Sat, 25 Feb 2012 15:00:59 +0000 (00:00 +0900)
13 files changed:
browse.php
config/init.inc.php
config/version.inc.php
lib/cmn.inc.php [new file with mode: 0644]
lib/common.inc.php [deleted file]
lib/debug.inc.php
lib/html.inc.php
page.php
themes/default/browse.html
themes/default/include/html_footer.inc.html [moved from themes/default/_html_footer.inc.html with 100% similarity]
themes/default/include/html_header.inc.html [moved from themes/default/_html_header.inc.html with 100% similarity]
themes/default/viewer.html
viwer.php

index 5b0fe26..9f0b7de 100644 (file)
@@ -5,7 +5,7 @@
 include_once('./config/site.inc.php');
 
 // とりあえず/抜いとけば安全?
-$request__d = check_request('d','/^[^\/]+$/','');
+$request__d = $my_cgi->check_request('d','/^[^\/]+$/','');
 
 if ($request__d == '') {
 
@@ -32,8 +32,8 @@ if ($request__d == '') {
                $idx++;
        }
 
-       $obj_html->set_value('indexlist',implode('|',$index));
-       $obj_html->set_value('comiclist',$comic);
+       $my_html->set_value('indexlist',implode('|',$index));
+       $my_html->set_value('comiclist',$comic);
 
 } else {
 
@@ -55,9 +55,9 @@ if ($request__d == '') {
                }
        }
 
-       $obj_html->set_value('comiclist',$html);
+       $my_html->set_value('comiclist',$html);
 }
 
-echo $obj_html->apply('browse.html');
+$my_html->apply_template('browse.html',html::REMOVE_UNDEF_TAGS,html::OUTPUT_HTML);
 
 ?>
\ No newline at end of file
index b1a553e..76291cb 100644 (file)
@@ -1,28 +1,17 @@
 <?php
 
 // ライブラリの読み込み
-include_once(PATH_LIB.'/common.inc.php');
+include_once(PATH_LIB.'/cmn.inc.php');
 include_once(PATH_LIB.'/debug.inc.php');
 include_once(PATH_LIB.'/html.inc.php');
 
 
 // テンプレート用インスタンスの生成
-$obj_html = new html();
-
+$my_html = new html();
+$my_cgi  = new cgi();
 
 // 各テーマ構成ファイルへのパス
-$files = scandir(PATH_THEMES.'/default');
-foreach ($files as $one) {
-       if ($one=='.' || $one=='..') continue;
-       if (file_exists(PATH_THEMES.'/'.SITE_THEME.'/'.$one)) {
-               $path = PATH_THEMES.'/'.SITE_THEME.'/'.$one;
-               $url  = URL_THEMES.'/'.SITE_THEME.'/'.$one;
-       } else {
-               $path = PATH_THEMES.'/default/'.$one;
-               $url  = URL_THEMES.'/default/'.$one;
-       }
-       $obj_html->regist_theme_file($one,$path,$url);
-}
+$my_html->probe_theme_files(PATH_THEMES,URL_THEMES,SITE_THEME);
 
 // ディレクトリインデックス作成用
 $config__dir_index = array(
@@ -47,7 +36,7 @@ define('CACHE_EXT_LIST' ,'_lst.txt');
 
 // 定義した定数をすべてHTMLリソースに格納する
 $vars = get_defined_constants(true);
-$obj_html->set_value_array($vars['user']);
+$my_html->set_value_array($vars['user']);
 
 //echo var_export($GLOBALS,true);
 
index a851583..8e40ffa 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 
 define('SYSTEM_NAME'   ,'Cosmic : Simple Comic Server');
-define('SYSTEM_VERSION','0.12');
+define('SYSTEM_VERSION','0.12a');
 
 ?>
\ No newline at end of file
diff --git a/lib/cmn.inc.php b/lib/cmn.inc.php
new file mode 100644 (file)
index 0000000..b0d62d0
--- /dev/null
@@ -0,0 +1,32 @@
+<?php
+
+//==========================================================
+// フレームワーク関連
+//==========================================================
+
+// ライブラリのインクルード
+function cmn__include_libs($libs) {
+       foreach ($libs as $one) {
+               include_once($one.'.inc.php');
+       }
+}
+
+
+//==========================================================
+// ファイル操作関連
+//==========================================================
+
+// ファイルパスより拡張子を返す
+function cmn__get_fileext($path) {
+
+       $p = strrpos($path,'.');
+       if ($p!==false) {
+               $ext = substr($path,$p+1);
+       } else {
+               $ext = '';
+       }
+
+       return $ext;
+}
+
+?>
\ No newline at end of file
diff --git a/lib/common.inc.php b/lib/common.inc.php
deleted file mode 100644 (file)
index 9331dd2..0000000
+++ /dev/null
@@ -1,67 +0,0 @@
-<?php
-
-//
-// ウェブ関連
-//
-
-// GET, POST チェック用正規表現
-define('CV_TEXT'  ,'/^.+$/');
-define('CV_ALNUM' ,'/^[[:alnum:]]+$/');  // アルファベットと数値、[:alpha:] + [:digit:]
-define('CV_ALPHA' ,'/^[[:alpha:]]+$/');  // 大小文字アルファベット、 [:lower:] + [:upper:]
-define('CV_LOWER' ,'/^[[:lower:]]+$/');  // 小文字アルファベット
-define('CV_UPPER' ,'/^[[:upper:]]+$/');  // 大文字アルファベット
-define('CV_DIGIT' ,'/^[[:digit:]]+$/');  // 数値
-define('CV_BLANK' ,'/^[[:blank:]]+$/');  // 空白文字、スペースとタブ
-define('CV_CNTRL' ,'/^[[:cntrl:]]+$/');  // 制御文字(000-037, 177('DEL')
-define('CV_GRAPH' ,'/^[[:graph:]]+$/');  // グラフィカル文字 ([:alnum:] と [:punct:])|
-define('CV_PRINT' ,'/^[[:print:]]+$/');  // 印字可能な文字、[:alnum:] + [:punct:] + space
-define('CV_PUNCT' ,'/^[[:punct:]]+$/');  // パンクチュエーション文字 ! " # $ % & ' ( ) * + , - . /
-define('CV_SPACE' ,'/^[[:space:]]+$/');  // 空白文字、タブ、改行、水平タブ、給紙、キャリッジリターン、空白
-define('CV_XDIGIT','/^[[:xdigit:]]+$/'); // 16進数 0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f
-define('CV_MD5'   ,'/^[a-z0-9]{32}$/');  // md5ハッシュ値
-define('CV_EMAIL' ,'/^[\w.-]+\@([\w-]+\.)+\w+$/'); // メールアドレス
-
-// GET, POST パラメータチェック
-function check_request($var,$chk='',$def='') {
-
-       $val = isset($_REQUEST[$var]) ? $_REQUEST[$var] : $def;
-       if ($chk!='') {
-               if (preg_match($chk,$val)==0) {
-                       $val = $def;
-               }
-       }
-
-       $_REQUEST[$var] = $val;
-
-       return $val;
-}
-
-// 一般変数の内容チェック
-function check_value(&$val,$pat,$def) {
-
-       if (preg_match($pat,$val)==0) {
-               $val = $def;
-       }
-
-       return $val;
-}
-
-
-//
-// ファイル操作関連
-//
-
-// ファイルパスより拡張子を返す
-function common__get_fileext($path) {
-
-       $p = strrpos($path,'.');
-       if ($p!==false) {
-               $ext = substr($path,$p+1);
-       } else {
-               $ext = '';
-       }
-
-       return $ext;
-}
-
-?>
\ No newline at end of file
index 6ce02d5..941addb 100644 (file)
@@ -1,5 +1,9 @@
 <?php
 
+//==========================================================
+// \83f\83o\83b\83O\8aÖ\98A
+//==========================================================
+
 function debug__out($var) {
        echo '<pre>'.var_export($var,true).'</pre>';
 }
index 93fda71..608541f 100644 (file)
@@ -1,20 +1,75 @@
 <?php
 
-
 //-------------------------------------------------------------------
-// テンプレート
+// HTML出力テンプレートクラス
 //-------------------------------------------------------------------
 
-$_HTML = array();
-
 class html {
 
+       // 定数
+       const PP_TYPE    = 0;
+       const PP_NAME    = 1;
+       const PP_PARAM_1 = 2;
+       const PP_PARAM_2 = 3;
+       const PP_PARAM_3 = 4;
+       const PP_PARAM_4 = 5;
+       const PP_PARAM_5 = 6;
+
+       const REMOVE_UNDEF_TAGS = true;
+       const LEAVE_UNDEF_TAGS  = false;
+
+       const OUTPUT_HTML = true;
+       const RETURN_HTML = false;
+
        // 置き換え要素
        public $values = array();
 
        // テーマファイル格納用
        public $theme_files = array();
 
+       // コンストラクタ
+       public function html() {
+
+               $this->values['$$'] = array();
+
+       }
+
+       // テーマファイルを検索し登録する
+       public function probe_theme_files($root_path,$root_url,$use_theme,$default_theme='default') {
+
+               $def_path_len = strlen($root_path.'/'.$default_theme.'/');
+
+               $files = $this->_scandir_r($root_path.'/'.$default_theme);
+               foreach ($files as $def_fid) {
+                       $one = substr($def_fid,$def_path_len);
+                       $use_fid = $root_path.'/'.$use_theme.'/'.$one;
+                       if (file_exists($use_fid)) {
+                               $path = $use_fid;
+                               $url  = $root_url.'/'.$use_theme.'/'.$one;
+                       } else {
+                               $path = $def_fid;
+                               $url  = $root_url.'/'.$default_theme.'/'.$one;
+                       }
+                       $this->regist_theme_file($one,$path,$url);
+               }
+
+       }
+
+       // 再帰的にディレクトリをスキャンする関数
+       private function _scandir_r($path) {
+               $result = array();
+               $files = scandir($path);
+               foreach ($files as $one) {
+                       if ($one=='.' || $one=='..') continue;
+                       if (is_dir($path.'/'.$one)) {
+                               $result = array_merge($result,$this->_scandir_r($path.'/'.$one));
+                       } else {
+                               $result[] = $path.'/'.$one;
+                       }
+               }
+               return $result;
+       }
+
        // テーマファイルの登録
        public function regist_theme_file($name,$path,$url) {
                $this->theme_files[$name] = array(
@@ -38,13 +93,43 @@ class html {
                $this->values[$var] = $val;
        }
 
-       // 置き換え要素の登録
+       // 置き換え要素の登録(配列版)
        public function set_value_array($ary) {
                $this->values = array_merge($this->values,$ary);
        }
 
-       // テンプレートの適用
-       public function apply($tmpl) {
+       // スタイルの摘要
+       public function apply_style($row,$style) {
+               $html = file_get_contents($this->get_theme_file_path('style/'.$style));
+
+               // PHP5.3以降(無名関数版)
+/*
+               $html = preg_replace_callback(
+                       '/{{row:(.+?)}}/',
+                       function ($match) {
+                               return $row[$match[1]];
+                       },
+                       $html
+               );
+*/
+               // PHP5.3未満(クロージャスコープ参照不可なためゴリ展開)
+               $seri = serialize($row);
+               $html = preg_replace_callback(
+                       '/{{row:(.+?)}}/',
+                       create_function(
+                               '$match',
+                               '$seri = '.var_export($seri,true).';'.
+                               '$row = unserialize($seri);' .
+                               'return $row[$match[1]];'
+                       ),
+                       $html
+               );
+
+               return $html;
+       }
+
+       // テンプレートの適用(最終出力用)
+       public function apply_template($tmpl,$fixtag=false,$output=false) {
 
                $tmpl = $this->get_theme_file_path($tmpl);
 
@@ -54,12 +139,64 @@ class html {
                        $html = '{{doc}}';
                }
 
+               $html = $this->apply($html,$fixtag);
+
+               if ($output) {
+                       echo $html;
+               }
+
+               return $html;
+       }
+
+       // {{}}タグの置換
+       public function apply($html,$fixtag=false) {
+
+               // include:xxxx
+               // テンプレートファイルを読み込んで置き換えます
                $html = $this->_apply_include($html);
+
+               // 未解決タグの処理
+               if ($fixtag) {
+                       $html = $this->_remove_undefined_tags($html);
+               }
+
+               // その他
+               // 通常パラメータを置き換えます
                $html = $this->_apply_replace($html);
 
                return $html;
        }
 
+       // $$置き換え準備
+       public function prepare($html) {
+
+               $html = preg_replace_callback(
+                       '/{{set:(.+?)}}/',
+                       array($this,'_prepare_callback'),
+                       $html
+               );
+
+               return $html;
+
+       }
+
+       // $$置き換え準備(コールバック関数)
+       private function _prepare_callback($match) {
+
+               $dat = preg_split('/,/',$match[1]);
+
+               $this->values['$$'][] = $dat;
+
+               if ($dat[1]=='none') {
+                       $tag = '';
+               } else {
+                       $tag = '{{'.$dat[1].'}}';
+               }
+
+               return $tag;
+
+       }
+
        // インクルード処理
        private function _apply_include($html) {
 
@@ -74,22 +211,21 @@ class html {
        }
 
        // インクルード処理(コールバック関数)
-       private function _apply_include_callback($matches) {
+       private function _apply_include_callback($match) {
 
-               $path = $this->get_theme_file_path($matches[1]);
+               $path = $this->get_theme_file_path('include/'.$match[1]);
                if (file_exists($path)) {
                        return file_get_contents($path);
                } else {
                        return '';
                }
-
        }
 
        // 置き換え処理
        private function _apply_replace($html) {
 
                $html = preg_replace_callback(
-                       '/{{(.+?)}}/',
+                       '/{{((.+):)?(.+?)}}/',
                        array($this,'_apply_replace_callback'),
                        $html
                );
@@ -99,16 +235,102 @@ class html {
        }
 
        // 置き換え処理(コールバック関数)
-       private function _apply_replace_callback($matches) {
+       private function _apply_replace_callback($match) {
+
+               $val = '';
+
+               switch ($match[2]) {
+               case 'themefile':
+                       $val = $this->get_theme_file_url($match[3]);
+                       break;
+               default:
+                       if (array_key_exists($match[3],$this->values)) {
+                               $val = $this->values[$match[3]];
+                       } else {
+                               $val = $match[0];
+                       }
+               }
 
-               if (array_key_exists($matches[1],$this->values)) {
-                       return $this->values[$matches[1]];
-               } else {
-                       return '';
+               return $val;
+       }
+
+       // 未解決タグを解決
+       private function _remove_undefined_tags($html) {
+
+               $html = preg_replace_callback(
+                       '/{{((.+):)?(.+?)}}/',
+                       array($this,'_remove_undefined_tags_callback'),
+                       $html
+               );
+
+               return $html;
+
+       }
+
+       // 未解決タグを解決(コールバック関数)
+       private function _remove_undefined_tags_callback($match) {
+
+               $val = $match[0];
+
+               if ($match[2]=='' && !array_key_exists($match[3],$this->values)) {
+                       $val = '';
+               }
+
+               return $val;
+       }
+
+}
+
+
+//-------------------------------------------------------------------
+// CGI処理用クラス
+//-------------------------------------------------------------------
+
+class cgi {
+
+       // GET, POST チェック用正規表現
+       const CV_TEXT   = '/^.+$/';
+       const CV_ALNUM  = '/^[[:alnum:]]+$/';  // アルファベットと数値、[:alpha:] + [:digit:]
+       const CV_ALPHA  = '/^[[:alpha:]]+$/';  // 大小文字アルファベット、 [:lower:] + [:upper:]
+       const CV_LOWER  = '/^[[:lower:]]+$/';  // 小文字アルファベット
+       const CV_UPPER  = '/^[[:upper:]]+$/';  // 大文字アルファベット
+       const CV_DIGIT  = '/^[[:digit:]]+$/';  // 数値
+       const CV_BLANK  = '/^[[:blank:]]+$/';  // 空白文字、スペースとタブ
+       const CV_CNTRL  = '/^[[:cntrl:]]+$/';  // 制御文字(000-037, 177('DEL')
+       const CV_GRAPH  = '/^[[:graph:]]+$/';  // グラフィカル文字 ([:alnum:] と [:punct:])|
+       const CV_PRINT  = '/^[[:print:]]+$/';  // 印字可能な文字、[:alnum:] + [:punct:] + space
+       const CV_PUNCT  = '/^[[:punct:]]+$/';  // パンクチュエーション文字 ! " # $ % & ' ( ) * + , - . /
+       const CV_SPACE  = '/^[[:space:]]+$/';  // 空白文字、タブ、改行、水平タブ、給紙、キャリッジリターン、空白
+       const CV_XDIGIT = '/^[[:xdigit:]]+$/'; // 16進数 0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f
+       const CV_MD5    = '/^[a-z0-9]{32}$/';  // md5ハッシュ値
+       const CV_EMAIL  = '/^[\w.-]+\@([\w-]+\.)+\w+$/'; // メールアドレス
+
+       // GET, POST パラメータチェック
+       public static function check_request($var,$chk='',$def='') {
+
+               $val = isset($_REQUEST[$var]) ? $_REQUEST[$var] : $def;
+               if ($chk!='') {
+                       if (preg_match($chk,$val)==0) {
+                               $val = $def;
+                       }
                }
 
+               $_REQUEST[$var] = $val;
+
+               return $val;
+       }
+
+       // 一般変数の内容チェック
+       public static function check_value(&$val,$pat,$def) {
+
+               if (preg_match($pat,$val)==0) {
+                       $val = $def;
+               }
+
+               return $val;
        }
 
 }
 
+
 ?>
\ No newline at end of file
index e4574a6..9ec8e8e 100644 (file)
--- a/page.php
+++ b/page.php
@@ -2,8 +2,8 @@
 
 include_once('./config/site.inc.php');
 
-$request__ix = check_request('ix',CV_MD5  ,'');
-$request__pg = check_request('pg',CV_DIGIT,0);
+$request__ix = $my_cgi->check_request('ix',cgi::CV_MD5  ,'');
+$request__pg = $my_cgi->check_request('pg',cgi::CV_DIGIT,0);
 
 $fid_index = PATH_CACHE.'/'.$request__ix.CACHE_EXT_INDEX;
 $fid_list  = PATH_CACHE.'/'.$request__ix.CACHE_EXT_LIST;
@@ -30,7 +30,7 @@ if (!file_exists($fid_list) || true) {
        while(($zip_entry = zip_read($zip)) !== false){
                $no++;
                $zip_entry_name = zip_entry_name($zip_entry);
-               $zip_entry_type = strtolower(common__get_fileext($zip_entry_name));
+               $zip_entry_type = strtolower(cmn__get_fileext($zip_entry_name));
                if ($zip_entry_type=='' || mb_strpos('jpg/jpeg/png/gif',$zip_entry_type)===false) continue;
                $entry_data[] = array(
                        'no'   => $no,
index 75f2845..9d95026 100644 (file)
@@ -1,6 +1,6 @@
-{{include:_html_header.inc.html}}
+{{include:html_header.inc.html}}
 
 <div id="indexlist">{{indexlist}}</div>
 <div id="comiclist">{{comiclist}}</div>
 
-{{include:_html_footer.inc.html}}
+{{include:html_footer.inc.html}}
index 046a469..1c60947 100644 (file)
@@ -1,4 +1,4 @@
-{{include:_html_header.inc.html}}
+{{include:html_header.inc.html}}
 
 <div id="navigator">
        <div id="backlink"><a href="javascript://" onclick="javascript:history.back();">戻る</a></div>
@@ -11,4 +11,4 @@
        <p id="request_ix">{{ix}}</p>
 </div>
 
-{{include:_html_footer.inc.html}}
+{{include:html_footer.inc.html}}
index 441f449..c6d029d 100644 (file)
--- a/viwer.php
+++ b/viwer.php
@@ -2,11 +2,11 @@
 
 include_once('./config/site.inc.php');
 
-$request__ix = check_request('ix',CV_MD5,'');
+$request__ix = $my_cgi->check_request('ix',cgi::CV_MD5,'');
 
-$obj_html->set_value('ix',$request__ix);
-$obj_html->set_value('head_additional','<script type="text/javascript" src="'.$obj_html->get_theme_file_url('viewer.js').'"></script>');
+$my_html->set_value('ix',$request__ix);
+$my_html->set_value('head_additional','<script type="text/javascript" src="'.$my_html->get_theme_file_url('viewer.js').'"></script>');
 
-echo $obj_html->apply('viewer.html');
+$my_html->apply_template('viewer.html',html::REMOVE_UNDEF_TAGS,html::OUTPUT_HTML);
 
 ?>
\ No newline at end of file