*/ final class Debug { /** * The unique identifier of this plugin. * * @since 2.0.0 * @access protected * @var string $plugin_name The string used to uniquely identify this plugin. */ protected $plugin_name; /** * Debug constructor. * * @since 2.0.0 * @param string $plugin_name The name of this plugin. */ public function __construct( $plugin_name ) { $this->plugin_name = $plugin_name; } /** * The loader of this plugin. * * @since 2.0.0 * @return void */ public function init() { $request = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : false; $method = isset( $_SERVER['REQUEST_METHOD'] ) ? wp_unslash( $_SERVER['REQUEST_METHOD'] ) : false; $enabled = get_option( $this->plugin_name . '-debug-enabled', '1' ) === '1'; $cron = defined( 'ACTIRISE_CRON' ) && ACTIRISE_CRON === 'true'; /** @var string $token */ $token = get_option( $this->plugin_name . '-debug-token', '' ); if ( $request === '/debug' && $method === 'POST') { if ($enabled) { if ($this->check_token($token)) { $this->render_debug(); } } } else { if ($cron === false && $enabled) { $this->check_update(); } } } /** * Generate abd render debug * * @since 2.0.0 * @return void */ public function render_debug() { // return 200 header( 'Content-Type: application/json' ); header( 'Actirise: true' ); header( 'HTTP/1.1 200 OK' ); $cron = new Cron(); $cron->refresh_token(); // return empty json wp_send_json_success( array( "wordpress" => array( "name" => get_bloginfo( 'name' ), "description" => get_bloginfo( 'description' ), "url" => get_bloginfo( 'url' ), "wpurl" => get_bloginfo( 'wpurl' ), "charset" => get_bloginfo( 'charset' ), "version" => get_bloginfo( 'version' ), "html_type" => get_bloginfo( 'html_type' ), "text_direction" => get_bloginfo( 'text_direction' ), "language" => get_bloginfo( 'language' ), "theme" => $this->get_theme_name(), "pingback_url" => get_bloginfo( 'pingback_url' ), "feed" => array( "atom_url" => get_bloginfo( 'atom_url' ), "rdf_url" => get_bloginfo( 'rdf_url' ), "rss_url" => get_bloginfo( 'rss_url' ), "rss2_url" => get_bloginfo( 'rss2_url' ), "comments_atom_url" => get_bloginfo( 'comments_atom_url' ), "comments_rss2_url" => get_bloginfo( 'comments_rss2_url' ), ), "plugins" => $this->get_plugin_info(), "database" => array( "charset" => $GLOBALS['wpdb']->charset, "collate" => $GLOBALS['wpdb']->collate, ), "network" => array( 'MULTISITE' => defined( 'MULTISITE' ) ? constant( 'MULTISITE' ) : false, 'ALLOW_SUBDIRECTORY_INSTALL' => defined( 'ALLOW_SUBDIRECTORY_INSTALL' ) ? constant( 'ALLOW_SUBDIRECTORY_INSTALL' ) : false, 'BLOG_ID_CURRENT_SITE' => defined( 'BLOG_ID_CURRENT_SITE' ) ? constant( 'BLOG_ID_CURRENT_SITE' ) : false, 'DOMAIN_CURRENT_SITE' => defined( 'DOMAIN_CURRENT_SITE' ) ? constant( 'DOMAIN_CURRENT_SITE' ) : false, 'DIEONDBERROR' => defined( 'DIEONDBERROR' ) ? constant( 'DIEONDBERROR' ) : false, 'ERRORLOGFILE' => defined( 'ERRORLOGFILE' ) ? constant( 'ERRORLOGFILE' ) : false, 'BLOGUPLOADDIR' => defined( 'BLOGUPLOADDIR' ) ? constant( 'BLOGUPLOADDIR' ) : false, 'NOBLOGREDIRECT' => defined( 'NOBLOGREDIRECT' ) ? constant( 'NOBLOGREDIRECT' ) : false, 'PATH_CURRENT_SITE' => defined( 'PATH_CURRENT_SITE' ) ? constant( 'PATH_CURRENT_SITE' ) : false, 'UPLOADBLOGSDIR' => defined( 'UPLOADBLOGSDIR' ) ? constant( 'UPLOADBLOGSDIR' ) : false, 'SITE_ID_CURRENT_SITE' => defined( 'SITE_ID_CURRENT_SITE' ) ? constant( 'SITE_ID_CURRENT_SITE' ) : false, 'SUBDOMAIN_INSTALL' => defined( 'SUBDOMAIN_INSTALL' ) ? constant( 'SUBDOMAIN_INSTALL' ) : false, 'UPLOADS' => defined( 'UPLOADS' ) ? constant( 'UPLOADS' ) : false, 'WPMU_ACCEL_REDIRECT' => defined( 'WPMU_ACCEL_REDIRECT' ) ? constant( 'WPMU_ACCEL_REDIRECT' ) : false, 'WPMU_SENDFILE' => defined( 'WPMU_SENDFILE' ) ? constant( 'WPMU_SENDFILE' ) : false, 'WP_ALLOW_MULTISITE' => defined( 'WP_ALLOW_MULTISITE' ) ? constant( 'WP_ALLOW_MULTISITE' ) : false, ), ), "actirise" => array( "version" => ACTIRISE_VERSION, 'channel' => get_option( $this->plugin_name . '-update-channel', 'stable' ), 'init' => get_option( $this->plugin_name . '-init', 'false' ), 'uuid' => get_option( $this->plugin_name . '-settings-uuid' ), 'type' => get_option( $this->plugin_name . '-settings-uuid-type', 'boot' ), 'cron' => defined( 'ACTIRISE_CRON' ) && ACTIRISE_CRON === 'true', 'noPub' => get_option( $this->plugin_name . '-nopub', array() ), 'customVar' => array( 'custom_fields' => $this->get_custom_fields(), 'selected' => array( 'custom1' => get_option( $this->plugin_name . '-custom1', 'author_ID' ), 'custom2' => get_option( $this->plugin_name . '-custom2', 'category_0_slug' ), 'custom3' => get_option( $this->plugin_name . '-custom3', 'post_ID' ), 'custom4' => get_option( $this->plugin_name . '-custom4', '' ), 'custom5' => get_option( $this->plugin_name . '-custom5', '' ), ), ), 'adstxt' => array( 'actirise' => get_option( $this->plugin_name . '-adstxt-actirise', '' ), 'custom' => get_option( $this->plugin_name . '-adstxt-custom', array() ), 'enabled' => get_option( $this->plugin_name . '-adstxt-active', 'false' ) === 'true', 'update' => get_option( $this->plugin_name . '-adstxt-update', 'false' ) === 'true', ), 'presizedDiv' => array( 'actirise' => get_option( $this->plugin_name . '-presizeddiv-actirise', array() ), 'selected' => get_option( $this->plugin_name . '-presizeddiv-selected', array() ), 'enabled' => get_option( $this->plugin_name . '-presizeddiv-active', 'false' ) === 'true', 'notif' => get_option( $this->plugin_name . '-presizeddiv-notif', array() ), ), 'cache' => array( 'wprocket' => defined( 'WP_ROCKET_VERSION' ), 'wpmeteor' => defined( 'WPMETEOR_VERSION' ), 'litespeed' => defined( 'LSCWP_V' ), ), 'fastcmp' => Helpers::get_fastcmp_options( false ), 'api' => array( 'api_url' => ACTIRISE_URL_API, 'api_url_v2' => ACTIRISE_URL_API_V2, 'api_token' => get_option( $this->plugin_name . '-settings-analytics-token', '' ), 'api_version' => get_option( $this->plugin_name . '-api_version', 'v1' ), 'api_userid' => get_option( $this->plugin_name . '-settings-analytics-userid', '' ), 'currency' => get_option( $this->plugin_name . '-currency', 'USD' ), ), 'autoUpdate' => get_option( $this->plugin_name . '-auto-update', 'false' ) === 'true', 'logs' => Logger::GetLogs(), ), "server" => array( "php" => array( "version" => phpversion(), "extensions" => get_loaded_extensions(), ), "webserver" => array( "server_software" => $_SERVER['SERVER_SOFTWARE'], "server_protocol" => $_SERVER['SERVER_PROTOCOL'], "request_time" => $_SERVER['REQUEST_TIME'], "request_time_float" => $_SERVER['REQUEST_TIME_FLOAT'], ), ), ) ); } /** * Update token to Actirise API * * @since 2.0.0 * @param string $new_token * @param string|boolean $old_token * @return bool|string */ public static function update_to_api($new_token, $old_token){ if (get_option( 'actirise-api_version', '' ) === 'v2') { $args = array( 'domain' => rawurlencode( wp_unslash( $_SERVER['HTTP_HOST'] ) ), "token" => $new_token, "oldToken" => $old_token === false ? '' : $old_token, ); } else { $args = array( 'domain' => rawurlencode( wp_unslash( $_SERVER['HTTP_HOST'] ) ), "new_token" => $new_token, "old_token" => $old_token, ); } $api_url = 'wordpress/token'; $api = new Api(); $response = $api->post( 'api', $api_url, $args); if ( is_wp_error( $response ) ) { Logger::AddLog( 'update_to_api wp error ' . $response->get_error_code(), 'public/include/debug', 'error' ); return false; } if ( ! is_array( $response ) ) { Logger::AddLog( 'update_to_api is not array', 'public/include/debug', 'error' ); return false; } if ( ! isset( $response['token'] ) ) { Logger::AddLog( 'update_to_api is not isset', 'public/include/debug', 'error' ); return false; } return $response['token']; } /** * Get list of plugin installed and activated * * @since 2.0.0 * @return array */ private function get_plugin_info() { if ( ! function_exists( 'get_plugins' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } $all_plugins = get_plugins(); /** @var array $active_plugins */ $active_plugins = get_option( 'active_plugins', array() ); $plugins = array(); foreach ( $all_plugins as $key => $value ) { $is_active = in_array( $key, $active_plugins ); $plugins[ $key ] = array( 'name' => $value['Name'], 'version' => $value['Version'], 'active' => $is_active, ); } // sort by name and active uasort( $plugins, function( $a, $b ) { if ( $a['active'] === $b['active'] ) { return 0; } return ( $a['active'] > $b['active'] ) ? -1 : 1; } ); return $plugins; } /** * Get list of custom fields * * @since 2.0.0 * @return array */ private function get_custom_fields() { global $wpdb; $cache_key = 'actirise_cache_custom_fields'; $customFields = wp_cache_get( $cache_key ); if ( false === $customFields ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery $customFields = $wpdb->get_results( "SELECT DISTINCT meta_key FROM $wpdb->postmeta WHERE meta_key NOT LIKE '\_%'", ARRAY_A ); wp_cache_set( $cache_key, $customFields, '', 3600 ); } if ( $customFields !== null && count( $customFields ) > 0 ) { $customFields = array_map( function ( $field ) { return array( 'name' => ucfirst( $field['meta_key'] ), 'value' => 'customFields_' . $field['meta_key'], ); }, $customFields ); } return $customFields; } /** * Get theme name * * @since 2.0.0 * @return string */ private function get_theme_name() { if (is_child_theme()) { $theme = wp_get_theme()->template; return wp_get_theme( $theme )->name; } else { return wp_get_theme()->name; } } /** * Check if the token is expired * * @since 2.0.0 * @return void */ private function check_update() { if ( get_option( $this->plugin_name . '-debug-last-update' ) === false ) { update_option( $this->plugin_name . '-debug-last-update', time() - 3601 ); } $last_update = get_option( $this->plugin_name . '-debug-last-update' ); $now = time(); $diff = $now - $last_update; if ( $diff > 3600 ) { $cron = new Cron(); $cron->refresh_token(); update_option( $this->plugin_name . '-debug-last-update', time() ); } } /** * Check if the token is valid * * @since 2.0.0 * @param string $token * @return boolean */ private function check_token($token) { if ( wp_unslash( $_POST['token'] === null ) ) { $error = new WP_Error( '500', 'Post empty' ); wp_send_json_error( $error ); } $token = wp_unslash( $_POST['token'] ); $current_token = get_option( $this->plugin_name . '-debug-token', '' ); if ( $token !== $current_token ) { Logger::AddLog( 'check_token token invalide', 'public/include/debug', 'error' ); $error = new WP_Error( '500', 'Token invalid' ); wp_send_json_error( $error ); } return true; } }