Jacek Kowalski
2019-06-30 bcd661488de087afab096c18aa55eda42e8c5226
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
<?php
class Fetch {
    static function ftp($url, $file = NULL) {
        $url = parse_url($url);
        if(!isset($url['scheme']) || $url['scheme'] != 'ftp') {
            throw new Exception('Only FTP URLs are supported');
        }
        if(!isset($url['host'])) {
            throw new Exception('Hostname not present in the URL');
        }
        if(!isset($url['path'])) {
            throw new Exception('Path component not present in the URL');
        }
        if(!isset($url['port'])) {
            $url['port'] = 21;
        }
        if(!isset($url['user'])) {
            $url['user'] = 'anonymous';
        }
        if(!isset($url['pass'])) {
            $url['pass'] = 'anonymous@mpk.jacekk.net';
        }
        if($file == NULL) {
            $file = basename($url['path']);
        }
        
        $localTime = -1;
        $localSize = -1;
        if(is_file($file)) {
            $localTime = filemtime($file);
            $localSize = filesize($file);
        }
        
        $ftp = FtpConnection::create($url['host'], $url['port'], $url['user'], $url['pass']);
        $remoteSize = $ftp->size($url['path']);
        $remoteTime = $ftp->mdtm($url['path']);
        
        $updated = FALSE;
        
        if($localTime < $remoteTime || ($localTime == $remoteTime && $localSize != $remoteSize)) {
            if(file_exists($file.'.tmp')) {
                unlink($file.'.tmp');
            }
            $ftp->get($file.'.tmp', $url['path'], FTP_BINARY);
            touch($file.'.tmp', $remoteTime);
            if(!rename($file.'.tmp', $file)) {
                throw new Exception('Temporary file rename failed');
            }
            $updated = TRUE;
        }
        
        return $updated;
    }
    
    static function generic($url, $file = NULL) {
        if($file == NULL) {
            $file = basename($url['url']);
        }
        $data = file_get_contents($url);
        if($data === FALSE) {
            throw new Exception('URL fetch failed');
        }
        if(file_put_contents($file.'.tmp', $data) === FALSE) {
            throw new Exception('Temporary file creation failed');
        }
        if(!rename($file.'.tmp', $file)) {
            throw new Exception('Temporary file rename failed');
        }
    }
}