getServer('TYPECHO_LANG');
if (!empty($serverLang)) {
return $serverLang;
} else {
$lang = 'zh_CN';
$request = \Typecho\Request::getInstance();
if ($request->is('lang')) {
$lang = $request->get('lang');
\Typecho\Cookie::set('lang', $lang);
}
return \Typecho\Cookie::get('lang', $lang);
}
}
/**
* get site url
*
* @return string
*/
function install_get_site_url(): string
{
$request = \Typecho\Request::getInstance();
return install_is_cli() ? $request->getServer('TYPECHO_SITE_URL', 'http://localhost') : $request->getRequestRoot();
}
/**
* detect cli mode
*
* @return bool
*/
function install_is_cli(): bool
{
return \Typecho\Request::getInstance()->isCli();
}
/**
* get default router
*
* @return string[][]
*/
function install_get_default_routers(): array
{
return [
'index' =>
[
'url' => '/',
'widget' => '\Widget\Archive',
'action' => 'render',
],
'archive' =>
[
'url' => '/blog/',
'widget' => '\Widget\Archive',
'action' => 'render',
],
'do' =>
[
'url' => '/action/[action:alpha]',
'widget' => '\Widget\Action',
'action' => 'action',
],
'post' =>
[
'url' => '/archives/[cid:digital]/',
'widget' => '\Widget\Archive',
'action' => 'render',
],
'attachment' =>
[
'url' => '/attachment/[cid:digital]/',
'widget' => '\Widget\Archive',
'action' => 'render',
],
'category' =>
[
'url' => '/category/[slug]/',
'widget' => '\Widget\Archive',
'action' => 'render',
],
'tag' =>
[
'url' => '/tag/[slug]/',
'widget' => '\Widget\Archive',
'action' => 'render',
],
'author' =>
[
'url' => '/author/[uid:digital]/',
'widget' => '\Widget\Archive',
'action' => 'render',
],
'search' =>
[
'url' => '/search/[keywords]/',
'widget' => '\Widget\Archive',
'action' => 'render',
],
'index_page' =>
[
'url' => '/page/[page:digital]/',
'widget' => '\Widget\Archive',
'action' => 'render',
],
'archive_page' =>
[
'url' => '/blog/page/[page:digital]/',
'widget' => '\Widget\Archive',
'action' => 'render',
],
'category_page' =>
[
'url' => '/category/[slug]/[page:digital]/',
'widget' => '\Widget\Archive',
'action' => 'render',
],
'tag_page' =>
[
'url' => '/tag/[slug]/[page:digital]/',
'widget' => '\Widget\Archive',
'action' => 'render',
],
'author_page' =>
[
'url' => '/author/[uid:digital]/[page:digital]/',
'widget' => '\Widget\Archive',
'action' => 'render',
],
'search_page' =>
[
'url' => '/search/[keywords]/[page:digital]/',
'widget' => '\Widget\Archive',
'action' => 'render',
],
'archive_year' =>
[
'url' => '/[year:digital:4]/',
'widget' => '\Widget\Archive',
'action' => 'render',
],
'archive_month' =>
[
'url' => '/[year:digital:4]/[month:digital:2]/',
'widget' => '\Widget\Archive',
'action' => 'render',
],
'archive_day' =>
[
'url' => '/[year:digital:4]/[month:digital:2]/[day:digital:2]/',
'widget' => '\Widget\Archive',
'action' => 'render',
],
'archive_year_page' =>
[
'url' => '/[year:digital:4]/page/[page:digital]/',
'widget' => '\Widget\Archive',
'action' => 'render',
],
'archive_month_page' =>
[
'url' => '/[year:digital:4]/[month:digital:2]/page/[page:digital]/',
'widget' => '\Widget\Archive',
'action' => 'render',
],
'archive_day_page' =>
[
'url' => '/[year:digital:4]/[month:digital:2]/[day:digital:2]/page/[page:digital]/',
'widget' => '\Widget\Archive',
'action' => 'render',
],
'comment_page' =>
[
'url' => '[permalink:string]/comment-page-[commentPage:digital]',
'widget' => '\Widget\CommentPage',
'action' => 'action',
],
'feed' =>
[
'url' => '/feed[feed:string:0]',
'widget' => '\Widget\Feed',
'action' => 'render',
],
'feedback' =>
[
'url' => '[permalink:string]/[type:alpha]',
'widget' => '\Widget\Feedback',
'action' => 'action',
],
'page' =>
[
'url' => '/[slug].html',
'widget' => '\Widget\Archive',
'action' => 'render',
],
];
}
/**
* list all default options
*
* @return array
*/
function install_get_default_options(): array
{
static $options;
if (empty($options)) {
$options = [
'theme' => 'default',
'theme:default' => json_encode([
'logoUrl' => '',
'sidebarBlock' => [
'ShowRecentPosts',
'ShowRecentComments',
'ShowCategory',
'ShowArchive',
'ShowOther'
]
]),
'timezone' => '28800',
'lang' => install_get_lang(),
'charset' => 'UTF-8',
'contentType' => 'text/html',
'gzip' => 0,
'generator' => 'Typecho ' . \Typecho\Common::VERSION,
'title' => 'Hello World',
'description' => 'Your description here.',
'keywords' => 'typecho,php,blog',
'rewrite' => 0,
'frontPage' => 'recent',
'frontArchive' => 0,
'commentsRequireMail' => 1,
'commentsWhitelist' => 0,
'commentsRequireUrl' => 0,
'commentsRequireModeration' => 0,
'plugins' => 'a:0:{}',
'commentDateFormat' => 'F jS, Y \a\t h:i a',
'siteUrl' => install_get_site_url(),
'defaultCategory' => 1,
'allowRegister' => 0,
'defaultAllowComment' => 1,
'defaultAllowPing' => 1,
'defaultAllowFeed' => 1,
'pageSize' => 5,
'postsListSize' => 10,
'commentsListSize' => 10,
'commentsHTMLTagAllowed' => null,
'postDateFormat' => 'Y-m-d',
'feedFullText' => 1,
'editorSize' => 350,
'autoSave' => 0,
'markdown' => 1,
'xmlrpcMarkdown' => 0,
'commentsMaxNestingLevels' => 5,
'commentsPostTimeout' => 24 * 3600 * 30,
'commentsUrlNofollow' => 1,
'commentsShowUrl' => 1,
'commentsMarkdown' => 0,
'commentsPageBreak' => 0,
'commentsThreaded' => 1,
'commentsPageSize' => 20,
'commentsPageDisplay' => 'last',
'commentsOrder' => 'ASC',
'commentsCheckReferer' => 1,
'commentsAutoClose' => 0,
'commentsPostIntervalEnable' => 1,
'commentsPostInterval' => 60,
'commentsShowCommentOnly' => 0,
'commentsAvatar' => 1,
'commentsAvatarRating' => 'G',
'commentsAntiSpam' => 1,
'routingTable' => json_encode(install_get_default_routers()),
'actionTable' => json_encode([]),
'panelTable' => json_encode([]),
'attachmentTypes' => '@image@',
'secret' => \Typecho\Common::randString(32, true),
'installed' => 0,
'allowXmlRpc' => 2
];
}
return $options;
}
/**
* get database driver type
*
* @param string $driver
* @return string
*/
function install_get_db_type(string $driver): string
{
$parts = explode('_', $driver);
return $driver == 'Mysqli' ? 'Mysql' : array_pop($parts);
}
/**
* list all available database drivers
*
* @return array
*/
function install_get_db_drivers(): array
{
$drivers = [];
if (\Typecho\Db\Adapter\Pdo\Mysql::isAvailable()) {
$drivers['Pdo_Mysql'] = _t('Pdo 驱动 Mysql 适配器');
}
if (\Typecho\Db\Adapter\Pdo\SQLite::isAvailable()) {
$drivers['Pdo_SQLite'] = _t('Pdo 驱动 SQLite 适配器');
}
if (\Typecho\Db\Adapter\Pdo\Pgsql::isAvailable()) {
$drivers['Pdo_Pgsql'] = _t('Pdo 驱动 PostgreSql 适配器');
}
if (\Typecho\Db\Adapter\Mysqli::isAvailable()) {
$drivers['Mysqli'] = _t('Mysql 原生函数适配器');
}
if (\Typecho\Db\Adapter\SQLite::isAvailable()) {
$drivers['SQLite'] = _t('SQLite 原生函数适配器');
}
if (\Typecho\Db\Adapter\Pgsql::isAvailable()) {
$drivers['Pgsql'] = _t('Pgsql 原生函数适配器');
}
return $drivers;
}
/**
* get current db driver
*
* @return string
*/
function install_get_current_db_driver(): string
{
global $installDb;
if (empty($installDb)) {
$driver = \Typecho\Request::getInstance()->get('driver');
$drivers = install_get_db_drivers();
if (empty($driver) || !isset($drivers[$driver])) {
return key($drivers);
}
return $driver;
} else {
return $installDb->getAdapterName();
}
}
/**
* generate config file
*
* @param string $adapter
* @param string $dbPrefix
* @param array $dbConfig
* @param bool $return
* @return string
*/
function install_config_file(string $adapter, string $dbPrefix, array $dbConfig, bool $return = false): string
{
global $configWritten;
$code = "<" . "?php
// site root path
define('__TYPECHO_ROOT_DIR__', dirname(__FILE__));
// plugin directory (relative path)
define('__TYPECHO_PLUGIN_DIR__', '/usr/plugins');
// theme directory (relative path)
define('__TYPECHO_THEME_DIR__', '/usr/themes');
// admin directory (relative path)
define('__TYPECHO_ADMIN_DIR__', '/admin/');
// register autoload
require_once __TYPECHO_ROOT_DIR__ . '/var/Typecho/Common.php';
// init
\Typecho\Common::init();
// config db
\$db = new \Typecho\Db('{$adapter}', '{$dbPrefix}');
\$db->addServer(" . (var_export($dbConfig, true)) . ", \Typecho\Db::READ | \Typecho\Db::WRITE);
\Typecho\Db::set(\$db);
";
$configWritten = false;
if (!$return) {
$configWritten = @file_put_contents(__TYPECHO_ROOT_DIR__ . '/config.inc.php', $code) !== false;
}
return $code;
}
/**
* remove config file if written
*/
function install_remove_config_file()
{
global $configWritten;
if ($configWritten) {
unlink(__TYPECHO_ROOT_DIR__ . '/config.inc.php');
}
}
/**
* check install
*
* @param string $type
* @return bool
*/
function install_check(string $type): bool
{
switch ($type) {
case 'config':
return file_exists(__TYPECHO_ROOT_DIR__ . '/config.inc.php');
case 'db_structure':
case 'db_data':
global $installDb;
if (empty($installDb)) {
return false;
}
try {
// check if table exists
$installed = $installDb->fetchRow($installDb->select()->from('table.options')
->where('user = 0 AND name = ?', 'installed'));
if ($type == 'db_data' && empty($installed['value'])) {
return false;
}
} catch (\Typecho\Db\Adapter\ConnectionException $e) {
return true;
} catch (\Typecho\Db\Adapter\SQLException $e) {
return false;
}
return true;
default:
return false;
}
}
/**
* raise install error
*
* @param mixed $error
* @param mixed $config
*/
function install_raise_error($error, $config = null)
{
if (install_is_cli()) {
if (is_array($error)) {
foreach ($error as $key => $value) {
echo (is_int($key) ? '' : $key . ': ') . $value . "\n";
}
} else {
echo $error . "\n";
}
exit(1);
} else {
install_throw_json([
'success' => 0,
'message' => is_string($error) ? nl2br($error) : $error,
'config' => $config
]);
}
}
/**
* @param $step
* @param array|null $config
*/
function install_success($step, ?array $config = null)
{
global $installDb;
if (install_is_cli()) {
if ($step == 3) {
\Typecho\Db::set($installDb);
}
if ($step > 0) {
$method = 'install_step_' . $step . '_perform';
$method();
}
if (!empty($config)) {
[$userName, $userPassword] = $config;
echo _t('安装成功') . "\n";
echo _t('您的用户名是') . " {$userName}\n";
echo _t('您的密码是') . " {$userPassword}\n";
}
exit(0);
} else {
install_throw_json([
'success' => 1,
'message' => $step,
'config' => $config
]);
}
}
/**
* @param $data
*/
function install_throw_json($data)
{
\Typecho\Response::getInstance()->setContentType('application/json')
->addResponder(function () use ($data) {
echo json_encode($data);
})
->respond();
}
/**
* @param string $url
*/
function install_redirect(string $url)
{
\Typecho\Response::getInstance()->setStatus(302)
->setHeader('Location', $url)
->respond();
}
/**
* add common js support
*/
function install_js_support()
{
?>
getConfig(\Typecho\Db::WRITE)->toArray();
$config['prefix'] = $installDb->getPrefix();
$config['adapter'] = $adapter;
}
?>
[
'dbHost' => 'localhost',
'dbPort' => 3306,
'dbUser' => null,
'dbPassword' => null,
'dbCharset' => 'utf8mb4',
'dbDatabase' => null,
'dbEngine' => 'InnoDB',
'dbSslCa' => null,
'dbSslVerify' => 'off',
],
'Pgsql' => [
'dbHost' => 'localhost',
'dbPort' => 5432,
'dbUser' => null,
'dbPassword' => null,
'dbCharset' => 'utf8',
'dbDatabase' => null,
'dbSslVerify' => 'off',
],
'SQLite' => [
'dbFile' => __TYPECHO_ROOT_DIR__ . '/usr/' . uniqid() . '.db'
]
];
if (install_is_cli()) {
$config = [
'dbHost' => $request->getServer('TYPECHO_DB_HOST'),
'dbUser' => $request->getServer('TYPECHO_DB_USER'),
'dbPassword' => $request->getServer('TYPECHO_DB_PASSWORD'),
'dbCharset' => $request->getServer('TYPECHO_DB_CHARSET'),
'dbPort' => $request->getServer('TYPECHO_DB_PORT'),
'dbDatabase' => $request->getServer('TYPECHO_DB_DATABASE'),
'dbFile' => $request->getServer('TYPECHO_DB_FILE'),
'dbDsn' => $request->getServer('TYPECHO_DB_DSN'),
'dbEngine' => $request->getServer('TYPECHO_DB_ENGINE'),
'dbPrefix' => $request->getServer('TYPECHO_DB_PREFIX', 'typecho_'),
'dbAdapter' => $request->getServer('TYPECHO_DB_ADAPTER', install_get_current_db_driver()),
'dbNext' => $request->getServer('TYPECHO_DB_NEXT', 'none'),
'dbSslCa' => $request->getServer('TYPECHO_DB_SSL_CA'),
'dbSslVerify' => $request->getServer('TYPECHO_DB_SSL_VERIFY', 'off'),
];
} else {
$config = $request->from([
'dbHost',
'dbUser',
'dbPassword',
'dbCharset',
'dbPort',
'dbDatabase',
'dbFile',
'dbDsn',
'dbEngine',
'dbPrefix',
'dbAdapter',
'dbNext',
'dbSslCa',
'dbSslVerify',
]);
}
$error = (new \Typecho\Validate())
->addRule('dbPrefix', 'required', _t('确认您的配置'))
->addRule('dbPrefix', 'minLength', _t('确认您的配置'), 1)
->addRule('dbPrefix', 'maxLength', _t('确认您的配置'), 16)
->addRule('dbPrefix', 'alphaDash', _t('确认您的配置'))
->addRule('dbAdapter', 'required', _t('确认您的配置'))
->addRule('dbAdapter', 'enum', _t('确认您的配置'), array_keys($drivers))
->addRule('dbNext', 'required', _t('确认您的配置'))
->addRule('dbNext', 'enum', _t('确认您的配置'), ['none', 'delete', 'keep', 'config'])
->run($config);
if (!empty($error)) {
install_raise_error($error);
}
$type = install_get_db_type($config['dbAdapter']);
$dbConfig = [];
foreach ($configMap[$type] as $key => $value) {
$config[$key] = !isset($config[$key]) ? (install_is_cli() ? $value : null) : $config[$key];
}
switch ($type) {
case 'Mysql':
$error = (new \Typecho\Validate())
->addRule('dbHost', 'required', _t('确认您的配置'))
->addRule('dbPort', 'required', _t('确认您的配置'))
->addRule('dbPort', 'isInteger', _t('确认您的配置'))
->addRule('dbUser', 'required', _t('确认您的配置'))
->addRule('dbCharset', 'required', _t('确认您的配置'))
->addRule('dbCharset', 'enum', _t('确认您的配置'), ['utf8', 'utf8mb4'])
->addRule('dbDatabase', 'required', _t('确认您的配置'))
->addRule('dbEngine', 'required', _t('确认您的配置'))
->addRule('dbEngine', 'enum', _t('确认您的配置'), ['InnoDB', 'MyISAM'])
->addRule('dbSslCa', 'file_exists', _t('确认您的配置'))
->addRule('dbSslVerify', 'enum', _t('确认您的配置'), ['on', 'off'])
->run($config);
break;
case 'Pgsql':
$error = (new \Typecho\Validate())
->addRule('dbHost', 'required', _t('确认您的配置'))
->addRule('dbPort', 'required', _t('确认您的配置'))
->addRule('dbPort', 'isInteger', _t('确认您的配置'))
->addRule('dbUser', 'required', _t('确认您的配置'))
->addRule('dbCharset', 'required', _t('确认您的配置'))
->addRule('dbCharset', 'enum', _t('确认您的配置'), ['utf8'])
->addRule('dbDatabase', 'required', _t('确认您的配置'))
->addRule('dbSslVerify', 'enum', _t('确认您的配置'), ['on', 'off'])
->run($config);
break;
case 'SQLite':
$error = (new \Typecho\Validate())
->addRule('dbFile', 'required', _t('确认您的配置'))
->addRule('dbFile', function (string $path) {
$pattern = "/^(\/[._a-z0-9-]+)*[a-z0-9]+\.[a-z0-9]{2,}$/i";
if (strstr(PHP_OS, 'WIN')) {
$pattern = "/(\/[._a-z0-9-]+)*[a-z0-9]+\.[a-z0-9]{2,}$/i";
}
return !!preg_match($pattern, $path);
}, _t('确认您的配置'))
->run($config);
break;
default:
install_raise_error(_t('确认您的配置'));
break;
}
if (!empty($error)) {
install_raise_error($error);
}
foreach ($configMap[$type] as $key => $value) {
$dbConfig[lcfirst(substr($key, 2))] = $config[$key];
}
// intval port number
if (isset($dbConfig['port'])) {
$dbConfig['port'] = intval($dbConfig['port']);
}
// bool ssl verify
if (isset($dbConfig['sslVerify'])) {
$dbConfig['sslVerify'] = $dbConfig['sslVerify'] == 'on' || !empty($dbConfig['sslCa']);
}
if (isset($dbConfig['file']) && preg_match("/^[a-z0-9]+\.[a-z0-9]{2,}$/i", $dbConfig['file'])) {
$dbConfig['file'] = __DIR__ . '/usr/' . $dbConfig['file'];
}
// check config file
if ($config['dbNext'] == 'config' && !install_check('config')) {
$code = install_config_file($config['dbAdapter'], $config['dbPrefix'], $dbConfig, true);
install_raise_error(_t('没有检测到您手动创建的配置文件, 请检查后再次创建'), ['code' => $code]);
} elseif (empty($installDb)) {
// detect db config
try {
$installDb = new \Typecho\Db($config['dbAdapter'], $config['dbPrefix']);
$installDb->addServer($dbConfig, \Typecho\Db::READ | \Typecho\Db::WRITE);
$installDb->query('SELECT 1=1');
} catch (\Typecho\Db\Adapter\ConnectionException $e) {
$code = $e->getCode();
if (('Mysql' == $type && 1049 == $code) || ('Pgsql' == $type && 7 == $code)) {
install_raise_error(_t('数据库: "%s"不存在,请手动创建后重试', $config['dbDatabase']));
} else {
install_raise_error(_t('对不起, 无法连接数据库, 请先检查数据库配置再继续进行安装: "%s"', $e->getMessage()));
}
} catch (\Typecho\Db\Exception $e) {
install_raise_error(_t('安装程序捕捉到以下错误: "%s". 程序被终止, 请检查您的配置信息.', $e->getMessage()));
}
$code = install_config_file($config['dbAdapter'], $config['dbPrefix'], $dbConfig);
if (!install_check('config')) {
install_raise_error(
_t('安装程序无法自动创建 config.inc.php 文件') . "\n" .
_t('您可以在网站根目录下手动创建 config.inc.php 文件, 并复制如下代码至其中'),
[
'code' => $code
]
);
}
}
// delete exists db
if ($config['dbNext'] == 'delete') {
$tables = [
$config['dbPrefix'] . 'comments',
$config['dbPrefix'] . 'contents',
$config['dbPrefix'] . 'fields',
$config['dbPrefix'] . 'metas',
$config['dbPrefix'] . 'options',
$config['dbPrefix'] . 'relationships',
$config['dbPrefix'] . 'users'
];
try {
foreach ($tables as $table) {
switch ($type) {
case 'Mysql':
$installDb->query("DROP TABLE IF EXISTS `{$table}`");
break;
case 'Pgsql':
case 'SQLite':
$installDb->query("DROP TABLE {$table}");
break;
}
}
} catch (\Typecho\Db\Exception $e) {
install_raise_error(_t('安装程序捕捉到以下错误: "%s". 程序被终止, 请检查您的配置信息.', $e->getMessage()));
}
}
// init db structure
try {
$scripts = file_get_contents(__TYPECHO_ROOT_DIR__ . '/install/' . $type . '.sql');
$scripts = str_replace('typecho_', $config['dbPrefix'], $scripts);
if (isset($dbConfig['charset'])) {
$scripts = str_replace('%charset%', $dbConfig['charset'], $scripts);
}
if (isset($dbConfig['engine'])) {
$scripts = str_replace('%engine%', $dbConfig['engine'], $scripts);
}
$scripts = explode(';', $scripts);
foreach ($scripts as $script) {
$script = trim($script);
if ($script) {
$installDb->query($script, \Typecho\Db::WRITE);
}
}
} catch (\Typecho\Db\Exception $e) {
$code = $e->getCode();
if (
('Mysql' == $type && (1050 == $code || '42S01' == $code)) ||
('SQLite' == $type && ('HY000' == $code || 1 == $code)) ||
('Pgsql' == $type && '42P07' == $code)
) {
if ($config['dbNext'] == 'keep') {
if (install_check('db_data')) {
install_success(0);
} else {
install_success(3);
}
} elseif ($config['dbNext'] == 'none') {
install_remove_config_file();
install_raise_error(_t('安装程序检查到原有数据表已经存在.'), [
'delete' => _t('删除原有数据'),
'keep' => _t('使用原有数据')
]);
}
} else {
install_remove_config_file();
install_raise_error(_t('安装程序捕捉到以下错误: "%s". 程序被终止, 请检查您的配置信息.', $e->getMessage()));
}
}
install_success(3);
}
/**
* display step 3
*/
function install_step_3()
{
$options = \Widget\Options::alloc();
?>
$request->getServer('TYPECHO_SITE_URL'),
'userName' => $request->getServer('TYPECHO_USER_NAME', 'typecho'),
'userPassword' => $request->getServer('TYPECHO_USER_PASSWORD'),
'userMail' => $request->getServer('TYPECHO_USER_MAIL', 'admin@localhost.local')
];
} else {
$config = $request->from([
'userUrl',
'userName',
'userPassword',
'userMail',
]);
}
$error = (new \Typecho\Validate())
->addRule('userUrl', 'required', _t('请填写站点地址'))
->addRule('userUrl', 'url', _t('请填写一个合法的URL地址'))
->addRule('userName', 'required', _t('必须填写用户名称'))
->addRule('userName', 'xssCheck', _t('请不要在用户名中使用特殊字符'))
->addRule('userName', 'maxLength', _t('用户名长度超过限制, 请不要超过 32 个字符'), 32)
->addRule('userMail', 'required', _t('必须填写电子邮箱'))
->addRule('userMail', 'email', _t('电子邮箱格式错误'))
->addRule('userMail', 'maxLength', _t('邮箱长度超过限制, 请不要超过 200 个字符'), 200)
->run($config);
if (!empty($error)) {
install_raise_error($error);
}
if (empty($config['userPassword'])) {
$config['userPassword'] = $defaultPassword;
}
try {
// write user
$hasher = new \Utils\PasswordHash(8, true);
$installDb->query(
$installDb->insert('table.users')->rows([
'name' => $config['userName'],
'password' => $hasher->hashPassword($config['userPassword']),
'mail' => $config['userMail'],
'url' => $config['userUrl'],
'screenName' => $config['userName'],
'group' => 'administrator',
'created' => \Typecho\Date::time()
])
);
// write category
$installDb->query(
$installDb->insert('table.metas')
->rows([
'name' => _t('默认分类'),
'slug' => 'default',
'type' => 'category',
'description' => _t('只是一个默认分类'),
'count' => 1
])
);
$installDb->query($installDb->insert('table.relationships')->rows(['cid' => 1, 'mid' => 1]));
// write first page and post
$installDb->query(
$installDb->insert('table.contents')->rows([
'title' => _t('欢迎使用 Typecho'),
'slug' => 'start', 'created' => \Typecho\Date::time(),
'modified' => \Typecho\Date::time(),
'text' => '' . _t('如果您看到这篇文章,表示您的 blog 已经安装成功.'),
'authorId' => 1,
'type' => 'post',
'status' => 'publish',
'commentsNum' => 1,
'allowComment' => 1,
'allowPing' => 1,
'allowFeed' => 1,
'parent' => 0
])
);
$installDb->query(
$installDb->insert('table.contents')->rows([
'title' => _t('关于'),
'slug' => 'start-page',
'created' => \Typecho\Date::time(),
'modified' => \Typecho\Date::time(),
'text' => '' . _t('本页面由 Typecho 创建, 这只是个测试页面.'),
'authorId' => 1,
'order' => 0,
'type' => 'page',
'status' => 'publish',
'commentsNum' => 0,
'allowComment' => 1,
'allowPing' => 1,
'allowFeed' => 1,
'parent' => 0
])
);
// write comment
$installDb->query(
$installDb->insert('table.comments')->rows([
'cid' => 1, 'created' => \Typecho\Date::time(),
'author' => 'Typecho',
'ownerId' => 1,
'url' => 'https://typecho.org',
'ip' => '127.0.0.1',
'agent' => $options->generator,
'text' => _t('欢迎加入 Typecho 大家族'),
'type' => 'comment',
'status' => 'approved',
'parent' => 0
])
);
// write options
foreach (install_get_default_options() as $key => $value) {
// mark installing finished
if ($key == 'installed') {
$value = 1;
}
$installDb->query(
$installDb->insert('table.options')->rows(['name' => $key, 'user' => 0, 'value' => $value])
);
}
} catch (\Typecho\Db\Exception $e) {
install_raise_error($e->getMessage());
}
$parts = parse_url($options->loginAction);
$parts['query'] = http_build_query([
'name' => $config['userName'],
'password' => $config['userPassword'],
'referer' => $options->adminUrl
]);
$loginUrl = \Typecho\Common::buildUrl($parts);
install_success(0, [
$config['userName'],
$config['userPassword'],
\Widget\Security::alloc()->getTokenUrl($loginUrl, $request->getReferer()),
$config['userUrl']
]);
}
/**
* dispatch install action
*
*/
function install_dispatch()
{
// disable root url on cli mode
if (install_is_cli()) {
define('__TYPECHO_ROOT_URL__', 'http://localhost');
}
// init default options
$options = \Widget\Options::alloc(install_get_default_options());
\Widget\Init::alloc();
// display version
if (install_is_cli()) {
echo $options->generator . "\n";
echo 'PHP ' . PHP_VERSION . "\n";
}
// install finished yet
if (
install_check('config')
&& install_check('db_structure')
&& install_check('db_data')
) {
// redirect to siteUrl if not cli
if (!install_is_cli()) {
install_redirect($options->siteUrl);
}
exit(1);
}
if (install_is_cli()) {
install_step_1_perform();
} else {
$request = \Typecho\Request::getInstance();
$step = $request->get('step');
$action = 1;
switch (true) {
case $step == 2:
if (!install_check('db_structure')) {
$action = 2;
} else {
install_redirect('install.php?step=3');
}
break;
case $step == 3:
if (install_check('db_structure')) {
$action = 3;
} else {
install_redirect('install.php?step=2');
}
break;
default:
break;
}
$method = 'install_step_' . $action;
if ($request->isPost()) {
$method .= '_perform';
$method();
exit;
}
?>