-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
56 lines (48 loc) · 1.71 KB
/
index.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
<?php
function rmdir_rec($dir)
{
$files = array_diff(scandir($dir), array('.', '..'));
foreach ($files as $file) {
if (is_dir("$dir/$file")) {
remove_directory("$dir/$file");
} else {
unlink("$dir/$file");
}
}
return rmdir($dir);
}
if (isset($_POST)) {
$html = json_decode(file_get_contents("php://input"),true)["html"];
$dir = uniqid();
mkdir($dir);
mkdir($dir . "/img");
$zip = new ZipArchive;
$zipPath = $dir.'/myPage.zip';
if ($zip->open($zipPath, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE) === true) {
$zip->addEmptyDir('img');
$html = preg_replace_callback('/img\s+src="([^"]+)"/', function ($match) use ($dir, $zip) {
preg_match('/\/([^\/]+)\?/', urldecode($match[1]), $fileTitle);
$content = file_get_contents($match[1]);
$filename = $fileTitle[1] . ".png";
$newPath = $dir . "/" . $filename;
file_put_contents($newPath, $content);
$zip->addFile($newPath, "img/" . $filename);
return 'img src="img/' . $filename . '"';
}, $html);
$zip->addFromString("index.html", $html);
$zip->close();
// HTTPヘッダを設定
header('Content-Type: application/zip');
header('Content-Length: ' . filesize($zipPath));
header('Content-Disposition: attachment; filename=myPage.zip');
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept');
readfile($zipPath);
} else {
echo '失敗しました';
}
//rmdir_rec($dir);
echo $html;
} else {
echo "No Available POST Data\n";
}