Beispiel #1
0
 public static function uploadFile($file, $content = null)
 {
     // 获取上传文件
     if (empty($file['name'])) {
         return false;
     }
     // 校验扩展名
     $part = explode('.', $file['name']);
     $ext = ($length = count($part)) > 1 ? strtolower($part[$length - 1]) : '';
     if (!Widget_Upload::checkFileType($ext)) {
         return false;
     }
     // 获取插件配置
     $option = self::getConfig();
     $date = new Typecho_Date(Typecho_Widget::widget('Widget_Options')->gmtTime);
     // 保存位置
     $savepath = preg_replace(array('/\\{year\\}/', '/\\{month\\}/', '/\\{day\\}/'), array($date->year, $date->month, $date->day), $option->savepath);
     $savename = $savepath . sprintf('%u', crc32(uniqid())) . '.' . $ext;
     if (isset($content)) {
         $savename = $content['attachment']->path;
         self::deleteFile($savename);
     }
     // 上传文件
     $filename = $file['tmp_name'];
     if (!isset($filename)) {
         return false;
     }
     // 初始化 SDK
     self::initSDK($option->accesskey, $option->sercetkey);
     // 上传凭证
     $policy = new Qiniu_RS_PutPolicy($option->bucket);
     $token = $policy->Token(null);
     $extra = new Qiniu_PutExtra();
     $extra->Crc32 = 1;
     // 上传
     list($result, $error) = Qiniu_PutFile($token, $savename, $filename, $extra);
     if ($error == null) {
         return array('name' => $file['name'], 'path' => $savename, 'size' => $file['size'], 'type' => $ext, 'mime' => Typecho_Common::mimeContentType($savename));
     } else {
         return false;
     }
 }
Beispiel #2
0
 /**
  * 通用过滤器
  *
  * @access public
  * @param array $value 需要过滤的行数据
  * @return array
  */
 public function filter(array $value)
 {
     /** 取出所有分类 */
     $value['categories'] = $this->db->fetchAll($this->db->select()->from('table.metas')->join('table.relationships', 'table.relationships.mid = table.metas.mid')->where('table.relationships.cid = ?', $value['cid'])->where('table.metas.type = ?', 'category')->order('table.metas.order', Typecho_Db::SORT_ASC), array($this->widget('Widget_Abstract_Metas'), 'filter'));
     /** 取出第一个分类作为slug条件 */
     $value['category'] = current(Typecho_Common::arrayFlatten($value['categories'], 'slug'));
     $value['date'] = new Typecho_Date($value['created']);
     /** 生成日期 */
     $value['year'] = $value['date']->year;
     $value['month'] = $value['date']->month;
     $value['day'] = $value['date']->day;
     /** 生成访问权限 */
     $value['hidden'] = false;
     /** 获取路由类型并判断此类型在路由表中是否存在 */
     $type = $value['type'];
     $routeExists = NULL != Typecho_Router::get($type);
     $tmpSlug = $value['slug'];
     $tmpCategory = $value['category'];
     $value['slug'] = urlencode($value['slug']);
     $value['category'] = urlencode($value['category']);
     /** 生成静态路径 */
     $value['pathinfo'] = $routeExists ? Typecho_Router::url($type, $value) : '#';
     /** 生成静态链接 */
     $value['permalink'] = Typecho_Common::url($value['pathinfo'], $this->options->index);
     /** 处理附件 */
     if ('attachment' == $type) {
         $content = @unserialize($value['text']);
         //增加数据信息
         $value['attachment'] = new Typecho_Config($content);
         $value['attachment']->isImage = in_array($content['type'], array('jpg', 'jpeg', 'gif', 'png', 'tiff', 'bmp'));
         $value['attachment']->url = Widget_Upload::attachmentHandle($value);
         if ($value['attachment']->isImage) {
             $value['text'] = '<img src="' . $value['attachment']->url . '" alt="' . $value['title'] . '" />';
         } else {
             $value['text'] = '<a href="' . $value['attachment']->url . '" title="' . $value['title'] . '">' . $value['title'] . '</a>';
         }
     }
     /** 处理Markdown **/
     $value['isMarkdown'] = 0 === strpos($value['text'], '<!--markdown-->');
     if ($value['isMarkdown']) {
         $value['text'] = substr($value['text'], 15);
     }
     /** 生成聚合链接 */
     /** RSS 2.0 */
     $value['feedUrl'] = $routeExists ? Typecho_Router::url($type, $value, $this->options->feedUrl) : '#';
     /** RSS 1.0 */
     $value['feedRssUrl'] = $routeExists ? Typecho_Router::url($type, $value, $this->options->feedRssUrl) : '#';
     /** ATOM 1.0 */
     $value['feedAtomUrl'] = $routeExists ? Typecho_Router::url($type, $value, $this->options->feedAtomUrl) : '#';
     $value['slug'] = $tmpSlug;
     $value['category'] = $tmpCategory;
     /** 处理密码保护流程 */
     if (!empty($value['password']) && $value['password'] != $this->request->protectPassword && $value['authorId'] != $this->user->uid && !$this->user->pass('editor', true)) {
         $value['hidden'] = true;
         /** 抛出错误 */
         if ($this->request->isPost() && isset($this->request->protectPassword)) {
             throw new Typecho_Widget_Exception(_t('对不起,您输入的密码错误'), 403);
         }
     }
     $value = $this->pluginHandle(__CLASS__)->filter($value, $this);
     /** 如果访问权限被禁止 */
     if ($value['hidden']) {
         $value['text'] = '<form class="protected" action="' . $value['permalink'] . '" method="post">' . '<p class="word">' . _t('请输入密码访问') . '</p>' . '<p><input type="password" class="text" name="protectPassword" />
         <input type="submit" class="submit" value="' . _t('提交') . '" /></p>' . '</form>';
         $value['title'] = _t('此内容被密码保护');
         $value['tags'] = array();
         $value['commentsNum'] = 0;
     }
     return $value;
 }
