diff --git a/src/db/config.php.default b/src/db/config.php.default index 564bf55..98fc8c8 100644 --- a/src/db/config.php.default +++ b/src/db/config.php.default @@ -54,4 +54,7 @@ $config['dateformatshort'] = 'j M'; # Show task date in list $config['showdate'] = 0; +# Autodetect mobile devices and switch theme +$config['detectmobile'] = 0; + ?> \ No newline at end of file diff --git a/src/includes/common.php b/src/includes/common.php index aa0dd91..b73b375 100644 --- a/src/includes/common.php +++ b/src/includes/common.php @@ -76,6 +76,16 @@ function _get($param,$defvalue = '') } } +function _server($param, $defvalue = '') +{ + if ( !isset($_SERVER[$param]) ) { + return $defvalue; + } + else { + return $_SERVER[$param]; + } +} + class Config { public static $params = array( diff --git a/src/index.php b/src/index.php index 3d7745d..81f16cf 100644 --- a/src/index.php +++ b/src/index.php @@ -29,7 +29,16 @@ if (!is_int(Config::get('firstdayofweek')) || Config::get('firstdayofweek')<0 || Config::set('firstdayofweek', 1); } -if ( isset($_GET['mobile']) || isset($_GET['pda'])) { +if ( !isset($_GET['mobile']) && !isset($_GET['pda']) && !isset($_GET['desktop']) && Config::get('detectmobile') ) { + if (is_mobile()) { + Config::set('mobile', 1); + } +} +//TODO: if we have a desktop or mobile theme request we have to save it in cookies and redirect (if auto-detect mobiles is on) +else if ( isset($_GET['desktop']) ) { //more priority than mobile + Config::set('mobile', 0); +} +else if ( isset($_GET['mobile']) || isset($_GET['pda']) ) { Config::set('mobile', 1); } @@ -41,7 +50,7 @@ require(TEMPLATEPATH. 'index.php'); function redirectWithHashRoute(array $q, array $hash) { - $url = url_dir(isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $_SERVER['SCRIPT_NAME']); + $url = get_mttinfo('url'); $query = http_build_query($q); if ($query != '') $url .= "?$query"; if (count($hash) > 0) { @@ -50,4 +59,18 @@ function redirectWithHashRoute(array $q, array $hash) } header("Location: ". $url); exit; +} + +function is_mobile() +{ //basic detection + if (preg_match("/(iphone|ipad|android)/i", _server('HTTP_USER_AGENT'))) { + return true; + } + return false; +} + +function getDesktopUrl($escape = true) +{ + $url = (Config::get('detectmobile') && is_mobile()) ? get_mttinfo('desktop_url') : get_mttinfo('url'); + return $escape ? htmlspecialchars($url) : $url; } \ No newline at end of file diff --git a/src/init.php b/src/init.php index 729998a..041beab 100644 --- a/src/init.php +++ b/src/init.php @@ -75,7 +75,7 @@ if (need_auth() && !isset($dontStartSession)) ini_set('session.use_cookies', true); ini_set('session.use_only_cookies', true); - session_set_cookie_params(1209600, url_dir(Config::get('url')=='' ? $_SERVER['REQUEST_URI'] : Config::get('url'))); # 14 days session cookie lifetime + session_set_cookie_params(1209600, url_dir(Config::get('url')=='' ? getRequestUri() : Config::get('url'))); # 14 days session cookie lifetime session_name('mtt-session'); session_start(); } @@ -158,12 +158,11 @@ function get_mttinfo($v) $_mttinfo['includes_url'] = get_mttinfo('mtt_url'). 'includes/'; return $_mttinfo['includes_url']; case 'url': - $_mttinfo['url'] = Config::get('url'); + $_mttinfo['url'] = Config::get('url'); // need to have a trailing slash if ($_mttinfo['url'] == '') { $proto = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != '' && $_SERVER['HTTPS'] != 'off') ? 'https://' : 'http://'; - $defport = $proto == 'https://' ? 443 : 80; - $_mttinfo['url'] = $proto. $_SERVER['HTTP_HOST']. ($_SERVER['SERVER_PORT'] != $defport ? ':'.$_SERVER['SERVER_PORT'] : ''). - url_dir(isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $_SERVER['SCRIPT_NAME']); + $defport = ($proto == 'https://') ? 443 : 80; + $_mttinfo['url'] = $proto. $_SERVER['HTTP_HOST']. ($_SERVER['SERVER_PORT'] != $defport ? ':'.$_SERVER['SERVER_PORT'] : ''). url_dir(getRequestUri()); } return $_mttinfo['url']; case 'mobile_url': @@ -172,10 +171,13 @@ function get_mttinfo($v) $_mttinfo['mobile_url'] = get_mttinfo('url'). '?mobile'; } return $_mttinfo['mobile_url']; + case 'desktop_url': + $_mttinfo['desktop_url'] = get_mttinfo('url'). '?desktop'; + return $_mttinfo['desktop_url']; case 'mtt_url': - $_mttinfo['mtt_url'] = Config::get('mtt_url'); + $_mttinfo['mtt_url'] = Config::get('mtt_url'); // need to have a trailing slash if ($_mttinfo['mtt_url'] == '') { - $_mttinfo['mtt_url'] = url_dir(isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $_SERVER['SCRIPT_NAME']); + $_mttinfo['mtt_url'] = url_dir(getRequestUri()); } return $_mttinfo['mtt_url']; case 'title': @@ -190,6 +192,21 @@ function get_mttinfo($v) } } +function getRequestUri() +{ + // Do not use HTTP_X_REWRITE_URL due to CVE-2018-14773 + // SCRIPT_NAME or PATH_INFO ? + if (isset($_SERVER['REQUEST_URI'])) { + return $_SERVER['REQUEST_URI']; + } + else if (isset($_SERVER['ORIG_PATH_INFO'])) // IIS 5.0 CGI + { + $uri = $_SERVER['ORIG_PATH_INFO']; //has no query + if (!empty($_SERVER['QUERY_STRING'])) $uri .= '?'. $_SERVER['QUERY_STRING']; + return $uri; + } +} + function jsonExit($data) { header('Content-type: application/json; charset=utf-8'); diff --git a/src/themes/default/index.php b/src/themes/default/index.php index 442f836..5286958 100644 --- a/src/themes/default/index.php +++ b/src/themes/default/index.php @@ -278,12 +278,12 @@ $().ready(function(){