Socket.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * This is a PHP library that handles calling reCAPTCHA.
  4. *
  5. * @copyright Copyright (c) 2015, Google Inc.
  6. * @link http://www.google.com/recaptcha
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. namespace ReCaptcha\RequestMethod;
  27. /**
  28. * Convenience wrapper around native socket and file functions to allow for
  29. * mocking.
  30. */
  31. class Socket
  32. {
  33. private $handle = null;
  34. /**
  35. * fsockopen
  36. *
  37. * @see http://php.net/fsockopen
  38. * @param string $hostname
  39. * @param int $port
  40. * @param int $errno
  41. * @param string $errstr
  42. * @param float $timeout
  43. * @return resource
  44. */
  45. public function fsockopen($hostname, $port = -1, &$errno = 0, &$errstr = '', $timeout = null)
  46. {
  47. $this->handle = fsockopen($hostname, $port, $errno, $errstr, (is_null($timeout) ? ini_get("default_socket_timeout") : $timeout));
  48. if ($this->handle != false && $errno === 0 && $errstr === '') {
  49. return $this->handle;
  50. } else {
  51. return false;
  52. }
  53. }
  54. /**
  55. * fwrite
  56. *
  57. * @see http://php.net/fwrite
  58. * @param string $string
  59. * @param int $length
  60. * @return int | bool
  61. */
  62. public function fwrite($string, $length = null)
  63. {
  64. return fwrite($this->handle, $string, (is_null($length) ? strlen($string) : $length));
  65. }
  66. /**
  67. * fgets
  68. *
  69. * @see http://php.net/fgets
  70. * @param int $length
  71. */
  72. public function fgets($length = null)
  73. {
  74. return fgets($this->handle, $length);
  75. }
  76. /**
  77. * feof
  78. *
  79. * @see http://php.net/feof
  80. * @return bool
  81. */
  82. public function feof()
  83. {
  84. return feof($this->handle);
  85. }
  86. /**
  87. * fclose
  88. *
  89. * @see http://php.net/fclose
  90. * @return bool
  91. */
  92. public function fclose()
  93. {
  94. return fclose($this->handle);
  95. }
  96. }