/** * Wraps text at $wrap characters * * Has a problem with special HTML characters, so call this before * you do character translation. * * Specifically, ' comes up as 5 characters instead of 1. * This should not add newlines to the end of lines. */ function sqWordWrap(&$line, $wrap, $charset = null) { global $languages, $squirrelmail_language; if (isset($languages[$squirrelmail_language]['XTRA_CODE']) && function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) { if (mb_detect_encoding($line) != 'ASCII') { $line = $languages[$squirrelmail_language]['XTRA_CODE']('wordwrap', $line, $wrap); return; } } ereg("^([\t >]*)([^\t >].*)?\$", $line, $regs); $beginning_spaces = $regs[1]; if (isset($regs[2])) { $words = explode(' ', $regs[2]); } else { $words = ''; } $i = 0; $line = $beginning_spaces; while ($i < count($words)) { /* Force one word to be on a line (minimum) */ $line .= $words[$i]; $line_len = strlen($beginning_spaces) + sq_strlen($words[$i], $charset) + 2; if (isset($words[$i + 1])) { $line_len += sq_strlen($words[$i + 1], $charset); } $i++; /* Add more words (as long as they fit) */ while ($line_len < $wrap && $i < count($words)) { $line .= ' ' . $words[$i]; $i++; if (isset($words[$i])) { $line_len += sq_strlen($words[$i], $charset) + 1; } else { $line_len += 1; } } /* Skip spaces if they are the first thing on a continued line */ while (!isset($words[$i]) && $i < count($words)) { $i++; } /* Go to the next line if we have more to process */ if ($i < count($words)) { $line .= "\n"; } } }
* We'll change them to \r\n later (in the sendMessage function) */ $body = str_replace("\r\n", "\n", $body); $body = str_replace("\r", "\n", $body); /** * Rewrap $body so that no line is bigger than $editor_size * This should only really kick in the sqWordWrap function * if the browser doesn't support "VIRTUAL" as the wrap type. */ $body = explode("\n", $body); $newBody = ''; foreach ($body as $line) { if ($line != '-- ') { $line = rtrim($line); } if (sq_strlen($line, $default_charset) <= $editor_size + 1) { $newBody .= $line . "\n"; } else { sqWordWrap($line, $editor_size, $default_charset); $newBody .= $line . "\n"; } } $body = $newBody; $composeMessage = $compose_messages[$session]; $Result = deliverMessage($composeMessage); do_hook('compose_send_after', $Result, $composeMessage); if (!$Result) { showInputForm($session); exit; } unset($compose_messages[$session]);
/** * Creates header fields in forwarded email body * * $default_charset global must be set correctly before you call this function. * @param object $orig_header * @return $string */ function getforwardHeader($orig_header) { global $editor_size, $default_charset; // using own strlen function in order to detect correct string length $display = array(_("Subject") => sq_strlen(_("Subject"), $default_charset), _("From") => sq_strlen(_("From"), $default_charset), _("Date") => sq_strlen(_("Date"), $default_charset), _("To") => sq_strlen(_("To"), $default_charset), _("Cc") => sq_strlen(_("Cc"), $default_charset)); $maxsize = max($display); $indent = str_pad('', $maxsize + 2); foreach ($display as $key => $val) { $display[$key] = $key . ': ' . str_pad('', $maxsize - $val); } $from = decodeHeader($orig_header->getAddr_s('from', "\n{$indent}"), false, false, true); $from = str_replace(' ', ' ', $from); $to = decodeHeader($orig_header->getAddr_s('to', "\n{$indent}"), false, false, true); $to = str_replace(' ', ' ', $to); $subject = decodeHeader($orig_header->subject, false, false, true); $subject = str_replace(' ', ' ', $subject); // using own str_pad function in order to create correct string pad $bodyTop = sq_str_pad(' ' . _("Original Message") . ' ', $editor_size - 2, '-', STR_PAD_BOTH, $default_charset) . "\n" . $display[_("Subject")] . $subject . "\n" . $display[_("From")] . $from . "\n" . $display[_("Date")] . getLongDateString($orig_header->date, $orig_header->date_unparsed) . "\n" . $display[_("To")] . $to . "\n"; if ($orig_header->cc != array() && $orig_header->cc != '') { $cc = decodeHeader($orig_header->getAddr_s('cc', "\n{$indent}"), false, false, true); $cc = str_replace(' ', ' ', $cc); $bodyTop .= $display[_("Cc")] . $cc . "\n"; } $bodyTop .= str_pad('', $editor_size - 2, '-') . "\n\n"; return $bodyTop; }
/** * Truncates the given string so that it has at * most $max_chars characters. NOTE that a "character" * may be a multibyte character, or (optionally), an * HTML entity , so this function is different than * using substr() or mb_substr(). * * NOTE that if $elipses is given and used, the returned * number of characters will be $max_chars PLUS the * length of $elipses * * @param string $string The string to truncate * @param int $max_chars The maximum allowable characters * @param string $elipses A string that will be added to * the end of the truncated string * (ONLY if it is truncated) (OPTIONAL; * default not used) * @param boolean $html_entities_as_chars Whether or not to keep * HTML entities together * (OPTIONAL; default ignore * HTML entities) * * @return string The truncated string * * @since 1.4.20 and 1.5.2 (replaced truncateWithEntities()) * */ function sm_truncate_string($string, $max_chars, $elipses = '', $html_entities_as_chars = FALSE) { // if the length of the string is less than // the allowable number of characters, just // return it as is (even if it contains any // HTML entities, that would just make the // actual length even smaller) // $actual_strlen = sq_strlen($string, 'auto'); if ($max_chars <= 0 || $actual_strlen <= $max_chars) { return $string; } // if needed, count the number of HTML entities in // the string up to the maximum character limit, // pushing that limit up for each entity found // $adjusted_max_chars = $max_chars; if ($html_entities_as_chars) { // $loop_count is needed to prevent an endless loop // which is caused by buggy mbstring versions that // return 0 (zero) instead of FALSE in some rare // cases. Thanks, PHP. // see: http://bugs.php.net/bug.php?id=52731 // also: tracker $3053349 // $loop_count = 0; $entity_pos = $entity_end_pos = -1; while ($entity_end_pos + 1 < $actual_strlen && ($entity_pos = sq_strpos($string, '&', $entity_end_pos + 1)) !== FALSE && ($entity_end_pos = sq_strpos($string, ';', $entity_pos)) !== FALSE && $entity_pos <= $adjusted_max_chars && $loop_count++ < $max_chars) { $adjusted_max_chars += $entity_end_pos - $entity_pos; } // this isn't necessary because sq_substr() would figure this // out anyway, but we can avoid a sq_substr() call and we // know that we don't have to add an elipses (this is now // an accurate comparison, since $adjusted_max_chars, like // $actual_strlen, does not take into account HTML entities) // if ($actual_strlen <= $adjusted_max_chars) { return $string; } } // get the truncated string // $truncated_string = sq_substr($string, 0, $adjusted_max_chars); // return with added elipses // return $truncated_string . $elipses; }