News:

SMF 2.1.4 has been released! Take it for a spin! Read more.

Main Menu

detectBrowser_update 1.0.1

Started by Antechinus, June 27, 2014, 12:32:09 PM

Previous topic - Next topic

Antechinus

Link to Mod

Note: Removing this one for the moment, as it's an old one and I'm not currently up to speed with the relevant range of user agent changes. It really should be double-checked for relevance.


detectBrowser_update 1.0.1
by Antechinus

This modification (for SMF 2.0 only) updates the standard SMF 2.0.x browser detection.
It adds detection for current versions of Internet Explorer, as well as various phones and tablets.

The updated code should be of use to people who know how to use it, and who want to customise their forum interface.
If you do not intend to customise the coding of your forum, or if you do not know how, this mod will not benefit you.

WARNING: Load.php is a critical file for the operation of your forum.

Do not install this mod if you have manually edited the browser detection array in Sources/Load.php, or if you have installed another mod which edits that array.

If you force the installation of this mod on an array which has previously been edited, you may break your entire site.

The package manager will warn you if there are any conflicts.

Please take note of any warnings. If you see one, do not install this mod.




This mod is licensed under the WTFPL.

If the possibility of seeing profanity means you would prefer to not read the actual license terms, rest assured that you can do whatever you like with this mod's code.




Although it is not critical, it is recommended to install the IE9_calendar_fix mod when installing this one. It will fix some minor bugs that affect the calendar if IE9+ detection is enabled.

Shambles


Antechinus

TBH the main reason I wanted this one officially approved is so I could make it a requirement for the theme I'm working on. :D

I wanted to use the better user agent detection for the theme, and that meant getting Sources edits past the Customize Team. ;)

Ninja ZX-10RR

I need this mod but the former existing one was terribly buggy and I had to uninstall it.
Does this work with Firefox, Chrome, Opera etc too? If yes, how exactly? Is there an admin setting for it?
Quote from: BeastMode topic=525177.msg3720020#msg3720020
It's so powerful that on this post and even in the two PMs you sent me,you still answered my question very quickly and you're apologizing for the delay. You're the #1 support I've probably ever encountered man, so much respect for that. Thank you, and get better soon.

I'll keep this in my siggy for a while just to remind me that someone appreciated what I did while others didn't.

♥ Jess ♥

STOP EDITING MY PROFILE

Antechinus

There is no admin setting. All it does is add more options for browser detection in Sources. To make use of those added options you have to be able to write your own extra code, since SMF 2.0.x does not use them anywhere by default (because they didn't exist when 2.0.x was finalised).

As one example, I wanted to be able to detect iOS devices, because I'm using CSS fixed positioning for some elements and iOS cannot handle that (totally borks scrolling on iOS). This means I want to set those elements back to either absolute or static if the user is running iOS.

The browser array now detects iShiz, like this:

// Teh mobile stuffz. Hopefully it will work. :P
'is_iphone' => (strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone') !== false || strpos($_SERVER['HTTP_USER_AGENT'], 'iPod') !== false) && strpos($_SERVER['HTTP_USER_AGENT'], 'iPad') === false,
'is_ipad' => strpos($_SERVER['HTTP_USER_AGENT'], 'iPad') !== false,



I can use that in my theme, by setting up a browser array in index.template.php, like this:

</head>
<body';

foreach (array('android_phone', 'android_tablet', 'blackberry', 'chrome',
'fennec', 'firefox', 'ie8', 'ie9', 'ie10', 'ie11',
'ie_mobile', 'ie_tablet', 'ipad', 'iphone',
'opera', 'opera_mini', 'safari', 'symbian') as $browser_body_id)

if ($context['browser']['is_' . $browser_body_id])
echo '
id="', $browser_body_id, '"';

echo '
class="', implode(' ',$body_class), '">';
}



If someone is using an iPad on the home page, the body tag HTML will be echoed to the browser as <body id="ipad" class="action_index">. I can then use this in my CSS, like this:

/* iOS can't deal with fixed position elements properly (borks scrolling) */
#ipad>#footer_section>#toplink, #iphone>#footer_section>#toplink {
position: absolute;
padding: 32px 9px 12px 9px;
bottom: 0;
}
#ipad>#botlink, #iphone>#botlink {
position: absolute;
top: 0;
padding: 12px 9px 32px 9px;
}



This stuff is default in 2.1 Alpha. What I'm doing is basically backporting it to 2.0.x, so we can use it while we wait for 2.1. :)

