Skip to content

Commit

Permalink
fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
joyqi committed Apr 22, 2014
1 parent 6a73282 commit 8c3d36d
Show file tree
Hide file tree
Showing 13 changed files with 119 additions and 51 deletions.
2 changes: 1 addition & 1 deletion admin/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
define('__TYPECHO_ADMIN__', true);

/** 载入配置文件 */
if (!@include_once __DIR__ . '/../config.inc.php') {
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;
}
Expand Down
2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

/** 载入配置支持 */
if (!@include_once 'config.inc.php') {
if (!defined('__TYPECHO_ROOT_DIR__') && !@include_once 'config.inc.php') {
file_exists('./install.php') ? header('Location: install.php') : print('Missing Config File');
exit;
}
Expand Down
3 changes: 2 additions & 1 deletion var/Typecho/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ class Typecho_Request
'integer' => 'intval',
'search' => array('Typecho_Common', 'filterSearchQuery'),
'xss' => array('Typecho_Common', 'removeXSS'),
'url' => array('Typecho_Common', 'safeUrl')
'url' => array('Typecho_Common', 'safeUrl'),
'slug' => array('Typecho_Common', 'slugName')
);

/**
Expand Down
3 changes: 0 additions & 3 deletions var/Typecho/Widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,6 @@ public static function widget($alias, $params = NULL, $request = NULL, $enableRe
}

if (!isset(self::$_widgetPool[$alias])) {
$fileName = str_replace('_', '/', $className) . '.php';
require_once $fileName;

/** 如果类不存在 */
if (!class_exists($className)) {
throw new Typecho_Widget_Exception($className);
Expand Down
4 changes: 2 additions & 2 deletions var/Widget/Archive.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public function __construct($request, $response, $params = NULL)
}

/** 初始化皮肤路径 */
$this->_themeDir = __TYPECHO_ROOT_DIR__ . '/' . __TYPECHO_THEME_DIR__ . '/' . $this->options->theme . '/';
$this->_themeDir = rtrim($this->options->themeFile($this->options->theme), '/') . '/';

/** 处理feed模式 **/
if ('feed' == $this->parameter->type) {
Expand Down Expand Up @@ -1864,7 +1864,7 @@ public function is($archiveType, $archiveSlug = NULL)
*/
public function need($fileName)
{
require __TYPECHO_ROOT_DIR__ . '/' . __TYPECHO_THEME_DIR__ . '/' . $this->options->theme . '/' . $fileName;
require $this->_themeDir . $fileName;
}

/**
Expand Down
23 changes: 23 additions & 0 deletions var/Widget/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,29 @@ public function pluginUrl($path = NULL)
echo Typecho_Common::url($path, $this->pluginUrl);
}

/**
* 获取皮肤文件
*
* @param string $theme
* @param string $file
* @return string
*/
public function themeFile($theme, $file = '')
{
return __TYPECHO_ROOT_DIR__ . __TYPECHO_THEME_DIR__ . '/' . trim($theme, './') . '/' . trim($file, './');
}

/**
* 获取插件目录
*
* @param $plugin
* @return string
*/
public function pluginDir($plugin)
{
return __TYPECHO_ROOT_DIR__ . '/' . __TYPECHO_PLUGIN_DIR__;
}

/**
* 输出后台路径
*
Expand Down
9 changes: 5 additions & 4 deletions var/Widget/Plugins/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,14 @@ class Widget_Plugins_Config extends Widget_Abstract_Options
public function execute()
{
$this->user->pass('administrator');
if (!isset($this->request->config)) {
$config = $this->request->filter('slug')->config;
if (empty($config)) {
throw new Typecho_Widget_Exception(_t('插件不存在'), 404);
}

/** 获取插件入口 */
list($this->_pluginFileName, $this->_className) = Typecho_Plugin::portal($this->request->config,
__TYPECHO_ROOT_DIR__ . '/' . __TYPECHO_PLUGIN_DIR__);
list($this->_pluginFileName, $this->_className) = Typecho_Plugin::portal($config,
$this->options->pluginDir($config));
$this->info = Typecho_Plugin::parseInfo($this->_pluginFileName);
}

