Source for file strings.php
Documentation is available at strings.php
* This code provides various string manipulation functions that are
* used by the rest of the SquirrelMail code.
* @copyright © 1999-2006 The SquirrelMail Project Team
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @version $Id: strings.php,v 1.255 2006/09/30 16:24:00 stekkel Exp $
* Appends citation markers to the string.
* Also appends a trailing space.
* @author Justus Pendleton
* @param string $str The string to append to
* @param int $citeLevel the number of markers to append
for ($i =
0; $i <
$citeLevel; $i++
) {
* Create a newline in the string, adding citation
* markers to the newline as necessary.
* @author Justus Pendleton
* @param string $str the string to make a newline in
* @param int $citeLevel the citation level the newline is at
* @param int $column starting column of the newline
$column =
$citeLevel +
1;
* Checks for spaces in strings - only used if PHP doesn't have native ctype support
* You might be able to rewrite the function by adding short evaluation form.
* - iso-2022-xx charsets - hex 20 might be part of other symbol. I might
* be wrong. 0x20 is not used in iso-2022-jp. I haven't checked iso-2022-kr
* and iso-2022-cn mappings.
* - no-break space ( ) - it is 8bit symbol, that depends on charset.
* there are at least three different charset groups that have nbsp in
* I don't see any charset/nbsp options in php ctype either.
* @param string $string tested string
* @return bool true when only whitespace symbols are present in test string
if ( preg_match('/^[\x09-\x0D]|^\x20/', $string) ||
$string==
'') {
* Wraps text at $wrap characters. While sqWordWrap takes
* a single line of text and wraps it, this function works
* on the entire corpus at once, this allows it to be a little
* bit smarter and when and how to wrap.
* @author Justus Pendleton
* @param string $body the entire body of text
* @param int $wrap the maximum line length
* @return string the wrapped text
//check for ctype support, and fake it if it doesn't exist
// the newly wrapped text
// current column since the last newline in the outstring
// where we are in the original string
// the number of >>> citation markers we are currently at
// the main loop, whenever we start a newline of input text
// we're at the beginning of a line, get the new cite level
while (($pos <
$length) &&
(sq_substr($body,$pos,1) ==
'>')) {
// skip over any spaces interleaved among the cite markers
while (($pos <
$length) &&
(sq_substr($body,$pos,1) ==
' ')) {
// special case: if this is a blank line then maintain it
// (i.e. try to preserve original paragraph breaks)
// unless they occur at the very beginning of the text
$outStringLast =
$outString{sq_strlen($outString) -
1};
if ($outStringLast !=
"\n") {
// if the cite level has changed, then start a new line
// with the new cite level.
if (($citeLevel !=
$newCiteLevel) &&
($pos >
($newCiteLevel +
1)) &&
($outStringCol !=
0)) {
$citeLevel =
$newCiteLevel;
// prepend the quote level if necessary
if ($outStringCol ==
0) {
// if we added a citation then move the column
// out by citelevel + 1 (the cite markers + the space)
$outStringCol =
$citeLevel +
($citeLevel ?
1 :
0);
} else if ($outStringCol >
$citeLevel) {
// not a cite and we're not at the beginning of a line
// in the output. add a space to separate the new text
// find the next newline -- we don't want to go further than that
$nextNewline =
sq_strpos ($body, "\n", $pos);
if ($nextNewline ===
FALSE) {
// Don't wrap unquoted lines at all. For now the textarea
// will work fine for this. Maybe revisit this later though
// (for completeness more than anything else, I think)
$outString .=
sq_substr ($body, $pos, ($nextNewline -
$pos));
$outStringCol =
$nextNewline -
$pos;
if ($nextNewline !=
$length) {
* Set this to false to stop appending short strings to previous lines
// inner loop, (obviously) handles wrapping up to
while ($pos <
$nextNewline) {
// skip over initial spaces
// if this is a short line then just append it and continue outer loop
if (($outStringCol +
$nextNewline -
$pos) <=
($wrap -
$citeLevel -
1) ) {
// if this is the final line in the input string then include
// echo substr($body,$pos,$wrap). "<br />";
if (($nextNewline +
1 ==
$length) &&
(sq_substr($body,$nextNewline,1) ==
"\n")) {
$lastRealChar =
$nextNewline;
while (($lastRealChar >
$pos &&
$lastRealChar <
$length) &&
(ctype_space (sq_substr($body,$lastRealChar,1)))) {
// decide if appending the short string is what we want
if (($nextNewline <
$length &&
sq_substr($body,$nextNewline,1) ==
"\n") &&
while (($mypos <
$length) &&
(sq_substr($body,$mypos,1) ==
'>')) {
// skip over any spaces interleaved among the cite markers
while (($mypos <
$length) &&
(sq_substr($body,$mypos,1) ==
' ')) {
if ($mypos == $nextNewline+1) {
while (($mypos < $length) && ($body{$mypos} == ' ')) {
//if ($dowrap || $ldnspacecnt > 1 || ($firstword && (
if (!$smartwrap ||
$firstword &&
(
$outString .=
sq_substr($body,$pos,($lastRealChar -
$pos+
1));
$outStringCol +=
($lastRealChar -
$pos);
$outString .=
sq_substr ($body, $pos, ($lastRealChar -
$pos +
1));
$outStringCol +=
($lastRealChar -
$pos);
$eol =
$pos +
$wrap -
$citeLevel -
$outStringCol;
// eol is the tentative end of line.
// look backwards for there for a whitespace to break at.
// if it's already less than our current position then
// our current line is already too long, break immediately
// and restart outer loop
// start looking backwards for whitespace to break at.
// if we didn't find a breakpoint by looking backward then we
// need to figure out what to do about that
if ($breakPoint ==
$pos) {
// if we are not at the beginning then end this line
if ($outStringCol >
($citeLevel +
1)) {
// just hard break here. most likely we are breaking
// a really long URL. could also try searching
// forward for a break point, which is what Mozilla
// does. don't bother for now.
// special case: maybe we should have wrapped last
// time. if the first breakpoint here makes the
// current line too long and there is already text on
// the current line, break and loop again if at
// beginning of current line, don't force break
if ((($outStringCol +
($breakPoint -
$pos)) >
($wrap +
$SLOP)) &&
($outStringCol >
($citeLevel +
1))) {
// skip newlines or whitespace at the beginning of the string
$substring =
sq_substr ($body, $pos, ($breakPoint -
$pos));
$substring =
rtrim ($substring); // do rtrim and ctype_space have the same ideas about whitespace?
$outString .=
$substring;
// advance past the whitespace which caused the wrap
* Wraps text at $wrap characters
* Has a problem with special HTML characters, so call this before
* you do character translation.
* Specifically, &#039; comes up as 5 characters instead of 1.
* This should not add newlines to the end of lines.
* @param string $line the line of text to wrap, by ref
* @param int $wrap the maximum line lenth
* @param string $charset name of charset used in $line string. Available since v.1.5.1.
// Use custom wrapping function, if translation provides it
if (isset
($languages[$squirrelmail_language]['XTRA_CODE']) &&
function_exists($languages[$squirrelmail_language]['XTRA_CODE'] .
'_wordwrap')) {
if (mb_detect_encoding($line) !=
'ASCII') {
$line =
call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] .
'_wordwrap', $line, $wrap);
ereg("^([\t >]*)([^\t >].*)?$", $line, $regs);
$beginning_spaces =
$regs[1];
$line =
$beginning_spaces;
while ($i <
count($words)) {
/* Force one word to be on a line (minimum) */
$line_len =
strlen($beginning_spaces) +
sq_strlen($words[$i],$charset) +
2;
if (isset
($words[$i +
1]))
$line_len +=
sq_strlen($words[$i +
1],$charset);
/* Add more words (as long as they fit) */
while ($line_len <
$wrap &&
$i <
count($words)) {
$line .=
' ' .
$words[$i];
$line_len +=
sq_strlen($words[$i],$charset) +
1;
/* Skip spaces if they are the first thing on a continued line */
while (!isset
($words[$i]) &&
$i <
count($words)) {
/* Go to the next line if we have more to process */
if ($i <
count($words)) {
* Does the opposite of sqWordWrap()
* @param string $body the text to un-wordwrap
global $squirrelmail_language;
if ($squirrelmail_language ==
'ja_JP') {
for ($i =
0; $i <
$cnt; $i ++
) {
preg_match("/^([\t >]*)([^\t >].*)?$/", $lines[$i], $regs);
$CurrentSpaces =
$regs[1];
$PreviousSpaces =
$CurrentSpaces;
} else if (($PreviousSpaces ==
$CurrentSpaces) /* Do the beginnings match */
&&
(strlen($lines[$i -
1]) >
65) /* Over 65 characters long */
&&
strlen($CurrentRest)) { /* and there's a line to continue with */
$body .=
' ' .
$CurrentRest;
$body .=
"\n" .
$lines[$i];
$PreviousSpaces =
$CurrentSpaces;
* If $haystack is a full mailbox name and $needle is the mailbox
* separator character, returns the last part of the mailbox name.
* @param string haystack full mailbox name to search
* @param string needle the mailbox separator character
* @return string the last part of the mailbox name
$parts =
explode($needle, $haystack);
while ($elem ==
'' &&
count($parts)) {
* Determines the location to forward to, relative to your server.
* This is used in HTTP Location: redirects.
* If set, it uses $config_location_base as the first part of the URL,
* specifically, the protocol, hostname and port parts. The path is
* @return string the base url for this SquirrelMail installation
/* Get the path, handle virtual directories */
// proto+host+port are already set in config:
if ( !empty($config_location_base) ) {
return $config_location_base .
$path ;
// we computed it before, get it from the session:
return $full_url .
$path;
/* Check if this is a HTTPS or regular HTTP request. */
* If you have 'SSLOptions +StdEnvVars' in your apache config
* OR if you have HTTPS=on in your HTTP_SERVER_VARS
* OR if you are on port 443
if ((isset
($getEnvVar) &&
strcasecmp($getEnvVar, 'on') ===
0) ||