mime.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. <?php
  2. /** mime.php
  3. **
  4. ** This contains the functions necessary to detect and decode MIME
  5. ** messages.
  6. **
  7. **/
  8. $debug_mime = false;
  9. $mime_php = true;
  10. if (!isset($i18n_php))
  11. include "../functions/i18n.php";
  12. if (!isset($imap_php))
  13. include "../functions/imap.php";
  14. if (!isset($config_php))
  15. include "../config/config.php";
  16. /** Setting up the objects that have the structure for the message **/
  17. class msg_header {
  18. /** msg_header contains generic variables for values that **/
  19. /** could be in a header. **/
  20. var $type0, $type1, $boundary, $charset, $encoding, $size;
  21. var $to, $from, $date, $cc, $bcc, $reply_to, $subject;
  22. var $id, $mailbox, $description;
  23. var $entity_id, $message_id, $charset;
  24. }
  25. class message {
  26. /** message is the object that contains messages. It is a recursive
  27. object in that through the $entities variable, it can contain
  28. more objects of type message. See documentation in mime.txt for
  29. a better description of how this works.
  30. **/
  31. var $header;
  32. var $entities;
  33. function addEntity ($msg) {
  34. $this->entities[count($this->entities)] = $msg;
  35. }
  36. }
  37. /* --------------------------------------------------------------------------------- */
  38. /* MIME DECODING */
  39. /* --------------------------------------------------------------------------------- */
  40. // This function gets the structure of a message and stores it in the "message" class.
  41. // It will return this object for use with all relevant header information and
  42. // fully parsed into the standard "message" object format.
  43. function mime_structure ($imap_stream, $header) {
  44. global $debug_mime;
  45. sqimap_messages_flag ($imap_stream, $header->id, $header->id, "Seen");
  46. $id = $header->id;
  47. fputs ($imap_stream, "a001 FETCH $id BODYSTRUCTURE\r\n");
  48. $read = fgets ($imap_stream, 10000);
  49. $response = substr($read, 0, 4);
  50. while ($response != "a001") {
  51. $bodystructure .= $read;
  52. $read = fgets ($imap_stream, 10000);
  53. $response = substr($read, 0, 4);
  54. }
  55. // $read = strtolower($bodystructure);
  56. $read = $bodystructure;
  57. if ($debug_mime) echo "<tt>$read</tt><br><br>\n";
  58. // isolate the body structure and remove beginning and end parenthesis
  59. $read = trim(substr ($read, strpos(strtolower($read), "bodystructure") + 13));
  60. $read = trim(substr ($read, 0, -1));
  61. $end = mime_match_parenthesis(0, $read);
  62. while ($end == strlen($read)-1) {
  63. $read = trim(substr ($read, 0, -1));
  64. $read = trim(substr ($read, 1));
  65. $end = mime_match_parenthesis(0, $read);
  66. }
  67. if ($debug_mime) echo "<tt>$read</tt><br><br>\n";
  68. $msg = mime_parse_structure ($read, 0);
  69. $msg->header = $header;
  70. return $msg;
  71. }
  72. // this starts the parsing of a particular structure. It is called recursively,
  73. // so it can be passed different structures. It returns an object of type
  74. // $message.
  75. // First, it checks to see if it is a multipart message. If it is, then it
  76. // handles that as it sees is necessary. If it is just a regular entity,
  77. // then it parses it and adds the necessary header information (by calling out
  78. // to mime_get_elements()
  79. function mime_parse_structure ($structure, $ent_id) {
  80. global $debug_mime;
  81. if ($debug_mime) echo "<font color=008800><tt>START: mime_parse_structure()</tt></font><br>\n";
  82. $msg = new message();
  83. if (substr($structure, 0, 1) == "(") {
  84. $ent_id = mime_new_element_level($ent_id);
  85. $start = $end = -1;
  86. if ($debug_mime) echo "<br><font color=0000aa><tt>$structure</tt></font><br>";
  87. do {
  88. if ($debug_mime) echo "<font color=008800><tt>Found entity...</tt></font><br>";
  89. $start = $end+1;
  90. $end = mime_match_parenthesis ($start, $structure);
  91. $element = substr($structure, $start+1, ($end - $start)-1);
  92. $ent_id = mime_increment_id ($ent_id);
  93. $newmsg = mime_parse_structure ($element, $ent_id);
  94. $msg->addEntity ($newmsg);
  95. } while (substr($structure, $end+1, 1) == "(");
  96. } else {
  97. // parse the elements
  98. if ($debug_mime) echo "<br><font color=0000aa><tt>$structure</tt></font><br>";
  99. $msg = mime_get_element (&$structure, $msg, $ent_id);
  100. if ($debug_mime) echo "<br>";
  101. }
  102. return $msg;
  103. if ($debug_mime) echo "<font color=008800><tt>&nbsp;&nbsp;END: mime_parse_structure()</tt></font><br>";
  104. }
  105. // Increments the element ID. An element id can look like any of
  106. // the following: 1, 1.2, 4.3.2.4.1, etc. This function increments
  107. // the last number of the element id, changing 1.2 to 1.3.
  108. function mime_increment_id ($id) {
  109. global $debug_mime;
  110. if (strpos($id, ".")) {
  111. $first = substr($id, 0, strrpos($id, "."));
  112. $last = substr($id, strrpos($id, ".")+1);
  113. $last++;
  114. $new = $first . "." .$last;
  115. } else {
  116. $new = $id + 1;
  117. }
  118. if ($debug_mime) echo "<b>INCREMENT: $new</b><br>";
  119. return $new;
  120. }
  121. // See comment for mime_increment_id().
  122. // This adds another level on to the entity_id changing 1.3 to 1.3.0
  123. // NOTE: 1.3.0 is not a valid element ID. It MUST be incremented
  124. // before it can be used. I left it this way so as not to have
  125. // to make a special case if it is the first entity_id. It
  126. // always increments it, and that works fine.
  127. function mime_new_element_level ($id) {
  128. if (!$id) $id = 0;
  129. else $id = $id . ".0";
  130. return $id;
  131. }
  132. function mime_get_element (&$structure, $msg, $ent_id) {
  133. global $debug_mime;
  134. $elem_num = 1;
  135. $msg->header = new msg_header();
  136. $msg->header->entity_id = $ent_id;
  137. while (strlen($structure) > 0) {
  138. $structure = trim($structure);
  139. $char = substr($structure, 0, 1);
  140. if (strtolower(substr($structure, 0, 3)) == "nil") {
  141. $text = "";
  142. $structure = substr($structure, 3);
  143. } else if ($char == "\"") {
  144. // loop through until we find the matching quote, and return that as a string
  145. $pos = 1;
  146. $char = substr($structure, $pos, 1);
  147. while ($char != "\"" && $pos < strlen($structure)) {
  148. $text .= $char;
  149. $pos++;
  150. $char = substr($structure, $pos, 1);
  151. }
  152. $structure = substr($structure, strlen($text) + 2);
  153. } else if ($char == "(") {
  154. // comment me
  155. $end = mime_match_parenthesis (0, $structure);
  156. $sub = substr($structure, 1, $end-1);
  157. $properties = mime_get_props($properties, $sub);
  158. $structure = substr($structure, strlen($sub) + 2);
  159. } else {
  160. // loop through until we find a space or an end parenthesis
  161. $pos = 0;
  162. $char = substr($structure, $pos, 1);
  163. while ($char != " " && $char != ")" && $pos < strlen($structure)) {
  164. $text .= $char;
  165. $pos++;
  166. $char = substr($structure, $pos, 1);
  167. }
  168. $structure = substr($structure, strlen($text));
  169. }
  170. if ($debug_mime) echo "<tt>$elem_num : $text</tt><br>";
  171. // This is where all the text parts get put into the header
  172. switch ($elem_num) {
  173. case 1:
  174. $msg->header->type0 = strtolower($text);
  175. if ($debug_mime) echo "<tt>type0 = ".strtolower($text)."</tt><br>";
  176. break;
  177. case 2:
  178. $msg->header->type1 = strtolower($text);
  179. if ($debug_mime) echo "<tt>type1 = ".strtolower($text)."</tt><br>";
  180. break;
  181. case 5:
  182. $msg->header->description = $text;
  183. if ($debug_mime) echo "<tt>description = $text</tt><br>";
  184. break;
  185. case 6:
  186. $msg->header->encoding = strtolower($text);
  187. if ($debug_mime) echo "<tt>encoding = ".strtolower($text)."</tt><br>";
  188. break;
  189. case 7:
  190. $msg->header->size = $text;
  191. if ($debug_mime) echo "<tt>size = $text</tt><br>";
  192. break;
  193. default:
  194. if ($msg->header->type0 == "text" && $elem_num == 8) {
  195. // This is a plain text message, so lets get the number of lines
  196. // that it contains.
  197. $msg->header->num_lines = $text;
  198. if ($debug_mime) echo "<tt>num_lines = $text</tt><br>";
  199. } else if ($msg->header->type0 == "message" && $msg->header->type1 == "rfc822" && $elem_num == 8) {
  200. // This is an encapsulated message, so lets start all over again and
  201. // parse this message adding it on to the existing one.
  202. $structure = trim($structure);
  203. if (substr($structure, 0, 1) == "(") {
  204. $e = mime_match_parenthesis (0, $structure);
  205. $structure = substr($structure, 0, $e);
  206. $structure = substr($structure, 1);
  207. $m = mime_parse_structure($structure, $msg->header->entity_id);
  208. // the following conditional is there to correct a bug that wasn't
  209. // incrementing the entity IDs correctly because of the special case
  210. // that message/rfc822 is. This fixes it fine.
  211. if (substr($structure, 1, 1) != "(")
  212. $m->header->entity_id = mime_increment_id(mime_new_element_level($ent_id));
  213. // Now we'll go through and reformat the results.
  214. if ($m->entities) {
  215. for ($i=0; $i < count($m->entities); $i++) {
  216. $msg->addEntity($m->entities[$i]);
  217. }
  218. } else {
  219. $msg->addEntity($m);
  220. }
  221. $structure = "";
  222. }
  223. }
  224. break;
  225. }
  226. $elem_num++;
  227. $text = "";
  228. }
  229. // loop through the additional properties and put those in the various headers
  230. if ($msg->header->type0 != "message") {
  231. for ($i=0; $i < count($properties); $i++) {
  232. $msg->header->{$properties[$i]["name"]} = $properties[$i]["value"];
  233. if ($debug_mime) echo "<tt>".$properties[$i]["name"]." = " . $properties[$i]["value"] . "</tt><br>";
  234. }
  235. }
  236. return $msg;
  237. }
  238. // I did most of the MIME stuff yesterday (June 20, 2000), but I couldn't
  239. // figure out how to do this part, so I decided to go to bed. I woke up
  240. // in the morning and had a flash of insight. I went to the white-board
  241. // and scribbled it out, then spent a bit programming it, and this is the
  242. // result. Nothing complicated, but I think my brain was fried yesterday.
  243. // Funny how that happens some times.
  244. //
  245. // This gets properties in a nested parenthesisized list. For example,
  246. // this would get passed something like: ("attachment" ("filename" "luke.tar.gz"))
  247. // This returns an array called $props with all paired up properties.
  248. // It ignores the "attachment" for now, maybe that should change later
  249. // down the road. In this case, what is returned is:
  250. // $props[0]["name"] = "filename";
  251. // $props[0]["value"] = "luke.tar.gz";
  252. function mime_get_props ($props, $structure) {
  253. global $debug_mime;
  254. while (strlen($structure) > 0) {
  255. $structure = trim($structure);
  256. $char = substr($structure, 0, 1);
  257. if ($char == "\"") {
  258. $pos = 1;
  259. $char = substr($structure, $pos, 1);
  260. while ($char != "\"" && $pos < strlen($structure)) {
  261. $tmp .= $char;
  262. $pos++;
  263. $char = substr($structure, $pos, 1);
  264. }
  265. $structure = trim(substr($structure, strlen($tmp) + 2));
  266. $char = substr($structure, 0, 1);
  267. if ($char == "\"") {
  268. $pos = 1;
  269. $char = substr($structure, $pos, 1);
  270. while ($char != "\"" && $pos < strlen($structure)) {
  271. $value .= $char;
  272. $pos++;
  273. $char = substr($structure, $pos, 1);
  274. }
  275. $structure = trim(substr($structure, strlen($tmp) + 2));
  276. $k = count($props);
  277. $props[$k]["name"] = strtolower($tmp);
  278. $props[$k]["value"] = $value;
  279. } else if ($char == "(") {
  280. $end = mime_match_parenthesis (0, $structure);
  281. $sub = substr($structure, 1, $end-1);
  282. $props = mime_get_props($props, $sub);
  283. $structure = substr($structure, strlen($sub) + 2);
  284. }
  285. return $props;
  286. } else if ($char == "(") {
  287. $end = mime_match_parenthesis (0, $structure);
  288. $sub = substr($structure, 1, $end-1);
  289. $props = mime_get_props($props, $sub);
  290. $structure = substr($structure, strlen($sub) + 2);
  291. return $props;
  292. } else {
  293. return $props;
  294. }
  295. }
  296. }
  297. // Matches parenthesis. It will return the position of the matching
  298. // parenthesis in $structure. For instance, if $structure was:
  299. // ("text" "plain" ("val1name", "1") nil ... )
  300. // x x
  301. // then this would return 42 to match up those two.
  302. function mime_match_parenthesis ($pos, $structure) {
  303. $char = substr($structure, $pos, 1);
  304. // ignore all extra characters
  305. // If inside of a string, skip string -- Boundary IDs and other
  306. // things can have ) in them.
  307. while ($pos < strlen($structure)) {
  308. $pos++;
  309. $char = substr($structure, $pos, 1);
  310. if ($char == ")") {
  311. return $pos;
  312. } else if ($char == '"') {
  313. $pos ++;
  314. while (substr($structure, $pos, 1) != '"' &&
  315. $pos < strlen($structure)) {
  316. $pos ++;
  317. }
  318. } else if ($char == "(") {
  319. $pos = mime_match_parenthesis ($pos, $structure);
  320. }
  321. }
  322. }
  323. function mime_fetch_body ($imap_stream, $id, $ent_id) {
  324. // do a bit of error correction. If we couldn't find the entity id, just guess
  325. // that it is the first one. That is usually the case anyway.
  326. if (!$ent_id) $ent_id = 1;
  327. fputs ($imap_stream, "a001 FETCH $id BODY[$ent_id]\r\n");
  328. $topline = fgets ($imap_stream, 1024);
  329. if (ereg('\{([^\}]*)\}', $topline, $regs)) {
  330. return fread ($imap_stream, $regs[1]);
  331. }
  332. else if (ereg('"([^"]*)"', $topline, $regs)) {
  333. return $regs[1];
  334. }
  335. return "Body retrival error, please report this bug!\n\nTop line is \"$topline\"\n";
  336. }
  337. /* -[ END MIME DECODING ]----------------------------------------------------------- */
  338. /** This is the first function called. It decides if this is a multipart
  339. message or if it should be handled as a single entity
  340. **/
  341. function decodeMime ($imap_stream, $body, $header) {
  342. global $username, $key, $imapServerAddress, $imapPort;
  343. return mime_structure ($imap_stream, $header);
  344. }
  345. // This is here for debugging purposese. It will print out a list
  346. // of all the entity IDs that are in the $message object.
  347. function listEntities ($message) {
  348. if ($message) {
  349. if ($message->header->entity_id)
  350. echo "<tt>" . $message->header->entity_id . " : " . $message->header->type0 . "/" . $message->header->type1 . "<br>";
  351. for ($i = 0; $message->entities[$i]; $i++) {
  352. $msg = listEntities($message->entities[$i], $ent_id);
  353. if ($msg)
  354. return $msg;
  355. }
  356. }
  357. }
  358. // returns a $message object for a particular entity id
  359. function getEntity ($message, $ent_id) {
  360. if ($message) {
  361. if ($message->header->entity_id == $ent_id && strlen($ent_id) == strlen($message->header->entity_id)) {
  362. return $message;
  363. } else {
  364. for ($i = 0; $message->entities[$i]; $i++) {
  365. $msg = getEntity ($message->entities[$i], $ent_id);
  366. if ($msg)
  367. return $msg;
  368. }
  369. }
  370. }
  371. }
  372. // figures out what entity to display and returns the $message object
  373. // for that entity.
  374. function findDisplayEntity ($message) {
  375. if ($message) {
  376. if ($message->header->type0 == "text") {
  377. if ($message->header->type1 == "plain" ||
  378. $message->header->type1 == "html") {
  379. return $message->header->entity_id;
  380. }
  381. } else {
  382. for ($i=0; $message->entities[$i]; $i++) {
  383. return findDisplayEntity($message->entities[$i]);
  384. }
  385. }
  386. }
  387. }
  388. /** This returns a parsed string called $body. That string can then
  389. be displayed as the actual message in the HTML. It contains
  390. everything needed, including HTML Tags, Attachments at the
  391. bottom, etc.
  392. **/
  393. function formatBody($imap_stream, $message, $color, $wrap_at) {
  394. // this if statement checks for the entity to show as the
  395. // primary message. To add more of them, just put them in the
  396. // order that is their priority.
  397. global $startMessage, $username, $key, $imapServerAddress, $imapPort;
  398. $id = $message->header->id;
  399. $urlmailbox = urlencode($message->header->mailbox);
  400. // Get the right entity and redefine message to be this entity
  401. $ent_num = findDisplayEntity ($message);
  402. $body_message = getEntity($message, $ent_num);
  403. if (($body_message->header->type0 == "text") ||
  404. ($body_message->header->type0 == "rfc822")) {
  405. $body = mime_fetch_body ($imap_stream, $id, $ent_num);
  406. $body = decodeBody($body, $body_message->header->encoding);
  407. // If there are other types that shouldn't be formatted, add
  408. // them here
  409. if ($body_message->header->type1 != "html") {
  410. translateText($body, $wrap_at, $body_message->header->charset);
  411. }
  412. $body .= "<SMALL><CENTER><A HREF=\"../src/download.php?absolute_dl=true&passed_id=$id&passed_ent_id=$ent_num&mailbox=$urlmailbox\">". _("Download this as a file") ."</A></CENTER><BR></SMALL>";
  413. /** Display the ATTACHMENTS: message if there's more than one part **/
  414. $body .= "</TD></TR></TABLE>";
  415. if ($message->entities) {
  416. $body .= formatAttachments ($message, $ent_num, $message->header->mailbox, $id);
  417. }
  418. } else {
  419. $body .= formatAttachments ($message, -1, $message->header->mailbox, $id);
  420. }
  421. return $body;
  422. }
  423. // A recursive function that returns a list of attachments with links
  424. // to where to download these attachments
  425. function formatAttachments ($message, $ent_id, $mailbox, $id) {
  426. global $where, $what;
  427. global $startMessage, $color;
  428. static $ShownHTML;
  429. if ($ShownHTML == 0)
  430. {
  431. $ShownHTML = 1;
  432. $body .= "<TABLE WIDTH=100% CELLSPACING=0 CELLPADDING=2 BORDER=0 BGCOLOR=\"$color[0]\"><TR>\n";
  433. $body .= "<TH ALIGN=\"left\" BGCOLOR=\"$color[9]\"><B>\n";
  434. $body .= _('Attachments') . ':';
  435. $body .= "</B></TH></TR><TR><TD>\n";
  436. $body .= "<TABLE CELLSPACING=0 CELLPADDING=1 BORDER=0>\n";
  437. $body .= formatAttachments ($message, $ent_id, $mailbox, $id);
  438. $body .= "</TABLE></TD></TR></TABLE>";
  439. return $body;
  440. }
  441. if ($message) {
  442. if (!$message->entities) {
  443. $type0 = strtolower($message->header->type0);
  444. $type1 = strtolower($message->header->type1);
  445. if ($message->header->entity_id != $ent_id) {
  446. $filename = decodeHeader($message->header->filename);
  447. if (trim($filename) == "") {
  448. $display_filename = "untitled-".$message->header->entity_id;
  449. } else {
  450. $display_filename = $filename;
  451. }
  452. $urlMailbox = urlencode($mailbox);
  453. $ent = urlencode($message->header->entity_id);
  454. $DefaultLink =
  455. "../src/download.php?startMessage=$startMessage&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent";
  456. if ($where && $what)
  457. $DefaultLink .= '&where=' . urlencode($where) . '&what=' . urlencode($what);
  458. $Links['download link']['text'] = _('download');
  459. $Links['download link']['href'] =
  460. "../src/download.php?absolute_dl=true&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent";
  461. $ImageURL = '';
  462. $HookResults = do_hook("attachment $type0/$type1", $Links,
  463. $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
  464. $display_filename, $where, $what);
  465. $Links = $HookResults[1];
  466. $DefaultLink = $HookResults[6];
  467. $body .= '<TR><TD>&nbsp;&nbsp;</TD><TD>';
  468. $body .= "<A HREF=\"$DefaultLink\">$display_filename</A>&nbsp;</TD>";
  469. $body .= '<TD><SMALL><b>' . show_readable_size($message->header->size) .
  470. '</b>&nbsp;&nbsp;</small></TD>';
  471. $body .= "<TD><SMALL>[ $type0/$type1 ]&nbsp;</SMALL></TD>";
  472. $body .= '<TD><SMALL>';
  473. if ($message->header->description)
  474. $body .= '<b>' . htmlspecialchars($message->header->description) . '</b>';
  475. $body .= '</SMALL></TD><TD><SMALL>&nbsp;';
  476. $SkipSpaces = 1;
  477. foreach ($Links as $Val)
  478. {
  479. if ($SkipSpaces)
  480. {
  481. $SkipSpaces = 0;
  482. }
  483. else
  484. {
  485. $body .= '&nbsp;&nbsp;|&nbsp;&nbsp;';
  486. }
  487. $body .= '<a href="' . $Val['href'] . '">' . $Val['text'] . '</a>';
  488. }
  489. unset($Links);
  490. $body .= "</SMALL></TD></TR>\n";
  491. }
  492. return $body;
  493. } else {
  494. for ($i = 0; $i < count($message->entities); $i++) {
  495. $body .= formatAttachments ($message->entities[$i], $ent_id, $mailbox, $id);
  496. }
  497. return $body;
  498. }
  499. }
  500. }
  501. /** this function decodes the body depending on the encoding type. **/
  502. function decodeBody($body, $encoding) {
  503. $body = str_replace("\r\n", "\n", $body);
  504. $encoding = strtolower($encoding);
  505. if ($encoding == "quoted-printable") {
  506. $body = quoted_printable_decode($body);
  507. while (ereg("=\n", $body))
  508. $body = ereg_replace ("=\n", "", $body);
  509. } else if ($encoding == "base64") {
  510. $body = base64_decode($body);
  511. }
  512. // All other encodings are returned raw.
  513. return $body;
  514. }
  515. // This functions decode strings that is encoded according to
  516. // RFC1522 (MIME Part Two: Message Header Extensions for Non-ASCII Text).
  517. function decodeHeader ($string) {
  518. if (eregi('=\?([^?]+)\?(q|b)\?([^?]+)\?=',
  519. $string, $res)) {
  520. if (ucfirst($res[2]) == "B") {
  521. $replace = base64_decode($res[3]);
  522. } else {
  523. $replace = ereg_replace("_", " ", $res[3]);
  524. // Convert lowercase Quoted Printable to uppercase for
  525. // quoted_printable_decode to understand it.
  526. while (ereg("(=([0-9][a-f])|([a-f][0-9])|([a-f][0-9]))", $replace, $res)) {
  527. $replace = str_replace($res[1], strtoupper($res[1]), $replace);
  528. }
  529. $replace = quoted_printable_decode($replace);
  530. }
  531. $replace = charset_decode ($res[1], $replace);
  532. $string = eregi_replace
  533. ('=\?([^?]+)\?(q|b)\?([^?]+)\?=',
  534. $replace, $string);
  535. // In case there should be more encoding in the string: recurse
  536. return (decodeHeader($string));
  537. } else
  538. return ($string);
  539. }
  540. // Encode a string according to RFC 1522 for use in headers if it
  541. // contains 8-bit characters or anything that looks like it should
  542. // be encoded.
  543. function encodeHeader ($string) {
  544. global $default_charset;
  545. // Encode only if the string contains 8-bit characters or =?
  546. if (ereg("([\200-\377]|=\\?)", $string)) {
  547. $newstring = "=?$default_charset?Q?";
  548. // First the special characters
  549. $string = str_replace("=", "=3D", $string);
  550. $string = str_replace("?", "=3F", $string);
  551. $string = str_replace("_", "=5F", $string);
  552. $string = str_replace(" ", "_", $string);
  553. while (ereg("([\200-\377])", $string, $regs)) {
  554. $replace = $regs[1];
  555. $insert = "=" . strtoupper(bin2hex($replace));
  556. $string = str_replace($replace, $insert, $string);
  557. }
  558. $newstring = "=?$default_charset?Q?".$string."?=";
  559. return $newstring;
  560. }
  561. return $string;
  562. }
  563. ?>