[PHP] 纯文本查看 复制代码<?php
/*
Plugin Name: 腾讯云COS同步插件
Plugin URI: [url]/cos-sync-plugins[/url]
Description: 使用腾讯云对象存储服务 COS 作为附件存储空间。(This is a plugin that uses QCloud Cloud Object Service for attachments remote saving.)
Version: 2.0
Author: 水冷眸
Author URI: [url][/url]
License: MIT
*/
require_once('sdk/include.php');
use Qcloudcos\Auth;
use Qcloudcos\Cosapi;
if (!defined('WP_PLUGIN_URL'))
define('WP_PLUGIN_URL', WP_CONTENT_URL . '/plugins');// plugin url
define('COS_BASENAME', plugin_basename(__FILE__));
define('COS_BASEFOLDER', plugin_basename(dirname(__FILE__)));
// 初始化选项
register_activation_hook(__FILE__, 'cos_set_options');
// 初始化选项
function cos_set_options(){
$options = array(
'bucket' => "",
'regional' => 'gz',
'app_id' => "",
'secret_id' => "",
'secret_key' => "",
'nothumb' => "false", // 是否上传所旅途
'nolocalsaving' => "false", // 是否保留本地备份
'upload_url_path' => "", // URL前缀
);
add_option('cos_options', $options, '', 'yes');
}
// 设置COS所在的区域:
$cos_opt = get_option('cos_options', TRUE);
$regional = esc_attr($cos_opt['regional']);
Cosapi::setRegion($regional);
/**
* 上传函数
* @param $object
* @param $file
* @param $opt
* [url=home.php?mod=space&uid=155549]@Return[/url] bool
*/
function _file_upload($object, $file, $opt = array()){
//如果文件不存在,直接返回FALSE
if (!@file_exists($file))
return FALSE;
//获取WP配置信息
$cos_options = get_option('cos_options', TRUE);
$cos_bucket = esc_attr($cos_options['bucket']);
if (@file_exists($file)) {
$dirname = dirname($object);
_create_folder($cos_bucket, $dirname);
$ret = Cosapi::upload($cos_bucket, $file, $object);
} else {
return FALSE;
}
/*
echo $object.'
';
echo $file.'
';
echo $dirname.'
';
*/
}
/**
* 创建相应的目录
* @param $cos_bucket
* @param $dir
*/
function _create_folder($cos_bucket, $dir){
$data = Cosapi::statFolder($cos_bucket, $dir . '/');
if ($data['code'] == -166) {
$dir_array = explode('/', $dir);
$dir_name = '';
foreach ($dir_array as $dir) {
$dir_name .= ($dir . '/');
$result = Cosapi::statFolder($cos_bucket, $dir_name);
if ($result['code'] == -166) {
$ret = Cosapi::createFolder($cos_bucket, $dir_name);
}
}
}
//var_dump($data,$result,$ret);
}
/**
* 是否需要删除本地文件
* @return bool
*/
function _is_delete_local_file(){
$cos_options = get_option('cos_options', TRUE);
return (esc_attr($cos_options['nolocalsaving']) == 'true');
}
/**
* 删除本地文件
* @param $file 本地文件路径
* @return bool
*/
function _delete_local_file($file){
try {
//文件不存在
if (!@file_exists($file))
return TRUE;
//删除文件
if (!@unlink($file))
return FALSE;
return TRUE;
} catch (Exception $ex) {
return FALSE;
}
}
/**
* 上传附件(包括图片的原图)
* @param $metadata
* @return array()
*/
function upload_attachments($metadata){
$wp_uploads = wp_upload_dir();
//生成object在OSS中的存储路径
if (get_option('upload_path') == '.') {
//如果含有“./”则去除之
$metadata['file'] = str_replace("./", '', $metadata['file']);
}
$object = str_replace("\\", '/', $metadata['file']);
$object = str_replace(get_home_path(), '', $object);
//在本地的存储路径
$file = get_home_path() . $object; //向上兼容,较早的WordPress版本上$metadata['file']存放的是相对路径
//设置可选参数
$opt = array('Content-Type' => $metadata['type']);
//执行上传操作
_file_upload('/' . $object, $file, $opt);
//如果不在本地保存,则删除本地文件
if (_is_delete_local_file()) {
_delete_local_file($file);
}
return $metadata;
}
//避免上传插件/主题时出现同步到COS的情况
if (substr_count($_SERVER['REQUEST_URI'], '/update.php') <= 0)
add_filter('wp_handle_upload', 'upload_attachments', 50);
/**
* 上传图片的缩略图
*/
function upload_thumbs($metadata){
//上传所有缩略图
if (isset($metadata['sizes']) && count($metadata['sizes']) > 0) {
//获取COS插件的配置信息
$cos_options = get_option('cos_options', TRUE);
//是否需要上传缩略图
$nothumb = (esc_attr($cos_options['nothumb']) == 'true');
//是否需要删除本地文件
$is_delete_local_file = (esc_attr($cos_options['nolocalsaving']) == 'true');
//如果禁止上传缩略图,就不用继续执行了
if ($nothumb) {
return $metadata;
}
//获取上传路径
$wp_uploads = wp_upload_dir();
$basedir = $wp_uploads['basedir'];
$file_dir = $metadata['file'];
//得到本地文件夹和远端文件夹
$file_path = $basedir . '/' . dirname($file_dir) . '/';
if (get_option('upload_path') == '.') {
$file_path = str_replace("\\", '/', $file_path);
$file_path = str_replace(get_home_path() . "./", '', $file_path);
} else {
$file_path = str_replace("\\", '/', $file_path);
}
$object_path = str_replace(get_home_path(), '', $file_path);
//there may be duplicated filenames,so ....
foreach ($metadata['sizes'] as $val) {
//生成object在COS中的存储路径
$object = '/' . $object_path . $val['file'];
//生成本地存储路径
$file = $file_path . $val['file'];
//设置可选参数
$opt = array('Content-Type' => $val['mime-type']);
//执行上传操作
_file_upload($object, $file, $opt);
//如果不在本地保存,则删除
if ($is_delete_local_file)
_delete_local_file($file);
}
}
return $metadata;
}
//避免上传插件/主题时出现同步到COS的情况
if (substr_count($_SERVER['REQUEST_URI'], '/update.php') <= 0)
add_filter('wp_generate_attachment_metadata', 'upload_thumbs', 100);
/**
* 删除远程服务器上的单个文件
*/
function delete_remote_file($file){
//获取WP配置信息
$cos_options = get_option('cos_options', TRUE);
$cos_bucket = esc_attr($cos_options['bucket']);
//得到远程路径
$file = str_replace("\\", '/', $file);
$del_file_path = str_replace(get_home_path(), '/', $file);
try {
//删除文件
Cosapi::delFile($cos_bucket, $del_file_path);
} catch (Exception $ex) {
}
return $file;
}
add_action('wp_delete_file', 'delete_remote_file', 100);
// 当upload_path为根目录时,需要移除URL中出现的“绝对路径”
function modefiy_img_url($url, $post_id){
$home_path = str_replace(array('/', '\\'), array('', ''), get_home_path());
$url = str_replace($home_path, '', $url);
return $url;
}
if (get_option('upload_path') == '.') {
add_filter('wp_get_attachment_url', 'modefiy_img_url', 30, 2);
}
function read_dir_queue($dir){
if (isset($dir)){
$files=array();
$queue=array($dir);
while($data=each($queue)){
$path=$data['value'];
if(is_dir($path) && $handle=opendir($path)){
while($file=readdir($handle)){
if($file=='.'||$file=='..') continue;
$files[] = $real_path = $path.'/'.$file;
if (is_dir($real_path)) $queue[] = $real_path;
//echo explode(get_option('upload_path'),$path)[1];
}
}
closedir($handle);
}
$i='';
foreach ($files as $v){
$i++;
if (!is_dir($v)){
$dd[$i]['j'] = $v;
$dd[$i]['x'] = '/'.get_option('upload_path').explode(get_option('upload_path'),$v)[1];
}
}
}else{
$dd = '';
}
return $dd;
}
// 在插件列表页添加设置按钮
function cos_plugin_action_links($links, $file){
if ($file == plugin_basename(dirname(__FILE__) . '/cos-sync.php')) {
$links[] = '' . 设置 . '';
}
return $links;
}
add_filter('plugin_action_links', 'cos_plugin_action_links', 10, 2);
// 在导航栏“设置”中添加条目
function cos_add_setting_page(){
add_options_page('腾讯云COS设置', '腾讯云COS设置', 'manage_options', __FILE__, 'cos_setting_page');
}
add_action('admin_menu', 'cos_add_setting_page');
// 插件设置页面
function cos_setting_page(){
if (!current_user_can('manage_options')) {
wp_die('Insufficient privileges!');
}
$options = array();
if (!empty($_POST) and $_POST['type'] == 'cos_set') {
$options['bucket'] = (isset($_POST['bucket'])) ? trim(stripslashes($_POST['bucket'])) : '';
$options['regional'] = (isset($_POST['regional'])) ? trim(stripslashes($_POST['regional'])) : '';
$options['app_id'] = (isset($_POST['app_id'])) ? trim(stripslashes($_POST['app_id'])) : '';
$options['secret_id'] = (isset($_POST['secret_id'])) ? trim(stripslashes($_POST['secret_id'])) : '';
$options['secret_key'] = (isset($_POST['secret_key'])) ? trim(stripslashes($_POST['secret_key'])) : '';
$options['nothumb'] = (isset($_POST['nothumb'])) ? 'true' : 'false';
$options['nolocalsaving'] = (isset($_POST['nolocalsaving'])) ? 'true' : 'false';
//仅用于插件卸载时比较使用
$options['upload_url_path'] = (isset($_POST['upload_url_path'])) ? trim(stripslashes($_POST['upload_url_path'])) : '';
}
if(!empty($_POST) and $_POST['type'] == 'cos_sync_all'){
$cos_options = get_option('cos_options', TRUE);
$cos_bucket = esc_attr($cos_options['bucket']);
$synv = read_dir_queue(get_home_path().get_option('upload_path'));
$i = 0;
foreach ($synv as $k){
$ret = Cosapi::stat($cos_bucket, $k['x']);
if ($ret['code']!=0){
$i++;
_file_upload($k['x'], $k['j']);
}
}
echo '
本次操作成功同步'.$i.'个文件
';
}
// 若$options不为空数组,则更新数据
if ($options !== array()) {
//更新数据库
update_option('cos_options', $options);
$upload_path = trim(trim(stripslashes($_POST['upload_path'])), '/');
$upload_path = ($upload_path == '') ? ('wp-content/uploads') : ($upload_path);
update_option('upload_path', $upload_path);
$upload_url_path = trim(trim(stripslashes($_POST['upload_url_path'])), '/');
update_option('upload_url_path', $upload_url_path);
?>
设置已保存!
}
$cos_options = get_option('cos_options', TRUE);
$upload_path = get_option('upload_path');
$upload_url_path = get_option('upload_url_path');
$cos_bucket = esc_attr($cos_options['bucket']);
$cos_regional = esc_attr($cos_options['regional']);
$cos_app_id = esc_attr($cos_options['app_id']);
$cos_secret_id = esc_attr($cos_options['secret_id']);
$cos_secret_key = esc_attr($cos_options['secret_key']);
$cos_nothumb = esc_attr($cos_options['nothumb']);
$cos_nothumb = ($cos_nothumb == 'true');
$cos_nolocalsaving = esc_attr($cos_options['nolocalsaving']);
$cos_nolocalsaving = ($cos_nolocalsaving == 'true');
?>
腾讯云 COS 附件设置
}
?>