1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cerbere\Model\Hacked; |
4
|
|
|
|
5
|
|
|
use splitbrain\PHPArchive\Archive; |
6
|
|
|
use splitbrain\PHPArchive\Tar; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Downloads a project using a standard Drupal method. |
10
|
|
|
*/ |
11
|
|
|
class HackedProjectWebFilesDownloader extends HackedProjectWebDownloader { |
12
|
|
|
/** |
13
|
|
|
* @return string|false |
14
|
|
|
*/ |
15
|
|
|
public function getDownloadLink() { |
16
|
|
|
$existing_version = $this->project->getVersion(); |
17
|
|
|
|
18
|
|
|
// Remove 'dev' tailing flag from version name. |
19
|
|
|
if (preg_match('/(\+\d+\-dev)$/', $existing_version)) { |
20
|
|
|
$existing_version = preg_replace('/(\+\d+\-dev)$/', '', $existing_version); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
if ($release = $this->project->getRelease($existing_version)) { |
24
|
|
|
return $release->getDownloadLink(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
return false; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return bool|string |
32
|
|
|
*/ |
33
|
|
|
public function downloadFile() { |
34
|
|
|
$dir = $this->getDestination(); |
35
|
|
|
|
36
|
|
|
if (!($release_url = $this->getDownloadLink())) { |
37
|
|
|
return FALSE; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
// If our directory already exists, we can just return the path to this cached version |
41
|
|
|
$whiteList = array('.', '..', 'CVS', '.svn', '.git'); |
42
|
|
|
if (file_exists($dir) && count(HackedFileGroup::scanDirectory($dir, '/.*/', $whiteList)) |
43
|
|
|
) { |
44
|
|
|
return $dir; |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
// Build the destination folder tree if it doesn't already exists. |
48
|
|
|
@mkdir($dir, 0775, TRUE); |
|
|
|
|
49
|
|
|
|
50
|
|
|
if (!($local_file = $this->getFile($release_url))) { |
51
|
|
|
return FALSE; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
try { |
55
|
|
|
$this->extractArchive($local_file, $dir); |
56
|
|
|
} |
57
|
|
|
catch (\Exception $e) { |
58
|
|
|
echo $e->getMessage() . "\n"; |
59
|
|
|
return FALSE; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return TRUE; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Copies a file from $url to the temporary directory for updates. |
67
|
|
|
* |
68
|
|
|
* If the file has already been downloaded, returns the the local path. |
69
|
|
|
* |
70
|
|
|
* @param $url |
71
|
|
|
* The URL of the file on the server. |
72
|
|
|
* |
73
|
|
|
* @return string |
74
|
|
|
* Path to local file. |
75
|
|
|
*/ |
76
|
|
|
protected function getFile($url) { |
77
|
|
|
$parsed_url = parse_url($url); |
78
|
|
|
$remote_schemes = array('http', 'https', 'ftp', 'ftps', 'smb', 'nfs'); |
79
|
|
|
|
80
|
|
|
if (!in_array($parsed_url['scheme'], $remote_schemes)) { |
81
|
|
|
// This is a local file, just return the path. |
82
|
|
|
return realpath($url); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// Todo: use Symfony's cache objects |
86
|
|
|
// Check the cache and download the file if needed. |
87
|
|
|
$cache_directory = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'hacked-cache'; |
88
|
|
|
$local = $cache_directory . DIRECTORY_SEPARATOR . basename($parsed_url['path']); |
89
|
|
|
|
90
|
|
|
if (!file_exists($cache_directory)) { |
91
|
|
|
@mkdir($cache_directory, 0775, true); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// Todo: use guzzle. |
95
|
|
|
$content = file_get_contents($url); |
96
|
|
|
|
97
|
|
|
if ($content !== false && file_put_contents($local, $content)) { |
98
|
|
|
return $local; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return false; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Unpack a downloaded archive file. |
106
|
|
|
* |
107
|
|
|
* @param string $file |
108
|
|
|
* The filename of the archive you wish to extract. |
109
|
|
|
* @param string $directory |
110
|
|
|
* The directory you wish to extract the archive into. |
111
|
|
|
* @return Archive |
112
|
|
|
* The Archiver object used to extract the archive. |
113
|
|
|
* @throws \Exception on failure. |
114
|
|
|
*/ |
115
|
|
|
protected function extractArchive($file, $directory) { |
116
|
|
|
$archiver = new Tar(); |
117
|
|
|
|
118
|
|
|
// Remove the directory if it exists, otherwise it might contain a mixture of |
119
|
|
|
// old files mixed with the new files (e.g. in cases where files were removed |
120
|
|
|
// from a later release). |
121
|
|
|
$archiver->open($file); |
122
|
|
|
$files = $archiver->contents(); |
123
|
|
|
|
124
|
|
|
// First entry contains the root folder. |
125
|
|
|
$project_path = $files[0]->getPath(); |
126
|
|
|
|
127
|
|
|
if (file_exists($directory)) { |
128
|
|
|
$this->removeDir($directory); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
// Reopen archive to extract all files. |
132
|
|
|
$archiver->open($file); |
133
|
|
|
// Strip first folder level. |
134
|
|
|
$archiver->extract($directory, $project_path); |
135
|
|
|
|
136
|
|
|
return $archiver; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.