Exemple #1
0
    function keitaiFilter($s)
    {
        if ($s === '') {
            return false;
        }
        $head = $header = $body = $footer = $pagetitle = '';
        $header_template = $body_template = $footer_template = '';
        $encode = '';
        $rebuilds = $this->k_tai_conf['rebuilds'];
        if (isset($rebuilds['redirectMessage'])) {
            // check "redirectMessage" at last.
            $_redirectMessage = $rebuilds['redirectMessage'];
            unset($rebuilds['redirectMessage']);
            $rebuilds['redirectMessage'] = $_redirectMessage;
        }
        // テンプレート読み込み
        if ($rebuilds && $this->k_tai_conf['template']) {
            $templates_dir = dirname(dirname(__FILE__)) . '/ktairender/templates/' . $this->k_tai_conf['template'] . '/';
            foreach (array('header', 'body', 'footer') as $_name) {
                if (is_file($templates_dir . $_name . '.html')) {
                    $var_name = $_name . '_template';
                    ${$var_name} = file_get_contents($templates_dir . $_name . '.html');
                }
            }
        }
        $r =& $this->HypKTaiRender;
        // use jquery mobile?
        $use_jquery = $r->Config_jquery;
        $is_rss = false;
        // Is RSS?
        if (preg_match('/<(?:feed.+?<entry|(?:rss|rdf).+?<channel)/isS', substr($s, 0, 1000))) {
            HypCommonFunc::loadClass('HypRss2Html');
            $rh = new HypRss2Html($s);
            $rh->detect_order = $this->detect_order;
            $s = $rh->getHtml();
            //$s = mb_convert_encoding($s, $this->encode, $r->encoding);
            $encode = $rh->encoding;
            $header = '<h1>RSS of ' . $rh->base['TITLE'] . '</h1><a href="' . $rh->base['LINK'] . '" data-icon="home" data-iconpos="notext">Home</a>';
            $s = str_replace('<head>', '<head><link href="' . $_SERVER['REQUEST_URI'] . '" title="RSS of ' . $rh->base['TITLE'] . '" type="application/rss+xml" rel="alternate" />', $s);
            $is_rss = true;
            //$use_jquery = false;
        }
        // preg_match では、サイズが大きいページで正常処理できないことがあるので。
        $s = str_replace(array('</BODY>', '</HEAD>', '<BODY', '<HEAD'), array('</body>', '</head>', '<body', '<head'), $s);
        $arr1 = explode('<head', $s, 2);
        if (isset($arr1[1]) && strpos($arr1[1], '</head>') !== FALSE) {
            $arr2 = explode('</head>', $arr1[1], 2);
            $head = substr($arr2[0], strpos($arr2[0], '>') + 1);
        }
        $arr1 = explode('<body', $s, 2);
        if (isset($arr1[1]) && strpos($arr1[1], '</body>') !== FALSE) {
            $arr2 = explode('</body>', $arr1[1], 2);
            $body = substr($arr2[0], strpos($arr2[0], '>') + 1);
        }
        if ($head && !$encode) {
            $encode = HypCommonFunc::get_encoding_by_meta($head, TRUE);
        }
        if (!$encode) {
            $encode = $this->encode;
        }
        if ($body) {
            // 最適化済みパートをエスケープ
            while (strpos($body, '<!--HypKTaiOptimized-->') !== FALSE) {
                $arr1 = explode('<!--HypKTaiOptimized-->', $body, 2);
                $arr2 = array_pad(explode('<!--/HypKTaiOptimized-->', $arr1[1], 2), 2, '');
                $body = $arr1[0] . str_replace(array('<', '>'), array("", ""), $arr2[0]) . $arr2[1];
            }
            // 携帯のみ有効にする部分
            $body = str_replace('<!--HypKTaiOnly', '', $body);
            $body = str_replace('HypKTaiOnly-->', '', $body);
            // 無視する部分(<!--HypKTaiIgnore-->...<!--/HypKTaiIgnore-->)を削除
            while (strpos($body, '<!--HypKTaiIgnore-->') !== FALSE) {
                $arr1 = explode('<!--HypKTaiIgnore-->', $body, 2);
                $arr2 = array_pad(explode('<!--/HypKTaiIgnore-->', $arr1[1], 2), 2, '');
                $body = $arr1[0] . $arr2[1];
            }
            // Block を処理
            $bid = isset($_GET[$this->k_tai_conf['getKeys']['block']]) ? intval($_GET[$this->k_tai_conf['getKeys']['block']]) : 0;
            $_showblocks = $showblocks = $blocks = $submenu = $blockmenu = array();
            $base = '?';
            $querys = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : '';
            if ($querys) {
                $base .= str_replace('&', '&amp;', $querys);
            }
            while (strpos($body, '<!--KTaiBlock_') !== FALSE) {
                $arr1 = explode('<!--KTaiBlock_', $body, 2);
                $arr2 = array_pad(explode('<!--/KTaiBlock-->', $arr1[1], 2), 2, '');
                list($id, $bcontent) = explode('-->', $arr2[0], 2);
                $title = preg_replace('#^.*?<!--KTaiTitle-->(.+?)<!--/KTaiTitle-->.*?$#s', '$1', $bcontent);
                $no_title = false;
                if ($title === $bcontent || !$title) {
                    $title = 'Block No.' . $id;
                    $no_title = true;
                }
                if ($use_jquery) {
                    $bcontent = preg_replace('#<h[1-6].*?<!--KTaiTitle-->(.+?)<!--/KTaiTitle-->.*?/h[1-6]>#s', '', $bcontent);
                    if ($no_title) {
                        $body = $arr1[0] . '<div id="ktaiblock' . $id . '">' . $bcontent . '</div>' . $arr2[1];
                    } else {
                        if (in_array($id, $this->k_tai_conf['showBlockIds'])) {
                            $body = $arr1[0] . '<div id="ktaiblock' . $id . '" data-role="collapsible" data-theme="' . $this->k_tai_conf['jquery_theme_block'] . '" data-content-theme="' . $this->k_tai_conf['jquery_theme_block'] . '" data-collapsed="false"><h3>' . $title . '</h3>' . $bcontent . '</div>' . $arr2[1];
                        } else {
                            $body = $arr1[0] . '<div id="ktaiblock' . $id . '" data-role="collapsible" data-theme="' . $this->k_tai_conf['jquery_theme_block'] . '" data-content-theme="' . $this->k_tai_conf['jquery_theme_block'] . '" data-collapsed="true"><h3>' . $title . '</h3>' . $bcontent . '</div>' . $arr2[1];
                        }
                        $blockmenu[$id] = '<a href="#ktaiblock' . $id . '" data-ajax="false">' . $title . '</a>';
                    }
                } else {
                    if ($no_title || in_array($id, $this->k_tai_conf['showBlockIds'])) {
                        $body = $arr1[0] . '<div id="ktaiblock' . $id . '">' . $bcontent . '</div>' . $arr2[1];
                        if (!$no_title) {
                            $showblocks['ktaiblock' . $id] = $title;
                        }
                    } else {
                        $blocks[$id]['content'] = $bcontent;
                        if ($bid != $id) {
                            $submenu[$id] = '<a href="' . $base . '&amp;' . $this->k_tai_conf['getKeys']['block'] . '=' . $id . '">' . $title . '</a>';
                        } else {
                            $submenu[$id] = '<span style="' . $this->k_tai_conf['style']['highlight'] . '">' . $title . '</span>';
                        }
                        $body = $arr1[0] . $arr2[1];
                    }
                }
            }
            if (!empty($_showblocks)) {
                $showblocks += $_showblocks;
            }
            if ($submenu) {
                $body .= '<!--subMenu--><ul>';
                foreach ($submenu as $sub) {
                    $body .= '<li>' . $sub . '</li>';
                }
                $body .= '</ul><!--/subMenu-->';
                $showblocks['submenu'] = $this->k_tai_conf['msg']['subMenu'];
            }
            if ($showblocks) {
                $block_menu = array();
                foreach ($showblocks as $id => $title) {
                    $block_menu[] = '<a href="#' . $id . '">' . $title . '</a>';
                }
                $body .= '<!--blockMenu-->' . join(' / ', $block_menu) . '<!--/blockMenu-->';
            }
            if ($blockmenu) {
                $_url = XOOPS_URL . '/';
                $blockmenu = join('</li><li>', $blockmenu);
                if (is_file(XOOPS_THEME_PATH . '/' . $this->k_tai_conf['themeSet'] . '/menubar_jqm.html')) {
                    $body .= str_replace(array('<_JQUERY_THEME_>', '<_BLOCK_MENU_>', '<_TOP_URL_>'), array($this->k_tai_conf['jquery_theme'], $blockmenu, XOOPS_URL . '/'), file_get_contents(XOOPS_THEME_PATH . '/' . $this->k_tai_conf['themeSet'] . '/menubar_jqm.html'));
                } else {
                    if (isset($rebuilds['blockMenuBar'])) {
                        $body .= <<<EOD
<!--blockMenu-->
<div id="keitaiblockmenu" data-role="header" data-theme="{$this->k_tai_conf['jquery_theme']}">
 <div data-role="navbar">
  <ul><li>{$blockmenu}</li></ul>
 </div>
</div>
<!--/blockMenu-->
<!--blockMenuBar-->
<div data-role="header" data-theme="{$this->k_tai_conf['jquery_theme']}">
 <a href="{$_url}" data-ajax="false" data-icon="home" data-iconpos="notext">Home</a>
 <h4>
  <a id="keitaifixedbar_main" href="#keitaiMainContents" data-ajax="false" style="display:inline;text-decoration:none;"><pagetitle></a>
 </h4>
 <a id="keitaifixedbar_block" href="#" data-ajax="false" data-icon="grid" data-iconpos="notext">block</a>
</div>
<!--/blockMenuBar-->
EOD;
                    } else {
                        // old way
                        $body .= <<<EOD
<!--blockMenu-->
<div id="keitaiblockmenu" data-role="header">
 <div data-role="navbar">
  <ul><li>{$blockmenu}</li></ul>
 </div>
</div>
<div data-role="header">
 <a href="{$_url}" data-ajax="false" data-icon="home" data-iconpos="notext">Home</a>
 <h4>
  <a id="keitaifixedbar_main" href="#keitaiMainContents" data-ajax="false" style="display:inline;text-decoration:none;"><pagetitle></a>
 </h4>
 <a id="keitaifixedbar_block" href="#" data-ajax="false" data-icon="grid" data-iconpos="notext">block</a>
</div>
<!--/blockMenu-->
EOD;
                    }
                }
            }
            if ($rebuilds) {
                $parts = array();
                $rebuild_found = FALSE;
                if (!empty($_SESSION['hyp_redirect_message'])) {
                    $body = '<!--redirectMessage-->' . $_SESSION['hyp_redirect_message'] . '<!--/redirectMessage-->' . $body;
                    unset($_SESSION['hyp_redirect_message']);
                }
                if (isset($_GET[$this->k_tai_conf['getKeys']['block']]) && isset($blocks[$_GET[$this->k_tai_conf['getKeys']['block']]])) {
                    $body .= '<!--toMain-->' . $this->k_tai_conf['icon']['toMain'] . '<a href="' . $base . '">' . $this->k_tai_conf['msg']['toMain'] . '</a><!--/toMain-->';
                    $body .= '<!--blockContent--><ns>' . $blocks[$_GET[$this->k_tai_conf['getKeys']['block']]]['content'] . '</ns><!--/blockContent-->';
                }
                foreach ($rebuilds as $id => $var) {
                    $qid = preg_quote($id, '#');
                    $parts[$id] = '';
                    // preg_match では、サイズが大きいページで正常処理できないことがあるので。
                    $arr1 = explode('<!--' . $id . '-->', $body, 2);
                    if (isset($arr1[1]) && strpos($arr1[1], '<!--/' . $id . '-->') !== FALSE) {
                        $arr2 = explode('<!--/' . $id . '-->', $arr1[1], 2);
                        $target = $arr2[0];
                        //$target = trim(preg_replace('/<!--.+?-->/sS', '', $target));
                        if (!$use_jquery) {
                            $target = trim(preg_replace('/<!--.+?-->/sS', '', $target));
                        }
                        if (trim(preg_replace('/<\\/?(?:div|span|ns|p)[^>]*?>/S', '', $target))) {
                            $parts[$id] = $var['above'] . $target . $var['below'];
                            if ($id !== 'redirectMessage') {
                                $rebuild_found = TRUE;
                            } else {
                                if ($rebuild_found) {
                                    $target = strip_tags($target);
                                }
                            }
                            $parts[$id] = $var['above'] . $target . $var['below'];
                        }
                    }
                }
                if ($rebuild_found) {
                    if (isset($_GET[$this->k_tai_conf['getKeys']['block']]) && isset($blocks[$_GET[$this->k_tai_conf['getKeys']['block']]])) {
                        if (empty($parts['content'])) {
                            $parts['toMain'] = '';
                        }
                        //$parts['content'] = $blocks[$_GET[$this->k_tai_conf['getKeys']['block']]]['content'];
                        $parts['content'] = '';
                    }
                    if ($use_jquery && !empty($parts['content'])) {
                        $parts['content'] = '<div id="keitaiMainContents" data-role="collapsible" data-theme="' . $this->k_tai_conf['jquery_theme_content'] . '" data-content-theme="' . $this->k_tai_conf['jquery_theme_content'] . '" data-collapsed="false">' . $parts['content'] . '</div>';
                    }
                    // Easy login & to PC
                    $easylogin = '';
                    $to_pc = '<li><a href="#" onclick="return jQuery.keitaiSwitchToPc();">PC</a></li>';
                    if (!empty($this->k_tai_conf['easyLogin'])) {
                        if (!empty($r->vars['ua']['isGuest'])) {
                            $add = '_EASYLOGIN';
                            if ($r->vars['ua']['carrier'] === 'docomo') {
                                $add .= '&guid=on';
                            }
                            //$url = $r->myRoot . $r->removeSID($r->SERVER['REQUEST_URI']);
                            $url = $r->myRoot . $r->removeQueryFromUrl($r->SERVER['REQUEST_URI'], array('guid', $r->session_name));
                            $url .= (strpos($url, '?') === FALSE ? '?' : '&') . $add;
                            $url = str_replace('&', '&amp;', $url);
                            if ($use_jquery) {
                                $easylogin = '******' . $url . '">' . $this->k_tai_conf['msg']['easylogin'] . '</a></li>' . $to_pc . '</ul>';
                            } else {
                                $easylogin = '******' . $url . '">' . $this->k_tai_conf['msg']['easylogin'] . '</a>';
                            }
                        } else {
                            $uname = '';
                            if (empty($_SESSION['hyp_redirect_uname'])) {
                                $member_handler =& xoops_gethandler('member');
                                $xoopsUser =& $member_handler->getUser($this->HypKTaiRender->vars['ua']['xoopsUid']);
                                $uname = $xoopsUser->getVar('uname');
                            } else {
                                $uname = $_SESSION['hyp_redirect_uname'];
                                unset($_SESSION['hyp_redirect_uname']);
                            }
                            if ($uname) {
                                $uname = htmlspecialchars($uname, ENT_COMPAT, _CHARSET);
                                $guid = $r->vars['ua']['carrier'] === 'docomo' ? '&amp;guid=on' : '';
                                $uname = '<a href="' . XOOPS_URL . '/userinfo.php?uid=' . $this->HypKTaiRender->vars['ua']['xoopsUid'] . $guid . '">' . $uname . '</a>';
                            }
                            if ($use_jquery) {
                                $easylogin = '******' . $uname . '</li><li><a href="' . XOOPS_URL . '/user.php?op=logout">' . $this->k_tai_conf['msg']['logout'] . '</a></li>' . $to_pc . '</ul>';
                            } else {
                                $easylogin = $uname . ' <a href="' . XOOPS_URL . '/user.php?op=logout">' . $this->k_tai_conf['msg']['logout'] . '</a>';
                            }
                            // 簡単ログイン:設定 or 解除
                            if (isset($this->k_tai_conf['easyLoginConfPath']) && isset($this->k_tai_conf['easyLoginConfuid'])) {
                                $purl = parse_url(XOOPS_URL);
                                $nowpath = $r->SERVER['PHP_SELF'];
                                if (isset($purl['path'])) {
                                    $nowpath = preg_replace('#^' . $purl['path'] . '#', '', $nowpath);
                                }
                                if (strpos($nowpath, $this->k_tai_conf['easyLoginConfPath']) === 0 && $this->HypKTaiRender->vars['ua']['xoopsUid'] == @$_GET[$this->k_tai_conf['easyLoginConfuid']]) {
                                    $uaUid = md5($r->vars['ua']['uid'] . XOOPS_DB_PASS);
                                    // Read easy login data file
                                    $myroot = str_replace('/', '_', preg_replace('#https?://#i', '', XOOPS_URL));
                                    $datfile = XOOPS_TRUST_PATH . '/uploads/hyp_common/' . $myroot . '_easylogin.dat';
                                    if (is_file($datfile)) {
                                        $uids = unserialize(HypCommonFunc::flock_get_contents($datfile));
                                    } else {
                                        $uids = array();
                                    }
                                    if (isset($uids[$uaUid])) {
                                        $add = '_EASYLOGINUNSET';
                                        $msg = 'easyloginUnset';
                                    } else {
                                        $add = '_EASYLOGINSET';
                                        $msg = 'easyloginSet';
                                    }
                                    if ($r->vars['ua']['carrier'] === 'docomo') {
                                        $add .= '&guid=on';
                                    }
                                    $url = $r->myRoot . $r->removeQueryFromUrl($r->SERVER['REQUEST_URI'], array('guid', '_EASYLOGINUNSET', '_EASYLOGINSET'));
                                    $url .= (strpos($url, '?') === FALSE ? '?' : '&') . $add;
                                    $url = str_replace('&', '&amp;', $url);
                                    $parts[$this->k_tai_conf['easyLoginConfInsert']] = '<hr /><div style="text-align:center">[<a href="' . $url . '">' . $this->k_tai_conf['msg'][$msg] . '</a>]</div>' . @$parts[$this->k_tai_conf['easyLoginConfInsert']];
                                }
                            }
                        }
                    } else {
                        if (!empty($r->vars['ua']['isGuest'])) {
                            if ($use_jquery) {
                                $easylogin = '******' . $to_pc . '</ul>';
                            }
                        } else {
                            if ($use_jquery) {
                                $easylogin = '******' . $uname . '</li><li><a href="' . XOOPS_URL . '/user.php?op=logout">' . $this->k_tai_conf['msg']['logout'] . '</a></li>' . $to_pc . '</ul>';
                            } else {
                                $easylogin = $uname . ' <a href="' . XOOPS_URL . '/user.php?op=logout">' . $this->k_tai_conf['msg']['logout'] . '</a>';
                            }
                        }
                    }
                    if ($easylogin) {
                        $parts['easylogin'] = $rebuilds['easylogin']['above'] . $easylogin . $rebuilds['easylogin']['below'];
                    }
                    foreach (array_keys($rebuilds) as $id) {
                        $header_template = str_replace('<' . $id . '>', $parts[$id], $header_template);
                        $body_template = str_replace('<' . $id . '>', $parts[$id], $body_template);
                        $footer_template = str_replace('<' . $id . '>', $parts[$id], $footer_template);
                    }
                    if ($header_template) {
                        $header = $header_template;
                    }
                    if ($body_template) {
                        $body = $body_template;
                    }
                    if ($footer_template) {
                        $footer = $footer_template;
                    }
                } elseif ($use_jquery && !$is_rss && strpos($head, '<!--jqm_theme') === false) {
                    return false;
                }
            }
        } else {
            return $s;
        }
        if ($head) {
            // Redirect
            if (preg_match('#<meta[^>]+http-equiv=("|\')Refresh\\1[^>]+content=("|\')[\\d]+;\\s*url=(.+)\\2[^>]*>#iUS', $head, $match)) {
                //$url = str_replace('&amp;', '&', $match[3]);
                $url = strtr(str_replace('&amp;', '&', $match[3]), "\r\n", "   ");
                if ($body) {
                    $body = preg_replace('#<p>.*?<a[^>]*?href="' . preg_quote($match[3], '#') . '.*?</p>#', '', $body);
                    $_SESSION['hyp_redirect_message'] = strip_tags($body);
                }
                $url = $r->getRealUrl($url);
                $url = $r->addSID($url, XOOPS_URL);
                $this->_locationRedirect($url, false);
                return '';
            }
            // <head>
            $_head = '<head>';
            if (preg_match('#<title[^>]*>(.*)</title>#isUS', $head, $match)) {
                $pagetitle = $match[1];
                $_head .= mb_convert_encoding($match[0], $use_jquery ? $encode : 'SJIS-win', $encode);
            }
            if (isset($r->vars['ua']['meta'])) {
                $_head .= $r->vars['ua']['meta'];
            }
            // Check RSS & CSS
            $_css_type = $use_jquery && $this->k_tai_conf['jquery_no_reduce'] ? 'all|screen|handheld' : 'handheld';
            $rss = array();
            $jquery_script = array();
            if ($use_jquery) {
                // remove jquery-ui css
                $head = preg_replace('#<link[^>]+?href=[^>]*?jquery-ui(?:\\.min)?\\.css[^>]*?>\\r?\\n?#i', '', $head);
            }
            if (preg_match_all('#<link([^>]+?)>#iS', $head, $match)) {
                foreach ($match[1] as $key => $attrs) {
                    if (preg_match('#type=("|\')application/(?:atom|rss)\\+xml\\1#iS', $attrs)) {
                        if (preg_match('#href=("|\')([^ <>"\']+)\\1#is', $attrs, $match2)) {
                            $title = 'RSS';
                            $url = $match2[2];
                            if (preg_match('#title=("|\')([^<>"\']+)\\1#isS', $attrs, $match3)) {
                                $title = $match3[2];
                            }
                            if (!$is_rss) {
                                $rss[] = '<a href="' . $url . '" data-ajax="true">' . $title . '</a>';
                            }
                            if ($use_jquery) {
                                $_head .= $match[0][$key];
                            }
                        }
                    } else {
                        if (preg_match('#rel=("|\')stylesheet\\1#iS', $attrs)) {
                            if (strpos($_css_type, 'all') !== false && strpos($attrs, 'media=') === false) {
                                $_head .= '<link' . $attrs . '>';
                            } else {
                                if (preg_match('# media=("|\')[a-z, ]*\\b(?:' . $_css_type . '|' . $r->vars['ua']['carrier'] . ')\\b[a-z, ]*\\1#iS', $attrs)) {
                                    $_head .= '<link' . preg_replace('# media=("|\')[^"\']*?\\1#iS', '', $attrs) . '>';
                                }
                            }
                        }
                    }
                }
            }
            /*
            if (preg_match_all('#<script(.+?)/script>\r?\n?#isS', $head, $match)) {
            	foreach($match[1] as $i => $attrs) {
            		if (preg_match('#jquery#iS', $attrs)) {
            			$jquery_script[] = $match[0][$i];
            			$head = str_replace($match[0][$i], '', $head);
            		}
            	}
            }
            */
            if ($rss) {
                if ($use_jquery && count($rss) > 1) {
                    $rss = '<div data-role="collapsible" data-theme="' . $this->k_tai_conf['jquery_theme_block'] . '" data-content-theme="' . $this->k_tai_conf['jquery_theme_block'] . '" data-collapsed="true"><h4>RSS Links</h4>' . $r->Config_icons['RSS'] . join('<br />' . $r->Config_icons['RSS'], $rss) . '</div>';
                } else {
                    $rss = '<div style="font-size:0.9em">' . $r->Config_icons['RSS'] . join('<br />' . $r->Config_icons['RSS'], $rss) . '</div>';
                }
            } else {
                $rss = '';
            }
            $header = str_replace('<rssLink>', $rss, $header);
            $body = str_replace('<rssLink>', $rss, $body);
            $footer = str_replace('<rssLink>', $rss, $footer);
            if ($use_jquery) {
                // remove empty script tag from <head>
                $head = preg_replace('#<script[^>]*?>\\s*?<!--\\s*?//-->\\s*?</script>\\r?\\n?#is', '', $head);
                // remove jquery tag  from <head>
                $head = preg_replace('#google\\.load\\("jqueryui",\\s*"[^"]+?"\\);\\r?\\n?#', '', $head);
                $head = preg_replace('#<script[^>]+?src=[^>]*?jquery-?ui(?:\\.min)?\\.js[^>]*?></script>\\r?\\n?#i', '', $head);
                $_head .= '<link href="' . XOOPS_THEME_URL . '/' . $this->k_tai_conf['themeSet'] . '/jquery.mobile.min.css" rel="stylesheet" type="text/css" />';
                if ($this->k_tai_conf['jqm_css']) {
                    $_head .= '<link href="' . $this->k_tai_conf['jqm_css'] . '" rel="stylesheet" type="text/css" />';
                }
                if (!$rebuild_found) {
                    $_head .= '<link href="' . XOOPS_THEME_URL . '/' . $this->k_tai_conf['themeSet'] . '/smart.css" rel="stylesheet" type="text/css" />';
                }
                if ($this->k_tai_conf['jquery_no_reduce']) {
                    $_head .= preg_replace('#<link([^>]+?)>\\r?\\n?|<title.+?/title>\\r?\\n?#iS', '', $head);
                }
                $_head .= '<script type="text/javascript" src="' . XOOPS_THEME_URL . '/' . $this->k_tai_conf['themeSet'] . '/jquery.min.js"></script>';
                $_head .= '<script type="text/javascript" src="' . XOOPS_THEME_URL . '/' . $this->k_tai_conf['themeSet'] . '/jquery.mobile-config.js"></script>';
                $_head .= '<script type="text/javascript" src="' . XOOPS_THEME_URL . '/' . $this->k_tai_conf['themeSet'] . '/jquery.mobile.min.js"></script>';
                //$_head .= join('', $jquery_script);
                $_head .= '<script type="text/javascript" src="' . XOOPS_THEME_URL . '/' . $this->k_tai_conf['themeSet'] . '/jquery.extra.js"></script>';
                if (preg_match('/<!--jqm_theme_([a-z])/', $head, $_match)) {
                    $this->k_tai_conf['jquery_theme'] = $_match[1];
                    $this->k_tai_conf['rebuilds']['header']['above'] = preg_replace('/data-theme="[a-z]"/', 'data-theme="' . $this->k_tai_conf['jquery_theme'] . '"', $this->k_tai_conf['rebuilds']['header']['above']);
                    $this->k_tai_conf['rebuilds']['body']['above'] = preg_replace('/data-theme="[a-z]"/', 'data-theme="' . $this->k_tai_conf['jquery_theme'] . '"', $this->k_tai_conf['rebuilds']['body']['above']);
                    $this->k_tai_conf['rebuilds']['footer']['above'] = preg_replace('/data-theme="[a-z]"/', 'data-theme="' . $this->k_tai_conf['jquery_theme'] . '"', $this->k_tai_conf['rebuilds']['footer']['above']);
                }
            }
            $_head .= '</head>';
            $head = $_head;
        }
        // Remove  xoopsCode buttons & Smilies buttons.
        if (strpos($body, '<div id="message_bbcode_buttons_pre"') !== FALSE) {
            $body = preg_replace('#<div id="message_bbcode_buttons_pre".+?/div>#sS', '', $body);
            $body = preg_replace('#<div id="message_bbcode_buttons_post".+?/div>#sS', '', $body);
            $body = preg_replace('#<input type="checkbox" id="message_bbcode_onoff".+?<br />#sS', '', $body);
            $body = preg_replace('#<input type="checkbox" id="d3f_post_advanced_options_onoff".+?>#sS', '', $body);
        }
        if (strpos($body, '<a name=\'moresmiley\'>') !== FALSE) {
            $body = preg_replace('#<a name=\'moresmiley\'>.+?<textarea#sS', '<textarea', $body);
            $body = preg_replace('#(?:<img |<a href="\\#" )onclick=\'xoopsCodeSmilie\\(.+?</a>\\]#sS', '', $body);
        }
        if ($r->vars['ua']['carrier'] === 'docomo') {
            $body = preg_replace('/<form[^>]+?user\\.php[^>]+?>/isS', '$0<input type="hidden" name="guid" value="ON">', $body);
        }
        if ($this->k_tai_conf['googleAnalyticsId']) {
            $header .= $r->googleAnalyticsGetImgTag($this->k_tai_conf['googleAnalyticsId'], $pagetitle);
        }
        $header = $this->k_tai_conf['rebuilds']['header']['above'] . $header . $this->k_tai_conf['rebuilds']['header']['below'];
        $body = $this->k_tai_conf['rebuilds']['body']['above'] . $body . $this->k_tai_conf['rebuilds']['body']['below'];
        $footer = $this->k_tai_conf['rebuilds']['footer']['above'] . $footer . $this->k_tai_conf['rebuilds']['footer']['below'];
        if ($use_jquery) {
            $header .= '<separator>';
            $body .= '<separator>';
            if ($this->k_tai_conf['jquery_no_reduce']) {
                $r->Config_no_diet = true;
            }
        }
        $r->contents['header'] = $header;
        $r->contents['body'] = $body;
        $r->contents['footer'] = $footer;
        $r->inputEncode = $encode;
        $r->outputEncode = $use_jquery ? $encode : 'SJIS';
        $r->outputMode = $use_jquery ? 'html5' : 'xhtml';
        $r->langcode = _LANGCODE;
        $r->doOptimize();
        $charset = strtoupper($r->outputEncode) === 'SJIS' ? 'Shift_JIS' : $encode;
        // Set <body> attribute
        $bodyAttr = $this->k_tai_conf['bodyAttribute'] ? ' ' . trim($this->k_tai_conf['bodyAttribute']) : '';
        if (!empty($r->vars['ua']['bodyAttribute'])) {
            $bodyAttr = ' ' . trim($r->vars['ua']['bodyAttribute']);
        }
        $outBody = $r->outputBody;
        $outBody = str_replace('<pagetitle>', $pagetitle, $outBody);
        if ($use_jquery) {
            $_array = explode('<separator>', $outBody);
            $outBody = '<div data-role="page" data-theme="' . $this->k_tai_conf['jquery_theme'] . '">';
            $outBody .= $_array[0];
            $outBody .= $_array[1];
            $outBody .= $_array[2];
            $outBody .= '</div>';
        }
        $s = $r->getHtmlDeclaration() . $head . '<body' . $bodyAttr . '>' . $outBody . '</body></html>';
        $ctype = $r->getOutputContentType();
        $r = NULL;
        unset($r);
        header('Content-Type: ' . $ctype . '; charset=' . $charset);
        if (!$use_jquery) {
            header('Cache-Control: no-cache');
            if (function_exists('headers_list')) {
                header('Content-Length: ' . strlen($s));
            }
        }
        $this->changeContentLength = true;
        return $s;
    }