Changeset View
Changeset View
Standalone View
Standalone View
src/infrastructure/markup/markuprule/PhutilRemarkupRule.php
Show First 20 Lines • Show All 124 Lines • ▼ Show 20 Lines | abstract class PhutilRemarkupRule extends Phobject { | ||||
protected function isURIRelativePath($uri) { | protected function isURIRelativePath($uri) { | ||||
// NOTE: This was the original check, but at | // NOTE: This was the original check, but at | ||||
// the moment '/' is assumed as relative but | // the moment '/' is assumed as relative but | ||||
// './' is assumed as not relative, and this | // './' is assumed as not relative, and this | ||||
// may be confusing and may deserve a future update | // may be confusing and may deserve a future update | ||||
return strncmp($uri, '/', 1) == 0; | return strncmp($uri, '/', 1) == 0; | ||||
} | } | ||||
/** | |||||
* Check if a generic URI points to an internal destination | |||||
* | |||||
* The URI can be relative or absolute | |||||
* | |||||
* @param string $uri Example '/bar/' or 'http://example.com/bar/' | |||||
* @return bool | |||||
*/ | |||||
protected function isInternalGenericURI($uri) { | |||||
return $this->isURIAnchor($uri) | |||||
|| $this->isURIRelativePath($uri) | |||||
|| $this->isInternalAbsoluteURI($uri); | |||||
} | |||||
/** | |||||
* Check if an absolute URI points to Phorge/Phabricator itself | |||||
* | |||||
* @param string $uri Example 'http://example.com/bar/' | |||||
* @return bool | |||||
*/ | |||||
protected function isInternalAbsoluteURI($uri) { | |||||
// Get the Base URI - full URI with protocol and ending with '/' | |||||
// It's difficult to imagine a case where this config is not defined, | |||||
// but better to be nice here | |||||
$base_uri = | |||||
PhabricatorEnv::getEnvConfigIfExists('phabricator.base-uri', null); | |||||
// Assume a safe default | |||||
if (!$base_uri) { | |||||
return false; | |||||
} | |||||
return $this->isURIStartingWith($uri, $base_uri); | |||||
} | |||||
/** | |||||
* Check whenever an URI starts with a specific prefix | |||||
* | |||||
* @param $uri string Example 'http://example.com/bar/' | |||||
* @param $prefix string Example 'http://example.com/' | |||||
* @return bool True if the first is contained in the second | |||||
*/ | |||||
private function isURIStartingWith($uri, $prefix) { | |||||
// I know that PHP 8 has 'str_starts_with()' | |||||
// but that is not the minimum version | |||||
// https://stackoverflow.com/a/2790919 | |||||
return substr($uri, 0, strlen($prefix)) === $prefix; | |||||
} | |||||
} | } |
Content licensed under Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) unless otherwise noted; code licensed under Apache 2.0 or other open source licenses. · CC BY-SA 4.0 · Apache 2.0