Exemplo n.º 1
0
/**
 * nv_rewrite_change()
 *
 * @param mixed $rewrite_optional
 * @return
 */
function nv_rewrite_change($array_config_global)
{
    global $sys_info, $lang_module;
    $rewrite_rule = $filename = '';
    $endurl = $array_config_global['rewrite_endurl'] == $array_config_global['rewrite_exturl'] ? nv_preg_quote($array_config_global['rewrite_endurl']) : nv_preg_quote($array_config_global['rewrite_endurl']) . '|' . nv_preg_quote($array_config_global['rewrite_exturl']);
    if ($sys_info['supports_rewrite'] == 'rewrite_mode_iis') {
        $filename = NV_ROOTDIR . '/web.config';
        $rulename = 0;
        $rewrite_rule .= "\n";
        $rewrite_rule .= " <rule name=\"nv_rule_" . ++$rulename . "\">\n";
        $rewrite_rule .= " <match url=\"^\" ignoreCase=\"false\" />\n";
        $rewrite_rule .= " <conditions>\n";
        $rewrite_rule .= " \t\t<add input=\"{REQUEST_FILENAME}\" pattern=\"/robots.txt\$\" />\n";
        $rewrite_rule .= " </conditions>\n";
        $rewrite_rule .= " <action type=\"Rewrite\" url=\"robots.php?action={HTTP_HOST}\" appendQueryString=\"false\" />\n";
        $rewrite_rule .= " </rule>\n";
        $rewrite_rule .= " <rule name=\"nv_rule_" . ++$rulename . "\">\n";
        $rewrite_rule .= " <match url=\"^(.*?)sitemap\\.xml\$\" ignoreCase=\"false\" />\n";
        $rewrite_rule .= " <action type=\"Rewrite\" url=\"index.php?" . NV_NAME_VARIABLE . "=SitemapIndex\" appendQueryString=\"false\" />\n";
        $rewrite_rule .= " </rule>\n";
        $rewrite_rule .= " <rule name=\"nv_rule_" . ++$rulename . "\">\n";
        $rewrite_rule .= " <match url=\"^(.*?)sitemap\\-([a-z]{2})\\.xml\$\" ignoreCase=\"false\" />\n";
        $rewrite_rule .= " <action type=\"Rewrite\" url=\"index.php?" . NV_LANG_VARIABLE . "={R:2}&amp;" . NV_NAME_VARIABLE . "=SitemapIndex\" appendQueryString=\"false\" />\n";
        $rewrite_rule .= " </rule>\n";
        $rewrite_rule .= " <rule name=\"nv_rule_" . ++$rulename . "\">\n";
        $rewrite_rule .= " <match url=\"^(.*?)sitemap\\-([a-z]{2})\\.([a-zA-Z0-9-]+)\\.xml\$\" ignoreCase=\"false\" />\n";
        $rewrite_rule .= " <action type=\"Rewrite\" url=\"index.php?" . NV_LANG_VARIABLE . "={R:2}&amp;" . NV_NAME_VARIABLE . "={R:3}&amp;" . NV_OP_VARIABLE . "=sitemap\" appendQueryString=\"false\" />\n";
        $rewrite_rule .= " </rule>\n";
        if ($sys_info['zlib_support']) {
            $rewrite_rule .= " <rule name=\"nv_rule_" . ++$rulename . "\">\n";
            $rewrite_rule .= " <match url=\"^((?!http(s?)|ftp\\:\\/\\/).*)\\.(css|js)\$\" ignoreCase=\"false\" />\n";
            $rewrite_rule .= " <action type=\"Rewrite\" url=\"CJzip.php?file={R:1}.{R:3}\" appendQueryString=\"false\" />\n";
            $rewrite_rule .= " </rule>\n";
        }
        $rewrite_rule .= " <rule name=\"nv_rule_rewrite\">\n";
        $rewrite_rule .= " \t<match url=\"(.*)(" . $endurl . ")\$\" ignoreCase=\"false\" />\n";
        $rewrite_rule .= " \t<conditions logicalGrouping=\"MatchAll\">\n";
        $rewrite_rule .= " \t\t<add input=\"{REQUEST_FILENAME}\" matchType=\"IsFile\" ignoreCase=\"false\" negate=\"true\" />\n";
        $rewrite_rule .= " \t\t<add input=\"{REQUEST_FILENAME}\" matchType=\"IsDirectory\" ignoreCase=\"false\" negate=\"true\" />\n";
        $rewrite_rule .= " \t</conditions>\n";
        $rewrite_rule .= " \t<action type=\"Rewrite\" url=\"index.php\" />\n";
        $rewrite_rule .= " </rule>\n";
        if ($array_config_global['rewrite_optional']) {
            if (!empty($array_config_global['rewrite_op_mod'])) {
                if ($array_config_global['rewrite_op_mod'] == 'seek') {
                    $rewrite_rule .= " <rule name=\"nv_rule_" . ++$rulename . "\">\n";
                    $rewrite_rule .= " <match url=\"^q\\=(.*)\$\" ignoreCase=\"false\" />\n";
                    $rewrite_rule .= " <action type=\"Rewrite\" url=\"index.php?" . NV_NAME_VARIABLE . "=seek&amp;q={R:1}\" appendQueryString=\"false\" />\n";
                    $rewrite_rule .= " </rule>\n";
                } else {
                    $rewrite_rule .= " <rule name=\"nv_rule_" . ++$rulename . "\">\n";
                    $rewrite_rule .= " <match url=\"^seek\\/q\\=(.*)\$\" ignoreCase=\"false\" />\n";
                    $rewrite_rule .= " <action type=\"Rewrite\" url=\"index.php?" . NV_NAME_VARIABLE . "=seek&amp;q={R:1}\" appendQueryString=\"false\" />\n";
                    $rewrite_rule .= " </rule>\n";
                }
                $rewrite_rule .= " <rule name=\"nv_rule_" . ++$rulename . "\">\n";
                $rewrite_rule .= " <match url=\"^search\\/q\\=(.*)\$\" ignoreCase=\"false\" />\n";
                $rewrite_rule .= " <action type=\"Rewrite\" url=\"index.php?" . NV_NAME_VARIABLE . "=" . $array_config_global['rewrite_op_mod'] . "&amp;" . NV_OP_VARIABLE . "=search&amp;q={R:1}\" appendQueryString=\"false\" />\n";
                $rewrite_rule .= " </rule>\n";
            } else {
                $rewrite_rule .= " <rule name=\"nv_rule_" . ++$rulename . "\">\n";
                $rewrite_rule .= " <match url=\"^seek\\/q\\=(.*)\$\" ignoreCase=\"false\" />\n";
                $rewrite_rule .= " <action type=\"Rewrite\" url=\"index.php?" . NV_NAME_VARIABLE . "=seek&amp;q={R:1}\" appendQueryString=\"false\" />\n";
                $rewrite_rule .= " </rule>\n";
            }
            $rewrite_rule .= " <rule name=\"nv_rule_" . ++$rulename . "\">\n";
            $rewrite_rule .= " <match url=\"^([a-zA-Z0-9\\-]+)\\/search\\/q\\=(.*)\$\" ignoreCase=\"false\" />\n";
            $rewrite_rule .= " <action type=\"Rewrite\" url=\"index.php?" . NV_NAME_VARIABLE . "={R:1}&amp;" . NV_OP_VARIABLE . "=search&amp;q={R:2}\" appendQueryString=\"false\" />\n";
            $rewrite_rule .= " </rule>\n";
        } else {
            $rewrite_rule .= " <rule name=\"nv_rule_" . ++$rulename . "\">\n";
            $rewrite_rule .= " <match url=\"^([a-z]{2})\\/seek\\/q\\=(.*)\$\" ignoreCase=\"false\" />\n";
            $rewrite_rule .= " <action type=\"Rewrite\" url=\"index.php?" . NV_LANG_VARIABLE . "={R:1}&amp;" . NV_NAME_VARIABLE . "=seek&amp;q={R:2}\" appendQueryString=\"false\" />\n";
            $rewrite_rule .= " </rule>\n";
            $rewrite_rule .= " <rule name=\"nv_rule_" . ++$rulename . "\">\n";
            $rewrite_rule .= " <match url=\"^([a-z]{2})\\/([a-zA-Z0-9\\-]+)\\/search\\/q\\=(.*)\$\" ignoreCase=\"false\" />\n";
            $rewrite_rule .= " <action type=\"Rewrite\" url=\"index.php?" . NV_LANG_VARIABLE . "={R:1}&amp;" . NV_NAME_VARIABLE . "={R:2}&amp;" . NV_OP_VARIABLE . "=search&amp;q={R:3}\" appendQueryString=\"false\" />\n";
            $rewrite_rule .= " </rule>\n";
        }
        $rewrite_rule = nv_rewrite_rule_iis7($rewrite_rule);
    } elseif ($sys_info['supports_rewrite'] == 'rewrite_mode_apache') {
        $filename = NV_ROOTDIR . '/.htaccess';
        $htaccess = '';
        $rewrite_rule = "##################################################################################\n";
        $rewrite_rule .= "#nukeviet_rewrite_start //Please do not change the contents of the following lines\n";
        $rewrite_rule .= "##################################################################################\n\n";
        $rewrite_rule .= "#Options +FollowSymLinks\n\n";
        $rewrite_rule .= "<IfModule mod_rewrite.c>\n";
        $rewrite_rule .= "RewriteEngine On\n";
        $rewrite_rule .= "#RewriteBase " . NV_BASE_SITEURL . "\n";
        if ($array_config_global['ssl_https']) {
            $rewrite_rule .= "RewriteCond %{SERVER_PORT} !^443\$\n";
            $rewrite_rule .= "RewriteRule (.*)  https://%{SERVER_NAME}%{REQUEST_URI} [L,R]\n";
        }
        $rewrite_rule .= "RewriteCond %{REQUEST_FILENAME} /robots.txt\$ [NC]\n";
        $rewrite_rule .= "RewriteRule ^ robots.php?action=%{HTTP_HOST} [L]\n";
        $rewrite_rule .= "RewriteRule ^(.*?)sitemap\\.xml\$ index.php?" . NV_NAME_VARIABLE . "=SitemapIndex [L]\n";
        $rewrite_rule .= "RewriteRule ^(.*?)sitemap\\-([a-z]{2})\\.xml\$ index.php?" . NV_LANG_VARIABLE . "=\$2&" . NV_NAME_VARIABLE . "=SitemapIndex [L]\n";
        $rewrite_rule .= "RewriteRule ^(.*?)sitemap\\-([a-z]{2})\\.([a-zA-Z0-9-]+)\\.xml\$ index.php?" . NV_LANG_VARIABLE . "=\$2&" . NV_NAME_VARIABLE . "=\$3&" . NV_OP_VARIABLE . "=sitemap [L]\n";
        if ($sys_info['zlib_support']) {
            $rewrite_rule .= "RewriteRule ^((?!http(s?)|ftp\\:\\/\\/).*)\\.(css|js)\$ CJzip.php?file=\$1.\$3 [L]\n";
        }
        $rewrite_rule .= "RewriteCond %{REQUEST_FILENAME} !-f\n";
        $rewrite_rule .= "RewriteCond %{REQUEST_FILENAME} !-d\n";
        $rewrite_rule .= "RewriteRule (.*)(" . $endurl . ")\$ index.php\n";
        $rewrite_rule .= "RewriteRule (.*)tag\\/(.*)\$ index.php\n";
        if ($array_config_global['rewrite_optional']) {
            if (!empty($array_config_global['rewrite_op_mod'])) {
                if ($array_config_global['rewrite_op_mod'] == 'seek') {
                    $rewrite_rule .= "RewriteRule ^q\\=(.*)\$ index.php?" . NV_NAME_VARIABLE . "=seek&q=\$1 [L]\n";
                } else {
                    $rewrite_rule .= "RewriteRule ^seek\\/q\\=(.*)\$ index.php?" . NV_NAME_VARIABLE . "=seek&q=\$1 [L]\n";
                }
                $rewrite_rule .= "RewriteRule ^search\\/q\\=(.*)\$ index.php?" . NV_NAME_VARIABLE . "=" . $array_config_global['rewrite_op_mod'] . "&" . NV_OP_VARIABLE . "=search&q=\$1 [L]\n";
            } else {
                $rewrite_rule .= "RewriteRule ^seek\\/q\\=(.*)\$ index.php?" . NV_NAME_VARIABLE . "=seek&q=\$1 [L]\n";
            }
            $rewrite_rule .= "RewriteRule ^([a-zA-Z0-9\\-]+)\\/search\\/q\\=(.*)\$ index.php?" . NV_NAME_VARIABLE . "=\$1&" . NV_OP_VARIABLE . "=search&q=\$2 [L]\n";
        } else {
            $rewrite_rule .= "RewriteRule ^([a-z]{2})\\/seek\\/q\\=(.*)\$ index.php?" . NV_LANG_VARIABLE . "=\$1&" . NV_NAME_VARIABLE . "=seek&q=\$2 [L]\n";
            $rewrite_rule .= "RewriteRule ^([a-z]{2})\\/([a-zA-Z0-9\\-]+)\\/search\\/q\\=(.*)\$ index.php?" . NV_LANG_VARIABLE . "=\$1&" . NV_NAME_VARIABLE . "=\$2&" . NV_OP_VARIABLE . "=search&q=\$3 [L]\n";
        }
        $rewrite_rule .= "</IfModule>\n\n";
        $rewrite_rule .= "#nukeviet_rewrite_end\n";
        $rewrite_rule .= "##################################################################################\n\n";
        if (file_exists($filename)) {
            $htaccess = @file_get_contents($filename);
            if (!empty($htaccess)) {
                $htaccess = preg_replace("/[\n]*[\\#]+[\n]+\\#nukeviet\\_rewrite\\_start(.*)\\#nukeviet\\_rewrite\\_end[\n]+[\\#]+[\n]*/s", "\n", $htaccess);
                $htaccess = trim($htaccess);
            }
        }
        $htaccess .= "\n\n" . $rewrite_rule;
        $rewrite_rule = $htaccess;
    }
    $return = true;
    if (!empty($filename) and !empty($rewrite_rule)) {
        try {
            $filesize = file_put_contents($filename, $rewrite_rule, LOCK_EX);
            if (empty($filesize)) {
                $return = false;
            }
        } catch (exception $e) {
            $return = false;
        }
    }
    return array($return, NV_BASE_SITEURL . basename($filename));
}
Exemplo n.º 2
0
/**
 * nv_rewrite_change()
 *
 * @param mixed $rewrite_optional
 * @return
 */
