diff --git a/README.md b/README.md index b6ce3a7..8b937e3 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ - [x] 支持阿里云 OSS 图片处理 - [x] 支持上传文件自动重命名 - [x] 支持使用 RAM 操作 +- [x] 支持原图保护 ## 安装 diff --git a/aliyun-oss-wordpress.php b/aliyun-oss-wordpress.php index 9786c0f..df8f5d7 100644 --- a/aliyun-oss-wordpress.php +++ b/aliyun-oss-wordpress.php @@ -3,7 +3,7 @@ Plugin Name: OSS Aliyun Plugin URI: https://github.com/sy-records/aliyun-oss-wordpress Description: 使用阿里云对象存储 OSS 作为附件存储空间。(This is a plugin that uses Aliyun Object Storage Service for attachments remote saving.) -Version: 1.4.11 +Version: 1.4.12 Author: 沈唁 Author URI: https://qq52o.me License: Apache2.0 @@ -19,7 +19,7 @@ use OSS\Credentials\CredentialsProvider; use AlibabaCloud\Credentials\Credential; use OSS\Credentials\StaticCredentialsProvider; -define('OSS_VERSION', '1.4.11'); +define('OSS_VERSION', '1.4.12'); define('OSS_BASEFOLDER', plugin_basename(dirname(__FILE__))); if (!function_exists('get_home_path')) { @@ -63,7 +63,8 @@ function oss_set_options() 'upload_url_path' => '', // URL前缀 'style' => '', // 图片处理 'update_file_name' => 'false', // 是否重命名文件名 - 'role_name' => '' // 角色名称 + 'role_name' => '', // 角色名称 + 'origin_protect' => '', ]; add_option('oss_options', $options, '', 'yes'); } @@ -486,7 +487,7 @@ function oss_plugin_action_links($links, $file) { if ($file == plugin_basename(dirname(__FILE__) . '/aliyun-oss-wordpress.php')) { $links[] = '设置'; - $links[] = '赞赏'; + $links[] = 'Donate'; } return $links; } @@ -625,6 +626,91 @@ function oss_get_option($key) return esc_attr(get_option($key)); } +$oss_options = get_option('oss_options', true); +if (!empty($oss_options['origin_protect']) && esc_attr($oss_options['origin_protect']) === 'on' && !empty(esc_attr($oss_options['style']))) { + add_filter('wp_get_attachment_url', 'oss_add_suffix_to_attachment_url', 10, 2); + add_filter('wp_get_attachment_thumb_url', 'oss_add_suffix_to_attachment_url', 10, 2); + add_filter('wp_get_original_image_url', 'oss_add_suffix_to_attachment_url', 10, 2); + add_filter('wp_prepare_attachment_for_js', 'oss_add_suffix_to_attachment', 10, 2); + add_filter('image_get_intermediate_size', 'oss_add_suffix_for_media_send_to_editor'); +} + +/** + * @param string $url + * @param int $post_id + * @return string + */ +function oss_add_suffix_to_attachment_url($url, $post_id) +{ + if (oss_is_image_type($url)) { + $url .= oss_get_image_style(); + } + + return $url; +} + +/** + * @param array $response + * @param array $attachment + * @return array + */ +function oss_add_suffix_to_attachment($response, $attachment) +{ + if ($response['type'] != 'image') { + return $response; + } + + $style = oss_get_image_style(); + if (!empty($response['sizes'])) { + foreach ($response['sizes'] as $size_key => $size_file) { + if (oss_is_image_type($size_file['url'])) { + $response['sizes'][$size_key]['url'] .= $style; + } + } + } + + if(!empty($response['originalImageURL'])) { + if (oss_is_image_type($response['originalImageURL'])) { + $response['originalImageURL'] .= $style; + } + } + + return $response; +} + +/** + * @param array $data + * @return array + */ +function oss_add_suffix_for_media_send_to_editor($data) +{ + // https://github.com/WordPress/wordpress-develop/blob/43d2455dc68072fdd43c3c800cc8c32590f23cbe/src/wp-includes/media.php#L239 + if (oss_is_image_type($data['file'])) { + $data['file'] .= oss_get_image_style(); + } + + return $data; +} + +/** + * @param string $url + * @return bool + */ +function oss_is_image_type($url) +{ + return (bool) preg_match('/\.(jpg|jpeg|jpe|gif|png|bmp|webp|heic|heif|svg)$/i', $url); +} + +/** + * @return string + */ +function oss_get_image_style() +{ + $oss_options = get_option('oss_options', true); + + return esc_attr($oss_options['style']); +} + // 在导航栏“设置”中添加条目 function oss_add_setting_page() { @@ -658,6 +744,7 @@ function oss_setting_page() $options['upload_url_path'] = isset($_POST['upload_url_path']) ? sanitize_text_field(stripslashes($_POST['upload_url_path'])) : ''; $options['style'] = isset($_POST['style']) ? sanitize_text_field($_POST['style']) : ''; $options['update_file_name'] = isset($_POST['update_file_name']) ? sanitize_text_field($_POST['update_file_name']) : 'false'; + $options['origin_protect'] = isset($_POST['origin_protect']) ? sanitize_text_field($_POST['origin_protect']) : 'off'; if ($options['regional'] === 'oss-rg-china-mainland' && $options['is_internal'] === 'true') { echo '

