-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.php
More file actions
259 lines (222 loc) · 9.24 KB
/
Copy pathprocess.php
File metadata and controls
259 lines (222 loc) · 9.24 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php
/**
* @author Nguyen Duc Hanh
* @copyright 2014
* @copygith nguyenduchanh.com
*/
session_start();
error_reporting(0);
include_once('simple_html_dom.php');
include_once('functions.php');
if(isset($_REQUEST['cmd']) && $_REQUEST['cmd']=='clear'){
session_destroy();
jsonResult(403, 'Đã xóa hết session');
die;
}
$url = $_SESSION['url'];
$folder = $_SESSION['folder'];
$save_file = $_SESSION['save_file'];
if($url==''){
jsonResult(403, 'url không hợp lệ');
die;
}
if($folder==''){
jsonResult(403, 'folder không hợp lệ');
die;
}
$path = "save/".$folder;
$css_path = "save/".$folder."/asset/css";
$js_path = "save/".$folder."/asset/js";
/*
step = 1; // download html content
step = 2; // get css and javascript file
step = 3; // save background images
step = 4; // update html content
step = 5; // finish
*/
if(!is_dir($path)){
mkdir($path, 0777, true);
}
if(!is_dir($css_path)){
mkdir($css_path, 0777, true);
}
if(!is_dir($js_path)){
mkdir($js_path, 0777, true);
}
$step = isset($_SESSION['step']) ? $_SESSION['step'] : 1 ;
switch($step){
case "1":
/* download html
----------------------------------------------------------------------------*/
$bytes = file_put_contents($path.'/tmp',get_file_content($url));
$step++;
$_SESSION['step'] = $step;
$byte = bytesToSize($bytes);
jsonResult(200, 'Tải cấu trúc html website', array('size' => $byte));
die;
break;
case "2":
/* get script and css
----------------------------------------------------------------------------*/
// clean css file
$website = file_get_html($path.'/tmp');
foreach ($website->find('link') as $stylesheet){
$stylesheet->rel = strtolower($stylesheet->rel);
}
//$website->save($path.'/tmp');
//$website = file_get_html($path.'/tmp');
$arrayCss = array();
$arrayCssNoClean = array();
foreach ($website->find('link[rel="stylesheet"]') as $stylesheet){
$stylesheet_url = cleanUrl($stylesheet->href,true);
$stylesheet_url_notclean = $stylesheet->href;
if($stylesheet_url!=''){
$stylesheet_url = (strpos($stylesheet_url,'//')===false) ? h_parse_url($_SESSION['url']).'/'.$stylesheet_url : $stylesheet_url;
$stylesheet_url_notclean = (strpos($stylesheet_url_notclean,'//')===false) ? h_parse_url($_SESSION['url']).'/'.$stylesheet_url_notclean : $stylesheet_url_notclean;
$arrayCss[] = $stylesheet_url;
$arrayCssNoClean[] = $stylesheet_url_notclean;
$path_info = pathinfo($stylesheet_url);
$file_path = $css_path."/".$path_info['filename'].".".$path_info['extension'];
file_put_contents($file_path,get_file_content($stylesheet->href));
$css_import = get_css_import($stylesheet_url_notclean);
if($css_import){
$arrayCss = array_merge($css_import,$arrayCss);
$arrayCssNoClean = array_merge($css_import,$arrayCssNoClean);
}
}
}
$_SESSION['arrayCss'] = $arrayCss;
$_SESSION['arrayCssNoClean'] = $arrayCssNoClean;
$arrayJs = array();
foreach ($website->find('script') as $script){
$script_url = cleanUrl($script->src);
$stylesheet_url = (strpos($script_url,'//')===false) ? h_parse_url($_SESSION['url']).'/'.$script_url : $script_url;
if($script_url){
$arrayJs[] = $script_url;
$path_info = pathinfo($script_url);
$file_path = $js_path."/".$path_info['filename'].".".$path_info['extension'];
file_put_contents($file_path,get_file_content($script->src));
//var_dump($file_path);
}
}
$step++;
$_SESSION['step'] = $step;
jsonResult(200, 'Tải các file css và javascript');
die;
break;
case "3":
/* save background images
----------------------------------------------------------------------------*/
$imgs = $_SESSION['img'];
$arrayCss = $_SESSION['arrayCss'];
$arrayCssNoClean = $_SESSION['arrayCssNoClean'];
if(count($imgs) > 0 ){
// remove duplicate value from images array
$imgs = array_unique($imgs);
$bg_images = array_pop($imgs);
if($bg_images){
//show_image_download($bg_images);
$images_info = pathinfo($bg_images);
$image_path = $css_path.'/'.$images_info['dirname'];
if(!is_dir($image_path)){
mkdir($image_path,0777,true);
}
$images_name = $images_info['filename'].'.'.$images_info['extension'];
$images_url = $_SESSION['css_url'].'/'.$bg_images;
$bytes = file_put_contents($image_path.'/'.$images_name,get_file_content($images_url));
$_SESSION['img'] = $imgs;
$byte = bytesToSize($bytes);
jsonResult(200, 'Tải ảnh background: <a href="'.$images_url.'" target="_blank">'.$bg_images.'</a>', array('size' => $byte));
die;
}else{
//show_image_download(false);
}
}else if(count($arrayCssNoClean) > 0 ){
$css_file = array_pop($arrayCssNoClean);
if($css_file){
$css_content = get_file_content($css_file);
$re = '/url\(\s*[\'"]?(\S*\.(?:jpe?g|gif|png|otf|eot|svg|ttf|woff))[\'"]?\s*\)[^;}]/i';
if (preg_match_all($re, $css_content, $matches)) {
$imgs = $matches[1];
}
$_SESSION['img'] = $imgs;
$file_info = pathinfo($css_file);
$_SESSION['css_url'] = $file_info[ 'dirname'];
}
$_SESSION['arrayCssNoClean'] = $arrayCssNoClean;
$path_info = pathinfo($css_file);
jsonResult(200, 'Bóc tách ảnh trong file css: <a href="'.$css_file.'" target="_blank">'.$path_info['basename'].'</a>');
die;
}
$step++;
$_SESSION['step'] = $step;
jsonResult(200, 'Cập nhật lại đường dẫn các file css và javascript ');
die;
break;
case "4":
/* update html content
----------------------------------------------------------------------------*/
// load temp html file, keep treserve the break line
$html_file = file_get_html($path.'/tmp',false,NULL,'-1','-1',true,true,DEFAULT_TARGET_CHARSET,false);
foreach ($html_file->find('link') as $stylesheet){
$stylesheet->rel = strtolower($stylesheet->rel);
}
// remore "base" dom element
foreach($html_file->find('base') as $base){
$base->outertext = '';
}
// replace link css
foreach ($html_file->find('link[rel="stylesheet"]') as $stylesheet){
$stylesheet_url = cleanUrl($stylesheet->href,true);
if($stylesheet_url){
$stylesheet_url = str_replace(h_parse_url($_SESSION['url']),'',$stylesheet_url);
$path_info = pathinfo($stylesheet_url);
$file_path = 'asset/css/'.$path_info['filename'].".".$path_info['extension'];
$stylesheet->href = $file_path;
}
}
// replace link js
foreach ($html_file->find('script') as $script){
$script_url = cleanUrl($script->src);
if($script_url){
$stylesheet_url = str_replace(h_parse_url($_SESSION['url']),'',$script_url);
$path_info = pathinfo($script_url);
$file_path = 'asset/js/'.$path_info['filename'].".".$path_info['extension'];
$script->src = $file_path;
}
}
// recreate images link
foreach ($html_file->find('img') as $h_img){
$images_src = $h_img->src;
if(strpos($images_src,'http')===false){
$h_img->src = h_parse_url($_SESSION['url']).'/'.$images_src;
}
}
// recreate sub link
foreach ($html_file->find('a') as $link){
$link_href = $link->href;
if(strpos($link_href,'//')===false){
$link->href = h_parse_url($_SESSION['url']).'/'.$link_href;
}
}
// delete temp file and create html file
$ret = $html_file->root->innertext();
file_put_contents($path.'/'.$save_file,$ret);
/*
$a = $html_file->save($path.'/'.$save_file);
unlink($path.'/tmp');
*/
$step++;
$_SESSION['step'] = $step;
$site_path = get_site_path();
$data = array('link_download' => $site_path . 'download.php?f=' . $folder , 'link_view' => $site_path . 'save/' . $folder );
jsonResult(500, '<strong class="blink">Đã hoàn thành...</strong>', $data);
session_destroy();
unlink($path.'/tmp');
die;
break;
case "5":
session_destroy();
break;
} // end switch
?>