wsl.watchdog.php ➔ wsl_watchdog_init()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 34
rs 9.376
c 0
b 0
f 0
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 logging agent
11
*
12
* This is an utility to Logs WSL authentication process to a file or database.
13
*
14
* Note:
15
*   Things ain't optimized here but will do for now.
16
*/
17
18
// Exit if accessed directly
19
if( ! defined( 'ABSPATH' ) ) exit;
20
21
// --------------------------------------------------------------------
22
23
function wsl_watchdog_init()
24
{
25
	if( ! get_option( 'wsl_settings_debug_mode_enabled' ) )
26
	{
27
		return;
28
	}
29
30
	define( 'WORDPRESS_SOCIAL_LOGIN_DEBUG_API_CALLS', true );
31
32
	add_action( 'wsl_process_login_start', 'wsl_watchdog_wsl_process_login' );
33
	add_action( 'wsl_process_login_begin_start', 'wsl_watchdog_wsl_process_login_begin_start' );
34
	add_action( 'wsl_process_login_end_start', 'wsl_watchdog_wsl_process_login_end_start' );
35
36
	add_action( 'wsl_hook_process_login_before_hybridauth_authenticate', 'wsl_watchdog_wsl_hook_process_login_before_hybridauth_authenticate', 10, 2 );
37
	add_action( 'wsl_hook_process_login_after_hybridauth_authenticate', 'wsl_watchdog_wsl_hook_process_login_after_hybridauth_authenticate', 10, 2 );
38
39
	add_action( 'wsl_process_login_end_get_user_data_start', 'wsl_watchdog_wsl_process_login_end_get_user_data_start', 10, 2 );
40
41
	add_action( 'wsl_process_login_complete_registration_start', 'wsl_watchdog_wsl_process_login_complete_registration_start', 10, 3 );
42
43
	add_action( 'wsl_process_login_create_wp_user_start', 'wsl_watchdog_wsl_process_login_create_wp_user_start', 10, 4 );
44
	add_action( 'wsl_hook_process_login_alter_wp_insert_user_data', 'wsl_watchdog_wsl_hook_process_login_alter_wp_insert_user_data', 10, 3 );
45
	add_action( 'wsl_process_login_update_wsl_user_data_start', 'wsl_watchdog_wsl_process_login_update_wsl_user_data_start', 10, 5 );
46
47
	add_action( 'wsl_process_login_authenticate_wp_user_start', 'wsl_watchdog_wsl_process_login_authenticate_wp_user_start', 10, 5 );
48
49
	add_action( 'wsl_hook_process_login_before_wp_set_auth_cookie', 'wsl_watchdog_wsl_hook_process_login_before_wp_set_auth_cookie', 10, 4 );
50
	add_action( 'wsl_hook_process_login_before_wp_safe_redirect', 'wsl_watchdog_wsl_hook_process_login_before_wp_safe_redirect', 10, 5 );
51
52
	add_action( 'wsl_process_login_render_error_page', 'wsl_watchdog_wsl_process_login_render_error_page', 10, 4 );
53
	add_action( 'wsl_process_login_render_notice_page', 'wsl_watchdog_wsl_process_login_render_notice_page', 10, 1 );
54
55
	add_action( 'wsl_log_provider_api_call', 'wsl_watchdog_wsl_log_provider_api_call', 10, 8 );
56
}
57
58
// --------------------------------------------------------------------
59
60
function wsl_watchdog_log_action( $action_name, $action_args = array(), $user_id = 0 )
61
{
62
	$provider = wsl_process_login_get_selected_provider();
63
64
	if( ! $provider )
65
	{
66
		if( isset( $_REQUEST['hauth_start'] ) ) $provider = $_REQUEST['hauth_start'];
67
		if( isset( $_REQUEST['hauth_done'] ) ) $provider = $_REQUEST['hauth_done'];
68
	}
69
70
	$action_args[] = "Backtrace: " . wsl_generate_backtrace();
71
	$action_args[] = 'USER: ' . get_current_user() . '. PID: ' . getmypid() . '. MEM: ' . ceil( memory_get_usage() / 1024 ) . 'KB.';
72
73
	if( get_option( 'wsl_settings_debug_mode_enabled' ) == 1 )
74
	{
75
		return wsl_watchdog_log_to_file( $action_name, $action_args, $user_id, $provider );
76
	}
77
78
	wsl_watchdog_log_to_database( $action_name, $action_args, $user_id, $provider );
79
}
80
81
// --------------------------------------------------------------------
82
83
function wsl_watchdog_log_to_file( $action_name, $action_args = array(), $user_id = 0, $provider = '' )
84
{
85
	$wp_upload_dir = wp_upload_dir();
86
	wp_mkdir_p( $wp_upload_dir['basedir'] . '/wordpress-social-login' );
87
	$wsl_path = $wp_upload_dir['basedir'] . '/wordpress-social-login';
88
	@file_put_contents( $wsl_path . '/.htaccess', "Deny from all" );
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
89
	@file_put_contents( $wsl_path . '/index.html', "" );
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
90
91
	$extra = '';
92
	if( in_array( $action_name, array( 'dbg:provider_api_call', 'wsl_hook_process_login_alter_wp_insert_user_data', 'wsl_process_login_update_wsl_user_data_start', 'wsl_process_login_authenticate_wp_user' ) ) )
93
	$extra = print_r( $action_args, true );
94
95
	$log_path = $wsl_path . '/auth-log-' . date ('d.m.Y') . '.log';
96
	file_put_contents( $log_path, "\n" . implode( ' -- ', array( session_id(), date('d-m-Y H:i:s'), $_SERVER['REMOTE_ADDR'], $provider, $user_id, $action_name, wsl_get_current_url(), $extra ) ), FILE_APPEND );
97
}
98
99
// --------------------------------------------------------------------
100
101
function wsl_watchdog_log_to_database( $action_name, $action_args = array(), $user_id = 0, $provider = '' )
102
{
103
	global $wpdb;
104
105
	$sql = "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}wslwatchdog` ( 
106
			  `id` int(11) NOT NULL AUTO_INCREMENT,
107
			  `session_id` varchar(50) NOT NULL,
108
			  `user_id` int(11) NOT NULL,
109
			  `user_ip` varchar(50) NOT NULL,
110
			  `url` varchar(450) NOT NULL,
111
			  `provider` varchar(50) NOT NULL,
112
			  `action_name` varchar(255) NOT NULL,
113
			  `action_args` text NOT NULL,
114
			  `is_connected` int(11) NOT NULL,
115
			  `created_at` varchar(50) NOT NULL,
116
			  PRIMARY KEY (`id`) 
117
			)"; 
