[21595] | 1 | <?php |
---|
| 2 | /* |
---|
| 3 | * This file is part of EC-CUBE |
---|
| 4 | * |
---|
| 5 | * Copyright(c) 2000-2012 LOCKON CO.,LTD. All Rights Reserved. |
---|
| 6 | * |
---|
| 7 | * http://www.lockon.co.jp/ |
---|
| 8 | * |
---|
| 9 | * This program is free software; you can redistribute it and/or |
---|
| 10 | * modify it under the terms of the GNU General Public License |
---|
| 11 | * as published by the Free Software Foundation; either version 2 |
---|
| 12 | * of the License, or (at your option) any later version. |
---|
| 13 | * |
---|
| 14 | * This program is distributed in the hope that it will be useful, |
---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 17 | * GNU General Public License for more details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License |
---|
| 20 | * along with this program; if not, write to the Free Software |
---|
| 21 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
| 22 | */ |
---|
| 23 | |
---|
| 24 | /** |
---|
| 25 | * テンプレートをDOM変形するためのヘルパークラス |
---|
| 26 | * |
---|
| 27 | * @package Helper |
---|
| 28 | * @version $Id$ |
---|
| 29 | */ |
---|
| 30 | class SC_Helper_Transform { |
---|
| 31 | protected $objDOM; |
---|
| 32 | protected $arrSmartyTagsOrg; |
---|
| 33 | protected $arrSmartyTagsSub; |
---|
| 34 | protected $smarty_tags_idx; |
---|
| 35 | protected $arrErr; |
---|
| 36 | protected $arrElementTree; |
---|
| 37 | protected $arrSelectElements; |
---|
| 38 | protected $html_source; |
---|
[21775] | 39 | protected $header_source; |
---|
| 40 | protected $footer_source; |
---|
[21595] | 41 | protected $search_depth; |
---|
| 42 | |
---|
| 43 | const ERR_TARGET_ELEMENT_NOT_FOUND = 1; |
---|
| 44 | |
---|
| 45 | /** |
---|
| 46 | * SmartyのHTMLソースをDOMに変換しておく |
---|
| 47 | * |
---|
| 48 | * @param string $source 変形対象のテンプレート |
---|
| 49 | * @return void |
---|
| 50 | */ |
---|
[21767] | 51 | public function __construct($source) { |
---|
[21595] | 52 | $this->objDOM = new DOMDocument(); |
---|
| 53 | $this->objDOM->strictErrorChecking = false; |
---|
| 54 | $this->snip_count = 0; |
---|
| 55 | $this->smarty_tags_idx = 0; |
---|
| 56 | $this->arrErr = array(); |
---|
| 57 | $this->arrElementTree = array(); |
---|
| 58 | $this->arrSelectElements = array(); |
---|
| 59 | $this->html_source = $source; |
---|
[21775] | 60 | $this->header_source = NULL; |
---|
| 61 | $this->footer_source = NULL; |
---|
[21595] | 62 | $this->search_depth = 0; |
---|
| 63 | |
---|
[21886] | 64 | $encoding = mb_detect_encoding($source); |
---|
| 65 | if (!in_array($encoding, array('ASCII', 'UTF-8'))) { |
---|
[21887] | 66 | if ($encoding === false) { |
---|
| 67 | $encoding = '検出不能'; |
---|
| 68 | } |
---|
| 69 | $msg = 'テンプレートの文字コードが「' . $encoding . '」です。「UTF-8」のみ利用できます。'; |
---|
[21886] | 70 | SC_Utils_Ex::sfDispSiteError(FREE_ERROR_MSG, '', true, $msg); |
---|
[21595] | 71 | } |
---|
[21829] | 72 | |
---|
[21775] | 73 | // Smartyのコメントを削除 |
---|
| 74 | $source = preg_replace( |
---|
| 75 | '/<\!--{\*.+?\*\}-->/s', |
---|
| 76 | '', |
---|
| 77 | $source |
---|
| 78 | ); |
---|
[21807] | 79 | |
---|
| 80 | // headタグの内側を退避 |
---|
| 81 | $source = preg_replace_callback( |
---|
| 82 | '/(<head[^>]*>)(.+)(<\/head>)/is', |
---|
| 83 | array($this, 'lfCaptureHeadTags2Comment'), |
---|
| 84 | $source |
---|
| 85 | ); |
---|
| 86 | |
---|
[21595] | 87 | // JavaScript内にSmartyのタグが存在するものを、コメント形式に置換 |
---|
| 88 | $source = preg_replace_callback( |
---|
| 89 | '/<script.+?\/script>/s', |
---|
| 90 | array($this, 'lfCaptureSmartyTags2Comment'), |
---|
| 91 | $source |
---|
| 92 | ); |
---|
[21807] | 93 | |
---|
[21595] | 94 | // HTMLタグ内にSmartyのタグが存在するものを、まず置換する |
---|
| 95 | $source = preg_replace_callback( |
---|
| 96 | '/<(?:[^<>]*?(?:(<\!--\{.+?\}-->)|(?R))[^<>]*?)*?>/s', |
---|
| 97 | array($this, 'lfCaptureSmartyTagsInTag'), |
---|
| 98 | $source |
---|
| 99 | ); |
---|
[21807] | 100 | |
---|
[21595] | 101 | // 通常のノードに属する部分を、コメント形式に置換 |
---|
| 102 | $source = preg_replace_callback( |
---|
| 103 | '/<\!--{.+?\}-->/s', |
---|
| 104 | array($this, 'lfCaptureSmartyTags2Comment'), |
---|
| 105 | $source |
---|
| 106 | ); |
---|
| 107 | |
---|
[21807] | 108 | // HTMLタグの有無、BODYタグの有無で動作を切り替える |
---|
| 109 | if (preg_match('/^(.*?)(<html[^>]*>.+<\/html>)(.*?)$/is', $source, $arrMatches)) { |
---|
[21775] | 110 | $this->header_source = $arrMatches[1]; |
---|
| 111 | $source = $arrMatches[2]; |
---|
| 112 | $this->footer_source = $arrMatches[3]; |
---|
| 113 | } |
---|
[21807] | 114 | elseif (preg_match('/^.*?<body[^>]*>.+<\/body>.*$/is', $source)) { |
---|
| 115 | $source = '<meta http-equiv="content-type" content="text/html; charset=UTF-8" /><html><!--TemplateTransformer start-->'.$source.'<!--TemplateTransformer end--></html>'; |
---|
| 116 | } |
---|
| 117 | else { |
---|
| 118 | $source = '<meta http-equiv="content-type" content="text/html; charset=UTF-8" /><html><body><!--TemplateTransformer start-->'.$source.'<!--TemplateTransformer end--></body></html>'; |
---|
| 119 | } |
---|
[21829] | 120 | |
---|
[21595] | 121 | @$this->objDOM->loadHTML($source); |
---|
| 122 | $this->lfScanChild($this->objDOM); |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | |
---|
| 126 | /** |
---|
| 127 | * jQueryライクなセレクタを用いてエレメントを選択する |
---|
| 128 | * |
---|
| 129 | * @param string $selector セレクタ |
---|
| 130 | * @param integer $index インデックス(指定がある場合) |
---|
| 131 | * @param boolean $require エレメントが見つからなかった場合、エラーとするか |
---|
| 132 | * @param string $err_msg エラーメッセージ |
---|
| 133 | * @return SC_Helper_Transformオブジェクト |
---|
| 134 | */ |
---|
| 135 | public function select($selector, $index = NULL, $require = true, $err_msg = NULL) { |
---|
| 136 | $this->arrSelectElements = array(); |
---|
| 137 | $this->search_depth = 0; |
---|
| 138 | |
---|
| 139 | $regex = $this->lfSelector2Regex($selector); // セレクタをツリー検索用正規表現に変換 |
---|
| 140 | |
---|
| 141 | $cur_idx = 0; |
---|
| 142 | // ツリーを初めから全検索する |
---|
[21596] | 143 | for ($iLoop=0; $iLoop < count($this->arrElementTree); $iLoop++) { |
---|
[21595] | 144 | if (preg_match($regex, $this->arrElementTree[$iLoop][0])) { |
---|
| 145 | // インデックスが指定されていない(見つけたエレメント全て)、もしくは指定されたインデックスなら選択する |
---|
| 146 | if (is_null($index) || $cur_idx == $index) { |
---|
| 147 | $this->lfAddElement($iLoop, $this->arrElementTree[$iLoop]); |
---|
| 148 | } |
---|
| 149 | $cur_idx++; |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | // 見つからなかった場合エラーとするならエラーを記録する |
---|
| 154 | if ($require && $cur_idx == 0) { |
---|
| 155 | $this->lfSetError( |
---|
| 156 | $selector, |
---|
| 157 | self::ERR_TARGET_ELEMENT_NOT_FOUND, |
---|
| 158 | $err_msg |
---|
| 159 | ); |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | return $this; |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | |
---|
| 166 | /** |
---|
| 167 | * jQueryライクなセレクタを用いて、選択したエレメント内をさらに絞り込む |
---|
| 168 | * |
---|
| 169 | * @param string $selector セレクタ |
---|
| 170 | * @param integer $index インデックス(指定がある場合) |
---|
| 171 | * @param boolean $require エレメントが見つからなかった場合、エラーとするか |
---|
| 172 | * @param string $err_msg エラーメッセージ |
---|
| 173 | * @return SC_Helper_Transformオブジェクト |
---|
| 174 | */ |
---|
| 175 | public function find($selector, $index = NULL, $require = true, $err_msg = NULL) { |
---|
| 176 | $arrParentElements = $this->arrSelectElements[$this->search_depth]; |
---|
| 177 | $this->search_depth++; |
---|
| 178 | $this->arrSelectElements[$this->search_depth] = array(); |
---|
| 179 | |
---|
| 180 | foreach ($arrParentElements as $key => &$objElement) { |
---|
| 181 | $regex = $this->lfSelector2Regex($selector, $objElement[0]); // セレクタをツリー検索用正規表現に変換(親要素のセレクタを頭に付ける) |
---|
| 182 | |
---|
| 183 | $cur_idx = 0; |
---|
| 184 | // 親エレメント位置からツリーを検索する |
---|
[21596] | 185 | for ($iLoop=$objElement[0]; $iLoop < count($this->arrElementTree); $iLoop++) { |
---|
[21595] | 186 | if (preg_match($regex, $this->arrElementTree[$iLoop][0])) { |
---|
| 187 | // インデックスが指定されていない(見つけたエレメント全て)、もしくは指定されたインデックスなら選択する |
---|
| 188 | if (is_null($index) || $cur_idx == $index) { |
---|
| 189 | $this->lfAddElement($iLoop, $this->arrElementTree[$iLoop]); |
---|
| 190 | } |
---|
| 191 | $cur_idx++; |
---|
| 192 | } |
---|
| 193 | } |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | // 見つからなかった場合エラーとするならエラーを記録する |
---|
| 197 | if ($require && count($this->arrSelectElements[$this->search_depth]) == 0) { |
---|
| 198 | $this->lfSetError( |
---|
| 199 | $selector, |
---|
| 200 | self::ERR_TARGET_ELEMENT_NOT_FOUND, |
---|
| 201 | $err_msg |
---|
| 202 | ); |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | return $this; |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | |
---|
| 209 | /** |
---|
| 210 | * 選択状態を指定数戻す |
---|
| 211 | * |
---|
| 212 | * @param int $back_num 選択状態を戻す数 |
---|
| 213 | * @return SC_Helper_Transformオブジェクト |
---|
| 214 | */ |
---|
| 215 | public function end($back_num = 1) { |
---|
| 216 | if ($this->search_depth >= $back_num) { |
---|
| 217 | $this->search_depth -= $back_num; |
---|
[21596] | 218 | } else { |
---|
[21595] | 219 | $this->search_depth = 0; |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | return $this; |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | |
---|
| 226 | /** |
---|
| 227 | * 要素の前にHTMLを挿入 |
---|
| 228 | * |
---|
| 229 | * @param string $html_snip 挿入するHTMLの断片 |
---|
| 230 | * @return SC_Helper_Transformオブジェクト |
---|
| 231 | */ |
---|
| 232 | public function insertBefore($html_snip) { |
---|
| 233 | foreach ($this->arrSelectElements[$this->search_depth] as $key => $objElement) { |
---|
| 234 | $this->lfSetTransform('insertBefore', $objElement[0], $html_snip); |
---|
| 235 | } |
---|
| 236 | return $this; |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | |
---|
| 240 | /** |
---|
| 241 | * 要素の後にHTMLを挿入 |
---|
| 242 | * |
---|
| 243 | * @param string $html_snip 挿入するHTMLの断片 |
---|
| 244 | * @return SC_Helper_Transformオブジェクト |
---|
| 245 | */ |
---|
| 246 | public function insertAfter($html_snip) { |
---|
| 247 | foreach ($this->arrSelectElements[$this->search_depth] as $key => $objElement) { |
---|
| 248 | $this->lfSetTransform('insertAfter', $objElement[0], $html_snip); |
---|
| 249 | } |
---|
| 250 | return $this; |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | |
---|
| 254 | /** |
---|
| 255 | * 要素の先頭にHTMLを挿入 |
---|
| 256 | * |
---|
| 257 | * @param string $html_snip 挿入するHTMLの断片 |
---|
| 258 | * @return SC_Helper_Transformオブジェクト |
---|
| 259 | */ |
---|
| 260 | public function appendFirst($html_snip) { |
---|
| 261 | foreach ($this->arrSelectElements[$this->search_depth] as $key => $objElement) { |
---|
| 262 | $this->lfSetTransform('appendFirst', $objElement[0], $html_snip); |
---|
| 263 | } |
---|
| 264 | return $this; |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | |
---|
| 268 | /** |
---|
| 269 | * 要素の末尾にHTMLを挿入 |
---|
| 270 | * |
---|
| 271 | * @param string $html_snip 挿入するHTMLの断片 |
---|
| 272 | * @return SC_Helper_Transformオブジェクト |
---|
| 273 | */ |
---|
| 274 | public function appendChild($html_snip) { |
---|
| 275 | foreach ($this->arrSelectElements[$this->search_depth] as $key => $objElement) { |
---|
| 276 | $this->lfSetTransform('appendChild', $objElement[0], $html_snip); |
---|
| 277 | } |
---|
| 278 | return $this; |
---|
| 279 | } |
---|
| 280 | |
---|
| 281 | |
---|
| 282 | /** |
---|
| 283 | * 要素を指定したHTMLに置換 |
---|
| 284 | * |
---|
| 285 | * @param string $html_snip 置換後のHTMLの断片 |
---|
| 286 | * @return SC_Helper_Transformオブジェクト |
---|
| 287 | */ |
---|
| 288 | public function replaceElement($html_snip) { |
---|
| 289 | foreach ($this->arrSelectElements[$this->search_depth] as $key => &$objElement) { |
---|
| 290 | $this->lfSetTransform('replaceElement', $objElement[0], $html_snip); |
---|
| 291 | } |
---|
| 292 | return $this; |
---|
| 293 | } |
---|
| 294 | |
---|
| 295 | |
---|
| 296 | /** |
---|
| 297 | * 要素を削除する |
---|
| 298 | * |
---|
| 299 | * @return SC_Helper_Transformオブジェクト |
---|
| 300 | */ |
---|
| 301 | public function removeElement() { |
---|
| 302 | foreach ($this->arrSelectElements[$this->search_depth] as $key => &$objElement) { |
---|
| 303 | $this->lfSetTransform('replaceElement', $objElement[0], ''); |
---|
| 304 | } |
---|
| 305 | return $this; |
---|
| 306 | } |
---|
| 307 | |
---|
| 308 | |
---|
| 309 | /** |
---|
| 310 | * HTMLに戻して、Transform用に付けたマーカーを削除し、Smartyのタグを復元する |
---|
| 311 | * |
---|
| 312 | * @return string トランスフォーム済みHTML。まったくトランスフォームが行われなかった場合は元のHTMLを返す。。 |
---|
| 313 | */ |
---|
| 314 | public function getHTML() { |
---|
| 315 | if (count($this->arrErr)) { |
---|
| 316 | // エラーメッセージ組み立て |
---|
[21596] | 317 | $err_msg = ''; |
---|
[21595] | 318 | foreach ($this->arrErr as $arrErr) { |
---|
| 319 | if ($arrErr['err_msg']) { |
---|
[21596] | 320 | $err_msg .= '<br />'.$arrErr['err_msg']; |
---|
[21595] | 321 | } else { |
---|
| 322 | if ($arrErr['type'] == self::ERR_TARGET_ELEMENT_NOT_FOUND) { |
---|
| 323 | $err_msg .= "<br />${arrErr['selector']} が存在しません"; |
---|
| 324 | } else { |
---|
[21596] | 325 | $err_msg .= '<br />'.print_r($arrErr, true); |
---|
[21595] | 326 | } |
---|
| 327 | } |
---|
| 328 | } |
---|
| 329 | // エラー画面表示 |
---|
| 330 | SC_Utils_Ex::sfDispSiteError(FREE_ERROR_MSG, '', true, 'テンプレートの操作に失敗しました。' . $err_msg); |
---|
| 331 | } elseif ($this->snip_count) { |
---|
| 332 | $html = $this->objDOM->saveHTML(); |
---|
[21807] | 333 | $html = preg_replace('/^.*(<html[^>]*>)/s', '$1', $html); |
---|
| 334 | $html = preg_replace('/(<\/html>).*$/s', '$1', $html); |
---|
[21595] | 335 | $html = preg_replace('/^.*<\!--TemplateTransformer start-->/s', '', $html); |
---|
| 336 | $html = preg_replace('/<\!--TemplateTransformer end-->.*$/s', '', $html); |
---|
[21807] | 337 | $html = preg_replace( |
---|
| 338 | '/<\!--TemplateTransformerSnip start-->.*?<\!--TemplateTransformerSnip end-->/s', |
---|
| 339 | '', |
---|
| 340 | $html |
---|
| 341 | ); |
---|
[21775] | 342 | $html = $this->header_source.$html.$this->footer_source; |
---|
| 343 | $html = str_replace($this->arrSmartyTagsSub, $this->arrSmartyTagsOrg, $html); |
---|
[21595] | 344 | return $html; |
---|
| 345 | } else { |
---|
| 346 | return $this->html_source; |
---|
| 347 | } |
---|
| 348 | } |
---|
| 349 | |
---|
| 350 | |
---|
| 351 | |
---|
| 352 | |
---|
| 353 | /** |
---|
| 354 | * DOMの処理の邪魔になるSmartyのタグを代理文字に置換する preg_replace_callback のコールバック関数 |
---|
| 355 | * |
---|
| 356 | * コメント形式への置換 |
---|
| 357 | * |
---|
| 358 | * @param array $arrMatches マッチしたタグの情報 |
---|
| 359 | * @return string 代わりの文字列 |
---|
| 360 | */ |
---|
| 361 | protected function lfCaptureSmartyTags2Comment(array $arrMatches) { |
---|
| 362 | $substitute_tag = sprintf('<!--###%08d###-->', $this->smarty_tags_idx); |
---|
| 363 | $this->arrSmartyTagsOrg[$this->smarty_tags_idx] = $arrMatches[0]; |
---|
| 364 | $this->arrSmartyTagsSub[$this->smarty_tags_idx] = $substitute_tag; |
---|
| 365 | $this->smarty_tags_idx++; |
---|
| 366 | return $substitute_tag; |
---|
| 367 | } |
---|
| 368 | |
---|
| 369 | |
---|
| 370 | /** |
---|
| 371 | * DOMの処理の邪魔になるSmartyのタグを代理文字に置換する preg_replace_callback のコールバック関数 |
---|
| 372 | * |
---|
[21807] | 373 | * コメント形式への置換 |
---|
| 374 | * |
---|
| 375 | * @param array $arrMatches マッチしたタグの情報 |
---|
| 376 | * @return string 代わりの文字列 |
---|
| 377 | */ |
---|
| 378 | protected function lfCaptureHeadTags2Comment(array $arrMatches) { |
---|
| 379 | $substitute_tag = sprintf('<!--###%08d###-->', $this->smarty_tags_idx); |
---|
| 380 | $this->arrSmartyTagsOrg[$this->smarty_tags_idx] = $arrMatches[2]; |
---|
| 381 | $this->arrSmartyTagsSub[$this->smarty_tags_idx] = $substitute_tag; |
---|
| 382 | $this->smarty_tags_idx++; |
---|
| 383 | |
---|
| 384 | // 文字化け防止用のMETAを入れておく |
---|
| 385 | $content_type_tag = '<!--TemplateTransformerSnip start-->'; |
---|
| 386 | $content_type_tag .= '<meta http-equiv="content-type" content="text/html; charset=UTF-8" />'; |
---|
| 387 | $content_type_tag .= '<!--TemplateTransformerSnip end-->'; |
---|
| 388 | |
---|
| 389 | return $arrMatches[1].$content_type_tag.$substitute_tag.$arrMatches[3]; |
---|
| 390 | } |
---|
| 391 | |
---|
| 392 | |
---|
| 393 | /** |
---|
| 394 | * DOMの処理の邪魔になるSmartyのタグを代理文字に置換する preg_replace_callback のコールバック関数 |
---|
| 395 | * |
---|
[21595] | 396 | * HTMLエレメント内部の処理 |
---|
| 397 | * |
---|
| 398 | * @param array $arrMatches マッチしたタグの情報 |
---|
| 399 | * @return string 代わりの文字列 |
---|
| 400 | */ |
---|
| 401 | protected function lfCaptureSmartyTagsInTag(array $arrMatches) { |
---|
| 402 | // Smartyタグ内のクォートを処理しやすいよう、いったんダミーのタグに |
---|
| 403 | $html = preg_replace_callback('/<\!--{.+?\}-->/s', array($this, 'lfCaptureSmartyTags2Temptag'), $arrMatches[0]); |
---|
| 404 | $html = preg_replace_callback('/\"[^"]*?\"/s', array($this, 'lfCaptureSmartyTagsInQuote'), $html); |
---|
| 405 | $html = preg_replace_callback('/###TEMP(\d{8})###/s', array($this, 'lfCaptureSmartyTags2Attr'), $html); |
---|
| 406 | return $html; |
---|
| 407 | } |
---|
| 408 | |
---|
| 409 | |
---|
| 410 | /** |
---|
| 411 | * DOMの処理の邪魔になるSmartyのタグを代理文字に置換する preg_replace_callback のコールバック関数 |
---|
| 412 | * |
---|
| 413 | * ダミーへの置換実行 |
---|
| 414 | * |
---|
| 415 | * @param array $arrMatches マッチしたタグの情報 |
---|
| 416 | * @return string 代わりの文字列 |
---|
| 417 | */ |
---|
| 418 | protected function lfCaptureSmartyTags2Temptag(array $arrMatches) { |
---|
| 419 | $substitute_tag = sprintf('###TEMP%08d###', $this->smarty_tags_idx); |
---|
| 420 | $this->arrSmartyTagsOrg[$this->smarty_tags_idx] = $arrMatches[0]; |
---|
| 421 | $this->arrSmartyTagsSub[$this->smarty_tags_idx] = $substitute_tag; |
---|
| 422 | $this->smarty_tags_idx++; |
---|
| 423 | return $substitute_tag; |
---|
| 424 | } |
---|
| 425 | |
---|
| 426 | |
---|
| 427 | /** |
---|
| 428 | * DOMの処理の邪魔になるSmartyのタグを代理文字に置換する preg_replace_callback のコールバック関数 |
---|
| 429 | * |
---|
| 430 | * クォート内(=属性値)内にあるSmartyタグ(ダミーに置換済み)を、テキストに置換 |
---|
| 431 | * |
---|
| 432 | * @param array $arrMatches マッチしたタグの情報 |
---|
| 433 | * @return string 代わりの文字列 |
---|
| 434 | */ |
---|
| 435 | protected function lfCaptureSmartyTagsInQuote(array $arrMatches) { |
---|
[21807] | 436 | $html = preg_replace_callback( |
---|
| 437 | '/###TEMP(\d{8})###/s', |
---|
| 438 | array($this, 'lfCaptureSmartyTags2Value'), |
---|
| 439 | $arrMatches[0] |
---|
| 440 | ); |
---|
[21595] | 441 | return $html; |
---|
| 442 | } |
---|
| 443 | |
---|
| 444 | |
---|
| 445 | /** |
---|
| 446 | * DOMの処理の邪魔になるSmartyのタグを代理文字に置換する preg_replace_callback のコールバック関数 |
---|
| 447 | * |
---|
| 448 | * テキストへの置換実行 |
---|
| 449 | * |
---|
| 450 | * @param array $arrMatches マッチしたタグの情報 |
---|
| 451 | * @return string 代わりの文字列 |
---|
| 452 | */ |
---|
| 453 | protected function lfCaptureSmartyTags2Value(array $arrMatches) { |
---|
| 454 | $tag_idx = (int)$arrMatches[1]; |
---|
| 455 | $substitute_tag = sprintf('###%08d###', $tag_idx); |
---|
| 456 | $this->arrSmartyTagsSub[$tag_idx] = $substitute_tag; |
---|
| 457 | return $substitute_tag; |
---|
| 458 | } |
---|
| 459 | |
---|
| 460 | |
---|
| 461 | /** |
---|
| 462 | * DOMの処理の邪魔になるSmartyのタグを代理文字に置換する preg_replace_callback のコールバック関数 |
---|
| 463 | * |
---|
| 464 | * エレメント内部にあって、属性値ではないものを、ダミーの属性として置換 |
---|
| 465 | * |
---|
| 466 | * @param array $arrMatches マッチしたタグの情報 |
---|
| 467 | * @return string 代わりの文字列 |
---|
| 468 | */ |
---|
| 469 | protected function lfCaptureSmartyTags2Attr(array $arrMatches) { |
---|
| 470 | $tag_idx = (int)$arrMatches[1]; |
---|
| 471 | $substitute_tag = sprintf('rel%08d="######"', $tag_idx); |
---|
| 472 | $this->arrSmartyTagsSub[$tag_idx] = $substitute_tag; |
---|
| 473 | return ' '.$substitute_tag.' '; // 属性はパース時にスペースが詰まるので、こちらにはスペースを入れておく |
---|
| 474 | } |
---|
| 475 | |
---|
| 476 | |
---|
| 477 | /** |
---|
| 478 | * DOM Element / Document を走査し、name、class別に分類する |
---|
| 479 | * |
---|
| 480 | * @param DOMNode $objDOMElement DOMNodeオブジェクト |
---|
| 481 | * @return void |
---|
| 482 | */ |
---|
| 483 | protected function lfScanChild(DOMNode $objDOMElement, $parent_selector = '') { |
---|
| 484 | $objNodeList = $objDOMElement->childNodes; |
---|
| 485 | if (is_null($objNodeList)) return; |
---|
| 486 | |
---|
| 487 | foreach ($objNodeList as $element) { |
---|
| 488 | // DOMElementのみ取り出す |
---|
| 489 | if ($element instanceof DOMElement) { |
---|
| 490 | $arrAttr = array(); |
---|
| 491 | $arrAttr[] = $element->tagName; |
---|
| 492 | if (method_exists($element, 'getAttribute')) { |
---|
| 493 | // idを持っていればidを付加する |
---|
| 494 | if ($element->hasAttribute('id')) |
---|
| 495 | $arrAttr[] = '#'.$element->getAttribute('id'); |
---|
| 496 | // classを持っていればclassを付加する(複数の場合は複数付加する) |
---|
| 497 | if ($element->hasAttribute('class')) { |
---|
| 498 | $arrClasses = preg_split('/\s+/', $element->getAttribute('class')); |
---|
| 499 | foreach ($arrClasses as $classname) $arrAttr[] = '.'.$classname; |
---|
| 500 | } |
---|
| 501 | } |
---|
| 502 | // 親要素のセレクタを付けてツリーへ登録する |
---|
| 503 | $this_selector = $parent_selector.' '.implode('', $arrAttr); |
---|
| 504 | $this->arrElementTree[] = array($this_selector, $element); |
---|
| 505 | // エレメントが子孫要素を持っていればさらに調べる |
---|
| 506 | if ($element->hasChildNodes()) $this->lfScanChild($element, $this_selector); |
---|
| 507 | } |
---|
| 508 | } |
---|
| 509 | } |
---|
| 510 | |
---|
| 511 | |
---|
| 512 | /** |
---|
| 513 | * セレクタ文字列をツリー検索用の正規表現に変換する |
---|
| 514 | * |
---|
| 515 | * @param string $selector セレクタ |
---|
| 516 | * @param string $parent_index セレクタ検索時の親要素の位置(子孫要素検索のため) |
---|
| 517 | * @return string 正規表現文字列 |
---|
| 518 | */ |
---|
| 519 | protected function lfSelector2Regex($selector, $parent_index = NULL){ |
---|
| 520 | // jQueryライクなセレクタを正規表現に |
---|
| 521 | $selector = preg_replace('/ *> */', ' >', $selector); // 子セレクタをツリー検索用に 「A >B」の記法にする |
---|
| 522 | $regex = '/'; |
---|
| 523 | if (!is_null($parent_index)) $regex .= preg_quote($this->arrElementTree[$parent_index][0], '/'); // (親要素の指定(絞り込み時)があれば頭に付加する(特殊文字はエスケープ) |
---|
| 524 | $arrSelectors = explode(' ', $selector); |
---|
| 525 | foreach ($arrSelectors as $sub_selector) { |
---|
| 526 | if (preg_match('/^(>?)([\w\-]+)?(#[\w\-]+)?(\.[\w\-]+)*$/', $sub_selector, $arrMatch)) { |
---|
| 527 | // 子セレクタ |
---|
| 528 | if (isset($arrMatch[1]) && $arrMatch[1]) $regex .= ' '; |
---|
| 529 | else $regex .= '.* '; |
---|
| 530 | // タグ名 |
---|
| 531 | if (isset($arrMatch[2]) && $arrMatch[2]) $regex .= preg_quote($arrMatch[2], '/'); |
---|
| 532 | else $regex .= '([\w\-]+)?'; |
---|
| 533 | // id |
---|
| 534 | if (isset($arrMatch[3]) && $arrMatch[3]) $regex .= preg_quote($arrMatch[3], '/'); |
---|
| 535 | else $regex .= '(#(\w|\-|#{3}[0-9]{8}#{3})+)?'; |
---|
| 536 | // class |
---|
| 537 | if (isset($arrMatch[4]) && $arrMatch[4]) $regex .= '(\.(\w|\-|#{3}[0-9]{8}#{3})+)*'.preg_quote($arrMatch[4], '/').'(\.(\w|\-|#{3}[0-9]{8}#{3})+)*'; // class指定の時は前後にもclassが付いているかもしれない |
---|
| 538 | else $regex .= '(\.(\w|\-|#{3}[0-9]{8}#{3})+)*'; |
---|
| 539 | } |
---|
| 540 | } |
---|
| 541 | $regex .= '$/i'; |
---|
| 542 | |
---|
| 543 | return $regex; |
---|
| 544 | } |
---|
| 545 | |
---|
| 546 | |
---|
| 547 | /** |
---|
| 548 | * 見つかった要素をプロパティに登録 |
---|
| 549 | * |
---|
| 550 | * @param integer $elementNo エレメントのインデックス |
---|
| 551 | * @param array $arrElement インデックスとDOMオブジェクトをペアとした配列 |
---|
| 552 | * @return void |
---|
| 553 | */ |
---|
| 554 | protected function lfAddElement($elementNo, array &$arrElement) { |
---|
| 555 | if (!array_key_exists($arrElement[0], $this->arrSelectElements[$this->search_depth])) { |
---|
| 556 | $this->arrSelectElements[$this->search_depth][$arrElement[0]] = array($elementNo, &$arrElement[1]); |
---|
| 557 | } |
---|
| 558 | } |
---|
| 559 | |
---|
| 560 | |
---|
| 561 | /** |
---|
| 562 | * DOMを用いた変形を実行する |
---|
| 563 | * |
---|
| 564 | * @param string $mode 実行するメソッドの種類 |
---|
| 565 | * @param string $target_key 対象のエレメントの完全なセレクタ |
---|
| 566 | * @param string $html_snip HTMLコード |
---|
| 567 | * @return boolean |
---|
| 568 | */ |
---|
| 569 | protected function lfSetTransform($mode, $target_key, $html_snip) { |
---|
| 570 | |
---|
| 571 | $substitute_tag = sprintf('<!--###%08d###-->', $this->smarty_tags_idx); |
---|
| 572 | $this->arrSmartyTagsOrg[$this->smarty_tags_idx] = $html_snip; |
---|
| 573 | $this->arrSmartyTagsSub[$this->smarty_tags_idx] = $substitute_tag; |
---|
| 574 | $this->smarty_tags_idx++; |
---|
| 575 | |
---|
| 576 | $this->objDOM->createDocumentFragment(); |
---|
| 577 | $objSnip = $this->objDOM->createDocumentFragment(); |
---|
| 578 | $objSnip->appendXML($substitute_tag); |
---|
| 579 | |
---|
| 580 | $objElement = false; |
---|
[21596] | 581 | if (isset($this->arrElementTree[$target_key]) && $this->arrElementTree[$target_key][0]) { |
---|
[21595] | 582 | $objElement = &$this->arrElementTree[$target_key][1]; |
---|
| 583 | } |
---|
| 584 | |
---|
| 585 | if (!$objElement) return false; |
---|
| 586 | |
---|
| 587 | try { |
---|
[21596] | 588 | switch ($mode) { |
---|
[21595] | 589 | case 'appendFirst': |
---|
| 590 | if ($objElement->hasChildNodes()) { |
---|
| 591 | $objElement->insertBefore($objSnip, $objElement->firstChild); |
---|
| 592 | } else { |
---|
| 593 | $objElement->appendChild($objSnip); |
---|
| 594 | } |
---|
| 595 | break; |
---|
| 596 | case 'appendChild': |
---|
| 597 | $objElement->appendChild($objSnip); |
---|
| 598 | break; |
---|
| 599 | case 'insertBefore': |
---|
| 600 | if (!is_object($objElement->parentNode)) return false; |
---|
| 601 | $objElement->parentNode->insertBefore($objSnip, $objElement); |
---|
| 602 | break; |
---|
| 603 | case 'insertAfter': |
---|
| 604 | if ($objElement->nextSibling) { |
---|
| 605 | $objElement->parentNode->insertBefore($objSnip, $objElement->nextSibling); |
---|
[21596] | 606 | } else { |
---|
[21595] | 607 | $objElement->parentNode->appendChild($objSnip); |
---|
| 608 | } |
---|
| 609 | break; |
---|
| 610 | case 'replaceElement': |
---|
| 611 | if (!is_object($objElement->parentNode)) return false; |
---|
| 612 | $objElement->parentNode->replaceChild($objSnip, $objElement); |
---|
| 613 | break; |
---|
| 614 | default: |
---|
| 615 | break; |
---|
| 616 | } |
---|
| 617 | $this->snip_count++; |
---|
| 618 | } |
---|
| 619 | catch (Exception $e) { |
---|
| 620 | SC_Utils_Ex::sfDispSiteError(FREE_ERROR_MSG, '', true, 'テンプレートの操作に失敗しました。'); |
---|
| 621 | } |
---|
| 622 | |
---|
| 623 | return true; |
---|
| 624 | } |
---|
| 625 | |
---|
| 626 | |
---|
| 627 | /** |
---|
| 628 | * セレクタエラーを記録する |
---|
| 629 | * |
---|
| 630 | * @param string $selector セレクタ |
---|
| 631 | * @param integer $type エラーの種類 |
---|
| 632 | * @param string $err_msg エラーメッセージ |
---|
| 633 | * @return void |
---|
| 634 | */ |
---|
| 635 | protected function lfSetError($selector, $type, $err_msg = NULL) { |
---|
| 636 | $this->arrErr[] = array( |
---|
| 637 | 'selector' => $selector, |
---|
| 638 | 'type' => $type, |
---|
| 639 | 'err_msg' => $err_msg |
---|
| 640 | ); |
---|
| 641 | } |
---|
| 642 | } |
---|