Requests_Auth_Basic

Basic Authentication provider.

Defined (1)

The class is defined in the following location(s).

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