forked from kyleabaker/wp-useragent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-useragent.php
419 lines (371 loc) · 17.1 KB
/
wp-useragent.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
<?php
/*
* Plugin Name: WP-UserAgent
* Plugin URI: https://www.kyleabaker.com/goodies/coding/wp-useragent/
* Description: A simple User-Agent detection plugin that lets you easily insert icons and/or textual web browser and operating system details with each comment.
* Version: 1.1.8
* Author: Kyle Baker
* Author URI: https://www.kyleabaker.com/
* Text Domain: wp-useragent
* Domain Path: /languages/
* //Author: Fernando Briano
* //Author URI: http://picandocodigo.net
*/
/* Copyright 2008-2017 Kyle Baker (email: [email protected])
//Copyright 2008 Fernando Briano (email : [email protected])
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// Security measure
defined( 'ABSPATH' ) or die( 'Cannot access pages directly.' );
// Pre-2.6 compatibility
if (!defined('WP_CONTENT_URL'))
define('WP_CONTENT_URL', get_option('siteurl').'/wp-content');
if (!defined('WP_CONTENT_DIR'))
define('WP_CONTENT_DIR', ABSPATH.'wp-content');
if (!defined('WP_PLUGIN_URL'))
define('WP_PLUGIN_URL', WP_CONTENT_URL.'/plugins');
if (!defined('WP_PLUGIN_DIR'))
define('WP_PLUGIN_DIR', WP_CONTENT_DIR.'/plugins');
// Include our main UA detection functions
include(WP_PLUGIN_DIR.'/wp-useragent/wp-useragent-detect-device.php');
include(WP_PLUGIN_DIR.'/wp-useragent/wp-useragent-detect-os.php');
include(WP_PLUGIN_DIR.'/wp-useragent/wp-useragent-detect-platform.php');
include(WP_PLUGIN_DIR.'/wp-useragent/wp-useragent-detect-trackback.php');
include(WP_PLUGIN_DIR.'/wp-useragent/wp-useragent-detect-webbrowser.php');
include(WP_PLUGIN_DIR.'/wp-useragent/wp-useragent-detect-webbrowser-version.php');
// Plugin Options
$wpua_img_url = WP_PLUGIN_URL.'/wp-useragent/img/';
$wpua_doctype = get_option('wpua_doctype');
$wpua_icon_size = get_option('wpua_icon_size');
$wpua_show_text_icons = get_option('wpua_show_text_icons');
$wpua_icon_style = get_option('wpua_icon_style');
$wpua_icon_style_input = get_option('wpua_icon_style_input');
$wpua_text_using = get_option('wpua_text_using');
$wpua_text_on = get_option('wpua_text_on');
$wpua_text_via = get_option('wpua_text_via');
$wpua_show_version = get_option('wpua_show_version');
$wpua_text_links = get_option('wpua_text_links');
$wpua_show_full_ua = get_option('wpua_show_full_ua');
$wpua_hide_unknown_ua = get_option('wpua_hide_unknown_ua');
$wpua_admin_only = get_option('wpua_admin_only');
$wpua_output_location = get_option('wpua_output_location');
// Migrate settings from previous database schema if necessary
$wpua_old_output_location = get_option('ua_output_location');
if (empty($wpua_output_location) && !empty($wpua_old_output_location))
{
// Attempt to retrieve settings stored using previous schema (transform where needed)
$wpua_doctype = get_option('ua_doctype');
$wpua_icon_size = get_option('ua_comment_size');
$wpua_show_text_icons = (get_option('ua_show_text') === '2') ? 'icons' : ((get_option('ua_show_text') === '3') ? 'text' : 'icons_and_text');
$wpua_icon_style = (get_option('ua_image_style') === '2') ? 'inline' : ((get_option('ua_image_style') === '3') ? 'css' : 'default');
$wpua_icon_style_input = get_option('ua_image_css');
$wpua_text_using = get_option('ua_text_surfing');
$wpua_text_on = get_option('ua_text_on');
$wpua_text_via = get_option('ua_text_via');
$wpua_text_links = (get_option('ua_text_links') === '1') ? 'true' : 'false';
$wpua_show_full_ua = get_option('ua_show_ua_bool');
$wpua_hide_unknown_ua = get_option('ua_hide_unknown_bool');
$wpua_admin_only = get_option('ua_admin_only_bool');
$wpua_output_location = get_option('ua_output_location');
// Add new options and attempt to specify no autoload
if ( ! add_option( 'wpua_doctype', $wpua_doctype, '', 'no' ) ) update_option( 'wpua_doctype', $wpua_doctype );
if ( ! add_option( 'wpua_icon_size', $wpua_icon_size, '', 'no' ) ) update_option( 'wpua_icon_size', $wpua_icon_size );
if ( ! add_option( 'wpua_show_text_icons', $wpua_show_text_icons, '', 'no' ) ) update_option( 'wpua_show_text_icons', $wpua_show_text_icons );
if ( ! add_option( 'wpua_icon_style', $wpua_icon_style, '', 'no' ) ) update_option( 'wpua_icon_style', $wpua_icon_style );
if ( ! add_option( 'wpua_icon_style_input', $wpua_icon_style_input, '', 'no' ) ) update_option( 'wpua_icon_style_input', $wpua_icon_style_input );
if ( ! add_option( 'wpua_text_using', $wpua_text_using, '', 'no' ) ) update_option( 'wpua_text_using', $wpua_text_using );
if ( ! add_option( 'wpua_text_on', $wpua_text_on, '', 'no' ) ) update_option( 'wpua_text_on', $wpua_text_on );
if ( ! add_option( 'wpua_text_via', $wpua_text_via, '', 'no' ) ) update_option( 'wpua_text_via', $wpua_text_via );
if ( ! add_option( 'wpua_text_links', $wpua_text_links, '', 'no' ) ) update_option( 'wpua_text_links', $wpua_text_links );
if ( ! add_option( 'wpua_show_full_ua', $wpua_show_full_ua, '', 'no' ) ) update_option( 'wpua_show_full_ua', $wpua_show_full_ua );
if ( ! add_option( 'wpua_hide_unknown_ua', $wpua_hide_unknown_ua, '', 'no' ) ) update_option( 'wpua_hide_unknown_ua', $wpua_hide_unknown_ua );
if ( ! add_option( 'wpua_admin_only', $wpua_admin_only, '', 'no' ) ) update_option( 'wpua_admin_only', $wpua_admin_only );
if ( ! add_option( 'wpua_output_location', $wpua_output_location, '', 'no' ) ) update_option( 'wpua_output_location', $wpua_output_location );
// New schema is initialized, ready to purge old schema...
delete_option('ua_doctype');
delete_option('ua_comment_size');
delete_option('ua_track_size');
delete_option('ua_show_text');
delete_option('ua_image_style');
delete_option('ua_image_css');
delete_option('ua_text_surfing');
delete_option('ua_text_on');
delete_option('ua_text_via');
delete_option('ua_text_links');
delete_option('ua_show_ua_bool');
delete_option('ua_hide_unknown_bool');
delete_option('ua_admin_only_bool');
delete_option('ua_output_location');
}
// Add new feature option for showing version to db if it didn't previously exist
if (empty($wpua_show_version))
{
$wpua_show_version = 'full';
if ( ! add_option( 'wpua_show_version', $wpua_show_version, '', 'no' ) ) update_option( 'wpua_show_version', $wpua_show_version );
}
// Set defaults
if (empty($wpua_doctype)) $wpua_doctype = 'html';
if (empty($wpua_icon_size)) $wpua_icon_size = '16';
if (empty($wpua_show_text_icons)) $wpua_show_text_icons = 'icons_and_text';
if (empty($wpua_icon_style)) $wpua_icon_style = 'default';
if (empty($wpua_icon_style_input) && (is_null($wpua_icon_style_input) || $wpua_icon_style === 'default')) $wpua_icon_style_input = 'border:0px;vertical-align:middle;';
if (empty($wpua_text_using) && is_null($wpua_text_using)) $wpua_text_using = '';
if (empty($wpua_text_on) && is_null($wpua_text_on)) $wpua_text_on = '';
if (empty($wpua_text_via) && is_null($wpua_text_via)) $wpua_text_via = '';
if (empty($wpua_show_version)) $wpua_show_version = 'full';
if (empty($wpua_text_links)) $wpua_text_links = 'false';
if (empty($wpua_show_full_ua)) $wpua_show_full_ua = 'true';
if (empty($wpua_hide_unknown_ua)) $wpua_hide_unknown_ua = 'false';
if (empty($wpua_admin_only)) $wpua_admin_only = 'false';
if (empty($wpua_output_location)) $wpua_output_location = 'before';
// Safe escape user entered input
$wpua_icon_style_input = wpua_str_escape($wpua_icon_style_input);
$wpua_text_using = wpua_str_escape($wpua_text_using);
$wpua_text_on = wpua_str_escape($wpua_text_on);
$wpua_text_via = wpua_str_escape($wpua_text_via);
/**
* Escape quotes from user input so options are displayed correctly
*/
function wpua_str_escape($str)
{
return esc_attr(htmlspecialchars(wp_strip_all_tags($str, false)));
}
/**
* Initialized by comment_text hook for each comment.
*/
function wpua_useragent()
{
global $comment, $useragent, $wpua_output_location;
// Retrieve user agent string from comment metadata
$useragent = wpua_str_escape($comment->comment_agent);
// Display user agent (before, after, custom), render comment if
// needed and re-apply filter for next comment
if ($wpua_output_location === 'before')
{
wpua_display_useragent(true);
wpua_display_comment();
add_filter('comment_text', 'wpua_useragent');
}
elseif ($wpua_output_location === 'after')
{
wpua_display_comment();
wpua_display_useragent(true);
add_filter('comment_text', 'wpua_useragent');
}
elseif ($wpua_output_location === 'custom')
{
wpua_display_useragent();
}
}
/**
* Print user comment now
*/
function wpua_display_comment()
{
global $comment;
remove_filter('comment_text', 'wpua_useragent');
apply_filters('get_comment_text', $comment->comment_content, $comment);
// The following conditional will hopefully prevent a problem where
// the echo statement will interrupt redirects from the comment page.
if (empty($_POST['comment_post_ID']) || is_admin())
{
echo apply_filters('comment_text', $comment->comment_content, $comment);
}
}
/**
* DEPRECATED: Please use wpua_custom_output() instead
* Deprecated in v1.0.9. Will be removed in a future release.
*/
function useragent_output_custom()
{
wpua_custom_output();
}
/**
* Custom function for displaying the output in non-standard locations. Can
* be used to display user agent in a custom area of user's template.
*/
function wpua_custom_output()
{
global $wpua_output_location, $useragent, $comment;
if ($wpua_output_location === 'custom')
{
$useragent = wpua_str_escape($comment->comment_agent);
wpua_display_useragent();
}
}
/**
* Generates html markup for final user agent output
*/
function wpua_display_useragent($wpua_wrapper_div = false)
{
global $comment, $wpua_show_text_icons, $wpua_text_using, $wpua_text_on, $wpua_text_via, $wpua_show_full_ua, $wpua_hide_unknown_ua, $wpua_doctype;
// Check if the comment is a trackback or a comment
$wpua_useragent = '';
if ($comment->comment_type === 'trackback' || $comment->comment_type === 'pingback')
{
// We've got a trackback...
$trackback = wpua_detect_trackback();
// Should unknown trackbacks be filtered? Display text, icons, or both?
if ($wpua_hide_unknown_ua !== 'true' || !strpos($trackback,'Unknown'))
{
$wpua_useragent = ($wpua_show_text_icons === 'icons_and_text' || $wpua_show_text_icons === 'text') ? "$wpua_text_via $trackback" : $trackback;
}
}
else
{
// We've got a comment...
$webbrowser = wpua_detect_webbrowser();
$platform = wpua_detect_platform();
// Should unknown browsers/platforms be filtered? Display text, icons, or both?
if ($wpua_hide_unknown_ua === 'true' && strpos($webbrowser,'Unknown') && strpos($platform,'Unknown'))
{
// Leave the useragent empty since web browser and platform were unknown and option to hide unknowns is enabled.
}
elseif ($wpua_hide_unknown_ua === 'true' && strpos($webbrowser,'Unknown'))
{
// Web browser was unknown and option to hide unknowns is enabled, only return the platform.
$wpua_useragent = ($wpua_show_text_icons === 'icons_and_text' || $wpua_show_text_icons === 'text') ? "$wpua_text_on $platform" : $platform;
}
elseif ($wpua_hide_unknown_ua === 'true' && strpos($platform,'Unknown'))
{
// Platform was unknown and option to hide unknowns is enabled, only return the web browser.
$wpua_useragent = ($wpua_show_text_icons === 'icons_and_text' || $wpua_show_text_icons === 'text') ? "$wpua_text_using $webbrowser" : $webbrowser;
}
else
{
$wpua_useragent = ($wpua_show_text_icons === 'icons_and_text' || $wpua_show_text_icons === 'text') ? "$wpua_text_using $webbrowser $wpua_text_on $platform" : $webbrowser.$platform;
}
}
// Does the user want to display the full useragent string?
if ($wpua_show_full_ua === 'true')
{
// Attach the full ua string to the output.
$br = (strlen($wpua_useragent) > 0) ? (($wpua_doctype === 'html') ? '<br>' : '<br />') : '';
$wpua_useragent .= "$br<small>".wpua_str_escape($comment->comment_agent)."</small>";
}
if ($wpua_wrapper_div === true)
{
// Wrap WP-UserAgent output in div
$wpua_useragent = "<div class='wp-useragent'>$wpua_useragent</div>";
}
// The following conditional will hopefully prevent a problem where
// the echo statement will interrupt redirects from the comment page.
if (empty($_POST['comment_post_ID']) || is_admin()) echo $wpua_useragent;
}
/**
* Return icon and/or text
*/
function wpua_get_icon_text($link, $title, $code, $type)
{
$icon = wpua_get_icon($title, $code, $type);
$text = wpua_get_text($link, $title);
return "$icon $text";
}
/**
* Generates html markup for <img> tags
*/
function wpua_get_icon($title, $code, $type)
{
global $wpua_show_text_icons, $wpua_icon_size, $wpua_icon_style, $wpua_icon_style_input, $wpua_img_url, $wpua_doctype;
if ($wpua_show_text_icons === 'icons_and_text' || $wpua_show_text_icons === 'icons')
{
// Generate image class or style (most commonly using default style)
$img_style = ($wpua_icon_style === 'css') ? 'class="'.$wpua_icon_style_input.'"' : 'style="'.$wpua_icon_style_input.'"';
// Set the img to display browser/os/device. Ex. http://blogurl/plugins/plugin-name/size/net-os-device/code.png
$img_src = $wpua_img_url.$wpua_icon_size.$type.$code.'.png';
// Select the correct closing tag based on doctype
$img_close = ($wpua_doctype === 'html') ? '' : ' /';
return "<img src='$img_src' title='$title' $img_style alt='$title' height='$wpua_icon_size' width='$wpua_icon_size'$img_close>";
}
return '';
}
/**
* Generates html markup for link <a> tags or returns title if links are disabled
*/
function wpua_get_text($link, $title)
{
global $wpua_show_text_icons, $wpua_text_links;
if ( ($wpua_show_text_icons === 'icons_and_text' || $wpua_show_text_icons === 'text') && $wpua_text_links === 'true' && $title !== 'Unknown')
{
return "<a href='$link' title='$title' rel='nofollow'>$title</a>";
}
elseif ($wpua_show_text_icons === 'icons_and_text' || $wpua_show_text_icons === 'text')
{
return $title;
}
return '';
}
/**
* Add a link to our Options page for Admin users.
*/
add_action('admin_menu', 'wpua_add_option_page');
function wpua_add_option_page()
{
add_options_page('WP-UserAgent', 'WP-UserAgent', 'manage_options', 'wp-useragent/wp-useragent-options.php');
}
/**
* Add quick links to plugins page
*/
$plugin_basename = plugin_basename(__FILE__);
add_filter("plugin_action_links_$plugin_basename", 'wpua_add_action_links' );
function wpua_add_action_links( $links )
{
// Add a link to this plugin's 'Settings' page
$settings_link = '<a href="options-general.php?page=wp-useragent/wp-useragent-options.php">Settings</a>';
array_unshift($links, $settings_link);
return $links;
}
// If the user selected to display output in a standard location
// and not a custom location then lets add a filter here.
if ($wpua_admin_only === 'true' && is_admin())
{
if ($wpua_output_location !== 'custom')
{
add_filter('comment_text', 'wpua_useragent');
}
}
elseif ($wpua_admin_only !== 'true' && $wpua_output_location !== 'custom')
{
add_filter('comment_text', 'wpua_useragent');
}
/**
* Add activation hook to create necessary db options with autoload disabled
*/
register_activation_hook( __FILE__, 'wpua_activation' );
function wpua_activation()
{
// Add new options and attempt to specify no autoload
if ( ! add_option( 'wpua_doctype', '', '', 'no' ) ) update_option( 'wpua_doctype', '' );
if ( ! add_option( 'wpua_icon_size', '', '', 'no' ) ) update_option( 'wpua_icon_size', '' );
if ( ! add_option( 'wpua_show_text_icons', '', '', 'no' ) ) update_option( 'wpua_show_text_icons', '' );
if ( ! add_option( 'wpua_icon_style', '', '', 'no' ) ) update_option( 'wpua_icon_style', '' );
if ( ! add_option( 'wpua_icon_style_input', '', '', 'no' ) ) update_option( 'wpua_icon_style_input', '' );
if ( ! add_option( 'wpua_text_using', '', '', 'no' ) ) update_option( 'wpua_text_using', '' );
if ( ! add_option( 'wpua_text_on', '', '', 'no' ) ) update_option( 'wpua_text_on', '' );
if ( ! add_option( 'wpua_text_via', '', '', 'no' ) ) update_option( 'wpua_text_via', '' );
if ( ! add_option( 'wpua_show_version', '', '', 'no' ) ) update_option( 'wpua_show_version', '' );
if ( ! add_option( 'wpua_text_links', '', '', 'no' ) ) update_option( 'wpua_text_links', '' );
if ( ! add_option( 'wpua_show_full_ua', '', '', 'no' ) ) update_option( 'wpua_show_full_ua', '' );
if ( ! add_option( 'wpua_hide_unknown_ua', '', '', 'no' ) ) update_option( 'wpua_hide_unknown_ua', '' );
if ( ! add_option( 'wpua_admin_only', '', '', 'no' ) ) update_option( 'wpua_admin_only', '' );
if ( ! add_option( 'wpua_output_location', '', '', 'no' ) ) update_option( 'wpua_output_location', '' );
}
/**
* Load plugin textdomain: wp-useragent
*/
add_action( 'plugins_loaded', 'wpua_load_textdomain' );
function wpua_load_textdomain()
{
load_plugin_textdomain( 'wp-useragent', false, plugin_basename( dirname( __FILE__ ) ) . '/languages' );
}
?>