From 8c3d36d1b20658fb1acc84eb1224f54ed640b8bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A5=81=E5=AE=81?= Date: Tue, 22 Apr 2014 22:42:35 +0800 Subject: [PATCH] fix some bugs --- admin/common.php | 2 +- index.php | 2 +- var/Typecho/Request.php | 3 +- var/Typecho/Widget.php | 3 -- var/Widget/Archive.php | 4 +-- var/Widget/Options.php | 23 ++++++++++++++ var/Widget/Plugins/Config.php | 9 +++--- var/Widget/Plugins/Edit.php | 12 +++---- var/Widget/Plugins/List.php | 59 +++++++++++++++++++++++++---------- var/Widget/Themes/Config.php | 6 ++-- var/Widget/Themes/Edit.php | 11 ++++--- var/Widget/Themes/Files.php | 12 +++---- var/Widget/Themes/List.php | 24 ++++++++++++-- 13 files changed, 119 insertions(+), 51 deletions(-) diff --git a/admin/common.php b/admin/common.php index 39b0a905c6..e3a67023aa 100644 --- a/admin/common.php +++ b/admin/common.php @@ -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; } diff --git a/index.php b/index.php index 0e13ba8b6c..79efa8b4a3 100644 --- a/index.php +++ b/index.php @@ -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; } diff --git a/var/Typecho/Request.php b/var/Typecho/Request.php index ecd66743e8..67b49cf14f 100644 --- a/var/Typecho/Request.php +++ b/var/Typecho/Request.php @@ -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') ); /** diff --git a/var/Typecho/Widget.php b/var/Typecho/Widget.php index 0d06da440d..c07b01c76a 100644 --- a/var/Typecho/Widget.php +++ b/var/Typecho/Widget.php @@ -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); diff --git a/var/Widget/Archive.php b/var/Widget/Archive.php index c67efe424e..2ce5dffc24 100644 --- a/var/Widget/Archive.php +++ b/var/Widget/Archive.php @@ -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) { @@ -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; } /** diff --git a/var/Widget/Options.php b/var/Widget/Options.php index e20f97fd23..8977e8a536 100644 --- a/var/Widget/Options.php +++ b/var/Widget/Options.php @@ -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__; + } + /** * 输出后台路径 * diff --git a/var/Widget/Plugins/Config.php b/var/Widget/Plugins/Config.php index 25fb0d75eb..ffbe2d2ede 100644 --- a/var/Widget/Plugins/Config.php +++ b/var/Widget/Plugins/Config.php @@ -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); } @@ -82,7 +83,7 @@ public function getMenuTitle() public function config() { /** 获取插件名称 */ - $pluginName = $this->request->config; + $pluginName = $this->request->filter('slug')->config; /** 获取已启用插件 */ $plugins = Typecho_Plugin::export(); diff --git a/var/Widget/Plugins/Edit.php b/var/Widget/Plugins/Edit.php index b50bfe7040..45a5a98143 100644 --- a/var/Widget/Plugins/Edit.php +++ b/var/Widget/Plugins/Edit.php @@ -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); /** 检测依赖信息 */ @@ -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; @@ -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); @@ -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); } } diff --git a/var/Widget/Plugins/List.php b/var/Widget/Plugins/List.php index a1d64b00fd..ca192cc6a2 100644 --- a/var/Widget/Plugins/List.php +++ b/var/Widget/Plugins/List.php @@ -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); + } + /** * 执行函数 * @@ -36,7 +72,7 @@ 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)); /** 获取已启用插件 */ @@ -44,25 +80,14 @@ public function execute() $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; diff --git a/var/Widget/Themes/Config.php b/var/Widget/Themes/Config.php index a50804435c..f68835cb9f 100644 --- a/var/Widget/Themes/Config.php +++ b/var/Widget/Themes/Config.php @@ -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; diff --git a/var/Widget/Themes/Edit.php b/var/Widget/Themes/Edit.php index 4b13dbae14..17c1164409 100644 --- a/var/Widget/Themes/Edit.php +++ b/var/Widget/Themes/Edit.php @@ -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)); @@ -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; @@ -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__)) { @@ -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); } diff --git a/var/Widget/Themes/Files.php b/var/Widget/Themes/Files.php index 9dd2cfac4c..99db3b4e47 100644 --- a/var/Widget/Themes/Files.php +++ b/var/Widget/Themes/Files.php @@ -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'); @@ -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))); } /** @@ -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__); } diff --git a/var/Widget/Themes/List.php b/var/Widget/Themes/List.php index cd86c91dda..5a46ab1723 100644 --- a/var/Widget/Themes/List.php +++ b/var/Widget/Themes/List.php @@ -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); + } + /** * 执行函数 * @@ -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'); @@ -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;