Add function is_local_url() to check if url is local.

This commit is contained in:
Harald Eilertsen
2022-03-20 14:35:25 +01:00
parent d35609f33a
commit b02f6a1dae
2 changed files with 41 additions and 0 deletions

View File

@@ -559,6 +559,14 @@ function z_dns_check($h,$check_mx = 0) {
return((@dns_get_record($h,$opts) || filter_var($h, FILTER_VALIDATE_IP)) ? true : false);
}
function is_local_url($url) {
if (str_starts_with($url, z_root()) || str_starts_with($url, '/')) {
return true;
}
return false;
}
/**
* @brief Validates a given URL.
*

View File

@@ -0,0 +1,33 @@
<?php
/**
* tests function from include/network.php
*
* @package test.util
*/
use PHPUnit\Framework\TestCase;
require_once('include/network.php');
class NetworkTest extends TestCase {
public function setup() : void {
\App::set_baseurl("https://mytest.org");
}
/**
* @dataProvider localUrlTestProvider
*/
public function testIsLocalURL($url, $expected) {
$this->assertEquals($expected, is_local_url($url));
}
public function localUrlTestProvider() : array {
return [
[ '/some/path', true ],
[ 'https://mytest.org/some/path', true ],
[ 'https://other.site/some/path', false ],
];
}
}