123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433 |
- <?php
- namespace Widget;
- use Typecho\Common;
- use Typecho\Date;
- use Typecho\Db\Exception;
- use Typecho\Plugin;
- use Typecho\Widget;
- use Widget\Base\Contents;
- if (!defined('__TYPECHO_ROOT_DIR__')) {
- exit;
- }
- /**
- * 上传组件
- *
- * @author qining
- * @category typecho
- * @package Widget
- */
- class Upload extends Contents implements ActionInterface
- {
- //上传文件目录
- public const UPLOAD_DIR = '/usr/uploads';
- /**
- * 删除文件
- *
- * @param array $content 文件相关信息
- * @return bool
- */
- public static function deleteHandle(array $content): bool
- {
- $result = Plugin::factory(Upload::class)->trigger($hasDeleted)->deleteHandle($content);
- if ($hasDeleted) {
- return $result;
- }
- return @unlink(__TYPECHO_ROOT_DIR__ . '/' . $content['attachment']->path);
- }
- /**
- * 获取实际文件绝对访问路径
- *
- * @param array $content 文件相关信息
- * @return string
- */
- public static function attachmentHandle(array $content): string
- {
- $result = Plugin::factory(Upload::class)->trigger($hasPlugged)->attachmentHandle($content);
- if ($hasPlugged) {
- return $result;
- }
- $options = Options::alloc();
- return Common::url(
- $content['attachment']->path,
- defined('__TYPECHO_UPLOAD_URL__') ? __TYPECHO_UPLOAD_URL__ : $options->siteUrl
- );
- }
- /**
- * 获取实际文件数据
- *
- * @param array $content
- * @return string
- */
- public static function attachmentDataHandle(array $content): string
- {
- $result = Plugin::factory(Upload::class)->trigger($hasPlugged)->attachmentDataHandle($content);
- if ($hasPlugged) {
- return $result;
- }
- return file_get_contents(
- Common::url(
- $content['attachment']->path,
- defined('__TYPECHO_UPLOAD_ROOT_DIR__') ? __TYPECHO_UPLOAD_ROOT_DIR__ : __TYPECHO_ROOT_DIR__
- )
- );
- }
- /**
- * 初始化函数
- */
- public function action()
- {
- if ($this->user->pass('contributor', true) && $this->request->isPost()) {
- $this->security->protect();
- if ($this->request->is('do=modify&cid')) {
- $this->modify();
- } else {
- $this->upload();
- }
- } else {
- $this->response->setStatus(403);
- }
- }
- /**
- * 执行升级程序
- *
- * @throws Exception
- */
- public function modify()
- {
- if (!empty($_FILES)) {
- $file = array_pop($_FILES);
- if (0 == $file['error'] && is_uploaded_file($file['tmp_name'])) {
- $this->db->fetchRow($this->select()->where('table.contents.cid = ?', $this->request->filter('int')->cid)
- ->where('table.contents.type = ?', 'attachment'), [$this, 'push']);
- if (!$this->have()) {
- $this->response->setStatus(404);
- exit;
- }
- if (!$this->allow('edit')) {
- $this->response->setStatus(403);
- exit;
- }
- // xhr的send无法支持utf8
- if ($this->request->isAjax()) {
- $file['name'] = urldecode($file['name']);
- }
- $result = self::modifyHandle($this->row, $file);
- if (false !== $result) {
- self::pluginHandle()->beforeModify($result);
- $this->update([
- 'text' => serialize($result)
- ], $this->db->sql()->where('cid = ?', $this->cid));
- $this->db->fetchRow($this->select()->where('table.contents.cid = ?', $this->cid)
- ->where('table.contents.type = ?', 'attachment'), [$this, 'push']);
- /** 增加插件接口 */
- self::pluginHandle()->modify($this);
- $this->response->throwJson([$this->attachment->url, [
- 'cid' => $this->cid,
- 'title' => $this->attachment->name,
- 'type' => $this->attachment->type,
- 'size' => $this->attachment->size,
- 'bytes' => number_format(ceil($this->attachment->size / 1024)) . ' Kb',
- 'isImage' => $this->attachment->isImage,
- 'url' => $this->attachment->url,
- 'permalink' => $this->permalink
- ]]);
- }
- }
- }
- $this->response->throwJson(false);
- }
- /**
- * 修改文件处理函数,如果需要实现自己的文件哈希或者特殊的文件系统,请在options表里把modifyHandle改成自己的函数
- *
- * @param array $content 老文件
- * @param array $file 新上传的文件
- * @return mixed
- */
- public static function modifyHandle(array $content, array $file)
- {
- if (empty($file['name'])) {
- return false;
- }
- $result = self::pluginHandle()->trigger($hasModified)->modifyHandle($content, $file);
- if ($hasModified) {
- return $result;
- }
- $ext = self::getSafeName($file['name']);
- if ($content['attachment']->type != $ext) {
- return false;
- }
- $path = Common::url(
- $content['attachment']->path,
- defined('__TYPECHO_UPLOAD_ROOT_DIR__') ? __TYPECHO_UPLOAD_ROOT_DIR__ : __TYPECHO_ROOT_DIR__
- );
- $dir = dirname($path);
- //创建上传目录
- if (!is_dir($dir)) {
- if (!self::makeUploadDir($dir)) {
- return false;
- }
- }
- if (isset($file['tmp_name'])) {
- @unlink($path);
- //移动上传文件
- if (!@move_uploaded_file($file['tmp_name'], $path)) {
- return false;
- }
- } elseif (isset($file['bytes'])) {
- @unlink($path);
- //直接写入文件
- if (!file_put_contents($path, $file['bytes'])) {
- return false;
- }
- } elseif (isset($file['bits'])) {
- @unlink($path);
- //直接写入文件
- if (!file_put_contents($path, $file['bits'])) {
- return false;
- }
- } else {
- return false;
- }
- if (!isset($file['size'])) {
- $file['size'] = filesize($path);
- }
- //返回相对存储路径
- return [
- 'name' => $content['attachment']->name,
- 'path' => $content['attachment']->path,
- 'size' => $file['size'],
- 'type' => $content['attachment']->type,
- 'mime' => $content['attachment']->mime
- ];
- }
- /**
- * 获取安全的文件名
- *
- * @param string $name
- * @return string
- */
- private static function getSafeName(string &$name): string
- {
- $name = str_replace(['"', '<', '>'], '', $name);
- $name = str_replace('\\', '/', $name);
- $name = false === strpos($name, '/') ? ('a' . $name) : str_replace('/', '/a', $name);
- $info = pathinfo($name);
- $name = substr($info['basename'], 1);
- return isset($info['extension']) ? strtolower($info['extension']) : '';
- }
- /**
- * 创建上传路径
- *
- * @param string $path 路径
- * @return boolean
- */
- private static function makeUploadDir(string $path): bool
- {
- $path = preg_replace("/\\\+/", '/', $path);
- $current = rtrim($path, '/');
- $last = $current;
- while (!is_dir($current) && false !== strpos($path, '/')) {
- $last = $current;
- $current = dirname($current);
- }
- if ($last == $current) {
- return true;
- }
- if (!@mkdir($last, 0755)) {
- return false;
- }
- return self::makeUploadDir($path);
- }
- /**
- * 执行升级程序
- *
- * @throws Exception
- */
- public function upload()
- {
- if (!empty($_FILES)) {
- $file = array_pop($_FILES);
- if (0 == $file['error'] && is_uploaded_file($file['tmp_name'])) {
- // xhr的send无法支持utf8
- if ($this->request->isAjax()) {
- $file['name'] = urldecode($file['name']);
- }
- $result = self::uploadHandle($file);
- if (false !== $result) {
- self::pluginHandle()->beforeUpload($result);
- $struct = [
- 'title' => $result['name'],
- 'slug' => $result['name'],
- 'type' => 'attachment',
- 'status' => 'publish',
- 'text' => serialize($result),
- 'allowComment' => 1,
- 'allowPing' => 0,
- 'allowFeed' => 1
- ];
- if (isset($this->request->cid)) {
- $cid = $this->request->filter('int')->cid;
- if ($this->isWriteable($this->db->sql()->where('cid = ?', $cid))) {
- $struct['parent'] = $cid;
- }
- }
- $insertId = $this->insert($struct);
- $this->db->fetchRow($this->select()->where('table.contents.cid = ?', $insertId)
- ->where('table.contents.type = ?', 'attachment'), [$this, 'push']);
- /** 增加插件接口 */
- self::pluginHandle()->upload($this);
- $this->response->throwJson([$this->attachment->url, [
- 'cid' => $insertId,
- 'title' => $this->attachment->name,
- 'type' => $this->attachment->type,
- 'size' => $this->attachment->size,
- 'bytes' => number_format(ceil($this->attachment->size / 1024)) . ' Kb',
- 'isImage' => $this->attachment->isImage,
- 'url' => $this->attachment->url,
- 'permalink' => $this->permalink
- ]]);
- }
- }
- }
- $this->response->throwJson(false);
- }
- /**
- * 上传文件处理函数,如果需要实现自己的文件哈希或者特殊的文件系统,请在options表里把uploadHandle改成自己的函数
- *
- * @param array $file 上传的文件
- * @return mixed
- */
- public static function uploadHandle(array $file)
- {
- if (empty($file['name'])) {
- return false;
- }
- $result = self::pluginHandle()->trigger($hasUploaded)->uploadHandle($file);
- if ($hasUploaded) {
- return $result;
- }
- $ext = self::getSafeName($file['name']);
- if (!self::checkFileType($ext)) {
- return false;
- }
- $date = new Date();
- $path = Common::url(
- defined('__TYPECHO_UPLOAD_DIR__') ? __TYPECHO_UPLOAD_DIR__ : self::UPLOAD_DIR,
- defined('__TYPECHO_UPLOAD_ROOT_DIR__') ? __TYPECHO_UPLOAD_ROOT_DIR__ : __TYPECHO_ROOT_DIR__
- ) . '/' . $date->year . '/' . $date->month;
- //创建上传目录
- if (!is_dir($path)) {
- if (!self::makeUploadDir($path)) {
- return false;
- }
- }
- //获取文件名
- $fileName = sprintf('%u', crc32(uniqid())) . '.' . $ext;
- $path = $path . '/' . $fileName;
- if (isset($file['tmp_name'])) {
- //移动上传文件
- if (!@move_uploaded_file($file['tmp_name'], $path)) {
- return false;
- }
- } elseif (isset($file['bytes'])) {
- //直接写入文件
- if (!file_put_contents($path, $file['bytes'])) {
- return false;
- }
- } elseif (isset($file['bits'])) {
- //直接写入文件
- if (!file_put_contents($path, $file['bits'])) {
- return false;
- }
- } else {
- return false;
- }
- if (!isset($file['size'])) {
- $file['size'] = filesize($path);
- }
- //返回相对存储路径
- return [
- 'name' => $file['name'],
- 'path' => (defined('__TYPECHO_UPLOAD_DIR__') ? __TYPECHO_UPLOAD_DIR__ : self::UPLOAD_DIR)
- . '/' . $date->year . '/' . $date->month . '/' . $fileName,
- 'size' => $file['size'],
- 'type' => $ext,
- 'mime' => Common::mimeContentType($path)
- ];
- }
- /**
- * 检查文件名
- *
- * @access private
- * @param string $ext 扩展名
- * @return boolean
- */
- public static function checkFileType(string $ext): bool
- {
- $options = Options::alloc();
- return in_array($ext, $options->allowedAttachmentTypes);
- }
- }
|