/** * 激活插件 * * @access public * @return void */ public function activate($pluginName) { /** 获取插件入口 */ list($pluginFileName, $className) = Typecho_Plugin::portal($pluginName, __TYPECHO_ROOT_DIR__ . '/' . __TYPECHO_PLUGIN_DIR__); $info = Typecho_Plugin::parseInfo($pluginFileName); /** 检测依赖信息 */ list($version, $build) = explode('/', Typecho_Common::VERSION); if (Typecho_Plugin::checkDependence($build, $info['dependence'])) { /** 获取已激活插件 */ $plugins = Typecho_Plugin::export(); $activatedPlugins = $plugins['activated']; /** 载入插件 */ require_once $pluginFileName; /** 判断实例化是否成功 */ if (isset($activatedPlugins[$pluginName]) || !class_exists($className) || !method_exists($className, 'activate')) { throw new Typecho_Widget_Exception(_t('无法激活插件'), 500); } try { $result = call_user_func(array($className, 'activate')); Typecho_Plugin::activate($pluginName); $this->update(array('value' => serialize(Typecho_Plugin::export())), $this->db->sql()->where('name = ?', 'plugins')); } catch (Typecho_Plugin_Exception $e) { /** 截获异常 */ $this->widget('Widget_Notice')->set($e->getMessage(), NULL, 'error'); $this->response->goBack(); } $form = new Typecho_Widget_Helper_Form(); call_user_func(array($className, 'config'), $form); $personalForm = new Typecho_Widget_Helper_Form(); call_user_func(array($className, 'personalConfig'), $personalForm); $options = $form->getValues(); $personalOptions = $personalForm->getValues(); if ($options && !$this->configHandle($pluginName, $options, true)) { self::configPlugin($pluginName, $options); } if ($personalOptions && !$this->personalConfigHandle($className, $personalOptions)) { self::configPlugin($pluginName, $personalOptions, true); } } else { $result = _t('<a href="%s">%s</a> 无法在此版本的typecho下正常工作', $info['link'], $info['title']); } /** 设置高亮 */ $this->widget('Widget_Notice')->highlight('plugin-' . $pluginName); if (isset($result) && is_string($result)) { $this->widget('Widget_Notice')->set($result, NULL, 'notice'); } else { $this->widget('Widget_Notice')->set(_t('插件已经被激活'), NULL, 'success'); } $this->response->goBack(); }
/** * 执行函数 * * @access public * @return void */ public function execute() { /** 列出插件目录 */ $pluginDirs = glob(__TYPECHO_ROOT_DIR__ . '/' . __TYPECHO_PLUGIN_DIR__ . '/*'); $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 { continue; } } if (file_exists($pluginFileName)) { $info = Typecho_Plugin::parseInfo($pluginFileName); $info['name'] = $pluginName; list($version, $build) = explode('/', Typecho_Common::VERSION); $info['dependence'] = Typecho_Plugin::checkDependence($build, $info['dependence']); /** 默认即插即用 */ $info['activated'] = true; if ($info['activate'] || $info['deactivate'] || $info['config'] || $info['personalConfig']) { $info['activated'] = isset($this->activatedPlugins[$pluginName]); if (isset($this->activatedPlugins[$pluginName])) { unset($this->activatedPlugins[$pluginName]); } } if (!is_bool($this->parameter->activated) || $info['activated'] == $this->parameter->activated) { $this->push($info); } } } } }
public function install() { $version = $this->request->get('version'); $plugin = $this->request->get('plugin'); $require = $this->request->get('require'); $require === '*' and $require = ''; $pluginPath = __TYPECHO_ROOT_DIR__ . __TYPECHO_PLUGIN_DIR__ . '/' . $plugin . '/'; $pluginBackupPath = __TYPECHO_ROOT_DIR__ . __TYPECHO_PLUGIN_DIR__ . '/_' . $plugin . '/'; $activatedPlugins = Typecho_Plugin::export(); $existed = false; $activated = false; $tempFile = __TYPECHO_ROOT_DIR__ . __TYPECHO_PLUGIN_DIR__ . '/.app_store/' . $plugin . '-' . $version . '.zip'; try { //检查版本 list(, $buildVersion) = explode('/', Typecho_Common::VERSION); if (!Typecho_Plugin::checkDependence($buildVersion, $require)) { throw new VersionNotMatchException('版本不匹配,无法安装.'); } //查看插件是否已经存在 //查看插件是否已经激活 if (file_exists($pluginPath)) { $existed = true; if (file_exists($pluginPath . 'Plugin.php') and isset($activatedPlugins['activated'][$plugin])) { $activated = true; } } //插件如果存在,则需要备份下,后面出错可以进行回滚 if ($existed or $activated) { file_exists($pluginBackupPath) and delete_files($pluginBackupPath) and @rmdir($pluginBackupPath); @rename($pluginPath, $pluginBackupPath); } //下载新插件zip包 $archive = http_get($this->server . 'archive/' . $plugin . '/' . str_replace(' ', '%20', $version)); if (!$archive) { throw new DownloadErrorException('下载插件包出错!'); } //保存文件 $fp = fopen($tempFile, 'w'); fwrite($fp, $archive); fclose($fp); //解压缩文件 $unzip = new Unzip(); //创建文件夹 @mkdir($pluginPath); $extractedFiles = $unzip->extract($tempFile, $pluginPath); if ($extractedFiles === false) { throw new UnzipErrorException('解压缩出错!'); } //OK,解压缩成功了 //删除备份文件 file_exists($pluginBackupPath) and delete_files($pluginBackupPath) and @rmdir($pluginBackupPath); //删除临时文件 @unlink($tempFile); //报告首长, 安装顺利完成 echo json_encode(array('status' => true, 'activated' => $activated)); } catch (VersionNotMatchException $e) { $e->responseJson(); } catch (DownloadErrorException $e) { //如果存在备份包,则进行回滚 file_exists($pluginBackupPath) and @rename($pluginBackupPath, $pluginPath); $e->responseJson(); } catch (UnzipErrorException $e) { //清理解锁压缩的废弃文件 file_exists($pluginPath) and delete_files($pluginPath) and @rmdir($pluginPath); //如果存在备份包,则进行回滚 file_exists($pluginBackupPath) and @rename($pluginBackupPath, $pluginPath); //删除临时文件 @unlink($tempFile); $e->responseJson(); } catch (Exception $e) { $error = new JsonableException($e->getMessage()); $error->responseJson(); } }
/** * 执行函数 * * @access public * @return void */ public function execute() { /** 列出插件目录 */ $pluginDirs = $this->getPlugins(); $this->parameter->setDefault(array('activated' => NULL)); /** 获取已启用插件 */ $plugins = Typecho_Plugin::export(); $this->activatedPlugins = $plugins['activated']; if (!empty($pluginDirs)) { 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; list($version, $build) = explode('/', Typecho_Common::VERSION); $info['dependence'] = Typecho_Plugin::checkDependence($build, $info['dependence']); /** 默认即插即用 */ $info['activated'] = true; if ($info['activate'] || $info['deactivate'] || $info['config'] || $info['personalConfig']) { $info['activated'] = isset($this->activatedPlugins[$pluginName]); if (isset($this->activatedPlugins[$pluginName])) { unset($this->activatedPlugins[$pluginName]); } } if ($info['activated'] == $this->parameter->activated) { $this->push($info); } } } } }