Completed
Pull Request — master (#140)
by
unknown
04:37
created

Ftp::cleanup()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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