[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/Requests/Proxy/ -> Http.php (source)

   1  <?php
   2  /**
   3   * HTTP Proxy connection interface
   4   *
   5   * @package Requests\Proxy
   6   * @since   1.6
   7   */
   8  
   9  namespace WpOrg\Requests\Proxy;
  10  
  11  use WpOrg\Requests\Exception\ArgumentCount;
  12  use WpOrg\Requests\Exception\InvalidArgument;
  13  use WpOrg\Requests\Hooks;
  14  use WpOrg\Requests\Proxy;
  15  
  16  /**
  17   * HTTP Proxy connection interface
  18   *
  19   * Provides a handler for connection via an HTTP proxy
  20   *
  21   * @package Requests\Proxy
  22   * @since   1.6
  23   */
  24  final class Http implements Proxy {
  25      /**
  26       * Proxy host and port
  27       *
  28       * Notation: "host:port" (eg 127.0.0.1:8080 or someproxy.com:3128)
  29       *
  30       * @var string
  31       */
  32      public $proxy;
  33  
  34      /**
  35       * Username
  36       *
  37       * @var string
  38       */
  39      public $user;
  40  
  41      /**
  42       * Password
  43       *
  44       * @var string
  45       */
  46      public $pass;
  47  
  48      /**
  49       * Do we need to authenticate? (ie username & password have been provided)
  50       *
  51       * @var boolean
  52       */
  53      public $use_authentication;
  54  
  55      /**
  56       * Constructor
  57       *
  58       * @since 1.6
  59       *
  60       * @param array|string|null $args Proxy as a string or an array of proxy, user and password.
  61       *                                When passed as an array, must have exactly one (proxy)
  62       *                                or three elements (proxy, user, password).
  63       *
  64       * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed argument is not an array, a string or null.
  65       * @throws \WpOrg\Requests\Exception\ArgumentCount On incorrect number of arguments (`proxyhttpbadargs`)
  66       */
  67  	public function __construct($args = null) {
  68          if (is_string($args)) {
  69              $this->proxy = $args;
  70          }
  71          elseif (is_array($args)) {
  72              if (count($args) === 1) {
  73                  list($this->proxy) = $args;
  74              }
  75              elseif (count($args) === 3) {
  76                  list($this->proxy, $this->user, $this->pass) = $args;
  77                  $this->use_authentication                    = true;
  78              }
  79              else {
  80                  throw ArgumentCount::create(
  81                      'an array with exactly one element or exactly three elements',
  82                      count($args),
  83                      'proxyhttpbadargs'
  84                  );
  85              }
  86          } elseif ($args !== null) {
  87              throw InvalidArgument::create(1, '$args', 'array|string|null', gettype($args));
  88          }
  89      }
  90  
  91      /**
  92       * Register the necessary callbacks
  93       *
  94       * @since 1.6
  95       * @see \WpOrg\Requests\Proxy\HTTP::curl_before_send()
  96       * @see \WpOrg\Requests\Proxy\HTTP::fsockopen_remote_socket()
  97       * @see \WpOrg\Requests\Proxy\HTTP::fsockopen_remote_host_path()
  98       * @see \WpOrg\Requests\Proxy\HTTP::fsockopen_header()
  99       * @param \WpOrg\Requests\Hooks $hooks Hook system
 100       */
 101  	public function register(Hooks $hooks) {
 102          $hooks->register('curl.before_send', [$this, 'curl_before_send']);
 103  
 104          $hooks->register('fsockopen.remote_socket', [$this, 'fsockopen_remote_socket']);
 105          $hooks->register('fsockopen.remote_host_path', [$this, 'fsockopen_remote_host_path']);
 106          if ($this->use_authentication) {
 107              $hooks->register('fsockopen.after_headers', [$this, 'fsockopen_header']);
 108          }
 109      }
 110  
 111      /**
 112       * Set cURL parameters before the data is sent
 113       *
 114       * @since 1.6
 115       * @param resource|\CurlHandle $handle cURL handle
 116       */
 117  	public function curl_before_send(&$handle) {
 118          curl_setopt($handle, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
 119          curl_setopt($handle, CURLOPT_PROXY, $this->proxy);
 120  
 121          if ($this->use_authentication) {
 122              curl_setopt($handle, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
 123              curl_setopt($handle, CURLOPT_PROXYUSERPWD, $this->get_auth_string());
 124          }
 125      }
 126  
 127      /**
 128       * Alter remote socket information before opening socket connection
 129       *
 130       * @since 1.6
 131       * @param string $remote_socket Socket connection string
 132       */
 133  	public function fsockopen_remote_socket(&$remote_socket) {
 134          $remote_socket = $this->proxy;
 135      }
 136  
 137      /**
 138       * Alter remote path before getting stream data
 139       *
 140       * @since 1.6
 141       * @param string $path Path to send in HTTP request string ("GET ...")
 142       * @param string $url Full URL we're requesting
 143       */
 144  	public function fsockopen_remote_host_path(&$path, $url) {
 145          $path = $url;
 146      }
 147  
 148      /**
 149       * Add extra headers to the request before sending
 150       *
 151       * @since 1.6
 152       * @param string $out HTTP header string
 153       */
 154  	public function fsockopen_header(&$out) {
 155          $out .= sprintf("Proxy-Authorization: Basic %s\r\n", base64_encode($this->get_auth_string()));
 156      }
 157  
 158      /**
 159       * Get the authentication string (user:pass)
 160       *
 161       * @since 1.6
 162       * @return string
 163       */
 164  	public function get_auth_string() {
 165          return $this->user . ':' . $this->pass;
 166      }
 167  }


Generated: Mon Dec 6 01:00:03 2021 Cross-referenced by PHPXref 0.7.1