From 1c548ebe50f04aa54d37336f12509de0e8c481ac Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Sat, 23 Aug 2008 17:24:54 +0200 Subject: mobile detection added This patch adds detection of mobile browsers to DokuWiki. $INFO[ismobile] will be set to true for mobile browsers. Template authos can use this to adapt their template dynamically. CSS support will follow. darcs-hash:20080823152454-7ad00-67e50139572a746d2b356bdbb2a53a3f21b53bcb.gz --- inc/common.php | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'inc/common.php') diff --git a/inc/common.php b/inc/common.php index 9f52fea1b..5dfc49856 100644 --- a/inc/common.php +++ b/inc/common.php @@ -208,6 +208,9 @@ function pageinfo(){ } } + // mobile detection + $info['ismobile'] = clientismobile(); + return $info; } @@ -596,6 +599,29 @@ function clientIP($single=false){ return $ip[0]; } +/** + * Check if the browser is on a mobile device + * + * Adapted from the example code at url below + * + * @link http://www.brainhandles.com/2007/10/15/detecting-mobile-browsers/#code + */ +function clientismobile(){ + + if(isset($_SERVER['HTTP_X_WAP_PROFILE'])) return true; + + if(preg_match('/wap\.|\.wap/i',$_SERVER['HTTP_ACCEPT'])) return true; + + if(!isset($_SERVER['HTTP_USER_AGENT'])) return false; + + $uamatches = 'midp|j2me|avantg|docomo|novarra|palmos|palmsource|240x320|opwv|chtml|pda|windows ce|mmp\/|blackberry|mib\/|symbian|wireless|nokia|hand|mobi|phone|cdm|up\.b|audio|SIE\-|SEC\-|samsung|HTC|mot\-|mitsu|sagem|sony|alcatel|lg|erics|vx|NEC|philips|mmm|xx|panasonic|sharp|wap|sch|rover|pocket|benq|java|pt|pg|vox|amoi|bird|compal|kg|voda|sany|kdd|dbt|sendo|sgh|gradi|jb|\d\d\di|moto'; + + if(preg_match("/$uamatches/i",$_SERVER['HTTP_USER_AGENT'])) return true; + + return false; +} + + /** * Convert one or more comma separated IPs to hostnames * @@ -1238,7 +1264,7 @@ function preg_quote_cb($string){ */ function shorten($keep,$short,$max,$min=9,$char='⌇'){ $max = $max - utf8_strlen($keep); - if($max < $min) return $keep; + if($max < $min) return $keep; $len = utf8_strlen($short); if($len <= $max) return $keep.$short; $half = floor($max/2); -- cgit v1.2.3