1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cerbere\Model\Hacked; |
4
|
|
|
|
5
|
|
|
use Cerbere\Model\Project; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Base class for downloading remote versions of projects. |
9
|
|
|
*/ |
10
|
|
|
class HackedProjectWebDownloader { |
11
|
|
|
/** |
12
|
|
|
* @var Project |
13
|
|
|
*/ |
14
|
|
|
protected $project; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Constructor, pass in the project this downloaded is expected to download. |
18
|
|
|
* @param Project $project |
19
|
|
|
*/ |
20
|
|
|
public function __construct(Project $project) { |
21
|
|
|
$this->project = $project; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Returns a temp directory to work in. |
26
|
|
|
* |
27
|
|
|
* @param string $namespace |
28
|
|
|
* The optional namespace of the temp directory, defaults to the classname. |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
|
|
protected function getTempDirectory($namespace = NULL) { |
32
|
|
|
if (null === $namespace) { |
33
|
|
|
$namespace = get_class($this); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$dir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'hacked-cache'; |
37
|
|
|
|
38
|
|
|
if (!empty($namespace)) { |
39
|
|
|
$dir .= DIRECTORY_SEPARATOR . preg_replace('/[^0-9A-Z\-_]/i', '', $namespace); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
@mkdir($dir, 0775, TRUE); |
|
|
|
|
43
|
|
|
|
44
|
|
|
return $dir; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Returns a directory to save the downloaded project into. |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
|
protected function getDestination() { |
52
|
|
|
$type = $this->project->getProjectType(); |
53
|
|
|
$name = $this->project->getProject(); |
54
|
|
|
$version = $this->project->getVersion(); |
55
|
|
|
|
56
|
|
|
$dir = $this->getTempDirectory() . DIRECTORY_SEPARATOR . $type . DIRECTORY_SEPARATOR . $name; |
57
|
|
|
|
58
|
|
|
// Build the destination folder tree if it doesn't already exists. |
59
|
|
|
@mkdir($dir, 0775, TRUE); |
|
|
|
|
60
|
|
|
|
61
|
|
|
return $dir . DIRECTORY_SEPARATOR . $version; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Returns the final destination of the unpacked project. |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
public function getFinalDestination() { |
69
|
|
|
$dir = $this->getDestination(); |
70
|
|
|
|
71
|
|
|
return $dir; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Download the remote files to the local filesystem. |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
|
|
public function downloadFile() { |
79
|
|
|
return true; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Recursively delete all files and folders in the specified filepath, then |
84
|
|
|
* delete the containing folder. |
85
|
|
|
* |
86
|
|
|
* Note that this only deletes visible files with write permission. |
87
|
|
|
* |
88
|
|
|
* @param string $path |
89
|
|
|
* A filepath relative to file_directory_path. |
90
|
|
|
*/ |
91
|
|
|
protected function removeDir($path) { |
92
|
|
|
if (is_file($path) || is_link($path)) { |
93
|
|
|
@unlink($path); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
elseif (is_dir($path)) { |
96
|
|
|
$d = dir($path); |
97
|
|
|
|
98
|
|
|
while (($entry = $d->read()) !== FALSE) { |
99
|
|
|
if ($entry == '.' || $entry == '..') { |
100
|
|
|
continue; |
101
|
|
|
} |
102
|
|
|
$entry_path = $path . DIRECTORY_SEPARATOR . $entry; |
103
|
|
|
$this->removeDir($entry_path); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$d->close(); |
107
|
|
|
@rmdir($path); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: