|
| 1 | +<?php |
| 2 | + |
| 3 | +class Export_Action extends Typecho_Widget implements Widget_Interface_Do |
| 4 | +{ |
| 5 | + /** |
| 6 | + * 导出动作 |
| 7 | + * |
| 8 | + * @access public |
| 9 | + * @return void |
| 10 | + */ |
| 11 | + public function doExport() |
| 12 | + { |
| 13 | + // 获取POST值 |
| 14 | + $tableSelect = $this->request->get('tableSelect'); |
| 15 | + |
| 16 | + // 获取SQL语句 |
| 17 | + $content = $this->getSql($tableSelect); |
| 18 | + |
| 19 | + // 获取备份目录并设置文件 |
| 20 | + $config = Typecho_Widget::widget('Widget_Options')->plugin('Export'); |
| 21 | + $path = __TYPECHO_ROOT_DIR__ . '/' . trim($config->path, '/') . '/'; |
| 22 | + $fileName = $this->request->get('fileName'); |
| 23 | + $file = $path . $fileName; |
| 24 | + |
| 25 | + if (!empty($fileName)) { |
| 26 | + if ((is_dir($path) || @mkdir($path, 0777)) && is_writable($path)) { |
| 27 | + $handle = fopen($file, 'wb'); |
| 28 | + if ($handle && fwrite($handle, $content)) { |
| 29 | + fclose($handle); |
| 30 | + $this->widget('Widget_Notice')->set(_t('备份文件 ' . $fileName . ' 已创建'), 'success'); |
| 31 | + } else { |
| 32 | + $this->widget('Widget_Notice')->set(_t('备份文件创建失败,请检查目录权限'), 'error'); |
| 33 | + } |
| 34 | + } else { |
| 35 | + $this->widget('Widget_Notice')->set(_t('文件夹创建失败或目录权限限制'), 'error'); |
| 36 | + } |
| 37 | + } else { |
| 38 | + $this->widget('Widget_Notice')->set(_t('备份文件名不能为空'), 'error'); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * 导入动作 |
| 44 | + * |
| 45 | + * @access public |
| 46 | + * @return void |
| 47 | + */ |
| 48 | + public function doImport() |
| 49 | + { |
| 50 | + // 暂时不写导入功能了 |
| 51 | + // $this->response->goBack(); |
| 52 | + // die(); |
| 53 | + |
| 54 | + // 数据库对象 |
| 55 | + $db = Typecho_Db::get(); |
| 56 | + // 表前缀 |
| 57 | + $dbPrefix = $db->getPrefix(); |
| 58 | + $prefixLength = strlen($dbPrefix); |
| 59 | + // 数据表 |
| 60 | + $dropTable = 'DROP TABLE'; |
| 61 | + $resource = $db->fetchAll($db->query('SHOW TABLES')); |
| 62 | + foreach ($resource as $value) { |
| 63 | + foreach ($value as $tableName) { |
| 64 | + if ($dbPrefix == substr($tableName, 0, $prefixLength)) { |
| 65 | + $dropTable .= ' `' . $tableName . '`, '; |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + $dropTable = rtrim($dropTable, ', ') . ';'; |
| 70 | + |
| 71 | + // 获取备份目录并设置文件 |
| 72 | + $config = Typecho_Widget::widget('Widget_Options')->plugin('Export'); |
| 73 | + $path = __TYPECHO_ROOT_DIR__ . '/' . trim($config->path, '/') . '/'; |
| 74 | + |
| 75 | + $bid = $this->request->get('bid'); |
| 76 | + $deleteCount = 0; |
| 77 | + $scripts = ''; |
| 78 | + |
| 79 | + if ($bid) { |
| 80 | + $imports = is_array($bid) ? $bid : array($bid); |
| 81 | + |
| 82 | + foreach ($imports as $import) { |
| 83 | + $scripts .= file_get_contents($path . $import); |
| 84 | + $deleteCount ++; |
| 85 | + } |
| 86 | + |
| 87 | + // 删除存在的同名数据表 |
| 88 | + $db->query($dropTable, Typecho_Db::WRITE); |
| 89 | + |
| 90 | + $scripts = explode(";\r\n", $scripts); |
| 91 | + foreach ($scripts as $script) { |
| 92 | + $script = trim($script); |
| 93 | + if ($script) { |
| 94 | + $db->query($script, Typecho_Db::WRITE); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + $this->widget('Widget_Notice')->set($deleteCount > 0 ? _t('备份已经被导入') : _t('没有备份被导入'), |
| 100 | + $deleteCount > 0 ? 'success' : 'notice'); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * 删除备份 |
| 105 | + * |
| 106 | + * @access public |
| 107 | + * @return void |
| 108 | + */ |
| 109 | + public function doDelete() |
| 110 | + { |
| 111 | + // 获取备份目录并设置文件 |
| 112 | + $config = Typecho_Widget::widget('Widget_Options')->plugin('Export'); |
| 113 | + $path = __TYPECHO_ROOT_DIR__ . '/' . trim($config->path, '/') . '/'; |
| 114 | + |
| 115 | + $bid = $this->request->get('bid'); |
| 116 | + $deleteCount = 0; |
| 117 | + |
| 118 | + if ($bid) { |
| 119 | + $backups = is_array($bid) ? $bid : array($bid); |
| 120 | + |
| 121 | + foreach ($backups as $backup) { |
| 122 | + @unlink($path . $backup); |
| 123 | + $deleteCount ++; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + $this->widget('Widget_Notice')->set($deleteCount > 0 ? _t('备份已经被删除') : _t('没有备份被删除'), |
| 128 | + $deleteCount > 0 ? 'success' : 'notice'); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * 构建SQL语句 |
| 133 | + * |
| 134 | + * @access public |
| 135 | + * @param array $tables 数据表数组 |
| 136 | + * @return string $sql SQL语句 |
| 137 | + */ |
| 138 | + public function getSql(array $tables) |
| 139 | + { |
| 140 | + // 数据库对象 |
| 141 | + $db = Typecho_Db::get(); |
| 142 | + |
| 143 | + // SQL语句 |
| 144 | + $sql = '-- Typecho Backup SQL' . "\r\n" |
| 145 | + . '-- 程序版本: ' . Typecho_Common::VERSION . "\r\n" |
| 146 | + . '--' . "\r\n" |
| 147 | + . '-- 备份工具: Export' . "\r\n" |
| 148 | + . '-- 插件作者: ShingChi' . "\r\n" |
| 149 | + . '-- 主页链接: http://lcz.me' . "\r\n" |
| 150 | + . '-- 生成日期: ' . date('Y 年 m 月 d 日', Typecho_Date::gmtTime()) . "\r\n\r\n"; |
| 151 | + |
| 152 | + // 循环构建每张表的SQL语句 |
| 153 | + foreach ($tables as $table) { |
| 154 | + // 创建表语句 |
| 155 | + $createSql = ''; |
| 156 | + // 插入记录语句 |
| 157 | + $insertSql = ''; |
| 158 | + |
| 159 | + // 创建表注释 |
| 160 | + $createSql .= '-- --------------------------------------------------------' . "\r\n\r\n" |
| 161 | + . '--' . "\r\n" |
| 162 | + . '-- 表的结构 `' . $table . "`\r\n" |
| 163 | + . '--' . "\r\n\r\n"; |
| 164 | + |
| 165 | + /* 表结构 */ |
| 166 | + $replace = 'CREATE TABLE IF NOT EXISTS'; |
| 167 | + $showTable = $db->fetchRow($db->query('SHOW CREATE TABLE ' . $table)); |
| 168 | + $createTable = $showTable['Create Table']; |
| 169 | + $createSql .= str_replace('CREATE TABLE', $replace, $createTable) . ';' . "\r\n\r\n"; |
| 170 | + |
| 171 | + /* 表记录 */ |
| 172 | + $rows = $db->fetchAll($db->select()->from($table)); |
| 173 | + if ($rows) { |
| 174 | + // 字段组合SQL语句 |
| 175 | + $fieldText = ''; |
| 176 | + |
| 177 | + // 值的组合SQL语句 |
| 178 | + $recordText = ''; |
| 179 | + |
| 180 | + // 所有记录 |
| 181 | + $records = array(); |
| 182 | + |
| 183 | + // 插入注释 |
| 184 | + $insertSql .= '--' . "\r\n" |
| 185 | + . '-- 转存表中的数据 `' . $table . "`\r\n" |
| 186 | + . '--' . "\r\n\r\n"; |
| 187 | + |
| 188 | + // 组合字段语句 |
| 189 | + foreach ($rows[0] as $key => $value) { |
| 190 | + $fieldText .= '`' . $key . '`, '; |
| 191 | + } |
| 192 | + $fieldText = rtrim($fieldText, ', '); |
| 193 | + $insertSql .= 'INSERT INTO ' . $table . ' (' . $fieldText . ') VALUES' . "\r\n"; |
| 194 | + |
| 195 | + // 组合一条记录的语法 |
| 196 | + foreach ($rows as $k => $row) { |
| 197 | + $records[$k] = ''; |
| 198 | + foreach ($row as $record) { |
| 199 | + $records[$k] .= isset($record) ? '\'' . mysql_escape_string($record) . '\', ' : 'NULL, '; |
| 200 | + } |
| 201 | + $records[$k] = rtrim($records[$k], ', '); |
| 202 | + } |
| 203 | + |
| 204 | + // 组合所有记录的语法 |
| 205 | + foreach ($records as $val) { |
| 206 | + $recordText .= '(' . $val . '),' . "\r\n"; |
| 207 | + } |
| 208 | + $recordText = rtrim($recordText, ",\r\n") . ";\r\n\r\n"; |
| 209 | + $insertSql .= $recordText; |
| 210 | + } |
| 211 | + |
| 212 | + $sql .= $createSql . $insertSql; |
| 213 | + } |
| 214 | + |
| 215 | + return $sql; |
| 216 | + } |
| 217 | + |
| 218 | + /** |
| 219 | + * 绑定动作 |
| 220 | + * |
| 221 | + * @access public |
| 222 | + * @return void |
| 223 | + */ |
| 224 | + public function action() |
| 225 | + { |
| 226 | + $this->widget('Widget_User')->pass('administrator'); |
| 227 | + $this->on($this->request->is('export'))->doExport(); |
| 228 | + $this->on($this->request->is('import'))->doImport(); |
| 229 | + $this->on($this->request->is('delete'))->doDelete(); |
| 230 | + $this->response->goBack(); |
| 231 | + } |
| 232 | +} |
0 commit comments