/** * @covers ../../../src/Library/Helper/Url::isUrl() */ public function testIsUrl() { $this->checkNoArg('isUrl'); // simple domain name: must be ok $url = 'http://www.google.com/'; $this->assertTrue(\Library\Helper\Url::isUrl($url), sprintf('isUrl fails for "%s"!', $url)); // simple domain name with HTTPS: must be ok $url = 'https://www.google.com/'; $this->assertTrue(\Library\Helper\Url::isUrl($url), sprintf('isUrl fails for "%s"!', $url)); // simple domain name with FTP: must be ok $url = 'ftp://www.google.com/'; $this->assertTrue(\Library\Helper\Url::isUrl($url), sprintf('isUrl fails for "%s"!', $url)); // complex url: must be ok $url = 'http://www.domain.com/azerty/hui.html?qsdfqsdf'; $this->assertTrue(\Library\Helper\Url::isUrl($url), sprintf('isUrl fails for "%s"!', $url)); // complex url with a hash: must be ko $url = 'http://www.domain.com/azerty/hui.html?qsdfqsdf#uioiu'; $this->assertFalse(\Library\Helper\Url::isUrl($url), sprintf('isUrl fails for "%s"!', $url)); // simple string: must be ko $url = 'lorem ipsum ? azeerty'; $this->assertFalse(\Library\Helper\Url::isUrl($url), sprintf('isUrl fails for "%s"!', $url)); // simple string with all url characters: must be ko $url = 'http lorem :// ipsum ? azeerty ? ppp'; $this->assertFalse(\Library\Helper\Url::isUrl($url), sprintf('isUrl fails for "%s"!', $url)); // complex url with a space: must be ko $url = 'http://www.domain .com/azerty/hui.html?qsdfqsdf'; $this->assertFalse(\Library\Helper\Url::isUrl($url), sprintf('isUrl fails for "%s"!', $url)); // simple domain name with FILE: must be ko $url = 'file://www.google.com/'; $this->assertFalse(\Library\Helper\Url::isUrl($url), sprintf('isUrl fails for "%s"!', $url)); // simple domain name with FILE passing arg 'file': must be ko $url = 'file://www.google.com/'; $this->assertTrue(\Library\Helper\Url::isUrl($url, array('file')), sprintf('isUrl fails for "%s"!', $url)); }
/** * Test if a string is a classic url or an url like `//domain.ext/asset` * * @param string $str * @return bool */ public static function isUrl($str) { return (bool) (UrlHelper::isUrl($str) || UrlHelper::isUrl('http' . $str)); }
<div id="content" role="main"> <form action="" method="get"> <label>Repository to test: <input type="text" name="repo" value="<?php echo $repo; ?> " size="60" /> </label> <input type="submit" /> </form> <pre> <?php if (!empty($repo)) { if (\Library\Helper\Url::isUrl($repo)) { $tmp_dir = __DIR__ . '/tmp'; if (!file_exists($tmp_dir)) { if (!mkdir($tmp_dir)) { die(sprintf('Can not create temporary directory "%s"!', $tmp_dir)); } } if (!is_writable($tmp_dir)) { die(sprintf('Temporary directory "%s" MUST be writable!', $tmp_dir)); } $a = \GitApi\GitApi::create($tmp_dir . '/' . basename($repo), $repo); } else { $a = \GitApi\GitApi::open($repo); } echo '<br />new GitApi object : ' . $a->getRepositoryPath(); echo '<br />';
public static function checkString($str) { if (Url::isUrl($str)) { return sprintf(self::mask_a_link, $str); } elseif (Url::isEmail($str)) { return sprintf(self::mask_mailto_link, $str); } elseif (@file_exists($str)) { return sprintf(self::mask_abbr, realpath($str), $str); } elseif (false !== strpos($str, '&')) { return str_replace('&', '&', $str); } return $str; }