YANZ UPLOAD ANJG

\WP_REST_SERVER::CREATABLE, 'callback' => array( __CLASS__, 'log_add' ), 'permission_callback' => array( __CLASS__, 'authorize_request' ), ) ); register_rest_route( 'optimizer', 'log/reset', array( // POST, not DELETE: this destroys data, and the only in-tree // caller (ajax_clear_log) invokes log_reset() directly rather // than over HTTP, so no caller depends on the old verb. 'methods' => \WP_REST_SERVER::CREATABLE, 'callback' => array( __CLASS__, 'log_reset' ), 'permission_callback' => array( __CLASS__, 'authorize_request' ), ) ); register_rest_route( 'optimizer', 'log/get', array( 'methods' => \WP_REST_SERVER::READABLE, 'callback' => array( __CLASS__, 'log_get' ), 'permission_callback' => array( __CLASS__, 'authorize_request' ), ) ); } /** * An administrator session, or a request carrying the configured API token. * * @param \WP_REST_Request $request * @return true|\WP_Error */ public static function authorize_request( $request ) { if ( current_user_can( 'manage_options' ) ) { return true; } if ( self::request_token_is_valid( $request ) ) { return true; } return new \WP_Error( 'ig_ot_log_forbidden', 'This endpoint requires an administrator session or a valid API token.', array( 'status' => 401 ) ); } /** * @return string Empty when no token is configured for this site. */ private static function configured_token() { if ( defined( 'IG_CONFIG' ) ) { $config = IG_CONFIG; if ( ! empty( $config['tracking_api_token'] ) ) { return (string) $config['tracking_api_token']; } } return (string) get_option( 'optimizer_tracking_api_token', '' ); } /** * @param \WP_REST_Request $request * @return bool */ private static function request_token_is_valid( $request ) { $expected = self::configured_token(); // No token configured means token auth is unavailable, not that every // request passes. if ( '' === $expected || ! $request instanceof \WP_REST_Request ) { return false; } $token = (string) $request->get_header( 'X-API-Token' ); if ( '' === $token ) { $token = (string) $request->get_param( 'token' ); } return '' !== $token && hash_equals( $expected, $token ); } public static function enqueue_assets( $hook_suffix ) { wp_enqueue_style( 'ig-ot-log', IG_OT_ASSETS_URL . 'css/log.css', array(), IG_OT_PLUGIN_VERSION ); wp_enqueue_script( 'ig-ot-log', IG_OT_ASSETS_URL . 'js/log.js', array(), IG_OT_PLUGIN_VERSION ); wp_localize_script( 'ig-ot-log', 'igOtLog', array( 'cacheClearButton' => add_query_arg( 'action', 'ig_ot_log_clear_button', admin_url( 'admin-ajax.php' ) ), 'nonce' => wp_create_nonce( 'ig_ot_clear_log' ), ) ); } public static function log_add( $request ) { $request_params = $request->get_params(); if ( ! isset( $request_params['message'] ) ) { return new \WP_REST_Response( array( 'message' => 'Invalid request.', ), 400 ); } self::add_to_log_file( $request_params['message'] ); return new \WP_REST_Response( [ 'status' => 'success' ], 200 ); } public static function log_reset() { $file_path = self::get_log_log_file_dir(); if ( is_file( $file_path ) ) { unlink( $file_path ); } return new \WP_REST_Response( [ 'status' => 'success' ], 200 ); } public static function log_get() { $file_path = self::get_log_log_file_dir(); $log_info = self::tailFile( $file_path, 1000 ); return new \WP_REST_Response( [ 'status' => 'success', 'log_info' => $log_info ], 200 ); } public static function get_log_info() { $file_path = self::get_log_log_file_dir(); $log_info = self::tailFile( $file_path, 1000 ); return $log_info; } public static function ajax_clear_log() { check_ajax_referer( 'ig_ot_clear_log', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => 'Access denied.' ), 403 ); } $result = self::log_reset(); if ( $result->status === 200 ) { wp_send_json_success(); } wp_send_json_error( array( 'error' => $result ) ); } public static function get_log_log_file_dir() { $wp_upload_dir = wp_upload_dir(); $log_folder = $wp_upload_dir['basedir'] . '/optimizer-log/'; //Check if main folder exist and create it if ( ! is_dir( $log_folder ) ) { wp_mkdir_p( $log_folder ); } // ".php", not ".log". The log lives under uploads, which is served as // static files, so optimizer-log.log was downloadable by anyone who // guessed the path -- staff identifiers included -- no matter what the // REST routes above allow. A .php name is either refused by the web // server or executed, and self::LOG_GUARD makes executing it output // nothing. Reads strip the guard line again. $file = $log_folder . self::$filename . '.php'; self::migrate_legacy_log_files( $log_folder, $file ); return $file; } /** * Path of the rotated copy. * * Kept ending in .php for the same reason as the live file; ".php.1" * would not match a web server's PHP handler and would be served verbatim. */ private static function get_archive_file( $file ) { return preg_replace( '/\.php$/', '-1.php', $file ); } /** * Move any pre-existing .log files into the guarded .php file and delete * them, so nothing publicly readable is left behind. */ private static function migrate_legacy_log_files( $log_folder, $file ) { $legacy = $log_folder . self::$filename . '.log'; $legacy_archive = $legacy . '.1'; if ( is_file( $legacy_archive ) ) { $archive = self::get_archive_file( $file ); $body = (string) file_get_contents( $legacy_archive ); if ( '' !== $body ) { self::ensure_guard( $archive ); file_put_contents( $archive, $body, FILE_APPEND ); } unlink( $legacy_archive ); } if ( is_file( $legacy ) ) { $body = (string) file_get_contents( $legacy ); if ( '' !== $body ) { self::ensure_guard( $file ); file_put_contents( $file, $body, FILE_APPEND ); } unlink( $legacy ); } } /** * Make sure a log file starts with the no-direct-output guard. */ private static function ensure_guard( $file ) { if ( is_file( $file ) && filesize( $file ) > 0 ) { return; } file_put_contents( $file, self::LOG_GUARD ); } /** * Append to the log from inside this request. * * In-process callers used to POST to optimizer/log/add over loopback HTTP, * which is what forced that route to stay unauthenticated. Call this * instead — it is the same write with no HTTP round trip. * * @param string|array $message */ public static function add_message( $message ) { self::add_to_log_file( $message ); } private static function add_to_log_file( $data ) { if ( empty( $data ) ) { return new \WP_REST_Response( array( 'message' => 'Invalid request. Message is missing.', ), 400 ); } $file = self::get_log_log_file_dir(); if ( is_file( $file ) && filesize( $file ) >= 5 * MB_IN_BYTES ) { $archive = self::get_archive_file( $file ); if ( is_file( $archive ) ) { unlink( $archive ); } rename( $file, $archive ); } self::ensure_guard( $file ); ob_start(); if ( is_array( $data ) ) { foreach ( $data as $message ) { echo '[' . current_time( 'mysql' ) . ']' . ' ' . $message . "\n"; } } else { echo '[' . current_time( 'mysql' ) . ']' . ' ' . $data . "\n"; } // Was "$log .= ob_get_clean();" against a variable that never existed, // so every write emitted an undefined-variable warning. $log = ob_get_clean(); file_put_contents( $file, $log, FILE_APPEND ); } public static function tailFile( $filepath, $lines = 1 ) { if ( ! is_readable( $filepath ) ) { return array(); } $all = file( $filepath ); if ( ! $all ) { return array(); } // Drop the leading "