Expand All @@ -82,7 +83,7 @@ public function getMenuTitle()
public function config()
{
/** 获取插件名称 */
$pluginName = $this->request->config;
$pluginName = $this->request->filter('slug')->config;

/** 获取已启用插件 */
$plugins = Typecho_Plugin::export();
Expand Down
12 changes: 6 additions & 6 deletions var/Widget/Plugins/Edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static function configPlugin($pluginName, array $settings, $isPersonal =
public function activate($pluginName)
{
/** 获取插件入口 */
list($pluginFileName, $className) = Typecho_Plugin::portal($pluginName, __TYPECHO_ROOT_DIR__ . '/' . __TYPECHO_PLUGIN_DIR__);
list($pluginFileName, $className) = Typecho_Plugin::portal($pluginName, $this->options->pluginDir($pluginName));
$info = Typecho_Plugin::parseInfo($pluginFileName);

/** 检测依赖信息 */
Expand Down Expand Up @@ -155,7 +155,7 @@ public function deactivate($pluginName)

try {
/** 获取插件入口 */
list($pluginFileName, $className) = Typecho_Plugin::portal($pluginName, __TYPECHO_ROOT_DIR__ . '/' . __TYPECHO_PLUGIN_DIR__);
list($pluginFileName, $className) = Typecho_Plugin::portal($pluginName, $this->options->pluginDir($pluginName));
} catch (Typecho_Plugin_Exception $e) {
$pluginFileExist = false;

Expand Down Expand Up @@ -251,7 +251,7 @@ public function config($pluginName)
public function configHandle($pluginName, array $settings, $isInit)
{
/** 获取插件入口 */
list($pluginFileName, $className) = Typecho_Plugin::portal($pluginName, __TYPECHO_ROOT_DIR__ . '/' . __TYPECHO_PLUGIN_DIR__);
list($pluginFileName, $className) = Typecho_Plugin::portal($pluginName, $this->options->pluginDir($pluginName));

if (method_exists($className, 'configHandle')) {
call_user_func(array($className, 'configHandle'), $settings, $isInit);
Expand Down Expand Up @@ -289,9 +289,9 @@ public function action()
{
$this->user->pass('administrator');
$this->security->protect();
$this->on($this->request->is('activate'))->activate($this->request->activate);
$this->on($this->request->is('deactivate'))->deactivate($this->request->deactivate);
$this->on($this->request->is('config'))->config($this->request->config);
$this->on($this->request->is('activate'))->activate($this->request->filter('slug')->activate);
$this->on($this->request->is('deactivate'))->deactivate($this->request->filter('slug')->deactivate);
$this->on($this->request->is('config'))->config($this->request->filter('slug')->config);
$this->response->redirect($this->options->adminUrl);
}
}
59 changes: 42 additions & 17 deletions var/Widget/Plugins/List.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,42 @@ class Widget_Plugins_List extends Typecho_Widget
*/
public $activatedPlugins = array();

/**
* @return array
*/
protected function getPlugins()
{
return glob(__TYPECHO_ROOT_DIR__ . '/' . __TYPECHO_PLUGIN_DIR__ . '/*');
}

/**
* @param string $plugin
* @param string $index
* @return array|null
*/
protected function getPlugin($plugin, $index)
{
if (is_dir($plugin)) {
/** 获取插件名称 */
$pluginName = basename($plugin);

/** 获取插件主文件 */
$pluginFileName = $plugin . '/Plugin.php';
} else if (file_exists($plugin) && 'index.php' != basename($plugin)) {
$pluginFileName = $plugin;
$part = explode('.', basename($plugin));
if (2 == count($part) && 'php' == $part[1]) {
$pluginName = $part[0];
} else {
return NULL;
}
} else {
return NULL;
}

return array($pluginName, $pluginFileName);
}

/**
* 执行函数
*
Expand All @@ -36,33 +72,22 @@ class Widget_Plugins_List extends Typecho_Widget
public function execute()
{
/** 列出插件目录 */
$pluginDirs = glob(__TYPECHO_ROOT_DIR__ . '/' . __TYPECHO_PLUGIN_DIR__ . '/*');
$pluginDirs = $this->getPlugins();
$this->parameter->setDefault(array('activated' => NULL));

/** 获取已启用插件 */
$plugins = Typecho_Plugin::export();
$this->activatedPlugins = $plugins['activated'];

if (!empty($pluginDirs)) {
foreach ($pluginDirs as $pluginDir) {
if (is_dir($pluginDir)) {
/** 获取插件名称 */
$pluginName = basename($pluginDir);

/** 获取插件主文件 */
$pluginFileName = $pluginDir . '/Plugin.php';
} else if (file_exists($pluginDir) && 'index.php' != basename($pluginDir)) {
$pluginFileName = $pluginDir;
$part = explode('.', basename($pluginDir));
if (2 == count($part) && 'php' == $part[1]) {
$pluginName = $part[0];
} else {
continue;
}
} else {
foreach ($pluginDirs as $key => $pluginDir) {
$parts = $this->getPlugin($pluginDir, $key);
if (empty($parts)) {
continue;
}

list ($pluginName, $pluginFileName) = $parts;

if (file_exists($pluginFileName)) {
$info = Typecho_Plugin::parseInfo($pluginFileName);
$info['name'] = $pluginName;
Expand Down
6 changes: 3 additions & 3 deletions var/Widget/Themes/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ public function getMenuTitle()
*/
public static function isExists()
{
$configFile = __TYPECHO_ROOT_DIR__ . __TYPECHO_THEME_DIR__
. '/' . Typecho_Widget::widget('Widget_Options')->theme . '/functions.php';
$options = Typecho_Widget::widget('Widget_Options');
$configFile = $options->themeFile($options->theme, 'functions.php');

if (file_exists($configFile)) {
require_once $configFile;

Expand Down
11 changes: 6 additions & 5 deletions var/Widget/Themes/Edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Widget_Themes_Edit extends Widget_Abstract_Options implements Widget_Inter
public function changeTheme($theme)
{
$theme = trim($theme, './');
if (is_dir(__TYPECHO_ROOT_DIR__ . __TYPECHO_THEME_DIR__ . '/' . $theme)) {
if (is_dir($this->options->themeFile($theme))) {
/** 删除原外观设置信息 */
$this->delete($this->db->sql()->where('name = ?', 'theme:' . $this->options->theme));

Expand All @@ -43,7 +43,7 @@ public function changeTheme($theme)
$this->update(array('value' => 'recent'), $this->db->sql()->where('name = ?', 'frontPage'));
}

$configFile = __TYPECHO_ROOT_DIR__ . __TYPECHO_THEME_DIR__ . '/' . $theme . '/functions.php';
$configFile = $this->options->themeFile($theme, 'functions.php');

if (file_exists($configFile)) {
require_once $configFile;
Expand Down Expand Up @@ -82,7 +82,7 @@ public function changeTheme($theme)
*/
public function editThemeFile($theme, $file)
{
$path = __TYPECHO_ROOT_DIR__ . __TYPECHO_THEME_DIR__ . '/' . trim($theme, './') . '/' . trim($file, './');
$path = $this->options->themeFile($theme, $file);

if (file_exists($path) && is_writeable($path) && !Typecho_Common::isAppEngine()
&& (!defined('__TYPECHO_THEME_WRITEABLE__') || __TYPECHO_THEME_WRITEABLE__)) {
Expand Down Expand Up @@ -170,8 +170,9 @@ public function action()
/** 需要管理员权限 */
$this->user->pass('administrator');
$this->security->protect();
$this->on($this->request->is('change'))->changeTheme($this->request->change);
$this->on($this->request->is('edit&theme'))->editThemeFile($this->request->theme, $this->request->edit);
$this->on($this->request->is('change'))->changeTheme($this->request->filter('slug')->change);
$this->on($this->request->is('edit&theme'))
->editThemeFile($this->request->filter('slug')->theme, $this->request->edit);
$this->on($this->request->is('config'))->config($this->options->theme);
$this->response->redirect($this->options->adminUrl);
}
Expand Down
12 changes: 6 additions & 6 deletions var/Widget/Themes/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ public function execute()
{
/** 管理员权限 */
$this->widget('Widget_User')->pass('administrator');
$this->_currentTheme = $this->request->get('theme', $this->widget('Widget_Options')->theme);
$this->_currentTheme = $this->request->filter('slug')->get('theme', $this->widget('Widget_Options')->theme);

if (preg_match("/^([_0-9a-z-\.\ ])+$/i", $this->_currentTheme)
&& is_dir($dir = __TYPECHO_ROOT_DIR__ . __TYPECHO_THEME_DIR__ . '/' . $this->_currentTheme)
&& is_dir($dir = $this->widget('Widget_Options')->themeFile($this->_currentTheme))
&& (!defined('__TYPECHO_THEME_WRITEABLE__') || __TYPECHO_THEME_WRITEABLE__)) {
$files = glob($dir . '/*.{php,PHP,js,JS,css,CSS,vbs,VBS}', GLOB_BRACE);
$this->_currentFile = $this->request->get('file', 'index.php');
Expand Down Expand Up @@ -95,8 +95,8 @@ public function getMenuTitle()
*/
public function currentContent()
{
return htmlspecialchars(file_get_contents(__TYPECHO_ROOT_DIR__ . __TYPECHO_THEME_DIR__ . '/' .
$this->_currentTheme . '/' . $this->_currentFile));
return htmlspecialchars(file_get_contents($this->widget('Widget_Options')
->themeFile($this->_currentTheme, $this->_currentFile)));
}

/**
Expand All @@ -107,8 +107,8 @@ public function currentContent()
*/
public function currentIsWriteable()
{
return is_writeable(__TYPECHO_ROOT_DIR__ . __TYPECHO_THEME_DIR__ . '/' .
$this->_currentTheme . '/' . $this->_currentFile) && !Typecho_Common::isAppEngine()
return is_writeable($this->widget('Widget_Options')
->themeFile($this->_currentTheme, $this->_currentFile)) && !Typecho_Common::isAppEngine()
&& (!defined('__TYPECHO_THEME_WRITEABLE__') || __TYPECHO_THEME_WRITEABLE__);
}

Expand Down
24 changes: 22 additions & 2 deletions var/Widget/Themes/List.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@
*/
class Widget_Themes_List extends Typecho_Widget
{
/**
* @return array
*/
protected function getThemes()
{
return glob(__TYPECHO_ROOT_DIR__ . __TYPECHO_THEME_DIR__ . '/*');
}

/**
* get theme
*
* @param string $theme
* @param mixed $index
* @return string
*/
protected function getTheme($theme, $index)
{
return basename($theme);
}

/**
* 执行函数
*
Expand All @@ -29,7 +49,7 @@ class Widget_Themes_List extends Typecho_Widget
*/
public function execute()
{
$themes = glob(__TYPECHO_ROOT_DIR__ . __TYPECHO_THEME_DIR__ . '/*');
$themes = $this->getThemes();

if ($themes) {
$options = $this->widget('Widget_Options');
Expand All @@ -42,7 +62,7 @@ public function execute()
$themeFile = $theme . '/index.php';
if (file_exists($themeFile)) {
$info = Typecho_Plugin::parseInfo($themeFile);
$info['name'] = basename($theme);
$info['name'] = $this->getTheme($theme, $key);

if ($info['activated'] = ($options->theme == $info['name'])) {
$activated = $key;
Expand Down

0 comments on commit 8c3d36d

Please sign in to comment.