File: FTP.php.inc

package info (click to toggle)
php-pear 1%3A1.10.12%2Bsubmodules%2Bnotgz%2B20210212-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 18,540 kB
  • sloc: php: 52,900; ansic: 39,986; xml: 33,116; yacc: 677; pascal: 452; makefile: 122; sh: 113
file content (339 lines) | stat: -rw-r--r-- 10,035 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
if (!defined('FTP_BINARY')) {
    define('FTP_BINARY', 1);
}

define('NET_FTP_ERR_OVERWRITELOCALFILE_FAILED', -14);
define('NET_FTP_ERR_DIRCHANGE_FAILED', 2); // Compatibillity reasons!
define('NET_FTP_ERR_CREATEDIR_FAILED', 1);
define('NET_FTP_ERR_OVERWRITELOCALFILE_FORBIDDEN', 32);
define('NET_FTP_ERR_UPLOADFILE_FAILED', 5);
function Net_FTP_setup(&$t)
{
    $a = &Net_FTP::singleton();
    foreach (get_object_vars($a) as $name => $val) {
        $t->$name = $val;
    }
}
/**
 * mock object
 */
class Net_FTP extends PEAR
{
    var $_connectErr = false;
    var $_loginErr = false;
    var $_cdErr = false;
    var $_cdTo = array();
    var $_dirsExisting = array();
    var $_dirsMade = array();
    var $_rmfiles = array();
    var $_host;
    var $_port;
    var $_handle;
    var $_failMkdir = array();
    function Net_FTP($host = true, $port = false, $timeout = false)
    {
        $this->_host = $host;
        $this->_port = $port;
        $this->_timeout = $timeout;
        if ($host) {
            Net_FTP_setup($this);
        }
    }

    /**
     * @return Net_FTP
     */
    function &singleton()
    {
        if (!isset($GLOBALS['_TEST_FTP'])) {
            $GLOBALS['_TEST_FTP'] = new Net_FTP(false, false, false);
        }
        return $GLOBALS['_TEST_FTP'];
    }

    function connect()
    {
        if ($this->_connectErr) {
            return PEAR::raiseError($this->_connectErr);
        }
    }

    function setConnectError($err)
    {
        $this->_connectErr = $err;
    }

    function login($user, $pass)
    {
        if ($this->_loginErr) {
            return $this->raiseError($this->_loginErr);
        }
    }

    function setLoginError($err)
    {
        $this->_loginErr = $err;
    }

    function cd($path)
    {
        $f = &Net_FTP::singleton();
        if (isset($this->_cdErr[$path])) {
            return $this->raiseError($this->_cdErr[$path]);
        }
        if ($path == '/' || $path == '\\') {
           $path = '';
        }
        if ($path != '' && !isset($f->_dirsExisting[$path])) {
            return $this->raiseError("Directory change failed", NET_FTP_ERR_DIRCHANGE_FAILED);
        }
        $this->_cdTo = $path;
        return true;
    }

    function setCdError($err)
    {
        $this->_cdErr = $err;
    }

    function put($local, $remote)
    {
        $f = &Net_FTP::singleton();
        if (!isset($f->_dirsExisting[dirname($remote)])) {
            return $this->raiseError("File '$local_file' could not be uploaded to '$remote_file'.", NET_FTP_ERR_UPLOADFILE_FAILED);
        }
        $f->_putfiles[$local] = $remote;
        return true;
    }

    function rm($remote)
    {
        $f = &Net_FTP::singleton();
        $f->_rmfiles[] = $remote;
        return true;
    }

    function pwd()
    {
        return $this->_cdTo;
    }

    function _construct_path($path)
    {
        return $path;
    }

    function _testftp_mkdir($handle, $dir)
    {
        $f = &Net_FTP::singleton();
        if (isset($this->_failMkdir[$dir]) || (dirname($dir) != '.' &&
              isset($f->_dirsExisting[$dir]))) {
            return false;
        }
        $f->_dirsMade[$dir] = true;
        $f->_dirsExisting[$dir] = true;
        return true;
    }

    function setFailmkdir($dirs)
    {
        $this->_failMkdir = $dirs;
    }

    function get($remote, $local, $overwrite = false, $mode = null)
    {
        if ($this->_cdTo) {
            $remote = $this->_cdTo . '/' . $remote;
        }
        if (isset($this->_remoteFiles[$remote])) {
            if (!$overwrite && file_exists($local)) {
                return $this->raiseError("Local file '$local_file' exists and may not be overwriten.", NET_FTP_ERR_OVERWRITELOCALFILE_FORBIDDEN);
            }
            $contents = implode('', file($this->_remoteFiles[$remote]));
            $fp = fopen($local, 'wb');
            fwrite($fp, $contents);
            fclose($fp);
            return true;
        }
        return $this->raiseError("File '$remote' could not be downloaded to 'local'.", NET_FTP_ERR_OVERWRITELOCALFILE_FAILED);
    }

    function addRemoteFile($path, $loc)
    {
        $this->_remoteFiles[$path] = $loc;
    }

    function disconnect()
    {
    }