Ninja ZX-10RR

I realised I feel really terrible when I understood that I totally misunderstood the purpose of this mod because I was thinking about something else... I need some time for myself... Feel too bad. Seriously.
Thanks for your reply and congrats for this mod even though I haven't tested it yet.
If I will be off for a few days I could either be dead or resting.

[OT]Hope that your default theme for 2.1 works ;) [/OT]
Quote from: BeastMode topic=525177.msg3720020#msg3720020
It's so powerful that on this post and even in the two PMs you sent me,you still answered my question very quickly and you're apologizing for the delay. You're the #1 support I've probably ever encountered man, so much respect for that. Thank you, and get better soon.

I'll keep this in my siggy for a while just to remind me that someone appreciated what I did while others didn't.

♥ Jess ♥

STOP EDITING MY PROFILE

Antechinus

Ok, well rest up and I hope you feel better soon. :)

Ninja ZX-10RR

Thanks I hope that too...
Meantime I wanted to install it but it fails hard (3rd step) as my code had been heavily modified there... I have this bunch of stuff:
// The following determines the user agent (browser) as best it can.
$context['browser'] = array(
'is_opera' => strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') !== false,
'is_opera6' => strpos($_SERVER['HTTP_USER_AGENT'], 'Opera 6') !== false,
'is_opera7' => strpos($_SERVER['HTTP_USER_AGENT'], 'Opera 7') !== false || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera/7') !== false,
'is_opera8' => strpos($_SERVER['HTTP_USER_AGENT'], 'Opera 8') !== false || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera/8') !== false,
'is_opera9' => preg_match('~Opera[ /]9(?!\\.[89])~', $_SERVER['HTTP_USER_AGENT']) === 1,
'is_opera10' => preg_match('~Opera[ /]10\\.~', $_SERVER['HTTP_USER_AGENT']) === 1 || (preg_match('~Opera[ /]9\\.[89]~', $_SERVER['HTTP_USER_AGENT']) === 1 && preg_match('~Version/1[0-9]\\.~', $_SERVER['HTTP_USER_AGENT']) === 1),
'is_ie4' => strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 4') !== false && strpos($_SERVER['HTTP_USER_AGENT'], 'WebTV') === false,
'is_webkit' => strpos($_SERVER['HTTP_USER_AGENT'], 'AppleWebKit') !== false,
'is_mac_ie' => strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 5.') !== false && strpos($_SERVER['HTTP_USER_AGENT'], 'Mac') !== false,
'is_web_tv' => strpos($_SERVER['HTTP_USER_AGENT'], 'WebTV') !== false,
'is_konqueror' => strpos($_SERVER['HTTP_USER_AGENT'], 'Konqueror') !== false,
'is_firefox' => preg_match('~(?:Firefox|Ice[wW]easel|IceCat)/~', $_SERVER['HTTP_USER_AGENT']) === 1,
'is_firefox1' => preg_match('~(?:Firefox|Ice[wW]easel|IceCat)/1\\.~', $_SERVER['HTTP_USER_AGENT']) === 1,
'is_firefox2' => preg_match('~(?:Firefox|Ice[wW]easel|IceCat)/2\\.~', $_SERVER['HTTP_USER_AGENT']) === 1,
'is_firefox3' => preg_match('~(?:Firefox|Ice[wW]easel|IceCat|Shiretoko|Minefield)/3\\.~', $_SERVER['HTTP_USER_AGENT']) === 1,
'is_iphone' => strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone') !== false || strpos($_SERVER['HTTP_USER_AGENT'], 'iPod') !== false,
'is_android' => strpos($_SERVER['HTTP_USER_AGENT'], 'Android') !== false,
);

$context['browser']['is_chrome'] = $context['browser']['is_webkit'] && strpos($_SERVER['HTTP_USER_AGENT'], 'Chrome') !== false;
$context['browser']['is_safari'] = !$context['browser']['is_chrome'] && strpos($_SERVER['HTTP_USER_AGENT'], 'Safari') !== false;
$context['browser']['is_gecko'] = strpos($_SERVER['HTTP_USER_AGENT'], 'Gecko') !== false && !$context['browser']['is_webkit'] && !$context['browser']['is_konqueror'];

