Skip to content

Commit

Permalink
Pr/1344 (#1360)
Browse files Browse the repository at this point in the history
* Add missing theme action

* Fix typo

* Optimize code

* Optimize code

* Update themes.php

* Simpler missing theme judgement

Co-authored-by: sy-records <52o@qq52o.cn>
  • Loading branch information
joyqi and sy-records committed Apr 1, 2022
1 parent 142a615 commit ded268e
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 9 deletions.
19 changes: 15 additions & 4 deletions admin/themes.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<ul class="typecho-option-tabs fix-tabs clearfix">
<li class="current"><a href="<?php $options->adminUrl('themes.php'); ?>"><?php _e('可以使用的外观'); ?></a>
</li>
<?php if (!defined('__TYPECHO_THEME_WRITEABLE__') || __TYPECHO_THEME_WRITEABLE__): ?>
<?php if (\Widget\Themes\Files::isWriteable()): ?>
<li><a href="<?php $options->adminUrl('theme-editor.php'); ?>"><?php _e('编辑当前外观'); ?></a></li>
<?php endif; ?>
<?php if (\Widget\Themes\Config::isExists()): ?>
Expand All @@ -33,10 +33,21 @@
</thead>

<tbody>
<?php if ($options->missingTheme): ?>
<tr id="theme-<?php $options->missingTheme; ?>" class="current">
<td colspan="2" class="warning">
<p><strong><?php _e('检测到您之前使用的 "%s" 外观文件不存在,您可以重新上传此外观或者启用其他外观。', $options->missingTheme); ?></strong></p>
<ul>
<li><?php _e('重新上传此外观后刷新当前页面,此提示将会消失。'); ?></li>
<li><?php _e('启用新外观后,当前外观的设置数据将被删除。'); ?></li>
</ul>
</td>
</tr>
<?php endif; ?>
<?php \Widget\Themes\Rows::alloc()->to($themes); ?>
<?php while ($themes->next()): ?>
<tr id="theme-<?php $themes->name(); ?>"
class="<?php if ($themes->activated): ?>current<?php endif; ?>">
class="<?php if ($themes->activated && !$options->missingTheme): ?>current<?php endif; ?>">
<td valign="top"><img src="<?php $themes->screen(); ?>"
alt="<?php $themes->name(); ?>"/></td>
<td valign="top">
Expand All @@ -46,9 +57,9 @@ class="<?php if ($themes->activated): ?>current<?php endif; ?>">
<?php if ($themes->version): ?><?php _e('版本'); ?>: <?php $themes->version() ?><?php endif; ?>
</cite>
<p><?php echo nl2br($themes->description); ?></p>
<?php if ($options->theme != $themes->name): ?>
<?php if ($options->theme != $themes->name || $options->missingTheme): ?>
<p>
<?php if (!defined('__TYPECHO_THEME_WRITEABLE__') || __TYPECHO_THEME_WRITEABLE__): ?>
<?php if (\Widget\Themes\Files::isWriteable()): ?>
<a class="edit"
href="<?php $options->adminUrl('theme-editor.php?theme=' . $themes->name); ?>"><?php _e('编辑'); ?></a> &nbsp;
<?php endif; ?>
Expand Down
27 changes: 27 additions & 0 deletions config.inc.sqlite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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('SQLite', 'typecho_');
$db->addServer(array (
'file' => '/Users/joyqi/Work/typecho/usr/62396a762847a.db',
), \Typecho\Db::READ | \Typecho\Db::WRITE);
\Typecho\Db::set($db);

define('__TYPECHO_DEBUG__', true);
8 changes: 7 additions & 1 deletion var/Widget/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
* @property string $keywords
* @property string $lang
* @property string $theme
* @property string|null $missingTheme
* @property int $pageSize
* @property int $serverTimezone
* @property int $timezone
Expand Down Expand Up @@ -186,7 +187,12 @@ public function execute()
$this->plugins = unserialize($this->plugins);

/** 动态判断皮肤目录 */
$this->theme = is_dir($this->themeFile($this->theme)) ? $this->theme : 'default';
$this->missingTheme = null;

if (!is_dir($this->themeFile($this->theme))) {
$this->missingTheme = $this->theme;
$this->theme = 'default';
}

/** 增加对SSL连接的支持 */
if ($this->request->isSecure() && 0 === strpos($this->siteUrl, 'http://')) {
Expand Down
2 changes: 1 addition & 1 deletion var/Widget/Themes/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static function isExists(): bool
$options = Options::alloc();
$configFile = $options->themeFile($options->theme, 'functions.php');

if (file_exists($configFile)) {
if (!$options->missingTheme && file_exists($configFile)) {
require_once $configFile;

if (function_exists('themeConfig')) {
Expand Down
5 changes: 3 additions & 2 deletions var/Widget/Themes/Edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public function changeTheme(string $theme)
$theme = trim($theme, './');
if (is_dir($this->options->themeFile($theme))) {
/** 删除原外观设置信息 */
$this->delete($this->db->sql()->where('name = ?', 'theme:' . $this->options->theme));
$oldTheme = $this->options->missingTheme ?: $this->options->theme;
$this->delete($this->db->sql()->where('name = ?', 'theme:' . $oldTheme));

$this->update(['value' => $theme], $this->db->sql()->where('name = ?', 'theme'));

Expand Down Expand Up @@ -130,7 +131,7 @@ public function config(string $theme)
$form = Config::alloc()->config();

/** 验证表单 */
if ($form->validate()) {
if (!Config::isExists() || $form->validate()) {
$this->response->goBack();
}

Expand Down
13 changes: 12 additions & 1 deletion var/Widget/Themes/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ public function execute()
throw new Widget\Exception('风格文件不存在', 404);
}

/**
* 判断是否拥有写入权限
*
* @return bool
*/
public static function isWriteable(): bool
{
return (!defined('__TYPECHO_THEME_WRITEABLE__') || __TYPECHO_THEME_WRITEABLE__)
&& !Options::alloc()->missingTheme;
}

/**
* 获取菜单标题
*
Expand Down Expand Up @@ -111,7 +122,7 @@ public function currentIsWriteable(): bool
{
return is_writeable(Options::alloc()
->themeFile($this->currentTheme, $this->currentFile))
&& (!defined('__TYPECHO_THEME_WRITEABLE__') || __TYPECHO_THEME_WRITEABLE__);
&& self::isWriteable();
}

/**
Expand Down

0 comments on commit ded268e

Please sign in to comment.