File: util.inc

package info (click to toggle)
basilix 1.0.3b-1.1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 828 kB
  • ctags: 206
  • sloc: php: 2,681; sql: 45; makefile: 40; sh: 7
file content (332 lines) | stat: -rw-r--r-- 10,290 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<?php
/*
+-------------------------------------------------------------------+
| BasiliX - Copyright (C) 2000 Murat Arslan <arslanm@cyberdude.com> |
+-------------------------------------------------------------------+
*/

// This file includes several useful functions..
// ------------------------------------------------------------------

function url_redirect($url = "") {
    global $BSX_BASEHREF, $is_ssl;
    
    if($url == "") $url = $BSX_BASEHREF;
    if($is_ssl) {
	$url .= "&is_ssl=" . $is_ssl;
	$url = ereg_replace("http://", "https://", $url);
    }
    Header("Location: " . $url);
    exit();
}

// gets the array index of a domain
function domain2index($domain) {
    global $bsx_domains, $bsx_domains_cnt;
    $domain1 = strtolower($domain);
    for($i = 0 ; $i < $bsx_domains_cnt ; $i++) {
       $domain2 = strtolower($bsx_domains[$i]["domain"]);
       if($domain1 == $domain2) return $i;
    }
    return -1;
}


// gets the domain name of an index
function index2domain($indx) {
    global $bsx_domains;
    $domain = $bsx_domains[$indx]["domain"];
    if($domain == "") return false;
    return $domain;
}


// Turkce karakterler (turkish characters)
// tr  -> us
// 199 -> 67
// 208 -> 71
// 214 -> 79
// 220 -> 85
// 221 -> 73
// 222 -> 83
// 231 -> 99
// 240 -> 103
// 246 -> 111
// 252 -> 117
// 253 -> 105
// 254 -> 115

function make_readable($name) {
   $newname = "";
   for($i = 0 ; $i < strlen($name) ; $i++) {
      $z = ord($name[$i]);
      switch($z) {
       case 199:
	 $newname .= chr(67);
	 break;
       case 208:
	 $newname .= chr(71);
	 break;
       case 214:
	 $newname .= chr(79);
	 break;
       case 220:
	 $newname .= chr(85);
	 break;
       case 221:
	 $newname .= chr(73);
	 break;
       case 222:
	 $newname .= chr(83);
	 break;
       case 231:
	 $newname .= chr(99);
	 break;
       case 240:
	 $newname .= chr(103);
	 break;
       case 246:
	 $newname .= chr(111);
	 break;
       case 252:
	 $newname .= chr(117);
	 break;
       case 253:
	 $newname .= chr(105);
	 break;
       case 254:
	 $newname .= chr(115);
	 break;
       default:
	 $newname .= chr($z);
      }
   }
   // we converted the tr chars to us chars
   
   // now we need to get rid of unreadable chars
   // i.e we allow: A-Z, a-z, 0-9 and blank
   $name2 = $newname;
   $newname = "";
   for($i = 0 ; $i < strlen($name2) ; $i++) {
      $z = ord($name2[$i]);
      if($z == 32) $newname .= " ";
      if(($z < 48) || (($z > 57) && ($z < 65)) ||
	 (($z > 90) && ($z < 97)) || ($z > 122)) continue;
      $newname .= $name2[$i];
   }
   return $newname;
}

// convert the size in bytes to kB/mB
function convert_size($byte) {
   if($byte == 0) // no need to calculate
     return "0kB";
   if($byte < 1000) // i guess users dont want to see the size like 1020 bytes
     return "$byte" . " B";
   $rem = $byte / 1024.0;
   $kb = sprintf("%.1f", $rem);
   $remkb = sprintf("%d", $rem);
   if($remkb < 1000)  // kilobytes is ok
     return "$kb" . "kB";
   $rem2 = $remkb / 1024.0;
   $mb = sprintf("%.1f", $rem2);
   return "$mb" . "mB"; // finally megabytes
}

// simple decrypt of a string
function decode_strip($str) {
   return stripslashes(urldecode($str));
}

