Skip to content

Commit

Permalink
added function to encode username for URL (commented regex) and funct…
Browse files Browse the repository at this point in the history
…ion to convert chars not allowed in IRC nicknames before

encoding it, updates #209
  • Loading branch information
bohrsty committed Jun 4, 2013
1 parent d7bbc8e commit 4412fd2
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 3 deletions.
2 changes: 1 addition & 1 deletion htdocs/lib2/logic/const.inc.php
Expand Up @@ -23,7 +23,7 @@
define('RE_INSERT_AUTOUUID', 8); // if empty, UUID is generated by before insert trigger (not supported for primary key fields)
define('RE_INSERT_NOW', 16); // NOW()

define('REGEX_USERNAME', '^[a-zA-Z0-9\.\-_@äüöÄÜÖ=)(\/\\\&*+~#][a-zA-Z0-9\.\-_ @äüöÄÜÖ=)(\/\\\&*+~#]{1,58}[a-zA-Z0-9\.\-_@äüöÄÜÖ=)(\/\\\&*+~#]$'); // min. 4 -> 3 chars -- following 2012-8-6
define('REGEX_USERNAME', '^[a-zA-Z0-9\.\-_@äüöÄÜÖ=)(\/\\\&*+~#][a-zA-Z0-9\.\-_ @äüöÄÜÖ=)(\/\\\&*+~#]{1,58}[a-zA-Z0-9\.\-_@äüöÄÜÖ=)(\/\\\&*+~#]$'); // if chars changed, ajust webchat.php // // min. 4 -> 3 chars -- following 2012-8-6
define('REGEX_PASSWORD', '^[a-zA-Z0-9\.\-_ @äüöÄÜÖ=)(\/\\\&*+~#]{3,60}$');
define('REGEX_LAST_NAME', '^[a-zA-Z][a-zA-Z0-9\.\- äüöÄÜÖ]{1,59}$');
define('REGEX_FIRST_NAME', '^[a-zA-Z][a-zA-Z0-9\.\- äüöÄÜÖ]{1,59}$');
Expand Down
103 changes: 101 additions & 2 deletions htdocs/webchat.php
Expand Up @@ -4,7 +4,7 @@
*
* Unicode Reminder メモ
*
* Display some status information about the server and Opencaching
* Displays the Chat/IRC using iframe of freenode.net escaping usernames
***************************************************************************/

require('./lib2/web.inc.php');
Expand All @@ -17,9 +17,19 @@
$tpl->cache_id = $sUserCountry;

// check loggedin and set username for chat
/*
* OC allows alphanumeric chars in username and:
* ". - _ @ ä ü ö Ä Ü Ö = ) ( / \ & * + ~ #"
*
* IRC allows alphanumeric chars in nick and:
* "_ - \ [ ] { } ^ ` |"
*
* so we have to convert the folloing chars before urlencoding it:
* ". @ ä ü ö Ä Ü Ö = ) ( / & * + ~ #"
*/
$chatusername = $translate->t('Guest', '', basename(__FILE__), __LINE__) . rand(100,999);
if ($login->userid != 0)
$chatusername = urlencode($login->username);
$chatusername = urlEncodeString(ircConvertString($login->username));

// prepare iframe-URL
$chatiframeurl = str_replace('{chatusername}',$chatusername,$opt['chat']['url']);
Expand All @@ -30,4 +40,93 @@
$tpl->assign('chatiframeheight',$opt['chat']['height']);

$tpl->display();

/*
* functions
*/
function urlEncodeString($string)
{
// set arrays with chars/encodings allowed in username see const REGEX_USERNAME
// ". - _ @ ä ü ö Ä Ü Ö = ) ( / \ & * + ~ #" (ajust if regex is changed)
$k[] = '.'; $v[] = '%2E';
$k[] = '-'; $v[] = '%2D';
$k[] = '_'; $v[] = '%5F';
$k[] = '@'; $v[] = '%40';
$k[] = 'ä'; $v[] = '%E4';
$k[] = 'ü'; $v[] = '%FC';
$k[] = 'ö'; $v[] = '%F5';
$k[] = 'Ä'; $v[] = '%C4';
$k[] = 'Ü'; $v[] = '%DC';
$k[] = 'Ö'; $v[] = '%D6';
$k[] = '='; $v[] = '%3D';
$k[] = ')'; $v[] = '%29';
$k[] = '('; $v[] = '%28';
$k[] = '/'; $v[] = '%2F';
$k[] = '\\'; $v[] = '%5C';
$k[] = '&'; $v[] = '%26';
$k[] = '*'; $v[] = '%2A';
$k[] = '+'; $v[] = '%2B';
$k[] = '~'; $v[] = '%7E';
$k[] = '#'; $v[] = '%23';
// used in converting to IRC compatible nicks
$k[] = '}'; $v[] = '%7D';
$k[] = '{'; $v[] = '%7B';

// walk through $string and encode string
$outstring = '';
for ($i=0;$i<mb_strlen($string);$i++)
{
$char = mb_substr($string,$i,1);

// find replacement
$id = array_search($char,$k);
if ($id !== false)
$outstring .= $v[$id];
else
$outstring .= $char;
}

// return
return $outstring;
}

function ircConvertString($string)
{
// set arrays with chars/replacement allowed OC usernames and not in IRC nickname
// ". @ ä ü ö Ä Ü Ö = ) ( / & * + ~ #"
$k[] = '.'; $v[] = '';
$k[] = '@'; $v[] = '{at}';
$k[] = 'ä'; $v[] = 'ae';
$k[] = 'ü'; $v[] = 'ue';
$k[] = 'ö'; $v[] = 'oe';
$k[] = 'Ä'; $v[] = 'Ae';
$k[] = 'Ü'; $v[] = 'Ue';
$k[] = 'Ö'; $v[] = 'Oe';
$k[] = '='; $v[] = '-';
$k[] = ')'; $v[] = '}';
$k[] = '('; $v[] = '{';
$k[] = '/'; $v[] = '\\';
$k[] = '&'; $v[] = '';
$k[] = '*'; $v[] = '';
$k[] = '+'; $v[] = '';
$k[] = '~'; $v[] = '-';
$k[] = '#'; $v[] = '';

// walk through $string and encode string
$outstring = '';
for ($i=0;$i<mb_strlen($string);$i++)
{
$char = mb_substr($string,$i,1);

// find replacement
$id = array_search($char,$k);
if ($id !== false)
$outstring .= $v[$id];
else
$outstring .= $char;
}

// return
return $outstring;
}
?>

0 comments on commit 4412fd2

Please sign in to comment.