/***********************************************************************
- CLASS:MHT File Maker
- version:1.2 Beta
- link:bbs.it-home.org
- Author:wudi
- Description:the class can make. mht file.
- ***********************************************************************/
Class 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: $from");
- }
function Setsubject ($subject) {
- $this->setheader ("Subject: $subject");
- }
function SetDate ($date = NULL, $istimestamp = FALSE) {
- if ($date = = NULL) {
- $date = time ();
- }
- if ($istimestamp = = TRUE) {
- $date = Date (' d, D M Y h:i:s O ', $date);
- }
- $this->setheader ("Date: $date");
- }
function setboundary ($boundary = NULL) {
- if ($boundary = = NULL) {
- $this->boundary = '--'. Strtoupper (MD5 (Mt_rand ())). ' _multipart_mixed ';
- } else {
- $this->boundary = $boundary;
- }
- }
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 (' not set the first page. ');
- }
- $filepath = Str_replace ($this->dir_base, ", $this->page_first);
- $filepath = ' Http://mhtfile '. $filepath;
- $this->addfile ($this->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 '. $filepath;
- $this->addfile ("$dir/$filename", $filepath, NULL);
- }
- }
- }
- Closedir ($handle _dir);
- }
function AddFile ($filename, $filepath = null, $encoding = null) {
- if ($filepath = = NULL) {
- $filepath = $filename;
- }
- $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) {
- return FALSE;
- } else {
- return TRUE;
- }
- }
function GetFile () {
- $this->checkheaders ();
- if (! $this->checkfiles ()) {
- Exit (' No file was added. ');
- }
- $contents = Implode ("rn", $this->headers);
- $contents. = "RN";
- $contents. = "Mime-version:1.0rn";
- $contents. = "Content-type:multipart/related;rn";
- $contents. = "tboundary=" {$this->boundary} "; RN";
- $contents. = "Ttype=" ". $this->files[0][' mimetype '). "RN";
- $contents. = "x-mimeole:produced by Mht File Maker v1.0 Betarn";
- $contents. = "RN";
- $contents. = "This was a multi-part message in MIME format.rn";
- $contents. = "RN";
- foreach ($this->files as $file) {
- $contents. = "--{$this->boundary}rn";
- $contents. = "Content-type: $file [Mimetype]rn";
- $contents. = "Content-transfer-encoding: $file [Encoding]rn";
- $contents. = "Content-location: $file [Filepath]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 '; Break
- Default: $mimetype = ' application/octet-stream '; Break
- }
- return $mimetype;
- }
- }
- ?>
Copy Code2. Export Word file exportdoc.php
/**
- * Get Word document content based on HTML code
- * Create a document that is essentially MHT, which parses the contents of a file and downloads the picture resource from the page remotely
- * This function depends on the class Mhtfilemaker
- * This function parses the IMG tag and extracts the SRC attribute values. However, the attribute value of SRC must be surrounded by quotation marks, otherwise it cannot be extracted
- *
- * @param string $content HTML content
- * @param string $absolutePath The absolute path of the Web page. If the image path in the HTML content is a relative path, you need to fill in this parameter to let the function automatically fill in the absolute path. This parameter finally needs to be/end
- * @param bool $isEraseLink whether to remove links from HTML content
- */
- Include_once ("docclass.php");
- function Getworddocument ($content, $absolutePath = "", $isEraseLink = True)
- {
- $MHT = new Mhtfilemaker ();
- if ($isEraseLink)
- $content = Preg_replace ('/(s*.*?s*)/I ', ' $ ', $content); Remove link
$images = Array ();
- $files = Array ();
- $matches = Array ();
- This algorithm requires that attribute values after src must be enclosed in quotation marks.
- 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//')
- {
- Absolute links, without prefixes
- }
- Else
- {
- $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 ($content)
- $MHT->addcontents ($files [$i], $mht->getmimetype ($image), $imgcontent);
- }
- Else
- {
- echo "File:". $image. "Not exist!
";
- }
- }
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);
- ?>
Copy Code |