function decode_mime($string) {
   if(eregi("=?([A-Z,0-9,-]+)?([A-Z,0-9,-]+)?([A-Z,0-9,-,=,_]+)?=", $string)) {
      $coded_strings = explode('=?', $string);
      $counter = 1;
      $string = $coded_strings[0];
      while($counter < count($coded_strings)) {
	 $elements = explode('?', $coded_strings[$counter]);
	 if(eregi("Q", $elements[1])) {
	    $elements[2] = str_replace('_', ' ', $elements[2]);
	    $elements[2] = eregi_replace("=([A-F,0-9]{2})", "%\\1", $elements[2]);
	    $string .= urldecode($elements[2]);
	 } else {
	    $elements[2] = str_replace('=', '', $elements[2]);
	    if ($elements[2]) { $string .= base64_decode($elements[2]); 
	    }
	 }
	 if(isset($elements[3]) && $elements[3] != '') {
	    $elements[3] = ereg_replace("^=", '', $elements[3]);
	    $string .= $elements[3];
	 }
	 $string .= " ";
	 $counter++;
      }
   }
   return $string;
}

function handle_emails($str) {
    global $BSX_BASEHREF, $BSX_LAUNCHER;
    global $is_js, $is_ssl, $SESSID;

    $url = $BSX_BASEHREF . "/" . $BSX_LAUNCHER . "?RequestID=CMPSNEW";
    if($SESSID) $url .= "&SESSID=$SESSID";
    if($is_js) $url .= "&is_js=$is_js";
    if($is_ssl) $url .= "&is_ssl=$is_ssl";
    $url .= "&cmps_to";
    return(ereg_replace("([A-Za-z0-9._-]+\@[[:alnum:].[a-zA-Z0-9_-]+[a-zA-Z]+)",
			  "<a href=\"$url=\\1\">\\1</a>", $str));
}

function handle_urls($str) {
    return(eregi_replace("(http|https|ftp)://([[:alnum:]/\n+-=&%:_.~?]+[#[:alnum:]+]*)","<a href=\"\\1://\\2\" target=_new>\\1://\\2</a>",  $str));
}

function put_ahref($href, $name, $linkid = "", $title = "") {

    $url = $GLOBALS["BSX_BASEHREF"] . "/" . $GLOBALS["BSX_LAUNCHER"] . "?" . $href;
    if($GLOBALS["SESSID"]) $url .= "&SESSID=" . $GLOBALS["SESSID"];
    if($GLOBALS["is_js"]) $url .= "&is_js=" . $GLOBALS["is_js"];
    if($GLOBALS["is_ssl"]) $url .= "&is_ssl=" . $GLOBALS["is_ssl"];
    $output = "<a href=\"$url\"";
    if($linkid != "") $output .= " id=\"$linkid\"";
    if($title != "") $output .= " title=\"$title\"";

    $output .= ">$name</a>";
    echo $output;
}

function nbsp($str) {
    $str = "$str";
    if(empty($str)) return "&nbsp;";
    return "&nbsp;$str&nbsp;";
}

function start_form($name, $extra = "") {
    global $BSX_BASEHREF, $BSX_LAUNCHER;
    global $is_ssl;
   
    $posturl = $BSX_BASEHREF . "/" . $BSX_LAUNCHER;
    echo "<form name=\"$name\" method=\"POST\" action=\"$posturl\" $extra>\n";
}

function stop_form() {
    global $SESSID, $is_js, $is_ssl, $nocookie;
    if($SESSID)
       echo "<input type=\"hidden\" name=\"SESSID\" value=\"$SESSID\">";
    if($is_js)
       echo "<input type=\"hidden\" name=\"is_js\" value=\"$is_js\">";
    if($is_ssl) 
       echo "<input type=\"hidden\" name=\"is_ssl\" value=\"$is_ssl\">";
    if($nocookie) 
       echo "<input type=\"hidden\" name=\"is_nocookie\" value=\"$nocookie\">";
    echo "</form>\n";
}

