-
-
/**************************************************** **** ********************
- 課程:Mht File Maker
- 版本:1.2 beta
- 連結:bbs.it-home. org
- 作者: Wudi
- 描述:該類別可以製作.mht檔。
- ************************ ***************************** ****************************/
類別 MhtFileMaker{
- var $config = array();
- var $headers = array();
- var $headers_exists = array();
- var $files = array();
- var $boundary;
- var $dir_base;
- var $page_first;
function MhtFile($config = array()){
- }
function SetHeader($header){
- $this->headers[] = $header;
- $key = strtolower(substr($header, 0, strpos($header, ':') ));
- $this->headers_exists[$key] = TRUE;
- }
function SetFrom($ from){
- $this->SetHeader("來自:$from");
- }
function SetSubject($subject){
- $this->SetHeader("主題:$subject");
- } p>
function SetDate($date = NULL, $isttimestamp = FALSE){
- if ($date == NULL) {
- $date = time();
- }
- if ($istimestamp == TRUE) {
- $date = date('D, d M Y H:i:s O', $date);
- }
- $this->SetHeader("日期: $date");
- }
function SetBoundary($boundary = NULL) {
- if ($boundary == NULL) {
- $this->boundary = ' --' 。 strtoupper(md5(mt_rand())) 。 >
function SetBaseDir($dir){
- $this->dir_base = str_replace("", "/", realpath($dir));
- }
function SetFirstPage($filename){
- $this->page_first = str_replace("", "/", realpath("{$this->dir_base}/$filename"));
- }
function AutoAddFiles(){
- if (!isset($this->page_first)) {
- exit ('不設定第一頁。');
- }
- $$ filepath = str_replace ($this->dir_base, '', $this->page_first);
- $filepath = 'http://mhtfile' 。 page_first, $filepath, NULL);
- $this->AddDir($this->dir_base);
- }
function AddDir($dir){
- $handle_dir = opendir($dir);
- while ($filename = readdir($handle_dir)) {
- if (( $filename!='.') && ($filename!='..') && (" $dir/$filename"!=$this->page_first)) {
- if (is_dir("$dir/ $filename")) {
- $this->AddDir("$dir/$filename") ;
- } elseif (is_file("$dir/$filename")) {
- $filepath = str_replace ($this->dir_base, '', "$dir/$filename");
- $filepath = 'http://mhtfile' 。 closedir($handle_dir);
- }
function AddFile($filename, $filepath = NULL, $encoding = NULL){
- if ($filepath == NULL) {
- $filepath = $檔名;
- }
- $mimetype = $this->GetMimeType($filename);
- $filecont = file_get_contents($filename);
- $this->AddContents( $filepath, $mimetype , $filecont, $encoding);
- }
function AddContents($filepath, $mimetype, $filecont, $encoding = NULL){
- if ( $encoding == NULL) {
- $filecont = chunk_split(base64_encode($filecont), 76);
- $encoding = 'base64';
- }
- $this->files[] = array (' filepath' => $filepath,
- 'mimetype' => $mimetype,
- 'filecont' => $filecont,
- 'encoding' => $encoding);
- }
function CheckHeaders(){
- if (!array_key_exists('date', $this->headers_exists)) {
- $this->SetDate(NULL, TRUE) );
- }
- if ($this->boundary == NULL) {
- $this->SetBoundary();
- }
- }
-
function CheckFiles() {
- if (count($this->files) == 0) {
- 回傳FALSE;
- } else {
- 回傳TRUE;
- }
- }
function GetFile(){
- $this->CheckHeaders();
- if (!$this->CheckFiles()) {
- exit ('沒有新增檔案。');
- }
- $contents = implode("rn", $this->headers);
- $contents .= "rn";
- $contents .= "MIME 版本:1.0rn";
- $contents .= "內容類型:多部分/相關;rn";
- $contents .= "tboundary="{$this->;邊界}";rn";
- $contents .= "ttype="" . $this->files[0]['mimetype'] . ""rn";
- $contents .= "X-MimeOLE:由Mht File Maker v1.0 betan 製作";
- $contents .= "rn";
- $contents .= "這是一條MIME 格式的多部分訊息.rn";
- $contents . = " rn";
- foreach ($this->files as $file) {
- $contents .= "--{$this->boundary}rn";
- $contents .= "內容類型:$ file[mimetype]rn";
- $contents .= "內容傳輸編碼:$file[encoding]rn";
- $contents .= "內容位置:$file[檔案路徑]rn";
- $contents .= "rn";
- $contents .= $file['filecont'];
- $contents .= "rn";
- }
- $contents .= "--{$ this->boundary}--rn";
- return $contents;
- }
function MakeFile($filename){
- $contents = $this->GetFile( );
- $fp = fopen($filename, 'w');
- fwrite($fp, $contents);
- fclose($fp);
- }
-
function GetMimeType($filename){
- $pathinfo = pathinfo($filename);
- switch ($pathinfo['extension']) {
- case 'htm': $mimetype = 'text/ html'; Break;
- case 'html': $mimetype = 'text/html'; Break;
- case 'txt': $mimetype = 'text/plain'; Break;
- case 'cgi' : $mimetype = 'text/plain'; Break;
- case 'php': $mimetype = 'text/plain'; Break;
- case 'css': $mimetype = 'text/css'; Break;
- case 'jpg': $mimetype = 'image/jpeg'; Break;
- case 'jpeg': $mimetype = 'image/jpeg'; Break;
- case 'jpe': $mimetype = ' image/jpeg'; Break;
- case 'gif': $mimetype = 'image/gif'; Break;
- case 'png': $mimetype = 'image/png';中斷;
- 預設: $mimetype = 'application/octet-stream'; Break;
- }
- return $mimetype;
- }
- }
- ?>
複製程式碼
2、匯出word檔exportdoc.php
-
-
/**
- * 根據html程式碼取得word文檔內容
- * 建立一個本質為mht的文檔,該函數會分析文件內容並從遠端下載頁面中的圖片資源
- * 此函數依賴類別MhtFileMaker
- * 函數會分析img標籤,提取src的屬性值。但是,src的屬性值必須被引號包圍,否則不能提取
- *
- * @param string $content HTML內容
- * @param string $absolutePath 網頁的絕對路徑。如果HTML內容裡的圖片路徑是相對路徑,那麼就需要填入這個參數,讓函數自動填入絕對路徑。這個參數最後需要以/結束
- * @param bool $isEraseLink 是否去掉HTML內容中的連結
- */
- include_once("docclass." );
- function getWordDocument( $content , $absolutePath = "" , $isEraseLink = true )
- {
- $mht = new MhtFileMaker();
- if ($isEraseLink))(s*.*?s*)/i' , '$1' , $content); // 去掉連結
$images = array();
- $files = array();
- $matches = array();
- //這個演算法要求src後的屬性值必須用引號括起來
- if ( preg_match_all('//i',$content ,$matches ) )
- {
- $arrPath = $matches[1];
- for ( $i=0;$i{
- $path = $ arrPath[$i];
- $imgPath = trim( $path );
- if ( $imgPath != "" )
- {
- $files[] = $imgPath;
- if( substr($imgPath,0,7) == 'http://')
- {
- //絕對鏈接,不加海外
- }
- 其他
- {
- $imgPath = $absolutePath.$imgPath;
- }
- $images[] = $imgPath;
- }
- }
- }
- $mht->AddContents("tmp.html",$$ mht->GetMimeType("tmp.html"),$content);
for ( $ i=0;$i{- $image = $images[$i];
- if ( @fopen($image , 'r') )
- {
- $imgcontent = @file_get_contents( $image );
- if ($imgcontent = @file_get_contents( $image );
- if ($imgcontent = @file_get_contents( $image );
- if ($imgcontent = @file_get_contents( $image );
- if ($imgcontent = @file_get_contents( $image );
- if ($imgcontent = @file_get_contents( $image );
- if ($imgcontent = @file_get_contents( $image );
- if ($imgcontent = @file_get_contents( $image );
- if( $content )
- $mht->AddContents($files[$i],$mht->GetMimeType($image) ),$imgcontent);
- }
- else
- {
- echocho "文件:".$image."不存在!
";
- }
- }
return $mht->GetFile();
- }
- $content=implode("",file( "http://bbs.it-home. org/print.php?id=3548"));
$fileContent = getWordDocument($content,"."); $fp = fopen("hugesky_word .doc", 'w'); fwrite($fp, $fileContent);fclose($fp); ?> |