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