Completed
Pull Request — master (#140)
by
unknown
03:01
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\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
     * Setup the Ftp sync.
22
     *
23
     * @param  array $config
24
     * @throws \phpbu\App\Backup\Sync\Exception
25
     */
26 6
    public function setup(array $config)
27
    {
28 6
        $path = Arr::getValue($config, 'path', '');
29 6
        if ('/' === substr($path, 0, 1)) {
30 1
            throw new Exception('absolute path is not allowed');
31
        }
32 5
        parent::setup($config);
33 2
    }
34
35
    /**
36
     * Check for required loaded libraries or extensions.
37
     *
38
     * @throws \phpbu\App\Backup\Sync\Exception
39
     */
40 5
    protected function checkRequirements()
41
    {
42 5
        if (!function_exists('ftp_connect')) {
43
            throw new Exception('ftp functions not enabled');
44
        }
45 5
    }
46
47
    /**
48
     * Return implemented (*)TP protocol name.
49
     *
50
     * @return string
51
     */
52 1
    protected function getProtocolName()
53
    {
54 1
        return 'FTP';
55
    }
56
57
    /**
58
     * (non-PHPDoc)
59
     *
60
     * @see    \phpbu\App\Backup\Sync::sync()
61
     * @param  \phpbu\App\Backup\Target $target
62
     * @param  \phpbu\App\Result        $result
63
     * @throws \phpbu\App\Backup\Sync\Exception
64
     */
65
    public function sync(Target $target, Result $result)
66
    {
67
        // silence ftp errors
68
        $old  = error_reporting(0);
69
        if (!$ftpConnection = ftp_connect($this->host)) {
70
            throw new Exception(
71
                sprintf(
72
                    'Unable to connect to ftp server %s',
73
                    $this->host
74
                )
75
            );
76
        }
77
78 View Code Duplication
        if (!ftp_login($ftpConnection, $this->user, $this->password)) {
79
            error_reporting($old);
80
            throw new Exception(
81
                sprintf(
82
                    'authentication failed for %s@%s%s',
83
                    $this->user,
84
                    $this->host,
85
                    empty($this->password) ? '' : ' with password ****'
86
                )
87
            );
88
        }
89
90
        $remoteFilename = $target->getFilename();
91
        $localFile      = $target->getPathname();
92
93
        if ('' !== $this->remotePath) {
94
            $remoteDirs = explode('/', $this->remotePath);
95
            foreach ($remoteDirs as $dir) {
96
                if (!ftp_chdir($ftpConnection, $dir)) {
97
                    $result->debug(sprintf('creating remote dir \'%s\'', $dir));
98
                    ftp_mkdir($ftpConnection, $dir);
99
                    ftp_chdir($ftpConnection, $dir);
100
                } else {
101
                    $result->debug(sprintf('change to remote dir \'%s\'', $dir));
102
                }
103
            }
104
        }
105
        $result->debug(sprintf('store file \'%s\' as \'%s\'', $localFile, $remoteFilename));
106
107
        if (!ftp_put($ftpConnection, $remoteFilename, $localFile, FTP_BINARY)) {
108
            $error = error_get_last();
109
            $message = $error['message'];
110
            throw new Exception(sprintf('error uploading file: %s - %s', $localFile, $message));
111
        }
112
113
        error_reporting($old);
114
    }
115
116
    /**
117
     * Execute the remote clean up if needed
118
     *
119
     * @param \phpbu\App\Backup\Target $target
120
     * @param \phpbu\App\Result        $result
121
     */
122
    public function cleanup(Target $target, Result $result)
123
    {
124
        // TODO: Implement cleanup() method.
125
    }
126
}
127