network.php 14 KB

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