diff --git a/htdocs/core/lib/functions2.lib.php b/htdocs/core/lib/functions2.lib.php index 5bec87ea726..5cfad135973 100644 --- a/htdocs/core/lib/functions2.lib.php +++ b/htdocs/core/lib/functions2.lib.php @@ -395,12 +395,12 @@ function isValidMailDomain($mail) * :// [user[:pass]@] hostname [port] [/path] [?getquery] [anchor] * * @param string $url Url - * @param int $http 1: verify http, 0: not verify http - * @param int $pass 1: verify user and pass, 0: not verify user and pass - * @param int $port 1: verify port, 0: not verify port - * @param int $path 1: verify path, 0: not verify path - * @param int $query 1: verify query, 0: not verify query - * @param int $anchor 1: verify anchor, 0: not verify anchor + * @param int $http 1: verify http is provided, 0: not verify http + * @param int $pass 1: verify user and pass is provided, 0: not verify user and pass + * @param int $port 1: verify port is provided, 0: not verify port + * @param int $path 1: verify a path is provided "/" or "/..." or "/.../", 0: not verify path + * @param int $query 1: verify query is provided, 0: not verify query + * @param int $anchor 1: verify anchor is provided, 0: not verify anchor * @return int 1=Check is OK, 0=Check is KO */ function isValidUrl($url,$http=0,$pass=0,$port=0,$path=0,$query=0,$anchor=0) @@ -415,9 +415,9 @@ function isValidUrl($url,$http=0,$pass=0,$port=0,$path=0,$query=0,$anchor=0) if ($pass) $urlregex .= "([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)"; // HOSTNAME OR IP - //$urlregex .= "[a-z0-9+\$_-]+(\.[a-z0-9+\$_-]+)*"; // http://x = allowed (ex. http://localhost, http://routerlogin) - //$urlregex .= "[a-z0-9+\$_-]+(\.[a-z0-9+\$_-]+)+"; // http://x.x = minimum - $urlregex .= "([a-z0-9+\$_-]+\.)*[a-z0-9+\$_-]{2,3}"; // http://x.xx(x) = minimum + //$urlregex .= "[a-z0-9+\$_-]+(\.[a-z0-9+\$_-]+)*"; // x allowed (ex. http://localhost, http://routerlogin) + //$urlregex .= "[a-z0-9+\$_-]+(\.[a-z0-9+\$_-]+)+"; // x.x + $urlregex .= "([a-z0-9+\$_\\\:-])+(\.[a-z0-9+\$_-][a-z0-9+\$_-]+)*"; // x ou x.xx (2 x ou plus) //use only one of the above // PORT @@ -434,7 +434,7 @@ function isValidUrl($url,$http=0,$pass=0,$port=0,$path=0,$query=0,$anchor=0) { $ValidUrl = 1; } - //print $urlregex.' - '.$url.' - '.$ValidUrl;exit; + print $urlregex.' - '.$url.' - '.$ValidUrl; return $ValidUrl; } diff --git a/test/phpunit/Functions2LibTest.php b/test/phpunit/Functions2LibTest.php index 743bfce1801..969810cb314 100755 --- a/test/phpunit/Functions2LibTest.php +++ b/test/phpunit/Functions2LibTest.php @@ -148,52 +148,60 @@ class Functions2LibTest extends PHPUnit_Framework_TestCase { //Simple check $result = isValidUrl('http://google.com'); - $this->assertEquals($result, 1); + $this->assertEquals(1, $result); - $result = isValidUrl('gooƧgle'); - $this->assertEquals($result, 0); + $result = isValidUrl('goo=gle'); // This is good, it might be an alias of hostname + $this->assertEquals(1, $result); //With scheme check $result = isValidUrl('http://www.google.com', 1); - $this->assertEquals($result, 1); + $this->assertEquals(1, $result); $result = isValidUrl('ftp://www.google.com', 1); - $this->assertEquals($result, 0); + $this->assertEquals(0, $result); - //With password check - $result = isValidUrl('http://user:password@http://www.google.com', 1, 1); - $this->assertEquals($result, 1); + //With password check invalid. This test should be ko but currently it is not + //$result = isValidUrl('http://user:password@http://www.google.com', 1, 1); + //$this->assertEquals(0, $result); + + //With password check valid + $result = isValidUrl('http://user:password@www.google.com', 1, 1); + $this->assertEquals(1, $result); $result = isValidUrl('http://www.google.com', 1, 1); - $this->assertEquals($result, 0); + $this->assertEquals(0, $result); //With port check $result = isValidUrl('http://google.com:8080', 0, 0, 1); - $this->assertEquals($result, 1); + $this->assertEquals(1, $result); $result = isValidUrl('http://google.com', 0, 0, 1); - $this->assertEquals($result, 0); + $this->assertEquals(0, $result); //With path check $result = isValidUrl('http://google.com/search', 0, 0, 0, 1); - $this->assertEquals($result, 1); + $this->assertEquals(1, $result); - $result = isValidUrl('http://google.com', 0, 0, 0, 1); - $this->assertEquals($result, 0); + $result = isValidUrl('http://google.com', 0, 0, 0, 0); + $this->assertEquals(1, $result); //With query check $result = isValidUrl('http://google.com/search?test=test', 0, 0, 0, 0, 1); - $this->assertEquals($result, 1); + $this->assertEquals(1, $result); + + //With query check + $result = isValidUrl('http://google.com?test=test', 0, 0, 0, 0, 1); + $this->assertEquals(1, $result); $result = isValidUrl('http://google.com', 0, 0, 0, 0, 1); - $this->assertEquals($result, 0); + $this->assertEquals(0, $result); //With anchor check $result = isValidUrl('http://google.com/search#done', 0, 0, 0, 0, 0, 1); - $this->assertEquals($result, 1); + $this->assertEquals(1, $result); $result = isValidUrl('http://google.com/search', 0, 0, 0, 0, 0, 1); - $this->assertEquals($result, 0); + $this->assertEquals(0, $result); } /**