// Internet Explorer 5 and 6 are often "emulated".
$context['browser']['is_ie8'] = !$context['browser']['is_opera'] && !$context['browser']['is_gecko'] && !$context['browser']['is_web_tv'] && strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 8') !== false;
$context['browser']['is_ie7'] = !$context['browser']['is_opera'] && !$context['browser']['is_gecko'] && !$context['browser']['is_web_tv'] && strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 7') !== false && !$context['browser']['is_ie8'];
$context['browser']['is_ie6'] = !$context['browser']['is_opera'] && !$context['browser']['is_gecko'] && !$context['browser']['is_web_tv'] && strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6') !== false && !$context['browser']['is_ie8'] && !$context['browser']['is_ie7'];
$context['browser']['is_ie5.5'] = !$context['browser']['is_opera'] && !$context['browser']['is_gecko'] && !$context['browser']['is_web_tv'] && strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 5.5') !== false;
$context['browser']['is_ie5'] = !$context['browser']['is_opera'] && !$context['browser']['is_gecko'] && !$context['browser']['is_web_tv'] && strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 5.0') !== false;

$context['browser']['is_ie'] = !$context['browser']['is_opera'] && !$context['browser']['is_gecko'] && !$context['browser']['is_web_tv'] && preg_match('~MSIE \d~', $_SERVER['HTTP_USER_AGENT']) == 1;
// Before IE8 we need to fix IE... lots!
$context['browser']['ie_standards_fix'] = $context['browser']['is_ie4'] || $context['browser']['is_ie5'] || $context['browser']['is_ie5.5'] || $context['browser']['is_ie6'] || $context['browser']['is_ie7'];

$context['browser']['needs_size_fix'] = ($context['browser']['is_ie5'] || $context['browser']['is_ie5.5'] || $context['browser']['is_ie4'] || $context['browser']['is_opera6']) && strpos($_SERVER['HTTP_USER_AGENT'], 'Mac') === false;

While your mod requires very much different strings, I can't remember if I had installed a mod there or not...
Quote from: BeastMode topic=525177.msg3720020#msg3720020
It's so powerful that on this post and even in the two PMs you sent me,you still answered my question very quickly and you're apologizing for the delay. You're the #1 support I've probably ever encountered man, so much respect for that. Thank you, and get better soon.

I'll keep this in my siggy for a while just to remind me that someone appreciated what I did while others didn't.

♥ Jess ♥

STOP EDITING MY PROFILE

Antechinus

Yeah looks like it has been changed by something already. The same code straight from vanilla 2.0.8 files is this:

// The following determines the user agent (browser) as best it can.
$context['browser'] = array(
'is_opera' => strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') !== false,
'is_opera6' => strpos($_SERVER['HTTP_USER_AGENT'], 'Opera 6') !== false,
'is_opera7' => strpos($_SERVER['HTTP_USER_AGENT'], 'Opera 7') !== false || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera/7') !== false,
'is_opera8' => strpos($_SERVER['HTTP_USER_AGENT'], 'Opera 8') !== false || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera/8') !== false,
'is_opera9' => preg_match('~Opera[ /]9(?!\\.[89])~', $_SERVER['HTTP_USER_AGENT']) === 1,
'is_opera10' => preg_match('~Opera[ /]10\\.~', $_SERVER['HTTP_USER_AGENT']) === 1 || (preg_match('~Opera[ /]9\\.[89]~', $_SERVER['HTTP_USER_AGENT']) === 1 && preg_match('~Version/1[0-9]\\.~', $_SERVER['HTTP_USER_AGENT']) === 1),
'is_ie4' => strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 4') !== false && strpos($_SERVER['HTTP_USER_AGENT'], 'WebTV') === false,
'is_webkit' => strpos($_SERVER['HTTP_USER_AGENT'], 'AppleWebKit') !== false,
'is_mac_ie' => strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 5.') !== false && strpos($_SERVER['HTTP_USER_AGENT'], 'Mac') !== false,
'is_web_tv' => strpos($_SERVER['HTTP_USER_AGENT'], 'WebTV') !== false,
'is_konqueror' => strpos($_SERVER['HTTP_USER_AGENT'], 'Konqueror') !== false,
'is_firefox' => preg_match('~(?:Firefox|Ice[wW]easel|IceCat)/~', $_SERVER['HTTP_USER_AGENT']) === 1,
'is_firefox1' => preg_match('~(?:Firefox|Ice[wW]easel|IceCat)/1\\.~', $_SERVER['HTTP_USER_AGENT']) === 1,
'is_firefox2' => preg_match('~(?:Firefox|Ice[wW]easel|IceCat)/2\\.~', $_SERVER['HTTP_USER_AGENT']) === 1,
'is_firefox3' => preg_match('~(?:Firefox|Ice[wW]easel|IceCat|Shiretoko|Minefield)/3\\.~', $_SERVER['HTTP_USER_AGENT']) === 1,
'is_iphone' => strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone') !== false || strpos($_SERVER['HTTP_USER_AGENT'], 'iPod') !== false,
'is_android' => strpos($_SERVER['HTTP_USER_AGENT'], 'Android') !== false,
);

