public function to_json() { return array_merge(parent::to_json(), array('codes' => $this->wp_error->get_error_codes(), 'messages' => $this->wp_error->get_error_messages())); }
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(); } }