| 1 |
<?php |
| 2 |
|
| 3 |
namespace Safe; |
| 4 |
|
| 5 |
use Safe\Exceptions\NetworkException; |
| 6 |
|
| 7 |
/** |
| 8 |
* closelog closes the descriptor being used to write to |
| 9 |
* the system logger. The use of closelog is optional. |
| 10 |
* |
| 11 |
* @throws NetworkException |
| 12 |
* |
| 13 |
*/ |
| 14 |
function closelog(): void |
| 15 |
{ |
| 16 |
error_clear_last(); |
| 17 |
$result = \closelog(); |
| 18 |
if ($result === false) { |
| 19 |
throw NetworkException::createFromPhpError(); |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
/** |
| 25 |
* Fetch DNS Resource Records associated with the given |
| 26 |
* hostname. |
| 27 |
* |
| 28 |
* @param string $hostname hostname should be a valid DNS hostname such |
| 29 |
* as "www.example.com". Reverse lookups can be generated |
| 30 |
* using in-addr.arpa notation, but |
| 31 |
* gethostbyaddr is more suitable for |
| 32 |
* the majority of reverse lookups. |
| 33 |
* |
| 34 |
* Per DNS standards, email addresses are given in user.host format (for |
| 35 |
* example: hostmaster.example.com as opposed to hostmaster@example.com), |
| 36 |
* be sure to check this value and modify if necessary before using it |
| 37 |
* with a functions such as mail. |
| 38 |
* @param int $type By default, dns_get_record will search for any |
| 39 |
* resource records associated with hostname. |
| 40 |
* To limit the query, specify the optional type |
| 41 |
* parameter. May be any one of the following: |
| 42 |
* DNS_A, DNS_CNAME, |
| 43 |
* DNS_HINFO, DNS_CAA, |
| 44 |
* DNS_MX, DNS_NS, |
| 45 |
* DNS_PTR, DNS_SOA, |
| 46 |
* DNS_TXT, DNS_AAAA, |
| 47 |
* DNS_SRV, DNS_NAPTR, |
| 48 |
* DNS_A6, DNS_ALL |
| 49 |
* or DNS_ANY. |
| 50 |
* |
| 51 |
* Because of eccentricities in the performance of libresolv |
| 52 |
* between platforms, DNS_ANY will not |
| 53 |
* always return every record, the slower DNS_ALL |
| 54 |
* will collect all records more reliably. |
| 55 |
* |
| 56 |
* DNS_CAA is not supported on Windows. |
| 57 |
* @param array|null $authns Passed by reference and, if given, will be populated with Resource |
| 58 |
* Records for the Authoritative Name Servers. |
| 59 |
* @param array|null $addtl Passed by reference and, if given, will be populated with any |
| 60 |
* Additional Records. |
| 61 |
* @param bool $raw The type will be interpreted as a raw DNS type ID |
| 62 |
* (the DNS_* constants cannot be used). |
| 63 |
* The return value will contain a data key, which needs |
| 64 |
* to be manually parsed. |
| 65 |
* @return array This function returns an array of associative arrays. Each associative array contains |
| 66 |
* at minimum the following keys: |
| 67 |
* |
| 68 |
* Basic DNS attributes |
| 69 |
* |
| 70 |
* |
| 71 |
* |
| 72 |
* Attribute |
| 73 |
* Meaning |
| 74 |
* |
| 75 |
* |
| 76 |
* |
| 77 |
* |
| 78 |
* host |
| 79 |
* |
| 80 |
* The record in the DNS namespace to which the rest of the associated data refers. |
| 81 |
* |
| 82 |
* |
| 83 |
* |
| 84 |
* class |
| 85 |
* |
| 86 |
* dns_get_record only returns Internet class records and as |
| 87 |
* such this parameter will always return IN. |
| 88 |
* |
| 89 |
* |
| 90 |
* |
| 91 |
* type |
| 92 |
* |
| 93 |
* String containing the record type. Additional attributes will also be contained |
| 94 |
* in the resulting array dependant on the value of type. See table below. |
| 95 |
* |
| 96 |
* |
| 97 |
* |
| 98 |
* ttl |
| 99 |
* |
| 100 |
* "Time To Live" remaining for this record. This will not equal |
| 101 |
* the record's original ttl, but will rather equal the original ttl minus whatever |
| 102 |
* length of time has passed since the authoritative name server was queried. |
| 103 |
* |
| 104 |
* |
| 105 |
* |
| 106 |
* |
| 107 |
* |
| 108 |
* |
| 109 |
* |
| 110 |
* Other keys in associative arrays dependant on 'type' |
| 111 |
* |
| 112 |
* |
| 113 |
* |
| 114 |
* Type |
| 115 |
* Extra Columns |
| 116 |
* |
| 117 |
* |
| 118 |
* |
| 119 |
* |
| 120 |
* A |
| 121 |
* |
| 122 |
* ip: An IPv4 addresses in dotted decimal notation. |
| 123 |
* |
| 124 |
* |
| 125 |
* |
| 126 |
* MX |
| 127 |
* |
| 128 |
* pri: Priority of mail exchanger. |
| 129 |
* Lower numbers indicate greater priority. |
| 130 |
* target: FQDN of the mail exchanger. |
| 131 |
* See also dns_get_mx. |
| 132 |
* |
| 133 |
* |
| 134 |
* |
| 135 |
* CNAME |
| 136 |
* |
| 137 |
* target: FQDN of location in DNS namespace to which |
| 138 |
* the record is aliased. |
| 139 |
* |
| 140 |
* |
| 141 |
* |
| 142 |
* NS |
| 143 |
* |
| 144 |
* target: FQDN of the name server which is authoritative |
| 145 |
* for this hostname. |
| 146 |
* |
| 147 |
* |
| 148 |
* |
| 149 |
* PTR |
| 150 |
* |
| 151 |
* target: Location within the DNS namespace to which |
| 152 |
* this record points. |
| 153 |
* |
| 154 |
* |
| 155 |
* |
| 156 |
* TXT |
| 157 |
* |
| 158 |
* txt: Arbitrary string data associated with this record. |
| 159 |
* |
| 160 |
* |
| 161 |
* |
| 162 |
* HINFO |
| 163 |
* |
| 164 |
* cpu: IANA number designating the CPU of the machine |
| 165 |
* referenced by this record. |
| 166 |
* os: IANA number designating the Operating System on |
| 167 |
* the machine referenced by this record. |
| 168 |
* See IANA's Operating System |
| 169 |
* Names for the meaning of these values. |
| 170 |
* |
| 171 |
* |
| 172 |
* |
| 173 |
* CAA |
| 174 |
* |
| 175 |
* flags: A one-byte bitfield; currently only bit 0 is defined, |
| 176 |
* meaning 'critical'; other bits are reserved and should be ignored. |
| 177 |
* tag: The CAA tag name (alphanumeric ASCII string). |
| 178 |
* value: The CAA tag value (binary string, may use subformats). |
| 179 |
* For additional information see: RFC 6844 |
| 180 |
* |
| 181 |
* |
| 182 |
* |
| 183 |
* SOA |
| 184 |
* |
| 185 |
* mname: FQDN of the machine from which the resource |
| 186 |
* records originated. |
| 187 |
* rname: Email address of the administrative contact |
| 188 |
* for this domain. |
| 189 |
* serial: Serial # of this revision of the requested |
| 190 |
* domain. |
| 191 |
* refresh: Refresh interval (seconds) secondary name |
| 192 |
* servers should use when updating remote copies of this domain. |
| 193 |
* retry: Length of time (seconds) to wait after a |
| 194 |
* failed refresh before making a second attempt. |
| 195 |
* expire: Maximum length of time (seconds) a secondary |
| 196 |
* DNS server should retain remote copies of the zone data without a |
| 197 |
* successful refresh before discarding. |
| 198 |
* minimum-ttl: Minimum length of time (seconds) a |
| 199 |
* client can continue to use a DNS resolution before it should request |
| 200 |
* a new resolution from the server. Can be overridden by individual |
| 201 |
* resource records. |
| 202 |
* |
| 203 |
* |
| 204 |
* |
| 205 |
* AAAA |
| 206 |
* |
| 207 |
* ipv6: IPv6 address |
| 208 |
* |
| 209 |
* |
| 210 |
* |
| 211 |
* A6(PHP >= 5.1.0) |
| 212 |
* |
| 213 |
* masklen: Length (in bits) to inherit from the target |
| 214 |
* specified by chain. |
| 215 |
* ipv6: Address for this specific record to merge with |
| 216 |
* chain. |
| 217 |
* chain: Parent record to merge with |
| 218 |
* ipv6 data. |
| 219 |
* |
| 220 |
* |
| 221 |
* |
| 222 |
* SRV |
| 223 |
* |
| 224 |
* pri: (Priority) lowest priorities should be used first. |
| 225 |
* weight: Ranking to weight which of commonly prioritized |
| 226 |
* targets should be chosen at random. |
| 227 |
* target and port: hostname and port |
| 228 |
* where the requested service can be found. |
| 229 |
* For additional information see: RFC 2782 |
| 230 |
* |
| 231 |
* |
| 232 |
* |
| 233 |
* NAPTR |
| 234 |
* |
| 235 |
* order and pref: Equivalent to |
| 236 |
* pri and weight above. |
| 237 |
* flags, services, regex, |
| 238 |
* and replacement: Parameters as defined by |
| 239 |
* RFC 2915. |
| 240 |
* |
| 241 |
* |
| 242 |
* |
| 243 |
* |
| 244 |
* |
| 245 |
* @throws NetworkException |
| 246 |
* |
| 247 |
*/ |
| 248 |
function dns_get_record(string $hostname, int $type = DNS_ANY, ?array &$authns = null, ?array &$addtl = null, bool $raw = false): array |
| 249 |
{ |
| 250 |
error_clear_last(); |
| 251 |
$result = \dns_get_record($hostname, $type, $authns, $addtl, $raw); |
| 252 |
if ($result === false) { |
| 253 |
throw NetworkException::createFromPhpError(); |
| 254 |
} |
| 255 |
return $result; |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
/** |
| 260 |
* Initiates a socket connection to the resource specified by |
| 261 |
* hostname. |
| 262 |
* |
| 263 |
* PHP supports targets in the Internet and Unix domains as described in |
| 264 |
* . A list of supported transports can also be |
| 265 |
* retrieved using stream_get_transports. |
| 266 |
* |
| 267 |
* The socket will by default be opened in blocking mode. You can |
| 268 |
* switch it to non-blocking mode by using |
| 269 |
* stream_set_blocking. |
| 270 |
* |
| 271 |
* The function stream_socket_client is similar but |
| 272 |
* provides a richer set of options, including non-blocking connection and the |
| 273 |
* ability to provide a stream context. |
| 274 |
* |
| 275 |
* @param string $hostname If OpenSSL support is |
| 276 |
* installed, you may prefix the hostname |
| 277 |
* with either ssl:// or tls:// to |
| 278 |
* use an SSL or TLS client connection over TCP/IP to connect to the |
| 279 |
* remote host. |
| 280 |
* @param int $port The port number. This can be omitted and skipped with |
| 281 |
* -1 for transports that do not use ports, such as |
| 282 |
* unix://. |
| 283 |
* @param int|null $errno If provided, holds the system level error number that occurred in the |
| 284 |
* system-level connect() call. |
| 285 |
* |
| 286 |
* If the value returned in errno is |
| 287 |
* 0 and the function returned FALSE, it is an |
| 288 |
* indication that the error occurred before the |
| 289 |
* connect() call. This is most likely due to a |
| 290 |
* problem initializing the socket. |
| 291 |
* @param string|null $errstr The error message as a string. |
| 292 |
* @param float $timeout The connection timeout, in seconds. |
| 293 |
* |
| 294 |
* If you need to set a timeout for reading/writing data over the |
| 295 |
* socket, use stream_set_timeout, as the |
| 296 |
* timeout parameter to |
| 297 |
* fsockopen only applies while connecting the |
| 298 |
* socket. |
| 299 |
* @return resource fsockopen returns a file pointer which may be used |
| 300 |
* together with the other file functions (such as |
| 301 |
* fgets, fgetss, |
| 302 |
* fwrite, fclose, and |
| 303 |
* feof). If the call fails, it will return FALSE |
| 304 |
* @throws NetworkException |
| 305 |
* |
| 306 |
*/ |
| 307 |
function fsockopen(string $hostname, int $port = -1, ?int &$errno = null, ?string &$errstr = null, float $timeout = null) |
| 308 |
{ |
| 309 |
error_clear_last(); |
| 310 |
if ($timeout !== null) { |
| 311 |
$result = \fsockopen($hostname, $port, $errno, $errstr, $timeout); |
| 312 |
} else { |
| 313 |
$result = \fsockopen($hostname, $port, $errno, $errstr); |
| 314 |
} |
| 315 |
if ($result === false) { |
| 316 |
throw NetworkException::createFromPhpError(); |
| 317 |
} |
| 318 |
return $result; |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
/** |
| 323 |
* gethostname gets the standard host name for |
| 324 |
* the local machine. |
| 325 |
* |
| 326 |
* @return string Returns a string with the hostname on success, otherwise FALSE is |
| 327 |
* returned. |
| 328 |
* @throws NetworkException |
| 329 |
* |
| 330 |
*/ |
| 331 |
function gethostname(): string |
| 332 |
{ |
| 333 |
error_clear_last(); |
| 334 |
$result = \gethostname(); |
| 335 |
if ($result === false) { |
| 336 |
throw NetworkException::createFromPhpError(); |
| 337 |
} |
| 338 |
return $result; |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
/** |
| 343 |
* getprotobyname returns the protocol number |
| 344 |
* associated with the protocol name as per |
| 345 |
* /etc/protocols. |
| 346 |
* |
| 347 |
* @param string $name The protocol name. |
| 348 |
* @return int Returns the protocol number. |
| 349 |
* @throws NetworkException |
| 350 |
* |
| 351 |
*/ |
| 352 |
function getprotobyname(string $name): int |
| 353 |
{ |
| 354 |
error_clear_last(); |
| 355 |
$result = \getprotobyname($name); |
| 356 |
if ($result === false) { |
| 357 |
throw NetworkException::createFromPhpError(); |
| 358 |
} |
| 359 |
return $result; |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
/** |
| 364 |
* getprotobynumber returns the protocol name |
| 365 |
* associated with protocol number as per |
| 366 |
* /etc/protocols. |
| 367 |
* |
| 368 |
* @param int $number The protocol number. |
| 369 |
* @return string Returns the protocol name as a string. |
| 370 |
* @throws NetworkException |
| 371 |
* |
| 372 |
*/ |
| 373 |
function getprotobynumber(int $number): string |
| 374 |
{ |
| 375 |
error_clear_last(); |
| 376 |
$result = \getprotobynumber($number); |
| 377 |
if ($result === false) { |
| 378 |
throw NetworkException::createFromPhpError(); |
| 379 |
} |
| 380 |
return $result; |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
/** |
| 385 |
* Registers a function that will be called when PHP starts sending output. |
| 386 |
* |
| 387 |
* The callback is executed just after PHP prepares all |
| 388 |
* headers to be sent, and before any other output is sent, creating a window |
| 389 |
* to manipulate the outgoing headers before being sent. |
| 390 |
* |
| 391 |
* @param callable $callback Function called just before the headers are sent. It gets no parameters |
| 392 |
* and the return value is ignored. |
| 393 |
* @throws NetworkException |
| 394 |
* |
| 395 |
*/ |
| 396 |
function header_register_callback(callable $callback): void |
| 397 |
{ |
| 398 |
error_clear_last(); |
| 399 |
$result = \header_register_callback($callback); |
| 400 |
if ($result === false) { |
| 401 |
throw NetworkException::createFromPhpError(); |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
/** |
| 407 |
* |
| 408 |
* |
| 409 |
* @param string $in_addr A 32bit IPv4, or 128bit IPv6 address. |
| 410 |
* @return string Returns a string representation of the address. |
| 411 |
* @throws NetworkException |
| 412 |
* |
| 413 |
*/ |
| 414 |
function inet_ntop(string $in_addr): string |
| 415 |
{ |
| 416 |
error_clear_last(); |
| 417 |
$result = \inet_ntop($in_addr); |
| 418 |
if ($result === false) { |
| 419 |
throw NetworkException::createFromPhpError(); |
| 420 |
} |
| 421 |
return $result; |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
/** |
| 426 |
* openlog opens a connection to the system |
| 427 |
* logger for a program. |
| 428 |
* |
| 429 |
* The use of openlog is optional. It |
| 430 |
* will automatically be called by syslog if |
| 431 |
* necessary, in which case ident will default |
| 432 |
* to FALSE. |
| 433 |
* |
| 434 |
* @param string $ident The string ident is added to each message. |
| 435 |
* @param int $option The option argument is used to indicate |
| 436 |
* what logging options will be used when generating a log message. |
| 437 |
* |
| 438 |
* openlog Options |
| 439 |
* |
| 440 |
* |
| 441 |
* |
| 442 |
* Constant |
| 443 |
* Description |
| 444 |
* |
| 445 |
* |
| 446 |
* |
| 447 |
* |
| 448 |
* LOG_CONS |
| 449 |
* |
| 450 |
* if there is an error while sending data to the system logger, |
| 451 |
* write directly to the system console |
| 452 |
* |
| 453 |
* |
| 454 |
* |
| 455 |
* LOG_NDELAY |
| 456 |
* |
| 457 |
* open the connection to the logger immediately |
| 458 |
* |
| 459 |
* |
| 460 |
* |
| 461 |
* LOG_ODELAY |
| 462 |
* |
| 463 |
* (default) delay opening the connection until the first |
| 464 |
* message is logged |
| 465 |
* |
| 466 |
* |
| 467 |
* |
| 468 |
* LOG_PERROR |
| 469 |
* print log message also to standard error |
| 470 |
* |
| 471 |
* |
| 472 |
* LOG_PID |
| 473 |
* include PID with each message |
| 474 |
* |
| 475 |
* |
| 476 |
* |
| 477 |
* |
| 478 |
* You can use one or more of these options. When using multiple options |
| 479 |
* you need to OR them, i.e. to open the connection |
| 480 |
* immediately, write to the console and include the PID in each message, |
| 481 |
* you will use: LOG_CONS | LOG_NDELAY | LOG_PID |
| 482 |
* @param int $facility The facility argument is used to specify what |
| 483 |
* type of program is logging the message. This allows you to specify |
| 484 |
* (in your machine's syslog configuration) how messages coming from |
| 485 |
* different facilities will be handled. |
| 486 |
* |
| 487 |
* openlog Facilities |
| 488 |
* |
| 489 |
* |
| 490 |
* |
| 491 |
* Constant |
| 492 |
* Description |
| 493 |
* |
| 494 |
* |
| 495 |
* |
| 496 |
* |
| 497 |
* LOG_AUTH |
| 498 |
* |
| 499 |
* security/authorization messages (use |
| 500 |
* LOG_AUTHPRIV instead |
| 501 |
* in systems where that constant is defined) |
| 502 |
* |
| 503 |
* |
| 504 |
* |
| 505 |
* LOG_AUTHPRIV |
| 506 |
* security/authorization messages (private) |
| 507 |
* |
| 508 |
* |
| 509 |
* LOG_CRON |
| 510 |
* clock daemon (cron and at) |
| 511 |
* |
| 512 |
* |
| 513 |
* LOG_DAEMON |
| 514 |
* other system daemons |
| 515 |
* |
| 516 |
* |
| 517 |
* LOG_KERN |
| 518 |
* kernel messages |
| 519 |
* |
| 520 |
* |
| 521 |
* LOG_LOCAL0 ... LOG_LOCAL7 |
| 522 |
* reserved for local use, these are not available in Windows |
| 523 |
* |
| 524 |
* |
| 525 |
* LOG_LPR |
| 526 |
* line printer subsystem |
| 527 |
* |
| 528 |
* |
| 529 |
* LOG_MAIL |
| 530 |
* mail subsystem |
| 531 |
* |
| 532 |
* |
| 533 |
* LOG_NEWS |
| 534 |
* USENET news subsystem |
| 535 |
* |
| 536 |
* |
| 537 |
* LOG_SYSLOG |
| 538 |
* messages generated internally by syslogd |
| 539 |
* |
| 540 |
* |
| 541 |
* LOG_USER |
| 542 |
* generic user-level messages |
| 543 |
* |
| 544 |
* |
| 545 |
* LOG_UUCP |
| 546 |
* UUCP subsystem |
| 547 |
* |
| 548 |
* |
| 549 |
* |
| 550 |
* |
| 551 |
* |
| 552 |
* LOG_USER is the only valid log type under Windows |
| 553 |
* operating systems |
| 554 |
* @throws NetworkException |
| 555 |
* |
| 556 |
*/ |
| 557 |
function openlog(string $ident, int $option, int $facility): void |
| 558 |
{ |
| 559 |
error_clear_last(); |
| 560 |
$result = \openlog($ident, $option, $facility); |
| 561 |
if ($result === false) { |
| 562 |
throw NetworkException::createFromPhpError(); |
| 563 |
} |
| 564 |
} |
| 565 |
|
| 566 |
|
| 567 |
/** |
| 568 |
* syslog generates a log message that will be |
| 569 |
* distributed by the system logger. |
| 570 |
* |
| 571 |
* For information on setting up a user defined log handler, see the |
| 572 |
* syslog.conf |
| 573 |
* 5 Unix manual page. More |
| 574 |
* information on the syslog facilities and option can be found in the man |
| 575 |
* pages for syslog |
| 576 |
* 3 on Unix machines. |
| 577 |
* |
| 578 |
* @param int $priority priority is a combination of the facility and |
| 579 |
* the level. Possible values are: |
| 580 |
* |
| 581 |
* syslog Priorities (in descending order) |
| 582 |
* |
| 583 |
* |
| 584 |
* |
| 585 |
* Constant |
| 586 |
* Description |
| 587 |
* |
| 588 |
* |
| 589 |
* |
| 590 |
* |
| 591 |
* LOG_EMERG |
| 592 |
* system is unusable |
| 593 |
* |
| 594 |
* |
| 595 |
* LOG_ALERT |
| 596 |
* action must be taken immediately |
| 597 |
* |
| 598 |
* |
| 599 |
* LOG_CRIT |
| 600 |
* critical conditions |
| 601 |
* |
| 602 |
* |
| 603 |
* LOG_ERR |
| 604 |
* error conditions |
| 605 |
* |
| 606 |
* |
| 607 |
* LOG_WARNING |
| 608 |
* warning conditions |
| 609 |
* |
| 610 |
* |
| 611 |
* LOG_NOTICE |
| 612 |
* normal, but significant, condition |
| 613 |
* |
| 614 |
* |
| 615 |
* LOG_INFO |
| 616 |
* informational message |
| 617 |
* |
| 618 |
* |
| 619 |
* LOG_DEBUG |
| 620 |
* debug-level message |
| 621 |
* |
| 622 |
* |
| 623 |
* |
| 624 |
* |
| 625 |
* @param string $message The message to send, except that the two characters |
| 626 |
* %m will be replaced by the error message string |
| 627 |
* (strerror) corresponding to the present value of |
| 628 |
* errno. |
| 629 |
* @throws NetworkException |
| 630 |
* |
| 631 |
*/ |
| 632 |
function syslog(int $priority, string $message): void |
| 633 |
{ |
| 634 |
error_clear_last(); |
| 635 |
$result = \syslog($priority, $message); |
| 636 |
if ($result === false) { |
| 637 |
throw NetworkException::createFromPhpError(); |
| 638 |
} |
| 639 |
} |
| 640 |
|