/** * 编辑外观文件 * * @access public * @param string $theme 外观名称 * @param string $file 文件名 * @return void * @throws Typecho_Widget_Exception */ public function editThemeFile($theme, $file) { $path = $this->options->themeFile($theme, $file); if (file_exists($path) && is_writeable($path) && !Typecho_Common::isAppEngine() && (!defined('__TYPECHO_THEME_WRITEABLE__') || __TYPECHO_THEME_WRITEABLE__)) { $handle = fopen($path, 'wb'); if ($handle && fwrite($handle, $this->request->content)) { fclose($handle); $this->widget('Widget_Notice')->set(_t("文件 %s 的更改已经保存", $file), 'success'); } else { $this->widget('Widget_Notice')->set(_t("文件 %s 无法被写入", $file), 'error'); } $this->response->goBack(); } else { throw new Typecho_Widget_Exception(_t('您编辑的文件不存在')); } }
/** * 检测是否可以rewrite * * @access public * @param string $value 是否打开rewrite * @return void */ public function checkRewrite($value) { if ($value) { $this->user->pass('administrator'); /** 首先直接请求远程地址验证 */ $client = Typecho_Http_Client::get(); $hasWrote = false; if (!file_exists(__TYPECHO_ROOT_DIR__ . '/.htaccess') && !Typecho_Common::isAppEngine()) { if (is_writeable(__TYPECHO_ROOT_DIR__)) { $parsed = parse_url($this->options->siteUrl); $basePath = empty($parsed['path']) ? '/' : $parsed['path']; $basePath = rtrim($basePath, '/') . '/'; $hasWrote = file_put_contents(__TYPECHO_ROOT_DIR__ . '/.htaccess', "<IfModule mod_rewrite.c>\nRewriteEngine On\nRewriteBase {$basePath}\nRewriteCond %{REQUEST_FILENAME} !-f\nRewriteCond %{REQUEST_FILENAME} !-d\nRewriteRule ^(.*)\$ {$basePath}index.php/\$1 [L]\n</IfModule>"); } } try { if ($client) { /** 发送一个rewrite地址请求 */ $client->setData(array('do' => 'remoteCallback'))->setHeader('User-Agent', $this->options->generator)->send(Typecho_Common::url('/action/ajax', $this->options->siteUrl)); if (200 == $client->getResponseStatus() && 'OK' == $client->getResponseBody()) { return true; } } if (false !== $hasWrote) { @unlink(__TYPECHO_ROOT_DIR__ . '/.htaccess'); //增强兼容性,使用wordpress的redirect式rewrite规则,虽然效率有点地下,但是对fastcgi模式兼容性较好 $hasWrote = file_put_contents(__TYPECHO_ROOT_DIR__ . '/.htaccess', "<IfModule mod_rewrite.c>\nRewriteEngine On\nRewriteBase {$basePath}\nRewriteCond %{REQUEST_FILENAME} !-f\nRewriteCond %{REQUEST_FILENAME} !-d\nRewriteRule . {$basePath}index.php [L]\n</IfModule>"); //再次进行验证 $client = Typecho_Http_Client::get(); if ($client) { /** 发送一个rewrite地址请求 */ $client->setData(array('do' => 'remoteCallback'))->setHeader('User-Agent', $this->options->generator)->send(Typecho_Common::url('/action/ajax', $this->options->siteUrl)); if (200 == $client->getResponseStatus() && 'OK' == $client->getResponseBody()) { return true; } } unlink(__TYPECHO_ROOT_DIR__ . '/.htaccess'); } } catch (Typecho_Http_Client_Exception $e) { if (false !== $hasWrote) { @unlink(__TYPECHO_ROOT_DIR__ . '/.htaccess'); } return false; } return false; } else { if (file_exists(__TYPECHO_ROOT_DIR__ . '/.htaccess')) { @unlink(__TYPECHO_ROOT_DIR__ . '/.htaccess'); } } return true; }
/** * 上传头像 * @param array $file * @return boolean|multitype:string unknown number Ambigous <string, unknown> Ambigous <Ambigous, string, mixed> */ private function uploadAvatar($file) { if (empty($file['name'])) { return false; } $ext = $this->getSafeName($file['name']); if (!Widget_Upload::checkFileType(strtolower($ext)) || Typecho_Common::isAppEngine()) { return false; } $options = Typecho_Widget::widget('Widget_Options'); $path = Widget_Common::getAvatarPath($this->user->uid); $realPath = Typecho_Common::url($path, defined('__TYPECHO_UPLOAD_ROOT_DIR__') ? __TYPECHO_UPLOAD_ROOT_DIR__ : __TYPECHO_ROOT_DIR__); //创建上传目录 if (!is_dir($realPath)) { if (!$this->makeAvatarDir($realPath)) { return false; } } //获取文件名 $fileName = $this->user->uid . '.tmp.' . $ext; $realPath = $realPath . '/' . $fileName; if (isset($file['tmp_name'])) { //移动上传文件 if (!@move_uploaded_file($file['tmp_name'], $realPath)) { return false; } } else { if (isset($file['bytes'])) { //直接写入文件 if (!file_put_contents($realPath, $file['bytes'])) { return false; } } else { return false; } } if (!isset($file['size'])) { $file['size'] = filesize($realPath); } //返回相对存储路径 return array('name' => $file['name'], 'path' => $path, 'file' => $path . $fileName, 'size' => $file['size'], 'type' => $ext, 'mime' => Typecho_Common::mimeContentType($realPath)); }
/** * 获取文件是否可读 * * @access public * @return string */ public function currentIsWriteable() { return is_writeable($this->widget('Widget_Options')->themeFile($this->_currentTheme, $this->_currentFile)) && !Typecho_Common::isAppEngine() && (!defined('__TYPECHO_THEME_WRITEABLE__') || __TYPECHO_THEME_WRITEABLE__); }
} catch (Typecho_Db_Exception $e) { $success = false; echo '<p class="message error">' . _t('安装程序捕捉到以下错误: " %s ". 程序被终止, 请检查您的配置信息.', $e->getMessage()) . '</p>'; } } if ($success) { Typecho_Cookie::set('__typecho_config', base64_encode(serialize(array_merge(array('prefix' => _r('dbPrefix'), 'userName' => _r('userName'), 'userPassword' => _r('userPassword'), 'userMail' => _r('userMail'), 'adapter' => $adapter, 'siteUrl' => _r('userUrl')), $dbConfig)))); if (_r('created')) { header('Location: ./install.php?start'); exit; } /** 初始化配置文件 */ $lines = array_slice(file(__FILE__), 0, 52); $lines[] = "\n/** 定义数据库参数 */\n\$db = new Typecho_Db('{$adapter}', '" . _r('dbPrefix') . "');\n\$db->addServer(" . (empty($config) ? var_export($dbConfig, true) : $config) . ", Typecho_Db::READ | Typecho_Db::WRITE);\nTypecho_Db::set(\$db);\n"; $contents = implode('', $lines); if (!Typecho_Common::isAppEngine()) { @file_put_contents('./config.inc.php', $contents); @file_put_contents('./var/config.inc.php', $contents); } // 创建一个用于标识的临时文件 $_SESSION['typecho'] = 1; if (!file_exists('./config.inc.php')) { ?> <div class="message notice"><p><?php _e('安装程序无法自动创建 <strong>config.inc.php</strong> 文件'); ?> <br /> <?php _e('您可以在网站根目录下手动创建 <strong>config.inc.php</strong> 文件, 并复制如下代码至其中'); ?> </p>
/** * 图片上传处理 * * @access public * @param array $file 上传的文件 * @return mixed */ public static function uploadHandle($file) { if (empty($file['image_upload'])) { return false; } $imgname = preg_split("(\\/|\\|:)", $file['image_upload']); $file['image_upload'] = array_pop($imgname); //扩展名 $ext = Typecho_Widget_Upload::getSafeName($file['image_upload']); if (!Typecho_Widget_Upload::checkFileType($ext) || Typecho_Common::isAppEngine()) { return false; } //创建上传目录 $imgdir = Typecho_Widget::widget('Widget_Options')->plugin('HuifengMembers')->imagepath; if (!is_dir($imgdir)) { Typecho_Widget_Upload::makeUploadDir($imgdir); } //获取文件名 $imgname = sprintf('%u', crc32(uniqid())) . '.' . $ext; $imgpath = $imgdir . $imgname; if (!isset($file['tmp_name'])) { return false; } //本地上传 if (!@move_uploaded_file($file['tmp_name'], $imgpath)) { return false; } return array('image_upload' => $imgname, 'title' => $file['image_upload'], 'size' => $file['size']); }
/** * 删除文件 * * @access public * @param array $content 文件相关信息 * @return string */ public static function deleteHandle(array $content) { $result = Typecho_Plugin::factory('Widget_Upload')->trigger($hasDeleted)->deleteHandle($content); if ($hasDeleted) { return $result; } return !Typecho_Common::isAppEngine() && @unlink(__TYPECHO_ROOT_DIR__ . '/' . $content['attachment']->path); }
/** * 图片删除处理 * * @access public * @param string $imgname 图片名称 * @param string $path 附件源路径 * @param string $url 附件源地址 * @return string */ public static function removehandle($imgname, $path = NULL, $url = NULL) { $options = Helper::options(); $settings = $options->plugin('HighSlide'); $imgdir = self::filedata($path, $url)->dir; $imgpath = $imgdir . $imgname; $thumbpath = $imgdir . 'thumb_' . $imgname; if (strpos($url, $options->siteUrl) === false) { //本地删除 if ($settings->storage == 'local') { if (!file_exists($imgpath)) { return false; } if (file_exists($thumbpath)) { unlink($thumbpath); } return !Typecho_Common::isAppEngine() && @unlink($imgpath); } //七牛删除 if ($settings->storage == 'qiniu') { self::qiniuset($settings->qiniuaccesskey, $settings->qiniusecretkey); $client = new Qiniu_MacHttpClient(null); Qiniu_RS_Delete($client, $settings->qiniubucket, $thumbpath); Qiniu_RS_Delete($client, $settings->qiniubucket, $imgpath); return true; } //又拍云删除 if ($settings->storage == 'upyun') { $upyun = self::upyunset(); $upyun->delete($thumbpath); return $upyun->delete($imgpath); } //百度BCS删除 if ($settings->storage == 'bcs') { $bcs = self::bcsset(); $bcs->delete_object($settings->bcsbucket, $thumbpath); $bcs->delete_object($settings->bcsbucket, $imgpath); return true; } //本地附件源删除 } else { if (!file_exists($imgpath)) { return false; } return @unlink($imgpath); } }
/** * 修改文件处理函数 * * @access public * @param array $content 老文件 * @param array $file 新上传的文件 * @return mixed */ public static function modifyHandle($content, $file) { if (empty($file['name'])) { return false; } //获取扩展名 $ext = self::getSafeName($file['name']); if ($content['attachment']->type != $ext || Typecho_Common::isAppEngine()) { return false; } //获取文件路径 $path = $content['attachment']->path; $uploadfile = isset($file['tmp_name']) ? $file['tmp_name'] : (isset($file['bits']) ? $file['bits'] : FALSE); if ($uploadfile == FALSE) { return false; } else { //修改文件 $upyun = self::upyunInit(); $fh = fopen($uploadfile, 'rb'); $upyun->writeFile($path, $fh, TRUE); fclose($fh); } if (!isset($file['size'])) { $fileInfo = $upyun->getFileInfo($path); $file['size'] = $fileInfo['x-upyun-file-size']; } //返回相对存储路径 return array('name' => $content['attachment']->name, 'path' => $content['attachment']->path, 'size' => $file['size'], 'type' => $content['attachment']->type, 'mime' => $content['attachment']->mime); }