无地域属性不支持内网,请重新填写配置!

'; @@ -725,6 +812,7 @@ function oss_setting_page() $oss_nolocalsaving = esc_attr($oss_options['nolocalsaving']); $oss_nolocalsaving = $oss_nolocalsaving == 'true'; $oss_update_file_name = esc_attr($oss_options['update_file_name']); + $oss_origin_protect = esc_attr($oss_options['origin_protect'] ?? 'off') !== 'off' ? 'checked="checked"' : ''; $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? 'https://' : 'http://'; ?> @@ -801,8 +889,7 @@ function oss_setting_page() 不在本地保留备份 - /> + />

建议不勾选

@@ -863,6 +950,18 @@ function oss_setting_page()

则填写为 !stylename

+ + + 原图保护 + + + /> + +

开启原图保护功能后,存储桶中的图片文件仅能以带样式的 URL 进行访问,能够阻止恶意用户对源文件的请求。

+

使用时请先访问阿里云对象存储控制台开启原图保护并设置图片处理样式

+

注:此功能为实验性功能,如遇错误或不可用,请关闭后联系作者反馈。

+ + 保存/更新选项 diff --git a/readme.txt b/readme.txt index c76578c..f02565a 100644 --- a/readme.txt +++ b/readme.txt @@ -5,7 +5,7 @@ Tags: oss, 阿里云, 对象存储, aliyun Requires at least: 4.2 Tested up to: 6.4 Requires PHP: 7.0 -Stable tag: 1.4.11 +Stable tag: 1.4.12 License: Apache2.0 License URI: http://www.apache.org/licenses/LICENSE-2.0.html @@ -28,7 +28,8 @@ License URI: http://www.apache.org/licenses/LICENSE-2.0.html 7. 支持阿里云 OSS 图片处理 8. 支持上传文件自动重命名 9. 支持使用 RAM 操作 -10. 插件更多详细介绍和安装:[https://github.com/sy-records/aliyun-oss-wordpress](https://github.com/sy-records/aliyun-oss-wordpress) +10. 支持原图保护 +11. 插件更多详细介绍和安装:[https://github.com/sy-records/aliyun-oss-wordpress](https://github.com/sy-records/aliyun-oss-wordpress) ## 其他插件 @@ -73,6 +74,10 @@ License URI: http://www.apache.org/licenses/LICENSE-2.0.html == Changelog == += 1.4.12 = + +- 支持原图保护 + = 1.4.11 = - 优化数据库数据替换语法 diff --git a/uninstall.php b/uninstall.php index 9db7ce6..0539ba3 100644 --- a/uninstall.php +++ b/uninstall.php @@ -1,6 +1,5 @@