| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
class WB_IMGSPY_Post extends IMGSPY_Base |
| 5 |
{ |
| 6 |
|
| 7 |
public static $last_err = null; |
| 8 |
|
| 9 |
protected static $mime_to_ext = array( |
| 10 |
'image/webp' => 'webp', |
| 11 |
'image/jpeg' => 'jpg', |
| 12 |
'image/png' => 'png', |
| 13 |
'image/gif' => 'gif', |
| 14 |
'image/bmp' => 'bmp', |
| 15 |
'image/tiff' => 'tif', |
| 16 |
'image/avif' => 'avif', |
| 17 |
'image/apng' => 'apng', |
| 18 |
); |
| 19 |
|
| 20 |
protected static $ext_to_mime = array( |
| 21 |
'webp' => 'image/webp', |
| 22 |
'jpg' => 'image/jpeg', |
| 23 |
'jpeg' => 'image/jpeg', |
| 24 |
'png' => 'image/png', |
| 25 |
'gif' => 'image/gif', |
| 26 |
'bmp' => 'image/bmp', |
| 27 |
'tif' => 'image/tiff', |
| 28 |
'avif' => 'image/avif', |
| 29 |
'apng' => 'image/apng', |
| 30 |
); |
| 31 |
|
| 32 |
public static function supports_avif() |
| 33 |
{ |
| 34 |
static $ok = null; |
| 35 |
if ($ok !== null) { |
| 36 |
return $ok; |
| 37 |
} |
| 38 |
$ok = function_exists('wp_image_editor_supports') |
| 39 |
&& wp_image_editor_supports(array('mime_type' => 'image/avif')); |
| 40 |
return $ok; |
| 41 |
} |
| 42 |
|
| 43 |
public static function supports_apng() |
| 44 |
{ |
| 45 |
$cnf = WB_IMGSPY_Conf::cnf('allow_apng', 1); |
| 46 |
return (int) $cnf === 1; |
| 47 |
} |
| 48 |
|
| 49 |
public static function collectable_exts() |
| 50 |
{ |
| 51 |
$exts = array('jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp', 'tif'); |
| 52 |
if (self::supports_avif()) { |
| 53 |
$exts[] = 'avif'; |
| 54 |
} |
| 55 |
if (self::supports_apng()) { |
| 56 |
$exts[] = 'apng'; |
| 57 |
} |
| 58 |
return apply_filters('imgspy_collectable_exts', $exts); |
| 59 |
} |
| 60 |
|
| 61 |
public static function normalize_source_url($url) |
| 62 |
{ |
| 63 |
$url = trim((string) $url); |
| 64 |
if ($url === '') { |
| 65 |
return ''; |
| 66 |
} |
| 67 |
$url = str_replace('&', '&', $url); |
| 68 |
$parts = wp_parse_url($url); |
| 69 |
if (!$parts || empty($parts['host']) || empty($parts['scheme'])) { |
| 70 |
return ''; |
| 71 |
} |
| 72 |
$scheme = strtolower($parts['scheme']); |
| 73 |
if ($scheme !== 'http' && $scheme !== 'https') { |
| 74 |
return ''; |
| 75 |
} |
| 76 |
$host = strtolower($parts['host']); |
| 77 |
$path = isset($parts['path']) ? $parts['path'] : '/'; |
| 78 |
$query = isset($parts['query']) ? $parts['query'] : ''; |
| 79 |
$norm = $scheme . '://' . $host . $path; |
| 80 |
if ($query !== '') { |
| 81 |
$norm .= '?' . $query; |
| 82 |
} |
| 83 |
return $norm; |
| 84 |
} |
| 85 |
|
| 86 |
public static function find_reuse_attachment($image_url) |
| 87 |
{ |
| 88 |
if (!apply_filters('imgspy_reuse_attachment', true, $image_url)) { |
| 89 |
return false; |
| 90 |
} |
| 91 |
$norm = self::normalize_source_url($image_url); |
| 92 |
if ($norm === '') { |
| 93 |
return false; |
| 94 |
} |
| 95 |
$db = self::db(); |
| 96 |
$id = (int) $db->get_var($db->prepare( |
| 97 |
"SELECT post_id FROM $db->postmeta WHERE meta_key=%s AND meta_value=%s ORDER BY post_id DESC LIMIT 1", |
| 98 |
'_wb_imgspy_source', |
| 99 |
$norm |
| 100 |
)); |
| 101 |
if ($id < 1) { |
| 102 |
return false; |
| 103 |
} |
| 104 |
$file = get_attached_file($id); |
| 105 |
if (!$file || !file_exists($file)) { |
| 106 |
return false; |
| 107 |
} |
| 108 |
$meta = wp_get_attachment_metadata($id); |
| 109 |
$url = wp_get_attachment_url($id); |
| 110 |
if (!$url) { |
| 111 |
return false; |
| 112 |
} |
| 113 |
return array( |
| 114 |
'id' => $id, |
| 115 |
'url' => $url, |
| 116 |
'title' => get_the_title($id), |
| 117 |
'width' => isset($meta['width']) ? (int) $meta['width'] : 0, |
| 118 |
'height' => isset($meta['height']) ? (int) $meta['height'] : 0, |
| 119 |
'reused' => 1, |
| 120 |
); |
| 121 |
} |
| 122 |
|
| 123 |
public static function remember_source($attachment_id, $image_url) |
| 124 |
{ |
| 125 |
$norm = self::normalize_source_url($image_url); |
| 126 |
if ($norm === '' || (int) $attachment_id < 1) { |
| 127 |
return; |
| 128 |
} |
| 129 |
if (strpos($norm, 'file://') === 0) { |
| 130 |
return; |
| 131 |
} |
| 132 |
update_post_meta($attachment_id, '_wb_imgspy_source', $norm); |
| 133 |
} |
| 134 |
|
| 135 |
public static function update_post($post_ID, $post, $update, $image_list, $img_html_list) |
| 136 |
{ |
| 137 |
|
| 138 |
$content = $post->post_content; |
| 139 |
$cnf = WB_IMGSPY_Conf::opt(); |
| 140 |
$idx = -1; |
| 141 |
foreach ($img_html_list as $r) { |
| 142 |
|
| 143 |
if (!isset($image_list[$r['key']])) { |
| 144 |
continue; |
| 145 |
} |
| 146 |
$idx++; |
| 147 |
$ret = $image_list[$r['key']]; |
| 148 |
$mode = isset($r['mode']) ? $r['mode'] : 'tag'; |
| 149 |
if ($mode === 'url' && !empty($r['html'])) { |
| 150 |
$content = str_replace($r['html'], esc_url($ret['url']), $content); |
| 151 |
} else { |
| 152 |
$new_html = self::img_html($ret, $idx, $post->post_title, $cnf); |
| 153 |
$content = str_replace($r['html'], $new_html, $content); |
| 154 |
} |
| 155 |
|
| 156 |
if (!$idx) { |
| 157 |
self::update_post_thumb($post_ID, $ret['id']); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
if ($cnf['del_src_url']) { |
| 162 |
$content = self::strip_image_links($content); |
| 163 |
} |
| 164 |
|
| 165 |
if ($idx > -1) { |
| 166 |
wp_update_post(array('ID' => $post_ID, 'post_content' => $content)); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
public static function update_post_thumb($post_ID, $thumb_id) |
| 171 |
{ |
| 172 |
|
| 173 |
$thumbnail = WB_IMGSPY_Conf::cnf('thumbnail'); |
| 174 |
if (!$thumbnail) { |
| 175 |
return; |
| 176 |
} |
| 177 |
|
| 178 |
$_thumbnail_id = get_post_meta($post_ID, '_thumbnail_id', true); |
| 179 |
if ($_thumbnail_id) { |
| 180 |
return; |
| 181 |
} |
| 182 |
|
| 183 |
update_post_meta($post_ID, '_thumbnail_id', $thumb_id); |
| 184 |
} |
| 185 |
|
| 186 |
public static function upload_image_name($image_url) |
| 187 |
{ |
| 188 |
|
| 189 |
$image_path = wp_parse_url($image_url, PHP_URL_PATH); |
| 190 |
$filename = basename((string) $image_path); |
| 191 |
$ext_group = implode('|', self::collectable_exts()); |
| 192 |
if (!preg_match('#\.(' . $ext_group . ')$#i', $filename)) { |
| 193 |
$ext = '.jpg'; |
| 194 |
if (preg_match('#wx_fmt=([a-z0-9]+)#i', $image_url, $m)) { |
| 195 |
if (preg_match('#^(' . $ext_group . ')$#i', $m[1])) { |
| 196 |
$ext = '.' . strtolower($m[1]); |
| 197 |
} |
| 198 |
} |
| 199 |
$filename .= $ext; |
| 200 |
} |
| 201 |
|
| 202 |
$filename = urldecode($filename); |
| 203 |
$filename = str_replace(array('%20', ' '), '_', $filename); |
| 204 |
$filename = str_replace('*', '', $filename); |
| 205 |
|
| 206 |
add_filter('upload_mimes', function ($mimes) { |
| 207 |
$site_exts = self::collectable_exts(); |
| 208 |
$site_mimes = array(); |
| 209 |
foreach ($site_exts as $ext) { |
| 210 |
foreach ($mimes as $ext_pattern => $mime) { |
| 211 |
if ('' !== $ext && strpos($ext_pattern, $ext) !== false) { |
| 212 |
$site_mimes[$ext_pattern] = $mime; |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
if (self::supports_avif() && !isset($site_mimes['avif'])) { |
| 217 |
$site_mimes['avif'] = 'image/avif'; |
| 218 |
} |
| 219 |
if (self::supports_apng() && !isset($site_mimes['apng'])) { |
| 220 |
$site_mimes['apng'] = 'image/apng'; |
| 221 |
} |
| 222 |
return $site_mimes; |
| 223 |
}); |
| 224 |
$validate = wp_check_filetype($filename); |
| 225 |
if ($validate['type'] === false) { |
| 226 |
$filename .= '.jpg'; |
| 227 |
} |
| 228 |
|
| 229 |
$config = WB_IMGSPY_Conf::opt(); |
| 230 |
|
| 231 |
$rule = $config['rule']; |
| 232 |
if ($rule['file_name'] == '2' && $rule['custom_name']) { |
| 233 |
$pos = strrpos($filename, '.'); |
| 234 |
$ext = substr($filename, $pos); |
| 235 |
$name = substr($filename, 0, $pos); |
| 236 |
|
| 237 |
$ymd = explode('-', current_time('Y-m-d')); |
| 238 |
$random = self::random(5); |
| 239 |
$search = array('%filename%', '%date%', '%year%', '%month%', '%day%', '%random%'); |
| 240 |
$replace = array($name, implode('', $ymd), $ymd[0], $ymd[1], $ymd[2], $random); |
| 241 |
$filename = str_replace($search, $replace, $rule['custom_name']) . $ext; |
| 242 |
} |
| 243 |
|
| 244 |
return $filename; |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
public static function random($num = 5) |
| 249 |
{ |
| 250 |
$str = 'abcdefghijklmnopqrstuvwxyz0123456789'; |
| 251 |
$len = strlen($str); |
| 252 |
$a = array(); |
| 253 |
for ($i = 0; $i < $num; $i++) { |
| 254 |
$j = wp_rand(0, $len - 1); |
| 255 |
$a[] = $str[$j]; |
| 256 |
} |
| 257 |
return implode('', $a); |
| 258 |
} |
| 259 |
|
| 260 |
public static function upload_img_file($file_id, $post_id, $post_date, &$error = null) |
| 261 |
{ |
| 262 |
if (!isset($_FILES[$file_id]) || empty($_FILES[$file_id])) { |
| 263 |
$error = 'empty file'; |
| 264 |
return false; |
| 265 |
} |
| 266 |
$file = $_FILES[$file_id]; |
| 267 |
if (isset($file['error']) && $file['error'] > 0) { |
| 268 |
$error = 'error [' . $file['error'] . ']'; |
| 269 |
return false; |
| 270 |
} |
| 271 |
if (!isset($file['tmp_name']) || !$file['tmp_name']) { |
| 272 |
$error = 'empty tmp file'; |
| 273 |
return false; |
| 274 |
} |
| 275 |
$filename = self::param('filename'); |
| 276 |
if ($filename) { |
| 277 |
$file['name'] = sanitize_text_field($filename); |
| 278 |
} |
| 279 |
|
| 280 |
if (!$file['name']) { |
| 281 |
$error = 'empty file name'; |
| 282 |
return false; |
| 283 |
} |
| 284 |
$ext_group = implode('|', self::collectable_exts()); |
| 285 |
if (!preg_match('#(' . $ext_group . ')$#i', $file['type']) && !preg_match('#\.(' . $ext_group . ')$#i', $file['name'])) { |
| 286 |
$error = 'not image file'; |
| 287 |
return false; |
| 288 |
} |
| 289 |
|
| 290 |
$image_url = 'file://fackpath/' . $file['name']; |
| 291 |
|
| 292 |
return self::save_image_data($file['tmp_name'], $image_url, $post_id, $post_date, true); |
| 293 |
} |
| 294 |
|
| 295 |
public static function upload_img_base64($dataurl, $post_id, $post_date) |
| 296 |
{ |
| 297 |
|
| 298 |
list($data, $image) = explode(';', $dataurl); |
| 299 |
list($field, $type) = explode(':', $data); |
| 300 |
list($encoding, $content) = explode(',', $image); |
| 301 |
$extension = ''; |
| 302 |
if ($type == 'image/png') { |
| 303 |
$extension = 'png'; |
| 304 |
} else if ($type == 'image/jpeg') { |
| 305 |
$extension = 'jpg'; |
| 306 |
} else if ($type == 'image/webp') { |
| 307 |
$extension = 'webp'; |
| 308 |
} else if ($type == 'image/avif' && self::supports_avif()) { |
| 309 |
$extension = 'avif'; |
| 310 |
} else if ($type == 'image/apng' && self::supports_apng()) { |
| 311 |
$extension = 'apng'; |
| 312 |
} else { |
| 313 |
return false; |
| 314 |
} |
| 315 |
$name = md5($dataurl); |
| 316 |
$filename = self::param('filename'); |
| 317 |
if ($filename) { |
| 318 |
$name = sanitize_text_field($filename); |
| 319 |
} |
| 320 |
|
| 321 |
$ext_group = implode('|', self::collectable_exts()); |
| 322 |
if (!preg_match('#\.(' . $ext_group . ')$#i', $name)) { |
| 323 |
$name = $name . '.' . $extension; |
| 324 |
} |
| 325 |
|
| 326 |
$image_url = 'file://fackpath/' . $name; |
| 327 |
|
| 328 |
return self::save_image_data(base64_decode($content), $image_url, $post_id, $post_date); |
| 329 |
} |
| 330 |
|
| 331 |
public static function upload($image_url, $post_id, $post_date) |
| 332 |
{ |
| 333 |
$reuse = self::find_reuse_attachment($image_url); |
| 334 |
if ($reuse) { |
| 335 |
return $reuse; |
| 336 |
} |
| 337 |
|
| 338 |
$arg = array(); |
| 339 |
if ($_SERVER && isset($_SERVER['HTTP_USER_AGENT'])) { |
| 340 |
$arg['user-agent'] = $_SERVER['HTTP_USER_AGENT']; |
| 341 |
} |
| 342 |
|
| 343 |
@ini_set('memory_limit', '50M'); |
| 344 |
|
| 345 |
$image_data = WB_IMGSPY_Down::down($image_url, $arg); |
| 346 |
|
| 347 |
if (!$image_data) { |
| 348 |
self::$last_err = WB_IMGSPY_Down::$last_err; |
| 349 |
return $image_data; |
| 350 |
} |
| 351 |
|
| 352 |
return self::save_image_data($image_data, $image_url, $post_id, $post_date); |
| 353 |
} |
| 354 |
|
| 355 |
public static function is_animated_image($file, $mime_type) |
| 356 |
{ |
| 357 |
if (!is_readable($file)) { |
| 358 |
return false; |
| 359 |
} |
| 360 |
$mime_type = strtolower((string) $mime_type); |
| 361 |
$head = (string) file_get_contents($file, false, null, 0, 4096); |
| 362 |
if ($head === '') { |
| 363 |
return false; |
| 364 |
} |
| 365 |
if (strpos($mime_type, 'gif') !== false) { |
| 366 |
return preg_match('#\x00\x21\xF9\x04.{4}\x00[\x2C\x21]#s', $head) && substr_count($head, "\x00\x21\xF9\x04") > 1; |
| 367 |
} |
| 368 |
if (strpos($mime_type, 'webp') !== false) { |
| 369 |
return strpos($head, 'ANIM') !== false || strpos($head, 'ANMF') !== false; |
| 370 |
} |
| 371 |
if (strpos($mime_type, 'png') !== false || strpos($mime_type, 'apng') !== false) { |
| 372 |
return strpos($head, 'acTL') !== false; |
| 373 |
} |
| 374 |
if (strpos($mime_type, 'avif') !== false) { |
| 375 |
return (bool) preg_match('#avis|animation#i', $head); |
| 376 |
} |
| 377 |
return false; |
| 378 |
} |
| 379 |
|
| 380 |
public static function maybe_convert_webp($file, &$mime_type, &$filename) |
| 381 |
{ |
| 382 |
$on = (int) WB_IMGSPY_Conf::cnf('convert_webp', 0); |
| 383 |
if (!$on) { |
| 384 |
return $file; |
| 385 |
} |
| 386 |
if (!apply_filters('imgspy_convert_webp', true, $file, $mime_type)) { |
| 387 |
return $file; |
| 388 |
} |
| 389 |
if (self::is_animated_image($file, $mime_type)) { |
| 390 |
return $file; |
| 391 |
} |
| 392 |
if (strpos((string) $mime_type, 'webp') !== false) { |
| 393 |
return $file; |
| 394 |
} |
| 395 |
if (!function_exists('wp_image_editor_supports') || !wp_image_editor_supports(array('mime_type' => 'image/webp'))) { |
| 396 |
return $file; |
| 397 |
} |
| 398 |
$editor = wp_get_image_editor($file); |
| 399 |
if (is_wp_error($editor)) { |
| 400 |
return $file; |
| 401 |
} |
| 402 |
$dest = preg_replace('#\.[a-z0-9]+$#i', '.webp', $file); |
| 403 |
$saved = $editor->save($dest, 'image/webp'); |
| 404 |
if (is_wp_error($saved) || empty($saved['path'])) { |
| 405 |
return $file; |
| 406 |
} |
| 407 |
if ($saved['path'] !== $file && file_exists($file)) { |
| 408 |
@unlink($file); |
| 409 |
} |
| 410 |
$mime_type = 'image/webp'; |
| 411 |
$filename = preg_replace('#\.[a-z0-9]+$#i', '.webp', $filename); |
| 412 |
return $saved['path']; |
| 413 |
} |
| 414 |
|
| 415 |
private static function save_image_data($image_data, $image_url, $post_id, $post_date, $upload = false) |
| 416 |
{ |
| 417 |
do { |
| 418 |
$filename = self::upload_image_name($image_url); |
| 419 |
$time = false; |
| 420 |
if ($post_date) { |
| 421 |
$time = gmdate('Y/m', strtotime($post_date)); |
| 422 |
} |
| 423 |
$uploads = wp_upload_dir($time); |
| 424 |
|
| 425 |
$filename = urldecode($filename); |
| 426 |
$filename = str_replace(array('%20', ' '), '_', $filename); |
| 427 |
$unique_filename_callback = null; |
| 428 |
$filename = wp_unique_filename($uploads['path'], $filename, $unique_filename_callback); |
| 429 |
|
| 430 |
$new_file = $uploads['path'] . '/' . $filename; |
| 431 |
|
| 432 |
if ($upload) { |
| 433 |
$move_new_file = @move_uploaded_file($image_data, $new_file); |
| 434 |
if (!$move_new_file) { |
| 435 |
self::$last_err = 'save new file fail'; |
| 436 |
break; |
| 437 |
} |
| 438 |
} else { |
| 439 |
if (!file_put_contents($new_file, $image_data)) { |
| 440 |
self::$last_err = 'save new file fail'; |
| 441 |
break; |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
$url = $uploads['url'] . '/' . $filename; |
| 446 |
|
| 447 |
$name_parts = pathinfo($filename); |
| 448 |
|
| 449 |
$name = $name_parts['filename']; |
| 450 |
|
| 451 |
$title = $name; |
| 452 |
$content = ''; |
| 453 |
|
| 454 |
$ret = array( |
| 455 |
'title' => $title, |
| 456 |
'url' => $url, |
| 457 |
); |
| 458 |
|
| 459 |
$mine_type = wp_get_image_mime($new_file); |
| 460 |
|
| 461 |
if (!$mine_type && isset($name_parts['extension']) && isset(self::$ext_to_mime[$name_parts['extension']])) { |
| 462 |
$mine_type = self::$ext_to_mime[$name_parts['extension']]; |
| 463 |
} |
| 464 |
|
| 465 |
if ($mine_type === 'image/svg+xml' || (isset($name_parts['extension']) && strtolower($name_parts['extension']) === 'svg')) { |
| 466 |
@unlink($new_file); |
| 467 |
self::$last_err = 'svg not allowed'; |
| 468 |
break; |
| 469 |
} |
| 470 |
|
| 471 |
if (strpos((string) $mine_type, 'avif') !== false && !self::supports_avif()) { |
| 472 |
@unlink($new_file); |
| 473 |
self::$last_err = 'avif not supported'; |
| 474 |
break; |
| 475 |
} |
| 476 |
|
| 477 |
$converted = self::maybe_convert_webp($new_file, $mine_type, $filename); |
| 478 |
if ($converted !== $new_file) { |
| 479 |
$new_file = $converted; |
| 480 |
$url = $uploads['url'] . '/' . $filename; |
| 481 |
$ret['url'] = $url; |
| 482 |
$ret['title'] = pathinfo($filename, PATHINFO_FILENAME); |
| 483 |
} |
| 484 |
|
| 485 |
WB_IMGSPY_Image::resize_image($new_file, $mine_type); |
| 486 |
|
| 487 |
$attachment = array( |
| 488 |
'post_mime_type' => $mine_type, |
| 489 |
'guid' => $url, |
| 490 |
'post_parent' => $post_id, |
| 491 |
'post_title' => $ret['title'], |
| 492 |
'post_content' => $content |
| 493 |
); |
| 494 |
|
| 495 |
$id = wp_insert_attachment($attachment, $new_file, $post_id); |
| 496 |
|
| 497 |
if (is_wp_error($id)) { |
| 498 |
self::$last_err = 'insert attachment fail [' . $id->get_error_message() . ']'; |
| 499 |
return $ret; |
| 500 |
} |
| 501 |
|
| 502 |
$ret['id'] = $id; |
| 503 |
self::remember_source($id, $image_url); |
| 504 |
|
| 505 |
if (!function_exists('wp_generate_attachment_metadata')) { |
| 506 |
require_once ABSPATH . 'wp-admin/includes/image.php'; |
| 507 |
} |
| 508 |
if (!function_exists('wp_crop_image')) { |
| 509 |
require_once ABSPATH . 'wp-admin/includes/image.php'; |
| 510 |
} |
| 511 |
|
| 512 |
$metadata = wp_generate_attachment_metadata($id, $new_file); |
| 513 |
if (!$metadata) { |
| 514 |
self::$last_err = 'generate attachment meta data fail '; |
| 515 |
return $ret; |
| 516 |
} |
| 517 |
|
| 518 |
if (is_wp_error($metadata)) { |
| 519 |
self::$last_err = 'generate attachment meta data fail [' . $metadata->get_error_message() . ']'; |
| 520 |
return $ret; |
| 521 |
} else if (!isset($metadata['file'])) { |
| 522 |
self::$last_err = 'generate attachment meta data fail [empty file]'; |
| 523 |
return $ret; |
| 524 |
} else { |
| 525 |
wp_update_attachment_metadata($id, $metadata); |
| 526 |
$ret['width'] = $metadata['width']; |
| 527 |
$ret['height'] = $metadata['height']; |
| 528 |
} |
| 529 |
return $ret; |
| 530 |
} while (false); |
| 531 |
|
| 532 |
return false; |
| 533 |
} |
| 534 |
|
| 535 |
|
| 536 |
public static function img_html($ret, $idx, $post_title, $config) |
| 537 |
{ |
| 538 |
|
| 539 |
$align = 'none'; |
| 540 |
$rule = isset($config['rule']) ? $config['rule'] : array(); |
| 541 |
if (!empty($rule['align'])) { |
| 542 |
$align = $rule['align']; |
| 543 |
} |
| 544 |
|
| 545 |
if (!empty($rule['title_alt']) && (string) $rule['title_alt'] === '1') { |
| 546 |
$alt_title = str_replace( |
| 547 |
array('%filename%', '%postname%'), |
| 548 |
array($ret['title'], $post_title), |
| 549 |
isset($rule['custom_title']) ? $rule['custom_title'] : '' |
| 550 |
) . '-' . ($idx + 1); |
| 551 |
} else { |
| 552 |
$alt_title = $ret['title']; |
| 553 |
} |
| 554 |
|
| 555 |
$attr = array( |
| 556 |
'class' => 'align' . $align . ' size-full', |
| 557 |
'alt' => $alt_title, |
| 558 |
'title' => $alt_title, |
| 559 |
); |
| 560 |
if (!empty($ret['id'])) { |
| 561 |
$attr['class'] .= ' wp-image-' . (int) $ret['id']; |
| 562 |
$html = wp_get_attachment_image((int) $ret['id'], 'full', false, $attr); |
| 563 |
if ($html) { |
| 564 |
return $html; |
| 565 |
} |
| 566 |
} |
| 567 |
|
| 568 |
$img_html = '<img class="' . esc_attr($attr['class']) . '" src="' . esc_url($ret['url']) . '"'; |
| 569 |
if (!empty($ret['width'])) { |
| 570 |
$img_html .= ' width="' . (int) $ret['width'] . '"'; |
| 571 |
} |
| 572 |
if (!empty($ret['height'])) { |
| 573 |
$img_html .= ' height="' . (int) $ret['height'] . '"'; |
| 574 |
} |
| 575 |
$img_html .= ' alt="' . esc_attr($alt_title) . '" title="' . esc_attr($alt_title) . '" />'; |
| 576 |
|
| 577 |
return $img_html; |
| 578 |
} |
| 579 |
|
| 580 |
public static function strip_image_links($content) |
| 581 |
{ |
| 582 |
if (!is_string($content) || $content === '') { |
| 583 |
return $content; |
| 584 |
} |
| 585 |
if (!preg_match_all('#(<a[^>]+>)\s*(<img[^>]+>)\s*</a>#is', $content, $match)) { |
| 586 |
return $content; |
| 587 |
} |
| 588 |
foreach ($match[0] as $k => $full) { |
| 589 |
$content = str_replace($full, $match[2][$k], $content); |
| 590 |
} |
| 591 |
return $content; |
| 592 |
} |
| 593 |
|
| 594 |
protected static function should_skip_url($img, $cnf, $allow_domain, $file_ext) |
| 595 |
{ |
| 596 |
if (!preg_match('#^https?://#is', $img)) { |
| 597 |
return true; |
| 598 |
} |
| 599 |
foreach ($allow_domain as $domain) { |
| 600 |
if ($domain && strpos($img, $domain) !== false) { |
| 601 |
return true; |
| 602 |
} |
| 603 |
} |
| 604 |
$img_name = self::upload_image_name($img); |
| 605 |
if ($file_ext) { |
| 606 |
foreach ($file_ext as $ext) { |
| 607 |
if (preg_match('#\.' . preg_quote($ext, '#') . '#i', $img_name)) { |
| 608 |
return true; |
| 609 |
} |
| 610 |
} |
| 611 |
} |
| 612 |
$ext_group = implode('|', self::collectable_exts()); |
| 613 |
$path = (string) wp_parse_url($img, PHP_URL_PATH); |
| 614 |
if (preg_match('#\.svg(\?|$)#i', $path)) { |
| 615 |
return true; |
| 616 |
} |
| 617 |
if (preg_match('#\.avif(\?|$)#i', $path) && !self::supports_avif()) { |
| 618 |
return true; |
| 619 |
} |
| 620 |
if (preg_match('#\.apng(\?|$)#i', $path) && !self::supports_apng()) { |
| 621 |
return true; |
| 622 |
} |
| 623 |
return false; |
| 624 |
} |
| 625 |
|
| 626 |
protected static function push_found(&$img_list, &$find_img_html, $img, $snippet, $mode) |
| 627 |
{ |
| 628 |
$img = rawurldecode($img); |
| 629 |
$img = str_replace('&', '&', $img); |
| 630 |
$key = md5($img); |
| 631 |
$img_list[$key] = $img; |
| 632 |
$find_img_html[] = array( |
| 633 |
'key' => $key, |
| 634 |
'html' => $snippet, |
| 635 |
'mode' => $mode, |
| 636 |
); |
| 637 |
} |
| 638 |
|
| 639 |
public static function find_img_src($post, &$find_img_html = array()) |
| 640 |
{ |
| 641 |
|
| 642 |
$cnf = WB_IMGSPY_Conf::opt(); |
| 643 |
$content = $post->post_content; |
| 644 |
if (!$content) { |
| 645 |
return false; |
| 646 |
} |
| 647 |
|
| 648 |
$host_name = wp_parse_url(home_url(), PHP_URL_HOST); |
| 649 |
$host_name = str_replace('www.', '', (string) $host_name); |
| 650 |
|
| 651 |
$allow_domain = array(); |
| 652 |
if (!isset($cnf['filter'])) { |
| 653 |
$cnf['filter'] = array(); |
| 654 |
} |
| 655 |
$filter = $cnf['filter']; |
| 656 |
if (isset($filter['domain']) && $filter['domain'] && is_array($filter['domain'])) { |
| 657 |
$allow_domain = $filter['domain']; |
| 658 |
} |
| 659 |
array_push($allow_domain, $host_name); |
| 660 |
|
| 661 |
$file_ext = array(); |
| 662 |
if (isset($filter['type']) && $filter['type']) { |
| 663 |
if (is_array($filter['type'])) { |
| 664 |
foreach ($filter['type'] as $type => $active) { |
| 665 |
if ($active) { |
| 666 |
$file_ext[] = $type; |
| 667 |
} |
| 668 |
} |
| 669 |
} |
| 670 |
} |
| 671 |
|
| 672 |
$except_index = array(); |
| 673 |
if (isset($filter['except_index']) && $filter['except_index']) { |
| 674 |
$except_index = explode(',', $filter['except_index']); |
| 675 |
} |
| 676 |
|
| 677 |
$min_width = intval(isset($filter['min_width']) ? $filter['min_width'] : 0); |
| 678 |
|
| 679 |
$img_list = array(); |
| 680 |
$img_tags = array(); |
| 681 |
if (preg_match_all('#<img[^>]+>#is', $content, $match)) { |
| 682 |
$img_tags = $match[0]; |
| 683 |
} |
| 684 |
if (in_array('z', $except_index, true)) { |
| 685 |
$except_index[] = (string) count($img_tags); |
| 686 |
} |
| 687 |
|
| 688 |
foreach ($img_tags as $k => $img_html) { |
| 689 |
if ($except_index && in_array((string) ($k + 1), $except_index, true)) { |
| 690 |
continue; |
| 691 |
} |
| 692 |
|
| 693 |
if (preg_match('#data-src=([^\s]+)#is', $img_html, $img_match)) { |
| 694 |
} else if (preg_match('#src=([^\s]+)#is', $img_html, $img_match)) { |
| 695 |
} else { |
| 696 |
continue; |
| 697 |
} |
| 698 |
$img_src = trim(preg_replace('#/?>$#', '', $img_match[1]), '\'"'); |
| 699 |
if (self::should_skip_url($img_src, $cnf, $allow_domain, $file_ext)) { |
| 700 |
continue; |
| 701 |
} |
| 702 |
|
| 703 |
if ($min_width && preg_match('#width=([^\s]+)#i', $img_html, $width_match)) { |
| 704 |
$width = intval(trim($width_match[1], "\"'")); |
| 705 |
if ($width && $width < $min_width) { |
| 706 |
continue; |
| 707 |
} |
| 708 |
} |
| 709 |
|
| 710 |
self::push_found($img_list, $find_img_html, $img_src, $img_html, 'tag'); |
| 711 |
} |
| 712 |
|
| 713 |
if (preg_match_all('#(?:srcset|data-srcset)=([\'"])([^\'"]+)\1#i', $content, $srcset_m)) { |
| 714 |
foreach ($srcset_m[2] as $srcset) { |
| 715 |
if (!preg_match_all('#https?://[^\s,]+#i', $srcset, $urls)) { |
| 716 |
continue; |
| 717 |
} |
| 718 |
foreach ($urls[0] as $u) { |
| 719 |
$u = rtrim($u, ','); |
| 720 |
if (self::should_skip_url($u, $cnf, $allow_domain, $file_ext)) { |
| 721 |
continue; |
| 722 |
} |
| 723 |
self::push_found($img_list, $find_img_html, $u, $u, 'url'); |
| 724 |
} |
| 725 |
} |
| 726 |
} |
| 727 |
|
| 728 |
if (preg_match_all('#background-image\s*:\s*url\((["\']?)(https?://[^)\'"]+)\1\)#i', $content, $bg_m)) { |
| 729 |
foreach ($bg_m[2] as $u) { |
| 730 |
if (self::should_skip_url($u, $cnf, $allow_domain, $file_ext)) { |
| 731 |
continue; |
| 732 |
} |
| 733 |
self::push_found($img_list, $find_img_html, $u, $u, 'url'); |
| 734 |
} |
| 735 |
} |
| 736 |
|
| 737 |
return $img_list ? $img_list : false; |
| 738 |
} |
| 739 |
} |
| 740 |
|