mirror of
https://github.com/YGGverse/net-php.git
synced 2026-04-01 17:45:38 +00:00
Compare commits
No commits in common. "main" and "1.6.0" have entirely different histories.
4 changed files with 49 additions and 171 deletions
67
README.md
67
README.md
|
|
@ -12,7 +12,7 @@ Network Library for PHP with native Yggdrasil support
|
|||
|
||||
#### Check socket is open
|
||||
|
||||
``` php
|
||||
```
|
||||
var_dump(
|
||||
\Yggverse\Net\Socket::isOpen('yo.index', 80)
|
||||
);
|
||||
|
|
@ -20,7 +20,7 @@ var_dump(
|
|||
|
||||
#### Check host valid
|
||||
|
||||
``` php
|
||||
```
|
||||
var_dump(
|
||||
\Yggverse\Net\Socket::isHost('yo.index')
|
||||
);
|
||||
|
|
@ -28,7 +28,7 @@ var_dump(
|
|||
|
||||
#### Check port valid
|
||||
|
||||
``` php
|
||||
```
|
||||
var_dump(
|
||||
\Yggverse\Net\Socket::isPort(80)
|
||||
);
|
||||
|
|
@ -38,7 +38,7 @@ var_dump(
|
|||
|
||||
#### Resolve records
|
||||
|
||||
``` php
|
||||
```
|
||||
var_dump(
|
||||
\Yggverse\Net\Dig::records('yo.index', ['A', 'AAAA'], &$result = [], &$error = [], $provider = null, $timeout = 5)
|
||||
);
|
||||
|
|
@ -46,7 +46,7 @@ var_dump(
|
|||
|
||||
#### Check hostname valid
|
||||
|
||||
``` php
|
||||
```
|
||||
var_dump(
|
||||
\Yggverse\Net\Dig::isHostName('yo.index')
|
||||
);
|
||||
|
|
@ -54,7 +54,7 @@ var_dump(
|
|||
|
||||
#### Check record valid
|
||||
|
||||
``` php
|
||||
```
|
||||
var_dump(
|
||||
\Yggverse\Net\Dig::isRecord('A')
|
||||
);
|
||||
|
|
@ -62,7 +62,7 @@ var_dump(
|
|||
|
||||
#### Check record value valid
|
||||
|
||||
``` php
|
||||
```
|
||||
var_dump(
|
||||
\Yggverse\Net\Dig::isRecordValue('A', '127.0.0.1')
|
||||
);
|
||||
|
|
@ -70,9 +70,7 @@ var_dump(
|
|||
|
||||
### Resolve
|
||||
|
||||
#### Init resolver
|
||||
|
||||
``` php
|
||||
```
|
||||
$resolve = new \Yggverse\Net\Resolve(
|
||||
[
|
||||
'A',
|
||||
|
|
@ -84,31 +82,9 @@ $resolve = new \Yggverse\Net\Resolve(
|
|||
],
|
||||
// ..
|
||||
);
|
||||
```
|
||||
|
||||
#### Get resolved URL string
|
||||
|
||||
``` php
|
||||
$resolved = $resolve->url(
|
||||
'https://en.wikipedia.org/wiki/Domain_Name_System'
|
||||
// next arguments contain debug variables and new address object
|
||||
);
|
||||
|
||||
if ($resolved)
|
||||
{
|
||||
var_dump(
|
||||
$resolved // https://185.15.59.224/wiki/Domain_Name_System
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
#### Resolve Address object
|
||||
|
||||
``` php
|
||||
$resolved = $resolve->address(
|
||||
new \Yggverse\Net\Address(
|
||||
'https://en.wikipedia.org/wiki/Domain_Name_System'
|
||||
)
|
||||
'https://en.wikipedia.org/wiki/Domain_Name_System'
|
||||
);
|
||||
|
||||
if ($resolved)
|
||||
|
|
@ -144,7 +120,7 @@ Different operations with address parts:
|
|||
|
||||
**Document root**
|
||||
|
||||
``` php
|
||||
```
|
||||
$base = new \Yggverse\Net\Address(
|
||||
'http://yo.ygg/a1/b1/c1'
|
||||
);
|
||||
|
|
@ -162,7 +138,7 @@ var_dump(
|
|||
|
||||
**Current folder**
|
||||
|
||||
``` php
|
||||
```
|
||||
$base = new \Yggverse\Net\Address(
|
||||
'http://yo.ygg/a1/b1/c1'
|
||||
);
|
||||
|
|
@ -180,7 +156,7 @@ var_dump(
|
|||
|
||||
**Ending slash**
|
||||
|
||||
``` php
|
||||
```
|
||||
$base = new \Yggverse\Net\Address(
|
||||
'http://yo.ygg/a1/b1/c1/'
|
||||
);
|
||||
|
|
@ -198,7 +174,7 @@ var_dump(
|
|||
|
||||
**All options**
|
||||
|
||||
``` php
|
||||
```
|
||||
$base = new \Yggverse\Net\Address(
|
||||
'http://user:password@yo.ygg/a1/b1/c1?attribute=value#anchor'
|
||||
);
|
||||
|
|
@ -214,20 +190,7 @@ var_dump(
|
|||
);
|
||||
```
|
||||
|
||||
### Valid
|
||||
|
||||
Network entities validation
|
||||
|
||||
**Supported methods**
|
||||
|
||||
* `Valid::ip`
|
||||
* `Valid::ip4`
|
||||
* `Valid::ip6`
|
||||
* `Valid::domainHostName`
|
||||
|
||||
## Integrations
|
||||
|
||||
* [gemini-dl](https://github.com/YGGverse/gemini-dl) - CLI batch downloader for Gemini protocol
|
||||
* [web-api](https://github.com/YGGverse/web-api) - Network API with native Yggdrasil/IPv6 support
|
||||
* [Yo!](https://github.com/YGGverse/Yo) - Crawler and search engine for different networks
|
||||
* [Yoda](https://github.com/YGGverse/Yoda) - Experimental PHP-GTK browser for Gemini protocol
|
||||
* [Network API with native Yggdrasil/IPv6 support](https://github.com/YGGverse/web-api)
|
||||
* [Yo! Crawler for different networks](https://github.com/YGGverse/Yo)
|
||||
|
|
|
|||
|
|
@ -83,9 +83,7 @@ class Address
|
|||
|
||||
public function isAbsolute(): bool
|
||||
{
|
||||
return boolval(
|
||||
$this->_scheme || $this->_host
|
||||
);
|
||||
return ($this->_scheme && $this->_host);
|
||||
}
|
||||
|
||||
public function isRelative(): bool
|
||||
|
|
@ -200,37 +198,28 @@ class Address
|
|||
$this->_separator = $value;
|
||||
}
|
||||
|
||||
public function get(
|
||||
bool $scheme = true,
|
||||
bool $user = true,
|
||||
bool $pass = true,
|
||||
bool $host = true,
|
||||
bool $port = true,
|
||||
bool $path = true,
|
||||
bool $query = true,
|
||||
bool $fragment = true,
|
||||
): string
|
||||
public function get(): string
|
||||
{
|
||||
$address = '';
|
||||
|
||||
if ($scheme && $this->getScheme())
|
||||
if ($scheme = $this->getScheme())
|
||||
{
|
||||
$address .= sprintf(
|
||||
'%s:%s%s',
|
||||
$this->getScheme(),
|
||||
$scheme,
|
||||
$this->getSeparator(),
|
||||
$this->getSeparator()
|
||||
);
|
||||
}
|
||||
|
||||
if ($user && $this->getUser())
|
||||
if ($user = $this->getUser())
|
||||
{
|
||||
if ($pass && $this->getPass())
|
||||
if ($pass = $this->getPass())
|
||||
{
|
||||
$address .= sprintf(
|
||||
'%s:%s@',
|
||||
$this->getUser(),
|
||||
$this->getPass()
|
||||
$user,
|
||||
$pass
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -238,65 +227,65 @@ class Address
|
|||
{
|
||||
$address .= sprintf(
|
||||
'%s@',
|
||||
$this->getUser()
|
||||
$user
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if ($host && $this->getHost())
|
||||
if ($host = $this->getHost())
|
||||
{
|
||||
$address .= $this->getHost();
|
||||
$address .= $host;
|
||||
}
|
||||
|
||||
if ($port && $this->getPort())
|
||||
if ($port = $this->getPort())
|
||||
{
|
||||
$address .= sprintf(
|
||||
':%d',
|
||||
$this->getPort()
|
||||
$port
|
||||
);
|
||||
}
|
||||
|
||||
if ($path && $this->getPath())
|
||||
if ($path = $this->getPath())
|
||||
{
|
||||
if (!str_starts_with($this->getPath(), $this->getSeparator()))
|
||||
if (!str_starts_with($path, $this->getSeparator()))
|
||||
{
|
||||
$address .= $this->getSeparator();
|
||||
}
|
||||
|
||||
$address .= $this->getPath();
|
||||
$address .= $path;
|
||||
}
|
||||
|
||||
if ($query && $this->getQuery())
|
||||
if ($query = $this->getQuery())
|
||||
{
|
||||
$address .= sprintf(
|
||||
'?%s',
|
||||
$this->getQuery()
|
||||
$query
|
||||
);
|
||||
}
|
||||
|
||||
if ($fragment && $this->getFragment())
|
||||
if ($fragment = $this->getFragment())
|
||||
{
|
||||
$address .= sprintf(
|
||||
'#%s',
|
||||
$this->getFragment()
|
||||
$fragment
|
||||
);
|
||||
}
|
||||
|
||||
return $address;
|
||||
}
|
||||
|
||||
public function toAbsolute(
|
||||
public function getAbsolute(
|
||||
\Yggverse\Net\Address $base
|
||||
): bool
|
||||
): ?string
|
||||
{
|
||||
if ($this->isAbsolute())
|
||||
{
|
||||
return true;
|
||||
return $this->get();
|
||||
}
|
||||
|
||||
if ($base->isRelative())
|
||||
{
|
||||
return false;
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->setScheme(
|
||||
|
|
@ -325,7 +314,7 @@ class Address
|
|||
|
||||
if (str_starts_with((string) $this->getPath(), $this->getSeparator()))
|
||||
{
|
||||
return true;
|
||||
return $this->get();
|
||||
}
|
||||
|
||||
if ($path = $this->getPath())
|
||||
|
|
@ -353,7 +342,7 @@ class Address
|
|||
{
|
||||
if (empty($prefix[$index]))
|
||||
{
|
||||
return false;
|
||||
return null;
|
||||
}
|
||||
|
||||
unset(
|
||||
|
|
@ -379,23 +368,8 @@ class Address
|
|||
)
|
||||
)
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// @TODO deprecated, legacy needs only
|
||||
public function getAbsolute(
|
||||
\Yggverse\Net\Address $base
|
||||
): ?string
|
||||
{
|
||||
if ($this->toAbsolute($base))
|
||||
{
|
||||
return $this->get();
|
||||
}
|
||||
|
||||
return null;
|
||||
return $this->get();
|
||||
}
|
||||
}
|
||||
|
|
@ -126,35 +126,16 @@ class Resolve
|
|||
return $this->_shuffle;
|
||||
}
|
||||
|
||||
public function url(
|
||||
string $url,
|
||||
array &$result = [],
|
||||
array &$error = [],
|
||||
?\Yggverse\Net\Address &$resolved = null
|
||||
): ?string
|
||||
{
|
||||
$resolved = $this->address(
|
||||
new \Yggverse\Net\Address(
|
||||
$url
|
||||
),
|
||||
$result,
|
||||
$error
|
||||
);
|
||||
|
||||
if ($resolved)
|
||||
{
|
||||
return $resolved->get();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function address(
|
||||
\Yggverse\Net\Address $address,
|
||||
string $url,
|
||||
array &$result = [],
|
||||
array &$error = []
|
||||
): ?\Yggverse\Net\Address
|
||||
{
|
||||
$address = new \Yggverse\Net\Address(
|
||||
$url
|
||||
);
|
||||
|
||||
foreach ($this->_providers as $provider)
|
||||
{
|
||||
foreach (
|
||||
|
|
@ -177,9 +158,9 @@ class Resolve
|
|||
|
||||
foreach ($data as $host)
|
||||
{
|
||||
if (\Yggverse\Net\Valid::ip6($host))
|
||||
if (false !== filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6))
|
||||
{
|
||||
$address->setHost(
|
||||
$resolved->setHost(
|
||||
sprintf(
|
||||
'[%s]',
|
||||
$host
|
||||
|
|
|
|||
|
|
@ -1,40 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yggverse\Net;
|
||||
|
||||
class Valid
|
||||
{
|
||||
public static function ip(mixed $value): bool
|
||||
{
|
||||
return self::ip4($value) || self::ip6($value);
|
||||
}
|
||||
|
||||
public static function ip4(mixed $value): bool
|
||||
{
|
||||
return false !== filter_var(
|
||||
$value,
|
||||
FILTER_VALIDATE_IP,
|
||||
FILTER_FLAG_IPV4
|
||||
);
|
||||
}
|
||||
|
||||
public static function ip6(mixed $value): bool
|
||||
{
|
||||
return false !== filter_var(
|
||||
$value,
|
||||
FILTER_VALIDATE_IP,
|
||||
FILTER_FLAG_IPV6
|
||||
);
|
||||
}
|
||||
|
||||
public static function domainHostName(mixed $value): bool
|
||||
{
|
||||
return false !== filter_var(
|
||||
$value,
|
||||
FILTER_VALIDATE_DOMAIN,
|
||||
FILTER_FLAG_HOSTNAME
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue