HEX
Server: nginx/1.22.1
System: Linux VM-16-9-centos 3.10.0-1160.99.1.el7.x86_64 #1 SMP Wed Sep 13 14:19:20 UTC 2023 x86_64
User: www (1001)
PHP: 7.3.31
Disabled: passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv
Upload Files
File: /www/wwwroot/softfox.com.cn/wp-content/plugins/baidu-submit-link/classes/indexnow.class.php
<?php

/**
 * Indexnow推送
 * Class WB_BSL_Inexnow
 */

class WB_BSL_Inexnow extends WB_BSL_Base
{
    public static function init(){

        $cnf = WB_BSL_Conf::cnf(null);

        if(!$cnf['indexnow'] || empty($cnf['indexnow_key'])){
            return;
        }
        add_action('parse_request',array(__CLASS__,'parse_request'));
        //add_action('wp_insert_post',array(__CLASS__,'edit_post'),91,2);
        add_action('wp_insert_post', [__CLASS__,'wb_push_post'], 91, 2);
        add_action('wb_push_post',array(__CLASS__,'edit_post'),91,2);

    }

    public static function parse_request($wp)
    {
        $cnf = WB_BSL_Conf::cnf(null);
        if ( $wp->request !== $cnf['indexnow_key'] . '.txt' ) {
            return;
        }
        if(!get_option('wb_bsl_ver',0)){
            return;
        }
        header( 'Content-Type: text/plain' );
        header( 'X-Robots-Tag: noindex' );
        status_header( 200 );
        echo esc_html( $cnf['indexnow_key'] );
        exit(0);

    }

    public static function edit_post($post_id,$post){
        WB_BSL_Utils::run_log('indexnow推送','收录推送');
        if(wp_is_post_revision($post)){
            return;
        }
        if(!get_option('wb_bsl_ver',0)){
            return;
        }
        if(!WB_BSL_Conf::check_post_type($post)){
            return;
        }

        do{
            $active = WB_BSL_Conf::cnf('indexnow');
            if(!$active){
                break;
            }
            $key = WB_BSL_Conf::cnf('indexnow_key');
            if(!$key){
                break;
            }
            $search = WB_BSL_Conf::cnf('indexnow_type');
            if(empty($search) || !is_array($search)){
                break;
            }

            $type = 32;
            $log = WB_BSL_Utils::push_log($post_id,$type);
            if($log && $log->push_status == 1){
                break;
            }

            if($log && current_time('timestamp') - strtotime($log->create_date) < 300){
                break;
            }

            $post_url = get_permalink($post);
            if(!preg_match('#^https?://#',$post_url)){
                $post_url = home_url($post_url);
            }


            //https://api.indexnow.org/indexnow
            //https://www.bing.com/indexnow
            //https://search.seznam.cz/indexnow
            //https://yandex.com/indexnow
            $api_list = [
                'indexnow'=>'https://api.indexnow.org/indexnow',
                'bing'=>'https://www.bing.com/indexnow',
                'seznam'=>'https://search.seznam.cz/indexnow',
                'yandex'=>'https://yandex.com/indexnow',
            ];
            $errors = [];
            $fail = 1;
            foreach($search as $s){
                if(isset($api_list[$s])){
                    WB_BSL_Utils::run_log($s.',推送url:','收录推送');
                    WB_BSL_Utils::run_log($post_url,'收录推送');
                    $state = self::push($key,$api_list[$s],$post_url);
                    if($state[0]){
                        $errors[$s] = $state[1];
                    }else{
                        $fail = 0;
                    }
                }
            }
            $ret = [
                'code'=>$fail,
                'desc' => $errors?implode(',',$errors):'success',
            ];

            WB_BSL_Utils::run_log('推送结果【'.$ret['desc'].'】','收录推送');

            WB_BSL_Utils::add_push_log($type,$post_id,$post_url,$ret);


        }while(false);

    }

    public static function push($key,$api,$post_url)
    {
        $args = array(
            'timeout' => 5,
            'sslverify' => false,
            'body'    => wp_json_encode(
                array(
                    'host'    => wp_parse_url( get_home_url(), PHP_URL_HOST ),
                    'key'     => $key,
                    'urlList' => array($post_url),
                )
            ),
            'headers' => [
                'Content-Type' => 'application/json',
            ],
        );

        $response    = wp_remote_post( $api, $args );
        if(is_wp_error($response)){
            self::error('接口请求失败,'.$response->get_error_message(),'Indexnow推送');
            return [1,$response->get_error_message()];
        }
        $status_code = wp_remote_retrieve_response_code( $response );
        if($status_code !== 200){
            /*$body = trim(wp_remote_retrieve_body( $response ));
            $data = [];
            if($body){
                $data = json_decode($body,true);
            }*/
            $msg  = [
                202=>'URL received. IndexNow key validation pending.',
                400=>'Invalid format',
                403=>'In case of key not valid (e.g. key not found, file found but key not in the file)',
                422=>'In case of URLs which don’t belong to the host or the key is not matching the schema in the protocol',
                429=>'Too Many Requests (potential Spam)',
            ];
            $err = $msg[$status_code] ? $msg[$status_code] : 'api error,response code['.$status_code.']';
            self::error($err,'Indexnow推送');
            return [1,$err];
        }

        return [0,'success'];
    }

}