// push the pages of the mbox
function push_pages($nmsgs) {
    global $sort, $fromMsg, $fromPage, $lng, $mbox, $nextPage, $prevPage;
    global $pluspsize;
    global $user_set;

    // print the More pages stuff
    // --
    // well, this is not necessary for most of us, but hey, may be we use this feature? 
    // --
    // this routine is written just for the people who has hundreds (may be thousands) of messages
    // staying just in one mailbox.
    // --
    // kinda complicated stuff but it works.
    // --
    $pages = ceil($nmsgs / $user_set["psize"]);
    $pgcnt = 0;
    if($pages != 1) { // if we have pages
       echo $lng->p(228);
       if($fromPage) {
           $prevPage = $fromPage - 10; // if we are not on the firstPage
           if($fromPage * $user_set["psize"] == $fromMsg) $prevMsg = $fromMsg - $user_set["psize"]; // if this page is x1 (e.g 21, 31, 41, etc)
           else $prevMsg = $fromMsg - (10 * $user_set["psize"]); // if this page is x1 make it (x-1)1 (e.g 23 -> 13, 45 -> 35, etc)
           put_ahref("RequestID=MBOXLST&mbox=" . urlencode($mbox) . "&sort=" . $sort . "&fromMsg=" . $prevMsg . "&fromPage=" . $prevPage, "&nbsp;&#171;&nbsp;");
       } else $fromPage = 0;
       for($i = $fromPage ; $i < $pages ; $i++, $pgcnt++) {
           $this_from = $i * $user_set["psize"];
           if($pgcnt == 10) {
              $nextPage = $i;
              if(($nextPage - 1) * $user_set["psize"] == $fromMsg) $nextMsg = $fromMsg + $user_set["psize"];
	      else {
	          $nextMsg = $fromMsg + (10 * $user_set["psize"]);
		  if($nextMsg > $nmsgs) // what if the next page does not exist?
		     $nextMsg = $nextPage * $user_set["psize"]; // if so, make the next page the first page of the next 10
              }
              put_ahref("RequestID=MBOXLST&mbox=" . urlencode($mbox) . "&sort=" . $sort . "&fromMsg=" . $nextMsg . "&fromPage=" . $nextPage, "&nbsp;&#187;");
              break; // not user set (max 10 "more pages")
           }
           $j = $i + 1;
           if($pgcnt) echo "&nbsp;<small>&#183;</small>&nbsp;";
           if($this_from != $fromMsg) 
              put_ahref("RequestID=MBOXLST&mbox=" . urlencode($mbox) . "&sort=" . $sort . "&fromMsg=" . $this_from . "&fromPage=" . $fromPage, $j);
           else echo "<b>$j</b>";
       }
    }

    // attach the nextPage stuff to the Previous|XXX|Next.
    if((($fromPage + 1) * $user_set["psize"]) == $pluspsize) $prevPage = $fromPage - 10;
    else $prevPage = $fromPage;
    if((($fromPage + 10) * $user_set["psize"]) == $pluspsize) $nextPage = $fromPage + 10;
    else $nextPage = $fromPage;
}

function push_errinfo() {
    global $err_msg, $info_msg;
    if(!empty($err_msg)) {
        echo "<!-- ERROR MSG : START-->";
        echo "<table><tr><td>";
        echo "<div align=\"center\" class=\"error\">&nbsp;$err_msg&nbsp;</div>";
        echo "</td></tr></table>";
        echo "<!-- ERROR MSG : FINISH -->\n";
    } else if(!empty($info_msg)) {
        echo "<!-- INFO MSG : START -->";
        echo "<table><tr><td>";
        echo "<div align=\"center\" class=\"info\">&nbsp;$info_msg&nbsp;</div>";
        echo "</td></tr></table>";
        echo "<!-- INFO MSG : FINISH -->\n";
    }
}
function push_pageheader($m) {
	$u = $GLOBALS["username"];
	$d = $GLOBALS["domain_name"];
	echo "<table border=\"0\" cellpadding=\"3\" cellspacing=\"0\" width=\"100%\">";
	echo "<tr><td align=\"left\" class=\"pagehdr\"><b>";
	echo $m . ":</b>&nbsp;" . $u . "@" . $d;
	echo "</td></tr></table>\n";
}

// select box for settings
function push_langs() {
	global $bsx_lang;
	global $set_lang;

	echo "<select name='set_lang' size='1'>\n";
	for($i = 0 ; $i < count($bsx_lang) ; $i++) {
		echo "<option value='$i'";
		if($i == $set_lang) echo " selected";
		echo ">" . $bsx_lang[$i]["long"] . "</option>\n";
	}
	echo "</select>\n";
}

// select box for themes
function push_themes() {
	global $bsx_theme;
	global $set_theme;

	echo "<select name='set_theme' size='1'>\n";
	for($i = 0 ; $i < count($bsx_theme) ; $i++) {
		if(!$bsx_theme[$i]["active"]) continue;
		echo "<option value='$i'";
		if($i == $set_theme) echo " selected";
		echo ">" . $bsx_theme[$i]["desc"] . "</option>\n";
	}
	echo "</select>\n";
}
?>