2
2
3
3
namespace Backdoor \WebsiteBackup ;
4
4
5
- use Backdoor \WebsiteBackup \WebsiteBackupHelper ;
6
- use Illuminate \Support \Facades \Log ;
5
+ use Backdoor \WebsiteBackup \WebsiteBackupHelper as Helper ;
7
6
8
7
class WebsiteBackup {
9
8
10
9
protected $ url = NULL ;
11
10
protected $ path = NULL ;
11
+ protected $ defaultElement = 'websitebackupuuid ' ;
12
12
13
13
protected $ data = [
14
14
'error ' => false ,
15
15
'message ' => '' ,
16
16
'path ' => '' ,
17
17
];
18
18
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
+
19
45
public function backup ($ url , $ path ){
20
46
21
47
$ this ->url = $ url ;
22
48
$ this ->path = $ path ;
49
+ Helper::generateFolder ($ this ->path );
50
+ Helper::setFolderPath ( $ this ->path );
51
+ Helper::logEntry ('Info: Backup started for ' .$ this ->url );
23
52
24
- $ baseUrl = WebsiteBackupHelper::getDomain ( $ this ->url );
53
+ $ baseUrl = Helper::getDomain ( $ this ->url );
54
+ Helper::logEntry ('Info: Base URL: ' .$ baseUrl );
25
55
26
56
$ ch = curl_init ();
27
-
28
57
curl_setopt ($ ch , CURLOPT_URL , $ this ->url );
29
58
curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
30
59
31
60
$ status = http_response_code ( curl_getinfo ($ ch , CURLINFO_HTTP_CODE ) );
61
+ Helper::logEntry ('Info: ' . 'Site Status: ' .$ status );
32
62
if ($ status != 200 ) {
33
63
$ this ->data ['error ' ] = true ;
34
64
$ this ->data ['message ' ] = "Site is not available or not found. Please check the URL. ErrorCode: $ status " ;
65
+ Helper::logEntry ('Error ' . $ this ->data ['message ' ]);
35
66
return $ this ->data ;
36
67
}
37
68
38
- WebsiteBackupHelper::generateFolder ($ this ->path );
39
-
40
69
$ output = curl_exec ($ ch );
41
70
42
71
$ dom = new \DOMDocument ();
43
72
@$ dom ->loadHTML ($ output );
73
+ set_time_limit (0 );
44
74
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 ) {
69
76
$ links = $ dom ->getElementsByTagName ($ element );
70
77
if ($ links ->length > 0 ){
71
78
$ folderName = $ path .$ value ['folder ' ];
72
- WebsiteBackupHelper ::generateFolder ($ folderName );
79
+ Helper ::generateFolder ($ folderName );
73
80
if ($ element == 'img ' || $ element == 'div ' ){
74
- $ this ->imageDownload ($ links , $ baseUrl , $ folderName );
81
+ $ this ->image ($ links , $ baseUrl , $ folderName );
75
82
}else {
76
- $ this ->commonDownload ($ links , $ value ['attribute ' ], $ baseUrl , $ folderName );
83
+ $ this ->common ($ links , $ value ['attribute ' ], $ baseUrl , $ folderName );
77
84
}
78
85
}
79
86
}
80
87
81
88
$ html = $ dom ->saveHTML ();
89
+ $ html = $ this ->downloadContent ($ html );
82
90
file_put_contents ($ path .'/index.html ' , $ html );
83
91
84
92
curl_close ($ ch );
@@ -87,45 +95,101 @@ public function backup($url , $path){
87
95
$ this ->data ['message ' ] = 'Backup created successfully ' ;
88
96
$ this ->data ['path ' ] = $ path . '/index.html ' ;
89
97
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
+
90
109
return $ this ->data ;
91
110
}
92
111
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 ){
94
143
foreach ($ list as $ item ) {
95
144
if ($ item ->hasAttribute ($ attribute )){
96
145
$ url = $ item ->getAttribute ($ attribute );
97
146
if ($ url ){
147
+ $ url = str_replace (' ' , '%20 ' , trim ($ url ));
98
148
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 );
101
151
}
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 );
102
164
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
- }
109
165
}catch (\Exception $ e ){
110
- Log:: error ( $ e ->getMessage ());
166
+ Helper:: logEntry ( ' Error: ' . $ e ->getMessage ());
111
167
}
112
168
}
113
169
}
114
170
}
115
171
}
116
172
117
- protected function imageDownload ($ images , $ baseUrl , $ folderName ){
173
+ protected function image ($ images , $ baseUrl , $ folderName ){
118
174
foreach ($ images as $ image ) {
119
- $ src = WebsiteBackupHelper ::findImageURl ($ image , $ baseUrl );
175
+ $ src = Helper ::findImageURl ($ image , $ baseUrl );
120
176
if ($ src != false ){
177
+ $ src = str_replace (' ' , '%20 ' , trim ($ src ));
121
178
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 );
127
191
}catch (\Exception $ e ){
128
- Log:: info ( " WebsiteBackup: video: " . $ e ->getMessage ());
192
+ Helper:: logEntry ( ' Error: ' . $ e ->getMessage ());
129
193
}
130
194
}
131
195
}
0 commit comments