/wp-includes/Requests/Auth/Basic.php

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