mhtfilemaker.class.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /***********************************************************************
  3. Class: Mht File Maker
  4. Version: 1.2 beta
  5. Date: 02/11/2007
  6. Author: Wudi <wudicgi@yahoo.de>
  7. Description: The class can make .mht file.
  8. ***********************************************************************/
  9. class MhtFileMaker
  10. {
  11. var $config = array();
  12. var $headers = array();
  13. var $headersExists = array();
  14. var $files = array();
  15. var $boundary;
  16. var $dirBase;
  17. var $pageFirst;
  18. public function __construct($config = array())
  19. {
  20. $this->config = $config;
  21. }
  22. public function setHeader($header)
  23. {
  24. $this->headers[] = $header;
  25. $key = strtolower(substr($header, 0, strpos($header, ':')));
  26. $this->headersExists[$key] = TRUE;
  27. }
  28. public function setFrom($from)
  29. {
  30. $this->setHeader("From: $from");
  31. }
  32. public function setSubject($subject)
  33. {
  34. $this->setHeader("Subject: $subject");
  35. }
  36. public function setDate($date = NULL, $istimestamp = FALSE)
  37. {
  38. if($date == NULL) $date = time();
  39. if($istimestamp) $date = date('D, d M Y H:i:s O', $date);
  40. $this->setHeader("Date: $date");
  41. }
  42. public function setBoundary($boundary = NULL)
  43. {
  44. $this->boundary = $boundary;
  45. if($boundary == NULL) $this->boundary = '--' . strtoupper(md5(mt_rand())) . '_MULTIPART_MIXED';
  46. }
  47. public function setBaseDir($dir)
  48. {
  49. $this->dirBase = str_replace("\\", "/", realpath($dir));
  50. }
  51. public function setFirstPage($filename)
  52. {
  53. $this->pageFirst = str_replace("\\", "/", realpath("{$this->dirBase}/$filename"));
  54. }
  55. public function autoAddFiles()
  56. {
  57. if(!isset($this->pageFirst)) die('Not set the first page.');
  58. $filepath = str_replace($this->dirBase, '', $this->pageFirst);
  59. $filepath = 'http://mhtfile' . $filepath;
  60. $this->addFile($this->pageFirst, $filepath, NULL);
  61. $this->addDir($this->dirBase);
  62. }
  63. public function addDir($dir)
  64. {
  65. $handleDir = opendir($dir);
  66. while($filename = readdir($handleDir))
  67. {
  68. if(($filename != '.') && ($filename != '..') && ("$dir/$filename" != $this->pageFirst))
  69. {
  70. if(is_dir("$dir/$filename"))
  71. {
  72. $this->addDir("$dir/$filename");
  73. }
  74. elseif(is_file("$dir/$filename"))
  75. {
  76. $filepath = str_replace($this->dirBase, '', "$dir/$filename");
  77. $filepath = 'http://mhtfile' . $filepath;
  78. $this->addFile("$dir/$filename", $filepath, NULL);
  79. }
  80. }
  81. }
  82. closedir($handleDir);
  83. }
  84. public function addFile($filename, $filepath = NULL, $encoding = NULL)
  85. {
  86. if($filepath == NULL) $filepath = $filename;
  87. $mimetype = $this->GetMimeType($filename);
  88. $filecont = file_get_contents($filename);
  89. $this->addContents($filepath, $mimetype, $filecont, $encoding);
  90. }
  91. public function addContents($filepath, $mimetype, $filecont, $encoding = NULL)
  92. {
  93. if($encoding == NULL)
  94. {
  95. $filecont = chunk_split(base64_encode($filecont), 76);
  96. $encoding = 'base64';
  97. }
  98. $this->files[] = array('filepath' => $filepath,
  99. 'mimetype' => $mimetype,
  100. 'filecont' => $filecont,
  101. 'encoding' => $encoding);
  102. }
  103. public function checkHeaders()
  104. {
  105. if(!array_key_exists('date', $this->headersExists)) $this->setDate(NULL, TRUE);
  106. if ($this->boundary == NULL) $this->SetBoundary();
  107. }
  108. public function checkFiles()
  109. {
  110. if(count($this->files) == 0) return FALSE;
  111. return TRUE;
  112. }
  113. public function getFile()
  114. {
  115. $this->checkHeaders();
  116. if(!$this->checkFiles()) die('No file was added.');
  117. $contents = implode("\r\n", $this->headers);
  118. $contents .= "\r\n";
  119. $contents .= "MIME-Version: 1.0\r\n";
  120. $contents .= "Content-Type: multipart/related;\r\n";
  121. $contents .= "\tboundary=\"{$this->boundary}\";\r\n";
  122. $contents .= "\ttype=\"" . $this->files[0]['mimetype'] . "\"\r\n";
  123. $contents .= "X-MimeOLE: Produced By Mht File Maker v1.0 beta\r\n";
  124. $contents .= "\r\n";
  125. $contents .= "This is a multi-part message in MIME format.\r\n";
  126. $contents .= "\r\n";
  127. foreach($this->files as $file)
  128. {
  129. $contents .= "--{$this->boundary}\r\n";
  130. $contents .= "Content-Type: $file[mimetype]\r\n";
  131. $contents .= "Content-Transfer-Encoding: $file[encoding]\r\n";
  132. $contents .= "Content-Location: $file[filepath]\r\n";
  133. $contents .= "\r\n";
  134. $contents .= $file['filecont'];
  135. $contents .= "\r\n";
  136. }
  137. $contents .= "--{$this->boundary}--\r\n";
  138. return $contents;
  139. }
  140. public function makeFile($filename)
  141. {
  142. $contents = $this->getFile();
  143. $fp = fopen($filename, 'w');
  144. fwrite($fp, $contents);
  145. fclose($fp);
  146. }
  147. public function getMimeType($filename)
  148. {
  149. $pathinfo = pathinfo($filename);
  150. switch($pathinfo['extension'])
  151. {
  152. case 'htm': $mimetype = 'text/html'; break;
  153. case 'html': $mimetype = 'text/html'; break;
  154. case 'txt': $mimetype = 'text/plain'; break;
  155. case 'cgi': $mimetype = 'text/plain'; break;
  156. case 'php': $mimetype = 'text/plain'; break;
  157. case 'css': $mimetype = 'text/css'; break;
  158. case 'jpg': $mimetype = 'image/jpeg'; break;
  159. case 'jpeg': $mimetype = 'image/jpeg'; break;
  160. case 'jpe': $mimetype = 'image/jpeg'; break;
  161. case 'gif': $mimetype = 'image/gif'; break;
  162. case 'png': $mimetype = 'image/png'; break;
  163. default: $mimetype = 'application/octet-stream'; break;
  164. }
  165. return $mimetype;
  166. }
  167. }
  168. ?>