home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / admin / common.inc < prev    next >
Encoding:
Text File  |  2003-08-20  |  7.3 KB  |  271 lines

  1. <?php
  2. /////////////////////////////
  3. //    common.inc
  4. //    (C)2002 Ryo Chijiiwa <Ryo@IlohaMail.org>
  5. //        
  6. //        Description:
  7. //            Charset agnostic functions related
  8. //            to the lang/charset.inc libraries.
  9. //
  10. //    This file is part of IlohaMail. IlohaMail is free software released 
  11. //    under the GPL license.  See enclosed file COPYING for details, or 
  12. //    see http://www.fsf.org/copyleft/gpl.html
  13. ////////////////////////////
  14.  
  15. function LangFormatIntTime($time, $system, $ampm, $format){
  16.     //purpose: take "930" and format as "9:30am" or "0930" as necessary
  17.     $min_pos = strlen($time) - 2;
  18.     $hours = substr($time, 0, $min_pos);
  19.     $minutes = substr($time, $min_pos);
  20.     if ($system==12){
  21.         if ($hours >= 12){
  22.             if ($hours>12) $hours-=12;
  23.             $a = "pm";
  24.         }else{
  25.             $a = "am";
  26.         }
  27.     }
  28.     $result = $format;
  29.     $result = str_replace("%h", $hours, $result);
  30.     $result = str_replace("%m", $minutes, $result);
  31.     $result = str_replace("%a", $ampm[$a], $result);
  32.     
  33.     return $result;
  34. }
  35.  
  36. function LangInsertStringsFromAK($dest, $source_a){
  37.     if (!is_array($source_a)) return $dest;
  38.     else{
  39.         while ( list($key, $val) = each($source_a) ){
  40.             $place_holder = "%".$key;
  41.             $dest = str_replace($place_holder, $val, $dest);
  42.         }
  43.     }
  44.     return $dest;
  45. }
  46.  
  47. function LangDecodeMimeString($str, $charset){
  48.     $a=explode("?", $str);
  49.     $count = count($a);
  50.     if ($count >= 3){            //should be in format "charset?encoding?base64_string"
  51.         for ($i=2; $i<$count; $i++) $rest.=$a[$i];
  52.         
  53.         if (($a[1]=="B")||($a[1]=="b")) $rest = base64_decode($rest);
  54.         else if (($a[1]=="Q")||($a[1]=="q")){
  55.             $rest = str_replace("_", " ", $rest);
  56.             $rest = quoted_printable_decode($rest);
  57.         }
  58.         if (strcasecmp($a[0], "utf-8")==0){
  59.             include_once("../include/utf8.inc");
  60.             return utf8ToUnicodeEntities($rest);
  61.         }else{
  62.             return LangConvert($rest, $charset, $a[0]);
  63.         }
  64.     }else{
  65.         return $str;        //we dont' know what to do with this
  66.     }
  67. }
  68.  
  69. function LangDecodeSubject($input, $charset){
  70.     $out = "";
  71.     //echo "Received: $input <br>\n";
  72.     $pos = strpos($input, "=?");
  73.     if ($pos !== false){
  74.         $out = substr($input, 0, $pos);
  75.  
  76.         $end_cs_pos = strpos($input, "?", $pos+2);
  77.         $end_en_pos = strpos($input, "?", $end_cs_pos+1);
  78.         $end_pos = strpos($input, "?=", $end_en_pos+1);
  79.  
  80.         $encstr = substr($input, $pos+2, ($end_pos-$pos-2));
  81.         //echo "encstr: $encstr <br>\n";
  82.         $rest = substr($input, $end_pos+2);
  83.         //echo "rest: $rest <br>\n";
  84.         $out.=LangDecodeMimeString($encstr, $charset);
  85.         $out.=LangDecodeSubject($rest, $charset);
  86.         //echo "returning: $out <br>\n";
  87.         return $out;
  88.     }else{
  89.         return LangConvert($input, $charset, $charset);
  90.     }
  91. }
  92.  
  93. function LangFormAddressHTML($user, $name, $address, $charset){
  94.     global $my_prefs;
  95.     
  96.     if ($my_prefs["compose_inside"]) $target="list2";
  97.     else $target="_blank";
  98.     
  99.     if (empty($name)) $name=$address;
  100.     $decoded_name = LangDecodeSubject($name, $charset);
  101.     if (strpos($decoded_name, " ")!==false) $q_decoded_name = "\"".$decoded_name."\"";
  102.     else $q_decoded_name = $decoded_name;
  103.     
  104.     $url = "compose2.php?user=".$user."&to=".urlencode($q_decoded_name." <".$address.">");
  105.     
  106.     $res="";
  107.     $res.="<a href=\"$url\" target=\"$target\">".$decoded_name."</a>";
  108.     $res.="[<a href=\"edit_contact.php?user=$user&name=".urlencode($decoded_name)."&email=".urlencode($address)."&edit=-1\">+</a>]";
  109.     return $res;
  110. }
  111.  
  112. function LangExplodeQuotedString($delimiter, $string){
  113.     $quotes=explode("\"", $string);
  114.     while ( list($key, $val) = each($quotes))
  115.         if (($key % 2) == 1) 
  116.             $quotes[$key] = str_replace($delimiter, "_!@!_", $quotes[$key]);
  117.     $string=implode("\"", $quotes);
  118.     
  119.     $result=explode($delimiter, $string);
  120.     while ( list($key, $val) = each($result) )
  121.         $result[$key] = str_replace("_!@!_", $delimiter, $result[$key]);
  122.     
  123.     return $result;
  124. }
  125.  
  126. function LangParseAddressList($str){
  127.     $a=LangExplodeQuotedString(",", $str);
  128.     $result=array();
  129.     reset($a);
  130.     while( list($key, $val) = each($a) ){
  131.         $val = str_replace("\"<", "\" <", $val);
  132.         $sub_a = LangExplodeQuotedString(" ", $val);
  133.         reset($sub_a);
  134.         while ( list($k, $v) = each($sub_a) ){
  135.             if ((strpos($v, "@") > 0) && (strpos($v, ".") > 0)) 
  136.                 $result[$key]["address"] = str_replace("<", "", str_replace(">", "", $v));
  137.             else $result[$key]["name"] .= (empty($result[$key]["name"])?"":" ").str_replace("\"","",stripslashes($v));
  138.         }
  139.         if (empty($result[$key]["name"])) $result[$key]["name"] = $result[$key]["address"];
  140.     }
  141.     
  142.     return $result;
  143. }
  144.  
  145. function LangEncodeAddressList($str, $charset){
  146.     $str = str_replace(", ", ",", $str);
  147.     $str = str_replace("," , ", ", $str);
  148.     $str = str_replace("; ", ";", $str);
  149.     $str = str_replace(";", "; ", $str);
  150.     
  151.     $a = LangExplodeQuotedString(" ", $str);
  152.     if (is_array($a)){
  153.         $c = count($a);
  154.         for ($i=0;$i<$c;$i++){
  155.             if ((strpos($a[$i],"@") > 0) && (strpos($a[$i], ".") > 0)){
  156.                 //probably an email address, leave it alone
  157.             }else{
  158.                 //some string, encode
  159.                 $word = stripslashes($a[$i]);
  160.                 $len = strlen($word);
  161.                 $enc = LangEncodeSubject(str_replace("\"", "", $word), $charset);
  162.                 if (($word[0]=="\"")&&($word[$len-1]=="\"")) $enc = "\"".$enc."\"";
  163.                 $a[$i] = $enc;
  164.             }
  165.         }
  166.         return implode(" ", $a);
  167.     }else{
  168.         return $str;
  169.     }
  170. }
  171.  
  172. function LangDecodeAddressList($str, $charset, $user){
  173.     $a=LangParseAddressList($str);
  174.     if (is_array($a)){
  175.         $c=count($a);
  176.         $j=0;
  177.         reset($a);
  178.         while( list($i, $val) = each($a) ){
  179.             $j++;
  180.             $address=$a[$i]["address"];
  181.             $name=str_replace("\"", "", $a[$i]["name"]);
  182.             $res.=LangFormAddressHTML($user, $name, $address, $charset);
  183.             if ((($j % 3)==0) && (($c-$j)>1)) $res.=",<br>   ";
  184.             else if ($c>$j) $res.=", ";
  185.             //$res.=(($c>1)&&($j<$c)?",<br>   ":"");
  186.         }
  187.     }
  188.     
  189.     return $res;
  190. }
  191.  
  192. function LangShowAddresses($str, $charset){
  193.     $a=LangParseAddressList($str);
  194.     if (is_array($a)){
  195.         $c=count($a);
  196.         $j=0;
  197.         reset($a);
  198.         while( list($i, $val) = each($a) ){
  199.             $j++;
  200.             $address=$a[$i]["address"];
  201.             $name=str_replace("\"", "", $a[$i]["name"]);
  202.             
  203.             $res.=htmlspecialchars("\"$name\" <$address>");
  204.             if ((($j % 3)==0) && (($c-$j)>1)) $res.=",<br>   ";
  205.             else if ($c>$j) $res.=", ";
  206.         }
  207.     }
  208.     
  209.     return $res;
  210. }
  211.  
  212. function LangFormatDate($timestamp, $format){
  213.     $date = getdate($timestamp);
  214.     
  215.     $result = $format;
  216.     $result = str_replace("%d", $date["mday"], $result);
  217.     $result = str_replace("%m", $date["mon"], $result);
  218.     $result = str_replace("%y", $date["year"], $result);
  219.     $result = str_replace("%t", $date["hour"].":".$date["minutes"], $result);
  220.     $result = str_replace("%S", date('S', $timestamp), $result);
  221.  
  222.     return $result;
  223. }
  224.  
  225.  
  226. function LangWrapLine($line, $width){
  227.     $line_len = strlen($line);
  228.     $i = 0;
  229.     
  230.     //if line is less than width, we're good
  231.     if ($line_len <= $width) return $line;
  232.     
  233.     for ($prev_i=0,$i=$width;$i<$line_len;$prev_i=$i,$i+=$width){
  234.         //extract last segment that is $width wide
  235.         $chunk = substr($line, $prev_i, ($i-$prev_i))."\n";
  236.         
  237.         //find last space in this chunk
  238.         $last_space = strrpos($chunk, " ");
  239.         $last_space = $prev_i + $last_space;
  240.         
  241.         if ($last_space==$prev_i){
  242.             //no space found in this chunk
  243.             $next_space = strpos($line, " ", $i);
  244.             if ($next_space!==false){
  245.                 $i = $next_space;
  246.                 $line[$next_space] = "\n";
  247.             }
  248.         }else{
  249.             //replace last space before width with newline
  250.             $line[$last_space]="\n";
  251.             $i = $last_space;
  252.         }
  253.     }
  254.     
  255.     return $line;
  256. }
  257.  
  258. function LangSmartWrap($text, $len){
  259.     $lines = explode("\n", $text);
  260.     
  261.     if (!is_array($lines)) return "";
  262.     
  263.     while ( list($i,$line)=each($lines) ){
  264.         if (!ereg("^>", $line)) $lines[$i] = LangWrapLine(chop($line), $len);
  265.     }
  266.     
  267.     return implode("\n", $lines);
  268. }
  269.  
  270.  
  271. ?>