From: root Date: Sat, 25 Feb 2012 15:00:59 +0000 (+0900) Subject: テンプレートエンジンの更新 X-Git-Url: http://git.osdn.net/view?p=cosmic%2Fsource.git;a=commitdiff_plain;h=826000cb51ed9a106aebd6460f344a16a5215810 テンプレートエンジンの更新 --- diff --git a/browse.php b/browse.php index 5b0fe26..9f0b7de 100644 --- a/browse.php +++ b/browse.php @@ -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 diff --git a/config/init.inc.php b/config/init.inc.php index b1a553e..76291cb 100644 --- a/config/init.inc.php +++ b/config/init.inc.php @@ -1,28 +1,17 @@ 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); diff --git a/config/version.inc.php b/config/version.inc.php index a851583..8e40ffa 100644 --- a/config/version.inc.php +++ b/config/version.inc.php @@ -1,6 +1,6 @@ \ No newline at end of file diff --git a/lib/cmn.inc.php b/lib/cmn.inc.php new file mode 100644 index 0000000..b0d62d0 --- /dev/null +++ b/lib/cmn.inc.php @@ -0,0 +1,32 @@ + \ No newline at end of file diff --git a/lib/common.inc.php b/lib/common.inc.php deleted file mode 100644 index 9331dd2..0000000 --- a/lib/common.inc.php +++ /dev/null @@ -1,67 +0,0 @@ - \ No newline at end of file diff --git a/lib/debug.inc.php b/lib/debug.inc.php index 6ce02d5..941addb 100644 --- a/lib/debug.inc.php +++ b/lib/debug.inc.php @@ -1,5 +1,9 @@ '.var_export($var,true).''; } diff --git a/lib/html.inc.php b/lib/html.inc.php index 93fda71..608541f 100644 --- a/lib/html.inc.php +++ b/lib/html.inc.php @@ -1,20 +1,75 @@ 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 diff --git a/page.php b/page.php index e4574a6..9ec8e8e 100644 --- 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, diff --git a/themes/default/browse.html b/themes/default/browse.html index 75f2845..9d95026 100644 --- a/themes/default/browse.html +++ b/themes/default/browse.html @@ -1,6 +1,6 @@ -{{include:_html_header.inc.html}} +{{include:html_header.inc.html}}
{{indexlist}}
{{comiclist}}
-{{include:_html_footer.inc.html}} +{{include:html_footer.inc.html}} diff --git a/themes/default/_html_footer.inc.html b/themes/default/include/html_footer.inc.html similarity index 100% rename from themes/default/_html_footer.inc.html rename to themes/default/include/html_footer.inc.html diff --git a/themes/default/_html_header.inc.html b/themes/default/include/html_header.inc.html similarity index 100% rename from themes/default/_html_header.inc.html rename to themes/default/include/html_header.inc.html diff --git a/themes/default/viewer.html b/themes/default/viewer.html index 046a469..1c60947 100644 --- a/themes/default/viewer.html +++ b/themes/default/viewer.html @@ -1,4 +1,4 @@ -{{include:_html_header.inc.html}} +{{include:html_header.inc.html}} -{{include:_html_footer.inc.html}} +{{include:html_footer.inc.html}} diff --git a/viwer.php b/viwer.php index 441f449..c6d029d 100644 --- 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',''); +$my_html->set_value('ix',$request__ix); +$my_html->set_value('head_additional',''); -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