118
119
	$wpdb->query( $sql );
120
121
	$wpdb->insert(
122
		"{$wpdb->prefix}wslwatchdog", 
123
			array( 
124
				"session_id"    => session_id(),
125
				"user_id"       => $user_id,
126
				"user_ip"       => $_SERVER['REMOTE_ADDR'],
127
				"url"           => wsl_get_current_url(), 
128
				"provider"      => $provider, 
129
				"action_name"   => $action_name,
130
				"action_args"   => json_encode( $action_args ),
131
				"is_connected"  => get_current_user_id() ? 1 : 0, 
132
				"created_at"    => microtime( true ), 
133
			)
134
		);
135
}
136
137
// --------------------------------------------------------------------
138
139
function wsl_watchdog_wsl_process_login()
140
{
141
	wsl_watchdog_log_action( 'wsl_process_login' );
142
}
143
144
// --------------------------------------------------------------------
145
146
function wsl_watchdog_wsl_process_login_begin_start()
147
{
148
	wsl_watchdog_log_action( 'wsl_process_login_begin_start' );
149
}
150
151
// --------------------------------------------------------------------
152
153
function wsl_watchdog_wsl_process_login_end_start()
154
{
155
	wsl_watchdog_log_action( 'wsl_process_login_end_start' );
156
}
157
158
// --------------------------------------------------------------------
159
160
function wsl_watchdog_wsl_process_login_end_get_user_data_start( $provider, $redirect_to )
161
{
162
	wsl_watchdog_log_action( 'wsl_process_login_end_get_user_data_start', array( $provider, $redirect_to ) );
163
}
164
165
// --------------------------------------------------------------------
166
167
function wsl_watchdog_wsl_hook_process_login_before_hybridauth_authenticate( $provider, $config )
168
{
169
	wsl_watchdog_log_action( 'wsl_hook_process_login_before_hybridauth_authenticate', array( $provider, $config ) );
170
}
171
172
// --------------------------------------------------------------------
173
174
function wsl_watchdog_wsl_hook_process_login_after_hybridauth_authenticate( $provider, $config )
175
{
176
	wsl_watchdog_log_action( 'wsl_hook_process_login_after_hybridauth_authenticate', array( $provider, $config ) );
177
}
178
179
// --------------------------------------------------------------------
180
181
function wsl_watchdog_wsl_process_login_complete_registration_start( $provider, $redirect_to, $hybridauth_user_profile )
182
{
183
	wsl_watchdog_log_action( 'wsl_process_login_complete_registration_start', array( $provider, $redirect_to, $hybridauth_user_profile ) );
184
}
185
186
// --------------------------------------------------------------------
187
188
function wsl_watchdog_wsl_process_login_create_wp_user_start( $provider, $hybridauth_user_profile, $request_user_login, $request_user_email )
189
{
190
	wsl_watchdog_log_action( 'wsl_process_login_create_wp_user_start', array( $provider, $hybridauth_user_profile, $request_user_login, $request_user_email ) );
191
}
192
193
// --------------------------------------------------------------------
194
195
function wsl_watchdog_wsl_hook_process_login_alter_wp_insert_user_data( $userdata, $provider, $hybridauth_user_profile )
196
{
197
	wsl_watchdog_log_action( 'wsl_hook_process_login_alter_wp_insert_user_data', array( $userdata, $provider, $hybridauth_user_profile ) );
198
199
	return $userdata;
200
}
201
202
// --------------------------------------------------------------------
203
204
function wsl_watchdog_wsl_process_login_update_wsl_user_data_start( $is_new_user, $user_id, $provider, $adapter, $hybridauth_user_profile  )
205
{
206
	wsl_watchdog_log_action( 'wsl_process_login_update_wsl_user_data_start', array( $is_new_user, $user_id, $provider, $adapter, $hybridauth_user_profile ), $user_id );
207
}
208
209
// --------------------------------------------------------------------
210
211
function wsl_watchdog_wsl_process_login_authenticate_wp_user_start( $user_id, $provider, $redirect_to, $adapter, $hybridauth_user_profile  )
212
{
213
	wsl_watchdog_log_action( 'wsl_process_login_authenticate_wp_user_start', array( $user_id, $provider, $redirect_to, $adapter, $hybridauth_user_profile ), $user_id );
214
}
215
216
// --------------------------------------------------------------------
217
218
function wsl_watchdog_wsl_hook_process_login_before_wp_set_auth_cookie( $user_id, $provider, $hybridauth_user_profile  )
219
{
220
	wsl_watchdog_log_action( 'wsl_hook_process_login_before_wp_set_auth_cookie', array( $user_id, $provider, $hybridauth_user_profile ), $user_id );
221
}
222
223
// --------------------------------------------------------------------
224
225
function wsl_watchdog_wsl_hook_process_login_before_wp_safe_redirect( $user_id, $provider, $hybridauth_user_profile, $redirect_to )
226
{
227
	wsl_watchdog_log_action( 'wsl_hook_process_login_before_wp_safe_redirect', array( $user_id, $provider, $hybridauth_user_profile, $redirect_to ), $user_id );
228
}
229
230
// --------------------------------------------------------------------
231
232
function wsl_watchdog_wsl_process_login_render_error_page( $e, $config, $provider, $adapter )
233
{
234
	wsl_watchdog_log_action( 'wsl_process_login_render_error_page', array( $e, $config, $provider, $adapter ) );
235
}
236
237
// --------------------------------------------------------------------
238
239
function wsl_watchdog_wsl_process_login_render_notice_page( $message )
240
{
241
	wsl_watchdog_log_action( 'wsl_process_login_render_notice_page', array( $message ) );
242
}
243
244
// --------------------------------------------------------------------
245
246
function wsl_watchdog_wsl_log_provider_api_call( $client, $url, $method, $post_data, $http_code, $http_info, $http_response )
247
{
248
	wsl_watchdog_log_action( 'dbg:provider_api_call', array( $client, $url, $method, $post_data, $http_code, $http_info, $http_response ) );
249
}
250
251
// --------------------------------------------------------------------
252