Server : LiteSpeed
System : Linux premium92.web-hosting.com 4.18.0-553.44.1.lve.el8.x86_64 #1 SMP Thu Mar 13 14:29:12 UTC 2025 x86_64
User : rbnsfqys ( 805)
PHP Version : 8.1.33
Disable Function : NONE
Directory :  /home/rbnsfqys/ali.rbn.services/wp-content/plugins/Repairplugin-pro/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]


Current File : /home/rbnsfqys/ali.rbn.services/wp-content/plugins/Repairplugin-pro/error_logs.php
<?php

// Exit if accessed directly

defined( 'ABSPATH' ) || exit;

function rp_get_path_of_wp_content() {

    $wp_content = defined( 'WP_CONTENT_DIR' ) ? WP_CONTENT_DIR : ABSPATH . 'wp-content';

    return str_replace( array( '/', '\\' ), DIRECTORY_SEPARATOR, $wp_content );

}

function rp_get_path_of_repairplugin_error_logs() {

    $wp_content = rp_get_path_of_wp_content();
    
    return $wp_content . DIRECTORY_SEPARATOR . 'repairplugin-error-logs';

}

function rp_create_error_log_folder_if_not_exists() {

    $error_log_folder = rp_get_path_of_repairplugin_error_logs();

    if ( ! file_exists( $error_log_folder ) ) {

        mkdir( $error_log_folder, 0777, true );

        // also create .htaccess file with deny from all
        $htaccess_file = $error_log_folder . DIRECTORY_SEPARATOR . '.htaccess';

        if ( ! file_exists( $htaccess_file ) ) {

            file_put_contents( $htaccess_file, 'deny from all' );

        }

    }

}

function rp_get_all_issues_files() {

    $error_log_folder = rp_get_path_of_repairplugin_error_logs();

    $files = glob( $error_log_folder . DIRECTORY_SEPARATOR . 'issues_*.json' );

    if( !empty( $files ) ) {

        // sort DESC
        rsort( $files );

    }

    return $files;

}

function rp_get_targeted_error_log_file( $fileName = '' ) {

    $error_log_folder = rp_get_path_of_repairplugin_error_logs();

    return $error_log_folder . DIRECTORY_SEPARATOR . $fileName;

}

function rp_get_today_error_log_file() {

    $error_log_folder = rp_get_path_of_repairplugin_error_logs();

    $today = date( 'Y-m-d' );

    return $error_log_folder . DIRECTORY_SEPARATOR . 'issues_' . $today . '.json';

}

function rp_get_todays_existing_errors() {

    $error_log_file = rp_get_today_error_log_file();

    if ( ! file_exists( $error_log_file ) ) {

        return array();

    }

    $errors = file_get_contents( $error_log_file );

    return json_decode( $errors, true );

}

// Error data format:
// array(
//    'timestamp' => 'Timestamp',
//    'event_time' => 'Event time',
//    'message' => 'Error message',
//    'backtrace' => 'Backtrace',
// )
function rp_error_already_exists( $error = array() ) {

    $errors = rp_get_todays_existing_errors();

    foreach ( $errors as $existing_error ) {

        if ( $existing_error['message'] === $error['message'] ) {

            return true;

        }

    }

    return false;

}

function rp_clear_all_error_files() {

    $files = rp_get_all_issues_files();

    try {

        foreach ( $files as $file ) {

            unlink( $file );

        }

    } catch ( Exception|Error|Throwable|ErrorException|ArgumentCountError|DivisionByZeroError|ParseError|TypeError|mysqli_sql_exception $e ) {

        // do nothing

    }

}

function rp_delete_already_existing_error( $error = array() ) {

    $first_occurred = null;

    $first_occurred_timestamp = null;

    $count = 0;

    $errors = rp_get_todays_existing_errors();

    $new_errors = array();

    foreach ( $errors as $existing_error ) {

        if ( $existing_error['message'] !== $error['message'] ) {

            $new_errors[] = $existing_error;

        } else {

            $first_occurred = $existing_error['first_occurred'] ?? '';

            $first_occurred_timestamp = $existing_error['first_occurred_timestamp'] ?? '';

            $count = $existing_error['count'] ?? 0;

            if( empty( $count ) ) {

                $count = 1;

            }

        }

    }

    if( empty( $first_occurred ) ) {

        $first_occurred = date( 'Y-m-d H:i:s' );

    }

    if( empty( $first_occurred_timestamp ) ) {

        $first_occurred_timestamp = time();

    }

    $count++;

    $error_log_file = rp_get_today_error_log_file();

    file_put_contents( $error_log_file, json_encode( $new_errors, JSON_PRETTY_PRINT ) );

    return array( $first_occurred, $first_occurred_timestamp, $count );

}

function rp_add_to_today_error_log( $error = array() ) {

    // Safely try to log the error

    try {

        rp_create_error_log_folder_if_not_exists();

        // add if not exists
        list( $first_occurred, $first_occurred_timestamp, $count ) = rp_delete_already_existing_error( $error );

        $error['first_occurred'] = $first_occurred;

        $error['first_occurred_timestamp'] = $first_occurred_timestamp;

        $error['count'] = $count;

        $error['last_occurred'] = date( 'Y-m-d H:i:s' );

        $error['last_occurred_timestamp'] = time();

        $errors = rp_get_todays_existing_errors();

        $errors[] = $error;

        $error_log_file = rp_get_today_error_log_file();

        file_put_contents( $error_log_file, json_encode( $errors, JSON_PRETTY_PRINT ) );

    } catch ( Exception|Error|Throwable|ErrorException|ArgumentCountError|DivisionByZeroError|ParseError|TypeError|mysqli_sql_exception $e ) {

        // do nothing

    }

}

