1<?php
2/**
3 * Copyright 2007-2016 Horde LLC (http://www.horde.org/)
4 *
5 * @author   Chuck Hagenbuch <chuck@horde.org>
6 * @license  http://www.horde.org/licenses/bsd BSD
7 * @category Horde
8 * @package  Http
9 */
10
11/**
12 * @author   Chuck Hagenbuch <chuck@horde.org>
13 * @license  http://www.horde.org/licenses/bsd BSD
14 * @category Horde
15 * @package  Http
16 */
17class Horde_Http_Response_Fopen extends Horde_Http_Response_Base
18{
19    /**
20     * Response body.
21     *
22     * @var stream
23     */
24    protected $_stream;
25
26    /**
27     * Response content
28     */
29    protected $_content;
30
31    /**
32     * Constructor.
33     */
34    public function __construct($uri, $stream, $headers = array())
35    {
36        $this->uri = $uri;
37        $this->_stream = $stream;
38        $this->_parseHeaders($headers);
39    }
40
41    /**
42     * Returns the body of the HTTP response.
43     *
44     * @throws Horde_Http_Exception
45     * @return string HTTP response body.
46     */
47    public function getBody()
48    {
49        if (is_null($this->_content)) {
50            $oldTrackErrors = ini_set('track_errors', 1);
51            $content = @stream_get_contents($this->_stream);
52            ini_set('track_errors', $oldTrackErrors);
53            if ($content === false) {
54                $msg = 'Problem reading data from ' . $this->uri;
55                if (isset($php_errormsg)) {
56                    $msg .= ': ' . $php_errormsg;
57                }
58                throw new Horde_Http_Exception($msg);
59            }
60            $this->_content = $content;
61        }
62
63        return $this->_content;
64    }
65
66    /**
67     * Returns a stream pointing to the response body that can be used with
68     * all standard PHP stream functions.
69     */
70    public function getStream()
71    {
72        return $this->_stream;
73    }
74}
75