Beispiel #3
0
 /**
  * mwNewMediaObject
  *
  * @param int $blogId
  * @param string $userName
  * @param string $password
  * @param mixed $data
  * @access public
  * @return void
  */
 public function mwNewMediaObject($blogId, $userName, $password, $data)
 {
     if (!$this->checkAccess($userName, $password)) {
         return $this->error;
     }
     $result = Widget_Upload::uploadHandle($data);
     if (false === $result) {
         return IXR_Error(500, _t('上传失败'));
     } else {
         $insertId = $this->insert(array('title' => $result['name'], 'slug' => $result['name'], 'type' => 'attachment', 'status' => 'publish', 'text' => serialize($result), 'allowComment' => 1, 'allowPing' => 0, 'allowFeed' => 1));
         $this->db->fetchRow($this->select()->where('table.contents.cid = ?', $insertId)->where('table.contents.type = ?', 'attachment'), array($this, 'push'));
         /** 增加插件接口 */
         $this->pluginHandle()->upload($this);
         return array('file' => $this->attachment->name, 'url' => $this->attachment->url);
     }
 }
Beispiel #4
0
 /**
  * clearAttachment  
  * 
  * @access public
  * @return void
  */
 public function clearAttachment()
 {
     $page = 1;
     $deleteCount = 0;
     do {
         $posts = Typecho_Common::arrayFlatten($this->db->fetchAll($this->select('cid')->from('table.contents')->where('type = ? AND parent = ?', 'attachment', 0)->page($page, 100)), 'cid');
         $page++;
         foreach ($posts as $post) {
             // 删除插件接口
             $this->pluginHandle()->delete($post, $this);
             $condition = $this->db->sql()->where('cid = ?', $post);
             $row = $this->db->fetchRow($this->select()->where('table.contents.type = ?', 'attachment')->where('table.contents.cid = ?', $post)->limit(1), array($this, 'push'));
             if ($this->isWriteable($condition) && $this->delete($condition)) {
                 /** 删除文件 */
                 Widget_Upload::deleteHandle($row);
                 /** 删除评论 */
                 $this->db->query($this->db->delete('table.comments')->where('cid = ?', $post));
                 $status = $this->status;
                 // 完成删除插件接口
                 $this->pluginHandle()->finishDelete($post, $this);
                 $deleteCount++;
             }
             unset($condition);
         }
     } while (count($posts) == 100);
     /** 设置提示信息 */
     $this->widget('Widget_Notice')->set($deleteCount > 0 ? _t('未归档文件已经被清理') : _t('没有未归档文件被清理'), $deleteCount > 0 ? 'success' : 'notice');
     /** 返回原网页 */
     $this->response->redirect(Typecho_Common::url('manage-medias.php', $this->options->adminUrl));
 }
