Hacky support for geo URI detection

Won't work with common_purify yet because there is no geo uri scheme for it
This commit is contained in:
Mikael Nordfeldth 2016-02-03 14:19:08 +01:00
parent b1ed1f48ea
commit e903bd0bc3
2 changed files with 24 additions and 0 deletions

View File

@ -343,6 +343,11 @@ class File_redirection extends Managed_DataObject
// don't touch anything // don't touch anything
break; break;
// URLs with coordinates, not browsable domain names
case 'geo':
// don't touch anything
break;
default: default:
$out_url = $default_scheme . ltrim($out_url, '/'); $out_url = $default_scheme . ltrim($out_url, '/');
$p = parse_url($out_url); $p = parse_url($out_url);

View File

@ -884,6 +884,7 @@ function common_render_text($text)
define('_URL_SCHEME_COLON_DOUBLE_SLASH', 1); define('_URL_SCHEME_COLON_DOUBLE_SLASH', 1);
define('_URL_SCHEME_SINGLE_COLON', 2); define('_URL_SCHEME_SINGLE_COLON', 2);
define('_URL_SCHEME_NO_DOMAIN', 4); define('_URL_SCHEME_NO_DOMAIN', 4);
define('_URL_SCHEME_COLON_COORDINATES', 8);
function common_url_schemes($filter=null) function common_url_schemes($filter=null)
{ {
@ -913,6 +914,7 @@ function common_url_schemes($filter=null)
'tel' => _URL_SCHEME_SINGLE_COLON, 'tel' => _URL_SCHEME_SINGLE_COLON,
'xmpp' => _URL_SCHEME_SINGLE_COLON, 'xmpp' => _URL_SCHEME_SINGLE_COLON,
'magnet' => _URL_SCHEME_NO_DOMAIN, 'magnet' => _URL_SCHEME_NO_DOMAIN,
'geo' => _URL_SCHEME_COLON_COORDINATES,
]; ];
return array_keys( return array_keys(
@ -931,6 +933,13 @@ function common_url_schemes($filter=null)
* @param mixed $arg: optional argument will be passed on to the callback * @param mixed $arg: optional argument will be passed on to the callback
*/ */
function common_replace_urls_callback($text, $callback, $arg = null) { function common_replace_urls_callback($text, $callback, $arg = null) {
$geouri_labeltext_regex = '\pN\pL\-';
$geouri_mark_regex = '\-\_\.\!\~\*\\\'\(\)'; // the \\\' is really pretty
$geouri_unreserved_regex = '\pN\pL' . $geouri_mark_regex;
$geouri_punreserved_regex = '\[\]\:\&\+\$';
$geouri_pctencoded_regex = '(?:\%[0-9a-fA-F][0-9a-fA-F])';
$geouri_paramchar_regex = $geouri_unreserved_regex . $geouri_punreserved_regex; //FIXME: add $geouri_pctencoded_regex here so it works
// Start off with a regex // Start off with a regex
$regex = '#'. $regex = '#'.
'(?:^|[\s\<\>\(\)\[\]\{\}\\\'\\\";]+)(?![\@\!\#])'. '(?:^|[\s\<\>\(\)\[\]\{\}\\\'\\\";]+)(?![\@\!\#])'.
@ -951,6 +960,16 @@ function common_replace_urls_callback($text, $callback, $arg = null) {
')'. ')'.
')'. ')'.
')'. ')'.
'|(?:'.
'(?:' . implode('|', common_url_schemes(_URL_SCHEME_COLON_COORDINATES)) . '):'.
// There's an order that must be followed here too, if ;crs= is used, it must precede ;u=
// Also 'crsp' (;crs=$crsp) must match $geouri_labeltext_regex
// Also 'uval' (;u=$uval) must be a pnum: \-?[0-9]+
'(?:'.
'(?:[0-9]+(?:\.[0-9]+)?(?:\,[0-9]+(?:\.[0-9]+)?){1,2})'. // 1(.23)?(,4(.56)){1,2}
'(?:\;(?:['.$geouri_labeltext_regex.']+)(?:\=['.$geouri_paramchar_regex.']+)*)*'.
')'.
')'.
// URLs without domain name, like magnet:?xt=... // URLs without domain name, like magnet:?xt=...
'|(?:(?:' . implode('|', common_url_schemes(_URL_SCHEME_NO_DOMAIN)) . '):)'. '|(?:(?:' . implode('|', common_url_schemes(_URL_SCHEME_NO_DOMAIN)) . '):)'.
(common_config('linkify', 'bare_ipv4') // Convert IPv4 addresses to hyperlinks (common_config('linkify', 'bare_ipv4') // Convert IPv4 addresses to hyperlinks