typecho全站静态化方案

61

方案

利用wget全站保存为html,然后再修改文件中的链接

脚本

<?php
$url = 'https://ifking.cn'; //网址,不能以"/"结尾
$rurl=''; //要替换成路径或网址,可为空,不能以"/"结尾
$dir = __DIR__ . "/" . str_replace('https://', '', str_replace('http://', "", $url));
exec("clear",$clc);
echo $clc[0];
echo "开始下载文件\r\n";
exec("rm -rf {$dir}",$return);
exec("wget -r -p -np {$url}",$return);

$dirs = get_filenamesbydir($dir);

//不处理非html文件
for ($i = 0; $i < count($dirs); $i++) {
	$file=str_replace(__DIR__, "", $dirs[$i]['file']);

    if (!preg_match("/html/",$file)  ) {
        //删除对应的元素
        unset($dirs[$i]);
	
    }
  
}
array_filter($dirs);
sort($dirs);//重新生成索引下标

//网址处理
$count=count($dirs);
for ($i = 0; $i < $count; $i++) {
	$content=str_replace($url,$rurl,file_get_contents($dirs[$i]['file']));
	file_put_contents($dirs[$i]['file'],$content);
	$n=$i+1;
	exec("clear",$clc);
	echo $clc[0];
	echo "文件下载完毕\r\n";
	echo "开始处理文件,共{$count}个文件需要处理,已处理{$n}个\r\n";

}
echo "处理完毕,文件目录:{$dir}\r\n";


function get_allfiles($path, &$files)
{
    if (is_dir($path)) {
        $dp = dir($path);
        while ($file = $dp->read()) {
            if ($file !== "." && $file !== "..") {
                get_allfiles($path . "/" . $file, $files);
            }
        }
        $dp->close();
    }
    if (is_file($path)) {
        $files[] = ['file' => $path];
    }
}
function get_filenamesbydir($dir)
{
    $files = array();
    get_allfiles($dir, $files);
    return $files;
}

保存上述代码为html.php并修改,运行 php html.php [scode type="yellow"]需要系统安装有wget(一般都有)并且启用php的exec函数[/scode]

思路来源于 https://blog.5v5.lol/archives/88/

预览

https://blog.ifking.cn