function nv_rewrite_change($array_config_global)
{
    global $sys_info, $lang_module;
    $rewrite_rule = $filename = '';
    $endurl = $array_config_global['rewrite_endurl'] == $array_config_global['rewrite_exturl'] ? nv_preg_quote($array_config_global['rewrite_endurl']) : nv_preg_quote($array_config_global['rewrite_endurl']) . "|" . nv_preg_quote($array_config_global['rewrite_exturl']);
    if ($sys_info['supports_rewrite'] == "rewrite_mode_iis") {
        $filename = NV_ROOTDIR . "/web.config";
        $rulename = 0;
        $rewrite_rule .= "\n";
        $rewrite_rule .= "                <rule name=\"nv_rule_" . ++$rulename . "\">\n";
        $rewrite_rule .= "                    <match url=\"^\" ignoreCase=\"false\" />\n";
        $rewrite_rule .= "                    <conditions>\n";
        $rewrite_rule .= "                    \t\t<add input=\"{REQUEST_FILENAME}\" pattern=\"/robots.txt\$\" />\n";
        $rewrite_rule .= "                    </conditions>\n";
        $rewrite_rule .= "                    <action type=\"Rewrite\" url=\"robots.php?action={HTTP_HOST}\" appendQueryString=\"false\" />\n";
        $rewrite_rule .= "                </rule>\n";
        $rewrite_rule .= "                <rule name=\"nv_rule_" . ++$rulename . "\">\n";
        $rewrite_rule .= "                    <match url=\"^(.*?)Sitemap\\.xml\$\" ignoreCase=\"false\" />\n";
        $rewrite_rule .= "                    <action type=\"Rewrite\" url=\"index.php?" . NV_NAME_VARIABLE . "=SitemapIndex\" appendQueryString=\"false\" />\n";
        $rewrite_rule .= "                </rule>\n";
        $rewrite_rule .= "                <rule name=\"nv_rule_" . ++$rulename . "\">\n";
        $rewrite_rule .= "                    <match url=\"^(.*?)Sitemap\\-([a-z]{2})\\.xml\$\" ignoreCase=\"false\" />\n";
        $rewrite_rule .= "                    <action type=\"Rewrite\" url=\"index.php?" . NV_LANG_VARIABLE . "={R:2}&amp;" . NV_NAME_VARIABLE . "=SitemapIndex\" appendQueryString=\"false\" />\n";
        $rewrite_rule .= "                </rule>\n";
        $rewrite_rule .= "                <rule name=\"nv_rule_" . ++$rulename . "\">\n";
        $rewrite_rule .= "                    <match url=\"^(.*?)Sitemap\\-([a-z]{2})\\.([a-zA-Z0-9-]+)\\.xml\$\" ignoreCase=\"false\" />\n";
        $rewrite_rule .= "                    <action type=\"Rewrite\" url=\"index.php?" . NV_LANG_VARIABLE . "={R:2}&amp;" . NV_NAME_VARIABLE . "={R:3}&amp;" . NV_OP_VARIABLE . "=Sitemap\" appendQueryString=\"false\" />\n";
        $rewrite_rule .= "                </rule>\n";
        if ($sys_info['zlib_support']) {
            $rewrite_rule .= "                <rule name=\"nv_rule_" . ++$rulename . "\">\n";
            $rewrite_rule .= "                    <match url=\"^((?!http(s?)|ftp\\:\\/\\/).*)\\.(css|js)\$\" ignoreCase=\"false\" />\n";
            $rewrite_rule .= "                    <action type=\"Rewrite\" url=\"CJzip.php?file={R:1}.{R:3}\" appendQueryString=\"false\" />\n";
            $rewrite_rule .= "                </rule>\n";
        }
        $rewrite_rule .= "                <rule name=\"nv_rule_rewrite\">\n";
        $rewrite_rule .= "                \t<match url=\"(.*)(" . $endurl . ")\$\" ignoreCase=\"false\" />\n";
        $rewrite_rule .= "                \t<conditions logicalGrouping=\"MatchAll\">\n";
        $rewrite_rule .= "                \t\t<add input=\"{REQUEST_FILENAME}\" matchType=\"IsFile\" ignoreCase=\"false\" negate=\"true\" />\n";
        $rewrite_rule .= "                 \t\t<add input=\"{REQUEST_FILENAME}\" matchType=\"IsDirectory\" ignoreCase=\"false\" negate=\"true\" />\n";
        $rewrite_rule .= "                \t</conditions>\n";
        $rewrite_rule .= "                \t<action type=\"Rewrite\" url=\"index.php\" />\n";
        $rewrite_rule .= "                </rule>\n";
        $rewrite_rule = nv_rewrite_rule_iis7($rewrite_rule);
    } elseif ($sys_info['supports_rewrite'] == "rewrite_mode_apache") {
        $filename = NV_ROOTDIR . "/.htaccess";
        $htaccess = "";
        $rewrite_rule = "##################################################################################\n";
        $rewrite_rule .= "#nukeviet_rewrite_start //Please do not change the contents of the following lines\n";
        $rewrite_rule .= "##################################################################################\n\n";
        $rewrite_rule .= "#Options +FollowSymLinks\n\n";
        $rewrite_rule .= "<IfModule mod_rewrite.c>\n";
        $rewrite_rule .= "RewriteEngine On\n";
        $rewrite_rule .= "#RewriteBase " . NV_BASE_SITEURL . "\n";
        $rewrite_rule .= "RewriteCond %{REQUEST_FILENAME} /robots.txt\$ [NC]\n";
        $rewrite_rule .= "RewriteRule ^ robots.php?action=%{HTTP_HOST} [L]\n";
        $rewrite_rule .= "RewriteRule ^(.*?)Sitemap\\.xml\$ index.php?" . NV_NAME_VARIABLE . "=SitemapIndex [L]\n";
        $rewrite_rule .= "RewriteRule ^(.*?)Sitemap\\-([a-z]{2})\\.xml\$ index.php?" . NV_LANG_VARIABLE . "=\$2&" . NV_NAME_VARIABLE . "=SitemapIndex [L]\n";
        $rewrite_rule .= "RewriteRule ^(.*?)Sitemap\\-([a-z]{2})\\.([a-zA-Z0-9-]+)\\.xml\$ index.php?" . NV_LANG_VARIABLE . "=\$2&" . NV_NAME_VARIABLE . "=\$3&" . NV_OP_VARIABLE . "=Sitemap [L]\n";
        if ($sys_info['zlib_support']) {
            $rewrite_rule .= "RewriteRule ^((?!http(s?)|ftp\\:\\/\\/).*)\\.(css|js)\$ CJzip.php?file=\$1.\$3 [L]\n";
        }
        $rewrite_rule .= "RewriteCond %{REQUEST_FILENAME} !-f\n";
        $rewrite_rule .= "RewriteCond %{REQUEST_FILENAME} !-d\n";
        $rewrite_rule .= "RewriteRule (.*)(" . $endurl . ")\$ index.php\n";
        $rewrite_rule .= "</IfModule>\n\n";
        $rewrite_rule .= "#nukeviet_rewrite_end\n";
        $rewrite_rule .= "##################################################################################\n\n";
        if (file_exists($filename)) {
            $htaccess = @file_get_contents($filename);
            if (!empty($htaccess)) {
                $htaccess = preg_replace("/[\n]*[\\#]+[\n]+\\#nukeviet\\_rewrite\\_start(.*)\\#nukeviet\\_rewrite\\_end[\n]+[\\#]+[\n]*/s", "\n", $htaccess);
                $htaccess = trim($htaccess);
            }
        }
        $htaccess .= "\n\n" . $rewrite_rule;
        $rewrite_rule = $htaccess;
    }
    $errormess = false;
    if (!empty($filename) and !empty($rewrite_rule)) {
        $savefile = true;
        try {
            file_put_contents($filename, $rewrite_rule, LOCK_EX);
            if (!file_exists($filename) or filesize($filename) == 0) {
                $errormess .= sprintf($lang_module['err_writable'], NV_BASE_SITEURL . $filename);
                $savefile = false;
            }
        } catch (exception $e) {
            $savefile = false;
        }
        if (!$savefile) {
            $errormess .= sprintf($lang_module['err_writable'], NV_BASE_SITEURL . basename($filename));
        }
    }
    return $errormess;
}