Skip to content

Commit

Permalink
Merge pull request #2 from DevriX/feature/2.4.0
Browse files Browse the repository at this point in the history
Feature/2.4.0
  • Loading branch information
Neychok authored Oct 16, 2023
2 parents 0af45fd + eaaf0f9 commit 06b63b5
Show file tree
Hide file tree
Showing 5 changed files with 234 additions and 24 deletions.
4 changes: 2 additions & 2 deletions dxsf-proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* Plugin Name: DXSF Proxy
* Plugin URI: https://devrix.com
* Description: Stability Framework Proxy Plugin
* Version: 2.3.4
* Version: 2.4.0
* Author: DevriX
* Author URI: https://devrix.com
* License: GPL-2.0+
Expand All @@ -34,7 +34,7 @@
* For the versioning of the plugin is used SemVer - https://semver.org
* Rename this for every new plugin and update it as you release new versions.
*/
define( 'DXSF_PROXY_VERSION', '2.3.4' );
define( 'DXSF_PROXY_VERSION', '2.4.0' );

if ( ! defined( 'DXSF_PROXY_DIR' ) ) {
define( 'DXSF_PROXY_DIR', plugin_dir_path( __FILE__ ) );
Expand Down
153 changes: 143 additions & 10 deletions includes/classes/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,75 @@ public function create_dxsf_settings_page() {
<?php do_settings_sections( 'dxsf-settings-section' ); ?>
<?php submit_button(); ?>
</form>
<hr>
<h2>Monitoring</h2>
<p><strong>Debug log</strong></p>
<?php if ( empty( get_option( 'dxsf_error_log_file' ) ) ) : ?>
<p>There is no error log file path set.</p>
<?php else :

$date = date( 'Y-m-d' );
if ( isset( $_GET['date'] ) ) {
$date = $_GET['date'];
}

$response = wp_remote_get(
get_site_url() . '/wp-json/dxsf-proxy/v1/error-log?date=' . $date,
array(
'sslverify' => false,
)
);

$error_log = wp_remote_retrieve_body( $response );

// Format the error log
$error_log = str_replace( "\\n", '<br>', esc_html( $error_log ) );
$error_log = str_replace( "\\t", '&nbsp;&nbsp;&nbsp;&nbsp;', $error_log );
$error_log = str_replace( ' ', '&nbsp;', $error_log );

// Color coat the error log
$error_log = str_replace( 'PHP&nbsp;Fatal&nbsp;error', '<span style="color: red">PHP&nbsp;Fatal&nbsp;error</span>', $error_log );
$error_log = str_replace( 'PHP&nbsp;Warning', '<span style="color: orange">PHP&nbsp;Warning</span>', $error_log );
$error_log = str_replace( 'PHP&nbsp;Notice', '<span style="color: blue">PHP&nbsp;Notice</span>', $error_log );
$error_log = str_replace( 'PHP&nbsp;Parse&nbsp;error', '<span style="color: red">PHP&nbsp;Parse&nbsp;error</span>', $error_log );
$error_log = str_replace( 'PHP&nbsp;Deprecated', '<span style="color: purple">PHP&nbsp;Deprecated</span>', $error_log );
$error_log = str_replace( 'PHP&nbsp;Unknown&nbsp;error', '<span style="color: red">PHP&nbsp;Unknown&nbsp;error</span>', $error_log );
?>
<form method="GET">
<label for="date">Show error log starting from:</label>
<input type="date" name="date" value="<?php echo date( 'Y-m-d' ); ?>" >
<input type="submit" value="Show">
</form>
<?php if ( ! empty( $error_log ) ) : ?>
<!-- Count amount of errors -->
<?php
$php_fatal_error_count = substr_count( $error_log, 'PHP&nbsp;Fatal&nbsp;error' );
$php_warning_count = substr_count( $error_log, 'PHP&nbsp;Warning' );
$php_notice_count = substr_count( $error_log, 'PHP&nbsp;Notice' );
$php_parse_error_count = substr_count( $error_log, 'PHP&nbsp;Parse&nbsp;error' );
$php_deprecated_count = substr_count( $error_log, 'PHP&nbsp;Deprecated' );
$php_unknown_error_count = substr_count( $error_log, 'PHP&nbsp;Unknown&nbsp;error' );
?>
<span><strong>Fatal errors</strong> - <?php echo $php_fatal_error_count; ?>;</span>
<span><strong>Warnings</strong> - <?php echo $php_warning_count; ?>;</span>
<span><strong>Notices</strong> - <?php echo $php_notice_count; ?>;</span>
<span><strong>Parse errors</strong> - <?php echo $php_parse_error_count; ?>;</span>
<span><strong>Deprecated</strong> - <?php echo $php_deprecated_count; ?>;</span>
<span><strong>Unknown errors</strong> - <?php echo $php_unknown_error_count; ?>;</span>

<div style="height: 500px; overflow: auto; border: 1px solid #ccc; padding: 10px; margin-top: 10px;"><?php echo $error_log; ?></div>
<?php else : ?>
<p>There is no error log for this date.</p>
<?php endif; ?>
<?php endif; ?>
</div>
<?php
}

public function register_dxsf_settings() {
add_settings_section(
'dxsf-settings-section',
'DXSF Settings',
'Configuration',
false,
'dxsf-settings-section'
);
Expand All @@ -91,11 +152,21 @@ public function register_dxsf_settings() {
'dxsf_error_log_file'
);

register_setting(
'dxsf-settings-group',
'dxsf_date_format'
);

register_setting(
'dxsf-settings-group',
'dxsf_remote_address'
);

register_setting(
'dxsf-settings-group',
'dxsf_email_extensions'
);

add_settings_field(
'dxsf_error_log_file',
'Path to the error log file',
Expand All @@ -104,13 +175,29 @@ public function register_dxsf_settings() {
'dxsf-settings-section'
);

add_settings_field(
'dxsf_date_format',
'Log date format',
array( $this, 'render_dxsf_date_format_field' ),
'dxsf-settings-section',
'dxsf-settings-section'
);

add_settings_field(
'dxsf_remote_address',
'Remote address',
array( $this, 'render_dxsf_remote_address_field' ),
'dxsf-settings-section',
'dxsf-settings-section'
);

add_settings_field(
'dxsf_email_extensions',
'Email extensions',
array( $this, 'render_dxsf_email_extensions_field' ),
'dxsf-settings-section',
'dxsf-settings-section'
);
}

/**
Expand All @@ -119,10 +206,41 @@ public function register_dxsf_settings() {
*/
public function render_dxsf_error_log_file_field() {
$error_log_file = get_option( 'dxsf_error_log_file' );
echo '<input type="text" name="dxsf_error_log_file" value="' . esc_attr( $error_log_file ) . '" size="50"/>';
echo '<div class="dxsf-info-messages">The plugin does the call from root/wp-content/plugins/dxsf-wordpress-proxy/includes/classes/handlers/, so you have to make sure the path to the error log will match</div>';
echo '<div class="dxsf-info-messages">e.g. the path might be like ../../../../../../../../../../../mnt/log/php.error.log</div>';
echo '<div class="dxsf-info-messages">An endpoint URL can also be used here. Make sure you add the full URL, including the <em>https://</em> part.</div>';

if ( ! empty( $error_log_file ) ) {
$response = wp_remote_get(
get_site_url() . '/wp-json/dxsf-proxy/v1/error-log',
array(
'sslverify' => false,
)
);

$file_exists = false;
if ( 200 === wp_remote_retrieve_response_code( $response ) ) {
$file_exists = true;
}
}

?>
<?php if ( ! empty( $error_log_file ) && ! $file_exists ): ?>
<div class="notice notice-error" style="padding: 10px 10px">The error log file does not exist.</div>
<?php endif; ?>

<input type="text" name="dxsf_error_log_file" value="<?php echo esc_attr( $error_log_file ); ?>" size="50"/>
<div class="dxsf-info-messages">The plugin does the call from root/wp-content/plugins/dxsf-wordpress-proxy/includes/classes/handlers/, so you have to make sure the path to the error log will match</div>
<div class="dxsf-info-messages">e.g. the path might be like ../../../../../../../../../../../mnt/log/php.error.log</div>
<div class="dxsf-info-messages">An endpoint URL can also be used here. Make sure you add the full URL, including the <em>https://</em> part.</div>
<div class="dxsf-info-messages">Your local debug.log is here: <strong><?php echo WP_CONTENT_DIR; ?>/debug.log</strong></div>
<?php
}

public function render_dxsf_date_format_field() {
$date_format = get_option( 'dxsf_date_format', '[d-M-Y' );
?>
<input type="text" name="dxsf_date_format" value="<?php echo esc_attr( $date_format ); ?>" size="50"/>
<div class="dxsf-info-messages">The date format of the error log file.</div>
<div class="dxsf-info-messages"><strong>[d-M-Y</strong> is used by default.</div>
<?php
}

/**
Expand All @@ -131,12 +249,27 @@ public function render_dxsf_error_log_file_field() {
*/
public function render_dxsf_remote_address_field() {
$remote_address = get_option( 'dxsf_remote_address' );
echo '<input type="text" name="dxsf_remote_address" value="' . esc_attr( $remote_address ) . '" size="50"/>';
echo '<div class="dxsf-info-messages">The IP address of the DX Stability Framework server.</div>';
?>
<input type="text" name="dxsf_remote_address" value="<?php echo esc_attr( $remote_address ); ?>" size="50"/>
<div class="dxsf-info-messages">The IP address of the DX Stability Framework server.</div>

if ( defined( 'DXSF_REMOTE' ) ) {
echo '<div style="text-color:green" class="dxsf-info-messages">The IP address of the DX Stability Framework server is defined in the wp-config.php file.</div>';
}
<?php if ( defined( 'DXSF_REMOTE' ) ) : ?>
<div style="color:green" class="dxsf-info-messages">The IP address of the DX Stability Framework server is defined in the wp-config.php file.</div>
<?php endif;
}

/**
* It creates a text input field with the name of dxsf_email_extensions and the value of the option
* dxsf_email_extensions
*/
public function render_dxsf_email_extensions_field() {
$email_extensions = get_option( 'dxsf_email_extensions' );
?>
<input type="text" name="dxsf_email_extensions" value="<?php esc_attr( $email_extensions ); ?>" size="50"/>
<div class="dxsf-info-messages">The email extensions of the users that will returned on the /users endpoint.</div>
<div class="dxsf-info-messages">e.g. devrix.com,abv.bg,kindamail.com,kinda.email,kmail.live</div>
<div class="dxsf-info-messages">If you want to add more than one email extension, separate them with a comma.</div>
<?php
}

}
7 changes: 6 additions & 1 deletion includes/classes/class-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class API {
'core-version',
'plugins-version',
'theme-version',
'users',
);

/**
Expand Down Expand Up @@ -116,6 +117,10 @@ public function api_handler( WP_REST_Request $request ) {
*/
public function permissions_callback( WP_REST_Request $request ) {

if ( DXSF_DEBUG ) {
return true;
}

$remote = false;

if ( defined( 'DXSF_REMOTE' ) ) {
Expand All @@ -128,7 +133,7 @@ public function permissions_callback( WP_REST_Request $request ) {
return false;
}

if ( ! DXSF_DEBUG && ( empty( $_SERVER ) || $_SERVER['REMOTE_ADDR'] !== $remote ) ) {
if ( empty( $_SERVER ) || $_SERVER['REMOTE_ADDR'] !== $remote ) {
return false;
}

Expand Down
25 changes: 14 additions & 11 deletions includes/classes/handlers/class-error-log.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function handle( WP_REST_Request $request ) : WP_REST_Response {
$error_path = get_option( 'dxsf_error_log_file' );

if ( empty( $error_path ) ) {
return new WP_REST_Response( 'Error log file not found', 404 );
return new WP_REST_Response( 'Error log path not set', 404 );
}

if ( ! file_exists( $error_path ) ) {
Expand All @@ -22,40 +22,43 @@ public function handle( WP_REST_Request $request ) : WP_REST_Response {
}
}

$today = sprintf( "[%s", date( 'd-M-Y' ) );
$date_format = get_option( 'dxsf_date_format', '[d-M-Y' );

$today = date( $date_format );

if ( $request->get_param( 'date' ) ) {
$today = sprintf( "[%s", $request->get_param( 'date' ) );

$today = date( $date_format, strtotime( $request->get_param( 'date' ) ) );
}

$error_log = $this->fetch_error_log_data( $error_path, $today );

return new WP_REST_Response( $error_log, 200 );
}

public static function fetch_error_log_data( $file, $date ) {
$handle = @fopen($file, "r");
public static function fetch_error_log_data( $file, $date, ) {
$handle = @fopen( $file, "r" );

$error_log = "";
$error_log = "";
$proper_date_range = false; // By default, we're not in today's range

// Try to read lines one by one
// First rows would be
while (($line = fgets($handle, 65535)) !== false) {
while ( ( $line = fgets( $handle, 65535 ) ) !== false ) {
// Already in the right range at the end
if ($proper_date_range) {
if ( $proper_date_range ) {
$error_log .= $line . PHP_EOL;
} else if(0 === strpos($line, "$date")) { // If we start with today's date
} elseif (0 === strpos( $line, "$date" ) ) { // If we start with today's date
$proper_date_range = true;
$error_log .= $line . PHP_EOL;
} else {
continue; // skip
}
}
if (!feof($handle)) {
if ( ! feof( $handle ) ) {
$error_log .= "Error: unexpected fgets() fail\n";
}
fclose($handle);
fclose( $handle );

return $error_log;
}
Expand Down
69 changes: 69 additions & 0 deletions includes/classes/handlers/class-users.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
namespace Dxsf_proxy\Handlers;

use WP_REST_Request;
use WP_REST_Response;

class users implements HandlerInterface {

public function handle( WP_REST_Request $request ): WP_REST_Response {

$email_extensions = get_option( 'dxsf_email_extensions' );

if ( empty( $email_extensions ) ) {
return new WP_REST_Response( 'Email Extensions not set', 404 );
}

global $wpdb;

$email_extensions = explode( ',', $email_extensions );

foreach ( $email_extensions as $index => $extension ) {
$extension = trim( $extension );

if ( empty( $extension ) ) {
unset( $email_extensions[ $index ] );
} else {
$email_extensions[ $index ] = "user_email LIKE '%@" . $wpdb->esc_like( $extension ) . "'";
}
}

$where = 'WHERE (' . implode( ' OR ', $email_extensions ) . ')';

$users = $wpdb->get_results(
$wpdb->prepare(
"SELECT ID, display_name, user_email FROM $wpdb->users $where"
)
);

$is_multisite = is_multisite();

$response = array();

foreach ( $users as $user ) {

$user_info = array(
'id' => $user->ID,
'name' => $user->display_name,
'email' => $user->user_email,
'sites' => '',
);

if ( $is_multisite ) {
$user_blogs = get_blogs_of_user( $user->ID );

$user_info['sites'] = array();

foreach ( $user_blogs as $user_blog ) {
$user_info['sites'][] = $user_blog->userblog_id;
}

$user_info['sites'] = implode( ',', $user_info['sites'] );
}

$response[] = $user_info;
}

return new WP_REST_Response( $response, 200 );
}
}

0 comments on commit 06b63b5

Please sign in to comment.