Completed
Pull Request — master (#140)
by
unknown
03:00
created

Ftp::setup()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
namespace phpbu\App\Backup\Sync;
3
4
use phpbu\App\Backup\File\Remote;
5
use phpbu\App\Result;
6
use phpbu\App\Backup\Sync;
7
use phpbu\App\Backup\Target;
8
use phpbu\App\Util\Arr;
9
use phpbu\App\Util\Str;
10
11
/**
12
 * Ftp sync
13
 *
14
 * @package    phpbu
15
 * @subpackage Backup
16
 * @author     Chris Hawes <[email protected]>
17
 * @copyright  Sebastian Feldmann <[email protected]>
18
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
19
 * @link       http://phpbu.de/
20
 */
21
class Ftp extends Xtp implements Simulator
22
{
23
    /**
24
     * Setup the Ftp sync.
25
     *
26
     * @param  array $config
27
     * @throws \phpbu\App\Backup\Sync\Exception
28
     */
29 6
    public function setup(array $config)
30
    {
31 6
        $path = Arr::getValue($config, 'path', '');
32 6
        if ('/' === substr($path, 0, 1)) {
33 1
            throw new Exception('absolute path is not allowed');
34
        }
35 5
        parent::setup($config);
36 2
    }
37
38
    /**
39
     * Check for required loaded libraries or extensions.
40
     *
41
     * @throws \phpbu\App\Backup\Sync\Exception
42
     */
43 5
    protected function checkRequirements()
44
    {
45 5
        if (!function_exists('ftp_connect')) {
46
            throw new Exception('ftp functions not enabled');
47
        }
48 5
    }
49
50
    /**
51
     * Return implemented (*)TP protocol name.
52
     *
53
     * @return string
54
     */
55 1
    protected function getProtocolName()
56
    {
57 1
        return 'FTP';
58
    }
59
60
    /**
61
     * (non-PHPDoc)
62
     *
63
     * @see    \phpbu\App\Backup\Sync::sync()
64
     * @param  \phpbu\App\Backup\Target $target
65
     * @param  \phpbu\App\Result        $result
66
     * @throws \phpbu\App\Backup\Sync\Exception
67
     */
68
    public function sync(Target $target, Result $result)
69
    {
70
        // silence ftp errors
71
        $old  = error_reporting(0);
72
        if (!$ftpConnection = ftp_connect($this->host)) {
73
            throw new Exception(
74
                sprintf(
75
                    'Unable to connect to ftp server %s',
76
                    $this->host
77
                )
78
            );
79
        }
80
81 View Code Duplication
        if (!ftp_login($ftpConnection, $this->user, $this->password)) {
82
            error_reporting($old);
83
            throw new Exception(
84
                sprintf(
85
                    'authentication failed for %s@%s%s',
86
                    $this->user,
87
                    $this->host,
88
                    empty($this->password) ? '' : ' with password ****'
89
                )
90
            );
91
        }
92
93
        $remoteFilename = $target->getFilename();
94
        $localFile      = $target->getPathname();
95
96
        if ('' !== $this->remotePath) {
97
            $remoteDirs = explode('/', $this->remotePath);
98
            foreach ($remoteDirs as $dir) {
99
                if (!ftp_chdir($ftpConnection, $dir)) {
100
                    $result->debug(sprintf('creating remote dir \'%s\'', $dir));
101
                    ftp_mkdir($ftpConnection, $dir);
102
                    ftp_chdir($ftpConnection, $dir);
103
                } else {
104
                    $result->debug(sprintf('change to remote dir \'%s\'', $dir));
105
                }
106
            }
107
        }
108
        $result->debug(sprintf('store file \'%s\' as \'%s\'', $localFile, $remoteFilename));
109
110
        if (!ftp_put($ftpConnection, $remoteFilename, $localFile, FTP_BINARY)) {
111
            $error = error_get_last();
112
            $message = $error['message'];
113
            throw new Exception(sprintf('error uploading file: %s - %s', $localFile, $message));
114
        }
115
116
        error_reporting($old);
117
    }
118
119
    /**
120
     * Execute the remote clean up if needed
121
     *
122
     * @param \phpbu\App\Backup\Target $target
123
     * @param \phpbu\App\Result        $result
124
     */
125
    public function cleanup(Target $target, Result $result)
126
    {
127
        // TODO: Implement cleanup() method.
128
    }
129
}
130