forked from dukai/WeiboJsBackup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
weibo-zip.php
161 lines (145 loc) · 4.83 KB
/
weibo-zip.php
1
<?phprequire_once "common.php";$db = getDb();$totalCount = $db->fetchOne("select count(*) from weibos");$backupId = $_GET['backupid'];$take = 40;$maxPages = ceil($totalCount/$take);function getHtml($backupId, $page, $totalCount, $take){ global $db; $start = ($page - 1) * $take; $cmd = "select * from weibos where backupid='{$backupId}' order by dateline desc limit {$start}, {$take}"; $weibos = $db->fetchAll($cmd); $html = ""; foreach($weibos as $w){ $temp = '<li class="item">'; $temp .= '<div>' . preg_replace("/@([^@)(。,:\s]+)(?=()|(|。|,|:|?|\s|$))/u", "<a href=\"http://weibo.com/n/$1\" target=\"_blank\">@$1</a>", preg_replace("/(http:\/\/.+?)(\s|$)/", '<a href="$1" target="_blank">$1</a>', $w['content'])) . '</div>'; if(!empty($w['refer_content'])){ $temp .= '<div class="refer">'; $temp .= '<div class="comment">' . $w['refer_content'] . '</div>'; $temp .= '<div>'; if(!empty($w['refer_img'])){ $temp .= '<img src="' . $w['refer_img'] . '" />'; } $temp .= '</div>'; $temp .= '</div>'; } $temp .= '<div>'; if(!empty($w['img'])){ $temp .= '<img src="' . $w['img'] . '" onclick="toggleImg(this);" />'; } $temp .= '</div>'; $temp .= '</li>'; $html .= $temp; } $head = <<<EOF<!DOCTYPE html><html><head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>HTML TEMPLATE</title> <style> h1{text-align:center;} body{font-size:14px;line-height:22px;background:url(images/bg.jpg);} a{color:#06c;text-decoration:none;} ul{margin:0;padding:0;} #wraper{max-width:800px;margin:0 auto;} .item{border-bottom:1px dashed #cccc;line-height:20px;border-bottom:1px dashed #ccc;list-style:none;padding:6px 0;} .refer{margin:6px 0 0 6px; border-left:2px solid #ccc;padding:6px;background:#f1f1f1;} </style></head><body><div id="wraper"> <h1>微博备份</h1> <ul>EOF; $q = "weibo_{page}.html"; $foot = '</ul><div class="pager">' . pager($page, $take, $totalCount, $q); $foot .= <<<EOF </div></div><script></script></body></html>EOF; $html = $head . $html . $foot; return $html;}function pager($currpage, $perpage, $nums, $q, $currPageStyle='', $othersPageStyle='') { $dp=10; /* 分页链接的数量 */ $nums = intval($nums); $maxPages = ceil($nums/$perpage); $pageStart=1; if ($maxPages==0) { $maxPages = 1; } if ($currpage>$maxPages) { $currpage=$maxPages; } if ($currpage<=1) { $s = "<span class=\"{$currPageStyle}\">上页 </span>"; $pageStart = 1; $currpage=1; $pageEnd=$dp; } else { $tmp = $currpage-1; $s = "<a href=\"".str_replace('{page}', $tmp, $q)."\" class=\"{$othersPageStyle}\">上页</a> "; /*** 下面开始计算 1--$dp 以后的 $pageStart ***/ $rangeOrder = floor(($currpage-2)/($dp-2)); $pageStart = $rangeOrder*($dp-2)+1; $pageEnd=$pageStart+$dp-1; } for ($i=$pageStart; $i<=$pageEnd; $i++) { if ($i>$maxPages) { break; } if ($i!=$currpage) { $s.= '<a href="'.str_replace('{page}', $i, $q).'" class="'.$othersPageStyle.'">'.$i.'</a> '; } else { $s.= '<span class="'.$currPageStyle.'">'.$i.'</span> '; } } if ($currpage>=$maxPages) { $s.= "<span class=\"{$currPageStyle}\">下页 </span>"; } else { $tmp = $currpage+1; $s.= "<a href=\"".str_replace('{page}', $tmp, $q)."\" class=\"{$othersPageStyle}\">下页</a>"; } return $s;}/** * 没有写成class 或者 function ,需要的朋友自己写,就这么几行。。 */ $now = date("YmdHis");$filename = "./{$now}.zip"; //最终生成的文件名(含路径) if(!file_exists($filename)){ //重新生成文件 $zip = new ZipArchive();//使用本类,linux需开启zlib,windows需取消php_zip.dll前的注释 if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) { exit('无法打开文件,或者文件创建失败'); } for($i = 1; $i <= $maxPages; $i++){ $zip->addFromString("weibo_{$i}.html", getHtml($backupId, $i, $totalCount, $take)); } /* foreach( $datalist as $val){ $attachfile = $attachmentDir . $val['filepath']; //获取原始文件路径 if(file_exists($attachfile)){ $zip->addFile( $attachfile , basename($attachfile));//第二个参数是放在压缩包中的文件名称,如果文件可能会有重复,就需要注意一下 } } */ $zip->close();//关闭 } if( !file_exists($filename)){ exit("无法找到文件"); //即使创建,仍有可能失败。。。。 } header("Cache-Control: public"); header("Content-Description: File Transfer"); header('Content-disposition: attachment; filename='.basename($filename)); //文件名 header("Content-Type: application/zip"); //zip格式的 header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件 header('Content-Length: '. filesize($filename)); //告诉浏览器,文件大小 @readfile($filename);