1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13:
14: 15: 16: 17: 18: 19:
20: class Horde_Http_Request_Fopen extends Horde_Http_Request_Base
21: {
22: 23: 24: 25: 26:
27: public function __construct($args = array())
28: {
29: if (!ini_get('allow_url_fopen')) {
30: throw new Horde_Http_Exception('allow_url_fopen must be enabled');
31: }
32:
33: parent::__construct($args);
34: }
35:
36: 37: 38: 39: 40: 41:
42: public function send()
43: {
44: $method = $this->method;
45: $uri = $this->uri;
46: $headers = $this->headers;
47: $data = $this->data;
48: if (is_array($data)) {
49: $data = http_build_query($data, '', '&');
50: }
51:
52: $opts = array('http' => array());
53:
54:
55: if ($this->proxyServer) {
56: $opts['http']['proxy'] = 'tcp://' . $this->proxyServer;
57: if ($this->proxyPort) {
58: $opts['http']['proxy'] .= ':' . $this->proxyPort;
59: }
60: $opts['http']['request_fulluri'] = true;
61: if ($this->proxyUsername && $this->proxyPassword) {
62:
63: $headers['Proxy-Authorization'] = 'Basic ' . base64_encode($this->proxyUsername . ':' . $this->proxyPassword);
64: }
65: if ($this->proxyType != Horde_Http::PROXY_HTTP) {
66: throw new Horde_Http_Exception(sprintf('Proxy type %s not supported by this request type!', $this->proxyType));
67: }
68: }
69:
70:
71: if ($this->username) {
72: switch ($this->authenticationScheme) {
73: case Horde_Http::AUTH_BASIC:
74: case Horde_Http::AUTH_ANY:
75: $headers['Authorization'] = 'Basic ' . base64_encode($this->username . ':' . $this->password);
76: break;
77:
78: default:
79: throw new Horde_Http_Exception('Unsupported authentication scheme (' . $this->authenticationScheme . ')');
80: }
81: }
82:
83:
84: $hdr = array();
85: foreach ($headers as $header => $value) {
86: $hdr[] = $header . ': ' . $value;
87: }
88:
89:
90: $opts['http']['method'] = $method;
91: $opts['http']['header'] = implode("\n", $hdr);
92: $opts['http']['content'] = $data;
93: $opts['http']['timeout'] = $this->timeout;
94: $opts['http']['max_redirects'] = $this->redirects;
95: $opts['http']['ignore_errors'] = true;
96:
97: $context = stream_context_create($opts);
98: $stream = @fopen($uri, 'rb', false, $context);
99: if (!$stream) {
100: $error = error_get_last();
101: if (preg_match('/HTTP\/(\d+\.\d+) (\d{3}) (.*)$/', $error['message'], $matches)) {
102:
103: return new Horde_Http_Response_Fopen($uri, null, $matches[0]);
104: } else {
105: throw new Horde_Http_Exception('Problem with ' . $uri . ': ', $error);
106: }
107: }
108:
109: $meta = stream_get_meta_data($stream);
110: $headers = isset($meta['wrapper_data']) ? $meta['wrapper_data'] : array();
111:
112: return new Horde_Http_Response_Fopen($uri, $stream, $headers);
113: }
114: }
115: