PluginProbe
WPWaterMark 轻水印插件 / 5.1.7
WPWaterMark 轻水印插件 v5.1.7
5.2.4 5.2.2 5.2.1 5.1.7 5.1.6 trunk 4.3 5.0.0 5.0.1 5.1.2 5.1.3 5.1.4 5.1.5
wpwatermark / WaterMarkConfig.php

WaterMarkConfig.php in WPWaterMark 轻水印插件 5.1.7, at WaterMarkConfig.php

198 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WaterMark Configuration Class
4 *
5 * Manages watermark settings with validation and defaults
6 */
7 class WaterMarkConfig {
8 // Default settings
9 private static $defaults = [
10 'watermark_enabled' => '0',
11 'watermark_type' => 'text_watermark',
12 'text_content' => '',
13 'text_font' => 'simhei.ttf',
14 'text_angle' => '0',
15 'text_size' => '14',
16 'text_color' => '#790000',
17 'watermark_mark_image' => '',
18 'watermark_position' => 'bottom-right',
19 'watermark_margin' => '50',
20 'watermark_diaphaneity' => '100',
21 'watermark_min_width' => '300',
22 'watermark_min_height' => '300',
23 'watermark_extension_whitelist' => ''
24 ];
25
26 private $options;
27
28 /**
29 * Constructor
30 *
31 * @param array $options Optional custom options
32 */
33 public function __construct($options = []) {
34 $this->options = wp_parse_args($options, self::$defaults);
35 $this->validateOptions();
36 }
37
38 /**
39 * Validate all options
40 */
41 private function validateOptions() {
42 // Validate watermark type
43 if (!in_array($this->options['watermark_type'], ['text_watermark', 'image_watermark'])) {
44 $this->options['watermark_type'] = self::$defaults['watermark_type'];
45 }
46
47 // Validate numeric values
48 $this->options['text_angle'] = $this->validateNumeric('text_angle', 0, 360);
49 $this->options['text_size'] = $this->validateNumeric('text_size', 8, 72);
50 $this->options['watermark_margin'] = $this->validateNumeric('watermark_margin', 0, 200);
51 $this->options['watermark_diaphaneity'] = $this->validateNumeric('watermark_diaphaneity', 0, 100);
52 $this->options['watermark_min_width'] = $this->validateNumeric('watermark_min_width', 100, 9999);
53 $this->options['watermark_min_height'] = $this->validateNumeric('watermark_min_height', 100, 9999);
54
55 // Validate watermark position(九宫格固定位置 + random 随机一格)
56 $valid_positions = [
57 'random',
58 'top-left', 'top-center', 'top-right',
59 'middle-left', 'middle-center', 'middle-right',
60 'bottom-left', 'bottom-center', 'bottom-right'
61 ];
62 if (!in_array($this->options['watermark_position'], $valid_positions, true)) {
63 $this->options['watermark_position'] = self::$defaults['watermark_position'];
64 }
65
66 // Validate color
67 $this->options['text_color'] = $this->validateColor($this->options['text_color']);
68
69 // Validate text content
70 $this->options['text_content'] = sanitize_text_field($this->options['text_content']);
71
72 // Validate font file
73 if (!$this->validateFont($this->options['text_font'])) {
74 $this->options['text_font'] = self::$defaults['text_font'];
75 }
76
77 // Validate image URL if using image watermark
78 if ($this->options['watermark_type'] === 'image_watermark') {
79 $this->options['watermark_mark_image'] = $this->validateImageUrl($this->options['watermark_mark_image']);
80 }
81
82 // Validate extension whitelist
83 $this->options['watermark_extension_whitelist'] = $this->normalizeExtensionList(
84 $this->options['watermark_extension_whitelist']
85 );
86 }
87
88 /**
89 * Validate numeric value
90 */
91 private function validateNumeric($key, $min, $max) {
92 $value = intval($this->options[$key]);
93 return max($min, min($max, $value));
94 }
95
96 /**
97 * Validate color value
98 */
99 private function validateColor($color) {
100 if (preg_match('/^#[a-fA-F0-9]{6}$/', $color)) {
101 return $color;
102 }
103 return self::$defaults['text_color'];
104 }
105
106 /**
107 * Validate font file
108 */
109 private function validateFont($font) {
110 $font_path = plugin_dir_path(__FILE__) . 'fonts/' . $font;
111 return file_exists($font_path) && is_readable($font_path);
112 }
113
114 /**
115 * Validate image URL
116 */
117 private function validateImageUrl($url) {
118 if (empty($url)) {
119 return '';
120 }
121
122 // Basic URL validation
123 if (!filter_var($url, FILTER_VALIDATE_URL)) {
124 return '';
125 }
126
127 // Check if image exists and is accessible
128 $headers = get_headers($url, 1);
129 if (strpos($headers[0], '200') === false ||
130 !preg_match('/^image\/(jpeg|png|gif|webp)/', $headers['Content-Type'])) {
131 return '';
132 }
133
134 return esc_url_raw($url);
135 }
136
137 /**
138 * Normalize extension whitelist to lowercase CSV
139 */
140 private function normalizeExtensionList($extensions) {
141 $extensions = is_string($extensions) ? strtolower($extensions) : '';
142 $extensions = explode(',', $extensions);
143 $normalized = [];
144
145 foreach ($extensions as $ext) {
146 $ext = trim($ext);
147 $ext = ltrim($ext, '.');
148 if ($ext === '') {
149 continue;
150 }
151 if (preg_match('/^[a-z0-9]+$/', $ext)) {
152 $normalized[] = $ext;
153 }
154 }
155
156 $normalized = array_values(array_unique($normalized));
157 return implode(',', $normalized);
158 }
159
160 /**
161 * Get all options
162 */
163 public function getOptions() {
164 return $this->options;
165 }
166
167 /**
168 * Get single option
169 */
170 public function getOption($key) {
171 return isset($this->options[$key]) ? $this->options[$key] : null;
172 }
173
174 /**
175 * Set single option
176 */
177 public function setOption($key, $value) {
178 if (array_key_exists($key, self::$defaults)) {
179 $this->options[$key] = $value;
180 $this->validateOptions();
181 }
182 }
183
184 /**
185 * Save options to WordPress
186 */
187 public function saveOptions() {
188 return update_option('wpwatermark_options', $this->options);
189 }
190
191 /**
192 * Load options from WordPress
193 */
194 public static function loadFromWordPress() {
195 $options = get_option('wpwatermark_options', []);
196 return new self($options);
197 }
198 }