typecho上传阿里云oss的插件都不能用了,新版我这要怎么改才能用?

修改了3天,完全不行,这几天干燥,上火,流鼻血,还是搞不定,外行人是不行的, 所以请教大神,要怎么改?
图片描述

救命啊,要挂了,这个代码简单一点,就是不知道跟oss直接怎么弄,搜集了各种版本的整理出来的。
<?php
/**
 * OSS For Typecho
 *
 * @package AOT
 * @author 
 * @version 
 * @link 
 * @date 
 */
require_once 'OSS/OssClient.php';
use OSS\OssClient;
use OSS\Core\OssUtil;
use OSS\Core\OssException;
use OSS\Core\MimeTypes;

class AOT_Plugin implements Typecho_Plugin_Interface
{
    /**
     * 激活插件方法,如果激活失败,直接抛出异常
     *
     * @access public
     * @return void
     * @throws Typecho_Plugin_Exception
     */
    public static function activate()
    {
        //上传
        Typecho_Plugin::factory('Widget_Upload')->uploadHandle = array('AOT_Plugin', 'uploadHandle');
        //修改
        Typecho_Plugin::factory('Widget_Upload')->modifyHandle = array('AOT_Plugin', 'modifyHandle');
        //删除
        Typecho_Plugin::factory('Widget_Upload')->deleteHandle = array('AOT_Plugin', 'deleteHandle');
        //路径参数处理
        Typecho_Plugin::factory('Widget_Upload')->attachmentHandle = array('AOT_Plugin', 'attachmentHandle');
        //文件内容数据
        Typecho_Plugin::factory('Widget_Upload')->attachmentDataHandle = array('AOT_Plugin', 'attachmentDataHandle');
    }

    /**
     * 禁用插件方法,如果禁用失败,直接抛出异常
     *
     * @static
     * @access public
     * @return void
     * @throws Typecho_Plugin_Exception
     */
    public static function deactivate()
    {
    }

    /**
     * 获取插件配置面板
     *
     * @access public
     * @param Typecho_Widget_Helper_Form $form 配置面板
     * @return void
     */
    public static function config(Typecho_Widget_Helper_Form $form)
    {
        /**
         * 节点
         */
        $endpointList = array(
            "" => _t('请选择所属地域'),
            "oss-cn-beijing.aliyuncs.com" => _t('北京'),
            "oss-cn-qingdao.aliyuncs.com" => _t('青岛'),
            "oss-cn-shenzhen.aliyuncs.com" => _t('深圳'),
            "oss-cn-hangzhou.aliyuncs.com" => _t('杭州'),
            "oss-cn-shanghai.aliyuncs.com" => _t('上海')
        );

        $endpoint = new Typecho_Widget_Helper_Form_Element_Select('endpoint', $endpointList, 'oss-cn-beijing.aliyuncs.com', _t('所属地域'), _t('请选择<strong style="color:#C33;">bucket对应节点</strong>,否则无法使用,默认为北京!'));
        $form->addInput($endpoint->addRule('required', _t('所属地域 不能为空!')));
        
        $accessid = new Typecho_Widget_Helper_Form_Element_Text('accessid',
            NULL, '',
            _t('accessid:'),
            _t('获取accessid'));
        $form->addInput($accessid);

        $accesskey = new Typecho_Widget_Helper_Form_Element_Text('accesskey',
            NULL, '',
            _t('accesskey:'),
            _t('获取accesskey'));
        $form->addInput($accesskey);


        $bucketName = new Typecho_Widget_Helper_Form_Element_Text('bucket',
            NULL, 'bucketName',
            _t('Bucket名称:'),
            _t('例如test'));
        $form->addInput($bucketName->addRule('required', _t('Bucket名称不能为空!')));


        $domain = new Typecho_Widget_Helper_Form_Element_Text('domain',
            NULL, 'http://',
            _t('使用的域名,必填,请带上http://或https://,可使用默认域名或自定义域名'),
            _t('在bucket中的域名管理'));
        $form->addInput($domain->addRule('required', _t('OSS域名不能为空!')));;
    }

    /**
     * 个人用户的配置面板
     *
     * @access public
     * @param Typecho_Widget_Helper_Form $form
     * @return void
     */
    public static function personalConfig(Typecho_Widget_Helper_Form $form)
    {
    }

    /**
     * 上传文件处理函数
     *
     * @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)) return false;
        //获取文件名
        $filePath = '/' . date('Y') . '/' . date('m') . '/' . date('d') . '/';
        $fileName = time(). '.' . $ext;
        //如果没有临时文件,则退出
        if (!isset($file['tmp_name'])) return false;
        //获取插件参数
        $options = Typecho_Widget::widget('Widget_Options')->plugin('AOT');
        //初始化cos

        $ossClient = new OssClient($options->accessid, $options->accesskey, $options->endpoint, true);
        
        $dstPath = $filePath . $fileName;
        $srcPath = $file['tmp_name'];
        $ossHeader = array(
            OssClient::OSS_HEADERS => array(
                'Cache-Control' => 'max-age=2592000'
            ),
        );
        
        $response = $ossClient->multiuploadFile($options->bucket, $dstPath, $srcPath, $ossHeader);
        
        if ($response->status===200) {
            return array(
                'name' => $file['name'],
                'path' => $filePath . $fileName,
                'size' => $file['size'],
                'type' => $ext,
                'mime' => @Typecho_Common::mimeContentType(rtrim($options->domain, '/') . '/' . $filePath . $fileName),
            );
        }
        
    }


    /**
     * 文件修改处理函数
     *
     * @access public
     * @param array $content 当前文件信息
     * @param array $file 新上传文件信息
     * @return mixed
     */
//    public static function modifyHandle($content, $file)
//    {

//    }

    /**
     * 文件删除
     *
     * @access public
     * @param array $content 当前文件信息
     * @return mixed
     */
    public static function deleteHandle($content)
    {
        $options = Typecho_Widget::widget('Widget_Options')->plugin('AOT');
        $ossClient = new OssClient($options->accessid, $options->accesskey, $options->endpoint, true);
        $err = $ossClient->deleteObject($options->bucket, $content['attachment']->path);
        return !$err;
    }

    /**
     * 获取实际文件数据
     *
     * @access public
     * @param array $content
     * @return string
     */
    public static function attachmentDataHandle($content)
    {
        $options = Typecho_Widget::widget('Widget_Options')->plugin('AOT');
        if ($options->domain == '')
        {
            return 'http://' . $options->bucket . '.' . $options->endpoint . '/' . $content['attachment']->path;
        } else{
            return 'https://' . $options->domain . '/' . $content['attachment']->path;
        }
    }

    /**
     * 获取实际文件绝对访问路径
     *
     * @access public
     * @param array $content 文件相关信息
     * @return string
     */
    public static function attachmentHandle(array $content)
    {
        $domain = Typecho_Widget::widget('Widget_Options')->plugin('AOT')->domain;
        $tmp = preg_match('/http(s)?:\/\/[\w\d\.\-\/]+$/is', $domain);    //粗略验证域名
        if (!$tmp) return false;
        return Typecho_Common::url($content['attachment']->path, $domain);
    }


    /**
     * 获取安全的文件名
     *
     * @param string $name
     * @static
     * @access private
     * @return string
     */
    private static function getSafeName(&$name)
    {
        $name = str_replace(array('"', '<', '>'), '', $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']) : '';
    }
}
阅读 3.7k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进