文章目录[隐藏]
实战教程:为WordPress网站集成智能化的内容原创性检测与版权保护系统
引言:数字时代的内容保护挑战
在当今信息爆炸的互联网时代,内容创作已成为网站运营的核心。然而,随着内容创作的普及,抄袭、盗用和未经授权的转载问题也日益严重。对于WordPress网站管理员和内容创作者而言,保护原创内容不仅是维护品牌声誉的需要,更是保障内容投资回报的关键。
传统的版权保护方法往往被动且效率低下,而智能化的内容原创性检测与版权保护系统则能主动识别侵权行为,为原创内容提供全方位保护。本教程将深入探讨如何通过WordPress代码二次开发,集成一套完整的智能化内容保护系统,同时实现多种实用的互联网小工具功能。
第一部分:系统架构设计与技术选型
1.1 系统核心功能规划
一个完整的智能化内容保护系统应包含以下核心功能:
- 原创性检测:自动检测新发布内容与互联网现有内容的相似度
- 版权水印:为多媒体内容添加隐形或可见的版权标识
- 侵权监控:定期扫描网络,发现未经授权的内容使用
- 自动化维权:发送侵权通知、生成侵权报告
- 访问控制:防止内容被非法复制、下载或截图
1.2 技术栈选择
为实现上述功能,我们需要选择合适的技术方案:
- 核心平台:WordPress 5.0+(支持REST API和现代开发特性)
- 检测引擎:结合本地算法与第三方API(如Copyscape、Grammarly API)
- 水印技术:使用PHP GD库或ImageMagick进行图像处理
- 监控机制:基于WordPress Cron的定时任务系统
- 前端保护:JavaScript内容保护技术
- 数据存储:MySQL数据库配合WordPress自定义表
1.3 系统架构图
用户发布内容 → WordPress → 原创性检测模块 → 内容处理模块
↓ ↓ ↓
版权水印添加 ← 检测结果分析 ← 与外部API交互
↓ ↓
数据库存储 侵权内容记录
↓ ↓
前端展示 侵权监控警报
第二部分:WordPress开发环境配置
2.1 开发环境搭建
首先,我们需要配置一个适合WordPress插件开发的环境:
// 创建插件主文件:wp-content/plugins/content-protection-system/content-protection-system.php
<?php
/**
* Plugin Name: 智能内容保护系统
* Plugin URI: https://yourwebsite.com/
* Description: 为WordPress网站提供智能化内容原创性检测与版权保护功能
* Version: 1.0.0
* Author: 您的名称
* License: GPL v2 or later
* Text Domain: content-protection-system
*/
// 防止直接访问
if (!defined('ABSPATH')) {
exit;
}
// 定义插件常量
define('CPS_VERSION', '1.0.0');
define('CPS_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('CPS_PLUGIN_URL', plugin_dir_url(__FILE__));
define('CPS_PLUGIN_BASENAME', plugin_basename(__FILE__));
2.2 数据库表设计
我们需要创建自定义数据库表来存储检测结果和侵权记录:
// 在插件激活时创建数据库表
register_activation_hook(__FILE__, 'cps_create_database_tables');
function cps_create_database_tables() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$table_name_content_checks = $wpdb->prefix . 'cps_content_checks';
$table_name_infringements = $wpdb->prefix . 'cps_infringements';
// 内容检测记录表
$sql_content_checks = "CREATE TABLE IF NOT EXISTS $table_name_content_checks (
id bigint(20) NOT NULL AUTO_INCREMENT,
post_id bigint(20) NOT NULL,
check_date datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
originality_score float NOT NULL,
sources_found text,
check_method varchar(50) NOT NULL,
check_status varchar(20) DEFAULT 'pending',
PRIMARY KEY (id),
KEY post_id (post_id),
KEY check_date (check_date)
) $charset_collate;";
// 侵权记录表
$sql_infringements = "CREATE TABLE IF NOT EXISTS $table_name_infringements (
id bigint(20) NOT NULL AUTO_INCREMENT,
original_post_id bigint(20) NOT NULL,
infringing_url varchar(500) NOT NULL,
detection_date datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
similarity_percent float NOT NULL,
action_taken varchar(50),
status varchar(20) DEFAULT 'pending',
notes text,
PRIMARY KEY (id),
KEY original_post_id (original_post_id),
KEY infringing_url (infringing_url(191))
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql_content_checks);
dbDelta($sql_infringements);
}
第三部分:原创性检测模块实现
3.1 本地文本相似度算法
首先实现一个本地的文本相似度检测算法作为基础检测层:
class ContentOriginalityChecker {
/**
* 计算两段文本的相似度
*/
public function calculate_similarity($text1, $text2) {
// 文本预处理
$text1 = $this->preprocess_text($text1);
$text2 = $this->preprocess_text($text2);
// 使用余弦相似度算法
$vector1 = $this->text_to_vector($text1);
$vector2 = $this->text_to_vector($text2);
return $this->cosine_similarity($vector1, $vector2);
}
/**
* 文本预处理
*/
private function preprocess_text($text) {
// 转换为小写
$text = strtolower($text);
// 移除HTML标签
$text = strip_tags($text);
// 移除标点符号和特殊字符
$text = preg_replace('/[^p{L}p{N}s]/u', ' ', $text);
// 移除多余空格
$text = preg_replace('/s+/', ' ', $text);
return trim($text);
}
/**
* 将文本转换为词频向量
*/
private function text_to_vector($text) {
$words = explode(' ', $text);
$vector = array();
foreach ($words as $word) {
// 移除停用词(简单示例,实际应使用更完整的停用词列表)
$stopwords = array('的', '了', '在', '是', '我', '有', '和', '就', '不', '人', '都', '一', '一个', '上', '也', '很', '到', '说', '要', '去', '你', '会', '着', '没有', '看', '好', '自己', '这');
if (in_array($word, $stopwords) || strlen($word) < 2) {
continue;
}
if (!isset($vector[$word])) {
$vector[$word] = 0;
}
$vector[$word]++;
}
return $vector;
}
/**
* 计算余弦相似度
*/
private function cosine_similarity($vector1, $vector2) {
// 获取所有唯一词
$all_words = array_unique(array_merge(array_keys($vector1), array_keys($vector2)));
// 计算点积和模长
$dot_product = 0;
$magnitude1 = 0;
$magnitude2 = 0;
foreach ($all_words as $word) {
$value1 = isset($vector1[$word]) ? $vector1[$word] : 0;
$value2 = isset($vector2[$word]) ? $vector2[$word] : 0;
$dot_product += $value1 * $value2;
$magnitude1 += $value1 * $value1;
$magnitude2 += $value2 * $value2;
}
$magnitude1 = sqrt($magnitude1);
$magnitude2 = sqrt($magnitude2);
if ($magnitude1 == 0 || $magnitude2 == 0) {
return 0;
}
return $dot_product / ($magnitude1 * $magnitude2);
}
/**
* 检测内容原创性
*/
public function check_originality($content, $title = '') {
$results = array(
'score' => 100, // 默认100%原创
'sources' => array(),
'method' => 'local'
);
// 从内容中提取关键片段进行搜索
$search_queries = $this->extract_search_queries($content, $title);
foreach ($search_queries as $query) {
$search_results = $this->search_online($query);
foreach ($search_results as $result) {
$similarity = $this->calculate_similarity($content, $result['content']);
if ($similarity > 0.3) { // 相似度超过30%视为可能侵权
$results['sources'][] = array(
'url' => $result['url'],
'title' => $result['title'],
'similarity' => round($similarity * 100, 2)
);
// 更新最低原创度分数
$results['score'] = min($results['score'], 100 - round($similarity * 100, 2));
}
}
}
return $results;
}
/**
* 从内容中提取搜索查询词
*/
private function extract_search_queries($content, $title) {
$queries = array();
// 使用标题作为搜索词
if (!empty($title)) {
$queries[] = $title;
}
// 提取内容中的关键句子
$sentences = preg_split('/[。.!?]/u', $content);
foreach ($sentences as $sentence) {
$sentence = trim($sentence);
if (mb_strlen($sentence) > 15 && mb_strlen($sentence) < 100) {
$queries[] = $sentence;
}
// 限制查询数量
if (count($queries) >= 5) {
break;
}
}
return $queries;
}
/**
* 模拟在线搜索(实际应调用搜索引擎API)
*/
private function search_online($query) {
// 这里应该调用搜索引擎API,如Google Custom Search API
// 以下为模拟数据
return array();
}
}
3.2 集成第三方原创性检测API
为了获得更准确的检测结果,我们可以集成第三方API:
class APIBasedOriginalityChecker {
private $api_keys = array();
public function __construct() {
// 从WordPress选项获取API密钥
$this->api_keys = get_option('cps_api_keys', array());
}
/**
* 使用Copyscape API检测内容原创性
*/
public function check_with_copyscape($content, $title) {
$api_key = isset($this->api_keys['copyscape']) ? $this->api_keys['copyscape'] : '';
if (empty($api_key)) {
return array('error' => 'Copyscape API密钥未配置');
}
// 准备API请求
$url = 'http://www.copyscape.com/api/';
$args = array(
'method' => 'POST',
'timeout' => 30,
'body' => array(
'k' => $api_key,
'o' => 'csearch',
'e' => 'UTF-8',
't' => $title,
'c' => $content,
'f' => 'xml'
)
);
// 发送请求
$response = wp_remote_post($url, $args);
if (is_wp_error($response)) {
return array('error' => $response->get_error_message());
}
$body = wp_remote_retrieve_body($response);
// 解析XML响应
$xml = simplexml_load_string($body);
$results = array(
'score' => 100,
'sources' => array(),
'method' => 'copyscape'
);
if ($xml && isset($xml->result)) {
foreach ($xml->result as $result) {
$similarity = floatval($result->percent);
$results['sources'][] = array(
'url' => (string)$result->url,
'title' => (string)$result->title,
'similarity' => $similarity
);
$results['score'] = min($results['score'], 100 - $similarity);
}
}
return $results;
}
/**
* 使用Grammarly API检测内容原创性
*/
public function check_with_grammarly($content) {
$api_key = isset($this->api_keys['grammarly']) ? $this->api_keys['grammarly'] : '';
if (empty($api_key)) {
return array('error' => 'Grammarly API密钥未配置');
}
// 注意:Grammarly API需要商业授权,此处为示例代码结构
$url = 'https://api.grammarly.com/plagiarism-checker/v1/check';
$args = array(
'method' => 'POST',
'timeout' => 30,
'headers' => array(
'Authorization' => 'Bearer ' . $api_key,
'Content-Type' => 'application/json'
),
'body' => json_encode(array(
'text' => $content,
'language' => 'en'
))
);
$response = wp_remote_post($url, $args);
// 处理响应...
return array('score' => 100, 'sources' => array(), 'method' => 'grammarly');
}
}
第四部分:版权保护与水印系统
4.1 图像水印功能
为上传的图像自动添加版权水印:
class ImageWatermark {
/**
* 为上传的图像添加水印
*/
public function add_watermark_to_image($image_path, $watermark_type = 'text') {
// 获取图像信息
$image_info = getimagesize($image_path);
$mime_type = $image_info['mime'];
// 根据MIME类型创建图像资源
switch ($mime_type) {
case 'image/jpeg':
$image = imagecreatefromjpeg($image_path);
break;
case 'image/png':
$image = imagecreatefrompng($image_path);
break;
case 'image/gif':
$image = imagecreatefromgif($image_path);
break;
default:
return false;
}
if (!$image) {
return false;
}
// 获取图像尺寸
$image_width = imagesx($image);
$image_height = imagesy($image);
if ($watermark_type == 'text') {
$this->add_text_watermark($image, $image_width, $image_height);
} else {
$this->add_image_watermark($image, $image_width, $image_height);
}
// 保存图像
switch ($mime_type) {
case 'image/jpeg':
imagejpeg($image, $image_path, 90);
break;
case 'image/png':
imagepng($image, $image_path, 9);
break;
case 'image/gif':
imagegif($image, $image_path);
break;
}
// 释放内存
imagedestroy($image);
return true;
}
/**
* 添加文字水印
*/
private function add_text_watermark($image, $image_width, $image_height) {
// 水印文字
$site_url = get_site_url();
$watermark_text = "© " . date('Y') . " " . get_bloginfo('name') . " - " . $site_url;
// 水印字体大小(根据图像尺寸动态调整)
$font_size = max(12, $image_width / 50);
// 加载字体
$font_path = CPS_PLUGIN_DIR . 'assets/fonts/arial.ttf';
// 计算文字尺寸
$text_box = imagettfbbox($font_size, 0, $font_path, $watermark_text);
$text_width = $text_box[2] - $text_box[0];
$text_height = $text_box[1] - $text_box[7];
// 水印位置(右下角,留边距)
$margin = 20;
$x = $image_width - $text_width - $margin;
$y = $image_height - $margin;
// 水印颜色(半透明白色)
$color = imagecolorallocatealpha($image, 255, 255, 255, 60);
// 添加文字水印
imagettftext($image, $font_size, 0, $x, $y, $color, $font_path, $watermark_text);
// 添加第二层更透明的水印(防移除)
$color2 = imagecolorallocatealpha($image, 255, 255, 255, 10);
$pattern_text = get_bloginfo('name') . " " . $site_url;
4.2 数字水印与隐形标识
除了可见水印,我们还可以添加隐形数字水印,用于追踪图像来源:
/**
* 添加隐形数字水印(LSB隐写术)
*/
private function add_digital_watermark($image_path) {
// 读取图像为二进制数据
$image_data = file_get_contents($image_path);
// 生成水印信息
$site_url = get_site_url();
$post_id = get_the_ID();
$watermark_data = "COPYRIGHT:" . $site_url . ":POST:" . $post_id . ":DATE:" . date('Y-m-d');
// 将水印信息转换为二进制
$binary_watermark = '';
for ($i = 0; $i < strlen($watermark_data); $i++) {
$binary_watermark .= sprintf('%08b', ord($watermark_data[$i]));
}
// 添加结束标记
$binary_watermark .= '00000000';
// 使用LSB隐写术嵌入水印
$watermark_length = strlen($binary_watermark);
$image_length = strlen($image_data);
// 确保图像足够大以容纳水印
if ($image_length < $watermark_length * 8) {
return false;
}
// 在图像数据中嵌入水印
$watermark_index = 0;
for ($i = 0; $i < $image_length && $watermark_index < $watermark_length; $i++) {
// 跳过文件头(前100字节)
if ($i < 100) continue;
// 修改每个字节的最低位
$byte = ord($image_data[$i]);
$bit = $binary_watermark[$watermark_index];
// 设置最低位
if ($bit == '1') {
$byte = $byte | 1; // 设置最低位为1
} else {
$byte = $byte & ~1; // 设置最低位为0
}
$image_data[$i] = chr($byte);
$watermark_index++;
}
// 保存修改后的图像
file_put_contents($image_path, $image_data);
return true;
}
/**
* 检测并提取数字水印
*/
public function detect_digital_watermark($image_path) {
$image_data = file_get_contents($image_path);
$image_length = strlen($image_data);
$binary_data = '';
// 提取LSB位
$max_bits = 1000; // 限制提取的位数
$bits_extracted = 0;
for ($i = 100; $i < $image_length && $bits_extracted < $max_bits; $i++) {
$byte = ord($image_data[$i]);
$lsb = $byte & 1; // 获取最低位
$binary_data .= $lsb;
$bits_extracted++;
}
// 将二进制数据转换为字符串
$watermark_string = '';
for ($j = 0; $j < strlen($binary_data); $j += 8) {
$byte_binary = substr($binary_data, $j, 8);
// 检查结束标记
if ($byte_binary == '00000000') {
break;
}
if (strlen($byte_binary) == 8) {
$char = chr(bindec($byte_binary));
// 只接受可打印字符
if (ctype_print($char) || $char == ':') {
$watermark_string .= $char;
} else {
// 遇到非打印字符,可能不是有效水印
break;
}
}
}
// 验证水印格式
if (strpos($watermark_string, 'COPYRIGHT:') === 0) {
$parts = explode(':', $watermark_string);
if (count($parts) >= 5) {
return array(
'type' => 'copyright',
'site' => $parts[1],
'post_id' => $parts[3],
'date' => $parts[5]
);
}
}
return false;
}
4.3 前端内容保护
防止用户轻易复制网站内容:
class FrontendContentProtection {
public function __construct() {
// 在文章内容加载时添加保护
add_filter('the_content', array($this, 'protect_content'));
// 添加前端脚本和样式
add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts'));
}
/**
* 保护文章内容
*/
public function protect_content($content) {
if (!is_single() && !is_page()) {
return $content;
}
// 只在需要保护的文章类型上应用
$protected_types = get_option('cps_protected_post_types', array('post', 'page'));
$current_type = get_post_type();
if (!in_array($current_type, $protected_types)) {
return $content;
}
// 添加保护层
$protected_content = '<div class="cps-protected-content" data-post-id="' . get_the_ID() . '">';
$protected_content .= '<div class="cps-content-wrapper">' . $content . '</div>';
$protected_content .= $this->get_protection_overlay();
$protected_content .= '</div>';
return $protected_content;
}
/**
* 获取保护覆盖层HTML
*/
private function get_protection_overlay() {
$site_name = get_bloginfo('name');
$current_year = date('Y');
return '
<div class="cps-protection-overlay" style="
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: transparent;
z-index: 9998;
pointer-events: none;
user-select: none;
"></div>
<div class="cps-copyright-notice" style="
position: fixed;
bottom: 20px;
right: 20px;
background: rgba(0,0,0,0.8);
color: white;
padding: 10px 15px;
border-radius: 5px;
font-size: 12px;
z-index: 10000;
display: none;
">
本文受版权保护 © ' . $current_year . ' ' . $site_name . ' - 未经授权禁止复制
</div>';
}
/**
* 添加前端脚本
*/
public function enqueue_scripts() {
if (!is_single() && !is_page()) {
return;
}
wp_enqueue_script(
'cps-frontend-protection',
CPS_PLUGIN_URL . 'assets/js/frontend-protection.js',
array('jquery'),
CPS_VERSION,
true
);
wp_enqueue_style(
'cps-frontend-style',
CPS_PLUGIN_URL . 'assets/css/frontend-style.css',
array(),
CPS_VERSION
);
// 传递数据到JavaScript
wp_localize_script('cps-frontend-protection', 'cps_data', array(
'ajax_url' => admin_url('admin-ajax.php'),
'protection_enabled' => true,
'copy_warning' => '本文受版权保护,如需引用请注明出处。',
'print_warning' => '打印功能已禁用,本文受版权保护。'
));
}
}
创建前端JavaScript保护脚本:
// assets/js/frontend-protection.js
jQuery(document).ready(function($) {
// 禁止右键菜单
$('.cps-protected-content').on('contextmenu', function(e) {
showCopyrightNotice();
return false;
});
// 禁止文本选择
$('.cps-protected-content').css({
'-webkit-user-select': 'none',
'-moz-user-select': 'none',
'-ms-user-select': 'none',
'user-select': 'none'
});
// 禁止拖拽
$('.cps-protected-content').on('dragstart', function(e) {
return false;
});
// 检测复制操作
document.addEventListener('copy', function(e) {
if ($(e.target).closest('.cps-protected-content').length) {
e.preventDefault();
// 获取文章基本信息
var postTitle = $('h1.entry-title').text() || document.title;
var siteName = document.title.split('|')[0] || window.location.hostname;
var currentUrl = window.location.href;
// 创建带有引用的文本
var selectedText = window.getSelection().toString();
var creditedText = selectedText + "nn—— 摘自《" + postTitle + "》,来源:" + siteName + " (" + currentUrl + ")";
// 将带引用的文本放入剪贴板
e.clipboardData.setData('text/plain', creditedText);
showCopyrightNotice();
// 记录复制行为
$.ajax({
url: cps_data.ajax_url,
method: 'POST',
data: {
action: 'cps_log_copy_action',
post_id: $('.cps-protected-content').data('post-id'),
text_length: selectedText.length
}
});
}
});
// 检测打印操作
window.addEventListener('beforeprint', function(e) {
if ($('.cps-protected-content').length) {
alert(cps_data.print_warning);
e.preventDefault();
return false;
}
});
// 检测开发者工具
var devtools = /./;
devtools.toString = function() {
showCopyrightNotice();
return 'copyright_protection_enabled';
};
console.log('%c', devtools);
// 显示版权通知
function showCopyrightNotice() {
$('.cps-copyright-notice').fadeIn().delay(3000).fadeOut();
}
// 防止截图(通过CSS)
document.addEventListener('keydown', function(e) {
// 检测Print Screen键
if (e.keyCode === 44) { // Print Screen键
showCopyrightNotice();
// 临时添加防截图效果
$('body').addClass('cps-anti-screenshot');
setTimeout(function() {
$('body').removeClass('cps-anti-screenshot');
}, 1000);
}
// 检测Ctrl+P(打印)
if ((e.ctrlKey || e.metaKey) && e.keyCode === 80) {
e.preventDefault();
showCopyrightNotice();
return false;
}
});
});
第五部分:侵权监控与自动化维权
5.1 定期网络监控
class InfringementMonitor {
private $search_engines = array(
'google' => 'https://www.google.com/search?q=',
'bing' => 'https://www.bing.com/search?q='
);
/**
* 初始化监控任务
*/
public function init_monitoring() {
// 每天执行一次侵权监控
if (!wp_next_scheduled('cps_daily_infringement_check')) {
wp_schedule_event(time(), 'daily', 'cps_daily_infringement_check');
}
add_action('cps_daily_infringement_check', array($this, 'run_daily_check'));
}
/**
* 执行每日侵权检查
*/
public function run_daily_check() {
// 获取最近30天发布的文章
$recent_posts = get_posts(array(
'post_type' => 'post',
'post_status' => 'publish',
'date_query' => array(
array(
'after' => '30 days ago'
)
),
'posts_per_page' => 20
));
foreach ($recent_posts as $post) {
$this->check_post_infringements($post);
}
}
/**
* 检查单篇文章的侵权情况
*/
public function check_post_infringements($post) {
// 提取文章特征
$signatures = $this->extract_content_signatures($post->post_content);
foreach ($signatures as $signature) {
$search_results = $this->search_content_online($signature);
foreach ($search_results as $result) {
// 检查是否来自自己的网站
if ($this->is_own_domain($result['url'])) {
continue;
}
// 获取疑似侵权页面内容
$infringing_content = $this->fetch_page_content($result['url']);
if ($infringing_content) {
$similarity = $this->calculate_content_similarity(
$post->post_content,
$infringing_content
);
// 如果相似度超过阈值,记录侵权
if ($similarity > 0.5) { // 50%相似度
$this->record_infringement(
$post->ID,
$result['url'],
$similarity,
$infringing_content
);
}
}
}
}
}
/**
* 提取内容特征(用于搜索)
*/
private function extract_content_signatures($content) {
$signatures = array();
// 清理内容
$clean_content = strip_tags($content);
$clean_content = preg_replace('/s+/', ' ', $clean_content);
// 提取独特句子
$sentences = preg_split('/[。.!?]/u', $clean_content);
foreach ($sentences as $sentence) {
$sentence = trim($sentence);
// 选择长度适中且包含关键词的句子
if (mb_strlen($sentence) > 20 && mb_strlen($sentence) < 100) {
// 检查句子是否包含重要关键词
if ($this->contains_important_keywords($sentence)) {
$signatures[] = $sentence;
}
}
// 限制特征数量
if (count($signatures) >= 5) {
break;
}
}
return $signatures;
}
/**
* 检查句子是否包含重要关键词
*/
private function contains_important_keywords($sentence) {
// 这里可以定义重要关键词列表
$important_keywords = get_option('cps_important_keywords', array());
if (empty($important_keywords)) {
// 如果没有设置,使用默认判断逻辑
$words = explode(' ', $sentence);
return count($words) > 5;
}
foreach ($important_keywords as $keyword) {
if (stripos($sentence, $keyword) !== false) {
return true;
}
}
return false;
}
/**
* 在线搜索内容
*/
private function search_content_online($query) {
$results = array();
// 使用多个搜索引擎进行搜索
foreach ($this->search_engines as $engine => $url) {
$search_url = $url . urlencode('"' . $query . '"');
// 使用WordPress HTTP API获取搜索结果
$response = wp_remote_get($search_url, array(
'timeout' => 30,
'user-agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
));
if (!is_wp_error($response)) {
$body = wp_remote_retrieve_body($response);
$engine_results = $this->parse_search_results($body, $engine);
$results = array_merge($results, $engine_results);
}
}
return $results;
}
/**
* 解析搜索结果
*/
private function parse_search_results($html, $engine) {
$results = array();
if ($engine == 'google') {
// 解析Google搜索结果
preg_match_all('/<a href="/url?q=([^&]+)[^>]*>([^<]+)</a>/', $html, $matches);
if (!empty($matches[1])) {
for ($i = 0; $i < count($matches[1]); $i++) {
$url = urldecode($matches[1][$i]);
$title = strip_tags($matches[2][$i]);
// 跳过Google自己的链接
if (strpos($url, 'google.com') === false) {
$results[] = array(
'url' => $url,
'title' => $title,
'engine' => 'google'
);
}
}
}
}
return $results;
}
/**
* 记录侵权信息
*/
private function record_infringement($post_id, $infringing_url, $similarity, $content) {
global $wpdb;
$table_name = $wpdb->prefix . 'cps_infringements';
// 检查是否已记录
$existing = $wpdb->get_var($wpdb->prepare(
"SELECT id FROM $table_name WHERE original_post_id = %d AND infringing_url = %s",
$post_id,
$infringing_url
));
if ($existing) {
// 更新现有记录
$wpdb->update(
$table_name,
array(
'similarity_percent' => $similarity * 100,
'detection_date' => current_time('mysql'),
'status' => 'detected'
),
array('id' => $existing)
);
} else {
// 插入新记录
$wpdb->insert(
$table_name,
array(
'original_post_id' => $post_id,
'infringing_url' => $infringing_url,
