PluginProbe
简数采集器 / 2.6.2
简数采集器 v2.6.2
trunk 2.6.2
keydatas / keydatas.php

keydatas.php in 简数采集器 2.6.2, at keydatas.php

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