3namespace PlanetTeamSpeak\TeamSpeak3Framework\Helper;
5use PlanetTeamSpeak\TeamSpeak3Framework\Exception\HelperException;
91 $this->regex[
"alphanum"] =
"[^\W_]";
92 $this->regex[
"escaped"] =
"(?:%[\da-fA-F]{2})";
93 $this->regex[
"mark"] =
"[-_.!~*'()\[\]]";
94 $this->regex[
"reserved"] =
"[;\/?:@&=+$,]";
95 $this->regex[
"unreserved"] =
"(?:" . $this->regex[
"alphanum"] .
"|" . $this->regex[
"mark"] .
")";
96 $this->regex[
"segment"] =
"(?:(?:" . $this->regex[
"unreserved"] .
"|" . $this->regex[
"escaped"] .
"|[:@&=+$,;])*)";
97 $this->regex[
"path"] =
"(?:\/" . $this->regex[
"segment"] .
"?)+";
98 $this->regex[
"uric"] =
"(?:" . $this->regex[
"reserved"] .
"|" . $this->regex[
"unreserved"] .
"|" . $this->regex[
"escaped"] .
")";
114 protected function parseUri(
string $uriString =
''): void
116 $components = parse_url($uriString, -1);
118 if ($components ===
false) {
124 if (empty(trim($this->scheme)) or !ctype_alnum($this->scheme->toString())) {
125 throw new HelperException(
"invalid URI scheme '" . $this->scheme .
"' supplied");
136 $this->fragment =
StringHelper::factory((isset($components[
"fragment"])) ? $components[
"fragment"] :
"");
138 if (str_contains($this->fragment,
"?")) {
139 throw new HelperException(
"invalid URI fragment '" . $this->fragment .
"' supplied (fragment must be after query)");
164 $uri =
new self(strval($uri));
169 return $uri->isValid();
179 return (
bool)strlen($this->scheme);
200 public function checkUser(
string $username =
null): bool
202 if ($username ===
null) {
203 $username = $this->user->toString();
206 if (strlen($username) == 0) {
210 $pattern =
"/^(" . $this->regex[
"alphanum"] .
"|" . $this->regex[
"mark"] .
"|" . $this->regex[
"escaped"] .
"|" .
"[;:&=+$,])+$/";
211 $status = @preg_match($pattern, $username);
213 if ($status ===
false) {
217 return ($status == 1);
227 return (
bool)strlen($this->user);
250 if ($password ===
null) {
251 $password = $this->pass->toString();
254 if (strlen($password) == 0) {
258 $pattern =
"/^(" . $this->regex[
"alphanum"] .
"|" . $this->regex[
"mark"] .
"|" . $this->regex[
"escaped"] .
"|" .
"[;:&=+$,])+$/";
259 $status = @preg_match($pattern, $password);
261 if ($status ===
false) {
265 return ($status == 1);
275 return (
bool)strlen($this->pass);
297 if (
$host ===
null) {
302 case filter_var(
$host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6):
305 case !preg_match(
"/^(([0-9]{1,3})\.){3}([0-9]{1,3})$/",
$host) and preg_match(
"/^(([a-z0-9]|[a-z0-9][a-z0-9\-]*[a-z0-9])\.)*([a-z0-9]|[a-z0-9][a-z0-9\-]*[a-z0-9])$/i",
$host):
326 return (
bool)strlen($this->host);
348 if (
$port ===
null) {
350 $port = intval($this->port->toString());
352 $port = intval($this->port);
357 case str_starts_with(
$port,
'-'):
360 case !filter_var(
$port, FILTER_VALIDATE_INT, [
'options' => [
'min_range' => 1,
'max_range' => 65535]]):
374 return (
bool)strlen($this->port->toString());
383 public function getPort(mixed $default =
null): int
385 return ($this->
hasPort()) ? intval($this->port->toString()) : $default;
397 if (
$path ===
null) {
398 $path = $this->path->toString();
401 if (strlen(
$path) == 0) {
405 $pattern =
"/^" . $this->regex[
"path"] .
"$/";
406 $status = @preg_match($pattern,
$path);
408 if ($status ===
false) {
412 return ($status == 1);
422 return (
bool)strlen($this->path);
449 if (strlen(
$query) == 0) {
453 $pattern =
"/^" . $this->regex[
"uric"] .
"*$/";
454 $status = @preg_match($pattern,
$query);
456 if ($status ===
false) {
460 return ($status == 1);
470 return (
bool)strlen($this->query);
479 public function getQuery(array $default = []): array
485 parse_str(rawurldecode($this->query), $queryArray);
502 parse_str($this->query, $queryArray);
504 return array_key_exists($key, $queryArray);
514 public function getQueryVar(
string $key, mixed $default =
null): mixed
520 parse_str(rawurldecode($this->query), $queryArray);
522 if (array_key_exists($key, $queryArray)) {
523 $val = $queryArray[$key];
525 if (ctype_digit($val)) {
527 } elseif (is_string($val)) {
554 $pattern =
"/^" . $this->regex[
"uric"] .
"*$/";
555 $status = @preg_match($pattern,
$fragment);
557 if ($status ===
false) {
561 return ($status == 1);
571 return (
bool)strlen($this->fragment);
592 public static function getUserParam(
string $key, mixed $default =
null): mixed
604 public static function getHostParam(
string $key, mixed $default =
null): mixed
606 return (array_key_exists($key, $_SERVER) && !empty($_SERVER[$key])) ? $_SERVER[$key] : $default;
616 public static function getSessParam(
string $key, mixed $default =
null): mixed
618 return (array_key_exists($key, $_SESSION) && !empty($_SESSION[$key])) ? $_SESSION[$key] : $default;
630 if (!preg_match(
"/^([a-z0-9][a-z0-9-]{0,62}\.)*([a-z0-9][a-z0-9-]{0,62}\.)+([a-z]{2,6})$/i", $hostname, $matches)) {
634 $parts[
"tld"] = $matches[3];
635 $parts[
"2nd"] = $matches[2];
636 $parts[
"3rd"] = $matches[1];
650 $serverName =
new StringHelper(self::getHostParam(
"HTTP_HOST"));
652 $serverPort = ($serverPort != 80 && $serverPort != 443) ?
":" . $serverPort :
"";
654 if ($serverName->endsWith($serverPort)) {
655 $serverName = $serverName->replace($serverPort,
"");
658 return new StringHelper($sheme .
"://" . $serverName . $serverPort);
668 $scriptPath =
new StringHelper(dirname(self::getHostParam(
"SCRIPT_NAME")));
670 return self::getHostUri()->append(($scriptPath == DIRECTORY_SEPARATOR ?
"" : $scriptPath) .
"/");
681 if (!is_array($var)) {
682 return stripslashes(strval($var));
685 foreach ($var as $key => $val) {
686 $var[$key] = (is_array($val)) ? self::stripslashesRecursive($val) : stripslashes(strval($val));
Enhanced exception class for PlanetTeamSpeak\TeamSpeak3Framework\Helper* objects.
Helper class for string handling.
static factory(string $string)
Helper class for URI handling.
getUser(mixed $default=null)
checkPath(string $path=null)
static stripslashesRecursive(mixed $var)
static getFQDNParts(string $hostname)
static getSessParam(string $key, mixed $default=null)
getPort(mixed $default=null)
checkUser(string $username=null)
checkFragment(string $fragment=null)
static getHostParam(string $key, mixed $default=null)
static getUserParam(string $key, mixed $default=null)
getScheme(mixed $default=null)
getFragment(mixed $default=null)
checkQuery(string $query=null)
getPath(string $default="/")
getPass(mixed $default=null)
parseUri(string $uriString='')
checkPort(int $port=null)
checkPass(StringHelper|string $password=null)
getQuery(array $default=[])
getQueryVar(string $key, mixed $default=null)
getHost(mixed $default=null)
static check(StringHelper $uri)
checkHost(string $host=null)
StringHelper string null $scheme