Beispiel #5
0
 /**
  * 删除文章
  *
  * @access public
  * @return void
  */
 public function deleteAttachment()
 {
     $cid = $this->request->filter('int')->cid;
     $deleteCount = 0;
     $status = 'publish';
     if ($cid) {
         /** 格式化文章主键 */
         $posts = is_array($cid) ? $cid : array($cid);
         foreach ($posts as $post) {
             $condition = $this->db->sql()->where('cid = ?', $post);
             $row = $this->db->fetchRow($this->select()->where('table.contents.type = ?', 'attachment')->where('table.contents.cid = ?', $post)->limit(1), array($this, 'push'));
             if ($this->isWriteable($condition) && $this->delete($condition)) {
                 /** 删除文件 */
                 Widget_Upload::deleteHandle($row);
                 /** 删除评论 */
                 $this->db->query($this->db->delete('table.comments')->where('cid = ?', $post));
                 $status = $this->status;
                 $deleteCount++;
             }
             unset($condition);
         }
     }
     if ($this->request->isAjax()) {
         $this->response->throwJson($deleteCount > 0 ? array('code' => 200, 'message' => _t('文件已经被删除')) : array('code' => 500, 'message' => _t('没有文件被删除')));
     } else {
         /** 设置提示信息 */
         $this->widget('Widget_Notice')->set($deleteCount > 0 ? _t('文件已经被删除') : _t('没有文件被删除'), $deleteCount > 0 ? 'success' : 'notice');
         /** 返回原网页 */
         $this->response->redirect(Typecho_Common::url('manage-medias.php', $this->options->adminUrl));
     }
 }
Beispiel #6
0
<?php

