| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: 简数采集器 |
| 4 |
Plugin URI: http://www.keydatas.com/caiji/wordpress-cms-caiji |
| 5 |
Description: 简数采集器(keydatas.com)是一个通用、简单、智能、在线的网页数据采集器,功能强大,操作简单。支持按� |
| 6 |
�键词采集;集成AI大模型接口、翻译等服务;图片下载支持存储到阿里云OSS、七牛、� |
| 7 |
�讯云对象存储等。 |
| 8 |
Version: 2.7.2 |
| 9 |
Author: keydatas |
| 10 |
Author URI: http://www.keydatas.com |
| 11 |
License: GPLv2 or later |
| 12 |
Text Domain: keydatas |
| 13 |
*/ |
| 14 |
|
| 15 |
function keydatas_successRsp($data = "", $msg = "") { |
| 16 |
keydatas_rsp(1,0, $data, $msg); |
| 17 |
} |
| 18 |
function keydatas_failRsp($code = 0, $data = "", $msg = "") { |
| 19 |
keydatas_rsp(0,$code, $data, $msg); |
| 20 |
} |
| 21 |
|
| 22 |
function keydatas_rsp($result = 1,$code = 0, $data = "", $msg = "") { |
| 23 |
die(json_encode(array("rs" => $result, "code" => $code, "data" => $data, "msg" => urlencode($msg)))); |
| 24 |
} |
| 25 |
function keydatas_genRandomIp(){ |
| 26 |
// $randIP = "".mt_rand(0,255).".".mt_rand(0,255).".".mt_rand(0,255).".".mt_rand(0,255); |
| 27 |
$randIP = "" . wp_rand(0, 255) . "." . wp_rand(0, 255) . "." . wp_rand(0, 255) . "." . wp_rand(0, 255); |
| 28 |
return $randIP; |
| 29 |
} |
| 30 |
|
| 31 |
function keydatas_getPostValSafe($paraName = ""){ |
| 32 |
$postVal=""; |
| 33 |
if(isset($_POST[$paraName])){ |
| 34 |
$postVal=sanitize_text_field($_POST[$paraName]); |
| 35 |
} |
| 36 |
return $postVal; |
| 37 |
} |
| 38 |
/** |
| 39 |
* 生成0~1随机小数 |
| 40 |
* @param Int $min |
| 41 |
* @param Int $max |
| 42 |
* @return Float |
| 43 |
*/ |
| 44 |
function keydatas_randFloat($min=0, $max=1){ |
| 45 |
//return $min + mt_rand()/mt_getrandmax() * ($max-$min); |
| 46 |
return $min + wp_rand() / mt_getrandmax() * ($max - $min); |
| 47 |
} |
| 48 |
|
| 49 |
if (is_admin()) { |
| 50 |
//将函数连接到添加菜单 |
| 51 |
add_action('admin_menu', 'keydatas_add_menu'); |
| 52 |
} |
| 53 |
|
| 54 |
//在后台管理界面添加菜单 |
| 55 |
function keydatas_add_menu() { |
| 56 |
if (function_exists('add_menu_page')) { |
| 57 |
$setting_menu_slug='keydatas/publish-setting.php'; |
| 58 |
add_menu_page('简数采集平台', '简数采集平台', 'administrator', $setting_menu_slug, '', plugins_url('images/icon.png',__FILE__)); |
| 59 |
} |
| 60 |
} |
| 61 |
add_action('init', 'keydatas_post_doc'); |
| 62 |
function keydatas_myplugin_activate() { |
| 63 |
} |
| 64 |
// 寄存一个插件函数,该插件函数在插件被激活时运行 |
| 65 |
register_activation_hook(__FILE__, 'keydatas_myplugin_activate'); |
| 66 |
|
| 67 |
function keydatas_post_doc() { |
| 68 |
global $wpdb; |
| 69 |
$kds_flag=""; |
| 70 |
if(isset($_GET['__kds_flag'])){ |
| 71 |
$kds_flag=sanitize_text_field($_GET["__kds_flag"]); |
| 72 |
} |
| 73 |
if (!empty($kds_flag)){ |
| 74 |
//$_REQ = keydatas_mergeRequest(); |
| 75 |
$kds_password = get_option('keydatas_password', ''); |
| 76 |
if (empty($kds_password)) { |
| 77 |
keydatas_failRsp(1403, "password empty", "提交的发布密码为空"); |
| 78 |
} |
| 79 |
$post_password = keydatas_getPostValSafe('kds_password'); |
| 80 |
if (empty($post_password) || $post_password != $kds_password) { |
| 81 |
keydatas_failRsp(1403, "password error", "提交的发布密码错误"); |
| 82 |
} |
| 83 |
|
| 84 |
//do post |
| 85 |
if ($kds_flag == "post") { |
| 86 |
|
| 87 |
$title = keydatas_getPostValSafe("post_title"); |
| 88 |
if (empty($title)) { |
| 89 |
keydatas_failRsp(1404, "title is empty", "标题不能为空"); |
| 90 |
} |
| 91 |
|
| 92 |
$content=''; |
| 93 |
if(isset($_POST["post_content"])){ |
| 94 |
$content =wp_kses_post($_POST["post_content"]); |
| 95 |
} |
| 96 |
if (empty($content)) { |
| 97 |
$content=''; |
| 98 |
} |
| 99 |
|
| 100 |
//文章摘要 |
| 101 |
$excerpt = keydatas_getPostValSafe("post_excerpt"); |
| 102 |
if (empty($excerpt)) { |
| 103 |
$excerpt=''; |
| 104 |
} |
| 105 |
//文章类型 |
| 106 |
$postType = keydatas_getPostValSafe("post_type"); |
| 107 |
if (empty($postType)) { |
| 108 |
$postType = 'post'; |
| 109 |
} |
| 110 |
|
| 111 |
/*$postStatus = 'publish'; |
| 112 |
if (isset($_POST["post_status"]) && in_array($_POST["post_status"], array('publish', 'draft'))) { |
| 113 |
$postStatus = $_POST["post_status"]; |
| 114 |
} |
| 115 |
*/ |
| 116 |
$postStatus = keydatas_getPostValSafe("post_status"); |
| 117 |
if (empty($postStatus) || !in_array($postStatus, array('publish', 'draft'))) { |
| 118 |
$postStatus = 'publish'; |
| 119 |
} |
| 120 |
|
| 121 |
// |
| 122 |
$commentStatus = keydatas_getPostValSafe("comment_status"); |
| 123 |
if (empty($commentStatus) || !in_array($commentStatus, array('open', 'closed'))) { |
| 124 |
$commentStatus = 'open'; |
| 125 |
} |
| 126 |
//文章密码,文章编辑才可为文章设定一个密码,凭这个密码才能对文章进行重新强加或修改 |
| 127 |
$postPassword = keydatas_getPostValSafe("post_password"); |
| 128 |
//if (isset($_POST["post_password"]) && $_POST["post_password"]) { |
| 129 |
if(empty($postPassword)){ |
| 130 |
$postPassword = ''; |
| 131 |
} |
| 132 |
|
| 133 |
$my_post = array( |
| 134 |
'post_password' => $postPassword, |
| 135 |
'post_status' => $postStatus, |
| 136 |
'comment_status' => $commentStatus, |
| 137 |
'post_author' => 1 |
| 138 |
); |
| 139 |
if (!empty($title)) { |
| 140 |
$my_post['post_title'] =$title; //htmlspecialchars_decode($title); |
| 141 |
} |
| 142 |
if (!empty($content)) { |
| 143 |
$my_post['post_content'] = $content; |
| 144 |
} |
| 145 |
if(!empty($excerpt)){ |
| 146 |
$my_post['post_excerpt'] = $excerpt; |
| 147 |
} |
| 148 |
if(!empty($postType)){ |
| 149 |
$my_post['post_type'] = $postType; |
| 150 |
} |
| 151 |
//文章别名 |
| 152 |
$postName = keydatas_getPostValSafe("post_name"); |
| 153 |
if (!empty($postName)) { |
| 154 |
$my_post['post_name'] = $postName; |
| 155 |
} |
| 156 |
|
| 157 |
///////////////目前主要用于lightsns |
| 158 |
$post_parent = keydatas_getPostValSafe("__kdsExt_post_parent");//default 0 |
| 159 |
if(!empty($post_parent)){ |
| 160 |
try{ |
| 161 |
$post_parent=intval($post_parent); |
| 162 |
if($post_parent>0){ |
| 163 |
$my_post['post_parent']=$post_parent; |
| 164 |
} |
| 165 |
} catch (Exception $ex1) { |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
//标题唯一校验 |
| 170 |
$title_unique = get_option('keydatas_title_unique', false); |
| 171 |
//error_log('title:'.stripslashes($my_post['post_title']), 3, '/var/log/wp_test.log'); |
| 172 |
if($title_unique){ |
| 173 |
//只返回id |
| 174 |
$post = $wpdb->get_row($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_title= %s and post_status!='trash' and post_status!='inherit' ",stripslashes($my_post['post_title']))); |
| 175 |
if(!empty($post)){ |
| 176 |
//这里可以补� |
| 177 |
|
| 178 |
图片 |
| 179 |
keydatas_downloadImages(); |
| 180 |
//返回访问路径 |
| 181 |
keydatas_successRsp(array("url" => get_permalink($post->ID)."#相同标题文章已存在")); |
| 182 |
} |
| 183 |
} |
| 184 |
$post_date=keydatas_getPostValSafe("post_date"); |
| 185 |
if (!empty($post_date)) { |
| 186 |
$post_date = intval($post_date); |
| 187 |
//$my_post['post_date'] = date("Y-m-d H:i:s", $post_date); |
| 188 |
$my_post['post_date'] = gmdate("Y-m-d H:i:s", $post_date); |
| 189 |
} else { |
| 190 |
//$my_post['post_date'] = date("Y-m-d H:i:s", time()); |
| 191 |
$my_post['post_date'] = gmdate("Y-m-d H:i:s", time()); |
| 192 |
} |
| 193 |
|
| 194 |
$author = keydatas_getPostValSafe("post_author"); |
| 195 |
if (!empty($author)) { |
| 196 |
//$author = htmlspecialchars_decode($author); |
| 197 |
if($author == "rand_users"){ |
| 198 |
$randNum=keydatas_randFloat(); |
| 199 |
//SELECT ID FROM $wpdb->users order by rand() limit 1 |
| 200 |
/** $user_id = $wpdb->get_var("SELECT ID FROM $wpdb->users WHERE |
| 201 |
id >= ((SELECT MAX(id) FROM $wpdb->users)-(SELECT MIN(id) FROM $wpdb->users)) * ".$randNum."+ (SELECT MIN(id) FROM $wpdb->users) LIMIT 1"); |
| 202 |
*/ |
| 203 |
$user_id = $wpdb->get_var( |
| 204 |
$wpdb->prepare( |
| 205 |
"SELECT ID FROM $wpdb->users WHERE id >= ((SELECT MAX(id) FROM $wpdb->users)-(SELECT MIN(id) FROM $wpdb->users)) * %f + (SELECT MIN(id) FROM $wpdb->users) LIMIT 1", |
| 206 |
$randNum |
| 207 |
) |
| 208 |
); |
| 209 |
}else{ |
| 210 |
//用户名(登录名) |
| 211 |
$user_id = username_exists($author); |
| 212 |
} |
| 213 |
$md5author = substr(md5($author), 8, 16); |
| 214 |
if(!$user_id){ |
| 215 |
$user_id = username_exists($md5author); |
| 216 |
} |
| 217 |
|
| 218 |
if (!$user_id) { |
| 219 |
//$md5author = substr(md5($author), 8, 16); |
| 220 |
$random_password = wp_generate_password(); |
| 221 |
$userdata = array( |
| 222 |
'user_login' => $md5author, |
| 223 |
'user_pass' => $random_password, |
| 224 |
'display_name' => $author, |
| 225 |
); |
| 226 |
$user_id = wp_insert_user($userdata); |
| 227 |
if (is_wp_error($user_id)) { |
| 228 |
$user_id = 0; |
| 229 |
} |
| 230 |
} |
| 231 |
if ($user_id) { |
| 232 |
$my_post['post_author'] = $user_id; |
| 233 |
} |
| 234 |
}//.. post_author end |
| 235 |
//分类目录 |
| 236 |
$category = keydatas_getPostValSafe("post_category"); |
| 237 |
if (!empty($category)) { |
| 238 |
$cates = explode(',',$category); |
| 239 |
if (is_array($cates)) { |
| 240 |
$post_cates = array(); |
| 241 |
$term = null; |
| 242 |
foreach ($cates as $cate) { |
| 243 |
//是否为数字 |
| 244 |
$cat_id=0; |
| 245 |
if(is_numeric($cate)&&intval($cate)>0){ |
| 246 |
$cat_name = get_cat_name($cate); |
| 247 |
// error_log('cat_name:'.$cat_name, 3, '/var/log/wp_test.log'); |
| 248 |
if(!empty($cat_name)){ |
| 249 |
$cat_id=intval($cate); |
| 250 |
} |
| 251 |
} |
| 252 |
if($cat_id>0){ |
| 253 |
array_push($post_cates, $cat_id); |
| 254 |
}else{ |
| 255 |
$term = term_exists($cate, "category"); |
| 256 |
if ($term === 0 || $term === null) { |
| 257 |
$term = wp_insert_term($cate, "category"); |
| 258 |
} |
| 259 |
if ($term !== 0 && $term !== null && !is_wp_error($term)) { |
| 260 |
array_push($post_cates, intval($term["term_id"])); |
| 261 |
} |
| 262 |
} |
| 263 |
} |
| 264 |
if (count($post_cates) > 0) { |
| 265 |
$my_post['post_category'] = $post_cates; |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
$post_tag = keydatas_getPostValSafe("post_tag"); |
| 271 |
if (!empty($post_tag)) { |
| 272 |
$tags = explode(',',$post_tag); |
| 273 |
if (is_array($tags)) { |
| 274 |
$post_tags = array(); |
| 275 |
$term = null; |
| 276 |
foreach ($tags as $tag) { |
| 277 |
$term = term_exists($tag, "post_tag"); |
| 278 |
if ($term === 0 || $term === null) { |
| 279 |
$term = wp_insert_term($tag, "post_tag"); |
| 280 |
} |
| 281 |
if ($term !== 0 && $term !== null && !is_wp_error($term)) { |
| 282 |
array_push($post_tags, intval($term["term_id"])); |
| 283 |
} |
| 284 |
} |
| 285 |
if (count($post_tags) > 0) { |
| 286 |
$my_post['tags_input'] = $post_tags; |
| 287 |
} |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
kses_remove_filters(); |
| 292 |
$post_id = wp_insert_post($my_post); |
| 293 |
kses_init_filters(); |
| 294 |
|
| 295 |
if (empty($post_id) || is_wp_error($post_id)) { |
| 296 |
keydatas_failRsp(1500, "post_id is Empty", "插� |
| 297 |
�文章失败"); |
| 298 |
} |
| 299 |
//缩略图处理 |
| 300 |
$image_url =keydatas_getPostValSafe("__kds_feature_url"); |
| 301 |
if (empty($image_url)) { |
| 302 |
$image_url = keydatas_getPostValSafe("post_thumbnail"); |
| 303 |
} |
| 304 |
if (!empty($post_id) && !empty($image_url)) { |
| 305 |
$image_url_final=$image_url; |
| 306 |
// 协议白名单验证 - 只� |
| 307 |
�许http和https |
| 308 |
$parsed_url = parse_url($image_url); |
| 309 |
if (isset($parsed_url['scheme']) && !in_array(strtolower($parsed_url['scheme']), ['http', 'https'])) { |
| 310 |
// 记录安� |
| 311 |
�日志或抛出错误 |
| 312 |
//error_log('Security warning: Invalid URL scheme detected: ' . $image_url); |
| 313 |
$image_url_final = ''; |
| 314 |
} |
| 315 |
|
| 316 |
if (substr($image_url, 0, 2) ==="//") { |
| 317 |
$image_url_final='http:'.$image_url; |
| 318 |
}else if(strpos($image_url, '/') === 0) { |
| 319 |
$image_url_final=get_home_url().$image_url; |
| 320 |
} |
| 321 |
// 再次验证最终URL的协议 |
| 322 |
$final_parsed_url = parse_url($image_url_final); |
| 323 |
if (!empty($image_url_final) && isset($final_parsed_url['scheme']) && |
| 324 |
in_array(strtolower($final_parsed_url['scheme']), ['http', 'https'])) { |
| 325 |
|
| 326 |
$upload_dir = wp_upload_dir(); |
| 327 |
$image_data = file_get_contents($image_url_final); |
| 328 |
$suffix = "jpg"; |
| 329 |
$raw_filename = md5($image_url_final) . "." . $suffix; |
| 330 |
// 使用sanitize_file_name()� |
| 331 |
理文件名 |
| 332 |
$filename = sanitize_file_name($raw_filename); |
| 333 |
|
| 334 |
if (wp_mkdir_p($upload_dir['path'])) { |
| 335 |
$file = $upload_dir['path'] . '/' . $filename; |
| 336 |
} else { |
| 337 |
$file = $upload_dir['basedir'] . '/' . $filename; |
| 338 |
} |
| 339 |
|
| 340 |
file_put_contents($file, $image_data); |
| 341 |
if (file_exists($file)) { |
| 342 |
//error_log('file_exists:'.$filename, 3, '/var/log/wp_test.log'); |
| 343 |
$wp_filetype = wp_check_filetype($filename, null); |
| 344 |
$attachment = array( |
| 345 |
'post_mime_type' => $wp_filetype['type'], |
| 346 |
'post_title' => sanitize_file_name($filename), |
| 347 |
'post_content' => '', |
| 348 |
'post_status' => 'inherit' |
| 349 |
); |
| 350 |
// attachment相� |
| 351 |
� |
| 352 |
$attach_id = wp_insert_attachment($attachment, $file, $post_id); |
| 353 |
require_once(ABSPATH . 'wp-admin/includes/image.php'); |
| 354 |
$attach_data = wp_generate_attachment_metadata($attach_id, $file); |
| 355 |
wp_update_attachment_metadata($attach_id, $attach_data); |
| 356 |
set_post_thumbnail($post_id, $attach_id); |
| 357 |
} |
| 358 |
}// .. 协议校验end |
| 359 |
}// .. no empty image_url |
| 360 |
///// |
| 361 |
keydatas_downloadImages(); |
| 362 |
|
| 363 |
|
| 364 |
//for tbk |
| 365 |
$keydatas_tbk_link_enble = get_option('keydatas_tbk_link_enble', false); |
| 366 |
if($keydatas_tbk_link_enble){ |
| 367 |
$tbk_link = keydatas_getPostValSafe("tbk_link"); |
| 368 |
if (!empty($tbk_link)) { |
| 369 |
add_post_meta($post_id, 'tbk_link', $tbk_link, true); |
| 370 |
} |
| 371 |
} |
| 372 |
//� |
| 373 |
�它meta数据处理 |
| 374 |
if (!empty($post_id)) { |
| 375 |
foreach ($_POST as $key => $value) { |
| 376 |
if (strpos($key, '__kdsExt_') === 0) { |
| 377 |
$real_name=substr($key,9); |
| 378 |
if (!empty($real_name)) { |
| 379 |
//add_post_meta |
| 380 |
update_post_meta($post_id, $real_name, $value);//, true |
| 381 |
|
| 382 |
} |
| 383 |
} |
| 384 |
} |
| 385 |
} |
| 386 |
//keydatas_successRsp(array("url" => get_home_url() . "/?p=" . $post_id)); |
| 387 |
keydatas_successRsp(array("url" =>get_permalink($post_id))); |
| 388 |
} else if ($kds_flag == "category") { |
| 389 |
//获取分类目录 |
| 390 |
$ret = array(); |
| 391 |
$postType = keydatas_getPostValSafe("type"); |
| 392 |
if (!empty($postType) && $postType === "cate") { |
| 393 |
$cates = get_terms('category', 'orderby=count&hide_empty=0'); |
| 394 |
foreach ($cates as $cate) { |
| 395 |
array_push($ret, array("value" => urlencode($cate->name), "text" => urlencode($cate->name))); |
| 396 |
} |
| 397 |
} |
| 398 |
keydatas_successRsp($ret); |
| 399 |
} else if ($kds_flag == "version") { |
| 400 |
//获取用户使用的Php和Wp版本信息 |
| 401 |
global $wp_version; |
| 402 |
$versions = array( |
| 403 |
'php' => PHP_VERSION, |
| 404 |
'plugin' => '1.0', |
| 405 |
'wp' => $wp_version, |
| 406 |
); |
| 407 |
keydatas_successRsp($versions); |
| 408 |
}//.. do by kds_flag |
| 409 |
}//... has __kds_flag end |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
function keydatas_downloadImages(){ |
| 414 |
$allowedExtensions = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'tiff', 'webp', 'ico']; |
| 415 |
try{ |
| 416 |
$downloadFlag = keydatas_getPostValSafe("__kds_download_imgs_flag"); |
| 417 |
if (!empty($downloadFlag) && $downloadFlag== "true") { |
| 418 |
$docImgsStr = keydatas_getPostValSafe("__kds_docImgs"); |
| 419 |
if (!empty($docImgsStr)) { |
| 420 |
$docImgs = explode(',',$docImgsStr); |
| 421 |
if (is_array($docImgs)) { |
| 422 |
$upload_dir = wp_upload_dir(); |
| 423 |
foreach ($docImgs as $imgUrl) { |
| 424 |
// � |
| 425 |
理和验证URL |
| 426 |
$imgUrl = filter_var($imgUrl, FILTER_SANITIZE_URL); |
| 427 |
if (!filter_var($imgUrl, FILTER_VALIDATE_URL)) { |
| 428 |
continue; // 跳过非法的URL |
| 429 |
} |
| 430 |
// 尝试获取图片扩展名 |
| 431 |
$parsedUrl = parse_url($imgUrl); |
| 432 |
$path = isset($parsedUrl['path']) ? $parsedUrl['path'] : ''; |
| 433 |
$extension = pathinfo($path, PATHINFO_EXTENSION); |
| 434 |
|
| 435 |
// 检查扩展名是否在� |
| 436 |
�许的图片格式中 |
| 437 |
if (!in_array(strtolower($extension), $allowedExtensions)) { |
| 438 |
continue; // 跳过非图片格式的URL |
| 439 |
} |
| 440 |
$urlItemArr = explode('/',$imgUrl); |
| 441 |
$itemLen=count($urlItemArr); |
| 442 |
if($itemLen>=3){ |
| 443 |
// |
| 444 |
$fileRelaPath=$urlItemArr[$itemLen-3].'/'.$urlItemArr[$itemLen-2]; |
| 445 |
$imgName=$urlItemArr[$itemLen-1]; |
| 446 |
$finalPath=$upload_dir['basedir'] . '/'.$fileRelaPath; |
| 447 |
if (wp_mkdir_p($finalPath)) { |
| 448 |
$file = $finalPath . '/' . $imgName; |
| 449 |
if(!file_exists($file)){ |
| 450 |
// 下载图片前,� |
| 451 |
�检查HTTP响应头是否为图片 |
| 452 |
$headers = @get_headers($imgUrl, 1); |
| 453 |
if (strpos($headers[0], '200 OK') !== false && strpos($headers['Content-Type'], 'image/') !== false) { |
| 454 |
$doc_image_data = file_get_contents($imgUrl); |
| 455 |
if ($doc_image_data !== false) { |
| 456 |
file_put_contents($file, $doc_image_data); |
| 457 |
} |
| 458 |
} |
| 459 |
} |
| 460 |
} |
| 461 |
} |
| 462 |
}//.for |
| 463 |
}//..is_array |
| 464 |
} |
| 465 |
} |
| 466 |
} catch (Exception $ex) { |
| 467 |
|
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
?> |