alecpl
2010-10-04 e25a357d956c263c90f1c816395418ef4dbc2939
commit | author | age
8fa58e 1 <?php
T 2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/include/rcube_message.php                                     |
6  |                                                                       |
e019f2 7  | This file is part of the Roundcube Webmail client                     |
A 8  | Copyright (C) 2008-2010, Roundcube Dev. - Switzerland                 |
8fa58e 9  | Licensed under the GNU GPL                                            |
T 10  |                                                                       |
11  | PURPOSE:                                                              |
12  |   Logical representation of a mail message with all its data          |
13  |   and related functions                                               |
14  +-----------------------------------------------------------------------+
15  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16  +-----------------------------------------------------------------------+
17
638fb8 18  $Id$
8fa58e 19
T 20 */
21
22
23 /**
45f56c 24  * Logical representation of a mail message with all its data
T 25  * and related functions
8fa58e 26  *
T 27  * @package    Mail
28  * @author     Thomas Bruederli <roundcube@gmail.com>
29  */
30 class rcube_message
31 {
5c461b 32     /**
A 33      * Instace of rcmail.
34      *
35      * @var rcmail
36      */
d311d8 37     private $app;
5c461b 38
A 39     /**
40      * Instance of imap class
41      *
42      * @var rcube_imap
43      */
d311d8 44     private $imap;
A 45     private $opt = array();
46     private $inline_parts = array();
47     private $parse_alternative = false;
8fa58e 48   
d311d8 49     public $uid = null;
A 50     public $headers;
51     public $structure;
52     public $parts = array();
53     public $mime_parts = array();
54     public $attachments = array();
55     public $subject = '';
56     public $sender = null;
57     public $is_safe = false;
8fa58e 58   
193fb4 59
d311d8 60     /**
A 61      * __construct
62      *
63      * Provide a uid, and parse message structure.
64      *
65      * @param string $uid The message UID.
66      *
67      * @uses rcmail::get_instance()
68      * @uses rcube_imap::decode_mime_string()
69      * @uses self::set_safe()
70      *
71      * @see self::$app, self::$imap, self::$opt, self::$structure
72      */
73     function __construct($uid)
8fa58e 74     {
d311d8 75         $this->app = rcmail::get_instance();
A 76         $this->imap = $this->app->imap;
64e3e8 77
d311d8 78         $this->uid = $uid;
A 79         $this->headers = $this->imap->get_headers($uid, NULL, true, true);
64e3e8 80
A 81         if (!$this->headers)
82             return;
8fa58e 83
d311d8 84         $this->subject = rcube_imap::decode_mime_string(
A 85             $this->headers->subject, $this->headers->charset);
86         list(, $this->sender) = each($this->imap->decode_address_list($this->headers->from));
64e3e8 87
d311d8 88         $this->set_safe((intval($_GET['_safe']) || $_SESSION['safe_messages'][$uid]));
A 89         $this->opt = array(
90             'safe' => $this->is_safe,
91             'prefer_html' => $this->app->config->get('prefer_html'),
92             'get_url' => rcmail_url('get', array(
93                 '_mbox' => $this->imap->get_mailbox_name(), '_uid' => $uid))
94         );
8fa58e 95
d311d8 96         if ($this->structure = $this->imap->get_structure($uid, $this->headers->body_structure)) {
A 97             $this->get_mime_numbers($this->structure);
98             $this->parse_structure($this->structure);
aa16b4 99         }
d311d8 100         else {
A 101             $this->body = $this->imap->get_body($uid);
102         }
103
104         // notify plugins and let them analyze this structured message object
105         $this->app->plugins->exec_hook('message_load', array('object' => $this));
106     }
107   
108   
109     /**
110      * Return a (decoded) message header
111      *
5c461b 112      * @param string $name Header name
A 113      * @param bool   $row  Don't mime-decode the value
d311d8 114      * @return string Header value
A 115      */
116     public function get_header($name, $raw = false)
117     {
e25a35 118         if ($this->headers->$name)
A 119             $value = $this->headers->$name;
120         else if ($this->headers->others[$name])
121             $value = $this->headers->others[$name];
122
d311d8 123         return $raw ? $value : $this->imap->decode_header($value);
A 124     }
125
126   
127     /**
128      * Set is_safe var and session data
129      *
5c461b 130      * @param bool $safe enable/disable
d311d8 131      */
A 132     public function set_safe($safe = true)
133     {
134         $this->is_safe = $safe;
135         $_SESSION['safe_messages'][$this->uid] = $this->is_safe;
136     }
137
138
139     /**
140      * Compose a valid URL for getting a message part
141      *
5c461b 142      * @param string $mime_id Part MIME-ID
d311d8 143      * @return string URL or false if part does not exist
A 144      */
145     public function get_part_url($mime_id)
146     {
147         if ($this->mime_parts[$mime_id])
148             return $this->opt['get_url'] . '&_part=' . $mime_id;
aa16b4 149         else
d311d8 150             return false;
8fa58e 151     }
T 152
d311d8 153
A 154     /**
155      * Get content of a specific part of this message
156      *
5c461b 157      * @param string $mime_id Part MIME-ID
A 158      * @param resource $fp File pointer to save the message part
d311d8 159      * @return string Part content
A 160      */
161     public function get_part_content($mime_id, $fp=NULL)
162     {
163         if ($part = $this->mime_parts[$mime_id]) {
164             // stored in message structure (winmail/inline-uuencode)
165             if ($part->encoding == 'stream') {
166                 if ($fp) {
167                     fwrite($fp, $part->body);
168                 }
169                 return $fp ? true : $part->body;
170             }
171             // get from IMAP
172             return $this->imap->get_message_part($this->uid, $mime_id, $part, NULL, $fp);
173         } else
174             return null;
8fa58e 175     }
T 176
177
d311d8 178     /**
A 179      * Determine if the message contains a HTML part
180      *
181      * @return bool True if a HTML is available, False if not
182      */
183     function has_html_part()
184     {
185         // check all message parts
186         foreach ($this->parts as $pid => $part) {
187             $mimetype = strtolower($part->ctype_primary . '/' . $part->ctype_secondary);
188             if ($mimetype == 'text/html')
189                 return true;
190         }
191
192         return false;
193     }
194
195
196     /**
197      * Return the first HTML part of this message
198      *
199      * @return string HTML message part content
200      */
201     function first_html_part()
202     {
203         // check all message parts
204         foreach ($this->mime_parts as $mime_id => $part) {
205             $mimetype = strtolower($part->ctype_primary . '/' . $part->ctype_secondary);
206             if ($mimetype == 'text/html') {
207                 return $this->imap->get_message_part($this->uid, $mime_id, $part);
208             }
209         }
210     }
211
212
213     /**
214      * Return the first text part of this message
215      *
216      * @return string Plain text message/part content
217      */
218     function first_text_part()
219     {
220         // no message structure, return complete body
221         if (empty($this->parts))
222             return $this->body;
223
224         $out = null;
225
226         // check all message parts
227         foreach ($this->mime_parts as $mime_id => $part) {
228             $mimetype = $part->ctype_primary . '/' . $part->ctype_secondary;
229
230             if ($mimetype == 'text/plain') {
231                 $out = $this->imap->get_message_part($this->uid, $mime_id, $part);
99b8c1 232
d311d8 233                 // re-format format=flowed content
A 234                 if ($part->ctype_secondary == 'plain' && $part->ctype_parameters['format'] == 'flowed')
235                     $out = self::unfold_flowed($out);
236                 break;
237             }
238             else if ($mimetype == 'text/html') {
99b8c1 239                 $out = $this->imap->get_message_part($this->uid, $mime_id, $part);
d311d8 240
A 241                 // remove special chars encoding
242                 $trans = array_flip(get_html_translation_table(HTML_ENTITIES));
99b8c1 243                 $out = strtr($out, $trans);
d311d8 244
A 245                 // create instance of html2text class
99b8c1 246                 $txt = new html2text($out);
d311d8 247                 $out = $txt->get_text();
A 248             }
249         }
250
251         return $out;
252     }
253
254
255     /**
256      * Raad the message structure returend by the IMAP server
257      * and build flat lists of content parts and attachments
258      *
5c461b 259      * @param rcube_message_part $structure Message structure node
A 260      * @param bool               $recursive True when called recursively
d311d8 261      */
A 262     private function parse_structure($structure, $recursive = false)
263     {
264         // real content-type of message/rfc822 part
eef0da 265         if ($structure->mimetype == 'message/rfc822' && $structure->real_mimetype)
5ced9c 266             $mimetype = $structure->real_mimetype;
A 267         else
268             $mimetype = $structure->mimetype;
d311d8 269
A 270         // show message headers
271         if ($recursive && is_array($structure->headers) && isset($structure->headers['subject'])) {
272             $c = new stdClass;
273             $c->type = 'headers';
274             $c->headers = &$structure->headers;
275             $this->parts[] = $c;
276         }
5ced9c 277
A 278         // Allow plugins to handle message parts
279         $plugin = $this->app->plugins->exec_hook('message_part_structure',
280             array('object' => $this, 'structure' => $structure,
281                 'mimetype' => $mimetype, 'recursive' => $recursive));
282
283         if ($plugin['abort'])
284             return;
285
286         $structure = $plugin['structure'];
287         list($message_ctype_primary, $message_ctype_secondary) = explode('/', $plugin['mimetype']);
d311d8 288
A 289         // print body if message doesn't have multiple parts
290         if ($message_ctype_primary == 'text' && !$recursive) {
291             $structure->type = 'content';
292             $this->parts[] = &$structure;
293             
294             // Parse simple (plain text) message body
295             if ($message_ctype_secondary == 'plain')
296                 foreach ((array)$this->uu_decode($structure) as $uupart) {
297                     $this->mime_parts[$uupart->mime_id] = $uupart;
298                     $this->attachments[] = $uupart;
299                 }
300         }
301         // the same for pgp signed messages
302         else if ($mimetype == 'application/pgp' && !$recursive) {
303             $structure->type = 'content';
304             $this->parts[] = &$structure;
305         }
306         // message contains alternative parts
307         else if ($mimetype == 'multipart/alternative' && is_array($structure->parts)) {
308             // get html/plaintext parts
309             $plain_part = $html_part = $print_part = $related_part = null;
310
311             foreach ($structure->parts as $p => $sub_part) {
312                 $sub_mimetype = $sub_part->mimetype;
313         
314                 // check if sub part is
315                 if ($sub_mimetype == 'text/plain')
316                     $plain_part = $p;
317                 else if ($sub_mimetype == 'text/html')
318                     $html_part = $p;
319                 else if ($sub_mimetype == 'text/enriched')
320                     $enriched_part = $p;
321                 else if (in_array($sub_mimetype, array('multipart/related', 'multipart/mixed', 'multipart/alternative')))
322                     $related_part = $p;
323             }
324
325             // parse related part (alternative part could be in here)
326             if ($related_part !== null && !$this->parse_alternative) {
327                 $this->parse_alternative = true;
328                 $this->parse_structure($structure->parts[$related_part], true);
329                 $this->parse_alternative = false;
330         
331                 // if plain part was found, we should unset it if html is preferred
332                 if ($this->opt['prefer_html'] && count($this->parts))
333                     $plain_part = null;
334             }
335
336             // choose html/plain part to print
337             if ($html_part !== null && $this->opt['prefer_html']) {
338                 $print_part = &$structure->parts[$html_part];
339             }
340             else if ($enriched_part !== null) {
341                 $print_part = &$structure->parts[$enriched_part];
342             }
343             else if ($plain_part !== null) {
344                 $print_part = &$structure->parts[$plain_part];
345             }
346
347             // add the right message body
348             if (is_object($print_part)) {
349                 $print_part->type = 'content';
350                 $this->parts[] = $print_part;
351             }
352             // show plaintext warning
353             else if ($html_part !== null && empty($this->parts)) {
354                 $c = new stdClass;
355                 $c->type            = 'content';
356                 $c->ctype_primary   = 'text';
357                 $c->ctype_secondary = 'plain';
358                 $c->body            = rcube_label('htmlmessage');
359
360                 $this->parts[] = $c;
361             }
362
363             // add html part as attachment
364             if ($html_part !== null && $structure->parts[$html_part] !== $print_part) {
365                 $html_part = &$structure->parts[$html_part];
366                 $html_part->filename = rcube_label('htmlmessage');
367                 $html_part->mimetype = 'text/html';
368
369                 $this->attachments[] = $html_part;
370             }
371         }
372         // this is an ecrypted message -> create a plaintext body with the according message
373         else if ($mimetype == 'multipart/encrypted') {
374             $p = new stdClass;
375             $p->type            = 'content';
376             $p->ctype_primary   = 'text';
377             $p->ctype_secondary = 'plain';
378             $p->body            = rcube_label('encryptedmessage');
379             $p->size            = strlen($p->body);
380         }
381         // message contains multiple parts
382         else if (is_array($structure->parts) && !empty($structure->parts)) {
383             // iterate over parts
384             for ($i=0; $i < count($structure->parts); $i++) {
385                 $mail_part      = &$structure->parts[$i];
386                 $primary_type   = $mail_part->ctype_primary;
387                 $secondary_type = $mail_part->ctype_secondary;
8fa58e 388
d311d8 389                 // real content-type of message/rfc822
A 390                 if ($mail_part->real_mimetype) {
391                     $part_orig_mimetype = $mail_part->mimetype;
392                     $part_mimetype = $mail_part->real_mimetype;
393                     list($primary_type, $secondary_type) = explode('/', $part_mimetype);
394                 }
395                 else
396                     $part_mimetype = $mail_part->mimetype;
6b6f2e 397
d311d8 398                 // multipart/alternative
A 399                 if ($primary_type == 'multipart') {
400                     $this->parse_structure($mail_part, true);
401
402                     // list message/rfc822 as attachment as well (mostly .eml)
403                     if ($part_orig_mimetype == 'message/rfc822' && !empty($mail_part->filename))
404                         $this->attachments[] = $mail_part;
405                 }
406                 // part text/[plain|html] OR message/delivery-status
407                 else if ((($part_mimetype == 'text/plain' || $part_mimetype == 'text/html') && $mail_part->disposition != 'attachment') ||
408                     $part_mimetype == 'message/delivery-status' || $part_mimetype == 'message/disposition-notification'
409                 ) {
1a2f83 410                     // Allow plugins to handle also this part
A 411                     $plugin = $this->app->plugins->exec_hook('message_part_structure',
412                         array('object' => $this, 'structure' => $mail_part,
413                             'mimetype' => $part_mimetype, 'recursive' => true));
414
415                     if ($plugin['abort'])
416                         continue;
417
418                     $mail_part = $plugin['structure'];
419                     list($primary_type, $secondary_type) = explode('/', $plugin['mimetype']);
420
d311d8 421                     // add text part if it matches the prefs
A 422                     if (!$this->parse_alternative ||
423                         ($secondary_type == 'html' && $this->opt['prefer_html']) ||
424                         ($secondary_type == 'plain' && !$this->opt['prefer_html'])
425                     ) {
426                         $mail_part->type = 'content';
427                         $this->parts[] = $mail_part;
428                     }
429           
430                     // list as attachment as well
431                     if (!empty($mail_part->filename))
432                         $this->attachments[] = $mail_part;
433                 }
434                 // part message/*
435                 else if ($primary_type=='message') {
436                     $this->parse_structure($mail_part, true);
437
438                     // list as attachment as well (mostly .eml)
439                     if (!empty($mail_part->filename))
440                         $this->attachments[] = $mail_part;
441                 }
442                 // ignore "virtual" protocol parts
443                 else if ($primary_type == 'protocol') {
444                     continue;
445                 }
446                 // part is Microsoft Outlook TNEF (winmail.dat)
447                 else if ($part_mimetype == 'application/ms-tnef') {
448                     foreach ((array)$this->tnef_decode($mail_part) as $tpart) {
449                         $this->mime_parts[$tpart->mime_id] = $tpart;
450                         $this->attachments[] = $tpart;
451                     }
452                 }
453                 // part is a file/attachment
454                 else if (preg_match('/^(inline|attach)/', $mail_part->disposition) ||
455                     $mail_part->headers['content-id'] || (empty($mail_part->disposition) && $mail_part->filename)
456                 ) {
457                     // skip apple resource forks
458                     if ($message_ctype_secondary == 'appledouble' && $secondary_type == 'applefile')
459                         continue;
460
461                     // part belongs to a related message and is linked
462                     if ($mimetype == 'multipart/related'
463                         && ($mail_part->headers['content-id'] || $mail_part->headers['content-location'])) {
464                         if ($mail_part->headers['content-id'])
465                             $mail_part->content_id = preg_replace(array('/^</', '/>$/'), '', $mail_part->headers['content-id']);
466                         if ($mail_part->headers['content-location'])
467                             $mail_part->content_location = $mail_part->headers['content-base'] . $mail_part->headers['content-location'];
468
469                         $this->inline_parts[] = $mail_part;
470                     }
471                     // attachment encapsulated within message/rfc822 part needs further decoding (#1486743)
472                     else if ($part_orig_mimetype == 'message/rfc822') {
473                         $this->parse_structure($mail_part, true);
474                     }
475                     // is a regular attachment
476                     else if (preg_match('!^[a-z0-9-.+]+/[a-z0-9-.+]+$!i', $part_mimetype)) {
477                         if (!$mail_part->filename)
478                             $mail_part->filename = 'Part '.$mail_part->mime_id;
479                         $this->attachments[] = $mail_part;
480                     }
481                 }
482             }
483
484             // if this was a related part try to resolve references
485             if ($mimetype == 'multipart/related' && sizeof($this->inline_parts)) {
486                 $a_replaces = array();
487
488                 foreach ($this->inline_parts as $inline_object) {
489                     $part_url = $this->get_part_url($inline_object->mime_id);
490                     if ($inline_object->content_id)
491                         $a_replaces['cid:'.$inline_object->content_id] = $part_url;
492                     if ($inline_object->content_location)
493                         $a_replaces[$inline_object->content_location] = $part_url;
494                 }
495
496                 // add replace array to each content part
497                 // (will be applied later when part body is available)
498                 foreach ($this->parts as $i => $part) {
499                     if ($part->type == 'content')
500                         $this->parts[$i]->replaces = $a_replaces;
501                 }
502             }
503         }
504         // message is a single part non-text
505         else if ($structure->filename) {
506             $this->attachments[] = $structure;
507         }
3e58bf 508         // message is a single part non-text (without filename)
A 509         else if (preg_match('/application\//i', $mimetype)) {
510             $structure->filename = 'Part '.$structure->mime_id;
511             $this->attachments[] = $structure;
512         }
6b6f2e 513     }
d311d8 514
A 515
516     /**
517      * Fill aflat array with references to all parts, indexed by part numbers
518      *
5c461b 519      * @param rcube_message_part $part Message body structure
d311d8 520      */
A 521     private function get_mime_numbers(&$part)
522     {
523         if (strlen($part->mime_id))
524             $this->mime_parts[$part->mime_id] = &$part;
f19d86 525
d311d8 526         if (is_array($part->parts))
A 527             for ($i=0; $i<count($part->parts); $i++)
528                 $this->get_mime_numbers($part->parts[$i]);
529     }
530
531
532     /**
533      * Decode a Microsoft Outlook TNEF part (winmail.dat)
534      *
5c461b 535      * @param rcube_message_part $part Message part to decode
A 536      * @return array
d311d8 537      */
A 538     function tnef_decode(&$part)
539     {
540         // @TODO: attachment may be huge, hadle it via file
541         if (!isset($part->body))
542             $part->body = $this->imap->get_message_part($this->uid, $part->mime_id, $part);
543
544         $parts = array();
f19d86 545         $tnef = new tnef_decoder;
A 546         $tnef_arr = $tnef->decompress($part->body);
d311d8 547
A 548         foreach ($tnef_arr as $pid => $winatt) {
549             $tpart = new rcube_message_part;
550
551             $tpart->filename        = trim($winatt['name']);
552             $tpart->encoding        = 'stream';
f19d86 553             $tpart->ctype_primary   = trim(strtolower($winatt['type']));
A 554             $tpart->ctype_secondary = trim(strtolower($winatt['subtype']));
d311d8 555             $tpart->mimetype        = $tpart->ctype_primary . '/' . $tpart->ctype_secondary;
A 556             $tpart->mime_id         = 'winmail.' . $part->mime_id . '.' . $pid;
557             $tpart->size            = $winatt['size'];
558             $tpart->body            = $winatt['stream'];
559
560             $parts[] = $tpart;
561             unset($tnef_arr[$pid]);
562         }
f19d86 563
d311d8 564         return $parts;
A 565     }
566
567
568     /**
569      * Parse message body for UUencoded attachments bodies
570      *
5c461b 571      * @param rcube_message_part $part Message part to decode
A 572      * @return array
d311d8 573      */
A 574     function uu_decode(&$part)
575     {
576         // @TODO: messages may be huge, hadle body via file
577         if (!isset($part->body))
578             $part->body = $this->imap->get_message_part($this->uid, $part->mime_id, $part);
579
580         $parts = array();
581         // FIXME: line length is max.65?
582         $uu_regexp = '/begin [0-7]{3,4} ([^\n]+)\n(([\x21-\x7E]{0,65}\n)+)`\nend/s';
583
584         if (preg_match_all($uu_regexp, $part->body, $matches, PREG_SET_ORDER)) {
585             // remove attachments bodies from the message body
586             $part->body = preg_replace($uu_regexp, '', $part->body);
587             // update message content-type
588             $part->ctype_primary   = 'multipart';
589             $part->ctype_secondary = 'mixed';
590             $part->mimetype        = $part->ctype_primary . '/' . $part->ctype_secondary;
591
592             // add attachments to the structure
593             foreach ($matches as $pid => $att) {
594                 $uupart = new rcube_message_part;
595
596                 $uupart->filename = trim($att[1]);
597                 $uupart->encoding = 'stream';
598                 $uupart->body     = convert_uudecode($att[2]);
599                 $uupart->size     = strlen($uupart->body);
600                 $uupart->mime_id  = 'uu.' . $part->mime_id . '.' . $pid;
601
602                 $ctype = rc_mime_content_type($uupart->body, $uupart->filename, 'application/octet-stream', true);
603                 $uupart->mimetype = $ctype;
604                 list($uupart->ctype_primary, $uupart->ctype_secondary) = explode('/', $ctype);
605
606                 $parts[] = $uupart;
607                 unset($matches[$pid]);
608             }
609         }
f19d86 610
d311d8 611         return $parts;
A 612     }
613
614
615     /**
616      * Interpret a format=flowed message body according to RFC 2646
617      *
5c461b 618      * @param string  $text Raw body formatted as flowed text
d311d8 619      * @return string Interpreted text with unwrapped lines and stuffed space removed
A 620      */
621     public static function unfold_flowed($text)
622     {
99b8c1 623         $text = preg_split('/\r?\n/', $text);
A 624         $last = -1;
625         $q_level = 0;
626
627         foreach ($text as $idx => $line) {
628             if ($line[0] == '>' && preg_match('/^(>+\s*)/', $line, $regs)) {
629                 $q = strlen(str_replace(' ', '', $regs[0]));
630                 $line = substr($line, strlen($regs[0]));
631
f12d21 632                 if ($q == $q_level && $line
A 633                     && isset($text[$last])
99b8c1 634                     && $text[$last][strlen($text[$last])-1] == ' '
A 635                 ) {
636                     $text[$last] .= $line;
637                     unset($text[$idx]);
638                 }
639                 else {
640                     $last = $idx;
641                 }
642             }
643             else {
644                 $q = 0;
645                 if ($line == '-- ') {
646                     $last = $idx;
647                 }
648                 else {
649                     // remove space-stuffing
650                     $line = preg_replace('/^\s/', '', $line);
651
f12d21 652                     if (isset($text[$last]) && $line
99b8c1 653                         && $text[$last] != '-- '
A 654                         && $text[$last][strlen($text[$last])-1] == ' '
655                     ) {
656                         $text[$last] .= $line;
657                         unset($text[$idx]);
658                     }
659                     else {
660                         $text[$idx] = $line;
661                         $last = $idx;
662                     }
663                 }
664             }
665             $q_level = $q;
666         }
667
668         return implode("\r\n", $text);
d311d8 669     }
A 670
671
672     /**
673      * Wrap the given text to comply with RFC 2646
5c461b 674      *
A 675      * @param string $text Text to wrap
676      * @param int $length Length
677      * @return string Wrapped text
d311d8 678      */
A 679     public static function format_flowed($text, $length = 72)
680     {
99b8c1 681         $text = preg_split('/\r?\n/', $text);
d311d8 682
99b8c1 683         foreach ($text as $idx => $line) {
A 684             if ($line != '-- ') {
685                 if ($line[0] == '>' && preg_match('/^(>+)/', $line, $regs)) {
686                     $prefix = $regs[0];
687                     $level = strlen($prefix);
688                     $line  = rtrim(substr($line, $level));
689                     $line  = $prefix . rc_wordwrap($line, $length - $level - 2, " \r\n$prefix ");
690                 }
6d1ae0 691                 else if ($line) {
99b8c1 692                     $line = ' ' . rc_wordwrap(rtrim($line), $length - 2, " \r\n ");
A 693                 }
694
695                 $text[$idx] = $line;
696             }
d311d8 697         }
99b8c1 698
A 699         return implode("\r\n", $text);
d311d8 700     }
6b6f2e 701
8fa58e 702 }