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;
36

37
    /**
38
     * Holds the save location
39
     *
40
     * @var string
41
     */
42
    protected $dir;
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;
57

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

65
    private $quiet = false;
66

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

77 2
        $options['verify'] = $this->sslVerifyPeer;
78

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

86 2
        $response = parent::request($options);
87

88 2
        $this->log("Fetching " . $this->url);
89

90 2
        return $response;
91
    }
92

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

109 2
        $content = $response->getBody();
110 2
        $disposition = $response->getHeader('content-disposition');
111

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

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

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

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

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

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

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

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

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

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

194
    /**
195
     * @param string $msg
196
     * @param int $msgLevel
197
     * @param Exception|null $t
198
     */
199 2
    public function log($msg, $msgLevel = Project::MSG_INFO, Exception $t = null)
200
    {
201 2
        if (!$this->quiet || $msgLevel <= Project::MSG_ERR) {
202 2
            parent::log($msg, $msgLevel, $t);
203
        }
204
    }
205
}

Read our documentation on viewing source code .

Loading