Completed
Pull Request — master (#140)
by
unknown
09:01
created

Ftp::getProtocolName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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