Added new function charset_decode_iso_8859_2.
[squirrelmail.git] / functions / strings.php
index 32bac75d6d32e104659ce2801489991c74206bde..7bf917621c9bbae8667ab1b225516a8fc7b49d0e 100644 (file)
 
    // Wraps text at $wrap characters
    function sqWordWrap($passed, $wrap) {
-      $passed = str_replace(">", ">", $passed);
       $passed = str_replace("&lt;", "<", $passed);
+      $passed = str_replace("&gt;", ">", $passed);
+
+      preg_match("/^(\s|>)+/", $passed, $regs);
+      $beginning_spaces = $regs[0];
 
-      $words = explode(" ", trim($passed));
-      $i = 0;
-      $line_len = strlen($words[$i])+1;
+      $words = explode(" ", $passed);
+      $i = -1;
+      $line_len = strlen($words[0])+1;
       $line = "";
       if (count($words) > 1) {   
-         while ($i < count($words)) {
+         while ($i++ < count($words)) {
             while ($line_len < $wrap) {
                $line = "$line$words[$i] ";
                $i++;
-               $line_len = $line_len + strlen($words[$i])+1;
+               $line_len = $line_len + strlen($words[$i]) + 1;
             }
             $line_len = strlen($words[$i])+1;
-            if ($line_len < $wrap) {
-               if ($i < count($words)) // don't <BR> the last line
-                  $line = "$line\n";
+            if ($line_len <= $wrap) {
+               if (strlen($beginning_spaces) +2 >= $wrap)
+                  $beginning_spaces = "";
+               if ($i < count($words)) { // don't <BR> the last line
+                  $line = "$line\n$beginning_spaces";
+               }   
+               $line = "$line$words[$i] ";
+               $line_len = strlen($beginning_spaces) + strlen($words[$i]) + 1;
             } else {
+               /*
                $endline = $words[$i];
                while ($line_len >= $wrap) {
                   $bigline = substr($endline, 0, $wrap);
                   $line_len = strlen($endline);
                   $line = "$line$bigline<BR>";
                }
-               $line = "$line$endline<BR>";
-               $i++;
+               */
+               if (strlen($line) > $wrap)
+                  $line = "$line\n$words[$i]";
+               else
+                  $line = "$line$words[$i]";
+               $line_len = strlen($words[$i]);
             }
          }
       } else {
       $text = ereg_replace( '"[^"]*"', "", $text);
       $text = str_replace(",", ";", $text);
       $array = explode(";", $text);
-               for ($i = 0; $i < count ($array); $i++) {
-                       $array[$i] = eregi_replace ("^.*\<", "", $array[$i]);
-                       $array[$i] = eregi_replace ("\>.*$", "", $array[$i]);
-               }
+      for ($i = 0; $i < count ($array); $i++) {
+                           $array[$i] = eregi_replace ("^.*[<]", "", $array[$i]);
+                           $array[$i] = eregi_replace ("[>].*$", "", $array[$i]);
+                 }
       return $array;
    }
 
       for ($i=0; $i < count($body_ary); $i++) {
          $line = $body_ary[$i];
          $line = charset_decode($charset, $line);
+         $line = str_replace("\t", "        ", $line);
          
          if (strlen($line) - 2 >= $wrap_at) {
             $line = sqWordWrap($line, $wrap_at);  
          }
          
          $line = str_replace(" ", "&nbsp;", $line);
-         $line = str_replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", $line);
          $line = nl2br($line);
 
          $line = parseEmail ($line);
    }
 
    /* SquirrelMail version number -- DO NOT CHANGE */
-   $version = "0.5pre1";
+   $version = "0.5pre2";
 
 
    function find_mailbox_name ($mailbox) {
       }
       return $location;
    }   
+
+   function sqStripSlashes($string) {
+      if (get_magic_quotes_gpc()) {
+         $string = stripslashes($string);
+      }
+      return $string;
+   }
+
+
+   // These functions are used to encrypt the passowrd before it is
+   // stored in a cookie.
+   function OneTimePadEncrypt ($string, $pad) {
+      for ($i = 0; $i < strlen ($string); $i++) {
+        $encrypted .= chr (ord($string[$i]) ^ ord($pad[$i]));
+      }
+
+      return base64_encode($encrypted);
+   }
+
+   function OneTimePadDecrypt ($string, $pad) {
+      $encrypted = base64_decode ($string);
+      
+      for ($i = 0; $i < strlen ($encrypted); $i++) {
+        $decrypted .= chr (ord($encrypted[$i]) ^ ord($pad[$i]));
+      }
+
+      return $decrypted;
+   }
+
+   function OneTimePadCreate ($length=100) {
+      srand ((double) microtime() * 1000000);
+      
+      for ($i = 0; $i < $length; $i++) {
+        $pad .= chr(rand(0,255));
+      }
+
+      return $pad;
+   }
+
 ?>