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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
<?php
/**
* Copyright (C) Piwik PRO - All rights reserved.
*
* Using this code requires that you first get a license from Piwik PRO.
* Unauthorized copying of this file, via any medium is strictly prohibited.
*
* @link http://piwik.pro
*/
namespace Piwik\Plugins\FileSynchronizer\SyncFiles;
use Piwik\Container\StaticContainer;
use Piwik\Db;
use Piwik\Filechecks;
use Piwik\Filesystem;
use Psr\Log\LoggerInterface;
class Copy
{
/**
* @var LoggerInterface
*/
private $logger;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
/**
* Copies the given source file to the target directory.
*
* @param string $sourceFile
* @param string $targetDir
* @param string $copyCommandTemplate eg 'cp $source $directory' or 'scp $source user@host:$target'
* @return Result
*/
public function copy($sourceFile, $targetDir, $copyCommandTemplate)
{
$command = $this->buildCommandToCopyFile($sourceFile, $targetDir, $copyCommandTemplate);
$this->logger->debug("Executing command '$command' to copy '$sourceFile' to '$targetDir'");
try {
exec($command . ' 2>&1', $output, $exitCode);
$output = implode("\n", $output);
} catch (\Exception $e) {
$output = $e->getMessage() . ' Trace: ' . $e->getTraceAsString();
$exitCode = 255;
}
$this->logger->debug("Finished copying '$sourceFile'. Exit code: '$exitCode', output: '$output'");
return new Result($command, $output, $exitCode);
}
/**
* Similar to {@link copy()} but should be used if there is no file that can be copied but content instead.
*
* @param string $filename The name of the file that shall be created in the target directory. eg 'test.hash'
* @param string $content The content of the file, eg '123456'
* @param string $targetDir
* @param string $copyCommandTemplate
* @return Result
*/
public function copyContent($filename, $content, $targetDir, $copyCommandTemplate)
{
$tmp = StaticContainer::get('path.tmp');
Filechecks::dieIfDirectoriesNotWritable(array($tmp));
$file = $tmp . DIRECTORY_SEPARATOR . $filename;
file_put_contents($file, $content);
$result = $this->copy($file, $targetDir, $copyCommandTemplate);
Filesystem::remove($file);
return $result;
}
private function buildCommandToCopyFile($sourceFile, $targetDir, $copyCommandTemplate)
{
$search = array('$source', '$target');
$replace = array(escapeshellarg($sourceFile), escapeshellarg($targetDir));
return str_replace($search, $replace, $copyCommandTemplate);
}
}
|