PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 2.4.4
Booking for Appointments and Events Calendar – Amelia v2.4.4
2.4.4 2.4.3 2.4.2 2.4.1 2.4 trunk 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.32 1.2.33 1.2.34 1.2.35 1.2.36 1.2.37 1.2.38 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.3
ameliabooking / vendor / rmccue / requests / src / Auth / Basic.php
ameliabooking / vendor / rmccue / requests / src / Auth Last commit date
Basic.php 5 months ago
Basic.php
104 lines
1 <?php
2 /**
3 * Basic Authentication provider
4 *
5 * @package Requests\Authentication
6 */
7
8 namespace AmeliaVendor\WpOrg\Requests\Auth;
9
10 use AmeliaVendor\WpOrg\Requests\Auth;
11 use AmeliaVendor\WpOrg\Requests\Exception\ArgumentCount;
12 use AmeliaVendor\WpOrg\Requests\Exception\InvalidArgument;
13 use AmeliaVendor\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 \AmeliaVendor\WpOrg\Requests\Exception\InvalidArgument When the passed argument is not an array or null.
47 * @throws \AmeliaVendor\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 \AmeliaVendor\WpOrg\Requests\Auth\Basic::curl_before_send()
68 * @see \AmeliaVendor\WpOrg\Requests\Auth\Basic::fsockopen_header()
69 * @param \AmeliaVendor\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 }
104