forked from xPh03n1x/Travian-T4.6-Gpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimageDownloader.php
executable file
·94 lines (67 loc) · 2.25 KB
/
imageDownloader.php
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
90
91
92
93
94
<?php
new class($argv){
private $cdn='https://gpack.travian.com/c3f4a986/';
private $initDir;
private $scriptsToParse=array();
private $imagesToFetch=array();
private $downloadedImages=0;
private $overWriteExistingFiles=false; // Download already downloaded files
private $request,$cssFile,$css;
public function __construct($args){
if(!file_exists($args[1])){
die("The requested CSS file doesn't exist!\n");
}
$this->initDir=__DIR__.'/';
$this->request=pathinfo($args[1]);
$this->cssFile=$this->request['basename'];
array_push($this->scriptsToParse, $this->cssFile);
chdir($this->request['dirname']);
$this->parseScript();
}
private function findImportedScripts(){
preg_match_all('/\@import.*\"(.*)\"\;/m', $this->css, $matches, PREG_SET_ORDER, 0);
if(!empty($matches)){
foreach($matches as $m=>$d){
if(isset($d[1])){
array_push($this->scriptsToParse, $d[1]);
}
}
}
echo $this->cssFile.": Found additional ".count($matches)." scripts to parse!\n";
}
private function parseScript($pos = 0){
$this->cssFile=$this->scriptsToParse[$pos];
$this->css=file_get_contents($this->scriptsToParse[$pos]);
$this->findImportedScripts();
preg_match_all('/\'\.\.\/(.*?)\'/i', $this->css, $matches);
if(!empty($matches[1])){
echo "Found total of ".count($matches[1])." links. Fetching ...\n";
$this->processFoundURLs($matches[1]);
}
// ...
$next=$pos+1;
if($next<count($this->scriptsToParse)){
return $this->parseScript($next);
}
echo "Downloaded a total of ".$this->downloadedImages." images!\n";
}
private function processFoundURLs($urls){
foreach($urls as $k=>$url){
$temp=pathinfo($url);
$relPath=str_replace($this->initDir,'', realpath('../')).'/';
$fullPath=$this->initDir.$relPath.$temp['dirname'].'/';
$fullURL=$this->cdn.$relPath.$url;
$targetFile=$fullPath.$temp['basename'];
if(!file_exists($targetFile) || $this->overWriteExistingFiles){
# Ensure the target directory exists
$this->make_dir($fullPath);
# Download
shell_exec('wget '.$fullURL.' -o '.$this->initDir.'/dl.log'.' -O '.$targetFile);
$this->downloadedImages++;
}
}
}
private function make_dir($path, $permissions=0755){
return is_dir($path) || mkdir($path, $permissions, true);
}
};