Basic.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * Basic Authentication provider
  4. *
  5. * @package Requests\Authentication
  6. */
  7. namespace WpOrg\Requests\Auth;
  8. use WpOrg\Requests\Auth;
  9. use WpOrg\Requests\Exception\ArgumentCount;
  10. use WpOrg\Requests\Exception\InvalidArgument;
  11. use WpOrg\Requests\Hooks;
  12. /**
  13. * Basic Authentication provider
  14. *
  15. * Provides a handler for Basic HTTP authentication via the Authorization
  16. * header.
  17. *
  18. * @package Requests\Authentication
  19. */
  20. class Basic implements Auth {
  21. /**
  22. * Username
  23. *
  24. * @var string
  25. */
  26. public $user;
  27. /**
  28. * Password
  29. *
  30. * @var string
  31. */
  32. public $pass;
  33. /**
  34. * Constructor
  35. *
  36. * @since 2.0 Throws an `InvalidArgument` exception.
  37. * @since 2.0 Throws an `ArgumentCount` exception instead of the Requests base `Exception.
  38. *
  39. * @param array|null $args Array of user and password. Must have exactly two elements
  40. *
  41. * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed argument is not an array or null.
  42. * @throws \WpOrg\Requests\Exception\ArgumentCount On incorrect number of array elements (`authbasicbadargs`).
  43. */
  44. public function __construct($args = null) {
  45. if (is_array($args)) {
  46. if (count($args) !== 2) {
  47. throw ArgumentCount::create('an array with exactly two elements', count($args), 'authbasicbadargs');
  48. }
  49. list($this->user, $this->pass) = $args;
  50. return;
  51. }
  52. if ($args !== null) {
  53. throw InvalidArgument::create(1, '$args', 'array|null', gettype($args));
  54. }
  55. }
  56. /**
  57. * Register the necessary callbacks
  58. *
  59. * @see \WpOrg\Requests\Auth\Basic::curl_before_send()
  60. * @see \WpOrg\Requests\Auth\Basic::fsockopen_header()
  61. * @param \WpOrg\Requests\Hooks $hooks Hook system
  62. */
  63. public function register(Hooks $hooks) {
  64. $hooks->register('curl.before_send', [$this, 'curl_before_send']);
  65. $hooks->register('fsockopen.after_headers', [$this, 'fsockopen_header']);
  66. }
  67. /**
  68. * Set cURL parameters before the data is sent
  69. *
  70. * @param resource|\CurlHandle $handle cURL handle
  71. */
  72. public function curl_before_send(&$handle) {
  73. curl_setopt($handle, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  74. curl_setopt($handle, CURLOPT_USERPWD, $this->getAuthString());
  75. }
  76. /**
  77. * Add extra headers to the request before sending
  78. *
  79. * @param string $out HTTP header string
  80. */
  81. public function fsockopen_header(&$out) {
  82. $out .= sprintf("Authorization: Basic %s\r\n", base64_encode($this->getAuthString()));
  83. }
  84. /**
  85. * Get the authentication string (user:pass)
  86. *
  87. * @return string
  88. */
  89. public function getAuthString() {
  90. return $this->user . ':' . $this->pass;
  91. }
  92. }