header('Content-Type: text/html; charset=UTF-8');
$rootDir = strstr(dirname(__FILE__), 'usr', TRUE);
require_once $rootDir . 'config.inc.php';
require_once $rootDir . 'var/Typecho/Common.php';
require_once $rootDir . 'var/Typecho/Request.php';
require_once $rootDir . 'var/Widget/Upload.php';
$fileInfo = Widget_Upload::uploadHandle($_FILES['upload']);
if (false === $fileInfo) {
    echo '上传失败!';
} else {
    echo sprintf("<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction(1, '%s', '');</script>", Typecho_Request::getInstance()->getUrlPrefix() . $fileInfo['path']);
}
Beispiel #7
0
 /**
  * 上传头像
  * @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));
 }
Beispiel #8
0
 public static function uploadFile($file, $content = null)
 {
     // 获取上传文件
     if (empty($file['name'])) {
         return false;
     }
     $option = self::getConfig();
     if (!isset($option->accessid)) {
         return false;
     }
     self::initSDK();
     $obj = new ALIOSS($option->accessid, $option->accesskey, $option->endpoint);
     // 校验扩展名
     $part = explode('.', $file['name']);
     $ext = ($length = count($part)) > 1 ? strtolower($part[$length - 1]) : '';
     if (!Widget_Upload::checkFileType($ext)) {
         return false;
     }
     // 保存位置
     $savename = str_replace(array('{year}', '{month}', '{day}'), array(date('Y'), date('m'), date('d')), ltrim(self::isImage($ext) ? $option->savepath : $option->nonimg_savepath, '/')) . sprintf('%u', crc32(uniqid())) . '.' . $ext;
     $response = $obj->upload_file_by_file($option->bucket, $savename, $file['tmp_name']);
     if ($response->status === 200) {
         return array('name' => $file['name'], 'path' => $savename, 'size' => $file['size'], 'type' => $ext, 'mime' => Typecho_Common::mimeContentType($savename));
     }
     return false;
 }
Beispiel #9
0
 /**
  * 上传文件处理函数
  *
  * @access public
  * @param array $file 上传的文件
  * @return mixed
  */
 public static function uploadHandle($file)
 {
     if (empty($file['name'])) {
         return false;
     }
     $fileName = preg_split("(\\/|\\|:)", $file['name']);
     $file['name'] = array_pop($fileName);
     //获取扩展名
     $ext = '';
     $part = explode('.', $file['name']);
     if (($length = count($part)) > 1) {
         $ext = strtolower($part[$length - 1]);
     }
     if (!Widget_Upload::checkFileType($ext)) {
         return false;
     }
     $options = Typecho_Widget::widget('Widget_Options');
     $date = new Typecho_Date($options->gmtTime);
     // 构建路径
     $path = Widget_Upload::UPLOAD_DIR . '/' . $date->year . '/' . $date->month;
     // 获取文件名
     $fileName = sprintf('%u', crc32(uniqid())) . '.' . $ext;
     $path = $path . '/' . $fileName;
     // 上传云存储
     $bos = self::bosInit();
     if (isset($file['tmp_name'])) {
         $bos->uploadFile($file['tmp_name'], $path);
     } else {
         if (isset($file['bits'])) {
             $bos->uploadFileWithData($file['bits'], $path);
         } else {
             return false;
         }
     }
     $meta = null;
     if (!isset($file['size'])) {
         $meta = $meta ?: $bos->getObjectMeta($path);
         $file['size'] = $meta->contentLength;
     }
     if (!isset($file['type'])) {
         $meta = $meta ?: $bos->getObjectMeta($path);
         $file['type'] = $meta->contentType;
     }
     // 返回相对存储路径
     return ['name' => $file['name'], 'path' => $path, 'size' => $file['size'], 'type' => $ext, 'mime' => $file['type']];
 }
Beispiel #10
0
 /**
  * 上传文件处理函数
  *
  * @access public
  * @param array $file 上传的文件
  * @return mixed
  */
 public static function uploadHandle($file)
 {
     if (empty($file['name'])) {
         return false;
     }
     //获取扩展名
     $ext = self::getSafeName($file['name']);
     if (!Widget_Upload::checkFileType($ext) || Typecho_Common::isAppEngine()) {
         return false;
     }
     $options = Typecho_Widget::widget('Widget_Options');
     $date = new Typecho_Date($options->gmtTime);
     //构建路径 /year/month/
     $path = '/' . $date->year . '/' . $date->month;
     $settings = $options->plugin('UpyunFile');
     if ($settings->mode == 'typecho') {
         $path = self::getUploadDir() . $path;
     }
     //获取文件名及文件路径
     $fileName = sprintf('%u', crc32(uniqid())) . '.' . $ext;
     $path = $path . '/' . $fileName;
     $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' => $file['name'], 'path' => $path, 'size' => $file['size'], 'type' => $ext, 'mime' => Typecho_Common::mimeContentType($path));
 }
