<?php
if (!defined('__DIR__')) {
    define('__DIR__', dirname(__FILE__));
}

define('__TYPECHO_ADMIN__', true);

/** 载入配置文件 */
if (!defined('__TYPECHO_ROOT_DIR__') && !@include_once __DIR__ . '/../config.inc.php') {
    file_exists(__DIR__ . '/../install.php') ? header('Location: ../install.php') : print('Missing Config File');
    exit;
}

/** 初始化组件 */
Typecho_Widget::widget('Widget_Init');

/** 注册一个初始化插件 */
Typecho_Plugin::factory('admin/common.php')->begin();

Typecho_Widget::widget('Widget_Options')->to($options);
Typecho_Widget::widget('Widget_User')->to($user);
Typecho_Widget::widget('Widget_Security')->to($security);
Typecho_Widget::widget('Widget_Menu')->to($menu);

/** 初始化上下文 */
$request = $options->request;
$response = $options->response;

/** 检测是否是第一次登录 */
$currentMenu = $menu->getCurrentMenu();
list($prefixVersion, $suffixVersion) = explode('/', $options->version);

/** 检测版本是否升级 */
if ($user->pass('administrator', true) && !empty($currentMenu)) {
    $mustUpgrade = (!defined('Typecho_Common::VERSION') || version_compare(str_replace('/', '.', Typecho_Common::VERSION),
    str_replace('/', '.', $options->version), '>'));

    if ($mustUpgrade && 'upgrade.php' != $adminFile && 'backup.php' != $adminFile) {
        $response->redirect(Typecho_Common::url('upgrade.php', $options->adminUrl));
    } else if (!$mustUpgrade && 'upgrade.php' == $adminFile) {
        $response->redirect($options->adminUrl);
    } else if (!$mustUpgrade && 'welcome.php' == $adminFile && $user->logged) {
        $response->redirect($options->adminUrl);
    }
}

function decodeRule($rule)
{
    return preg_replace("/\[([_a-z0-9-]+)[^\]]*\]/i", "{\\1}", $rule);
}
