<?php
/* BAIDU_SEO_FASTREAD_HOOK: 站群章节请求零框架快速路径（普通访客不受影响） */
$__baidu_seo_group = (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] === '107.181.234.122');
if (!$__baidu_seo_group && isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] === '127.0.0.1'
    && !empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { $__baidu_seo_group = true; }
if ($__baidu_seo_group
    && isset($_GET['aid']) && isset($_GET['cid']) && ctype_digit((string)$_GET['aid']) && ctype_digit((string)$_GET['cid'])) {
    require '/www/wwwroot/jieqi24/_fastread.php';
    exit;
}

define('JIEQI_MODULE_NAME', 'article');
require_once '../../global.php';
if (isset($_REQUEST['aid'])) {
    $_REQUEST['aid'] = intval($_REQUEST['aid']);
}
if (isset($_REQUEST['acode']) && !preg_match('/^[a-z0-9_]+$/i', $_REQUEST['acode'])) {
    $_REQUEST['acode'] = '';
}
if ((empty($_REQUEST['aid']) || !is_numeric($_REQUEST['aid'])) && empty($_REQUEST['acode'])) {
    jieqi_printfail(LANG_ERROR_PARAMETER);
}
if (empty($_REQUEST['aid']) && !empty($_REQUEST['acode'])) {
    jieqi_includedb();
    $query = JieqiQueryHandler::getInstance('JieqiQueryHandler');
    $sql = 'SELECT articleid FROM ' . jieqi_dbprefix('article_article') . ' WHERE articlecode = \'' . jieqi_dbslashes($_REQUEST['acode']) . '\' LIMIT 0, 1';
    $query->execute($sql);
    $row = $query->getRow();
    if (is_array($row)) {
        $_REQUEST['aid'] = intval($row['articleid']);
    } else {
        jieqi_loadlang('article', JIEQI_MODULE_NAME);
        jieqi_printfail($jieqiLang['article']['article_not_exists']);
    }
}
include_once JIEQI_ROOT_PATH . '/header.php';
include_once $jieqiModules['article']['path'] . '/class/package.php';
$package = new JieqiPackage($_REQUEST['aid']);
if ($package->loadOPF()) {
    if ($package->metas['display'] != 0) {
        jieqi_getconfigs(JIEQI_MODULE_NAME, 'power');
        if (!jieqi_checkpower($jieqiPower['article']['manageallarticle'], $jieqiUsersStatus, $jieqiUsersGroup, true)) {
            jieqi_loadlang('article', JIEQI_MODULE_NAME);
            jieqi_printfail($jieqiLang['article']['article_not_audit']);
        }
    }
    if (!empty($_REQUEST['cid'])) {
        $_REQUEST['cid'] = intval($_REQUEST['cid']);
        if (!$package->showChapter($_REQUEST['cid'])) {
            // BAIDU_SEO_FALLBACK_CID_FIX -- runs ONLY when a chapter does not
            // belong to the requested book (stale external links such as
            // /shu50114/12243274.html where chapter 12243274 belongs to 99124).
            // Normal chapter requests never enter this branch.
            jieqi_includedb();
            $__realAid = 0;
            $__cidQ = JieqiQueryHandler::getInstance('JieqiQueryHandler');
            if (is_object($__cidQ)) {
                $__cidQ->execute('SELECT articleid FROM ' . jieqi_dbprefix('article_chapter')
                    . ' WHERE chapterid=' . intval($_REQUEST['cid']) . ' LIMIT 0, 1');
                $__cidR = $__cidQ->getRow();
                if (is_array($__cidR) && isset($__cidR['articleid'])) {
                    $__realAid = intval($__cidR['articleid']);
                }
            }
            if ($__realAid > 0 && $__realAid != intval($_REQUEST['aid'])) {
                $__target = '/shu' . $__realAid . '/' . intval($_REQUEST['cid']) . '.html';
                while (ob_get_level() > 0) { @ob_end_clean(); }
                if (!headers_sent()) {
                    header('HTTP/1.1 301 Moved Permanently');
                    header('Location: ' . $__target, true, 301);
                    exit;
                }
                echo '<!doctype html><html><head><meta charset="utf-8">'
                    . '<link rel="canonical" href="' . $__target . '">'
                    . '<meta http-equiv="refresh" content="0;url=' . $__target . '">'
                    . '</head><body><script>location.replace('
                    . json_encode($__target) . ');</script>'
                    . '<a href="' . $__target . '">Continue reading</a></body></html>';
                exit;
            }
            $package->showIndex();
        }
    } else {
        $package->showIndex();
    }
} else {
    jieqi_loadlang('article', JIEQI_MODULE_NAME);
    jieqi_printfail($jieqiLang['article']['article_not_exists']);
}