Beispiel #11
0
 /**
  * 上传附件
  * 
  * @static
  * @access public
  * @return bool
  * @throws Typecho_Plugin_Exception
  */
 public static function ScsUpload($file, $content = null)
 {
     if (empty($file['name'])) {
         return false;
     }
     //获取安全的文件名后缀
     $ext = self::getSafeName($file['name']);
     if (!Widget_Upload::checkFileType($ext)) {
         return false;
     }
     $option = self::getSCSconfig();
     $date = new Typecho_Date(Typecho_Widget::widget('Widget_Options')->gmtTime);
     $path = $option->format == null ? $date->year . '/' . $date->month . '/' : preg_replace(array('/\\{year\\}/', '/\\{month\\}/', '/\\{day\\}/'), array($date->year, $date->month, $date->day), self::getSCSFilepath($option->format));
     /*非必须(在本地附件目录/usr/uploads/下创建新目录)
             if (!is_dir($path)) {
                 if (!self::makeUploadDir($path)) {
                     return false;
                 }
             }
     		*/
     //以Unix time stamp形式储存:$path .= sprintf('%u', crc32(uniqid())) . '.' . $ext;
     $path .= $file['name'];
     if (isset($content)) {
         $path = $content['attachment']->path;
         self::ScsDelete($path);
     }
     $clienttmp = $file['tmp_name'];
     if (!isset($clienttmp)) {
         return false;
     }
     self::getSCSsdk();
     $scs = new SCS($option->accesskey, $option->secretkey);
     if ($scs->putObjectFile($clienttmp, $option->bucket, $path, SCS::ACL_PUBLIC_READ)) {
         return array('name' => $file['name'], 'path' => $path, 'size' => $file['size'], 'type' => $ext, 'mime' => Typecho_Common::mimeContentType($path));
     } else {
         return false;
     }
 }
Beispiel #12
0
 /**
  * 上传文件处理函数
  *
  * @access public
  * @param array $file 上传的文件
  * @return mixed
  */
 public static function uploadHandle($file)
 {
     if (empty($file['name'])) {
         return false;
     }
     $fileName = preg_split("(\\/|\\|:)", $file['name']);
     $file['name'] = array_pop($fileName);
     //获取扩展名
     $ext = '';
     $part = explode('.', $file['name']);
     if (($length = count($part)) > 1) {
         $ext = strtolower($part[$length - 1]);
     }
     if (!Widget_Upload::checkFileType($ext)) {
         return false;
     }
     $options = Typecho_Widget::widget('Widget_Options');
     $date = new Typecho_Date($options->gmtTime);
     //构建路径
     $path = Widget_Upload::UPLOAD_PATH . '/' . $date->year . '/' . $date->month;
     //获取文件名
     $fileName = sprintf('%u', crc32(uniqid())) . '.' . $ext;
     $path = $path . '/' . $fileName;
     $bcs = self::bcsInit();
     $bucket = $options->plugin('BaeUpload')->bucket;
     //空日志记录函数
     function bcs_log()
     {
     }
     if (isset($file['tmp_name'])) {
         //移动上传文件
         if (!$bcs->create_object($bucket, $path, $file['tmp_name'], array('acl' => BaiduBCS::BCS_SDK_ACL_TYPE_PUBLIC_READ, BaiduBCS::IMPORT_BCS_LOG_METHOD => 'bcs_log'))->isOK()) {
             return false;
         }
     } else {
         if (isset($file['bits'])) {
             //直接写入文件
             if (!$bcs->create_object_by_content($bucket, $path, $file['bits'], array('acl' => BaiduBCS::BCS_SDK_ACL_TYPE_PUBLIC_READ, BaiduBCS::IMPORT_BCS_LOG_METHOD => 'bcs_log'))->isOK()) {
                 return false;
             }
         } else {
             return false;
         }
     }
     //设置文件Content-Type
     $bcs->set_object_meta($bucket, $path, array('Content-Type' => BCS_MimeTypes::get_mimetype($ext)), array(BaiduBCS::IMPORT_BCS_LOG_METHOD => 'bcs_log'));
     if (!isset($file['size'])) {
         $file['size'] = $bcs->get_object_info($bucket, $path, array(BaiduBCS::IMPORT_BCS_LOG_METHOD => 'bcs_log'))->header['Content-Length'];
     }
     //返回相对存储路径
     return array('name' => $file['name'], 'path' => $path, 'size' => $file['size'], 'type' => $ext, 'mime' => Typecho_Common::mimeContentType($path));
 }