Skip to content

Commit

Permalink
[en] Bug fix: Cannot post " character (sourceforge bug 1657437) [2h15]
Browse files Browse the repository at this point in the history
[fr] Bug fix : On ne pouvait pas poster le caractère " (sourceforge bug 1657437) [2h15]


git-svn-id: https://phpfreechat.svn.sourceforge.net/svnroot/phpfreechat/trunk@976 2772adf2-ac07-0410-9d30-e29d8120292e
  • Loading branch information
kerphi committed Feb 20, 2007
1 parent ecce7a2 commit 477fe58
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 25 deletions.
71 changes: 66 additions & 5 deletions src/pfccommand.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,19 +253,33 @@ function trace(&$xml_reponse, $msg, $data = NULL)

}

function ParseCommand($cmd_str)
function ParseCommand($cmd_str, $one_parameter = false)
{
$pattern_quote = '/([^\\\]|^)"([^"]+[^\\\])"/';
$pattern_quote = '/"([^"]+)"/';
$pattern_noquote = '/([^"\s]+)/';
$pattern_command = '/^\/([a-z0-9]+)\s*(.*)/';
$pattern_command = '/^\/([a-z0-9]+)\s*([a-z0-9]+)\s*([a-z0-9]+)\s*(.*)/';
$result = array();

// parse the command name (ex: '/invite')
if (preg_match($pattern_command, $cmd_str, $res))
{
$cmd = $res[1];
$params_str = $res[2];
$cmd = $res[1];
$clientid = $res[2];
$recipientid = $res[3];
$params_str = $res[4];

// don't parse multiple parameters for special commands with only one parameter
// this make possible to send double quotes (") in these commands
if ($one_parameter || $cmd == 'send' || $cmd == 'notice' || $cmd == 'me')
{
$result['cmdstr'] = $cmd_str;
$result['cmdname'] = $cmd;
$result['params'] = array($clientid, $recipientid, $params_str);
return $result;
}


// parse the quotted parameters (ex: '/invite "nickname with spaces"')
preg_match_all($pattern_quote,$params_str,$res1,PREG_OFFSET_CAPTURE);
$params_res = $res1[1];
Expand All @@ -288,7 +302,54 @@ function ParseCommand($cmd_str)
ksort($params);
$params = array_values($params);
$params = array_map("trim",$params);
$params = array_merge(array($clientid,$recipientid), $params);


// THIS IS ANOTHER WAY TO PARSE THE PARAMETERS
// IT'S NOT SIMPLIER BUT MAYBE FASTER
// @todo : take the faster methode
/*
$params = array($clientid, $recipientid);
$sep = preg_match('/[^\\\\]"/',$params_str) ? '"' : ' ';
if ($sep == ' ') $params_str = ' ' . $params_str;
$offset = 0;
while (1)
{
$i1 = strpos($params_str,$sep,$offset);
// capture the parameter value
if ($i1 !== FALSE)
{
// remove multi-separators
while (1)
{
if (strpos($params_str,$sep,$i1+1) - $i1 == 1)
$i1++;
else
break;
}
// search the parameter terminason
$offset = $i1+1;
$i2 = strpos($params_str,$sep,$offset);
if ($i2 !== FALSE)
{
$offset = $i2 + ($sep == '"' ? 1 : 0);
$p = substr($params_str, $i1+1, $i2-$i1-1);
if (!preg_match('/^\s*$/',$p))
$params[] = $p;
}
else
break;
}
else
break;
}
// append the tail
if ($offset < strlen($params_str))
$params[] = substr($params_str,$offset);
*/


$result['cmdstr'] = $cmd_str;
$result['cmdname'] = $cmd;
$result['params'] = $params;
Expand Down
51 changes: 31 additions & 20 deletions testcase/parsecommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,47 @@
require_once dirname(__FILE__).'/../src/pfccommand.class.php';

$results = array();
$results[] = array('cmdstr' => '/cmdname',

$results[] = array('cmdstr' => '/cmdname clientid recipientid',
'cmdname' => 'cmdname',
'params' => array('clientid', 'recipientid'));
$results[] = array('cmdstr' => '/cmdname clientid recipientid param1 param2',
'cmdname' => 'cmdname',
'params' => array());
$results[] = array('cmdstr' => '/cmdname "param1" "param2"',
'params' => array('clientid', 'recipientid', 'param1','param2'));
$results[] = array('cmdstr' => '/cmdname clientid recipientid param1 param2 param3',
'cmdname' => 'cmdname',
'params' => array('param1','param2'));
$results[] = array('cmdstr' => '/cmdname "param1" "param2" "param3"',
'params' => array('clientid', 'recipientid', 'param1','param2','param3'));
$results[] = array('cmdstr' => '/cmdname clientid recipientid "param1" "param2"',
'cmdname' => 'cmdname',
'params' => array('param1','param2','param3'));
$results[] = array('cmdstr' => '/cmdname "param1 with spaces" "param2 with spaces"',
'params' => array('clientid', 'recipientid', 'param1','param2'));
$results[] = array('cmdstr' => '/cmdname clientid recipientid "param1" "param2" "param3"',
'cmdname' => 'cmdname',
'params' => array('param1 with spaces','param2 with spaces'));
$results[] = array('cmdstr' => '/cmdname000 "param1" "param2"',
'params' => array('clientid', 'recipientid', 'param1','param2','param3'));
$results[] = array('cmdstr' => '/cmdname clientid recipientid "param1 with spaces" "param2 with spaces"',
'cmdname' => 'cmdname',
'params' => array('clientid', 'recipientid', 'param1 with spaces','param2 with spaces'));
$results[] = array('cmdstr' => '/cmdname000 clientid recipientid "param1" "param2"',
'cmdname' => 'cmdname000',
'params' => array('param1','param2'));
$results[] = array('cmdstr' => '/cmdname param1 param2',
'params' => array('clientid', 'recipientid', 'param1','param2'));
$results[] = array('cmdstr' => '/cmdname clientid recipientid param1 param2',
'cmdname' => 'cmdname',
'params' => array('param1','param2'));
$results[] = array('cmdstr' => '/cmdname "param1 with spaces" param2 param3',
'params' => array('clientid', 'recipientid', 'param1','param2'));
$results[] = array('cmdstr' => '/cmdname clientid recipientid "param1 with spaces" param2 param3',
'cmdname' => 'cmdname',
'params' => array('param1 with spaces','param2','param3'));
$results[] = array('cmdstr' => '/cmdname "param1" param2 "param3 with spaces" param4',
'params' => array('clientid', 'recipientid', 'param1 with spaces','param2', 'param3'));
$results[] = array('cmdstr' => '/cmdname clientid recipientid "param1" param2 "param3 with spaces" param4',
'cmdname' => 'cmdname',
'params' => array('param1', 'param2', 'param3 with spaces', 'param4'));
$results[] = array('cmdstr' => '/cmdname "param1""param2"',
'params' => array('clientid', 'recipientid', 'param1', 'param2', 'param3 with spaces', 'param4'));
$results[] = array('cmdstr' => '/cmdname clientid recipientid "param1""param2"',
'cmdname' => 'cmdname',
'params' => array('param1', 'param2'));
$results[] = array('cmdstr' => '/cmdname "param1withoutspace"',
'params' => array('clientid', 'recipientid', 'param1', 'param2'));
$results[] = array('cmdstr' => '/cmdname clientid recipientid "param1withoutspace"',
'cmdname' => 'cmdname',
'params' => array('param1withoutspace'));
'params' => array('clientid', 'recipientid', 'param1withoutspace'));
$results[] = array('cmdstr' => '/send clientid recipientid my sentance " with double " quotes',
'cmdname' => 'send',
'params' => array('clientid', 'recipientid', 'my sentance " with double " quotes'));

echo '<pre>';
for($i = 0; $i<count($results); $i++)
{
Expand Down

0 comments on commit 477fe58

Please sign in to comment.