1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19

20
/**
21
 * A HTTP download task.
22
 *
23
 * Downloads a file via HTTP GET method and saves it to a specified directory
24
 *
25
 * @package phing.tasks.ext
26
 * @author  Ole Markus With <o.with@sportradar.com>
27
 */
28
class HttpGetTask extends HttpTask
29
{
30
    /**
31
     * Holds the filename to store the output in
32
     *
33
     * @var string
34
     */
35
    protected $filename = null;
36

37
    /**
38
     * Holds the save location
39
     *
40
     * @var string
41
     */
42
    protected $dir = null;
43

44
    /**
45
     * Holds value for "ssl_verify_peer" option
46
     *
47
     * @var boolean
48
     */
49
    protected $sslVerifyPeer = true;
50

51
    /**
52
     * Holds value for "follow_redirects" option
53
     *
54
     * @var null|bool
55
     */
56
    protected $followRedirects = null;
57

58
    /**
59
     * Holds the proxy
60
     *
61
     * @var string
62
     */
63
    protected $proxy = null;
64

65
    private $quiet = false;
66

67
    /**
68
     * @param array $options
69
     * @return \Psr\Http\Message\ResponseInterface
70
     */
71 1
    protected function request($options = [])
72
    {
73 1
        if (!isset($this->dir)) {
74 1
            throw new BuildException("Required attribute 'dir' is missing", $this->getLocation());
75
        }
76

77
        $options = [
78 1
            'verify' => $this->sslVerifyPeer
79
        ];
80 1
        if (isset($this->proxy)) {
81 1
            $options['proxy'] = $this->proxy;
82
        }
83 1
        if (null !== $this->followRedirects) {
84 1
            $options['allow_redirects'] = $this->followRedirects;
85
        }
86

87 1
        $response = parent::request($options);
88

89 1
        $this->log("Fetching " . $this->url);
90

91 1
        return $response;
92
    }
93

94
    /**
95
     * Saves the response body to a specified directory
96
     *
97
     * @param \Psr\Http\Message\ResponseInterface $response
98
     * @return void
99
     */
100 1
    protected function processResponse(\Psr\Http\Message\ResponseInterface $response)
101
    {
102 1
        if ($response->getStatusCode() != 200) {
103 0
            throw new BuildException(
104 0
                "Request unsuccessful. Response from server: " . $response->getStatusCode()
105 0
                . " " . $response->getReasonPhrase(),
106 0
                $this->getLocation()
107
            );
108
        }
109

110 1
        $content = $response->getBody();
111 1
        $disposition = $response->getHeader('content-disposition');
112

113 1
        if ($this->filename) {
114 1
            $filename = $this->filename;
115
        } elseif (
116 0
            !empty($disposition)
117 1
            && 0 == strpos($disposition[0], 'attachment')
118 1
            && preg_match('/filename="([^"]+)"/', $disposition[0], $m)
119
        ) {
120 1
            $filename = basename($m[1]);
121
        } else {
122 1
            $filename = basename(parse_url($this->url, PHP_URL_PATH));
123
        }
124

125 1
        if (!is_writable($this->dir)) {
126 0
            throw new BuildException("Cannot write to directory: " . $this->dir, $this->getLocation());
127
        }
128

129 1
        $filename = $this->dir . "/" . $filename;
130 1
        file_put_contents($filename, $content);
131

132 1
        $this->log("Contents from " . $this->url . " saved to $filename");
133
    }
134

135
    /**
136
     * Sets the filename to store the output in
137
     *
138
     * @param string $filename
139
     */
140 1
    public function setFilename($filename)
141
    {
142 1
        $this->filename = $filename;
143
    }
144

145
    /**
146
     * Sets the save location
147
     *
148
     * @param string $dir
149
     */
150 1
    public function setDir($dir)
151
    {
152 1
        $this->dir = $dir;
153
    }
154

155
    /**
156
     * Sets the ssl_verify_peer option
157
     *
158
     * @param bool $value
159
     */
160 1
    public function setSslVerifyPeer($value)
161
    {
162 1
        $this->sslVerifyPeer = $value;
163
    }
164

165
    /**
166
     * Sets the follow_redirects option
167
     *
168
     * @param bool $value
169
     */
170 1
    public function setFollowRedirects($value)
171
    {
172 1
        $this->followRedirects = $value;
173
    }
174

175
    /**
176
     * Sets the proxy
177
     *
178
     * @param string $proxy
179
     */
180 1
    public function setProxy($proxy)
181
    {
182 1
        $this->proxy = $proxy;
183
    }
184

185
    /**
186
     * If true, set default log level to Project.MSG_ERR.
187
     *
188
     * @param boolean $v if "true" then be quiet
189
     */
190 0
    public function setQuiet($v)
191
    {
192 0
        $this->quiet = $v;
193
    }
194

195 1
    public function log($msg, $msgLevel = Project::MSG_INFO, Exception $t = null)
196
    {
197 1
        if (!$this->quiet || $msgLevel <= Project::MSG_ERR) {
198 1
            parent::log($msg, $msgLevel, $t);
199
        }
200
    }
201
}

Read our documentation on viewing source code .

Loading