index.php ➔ wsl_component_watchdog_database()   F
last analyzed

Complexity

Conditions 24
Paths 6

Size

Total Lines 163

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 24
nc 6
nop 0
dl 0
loc 163
rs 3.3333
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*!
3
* WordPress Social Login
4
*
5
* https://miled.github.io/wordpress-social-login/ | https://github.com/miled/wordpress-social-login
6
*   (c) 2011-2020 Mohamed Mrassi and contributors | https://wordpress.org/plugins/wordpress-social-login/
7
*/
8
9
/**
10
* WSL Watchdog - Log viewer.
11
*/
12
13
// Exit if accessed directly
14
if ( !defined( 'ABSPATH' ) ) exit;
15
16
// --------------------------------------------------------------------
17
18
function wsl_component_watchdog()
19
{
20
 	if( ! get_option( 'wsl_settings_debug_mode_enabled' ) )
21
	{
22
		return _wsl_e("<p>Debug mode is disabled.</p>", 'wordpress-social-login');
23
	}
24
25
	if( get_option( 'wsl_settings_debug_mode_enabled' ) == 1 )
26
	{
27
		return wsl_component_watchdog_files();
28
	}
29
30
	wsl_component_watchdog_database();
31
}
32
33
wsl_component_watchdog();
34
35
// --------------------------------------------------------------------
36
37
function wsl_component_watchdog_files()
38
{
39
?>
40
<div style="padding: 5px 20px; border: 1px solid #ddd; background-color: #fff;">
41
	<h3></h3>
42
	<h3><?php _wsl_e("Authentication log files viewer", 'wordpress-social-login') ?></h3>
43
44
	<form method="post" action="" style="float: right;margin-top:-45px">
45
		<select name="log_file">
46
			<option value=""> &mdash; <?php _wsl_e("Select a log file to display", 'wordpress-social-login') ?> &mdash;</option>
47
48
			<?php
49
				$wp_upload_dir = wp_upload_dir();
50
				$wsl_path = $wp_upload_dir['basedir'] . '/wordpress-social-login';
51
52
				$selected = isset( $_REQUEST['log_file'] ) ? $_REQUEST['log_file'] : '';
53
54
				$files = scandir( $wsl_path );
55
56
				if( ! empty($files) )
57
					foreach( $files as $file )
58
					{
59
						if( in_array( $file, array( '.', '..', '.htaccess', 'index.html' ) ) )
60
							continue;
61
62
						?>
63
							<option value="<?php echo $file; ?>" <?php if( $selected == $file ) echo 'selected'; ?>><?php echo $file; ?></option>
64
						<?php
65
					}
66
			?>
67
		</select>
68
69
		<input type="submit" value="<?php _wsl_e("View", 'wordpress-social-login') ?>" class="button">
70
	</form>
71
72
	<textarea rows="25" cols="70" wrap="off" style="width:100%;height:580px;margin-bottom:15px;white-space: nowrap;font-family: monospace;font-size: 12px;"><?php if( $selected && file_exists( $wsl_path . '/' . $selected ) ) echo file_get_contents( $wsl_path . '/' . $selected ); ?></textarea>
73
</div>
74
<?php
75
}
76
77
// --------------------------------------------------------------------
78
79
function wsl_component_watchdog_database()
80
{
81
	$assets_base_url = WORDPRESS_SOCIAL_LOGIN_PLUGIN_URL . 'assets/img/16x16/';
82
83
	global $wpdb;
84
85
	// If action eq delete WSL user profiles
86
	if( isset( $_REQUEST['delete'] ) && isset( $_REQUEST['_wpnonce'] ) && wp_verify_nonce( $_REQUEST['_wpnonce'] ) )
87
	{
88
		if( $_REQUEST['delete'] == 'log' )
89
		{
90
			$wpdb->query( "TRUNCATE TABLE {$wpdb->prefix}wslwatchdog" );
91
		}
92
	}
93
?>
94
<style>
95
	.widefatop td, .widefatop th { border: 1px solid #DDDDDD; }
96
	.widefatop th label { font-weight: bold; }
97
</style>
98
99
<div style="padding: 5px 20px; border: 1px solid #ddd; background-color: #fff;">
100
101
	<h3><?php _wsl_e("Authentication log viewer - latest activity", 'wordpress-social-login') ?></h3>
102
103
	<p style="float: right;margin-top:-45px">
104
		<?php
105
			$delete_url = wp_nonce_url( 'options-general.php?page=wordpress-social-login&wslp=watchdog&delete=log' );
106
		?>
107
		<a class="button button-secondary" style="background-color: #da4f49;border-color: #bd362f;text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.25);color: #ffffff;" href="<?php echo $delete_url ?>" onClick="return confirm('Are you sure?');"><?php _wsl_e("Delete WSL Log", 'wordpress-social-login'); ?></a>
108
	</p>
109
110
	<hr />
111
112
	<?php
113
		$list_sessions = $wpdb->get_results( "SELECT user_ip, session_id, provider, max(id) as max_id FROM `{$wpdb->prefix}wslwatchdog` GROUP BY session_id, provider ORDER BY max_id DESC LIMIT 25" );
114
115
		if( ! $list_sessions )
116
		{
117
			_wsl_e("<p>No log found!</p>", 'wordpress-social-login');
118
		}
119
		else
120
		{
121
			foreach( $list_sessions as $seesion_data )
122
			{
123
				$user_ip    = $seesion_data->user_ip;
124
				$session_id = $seesion_data->session_id;
125
				$provider   = $seesion_data->provider;
126
127
				if( ! $provider )
128
				{
129
					continue;
130
				}
131
132
				?>
133
				<div style="padding: 15px; margin-bottom: 8px; border: 1px solid #ddd; background-color: #fff;box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);">
134
					<img src="<?php echo $assets_base_url . strtolower( $provider ) . '.png' ?>" style="vertical-align:top;width:16px;height:16px;" /> <?php echo sprintf( _wsl__("<b>%s</b> : %s - %s", 'wordpress-social-login'), $provider, $user_ip, $session_id ) ?>
135
				</div>
136
137
				<table class="wp-list-table widefat widefatop">
138
					<tr>
139
						<th>#</th>
140
						<th>Action</th>
141
						<th>Args</th>
142
						<th>Time</th>
143
						<th>User</th>
144
						<th style="text-align:center">&#916;</th>
145
					</tr>
146
			<?php
147
				$list_calls = $wpdb->get_results( "SELECT * FROM `{$wpdb->prefix}wslwatchdog` WHERE session_id = '$session_id' AND provider = '$provider' ORDER BY id ASC LIMIT 500" );
148
149
				$abandon    = false;
0 ignored issues
show
Unused Code introduced by
$abandon is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
150
				$newattempt = false;
0 ignored issues
show
Unused Code introduced by
$newattempt is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
151
				$newsession = true;
0 ignored issues
show
Unused Code introduced by
$newsession is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
152
				$functcalls = 0;
0 ignored issues
show
Unused Code introduced by
$functcalls is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
153
				$exectime   = 0;
0 ignored issues
show
Unused Code introduced by
$exectime is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
154
				$oexectime  = 0;
155
				$texectime  = 0;
156
157
				foreach( $list_calls as $call_data )
158
				{
159
					$exectime = (float) $call_data->created_at - ( $oexectime ? $oexectime : (float) $call_data->created_at );
160
					$oexectime = (float) $call_data->created_at;
161
					$texectime += $exectime;
162
163
					$call_data->action_args = json_decode( $call_data->action_args );
164
165
					$newattempt = false;
0 ignored issues
show
Unused Code introduced by
$newattempt is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
166
167
					$action_name_uid = uniqid();
168
169
					$action_desc = 'N.A.';
0 ignored issues
show
Unused Code introduced by
$action_desc is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
170
					?>
171
					<tr  style="<?php if( stristr( $call_data->action_name, 'dbg:' ) ) echo 'background-color:#fffcf5;'; ?> <?php if( 'wsl_render_login_form_user_loggedin' == $call_data->action_name || $call_data->action_name == 'wsl_hook_process_login_before_wp_set_auth_cookie' ) echo 'background-color:#edfff7;'; ?><?php if( 'wsl_process_login_complete_registration_start' == $call_data->action_name ) echo 'background-color:#fefff0;'; ?><?php if( 'wsl_process_login_render_error_page' == $call_data->action_name || $call_data->action_name == 'wsl_process_login_render_notice_page' ) echo 'background-color:#fffafa;'; ?>">
172
						<td nowrap width="10">
173
							<?php echo $call_data->id; ?>
174
						</td>
175
						<td nowrap width="350">
176
							<span style="color:#<?php
177
											if( stristr( $call_data->action_name, 'dbg:' ) ){
178
												echo '333333';
179
											}
180
181
											if( 'wsl_hook_process_login_before_wp_safe_redirect' == $call_data->action_name ){
182
												echo 'a6354b';
183
											}
184
185
											if( 'wsl_hook_process_login_before_wp_set_auth_cookie' == $call_data->action_name ){
186
												echo '9035a6';
187
											}
188
189
											if( 'wsl_process_login_render_error_page' == $call_data->action_name ){
190
												echo 'f50505';
191
											}
192
193
											if( 'wsl_process_login_render_notice_page' == $call_data->action_name ){
194
												echo 'fa1797';
195
											}
196
										?>"
197
										><?php echo $call_data->action_name; ?></span>
198
						</td>
199
						<td>
200
							<span style="float:right;"><a style="font-size:25px" href="javascript:void(0);" onClick="action_args_toggle( '<?php echo $action_name_uid; ?>' )">+</a></span>
201
							<a href="javascript:alert('<?php echo $call_data->url; ?>');">
202
								<small>
203
									<?php
204
										echo substr( $call_data->url, 0, 100 );
205
										echo strlen( $call_data->url ) > 100 ? '...' : '';
206
									?>
207
								</small>
208
							</a>
209
							<pre style="display:none; overflow:scroll; background-color:#fcfcfc; color:#808080;font-size:11px;max-width:750px;" class="action_args_<?php echo $action_name_uid; ?>"><?php echo htmlentities( print_r( $call_data->action_args, true ) ); ?></pre>
210
						</td>
211
						<td nowrap width="115">
212
							<?php echo date( "Y-m-d h:i:s", $call_data->created_at ); ?>
213
						</td>
214
						<td nowrap width="40">
215
							<?php if( $call_data->user_id ) echo '<a href="options-general.php?page=wordpress-social-login&wslp=users&uid=' . $call_data->user_id . '">#' . $call_data->user_id . '</a>'; ?>
216
						</td>
217
						<td nowrap width="10" style="<?php if( $exectime > 0.5 ) echo 'color: #f44 !important;'; ?>">
218
							<?php echo number_format( $exectime, 3, '.', '' ); ?>
219
						</td>
220
					</tr>
221
				<?php
222
				}
223
			?>
224
			</table>
225
			<?php
226
				echo number_format( $texectime, 3, '.', '' );
227
				echo '<br />';
228
			}
229
		}
230
	?>
231
	<script>
232
		function action_args_toggle( action )
233
		{
234
			jQuery('.action_args_' + action ).toggle();
235
236
			return false;
237
		}
238
	</script>
239
</div>
240
<?php
241
}
242
243
// --------------------------------------------------------------------
244