2
/////////////////////////////
4
// (C)2002 Ryo Chijiiwa <Ryo@IlohaMail.org>
7
// Charset agnostic functions related
8
// to the lang/charset.inc libraries.
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
////////////////////////////
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);
22
if ($hours>12) $hours-=12;
29
$result = str_replace("%h", $hours, $result);
30
$result = str_replace("%m", $minutes, $result);
31
$result = str_replace("%a", $ampm[$a], $result);
36
function LangInsertStringsFromAK($dest, $source_a){
37
if (!is_array($source_a)) return $dest;
39
while ( list($key, $val) = each($source_a) ){
40
$place_holder = "%".$key;
41
$dest = str_replace($place_holder, $val, $dest);
47
function LangDecodeMimeString($str, $charset){
48
$a=explode("?", $str);
50
if ($count >= 3){ //should be in format "charset?encoding?base64_string"
51
for ($i=2; $i<$count; $i++) $rest.=$a[$i];
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);
58
if (strcasecmp($a[0], "utf-8")==0){
59
include_once("../include/utf8.inc");
60
return utf8ToUnicodeEntities($rest);
62
return LangConvert($rest, $charset, $a[0]);
65
return $str; //we dont' know what to do with this
69
function LangDecodeSubject($input, $charset){
71
//echo "Received: $input <br>\n";
72
$pos = strpos($input, "=?");
74
$out = substr($input, 0, $pos);
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);
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";
89
return LangConvert($input, $charset, $charset);
93
function LangFormAddressHTML($user, $name, $address, $charset){
96
if ($my_prefs["compose_inside"]) $target="list2";
97
else $target="_blank";
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;
104
$url = "compose2.php?user=".$user."&to=".urlencode($q_decoded_name." <".$address.">");
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>]";
112
function LangExplodeQuotedString($delimiter, $string){
113
$quotes=explode("\"", $string);
114
while ( list($key, $val) = each($quotes))
116
$quotes[$key] = str_replace($delimiter, "_!@!_", $quotes[$key]);
117
$string=implode("\"", $quotes);
119
$result=explode($delimiter, $string);
120
while ( list($key, $val) = each($result) )
121
$result[$key] = str_replace("_!@!_", $delimiter, $result[$key]);
126
function LangParseAddressList($str){
127
$a=LangExplodeQuotedString(",", $str);
130
while( list($key, $val) = each($a) ){
131
$val = str_replace("\"<", "\" <", $val);
132
$sub_a = LangExplodeQuotedString(" ", $val);
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));
139
if (empty($result[$key]["name"])) $result[$key]["name"] = $result[$key]["address"];
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);
151
$a = LangExplodeQuotedString(" ", $str);
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
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."\"";
166
return implode(" ", $a);
172
function LangDecodeAddressList($str, $charset, $user){
173
$a=LangParseAddressList($str);
178
while( list($i, $val) = each($a) ){
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> ":"");
192
function LangShowAddresses($str, $charset){
193
$a=LangParseAddressList($str);
198
while( list($i, $val) = each($a) ){
200
$address=$a[$i]["address"];
201
$name=str_replace("\"", "", $a[$i]["name"]);
203
$res.=htmlspecialchars("\"$name\" <$address>");
204
if ((($j % 3)==0) && (($c-$j)>1)) $res.=",<br> ";
205
else if ($c>$j) $res.=", ";
212
function LangFormatDate($timestamp, $format){
213
$date = getdate($timestamp);
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);
226
function LangWrapLine($line, $width){
227
$line_len = strlen($line);
230
//if line is less than width, we're good
231
if ($line_len <= $width) return $line;
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";
237
//find last space in this chunk
238
$last_space = strrpos($chunk, " ");
239
$last_space = $prev_i + $last_space;
241
if ($last_space==$prev_i){
242
//no space found in this chunk
243
$next_space = strpos($line, " ", $i);
244
if ($next_space!==false){
246
$line[$next_space] = "\n";
249
//replace last space before width with newline
250
$line[$last_space]="\n";
258
function LangSmartWrap($text, $len){
259
$lines = explode("\n", $text);
261
if (!is_array($lines)) return "";
263
while ( list($i,$line)=each($lines) ){
264
if (!ereg("^>", $line)) $lines[$i] = LangWrapLine(chop($line), $len);
267
return implode("\n", $lines);
b'\\ No newline at end of file'