PageRenderTime 57ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/program/include/rcube_message.php

https://github.com/netconstructor/roundcubemail
PHP | 758 lines | 442 code | 109 blank | 207 comment | 181 complexity | f5206aee25016d65d302a4ca761a4c19 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
  1. <?php
  2. /*
  3. +-----------------------------------------------------------------------+
  4. | program/include/rcube_message.php |
  5. | |
  6. | This file is part of the Roundcube Webmail client |
  7. | Copyright (C) 2008-2010, The Roundcube Dev Team |
  8. | |
  9. | Licensed under the GNU General Public License version 3 or |
  10. | any later version with exceptions for skins & plugins. |
  11. | See the README file for a full license statement. |
  12. | |
  13. | PURPOSE: |
  14. | Logical representation of a mail message with all its data |
  15. | and related functions |
  16. +-----------------------------------------------------------------------+
  17. | Author: Thomas Bruederli <roundcube@gmail.com> |
  18. +-----------------------------------------------------------------------+
  19. */
  20. /**
  21. * Logical representation of a mail message with all its data
  22. * and related functions
  23. *
  24. * @package Framework
  25. * @subpackage Storage
  26. * @author Thomas Bruederli <roundcube@gmail.com>
  27. */
  28. class rcube_message
  29. {
  30. /**
  31. * Instace of framework class.
  32. *
  33. * @var rcube
  34. */
  35. private $app;
  36. /**
  37. * Instance of storage class
  38. *
  39. * @var rcube_storage
  40. */
  41. private $storage;
  42. /**
  43. * Instance of mime class
  44. *
  45. * @var rcube_mime
  46. */
  47. private $mime;
  48. private $opt = array();
  49. private $parse_alternative = false;
  50. public $uid;
  51. public $folder;
  52. public $headers;
  53. public $parts = array();
  54. public $mime_parts = array();
  55. public $inline_parts = array();
  56. public $attachments = array();
  57. public $subject = '';
  58. public $sender = null;
  59. public $is_safe = false;
  60. /**
  61. * __construct
  62. *
  63. * Provide a uid, and parse message structure.
  64. *
  65. * @param string $uid The message UID.
  66. * @param string $folder Folder name
  67. *
  68. * @see self::$app, self::$storage, self::$opt, self::$parts
  69. */
  70. function __construct($uid, $folder = null)
  71. {
  72. $this->uid = $uid;
  73. $this->app = rcube::get_instance();
  74. $this->storage = $this->app->get_storage();
  75. $this->folder = strlen($folder) ? $folder : $this->storage->get_folder();
  76. $this->storage->set_options(array('all_headers' => true));
  77. // Set current folder
  78. $this->storage->set_folder($this->folder);
  79. $this->headers = $this->storage->get_message($uid);
  80. if (!$this->headers)
  81. return;
  82. $this->mime = new rcube_mime($this->headers->charset);
  83. $this->subject = $this->mime->decode_mime_string($this->headers->subject);
  84. list(, $this->sender) = each($this->mime->decode_address_list($this->headers->from, 1));
  85. $this->set_safe((intval($_GET['_safe']) || $_SESSION['safe_messages'][$uid]));
  86. $this->opt = array(
  87. 'safe' => $this->is_safe,
  88. 'prefer_html' => $this->app->config->get('prefer_html'),
  89. 'get_url' => $this->app->url(array(
  90. 'action' => 'get',
  91. 'mbox' => $this->storage->get_folder(),
  92. 'uid' => $uid))
  93. );
  94. if (!empty($this->headers->structure)) {
  95. $this->get_mime_numbers($this->headers->structure);
  96. $this->parse_structure($this->headers->structure);
  97. }
  98. else {
  99. $this->body = $this->storage->get_body($uid);
  100. }
  101. // notify plugins and let them analyze this structured message object
  102. $this->app->plugins->exec_hook('message_load', array('object' => $this));
  103. }
  104. /**
  105. * Return a (decoded) message header
  106. *
  107. * @param string $name Header name
  108. * @param bool $row Don't mime-decode the value
  109. * @return string Header value
  110. */
  111. public function get_header($name, $raw = false)
  112. {
  113. if (empty($this->headers))
  114. return null;
  115. if ($this->headers->$name)
  116. $value = $this->headers->$name;
  117. else if ($this->headers->others[$name])
  118. $value = $this->headers->others[$name];
  119. return $raw ? $value : $this->mime->decode_header($value);
  120. }
  121. /**
  122. * Set is_safe var and session data
  123. *
  124. * @param bool $safe enable/disable
  125. */
  126. public function set_safe($safe = true)
  127. {
  128. $this->is_safe = $safe;
  129. $_SESSION['safe_messages'][$this->uid] = $this->is_safe;
  130. }
  131. /**
  132. * Compose a valid URL for getting a message part
  133. *
  134. * @param string $mime_id Part MIME-ID
  135. * @return string URL or false if part does not exist
  136. */
  137. public function get_part_url($mime_id, $embed = false)
  138. {
  139. if ($this->mime_parts[$mime_id])
  140. return $this->opt['get_url'] . '&_part=' . $mime_id . ($embed ? '&_embed=1' : '');
  141. else
  142. return false;
  143. }
  144. /**
  145. * Get content of a specific part of this message
  146. *
  147. * @param string $mime_id Part MIME-ID
  148. * @param resource $fp File pointer to save the message part
  149. * @param boolean $skip_charset_conv Disables charset conversion
  150. *
  151. * @return string Part content
  152. */
  153. public function get_part_content($mime_id, $fp = null, $skip_charset_conv = false)
  154. {
  155. if ($part = $this->mime_parts[$mime_id]) {
  156. // stored in message structure (winmail/inline-uuencode)
  157. if (!empty($part->body) || $part->encoding == 'stream') {
  158. if ($fp) {
  159. fwrite($fp, $part->body);
  160. }
  161. return $fp ? true : $part->body;
  162. }
  163. // get from IMAP
  164. $this->storage->set_folder($this->folder);
  165. return $this->storage->get_message_part($this->uid, $mime_id, $part, NULL, $fp, $skip_charset_conv);
  166. }
  167. }
  168. /**
  169. * Determine if the message contains a HTML part
  170. *
  171. * @param bool $recursive Enables checking in all levels of the structure
  172. * @param bool $enriched Enables checking for text/enriched parts too
  173. *
  174. * @return bool True if a HTML is available, False if not
  175. */
  176. function has_html_part($recursive = true, $enriched = false)
  177. {
  178. // check all message parts
  179. foreach ($this->parts as $part) {
  180. if ($part->mimetype == 'text/html' || ($enriched && $part->mimetype == 'text/enriched')) {
  181. // Level check, we'll skip e.g. HTML attachments
  182. if (!$recursive) {
  183. $level = explode('.', $part->mime_id);
  184. // Skip if level too deep or part has a file name
  185. if (count($level) > 2 || $part->filename) {
  186. continue;
  187. }
  188. // HTML part can be on the lower level, if not...
  189. if (count($level) > 1) {
  190. array_pop($level);
  191. $parent = $this->mime_parts[join('.', $level)];
  192. // ... parent isn't multipart/alternative or related
  193. if ($parent->mimetype != 'multipart/alternative' && $parent->mimetype != 'multipart/related') {
  194. continue;
  195. }
  196. }
  197. }
  198. return true;
  199. }
  200. }
  201. return false;
  202. }
  203. /**
  204. * Return the first HTML part of this message
  205. *
  206. * @return string HTML message part content
  207. */
  208. function first_html_part()
  209. {
  210. // check all message parts
  211. foreach ($this->mime_parts as $pid => $part) {
  212. if ($part->mimetype == 'text/html') {
  213. return $this->get_part_content($pid);
  214. }
  215. }
  216. }
  217. /**
  218. * Return the first text part of this message
  219. *
  220. * @param rcube_message_part $part Reference to the part if found
  221. * @return string Plain text message/part content
  222. */
  223. function first_text_part(&$part=null)
  224. {
  225. // no message structure, return complete body
  226. if (empty($this->parts))
  227. return $this->body;
  228. // check all message parts
  229. foreach ($this->mime_parts as $mime_id => $part) {
  230. if ($part->mimetype == 'text/plain') {
  231. return $this->get_part_content($mime_id);
  232. }
  233. else if ($part->mimetype == 'text/html') {
  234. $out = $this->get_part_content($mime_id);
  235. // create instance of html2text class
  236. $txt = new html2text($out);
  237. return $txt->get_text();
  238. }
  239. }
  240. $part = null;
  241. return null;
  242. }
  243. /**
  244. * Checks if part of the message is an attachment (or part of it)
  245. *
  246. * @param rcube_message_part $part Message part
  247. *
  248. * @return bool True if the part is an attachment part
  249. */
  250. public function is_attachment($part)
  251. {
  252. foreach ($this->attachments as $att_part) {
  253. if ($att_part->mime_id == $part->mime_id) {
  254. return true;
  255. }
  256. // check if the part is a subpart of another attachment part (message/rfc822)
  257. if ($att_part->mimetype == 'message/rfc822') {
  258. if (in_array($part, (array)$att_part->parts)) {
  259. return true;
  260. }
  261. }
  262. }
  263. return false;
  264. }
  265. /**
  266. * Read the message structure returend by the IMAP server
  267. * and build flat lists of content parts and attachments
  268. *
  269. * @param rcube_message_part $structure Message structure node
  270. * @param bool $recursive True when called recursively
  271. */
  272. private function parse_structure($structure, $recursive = false)
  273. {
  274. // real content-type of message/rfc822 part
  275. if ($structure->mimetype == 'message/rfc822' && $structure->real_mimetype)
  276. $mimetype = $structure->real_mimetype;
  277. else
  278. $mimetype = $structure->mimetype;
  279. // show message headers
  280. if ($recursive && is_array($structure->headers) && isset($structure->headers['subject'])) {
  281. $c = new stdClass;
  282. $c->type = 'headers';
  283. $c->headers = &$structure->headers;
  284. $this->parts[] = $c;
  285. }
  286. // Allow plugins to handle message parts
  287. $plugin = $this->app->plugins->exec_hook('message_part_structure',
  288. array('object' => $this, 'structure' => $structure,
  289. 'mimetype' => $mimetype, 'recursive' => $recursive));
  290. if ($plugin['abort'])
  291. return;
  292. $structure = $plugin['structure'];
  293. list($message_ctype_primary, $message_ctype_secondary) = explode('/', $plugin['mimetype']);
  294. // print body if message doesn't have multiple parts
  295. if ($message_ctype_primary == 'text' && !$recursive) {
  296. $structure->type = 'content';
  297. $this->parts[] = &$structure;
  298. // Parse simple (plain text) message body
  299. if ($message_ctype_secondary == 'plain')
  300. foreach ((array)$this->uu_decode($structure) as $uupart) {
  301. $this->mime_parts[$uupart->mime_id] = $uupart;
  302. $this->attachments[] = $uupart;
  303. }
  304. }
  305. // the same for pgp signed messages
  306. else if ($mimetype == 'application/pgp' && !$recursive) {
  307. $structure->type = 'content';
  308. $this->parts[] = &$structure;
  309. }
  310. // message contains (more than one!) alternative parts
  311. else if ($mimetype == 'multipart/alternative'
  312. && is_array($structure->parts) && count($structure->parts) > 1
  313. ) {
  314. // get html/plaintext parts
  315. $plain_part = $html_part = $print_part = $related_part = null;
  316. foreach ($structure->parts as $p => $sub_part) {
  317. $sub_mimetype = $sub_part->mimetype;
  318. // skip empty text parts
  319. if (!$sub_part->size && preg_match('#^text/(plain|html|enriched)$#', $sub_mimetype)) {
  320. continue;
  321. }
  322. // check if sub part is
  323. if ($sub_mimetype == 'text/plain')
  324. $plain_part = $p;
  325. else if ($sub_mimetype == 'text/html')
  326. $html_part = $p;
  327. else if ($sub_mimetype == 'text/enriched')
  328. $enriched_part = $p;
  329. else if (in_array($sub_mimetype, array('multipart/related', 'multipart/mixed', 'multipart/alternative')))
  330. $related_part = $p;
  331. }
  332. // parse related part (alternative part could be in here)
  333. if ($related_part !== null && !$this->parse_alternative) {
  334. $this->parse_alternative = true;
  335. $this->parse_structure($structure->parts[$related_part], true);
  336. $this->parse_alternative = false;
  337. // if plain part was found, we should unset it if html is preferred
  338. if ($this->opt['prefer_html'] && count($this->parts))
  339. $plain_part = null;
  340. }
  341. // choose html/plain part to print
  342. if ($html_part !== null && $this->opt['prefer_html']) {
  343. $print_part = &$structure->parts[$html_part];
  344. }
  345. else if ($enriched_part !== null) {
  346. $print_part = &$structure->parts[$enriched_part];
  347. }
  348. else if ($plain_part !== null) {
  349. $print_part = &$structure->parts[$plain_part];
  350. }
  351. // add the right message body
  352. if (is_object($print_part)) {
  353. $print_part->type = 'content';
  354. $this->parts[] = $print_part;
  355. }
  356. // show plaintext warning
  357. else if ($html_part !== null && empty($this->parts)) {
  358. $c = new stdClass;
  359. $c->type = 'content';
  360. $c->ctype_primary = 'text';
  361. $c->ctype_secondary = 'plain';
  362. $c->mimetype = 'text/plain';
  363. $c->realtype = 'text/html';
  364. $this->parts[] = $c;
  365. }
  366. // add html part as attachment
  367. if ($html_part !== null && $structure->parts[$html_part] !== $print_part) {
  368. $html_part = &$structure->parts[$html_part];
  369. $html_part->mimetype = 'text/html';
  370. $this->attachments[] = $html_part;
  371. }
  372. }
  373. // this is an ecrypted message -> create a plaintext body with the according message
  374. else if ($mimetype == 'multipart/encrypted') {
  375. $p = new stdClass;
  376. $p->type = 'content';
  377. $p->ctype_primary = 'text';
  378. $p->ctype_secondary = 'plain';
  379. $p->mimetype = 'text/plain';
  380. $p->realtype = 'multipart/encrypted';
  381. $this->parts[] = $p;
  382. }
  383. // message contains multiple parts
  384. else if (is_array($structure->parts) && !empty($structure->parts)) {
  385. // iterate over parts
  386. for ($i=0; $i < count($structure->parts); $i++) {
  387. $mail_part = &$structure->parts[$i];
  388. $primary_type = $mail_part->ctype_primary;
  389. $secondary_type = $mail_part->ctype_secondary;
  390. // real content-type of message/rfc822
  391. if ($mail_part->real_mimetype) {
  392. $part_orig_mimetype = $mail_part->mimetype;
  393. $part_mimetype = $mail_part->real_mimetype;
  394. list($primary_type, $secondary_type) = explode('/', $part_mimetype);
  395. }
  396. else
  397. $part_mimetype = $mail_part->mimetype;
  398. // multipart/alternative
  399. if ($primary_type == 'multipart') {
  400. $this->parse_structure($mail_part, true);
  401. // list message/rfc822 as attachment as well (mostly .eml)
  402. if ($part_orig_mimetype == 'message/rfc822' && !empty($mail_part->filename))
  403. $this->attachments[] = $mail_part;
  404. }
  405. // part text/[plain|html] or delivery status
  406. else if ((($part_mimetype == 'text/plain' || $part_mimetype == 'text/html') && $mail_part->disposition != 'attachment') ||
  407. in_array($part_mimetype, array('message/delivery-status', 'text/rfc822-headers', 'message/disposition-notification'))
  408. ) {
  409. // Allow plugins to handle also this part
  410. $plugin = $this->app->plugins->exec_hook('message_part_structure',
  411. array('object' => $this, 'structure' => $mail_part,
  412. 'mimetype' => $part_mimetype, 'recursive' => true));
  413. if ($plugin['abort'])
  414. continue;
  415. if ($part_mimetype == 'text/html' && $mail_part->size) {
  416. $got_html_part = true;
  417. }
  418. $mail_part = $plugin['structure'];
  419. list($primary_type, $secondary_type) = explode('/', $plugin['mimetype']);
  420. // add text part if it matches the prefs
  421. if (!$this->parse_alternative ||
  422. ($secondary_type == 'html' && $this->opt['prefer_html']) ||
  423. ($secondary_type == 'plain' && !$this->opt['prefer_html'])
  424. ) {
  425. $mail_part->type = 'content';
  426. $this->parts[] = $mail_part;
  427. }
  428. // list as attachment as well
  429. if (!empty($mail_part->filename)) {
  430. $this->attachments[] = $mail_part;
  431. }
  432. // list html part as attachment (here the part is most likely inside a multipart/related part)
  433. else if ($this->parse_alternative && ($secondary_type == 'html' && !$this->opt['prefer_html'])) {
  434. $this->attachments[] = $mail_part;
  435. }
  436. }
  437. // part message/*
  438. else if ($primary_type == 'message') {
  439. $this->parse_structure($mail_part, true);
  440. // list as attachment as well (mostly .eml)
  441. if (!empty($mail_part->filename))
  442. $this->attachments[] = $mail_part;
  443. }
  444. // ignore "virtual" protocol parts
  445. else if ($primary_type == 'protocol') {
  446. continue;
  447. }
  448. // part is Microsoft Outlook TNEF (winmail.dat)
  449. else if ($part_mimetype == 'application/ms-tnef') {
  450. foreach ((array)$this->tnef_decode($mail_part) as $tpart) {
  451. $this->mime_parts[$tpart->mime_id] = $tpart;
  452. $this->attachments[] = $tpart;
  453. }
  454. }
  455. // part is a file/attachment
  456. else if (preg_match('/^(inline|attach)/', $mail_part->disposition) ||
  457. $mail_part->headers['content-id'] ||
  458. ($mail_part->filename &&
  459. (empty($mail_part->disposition) || preg_match('/^[a-z0-9!#$&.+^_-]+$/i', $mail_part->disposition)))
  460. ) {
  461. // skip apple resource forks
  462. if ($message_ctype_secondary == 'appledouble' && $secondary_type == 'applefile')
  463. continue;
  464. // part belongs to a related message and is linked
  465. if ($mimetype == 'multipart/related'
  466. && ($mail_part->headers['content-id'] || $mail_part->headers['content-location'])) {
  467. if ($mail_part->headers['content-id'])
  468. $mail_part->content_id = preg_replace(array('/^</', '/>$/'), '', $mail_part->headers['content-id']);
  469. if ($mail_part->headers['content-location'])
  470. $mail_part->content_location = $mail_part->headers['content-base'] . $mail_part->headers['content-location'];
  471. $this->inline_parts[] = $mail_part;
  472. }
  473. // attachment encapsulated within message/rfc822 part needs further decoding (#1486743)
  474. else if ($part_orig_mimetype == 'message/rfc822') {
  475. $this->parse_structure($mail_part, true);
  476. // list as attachment as well (mostly .eml)
  477. if (!empty($mail_part->filename))
  478. $this->attachments[] = $mail_part;
  479. }
  480. // regular attachment with valid content type
  481. // (content-type name regexp according to RFC4288.4.2)
  482. else if (preg_match('/^[a-z0-9!#$&.+^_-]+\/[a-z0-9!#$&.+^_-]+$/i', $part_mimetype)) {
  483. if (!$mail_part->filename)
  484. $mail_part->filename = 'Part '.$mail_part->mime_id;
  485. $this->attachments[] = $mail_part;
  486. }
  487. // attachment with invalid content type
  488. // replace malformed content type with application/octet-stream (#1487767)
  489. else if ($mail_part->filename) {
  490. $mail_part->ctype_primary = 'application';
  491. $mail_part->ctype_secondary = 'octet-stream';
  492. $mail_part->mimetype = 'application/octet-stream';
  493. $this->attachments[] = $mail_part;
  494. }
  495. }
  496. // attachment part as message/rfc822 (#1488026)
  497. else if ($mail_part->mimetype == 'message/rfc822') {
  498. $this->parse_structure($mail_part);
  499. }
  500. }
  501. // if this was a related part try to resolve references
  502. if ($mimetype == 'multipart/related' && sizeof($this->inline_parts)) {
  503. $a_replaces = array();
  504. $img_regexp = '/^image\/(gif|jpe?g|png|tiff|bmp|svg)/';
  505. foreach ($this->inline_parts as $inline_object) {
  506. $part_url = $this->get_part_url($inline_object->mime_id, true);
  507. if ($inline_object->content_id)
  508. $a_replaces['cid:'.$inline_object->content_id] = $part_url;
  509. if ($inline_object->content_location) {
  510. $a_replaces[$inline_object->content_location] = $part_url;
  511. }
  512. if (!empty($inline_object->filename)) {
  513. // MS Outlook sends sometimes non-related attachments as related
  514. // In this case multipart/related message has only one text part
  515. // We'll add all such attachments to the attachments list
  516. if (!isset($got_html_part) && empty($inline_object->content_id)) {
  517. $this->attachments[] = $inline_object;
  518. }
  519. // MS Outlook sometimes also adds non-image attachments as related
  520. // We'll add all such attachments to the attachments list
  521. // Warning: some browsers support pdf in <img/>
  522. else if (!preg_match($img_regexp, $inline_object->mimetype)) {
  523. $this->attachments[] = $inline_object;
  524. }
  525. // @TODO: we should fetch HTML body and find attachment's content-id
  526. // to handle also image attachments without reference in the body
  527. // @TODO: should we list all image attachments in text mode?
  528. }
  529. }
  530. // add replace array to each content part
  531. // (will be applied later when part body is available)
  532. foreach ($this->parts as $i => $part) {
  533. if ($part->type == 'content')
  534. $this->parts[$i]->replaces = $a_replaces;
  535. }
  536. }
  537. }
  538. // message is a single part non-text
  539. else if ($structure->filename) {
  540. $this->attachments[] = $structure;
  541. }
  542. // message is a single part non-text (without filename)
  543. else if (preg_match('/application\//i', $mimetype)) {
  544. $structure->filename = 'Part '.$structure->mime_id;
  545. $this->attachments[] = $structure;
  546. }
  547. }
  548. /**
  549. * Fill aflat array with references to all parts, indexed by part numbers
  550. *
  551. * @param rcube_message_part $part Message body structure
  552. */
  553. private function get_mime_numbers(&$part)
  554. {
  555. if (strlen($part->mime_id))
  556. $this->mime_parts[$part->mime_id] = &$part;
  557. if (is_array($part->parts))
  558. for ($i=0; $i<count($part->parts); $i++)
  559. $this->get_mime_numbers($part->parts[$i]);
  560. }
  561. /**
  562. * Decode a Microsoft Outlook TNEF part (winmail.dat)
  563. *
  564. * @param rcube_message_part $part Message part to decode
  565. * @return array
  566. */
  567. function tnef_decode(&$part)
  568. {
  569. // @TODO: attachment may be huge, hadle it via file
  570. if (!isset($part->body)) {
  571. $this->storage->set_folder($this->folder);
  572. $part->body = $this->storage->get_message_part($this->uid, $part->mime_id, $part);
  573. }
  574. $parts = array();
  575. $tnef = new tnef_decoder;
  576. $tnef_arr = $tnef->decompress($part->body);
  577. foreach ($tnef_arr as $pid => $winatt) {
  578. $tpart = new rcube_message_part;
  579. $tpart->filename = trim($winatt['name']);
  580. $tpart->encoding = 'stream';
  581. $tpart->ctype_primary = trim(strtolower($winatt['type']));
  582. $tpart->ctype_secondary = trim(strtolower($winatt['subtype']));
  583. $tpart->mimetype = $tpart->ctype_primary . '/' . $tpart->ctype_secondary;
  584. $tpart->mime_id = 'winmail.' . $part->mime_id . '.' . $pid;
  585. $tpart->size = $winatt['size'];
  586. $tpart->body = $winatt['stream'];
  587. $parts[] = $tpart;
  588. unset($tnef_arr[$pid]);
  589. }
  590. return $parts;
  591. }
  592. /**
  593. * Parse message body for UUencoded attachments bodies
  594. *
  595. * @param rcube_message_part $part Message part to decode
  596. * @return array
  597. */
  598. function uu_decode(&$part)
  599. {
  600. // @TODO: messages may be huge, hadle body via file
  601. if (!isset($part->body)) {
  602. $this->storage->set_folder($this->folder);
  603. $part->body = $this->storage->get_message_part($this->uid, $part->mime_id, $part);
  604. }
  605. $parts = array();
  606. // FIXME: line length is max.65?
  607. $uu_regexp = '/begin [0-7]{3,4} ([^\n]+)\n/s';
  608. if (preg_match_all($uu_regexp, $part->body, $matches, PREG_SET_ORDER)) {
  609. // update message content-type
  610. $part->ctype_primary = 'multipart';
  611. $part->ctype_secondary = 'mixed';
  612. $part->mimetype = $part->ctype_primary . '/' . $part->ctype_secondary;
  613. $uu_endstring = "`\nend\n";
  614. // add attachments to the structure
  615. foreach ($matches as $pid => $att) {
  616. $startpos = strpos($part->body, $att[1]) + strlen($att[1]) + 1; // "\n"
  617. $endpos = strpos($part->body, $uu_endstring);
  618. $filebody = substr($part->body, $startpos, $endpos-$startpos);
  619. // remove attachments bodies from the message body
  620. $part->body = substr_replace($part->body, "", $startpos, $endpos+strlen($uu_endstring)-$startpos);
  621. $uupart = new rcube_message_part;
  622. $uupart->filename = trim($att[1]);
  623. $uupart->encoding = 'stream';
  624. $uupart->body = convert_uudecode($filebody);
  625. $uupart->size = strlen($uupart->body);
  626. $uupart->mime_id = 'uu.' . $part->mime_id . '.' . $pid;
  627. $ctype = rcube_mime::content_type($uupart->body, $uupart->filename, 'application/octet-stream', true);
  628. $uupart->mimetype = $ctype;
  629. list($uupart->ctype_primary, $uupart->ctype_secondary) = explode('/', $ctype);
  630. $parts[] = $uupart;
  631. unset($matches[$pid]);
  632. }
  633. // remove attachments bodies from the message body
  634. $part->body = preg_replace($uu_regexp, '', $part->body);
  635. }
  636. return $parts;
  637. }
  638. /**
  639. * Deprecated methods (to be removed)
  640. */
  641. public static function unfold_flowed($text)
  642. {
  643. return rcube_mime::unfold_flowed($text);
  644. }
  645. public static function format_flowed($text, $length = 72)
  646. {
  647. return rcube_mime::format_flowed($text, $length);
  648. }
  649. }