Skip to content

Commit 40314a6

Browse files
authored
Merge pull request #3 from Riaz-Mahmud/dev
download improve, Bug fix
2 parents ee34493 + ee39d96 commit 40314a6

File tree

4 files changed

+227
-118
lines changed

4 files changed

+227
-118
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Abdul Al Mahmud Riaz
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function siteBackup(){
3434

3535
```php
3636

37-
array:2 [▼[
37+
array:3 [▼[
3838
'error' => false,
3939
'message' => 'Backup created successfully',
4040
'path' => 'your_given_path/index.html'
@@ -58,7 +58,7 @@ function siteBackup(){
5858
$websiteBackup = new WebsiteBackup();
5959
$backup = $websiteBackup->backup($url, $path);
6060

61-
if($data['error'] == false){
61+
if(!$data['error']){
6262
echo $data['path'];
6363
}
6464
}
@@ -67,7 +67,7 @@ function siteBackup(){
6767

6868
## License
6969

70-
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
70+
The MIT License (MIT). Please see [License](LICENSE) for more information.
7171

7272

7373
## Security

src/WebsiteBackup.php

Lines changed: 115 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,83 +2,91 @@
22

33
namespace Backdoor\WebsiteBackup;
44

5-
use Backdoor\WebsiteBackup\WebsiteBackupHelper;
6-
use Illuminate\Support\Facades\Log;
5+
use Backdoor\WebsiteBackup\WebsiteBackupHelper as Helper;
76

87
class WebsiteBackup {
98

109
protected $url = NULL;
1110
protected $path = NULL;
11+
protected $defaultElement = 'websitebackupuuid';
1212

1313
protected $data = [
1414
'error' => false,
1515
'message' => '',
1616
'path' => '',
1717
];
1818

19+
protected $elements = [
20+
'img' => [
21+
'attribute' => 'src',
22+
'folder' => '/images'.'/',
23+
],
24+
'link' => [
25+
'attribute' => 'href',
26+
'folder' => '/css'.'/',
27+
],
28+
'script' => [
29+
'attribute' => 'src',
30+
'folder' => '/js'.'/',
31+
],
32+
'source' => [
33+
'attribute' => 'src',
34+
'folder' => '/source'.'/',
35+
],
36+
'div' => [
37+
'attribute' => 'data-bg',
38+
'folder' => '/div'.'/',
39+
],
40+
];
41+
42+
protected $downloadQueue = [];
43+
protected $maxExecutionTime = 30;
44+
1945
public function backup($url , $path){
2046

2147
$this->url = $url;
2248
$this->path = $path;
49+
Helper::generateFolder($this->path);
50+
Helper::setFolderPath( $this->path );
51+
Helper::logEntry('Info: Backup started for '.$this->url);
2352

24-
$baseUrl = WebsiteBackupHelper::getDomain( $this->url );
53+
$baseUrl = Helper::getDomain( $this->url );
54+
Helper::logEntry('Info: Base URL: '.$baseUrl);
2555

2656
$ch = curl_init();
27-
2857
curl_setopt($ch, CURLOPT_URL, $this->url);
2958
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
3059

3160
$status = http_response_code( curl_getinfo($ch, CURLINFO_HTTP_CODE) );
61+
Helper::logEntry('Info: '. 'Site Status: '.$status);
3262
if ($status != 200) {
3363
$this->data['error'] = true;
3464
$this->data['message'] = "Site is not available or not found. Please check the URL. ErrorCode: $status";
65+
Helper::logEntry('Error '. $this->data['message']);
3566
return $this->data;
3667
}
3768

38-
WebsiteBackupHelper::generateFolder($this->path);
39-
4069
$output = curl_exec($ch);
4170

4271
$dom = new \DOMDocument();
4372
@$dom->loadHTML($output);
73+
set_time_limit(0);
4474

45-
$elements = [
46-
'img' => [
47-
'attribute' => 'src',
48-
'folder' => '/images'.'/',
49-
],
50-
'link' => [
51-
'attribute' => 'href',
52-
'folder' => '/css'.'/',
53-
],
54-
'script' => [
55-
'attribute' => 'src',
56-
'folder' => '/js'.'/',
57-
],
58-
'source' => [
59-
'attribute' => 'src',
60-
'folder' => '/source'.'/',
61-
],
62-
'div' => [
63-
'attribute' => 'data-bg',
64-
'folder' => '/div'.'/',
65-
],
66-
];
67-
68-
foreach ($elements as $element => $value) {
75+
foreach ($this->elements as $element => $value) {
6976
$links = $dom->getElementsByTagName($element);
7077
if($links->length > 0){
7178
$folderName = $path.$value['folder'];
72-
WebsiteBackupHelper::generateFolder($folderName);
79+
Helper::generateFolder($folderName);
7380
if($element == 'img' || $element == 'div'){
74-
$this->imageDownload($links , $baseUrl , $folderName);
81+
$this->image($links , $baseUrl , $folderName);
7582
}else{
76-
$this->commonDownload($links, $value['attribute'], $baseUrl, $folderName);
83+
$this->common($links, $value['attribute'], $baseUrl, $folderName);
7784
}
7885
}
7986
}
8087

8188
$html = $dom->saveHTML();
89+
$html = $this->downloadContent($html);
8290
file_put_contents($path.'/index.html', $html);
8391

8492
curl_close($ch);
@@ -87,45 +95,101 @@ public function backup($url , $path){
8795
$this->data['message'] = 'Backup created successfully';
8896
$this->data['path'] = $path . '/index.html';
8997

98+
Helper::logEntry('Info: '. $this->data['message']. ' Path: '.$this->data['path'] );
99+
100+
Helper::logEntry('Download Queue: Unsuccesful download links =>');
101+
foreach ($this->downloadQueue as $value) {
102+
if($value['status'] == 'pending'){
103+
Helper::logEntry('Pending Queue: link:'. $value['url'] . ' status:'. $value['status']);
104+
}
105+
}
106+
107+
set_time_limit(30);
108+
90109
return $this->data;
91110
}
92111

93-
protected function commonDownload($list, $attribute , $baseUrl , $folderName){
112+
protected function downloadContent($html){
113+
$dom = new \DOMDocument();
114+
@$dom->loadHTML($html);
115+
116+
foreach ($this->elements as $element => $value) {
117+
$items = $dom->getElementsByTagName($element);
118+
if($items->length > 0){
119+
foreach($items as $item){
120+
foreach($this->downloadQueue as $key => $queue){
121+
if( $item->hasAttribute($this->defaultElement) && isset($queue['uuid']) && $item->getAttribute($this->defaultElement) == $queue['uuid'] ){
122+
$saveFile = Helper::downloadFile($queue['url'] , $queue['folder']);
123+
if($saveFile['status']){
124+
$this->downloadQueue[$key]['status'] = 'success';
125+
if($queue['type'] == 'image'){
126+
$item->removeAttribute($this->defaultElement);
127+
$item = Helper::replaceImageURL($item , '/'.$saveFile['file_path']);
128+
}else{
129+
$item->removeAttribute($this->defaultElement);
130+
$item->setAttribute($queue['attribute'] , '/'.$saveFile['file_path']);
131+
}
132+
}
133+
}
134+
}
135+
}
136+
}
137+
}
138+
$html = $dom->saveHTML();
139+
return $html;
140+
}
141+
142+
protected function common($list, $attribute , $baseUrl , $folderName){
94143
foreach ($list as $item) {
95144
if($item->hasAttribute($attribute)){
96145
$url = $item->getAttribute($attribute);
97146
if($url){
147+
$url = str_replace(' ', '%20', trim($url));
98148
try{
99-
if(WebsiteBackupHelper::isFullURL($url) === false){
100-
$url = $baseUrl . WebsiteBackupHelper::checkFirstSlash($url);
149+
if(Helper::isFullURL($url) === false){
150+
$url = $baseUrl . Helper::checkFirstSlash($url);
101151
}
152+
$uuid = Helper::generateUUID();
153+
154+
$this->downloadQueue[] = [
155+
'url' => Helper::checkHttpWWW($url),
156+
'path' => $url,
157+
'folder' => $folderName,
158+
'attribute' => $attribute,
159+
'type' => 'common',
160+
'uuid' => $uuid,
161+
'status' => 'pending',
162+
];
163+
$item->setAttribute($this->defaultElement, $uuid);
102164

103-
$file_path = $folderName . WebsiteBackupHelper::replaceFileName( WebsiteBackupHelper::fileName($url), $folderName);
104-
$saveFile = WebsiteBackupHelper::downloadFile($url , $file_path);
105-
if($saveFile){
106-
$item->removeAttribute($attribute);
107-
$item->setAttribute($attribute, '/'.$file_path);
108-
}
109165
}catch(\Exception $e){
110-
Log::error($e->getMessage());
166+
Helper::logEntry('Error: '.$e->getMessage());
111167
}
112168
}
113169
}
114170
}
115171
}
116172

117-
protected function imageDownload($images , $baseUrl , $folderName){
173+
protected function image($images , $baseUrl , $folderName){
118174
foreach ($images as $image) {
119-
$src = WebsiteBackupHelper::findImageURl($image , $baseUrl);
175+
$src = Helper::findImageURl($image , $baseUrl);
120176
if($src != false){
177+
$src = str_replace(' ', '%20', trim($src));
121178
try{
122-
$file_path = $folderName . WebsiteBackupHelper::replaceFileName(WebsiteBackupHelper::fileName($src), $folderName);
123-
$saveFile = WebsiteBackupHelper::downloadFile($src , $file_path);
124-
if($saveFile){
125-
$image = WebsiteBackupHelper::replaceImageURL($image, '/'.$file_path);
126-
}
179+
$uuid = Helper::generateUUID();
180+
181+
$this->downloadQueue[] = [
182+
'url' => Helper::checkHttpWWW($src),
183+
'path' => $src,
184+
'folder' => $folderName,
185+
'type' => 'image',
186+
'uuid' => $uuid,
187+
'status' => 'pending',
188+
];
189+
190+
$image->setAttribute($this->defaultElement, $uuid);
127191
}catch(\Exception $e){
128-
Log::info("WebsiteBackup: video: ".$e->getMessage());
192+
Helper::logEntry('Error: '. $e->getMessage());
129193
}
130194
}
131195
}

0 commit comments

Comments
 (0)