mime.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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. $size = substr ($topline, strpos($topline, "{")+1);
  330. $size = substr ($size, 0, strpos($size, "}"));
  331. $read = fread ($imap_stream, $size);
  332. return $read;
  333. }
  334. /* -[ END MIME DECODING ]----------------------------------------------------------- */
  335. /** This is the first function called. It decides if this is a multipart
  336. message or if it should be handled as a single entity
  337. **/
  338. function decodeMime ($imap_stream, $body, $header) {
  339. global $username, $key, $imapServerAddress, $imapPort;
  340. return mime_structure ($imap_stream, $header);
  341. }
  342. // This is here for debugging purposese. It will print out a list
  343. // of all the entity IDs that are in the $message object.
  344. function listEntities ($message) {
  345. if ($message) {
  346. if ($message->header->entity_id)
  347. echo "<tt>" . $message->header->entity_id . " : " . $message->header->type0 . "/" . $message->header->type1 . "<br>";
  348. for ($i = 0; $message->entities[$i]; $i++) {
  349. $msg = listEntities($message->entities[$i], $ent_id);
  350. if ($msg)
  351. return $msg;
  352. }
  353. }
  354. }
  355. // returns a $message object for a particular entity id
  356. function getEntity ($message, $ent_id) {
  357. if ($message) {
  358. if ($message->header->entity_id == $ent_id && strlen($ent_id) == strlen($message->header->entity_id)) {
  359. return $message;
  360. } else {
  361. for ($i = 0; $message->entities[$i]; $i++) {
  362. $msg = getEntity ($message->entities[$i], $ent_id);
  363. if ($msg)
  364. return $msg;
  365. }
  366. }
  367. }
  368. }
  369. // figures out what entity to display and returns the $message object
  370. // for that entity.
  371. function findDisplayEntity ($message) {
  372. if ($message) {
  373. if ($message->header->type0 == "text") {
  374. if ($message->header->type1 == "plain" ||
  375. $message->header->type1 == "html") {
  376. return $message->header->entity_id;
  377. }
  378. } else {
  379. for ($i=0; $message->entities[$i]; $i++) {
  380. return findDisplayEntity($message->entities[$i]);
  381. }
  382. }
  383. }
  384. }
  385. /** This returns a parsed string called $body. That string can then
  386. be displayed as the actual message in the HTML. It contains
  387. everything needed, including HTML Tags, Attachments at the
  388. bottom, etc.
  389. **/
  390. function formatBody($imap_stream, $message, $color, $wrap_at) {
  391. // this if statement checks for the entity to show as the
  392. // primary message. To add more of them, just put them in the
  393. // order that is their priority.
  394. global $startMessage, $username, $key, $imapServerAddress, $imapPort;
  395. $id = $message->header->id;
  396. $urlmailbox = urlencode($message->header->mailbox);
  397. // Get the right entity and redefine message to be this entity
  398. $ent_num = findDisplayEntity ($message);
  399. $body_message = getEntity($message, $ent_num);
  400. if (($body_message->header->type0 == "text") ||
  401. ($body_message->header->type0 == "rfc822")) {
  402. $body = mime_fetch_body ($imap_stream, $id, $ent_num);
  403. $body = decodeBody($body, $body_message->header->encoding);
  404. // If there are other types that shouldn't be formatted, add
  405. // them here
  406. if ($message->header->type1 != "html") {
  407. translateText($body, $wrap_at, $body_message->header->charset);
  408. }
  409. $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>";
  410. /** Display the ATTACHMENTS: message if there's more than one part **/
  411. $body .= "</TD></TR></TABLE>";
  412. if ($message->entities) {
  413. $body .= formatAttachments ($message, $ent_num, $message->header->mailbox, $id);
  414. }
  415. } else {
  416. $body .= formatAttachments ($message, -1, $message->header->mailbox, $id);
  417. }
  418. return $body;
  419. }
  420. // A recursive function that returns a list of attachments with links
  421. // to where to download these attachments
  422. function formatAttachments ($message, $ent_id, $mailbox, $id) {
  423. global $where, $what;
  424. global $startMessage, $color;
  425. static $ShownHTML;
  426. if ($ShownHTML == 0)
  427. {
  428. $ShownHTML = 1;
  429. $body .= "<TABLE WIDTH=100% CELLSPACING=0 CELLPADDING=2 BORDER=0 BGCOLOR=\"$color[0]\"><TR>\n";
  430. $body .= "<TH ALIGN=\"left\" BGCOLOR=\"$color[9]\"><B>\n";
  431. $body .= _('Attachments') . ':';
  432. $body .= "</B></TH></TR><TR><TD>\n";
  433. $body .= "<TABLE CELLSPACING=0 CELLPADDING=1 BORDER=0>\n";
  434. $body .= formatAttachments ($message, $ent_id, $mailbox, $id);
  435. $body .= "</TABLE></TD></TR></TABLE>";
  436. return $body;
  437. }
  438. if ($message) {
  439. if (!$message->entities) {
  440. $type0 = strtolower($message->header->type0);
  441. $type1 = strtolower($message->header->type1);
  442. if ($message->header->entity_id != $ent_id) {
  443. $filename = decodeHeader($message->header->filename);
  444. if (trim($filename) == "") {
  445. $display_filename = "untitled-".$message->header->entity_id;
  446. } else {
  447. $display_filename = $filename;
  448. }
  449. $urlMailbox = urlencode($mailbox);
  450. $ent = urlencode($message->header->entity_id);
  451. $DefaultLink =
  452. "../src/download.php?startMessage=$startMessage&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent";
  453. if ($where && $what)
  454. $DefaultLink .= '&where=' . urlencode($where) . '&what=' . urlencode($what);
  455. $Links['download link']['text'] = _('download');
  456. $Links['download link']['href'] =
  457. "../src/download.php?absolute_dl=true&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent";
  458. $ImageURL = '';
  459. $HookResults = do_hook("attachment $type0/$type1", $Links,
  460. $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
  461. $display_filename, $where, $what);
  462. $Links = $HookResults[1];
  463. $DefaultLink = $HookResults[6];
  464. $body .= '<TR><TD>&nbsp;&nbsp;</TD><TD>';
  465. $body .= "<A HREF=\"$DefaultLink\">$display_filename</A>&nbsp;</TD>";
  466. $body .= '<TD><SMALL><b>' . show_readable_size($message->header->size) .
  467. '</b>&nbsp;&nbsp;</small></TD>';
  468. $body .= "<TD><SMALL>[ $type0/$type1 ]&nbsp;</SMALL></TD>";
  469. $body .= '<TD><SMALL>';
  470. if ($message->header->description)
  471. $body .= '<b>' . htmlspecialchars($message->header->description) . '</b>';
  472. $body .= '</SMALL></TD><TD><SMALL>&nbsp;';
  473. $SkipSpaces = 1;
  474. foreach ($Links as $Val)
  475. {
  476. if ($SkipSpaces)
  477. {
  478. $SkipSpaces = 0;
  479. }
  480. else
  481. {
  482. $body .= '&nbsp;&nbsp;|&nbsp;&nbsp;';
  483. }
  484. $body .= '<a href="' . $Val['href'] . '">' . $Val['text'] . '</a>';
  485. }
  486. unset($Links);
  487. $body .= "</SMALL></TD></TR>\n";
  488. }
  489. return $body;
  490. } else {
  491. for ($i = 0; $i < count($message->entities); $i++) {
  492. $body .= formatAttachments ($message->entities[$i], $ent_id, $mailbox, $id);
  493. }
  494. return $body;
  495. }
  496. }
  497. }
  498. /** this function decodes the body depending on the encoding type. **/
  499. function decodeBody($body, $encoding) {
  500. $body = str_replace("\r\n", "\n", $body);
  501. $encoding = strtolower($encoding);
  502. if ($encoding == "quoted-printable") {
  503. $body = quoted_printable_decode($body);
  504. while (ereg("=\n", $body))
  505. $body = ereg_replace ("=\n", "", $body);
  506. } else if ($encoding == "base64") {
  507. $body = base64_decode($body);
  508. }
  509. // All other encodings are returned raw.
  510. return $body;
  511. }
  512. // This functions decode strings that is encoded according to
  513. // RFC1522 (MIME Part Two: Message Header Extensions for Non-ASCII Text).
  514. function decodeHeader ($string) {
  515. if (eregi('=\?([^?]+)\?(q|b)\?([^?]+)\?=',
  516. $string, $res)) {
  517. if (ucfirst($res[2]) == "B") {
  518. $replace = base64_decode($res[3]);
  519. } else {
  520. $replace = ereg_replace("_", " ", $res[3]);
  521. // Convert lowercase Quoted Printable to uppercase for
  522. // quoted_printable_decode to understand it.
  523. while (ereg("(=([0-9][a-f])|([a-f][0-9])|([a-f][0-9]))", $replace, $res)) {
  524. $replace = str_replace($res[1], strtoupper($res[1]), $replace);
  525. }
  526. $replace = quoted_printable_decode($replace);
  527. }
  528. $replace = charset_decode ($res[1], $replace);
  529. $string = eregi_replace
  530. ('=\?([^?]+)\?(q|b)\?([^?]+)\?=',
  531. $replace, $string);
  532. // In case there should be more encoding in the string: recurse
  533. return (decodeHeader($string));
  534. } else
  535. return ($string);
  536. }
  537. // Encode a string according to RFC 1522 for use in headers if it
  538. // contains 8-bit characters or anything that looks like it should
  539. // be encoded.
  540. function encodeHeader ($string) {
  541. global $default_charset;
  542. // Encode only if the string contains 8-bit characters or =?
  543. if (ereg("([\200-\377])|=\\?", $string)) {
  544. $newstring = "=?$default_charset?Q?";
  545. // First the special characters
  546. $string = str_replace("=", "=3D", $string);
  547. $string = str_replace("?", "=3F", $string);
  548. $string = str_replace("_", "=5F", $string);
  549. $string = str_replace(" ", "_", $string);
  550. while (ereg("([\200-\377])", $string, $regs)) {
  551. $replace = $regs[1];
  552. $insert = "=" . strtoupper(bin2hex($replace));
  553. $string = str_replace($replace, $insert, $string);
  554. }
  555. $newstring = "=?$default_charset?Q?".$string."?=";
  556. return $newstring;
  557. }
  558. return $string;
  559. }
  560. ?>