forked from backdrop-contrib/mailsystem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
html_to_text.inc
742 lines (706 loc) · 25.5 KB
/
html_to_text.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
<?php
/**
* @file
* Copy of backdrop_html_to_text improvements from issue #299138.
*/
/**
* Perform format=flowed soft wrapping for mail (RFC 3676).
*
* We use delsp=yes wrapping, but only break non-spaced languages when
* absolutely necessary to avoid compatibility issues.
*
* We deliberately use settings_get('mail_line_endings', MAIL_LINE_ENDINGS)
* rather than "\r\n".
*
* @param $text
* The plain text to process.
* @param array $options
* (optional) An array containing one or more of the following keys:
* - indent: A string to indent the text with. Only '>' characters are
* repeated on subsequent wrapped lines. Others are replaced by spaces.
* - max: The maximum length at which to wrap each line. Defaults to 80.
* - stuff: Whether to space-stuff special lines. Defaults to TRUE.
* - hard: Whether to enforce the maximum line length even if no convenient
* space character is available. Defaults to FALSE.
* - pad: A string to use for padding short lines to 'max' characters. If
* more than one character, only the last will be repeated.
* - break: The line break sequence to insert. The default is one of the
* following:
* - "\r\n": Windows, when $text does not contain a space character.
* - "\n": Non-Windows, when $text does not contain a space character.
* - " \r\n": On Windows, when $text contains at least one space.
* - " \n": Non-Windows, when $text contains at least one space.
*
* @see backdrop_mail()
*/
function mailsystem_wrap_mail($text, array $options = array()) {
static $defaults;
if (!isset($defaults)) {
$defaults = array(
'indent' => '',
'pad' => '',
'pad_repeat' => '',
'max' => 80,
'stuff' => TRUE,
'hard' => FALSE,
'eol' => settings_get('mail_line_endings', MAIL_LINE_ENDINGS),
);
}
$options += $defaults;
if (!isset($options['break'])) {
// Allow soft-wrap spaces only when $text contains at least one space.
$options['break'] = (strpos($text, ' ') === FALSE ? '' : ' ') . $defaults['eol'];
}
$options['wrap'] = $options['max'] - backdrop_strlen($options['indent']);
if ($options['pad']) {
$options['pad_repeat'] = backdrop_substr($options['pad'], -1, 1);
}
// The 'clean' indent is applied to all lines after the first one.
$options['clean'] = _mailsystem_html_to_text_clean($options['indent']);
// Wrap lines according to RFC 3676.
$lines = explode($defaults['eol'], $text);
array_walk($lines, '_mailsystem_wrap_mail_line', $options);
// Expand the lines array on newly-inserted line breaks.
$lines = explode($defaults['eol'], implode($defaults['eol'], $lines));
// Apply indentation, space-stuffing, and padding.
array_walk($lines, '_mailsystem_indent_mail_line', $options);
return implode($defaults['eol'], $lines);
}
/**
* Transform an HTML string into plain text, preserving the structure of the
* markup. Useful for preparing the body of a node to be sent by e-mail.
*
* The output will be suitable for use as 'format=flowed; delsp=yes' text
* (RFC 3676) and can be passed directly to backdrop_mail() for sending.
*
* We deliberately use settings_get('mail_line_endings', MAIL_LINE_ENDINGS)
* rather than "\r\n".
*
* This function provides suitable alternatives for the following tags:
*
* <a> <address> <b> <blockquote> <br /> <caption> <cite> <dd> <div> <dl> <dt>
* <em> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <ol> <p> <pre> <strong>
* <table> <tbody> <td> <tfoot> <thead> <tr> <u> <ul>
*
* The following tag attributes are supported:
* - <a href=...>: Hyperlink destination urls.
* - <li value=...>: Ordered list item numbers.
* - <ol start=...>: Ordered list start number.
*
* @param $string
* The string to be transformed.
* @param $allowed_tags
* (optional) If supplied, a list of tags that will be transformed. If
* omitted, all supported tags are transformed.
*
* @return
* The transformed string.
*
* @see backdrop_mail()
*/
function mailsystem_html_to_text($string, $allowed_tags = NULL) {
$eol = settings_get('mail_line_endings', MAIL_LINE_ENDINGS);
// Cache list of supported tags.
static $supported_tags;
if (!isset($supported_tags)) {
$supported_tags = array(
'a', 'address', 'b', 'blockquote', 'br', 'cite', 'dd', 'div', 'dl',
'dt', 'em', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'i', 'li',
'ol', 'p', 'pre', 'strong', 'table', 'td', 'tr', 'u', 'ul',
);
}
// Make sure only supported tags are kept.
$allowed_tags = isset($allowed_tags) ? array_intersect($supported_tags, $allowed_tags) : $supported_tags;
// Parse $string into a DOM tree.
$dom = filter_dom_load($string);
$notes = array();
// Recursively convert the DOM tree into plain text.
$text = _mailsystem_html_to_text($dom->documentElement, $allowed_tags, $notes);
// Hard-wrap at 1000 characters (including the line break sequence)
// and space-stuff special lines.
$text = mailsystem_wrap_mail($text, array('max' => 1000 - strlen($eol), 'hard' => TRUE));
// Change non-breaking spaces back to regular spaces, and trim line breaks.
// chr(160) is the non-breaking space character.
$text = str_replace(chr(160), ' ', trim($text, $eol));
// Add footnotes;
if ($notes) {
// Add a blank line before the footnote list.
$text .= $eol;
foreach ($notes as $url => $note) {
$text .= $eol . '[' . $note . '] ' . $url;
}
}
return $text;
}
/**
* Helper function for backdrop_html_to_text().
*
* Recursively converts $node to text, wrapping and indenting as necessary.
*
* @param $node
* The source DOMNode.
* @param $allowed_tags
* A list of tags that will be transformed.
* @param array &$notes
* A writeable array of footnote reference numbers, keyed by their
* respective hyperlink destination urls.
* @param $line_length
* The maximum length of a line, for wrapping. Defaults to 80 characters.
* @param array $parents
* The list of ancestor tags, from nearest to most distant. Defaults to an
* empty array().
* @param $count
* The number to use for the next list item within an ordered list. Defaults
* to 1.
*/
function _mailsystem_html_to_text(DOMNode $node, array $allowed_tags, array &$notes, $line_length = 80, array $parents = array(), &$count = NULL) {
if (!isset($count)) {
$count = 1;
}
$eol = settings_get('mail_line_endings', MAIL_LINE_ENDINGS);
if ($node->nodeType === XML_TEXT_NODE) {
// For text nodes, we just copy the text content.
$text = $node->textContent;
// Convert line breaks and trim trailing spaces.
$text = preg_replace('/ *\r?\n/', $eol, $text);
if (in_array('pre', $parents)) {
// Within <pre> tags, all spaces become non-breaking.
// chr(160) is the non-breaking space character.
$text = str_replace(' ', chr(160), $text);
}
else {
// Outside <pre> tags, collapse whitespace.
$text = preg_replace('/[[:space:]]+/', ' ', $text);
}
return $text;
}
// Non-text node.
$tag = '';
$text = '';
$child_text = '';
$child_count = 1;
$indent = '';
$prefix = '';
$suffix = '';
$pad = '';
if (isset($node->tagName) && in_array($node->tagName, $allowed_tags)) {
$tag = $node->tagName;
switch ($tag) {
// Turn links with valid hrefs into footnotes.
case 'a':
$test = !empty($node->attributes);
$test = $test && ($href = $node->attributes->getNamedItem('href'));
$test = $test && ($url = url(preg_replace('|^' . base_path() . '|', '', $href->nodeValue), array('absolute' => TRUE)));
$test = $test && valid_url($url);
if ($test) {
// Only add links that have not already been added.
if (isset($notes[$url])) {
$note = $notes[$url];
}
else {
$note = count($notes) + 1;
$notes[$url] = $note;
}
$suffix = ' [' . $note . ']';
}
break;
// Generic block-level tags.
case 'address':
case 'caption':
case 'div':
case 'p':
case 'pre':
// Start on a new line except as the first child of a list item.
if (!isset($parents[0]) || $parents[0] !== 'li' || !$node->isSameNode($node->parentNode->firstChild)) {
$text = $eol;
}
$suffix = $eol;
break;
// Forced line break.
case 'br':
$text = $eol;
break;
// Boldface by wrapping with "*" characters.
case 'b':
case 'strong':
$prefix = '*';
$suffix = '*';
break;
// Italicize by wrapping with "/" characters.
case 'cite':
case 'em':
case 'i':
$prefix = '/';
$suffix = '/';
break;
// Underline by wrapping with "_" characters.
case 'u':
$prefix = '_';
$suffix = '_';
break;
// Blockquotes are indented by "> " at each level.
case 'blockquote':
$text = $eol;
// chr(160) is the non-breaking space character.
$indent = '>' . chr(160);
$suffix = $eol;
break;
// Dictionary definitions are indented by four spaces.
case 'dd':
// chr(160) is the non-breaking space character.
$indent = chr(160) . chr(160) . chr(160) . chr(160);
$suffix = $eol;
break;
// Dictionary list.
case 'dl':
// Start on a new line as the first child of a list item.
if (!isset($parents[0]) || $parents[0] !== 'li' || !$node->isSameNode($node->parentNode->firstChild)) {
$text = $eol;
}
$suffix = $eol;
break;
// Dictionary term.
case 'dt':
$suffix = $eol;
break;
// Header level 1 is prefixed by eight "=" characters.
case 'h1':
$text = "$eol$eol";
// chr(160) is the non-breaking space character.
$indent = '========' . chr(160);
$pad = chr(160) . '=';
$suffix = $eol;
break;
// Header level 2 is prefixed by six "-" characters.
case 'h2':
$text = "$eol$eol";
// chr(160) is the non-breaking space character.
$indent = '------' . chr(160);
$pad = chr(160) . '-';
$suffix = $eol;
break;
// Header level 3 is prefixed by four "." characters and a space.
case 'h3':
$text = "$eol$eol";
// chr(160) is the non-breaking space character.
$indent = '....' . chr(160);
$suffix = $eol;
break;
// Header level 4 is prefixed by three "." characters and a space.
case 'h4':
$text = "$eol$eol";
// chr(160) is the non-breaking space character.
$indent = '...' . chr(160);
$suffix = $eol;
break;
// Header level 5 is prefixed by two "." character and a space.
case 'h5':
$text = "$eol$eol";
// chr(160) is the non-breaking space character.
$indent = '..' . chr(160);
$suffix = $eol;
break;
// Header level 6 is prefixed by one "." character and a space.
case 'h6':
$text = "$eol$eol";
// chr(160) is the non-breaking space character.
$indent = '.' . chr(160);
$suffix = $eol;
break;
// Horizontal rulers become a line of "-" characters.
case 'hr':
$text = $eol;
$child_text = '-';
$pad = '-';
$suffix = $eol;
break;
// List items are treated differently depending on the parent tag.
case 'li':
// Ordered list item.
if (reset($parents) === 'ol') {
// Check the value attribute.
$test = !empty($node->attributes);
$test = $test && ($value = $node->attributes->getNamedItem('value'));
if ($test) {
$count = $value->nodeValue;
}
// chr(160) is the non-breaking space character.
$indent = ($count < 10 ? chr(160) : '') . chr(160) . "$count)" . chr(160);
$count++;
}
// Unordered list item.
else {
// chr(160) is the non-breaking space character.
$indent = chr(160) . '*' . chr(160);
}
$suffix = $eol;
break;
// Ordered lists.
case 'ol':
// Start on a new line as the first child of a list item.
if (!isset($parents[0]) || $parents[0] !== 'li' || !$node->isSameNode($node->parentNode->firstChild)) {
$text = $eol;
}
// Check the start attribute.
$test = !empty($node->attributes);
$test = $test && ($value = $node->attributes->getNamedItem('start'));
if ($test) {
$child_count = $value->nodeValue;
}
break;
// Tables require special handling.
case 'table':
return _mailsystem_html_to_text_table($node, $allowed_tags, $notes, $line_length);
// Separate adjacent table cells by two non-breaking spaces.
case 'td':
if (!empty($node->nextSibling)) {
// chr(160) is the non-breaking space character.
$suffix = chr(160) . chr(160);
}
break;
// End each table row with a newline.
case 'tr':
$suffix = $eol;
break;
// Unordered lists.
case 'ul':
// Start on a new line as the first child of a list item.
if (!isset($parents[0]) || $parents[0] !== 'li' || !$node->isSameNode($node->parentNode->firstChild)) {
$text = $eol;
}
break;
default:
// Coder review complains if there is no default case.
break;
}
// Only add allowed tags to the $parents array.
array_unshift($parents, $tag);
}
// Copy each child node to output.
if ($node->hasChildNodes()) {
foreach ($node->childNodes as $child) {
$child_text .= _mailsystem_html_to_text($child, $allowed_tags, $notes, $line_length - backdrop_strlen($indent), $parents, $child_count); }
}
// We only add prefix and suffix if the child nodes were non-empty.
if ($child_text > '') {
// We capitalize the contents of h1 and h2 tags.
if ($tag === 'h1' || $tag === 'h2') {
$child_text = backdrop_strtoupper($child_text);
}
// Don't add a newline to an existing newline.
if ($suffix === $eol && backdrop_substr($child_text, - backdrop_strlen($eol)) === $eol) {
$suffix = '';
}
// Trim spaces around newlines except with <pre> or inline tags.
if (!in_array($tag, array('a', 'b', 'cite', 'em', 'i', 'pre', 'strong', 'u'))) {
$child_text = preg_replace('/ *' . $eol . ' */', $eol, $child_text);
}
// Soft-wrap at effective line length, but don't space-stuff.
$child_text = mailsystem_wrap_mail(
$prefix . $child_text,
array(
// chr(160) is the non-breaking space character.
'break' => chr(160) . $eol,
'indent' => $indent,
'max' => $line_length,
'pad' => $pad,
'stuff' => FALSE,
)
) . $suffix;
if ($tag === 'pre') {
// Perform RFC-3676 soft-wrapping.
// chr(160) is the non-breaking space character.
$child_text = str_replace(chr(160), ' ', $child_text);
$child_text = mailsystem_wrap_mail(
$child_text,
array('max' => $line_length, 'stuff' => FALSE)
);
// chr(160) is the non-breaking space character.
$child_text = str_replace(' ', chr(160), $child_text);
}
$text .= $child_text;
}
return $text;
}
/**
* Helper function for _mailsystem_html_to_text().
*
* Renders a <table> DOM Node into plain text. Attributes such as rowspan,
* colspan, padding, border, etc. are ignored.
*
* @param DOMNode $node
* The DOMNode corresponding to the <table> tag and its contents.
* @param $allowed_tags
* The list of allowed tags passed to _mailsystem_html_to_text().
* @param array &$notes
* A writeable array of footnote reference numbers, keyed by their
* respective hyperlink destination urls.
* @param $table_width
* The desired maximum table width, after word-wrapping each table cell.
*
* @return
* A plain text representation of the table.
*
* @see _mailsystem_html_to_text()
*/
function _mailsystem_html_to_text_table(DOMNode $node, $allowed_tags = NULL, array &$notes = array(), $table_width = 80) {
$eol = settings_get('mail_line_endings', MAIL_LINE_ENDINGS);
$header = array();
$footer = array();
$body = array();
$text = $eol;
$current = $node;
while (TRUE) {
if (isset($current->tagName)) {
switch ($current->tagName) {
case 'caption': // The table caption is added first.
$text = _mailsystem_html_to_text($current, $allowed_tags, $notes, $table_width);
break;
case 'tr':
switch ($current->parentNode->tagName) {
case 'thead':
$header[] = $current;
break;
case 'tfoot':
$footer[] = $current;
break;
default: // Either 'tbody' or 'table'
$body[] = $current;
break;
}
break;
default:
if ($current->hasChildNodes()) {
$current = $current->firstChild;
continue 2;
}
}
}
do {
if ($current->nextSibling) {
$current = $current->nextSibling;
continue 2;
}
$current = $current->parentNode;
} while ($current && !$current->isSameNode($node));
break;
}
// Merge the thead, tbody, and tfoot sections together.
if ($rows = array_merge($header, $body, $footer)) {
$num_rows = count($rows);
// First just count the number of columns.
$num_cols = 0;
foreach ($rows as $row) {
$row_cols = 0;
foreach ($row->childNodes as $cell) {
if (isset($cell->tagName) && in_array($cell->tagName, array('td', 'th'))) {
$row_cols++;
}
}
$num_cols = max($num_cols, $row_cols);
}
// If any columns were found, calculate each column height and width.
if ($num_cols) {
// Set up a binary search for best wrap width for each column.
$max = max($table_width - $num_cols - 1, 1);
$max_wraps = array_fill(0, $num_cols, $max);
$try = max(intval(($table_width - 1) / $num_cols - 1), 1);
$try_wraps = array_fill(0, $num_cols, $try);
$min_wraps = array_fill(0, $num_cols, 1);
// Start searching...
$change = FALSE;
do {
$change = FALSE;
$widths = array_fill(0, $num_cols, 0);
$heights = array_fill(0, $num_rows, 0);
$table = array_fill(0, $num_rows, array_fill(0, $num_cols, ''));
$breaks = array_fill(0, $num_cols, FALSE);
foreach ($rows as $i => $row) {
$j = 0;
foreach ($row->childNodes as $cell) {
if (!isset($cell->tagName) || !in_array($cell->tagName, array('td', 'th'))) {
// Skip text nodes.
continue;
}
// Render the cell contents.
$cell = _mailsystem_html_to_text($cell, $allowed_tags, $notes, $try_wraps[$j]);
// Trim leading line-breaks and trailing whitespace.
// chr(160) is the non-breaking space character.
$cell = rtrim(ltrim($cell, $eol), ' ' . $eol . chr(160));
$table[$i][$j] = $cell;
if ($cell > '') {
// Split the cell into lines.
$lines = explode($eol, $cell);
// The row height is the maximum number of lines among all the
// cells in that row.
$heights[$i] = max($heights[$i], count($lines));
foreach ($lines as $line) {
$this_width = backdrop_strlen($line);
// The column width is the maximum line width among all the
// lines in that column.
if ($this_width > $widths[$j]) {
$widths[$j] = $this_width;
// If the longest line in a column contains at least one
// space character, then the table can be made narrower.
$breaks[$j] = strpos(' ', $line) !== FALSE;
}
}
}
$j++;
}
}
// Calculate the total table width;
$this_width = array_sum($widths) + $num_cols + 1;
if ($this_width > $table_width) {
// Wider than desired.
if (!in_array(TRUE, $breaks)) {
// If there are no more break points, then the table is already as
// narrow as it can get, so we're done.
break;
}
foreach ($try_wraps as $i => $wrap) {
$max_wraps[$i] = min($max_wraps[$i], $wrap);
if ($breaks[$i]) {
$new_wrap = intval(($min_wraps[$i] + $max_wraps[$i]) / 2);
$new_wrap = min($new_wrap, $widths[$i] - 1);
$new_wrap = max($new_wrap, $min_wraps[$i]);
}
else {
// There's no point in trying to make the column narrower than
// the widest un-wrappable line in the column.
$min_wraps[$i] = $widths[$i];
$new_wrap = $widths[$i];
}
if ($try_wraps[$i] > $new_wrap) {
$try_wraps[$i] = $new_wrap;
$change = TRUE;
}
}
}
elseif ($this_width < $table_width) {
// Narrower than desired.
foreach ($try_wraps as $i => $wrap) {
if ($min_wraps[$i] < $wrap) {
$min_wraps[$i] = $wrap;
}
$new_wrap = intval(($min_wraps[$i] + $max_wraps[$i]) / 2);
$new_wrap = max($new_wrap, $widths[$i] + 1);
$new_wrap = min($new_wrap, $max_wraps[$i]);
if ($try_wraps[$i] < $new_wrap) {
$try_wraps[$i] = $new_wrap;
$change = TRUE;
}
}
}
} while ($change);
// Pad each cell to column width and line height.
for ($i = 0; $i < $num_rows; $i++) {
if ($heights[$i]) {
for ($j = 0; $j < $num_cols; $j++) {
$cell = $table[$i][$j];
// Pad each cell to the maximum number of lines in that row.
$lines = array_pad(explode($eol, $cell), $heights[$i], '');
foreach ($lines as $k => $line) {
// Pad each line to the maximum width in that column.
$repeat = $widths[$j] - backdrop_strlen($line);
if ($repeat > 0) {
// chr(160) is the non-breaking space character.
$lines[$k] .= str_repeat(chr(160), $repeat);
}
}
$table[$i][$j] = $lines;
}
}
}
// Generate the row separator line.
$separator = '+';
for($i = 0; $i < $num_cols; $i++) {
$separator .= str_repeat('-', $widths[$i]) . '+';
}
$separator .= $eol;
for ($i = 0; $i < $num_rows; $i++) {
$text .= $separator;
if (!$heights[$i]) {
continue;
}
$row = $table[$i];
// For each row, iterate first by lines within the row.
for ($k = 0; $k < $heights[$i]; $k++) {
// Add a vertical-bar at the beginning of each row line.
$row_line = '|';
$trimmed = '';
// Within each row line, iterate by cells within that line.
for ($j = 0; $j < $num_cols; $j++) {
// Add a vertical bar at the end of each cell line.
$row_line .= $row[$j][$k] . '|';
// chr(160) is the non-breaking space character.
$trimmed .= trim($row[$j][$k], ' ' . $eol . chr(160));
}
if ($trimmed > '') {
// Only print rows that are non-empty.
$text .= $row_line . $eol;
}
}
}
// Final output ends with a row separator.
$text .= $separator;
}
}
// Make sure formatted table content doesn't line-wrap.
// chr(160) is the non-breaking space character.
return str_replace(' ', chr(160), $text);
}
/**
* Helper function for array_walk in backdrop_wrap_mail().
*
* Inserts $values['break'] sequences to break up $line into parts of no more
* than $values['wrap'] characters. Only breaks at space characters, unless
* $values['hard'] is TRUE.
*/
function _mailsystem_wrap_mail_line(&$line, $key, $values) {
$line = wordwrap($line, $values['wrap'], $values['break'], $values['hard']);
}
/**
* Helper function for array_walk in backdrop_wrap_mail().
*
* If $values['pad'] is non-empty, $values['indent'] will be added at the start
* of each line, and $values['pad'] at the end, repeating the last character of
* $values['pad'] until the line length equals $values['max'].
*
* If $values['pad'] is empty, $values['indent'] will be added at the start of
* the first line, and $values['clean'] at the start of subsequent lines.
*
* If $values['stuff'] is true, then an extra space character will be added at
* the start of any line beginning with a space, a '>', or the word 'From'.
*
* @see http://www.ietf.org/rfc/rfc3676.txt
*/
function _mailsystem_indent_mail_line(&$line, $key, $values) {
if ($line == '') {
return;
}
if ($values['pad']) {
$line = $values['indent'] . $line;
$count = $values['max'] - backdrop_strlen($line) - backdrop_strlen($values['pad']);
if ($count >= 0) {
$line .= $values['pad'] . str_repeat($values['pad_repeat'], $count);
}
}
else {
$line = $values[$key === 0 ? 'indent' : 'clean'] . $line;
}
if ($values['stuff']) {
// chr(160) is the non-breaking space character.
$line = preg_replace('/^(' . chr(160) . '| |>|From)/', ' $1', $line);
}
}
/**
* Helper function for backdrop_wrap_mail() and backdrop_html_to_text().
*
* Replace all non-quotation markers from a given piece of indentation with
* non-breaking space characters.
*/
function _mailsystem_html_to_text_clean($indent) {
// chr(160) is the non-breaking space character.
return preg_replace('/[^>]/', chr(160), $indent);
}