// add admin notice if any issues file is found
function rp_add_admin_notice_for_issues() {

    $files = rp_get_all_issues_files();

    if ( ! empty( $files ) ) {

        $class = 'notice notice-error';
        $message = __( 'Repairplugin Pro has detected some issues. Please check the errors section on <a href="' . admin_url( 'admin.php?page=repair-plugin#error-logs-section' ) . '">status page</a> for more details.', 'wp-repair-plugin' );

        printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), $message );

    }

}

// add admin notice
add_action( 'admin_notices', 'rp_add_admin_notice_for_issues' );

function rp_debug_backtrace_for_error_log( $is_error = TRUE ) {

    $max_trace = 5;

    $backtrace = debug_backtrace();

    $traced_items = array();

    $current_trace = 1;

    if( $is_error === FALSE ) {

        $current_trace = 2;

        $max_trace = 1;

    }

    while( $current_trace <= ($max_trace + 1) ) {

        if( isset( $backtrace[ $current_trace ] ) ) {

            $current_trace_item = $backtrace[ $current_trace ];

            // remove args if exists
            if( isset( $current_trace_item['args'] ) ) {

                unset( $current_trace_item['args'] );

            }

            // remove object if exists
            if( isset( $current_trace_item['object'] ) ) {

                unset( $current_trace_item['object'] );

            }

            // remove type if exists
            if( isset( $current_trace_item['type'] ) ) {

                unset( $current_trace_item['type'] );

            }

            // Create single line trace with file,line,function,class
            $trace = '#'. ($current_trace - 1) . ' ';

            if( isset( $current_trace_item['file'] ) ) {

                $trace .= $current_trace_item['file'];

            }

            if( isset( $current_trace_item['line'] ) ) {

                $trace .= ':' . $current_trace_item['line'];

            }

            if( isset( $current_trace_item['function'] ) ) {

                $trace .= ' ' . $current_trace_item['function'];

            }

            if( isset( $current_trace_item['class'] ) ) {

                $trace .= ' ' . $current_trace_item['class'];

            }

            $traced_items[] = $trace;

        } else {

            break;

        }

        $current_trace++;

    }

    $traced_items = implode( "\n", $traced_items );
    
    return $traced_items;

}

function rp_download_all_error_files_handler() {

    try {

        // create a zip file of all error files
        $error_log_folder = rp_get_path_of_repairplugin_error_logs();

        $file_name = get_site_url();

        // remove http:// or https://
        $file_name = str_replace( array( 'http://', 'https://' ), '', $file_name );

        // remove /
        $file_name = str_replace( '/', '', $file_name );

        // remove :
        $file_name = str_replace( ':', '', $file_name );

        // add timestamp
        $file_name .= '_' . date( 'Y-m-d_H-i-s' );

        $zip_file = $error_log_folder . DIRECTORY_SEPARATOR . $file_name . '_issues.zip';

        $zip = new ZipArchive;

        if ( $zip->open( $zip_file, ZipArchive::CREATE ) === TRUE ) {

            $files = rp_get_all_issues_files();

            foreach ( $files as $file ) {

                $zip->addFile( $file, basename( $file ) );

            }

            $zip->close();

        }

        // download the zip file
        if ( file_exists( $zip_file ) ) {

            header( 'Content-Type: application/zip' );

            header( 'Content-Disposition: attachment; filename="' . basename( $zip_file ) . '"' );

            header( 'Content-Length: ' . filesize( $zip_file ) );

            readfile( $zip_file );

            unlink( $zip_file );

        } else {

            wp_die( 'Error: File not found' );

        }

    } catch ( Exception|Error|Throwable|ErrorException|ArgumentCountError|DivisionByZeroError|ParseError|TypeError|mysqli_sql_exception $e ) {

        wp_die( 'Error: ' . $e->getMessage() );

    }

}

function rp_remove_repairplugin_pro_from_active_plugins() {

    $active_plugins = get_option( 'active_plugins' );

    $pluginBaseName = defined('WP_REPAIR_PLUGIN_BASENAME') ? WP_REPAIR_PLUGIN_BASENAME : '';

    if( !empty( $pluginBaseName ) ) {

        $key = array_search( $pluginBaseName, $active_plugins );

        if( $key !== false ) {

            unset( $active_plugins[ $key ] );

            update_option( 'active_plugins', $active_plugins );

        }

    }

}

function rp_shutdown_function_for_error_logging( $message = '', $error = array() ) {

    if( !empty( $error ) && ( strpos( strtolower( $error['file'] ), 'repairplugin-pro' ) !== false ) ) {

        $backtrace = '#0 ' . $error['file'] . ':' . $error['line'];
    
        $error_types_to_handle = array(
			E_ERROR => 'E_ERROR',
			E_PARSE => 'E_PARSE',
			E_USER_ERROR => 'E_USER_ERROR',
			E_COMPILE_ERROR => 'E_COMPILE_ERROR',
			E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR',
		);

        $errorType = $error_types_to_handle[ $error['type'] ?? '' ] ?? '';

        $error = array(
            'message' => trim( $errorType .' '. $error['message'] ),
            'backtrace' => $backtrace,
        );

        rp_add_to_today_error_log( $error );

        if( get_option('rp_safe_debug_mode', '0') == '1' ) {

            rp_remove_repairplugin_pro_from_active_plugins();

        }

    }

    return $message;

}

add_filter('wp_php_error_message', 'rp_shutdown_function_for_error_logging', 10, 2);


F1le Man4ger