* add basic auto-detection of mobile devices (disabled yet)

This commit is contained in:
Max Pozdeev 2020-09-12 16:17:43 +03:00
parent 3caf17be9d
commit dbfa448576
5 changed files with 64 additions and 11 deletions

View file

@ -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;
?>

View file

@ -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(

View file

@ -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;
}

View file

@ -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');

View file

@ -278,12 +278,12 @@ $().ready(function(){
<div id="footer">
<div id="footer_content">
<span>Powered by <strong><a href="http://www.mytinytodo.net/">myTinyTodo</a></strong> <?php mttinfo('version'); ?></span>
<span id="mobileordesktop">
<?php if(Config::get('mobile')): ?><a href="<?php mttinfo('url'); ?>">Desktop</a>
<?php if(Config::get('mobile')): ?><a href="<?php echo getDesktopUrl(); ?>">Desktop</a>
<?php else: ?><a href="<?php mttinfo('mobile_url'); ?>">Mobile</a>
<?php endif; ?>
</span>
<span>Powered by <strong><a href="http://www.mytinytodo.net/">myTinyTodo</a></strong> <?php mttinfo('version'); ?></span>
</div>
</div>