    function setDirsExisting($dirs)
    {
        $f = &Net_FTP::singleton();
        $f->_dirsExisting = array_flip($dirs);
    }
}
require_once 'PEAR/RemoteInstaller.php';
class test_PEAR_RemoteInstaller extends PEAR_RemoteInstaller {
    function download($packages, $options, &$config, &$installpackages,
                      &$errors, $installed = false, $willinstall = false, $state = false)
    {
        // trickiness: initialize here
        $this->PEAR_Downloader($this->ui, $options, $config);
        $this->_remote = new test_PEAR_Remote($config);
        $ret = PEAR_Downloader::download($packages);
        $errors = $this->getErrorMsgs();
        $installpackages = $this->getDownloadedPackages();
        trigger_error("PEAR Warning: PEAR_Installer::download() is deprecated " .
                      "in favor of PEAR_Downloader class", E_USER_WARNING);
        return $ret;
    }

    /**
     * For simpler unit-testing
     * @param PEAR_Config
     * @param int
     * @param string
     */
    function &getPackagefileObject(&$c, $d, $dir = null)
    {
        $a = new test_PEAR_PackageFile($c, $d, $dir);
        return $a;
    }
    
    function downloadHttp($url, &$ui, $save_dir = '.', $callback = null)
    {
//        return parent::downloadHttp($url, $ui, $save_dir, $callback);
        if ($callback) {
            call_user_func($callback, 'setup', array(&$ui));
        }
        $info = parse_url($url);
        if (!isset($info['scheme']) || $info['scheme'] != 'http') {
            return PEAR::raiseError('Cannot download non-http URL "' . $url . '"');
        }
        if (!isset($info['host'])) {
            return PEAR::raiseError('Cannot download from non-URL "' . $url . '"');
        } else {
            $host = @$info['host'];
            $port = @$info['port'];
            $path = @$info['path'];
        }
        if (isset($this)) {
            $config = &$this->config;
        } else {
            $config = &PEAR_Config::singleton();
        }
        $proxy_host = $proxy_port = $proxy_user = $proxy_pass = '';
        if ($proxy = parse_url($config->get('http_proxy'))) {
            $proxy_host = @$proxy['host'];
            $proxy_port = @$proxy['port'];
            $proxy_user = @$proxy['user'];
            $proxy_pass = @$proxy['pass'];

            if ($proxy_port == '') {
                $proxy_port = 8080;
            }
            if ($callback) {
                call_user_func($callback, 'message', "Using HTTP proxy $host:$port");
            }
        }
        if (empty($port)) {
            $port = 80;
        }
        // use _pearweb to get file
        $retrieved = explode("\n", $this->_remote->_pearweb->receiveHttp($url));
        $headers = array();
        $line = array_shift($retrieved);
        while (strlen(trim($line))) {
            if (preg_match('/^([^:]+):\s+(.*)\s*$/', $line, $matches)) {
                $headers[strtolower($matches[1])] = trim($matches[2]);
            } elseif (preg_match('|^HTTP/1.[01] ([0-9]{3}) |', $line, $matches)) {
                if ($matches[1] != 200) {
                    return PEAR::raiseError("File http://$host:$port$path not valid (received: $line)");
                }
            }
            $line = array_shift($retrieved);
        }
        $retrieved = join("\n", $retrieved);
        if (isset($headers['content-disposition']) &&
            preg_match('/\sfilename=\"([^;]*\S)\"\s*(;|$)/', $headers['content-disposition'], $matches)) {
            $save_as = basename($matches[1]);
        } else {
            $save_as = basename($url);
        }
        if ($callback) {
            $tmp = call_user_func($callback, 'saveas', $save_as);
            if ($tmp) {
                $save_as = $tmp;
            }
        }
        $dest_file = $save_dir . DIRECTORY_SEPARATOR . $save_as;
        if (!$wp = @fopen($dest_file, 'wb')) {
            fclose($fp);
            if ($callback) {
                call_user_func($callback, 'writefailed', array($dest_file, $php_errormsg));
            }
            return PEAR::raiseError("could not open $dest_file for writing");
        }
        if (isset($headers['content-length'])) {
            $length = $headers['content-length'];
        } else {
            $length = -1;
        }
        $bytes = 0;
        if ($callback) {
            call_user_func($callback, 'start', array(basename($dest_file), $length));
        }
        $start = 0;
        while ($start < strlen($retrieved) - 1) {
            $data = substr($retrieved, $start, 1024);
            $start += 1024;
            $bytes += strlen($data);
            if ($callback) {
                call_user_func($callback, 'bytesread', $bytes);
            }
            if (!@fwrite($wp, $data)) {
                if ($callback) {
                    call_user_func($callback, 'writefailed', array($dest_file, $php_errormsg));
                }
                return PEAR::raiseError("$dest_file: write failed ($php_errormsg)");
            }
        }
        fclose($wp);
        if ($callback) {
            call_user_func($callback, 'done', $bytes);
        }
        return $dest_file;
    }

    function log($level, $msg)
    {
        global $fakelog;
        if (isset($fakelog)) {
            $fakelog->log($level, $msg);
        } else {
            return parent::log($level, $msg);
        }
    }

    function &getDependency2($c, $i, $p, $s)
    {
        $a = &test_PEAR_Dependency2::singleton($c, $i, $p, $s);
        return $a;
    }

    /**
     * For simpler unit-testing
     * @param PEAR_Config
     * @param array
     * @param array
     * @param int
     */
    function &getDependency2Object(&$c, $i, $p, $s)
    {
        $z = &test_PEAR_Dependency2::singleton($c, $i, $p, $s);
        return $z;
    }
}
?>