$context['browser']['is_chrome'] = $context['browser']['is_webkit'] && strpos($_SERVER['HTTP_USER_AGENT'], 'Chrome') !== false;
$context['browser']['is_safari'] = !$context['browser']['is_chrome'] && strpos($_SERVER['HTTP_USER_AGENT'], 'Safari') !== false;
$context['browser']['is_gecko'] = strpos($_SERVER['HTTP_USER_AGENT'], 'Gecko') !== false && !$context['browser']['is_webkit'] && !$context['browser']['is_konqueror'];

// Internet Explorer 5 and 6 are often "emulated".
$context['browser']['is_ie8'] = !$context['browser']['is_opera'] && !$context['browser']['is_gecko'] && !$context['browser']['is_web_tv'] && strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 8') !== false;
$context['browser']['is_ie7'] = !$context['browser']['is_opera'] && !$context['browser']['is_gecko'] && !$context['browser']['is_web_tv'] && strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 7') !== false && !$context['browser']['is_ie8'];
$context['browser']['is_ie6'] = !$context['browser']['is_opera'] && !$context['browser']['is_gecko'] && !$context['browser']['is_web_tv'] && strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6') !== false && !$context['browser']['is_ie8'] && !$context['browser']['is_ie7'];
$context['browser']['is_ie5.5'] = !$context['browser']['is_opera'] && !$context['browser']['is_gecko'] && !$context['browser']['is_web_tv'] && strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 5.5') !== false;
$context['browser']['is_ie5'] = !$context['browser']['is_opera'] && !$context['browser']['is_gecko'] && !$context['browser']['is_web_tv'] && strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 5.0') !== false;

$context['browser']['is_ie'] = $context['browser']['is_ie4'] || $context['browser']['is_ie5'] || $context['browser']['is_ie5.5'] || $context['browser']['is_ie6'] || $context['browser']['is_ie7'] || $context['browser']['is_ie8'];
// Before IE8 we need to fix IE... lots!
$context['browser']['ie_standards_fix'] = !$context['browser']['is_ie8'];

$context['browser']['needs_size_fix'] = ($context['browser']['is_ie5'] || $context['browser']['is_ie5.5'] || $context['browser']['is_ie4'] || $context['browser']['is_opera6']) && strpos($_SERVER['HTTP_USER_AGENT'], 'Mac') === false;


Your arrays have changed these lines:

$context['browser']['is_ie'] = $context['browser']['is_ie4'] || $context['browser']['is_ie5'] || $context['browser']['is_ie5.5'] || $context['browser']['is_ie6'] || $context['browser']['is_ie7'] || $context['browser']['is_ie8'];
// Before IE8 we need to fix IE... lots!
$context['browser']['ie_standards_fix'] = !$context['browser']['is_ie8'];

Antechinus

Hey that's weird. That last line is wrong, because it will treat every non-IE8 browser as if it was an old version of IE, unless it's combined with a second 'is_ie' conditional. That doesn't make sense. WTF is that doing in the default array?

The version in your array makes more sense, because it will specifically target old IE without requiring an extra conditional. Anyway I changed it to something that does make sense in the mod, since nobody cares about IE<6 any more, so at worst the standards fix only has to go back to IE6.

Ninja ZX-10RR

Lol ok too much stuff to look at for me at 4:13 a.m. I'll get back to you later or in the next days if I won't be able to do it later ;)
Quote from: BeastMode topic=525177.msg3720020#msg3720020
It's so powerful that on this post and even in the two PMs you sent me,you still answered my question very quickly and you're apologizing for the delay. You're the #1 support I've probably ever encountered man, so much respect for that. Thank you, and get better soon.

I'll keep this in my siggy for a while just to remind me that someone appreciated what I did while others didn't.

♥ Jess ♥

STOP EDITING MY PROFILE

Advertisement: