thomascube
2006-01-13 be2380fb47b05a222ec5b22deff36d5156a8c943
commit | author | age
4e17e6 1 <?php
T 2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/include/main.inc                                              |
6  |                                                                       |
7  | This file is part of the RoundCube Webmail client                     |
8  | Copyright (C) 2005, RoundCube Dev, - Switzerland                      |
30233b 9  | Licensed under the GNU GPL                                            |
4e17e6 10  |                                                                       |
T 11  | PURPOSE:                                                              |
12  |   Provide basic functions for the webmail package                     |
13  |                                                                       |
14  +-----------------------------------------------------------------------+
15  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16  +-----------------------------------------------------------------------+
17
18  $Id$
19
20 */
21
22 require_once('lib/des.inc');
0af7e8 23 require_once('lib/utf7.inc');
4e17e6 24
T 25
26 // register session and connect to server
27 function rcmail_startup($task='mail')
28   {
29   global $sess_id, $sess_auth, $sess_user_lang;
30   global $CONFIG, $INSTALL_PATH, $BROWSER, $OUTPUT, $_SESSION, $IMAP, $DB, $JS_OBJECT_NAME;
31
32   // check client
33   $BROWSER = rcube_browser();
de2e1e 34
4e17e6 35   // load config file
T 36   include_once('config/main.inc.php');
37   $CONFIG = is_array($rcmail_config) ? $rcmail_config : array();
38   $CONFIG['skin_path'] = $CONFIG['skin_path'] ? preg_replace('/\/$/', '', $CONFIG['skin_path']) : 'skins/default';
39
40   // load db conf
41   include_once('config/db.inc.php');
42   $CONFIG = array_merge($CONFIG, $rcmail_config);
43
fd8c50 44   if (empty($CONFIG['log_dir']))
T 45     $CONFIG['log_dir'] = $INSTALL_PATH.'logs';
46   else
47     $CONFIG['log_dir'] = ereg_replace('\/$', '', $CONFIG['log_dir']);
4e17e6 48
T 49   // set PHP error logging according to config
50   if ($CONFIG['debug_level'] & 1)
51     {
52     ini_set('log_errors', 1);
fd8c50 53     ini_set('error_log', $CONFIG['log_dir'].'/errors');
4e17e6 54     }
T 55   if ($CONFIG['debug_level'] & 4)
56     ini_set('display_errors', 1);
57   else
58     ini_set('display_errors', 0);
7902df 59   
T 60   // set session garbage collecting time according to session_lifetime
61   if (!empty($CONFIG['session_lifetime']))
62     ini_set('session.gc_maxlifetime', ($CONFIG['session_lifetime']+2)*60);
4e17e6 63
T 64
65   // prepare DB connection
f45ec7 66   require_once('include/rcube_'.(empty($CONFIG['db_backend']) ? 'db' : $CONFIG['db_backend']).'.inc');
S 67   
1676e1 68   $DB = new rcube_db($CONFIG['db_dsnw'], $CONFIG['db_dsnr']);
42b113 69   $DB->sqlite_initials = $INSTALL_PATH.'SQL/sqlite.initial.sql';
4e17e6 70
T 71   // we can use the database for storing session data
36df57 72   // session queries do not work with MDB2
1cded8 73   if ($CONFIG['db_backend']!='mdb2' && is_object($DB) /* && $DB->db_provider!='sqlite' */)
4e17e6 74     include_once('include/session.inc');
T 75
76
77   // init session
78   session_start();
79   $sess_id = session_id();
80   
81   // create session and set session vars
82   if (!$_SESSION['client_id'])
83     {
84     $_SESSION['client_id'] = $sess_id;
0af7e8 85     $_SESSION['user_lang'] = rcube_language_prop($CONFIG['locale_string']);
4e17e6 86     $_SESSION['auth_time'] = mktime();
T 87     $_SESSION['auth'] = rcmail_auth_hash($sess_id, $_SESSION['auth_time']);
88     unset($GLOBALS['_auth']);
89     }
90
91   // set session vars global
92   $sess_auth = $_SESSION['auth'];
0af7e8 93   $sess_user_lang = rcube_language_prop($_SESSION['user_lang']);
4e17e6 94
T 95
96   // overwrite config with user preferences
97   if (is_array($_SESSION['user_prefs']))
98     $CONFIG = array_merge($CONFIG, $_SESSION['user_prefs']);
99
100
101   // reset some session parameters when changing task
102   if ($_SESSION['task'] != $task)
103     unset($_SESSION['page']);
104
105   // set current task to session
106   $_SESSION['task'] = $task;
107
108
109   // create IMAP object
110   if ($task=='mail')
111     rcmail_imap_init();
112
113
114   // set localization
115   if ($CONFIG['locale_string'])
116     setlocale(LC_ALL, $CONFIG['locale_string']);
117   else if ($sess_user_lang)
118     setlocale(LC_ALL, $sess_user_lang);
119
120
121   register_shutdown_function('rcmail_shutdown');
122   }
123   
124
125 // create authorization hash
126 function rcmail_auth_hash($sess_id, $ts)
127   {
128   global $CONFIG;
129   
130   $auth_string = sprintf('rcmail*sess%sR%s*Chk:%s;%s',
131                          $sess_id,
132                          $ts,
133                          $CONFIG['ip_check'] ? $_SERVER['REMOTE_ADDR'] : '***.***.***.***',
134                          $_SERVER['HTTP_USER_AGENT']);
135   
136   if (function_exists('sha1'))
137     return sha1($auth_string);
138   else
139     return md5($auth_string);
140   }
141
142
143
144 // create IMAP object and connect to server
145 function rcmail_imap_init($connect=FALSE)
146   {
1cded8 147   global $CONFIG, $DB, $IMAP;
6dc026 148
1cded8 149   $IMAP = new rcube_imap($DB);
15a9d1 150   $IMAP->debug_level = $CONFIG['debug_level'];
T 151   $IMAP->skip_deleted = $CONFIG['skip_deleted'];
152
4e17e6 153
7902df 154   // connect with stored session data
T 155   if ($connect)
156     {
157     if (!($conn = $IMAP->connect($_SESSION['imap_host'], $_SESSION['username'], decrypt_passwd($_SESSION['password']), $_SESSION['imap_port'], $_SESSION['imap_ssl'])))
158       show_message('imaperror', 'error');
159       
160     rcmail_set_imap_prop();
161     }
162
6dc026 163   // enable caching of imap data
T 164   if ($CONFIG['enable_caching']===TRUE)
165     $IMAP->set_caching(TRUE);
166
4e17e6 167   if (is_array($CONFIG['default_imap_folders']))
T 168     $IMAP->set_default_mailboxes($CONFIG['default_imap_folders']);
169
170   // set pagesize from config
171   if (isset($CONFIG['pagesize']))
172     $IMAP->set_pagesize($CONFIG['pagesize']);
7902df 173   }
4e17e6 174
T 175
7902df 176 // set root dir and last stored mailbox
T 177 // this must be done AFTER connecting to the server
178 function rcmail_set_imap_prop()
179   {
180   global $CONFIG, $IMAP;
181
182   // set root dir from config
183   if (strlen($CONFIG['imap_root']))
184     $IMAP->set_rootdir($CONFIG['imap_root']);
185
186   if (strlen($_SESSION['mbox']))
187     $IMAP->set_mailbox($_SESSION['mbox']);
188     
189   if (isset($_SESSION['page']))
190     $IMAP->set_page($_SESSION['page']);
4e17e6 191   }
T 192
193
194 // do these things on script shutdown
195 function rcmail_shutdown()
196   {
197   global $IMAP;
198   
199   if (is_object($IMAP))
200     {
201     $IMAP->close();
202     $IMAP->write_cache();
203     }
0af7e8 204     
T 205   // before closing the database connection, write session data
206   session_write_close();
4e17e6 207   }
T 208
209
210 // destroy session data and remove cookie
211 function rcmail_kill_session()
212   {
213 /* $sess_name = session_name();
214   if (isset($_COOKIE[$sess_name]))
215    setcookie($sess_name, '', time()-42000, '/');
216 */
217   $_SESSION = array();
218   session_destroy();
219   }
220
221
222 // return correct name for a specific database table
223 function get_table_name($table)
224   {
225   global $CONFIG;
226   
227   // return table name if configured
228   $config_key = 'db_table_'.$table;
229
230   if (strlen($CONFIG[$config_key]))
231     return $CONFIG[$config_key];
232   
233   return $table;
234   }
235
236
1cded8 237 // return correct name for a specific database sequence
T 238 // (used for Postres only)
239 function get_sequence_name($sequence)
240   {
241   global $CONFIG;
242   
243   // return table name if configured
244   $config_key = 'db_sequence_'.$sequence;
245
246   if (strlen($CONFIG[$config_key]))
247     return $CONFIG[$config_key];
248   
249   return $table;
250   }
0af7e8 251
T 252
253 // check the given string and returns language properties
254 function rcube_language_prop($lang, $prop='lang')
255   {
256   global $INSTLL_PATH;
257   static $rcube_languages, $rcube_language_aliases, $rcube_charsets;
258
259   if (empty($rcube_languages))
260     @include($INSTLL_PATH.'program/localization/index.inc');
261     
262   // check if we have an alias for that language
263   if (!isset($rcube_languages[$lang]) && isset($rcube_language_aliases[$lang]))
264     $lang = $rcube_language_aliases[$lang];
265     
266   // try the first two chars
267   if (!isset($rcube_languages[$lang]) && strlen($lang>2))
268     {
269     $lang = substr($lang, 0, 2);
270     $lang = rcube_language_prop($lang);
271     }
272
273   if (!isset($rcube_languages[$lang]))
274     $lang = 'en_US';
275
276   // language has special charset configured
277   if (isset($rcube_charsets[$lang]))
278     $charset = $rcube_charsets[$lang];
279   else
280     $charset = 'UTF-8';    
281
282   if ($prop=='charset')
283     return $charset;
284   else
285     return $lang;
286   }
1cded8 287   
4e17e6 288
T 289 // init output object for GUI and add common scripts
290 function load_gui()
291   {
3f9edb 292   global $CONFIG, $OUTPUT, $COMM_PATH, $JS_OBJECT_NAME, $sess_user_lang;
4e17e6 293
T 294   // init output page
295   $OUTPUT = new rcube_html_page();
296   
297   // add common javascripts
298   $javascript = "var $JS_OBJECT_NAME = new rcube_webmail();\n";
299   $javascript .= "$JS_OBJECT_NAME.set_env('comm_path', '$COMM_PATH');\n";
300
597170 301   if (!empty($GLOBALS['_framed']))
4e17e6 302     $javascript .= "$JS_OBJECT_NAME.set_env('framed', true);\n";
7cc38e 303     
4e17e6 304   $OUTPUT->add_script($javascript);
T 305   $OUTPUT->include_script('program/js/common.js');
7cc38e 306   $OUTPUT->include_script('program/js/app.js');
T 307
308   // set user-selected charset
5bc8cb 309   if (!empty($CONFIG['charset']))
7cc38e 310     $OUTPUT->set_charset($CONFIG['charset']);
T 311   else
312     rcmail_set_locale($sess_user_lang);
0af7e8 313
10a699 314   // add some basic label to client
T 315   rcube_add_label('loading');
0af7e8 316   }
7cc38e 317
T 318
319 // set localization charset based on the given language
320 function rcmail_set_locale($lang)
321   {
3f9edb 322   global $OUTPUT;
T 323   $OUTPUT->set_charset(rcube_language_prop($lang, 'charset'));
7cc38e 324   }
4e17e6 325
T 326
327 // perfom login to the IMAP server and to the webmail service
328 function rcmail_login($user, $pass, $host=NULL)
329   {
330   global $CONFIG, $IMAP, $DB, $sess_user_lang;
42b113 331   $user_id = NULL;
4e17e6 332   
T 333   if (!$host)
334     $host = $CONFIG['default_host'];
335
f619de 336   // parse $host URL
T 337   $a_host = parse_url($host);
338   if ($a_host['host'])
339     {
340     $host = $a_host['host'];
341     $imap_ssl = (isset($a_host['scheme']) && in_array($a_host['scheme'], array('ssl','imaps','tls'))) ? TRUE : FALSE;
342     $imap_port = isset($a_host['port']) ? $a_host['port'] : ($imap_ssl ? 993 : $CONFIG['default_port']);
343     }
344
4e17e6 345   // query if user already registered
d7cb77 346   $sql_result = $DB->query("SELECT user_id, username, language, preferences
S 347                             FROM ".get_table_name('users')."
348                             WHERE  mail_host=? AND (username=? OR alias=?)",
349                             $host,
350                             $user,
351                             $user);
4e17e6 352
42b113 353   // user already registered -> overwrite username
4e17e6 354   if ($sql_arr = $DB->fetch_assoc($sql_result))
T 355     {
356     $user_id = $sql_arr['user_id'];
42b113 357     $user = $sql_arr['username'];
T 358     }
359
977a29 360   // try to resolve email address from virtuser table    
T 361   if (!empty($CONFIG['virtuser_file']) && strstr($user, '@'))
362     $user = rcmail_email2user($user);
363
364
42b113 365   // exit if IMAP login failed
T 366   if (!($imap_login  = $IMAP->connect($host, $user, $pass, $imap_port, $imap_ssl)))
367     return FALSE;
368
369   // user already registered
370   if ($user_id && !empty($sql_arr))
371     {
4e17e6 372     // get user prefs
T 373     if (strlen($sql_arr['preferences']))
374       {
375       $user_prefs = unserialize($sql_arr['preferences']);
376       $_SESSION['user_prefs'] = $user_prefs;
377       array_merge($CONFIG, $user_prefs);
378       }
379
f3b659 380
4e17e6 381     // set user specific language
T 382     if (strlen($sql_arr['language']))
383       $sess_user_lang = $_SESSION['user_lang'] = $sql_arr['language'];
f3b659 384       
4e17e6 385     // update user's record
d7cb77 386     $DB->query("UPDATE ".get_table_name('users')."
667737 387                 SET    last_login=now()
d7cb77 388                 WHERE  user_id=?",
S 389                 $user_id);
4e17e6 390     }
T 391   // create new system user
392   else if ($CONFIG['auto_create_user'])
393     {
394     $user_id = rcmail_create_user($user, $host);
395     }
396
397   if ($user_id)
398     {
399     $_SESSION['user_id']   = $user_id;
400     $_SESSION['imap_host'] = $host;
7902df 401     $_SESSION['imap_port'] = $imap_port;
T 402     $_SESSION['imap_ssl']  = $imap_ssl;
4e17e6 403     $_SESSION['username']  = $user;
f3b659 404     $_SESSION['user_lang'] = $sess_user_lang;
4e17e6 405     $_SESSION['password']  = encrypt_passwd($pass);
T 406
407     // force reloading complete list of subscribed mailboxes    
408     $IMAP->clear_cache('mailboxes');
409
410     return TRUE;
411     }
412
413   return FALSE;
414   }
415
416
417 // create new entry in users and identities table
418 function rcmail_create_user($user, $host)
419   {
420   global $DB, $CONFIG, $IMAP;
977a29 421
T 422   $user_email = '';
423
424   // try to resolve user in virtusertable
425   if (!empty($CONFIG['virtuser_file']) && strstr($user, '@')==FALSE)
426     $user_email = rcmail_user2email($user);
427
d7cb77 428   $DB->query("INSERT INTO ".get_table_name('users')."
977a29 429               (created, last_login, username, mail_host, alias, language)
T 430               VALUES (now(), now(), ?, ?, ?, ?)",
d7cb77 431               $user,
S 432               $host,
977a29 433               $user_email,
d7cb77 434               $_SESSION['user_lang']);
4e17e6 435
1cded8 436   if ($user_id = $DB->insert_id(get_sequence_name('users')))
4e17e6 437     {
977a29 438     if (is_string($CONFIG['mail_domain']))
T 439       $mail_domain = $CONFIG['mail_domain'];
440     else if (is_array($CONFIG['mail_domain']) && isset($CONFIG['mail_domain'][$host]))
441       $mail_domain = $CONFIG['mail_domain'][$host];
442     else
443       $mail_domain = $host;
444    
445     if ($user_email=='')
446       $user_email = strstr($user, '@') ? $user : sprintf('%s@%s', $user, $mail_domain);
447
52c1f2 448     $user_name = $user!=$user_email ? $user : '';
977a29 449
4e17e6 450     // also create a new identity record
d7cb77 451     $DB->query("INSERT INTO ".get_table_name('identities')."
1cded8 452                 (user_id, del, standard, name, email)
T 453                 VALUES (?, 0, 1, ?, ?)",
d7cb77 454                 $user_id,
S 455                 $user_name,
456                 $user_email);
4e17e6 457                        
T 458     // get existing mailboxes
459     $a_mailboxes = $IMAP->list_mailboxes();
460     
461     // check if the configured mailbox for sent messages exists
462     if ($CONFIG['sent_mbox'] && !in_array_nocase($CONFIG['sent_mbox'], $a_mailboxes))
463       $IMAP->create_mailbox($CONFIG['sent_mbox'], TRUE);
464
465     // check if the configured mailbox for sent messages exists
466     if ($CONFIG['trash_mbox'] && !in_array_nocase($CONFIG['trash_mbox'], $a_mailboxes))
467       $IMAP->create_mailbox($CONFIG['trash_mbox'], TRUE);
42b113 468     }
T 469   else
470     {
471     raise_error(array('code' => 500,
472                       'type' => 'php',
473                       'line' => __LINE__,
474                       'file' => __FILE__,
475                       'message' => "Failed to create new user"), TRUE, FALSE);
4e17e6 476     }
T 477     
478   return $user_id;
479   }
480
481
977a29 482 // load virtuser table in array
T 483 function rcmail_getvirtualfile()
484   {
485   global $CONFIG;
486   if (empty($CONFIG['virtuser_file']) || !is_file($CONFIG['virtuser_file']))
487     return FALSE;
488   
489   // read file 
490   $a_lines = file($CONFIG['virtuser_file']);
491   return $a_lines;
492   }
493
494
495 // find matches of the given pattern in virtuser table
496 function rcmail_findinvirtual($pattern)
497   {
498   $result = array();
499   $virtual = rcmail_getvirtualfile();
500   if ($virtual==FALSE)
501     return $result;
502
503   // check each line for matches
504   foreach ($virtual as $line)
505     {
506     $line = trim($line);
507     if (empty($line) || $line{0}=='#')
508       continue;
509       
510     if (eregi($pattern, $line))
511       $result[] = $line;
512     }
513
514   return $result;
515   }
516
517
518 // resolve username with virtuser table
519 function rcmail_email2user($email)
520   {
521   $user = $email;
522   $r = rcmail_findinvirtual("^$email");
523
524   for ($i=0; $i<count($r); $i++)
525     {
526     $data = $r[$i];
527     $arr = preg_split('/\s+/', $data);
528     if(count($arr)>0)
529       {
530       $user = trim($arr[count($arr)-1]);
531       break;
532       }
533     }
534
535   return $user;
536   }
537
538
539 // resolve e-mail address with virtuser table
540 function rcmail_user2email($user)
541   {
542   $email = "";
543   $r = rcmail_findinvirtual("$user$");
544
545   for ($i=0; $i<count($r); $i++)
546     {
547     $data=$r[$i];
548     $arr = preg_split('/\s+/', $data);
549     if (count($arr)>0)
550       {
551       $email = trim($arr[0]);
552       break;
553       }
554     }
555
556   return $email;
557   } 
558
559
10a699 560 // overwrite action variable  
T 561 function rcmail_overwrite_action($action)
562   {
563   global $OUTPUT, $JS_OBJECT_NAME;
564   $GLOBALS['_action'] = $action;
565
566   $OUTPUT->add_script(sprintf("\n%s.set_env('action', '%s');", $JS_OBJECT_NAME, $action));  
567   }
568
569
4e17e6 570 function show_message($message, $type='notice')
T 571   {
572   global $OUTPUT, $JS_OBJECT_NAME, $REMOTE_REQUEST;
573
597170 574   $framed = $GLOBALS['_framed'];
4e17e6 575   $command = sprintf("display_message('%s', '%s');",
T 576                      addslashes(rep_specialchars_output(rcube_label($message))),
577                      $type);
578                      
579   if ($REMOTE_REQUEST)
580     return 'this.'.$command;
581   
582   else
583     $OUTPUT->add_script(sprintf("%s%s.%s",
584                                 $framed ? sprintf('if(parent.%s)parent.', $JS_OBJECT_NAME) : '',
585                                 $JS_OBJECT_NAME,
586                                 $command));
587   
588   // console(rcube_label($message));
589   }
590
591
592 function console($msg, $type=1)
593   {
520c36 594   if ($GLOBALS['REMOTE_REQUEST'])
T 595     print "// $msg\n";
596   else
597     {
598     print $msg;
599     print "\n<hr>\n";
600     }
4e17e6 601   }
T 602
603
604 function encrypt_passwd($pass)
605   {
606   $cypher = des('rcmail?24BitPwDkeyF**ECB', $pass, 1, 0, NULL);
607   return base64_encode($cypher);
608   }
609
610
611 function decrypt_passwd($cypher)
612   {
613   $pass = des('rcmail?24BitPwDkeyF**ECB', base64_decode($cypher), 0, 0, NULL);
614   return trim($pass);
615   }
616
617
618 // send correct response on a remote request
15a9d1 619 function rcube_remote_response($js_code, $flush=FALSE)
4e17e6 620   {
15a9d1 621   static $s_header_sent = FALSE;
T 622   
623   if (!$s_header_sent)
624     {
625     $s_header_sent = TRUE;
626     send_nocacheing_headers();
627     header('Content-Type: application/x-javascript');
628     print '/** remote response ['.date('d/M/Y h:i:s O')."] **/\n";
629     }
4e17e6 630
15a9d1 631   // send response code
4e17e6 632   print $js_code;
15a9d1 633
T 634   if ($flush)  // flush the output buffer
635     flush();
636   else         // terminate script
637     exit;
4e17e6 638   }
T 639
640
9fee0e 641 // read directory program/localization/ and return a list of available languages
T 642 function rcube_list_languages()
643   {
644   global $CONFIG, $INSTALL_PATH;
645   static $sa_languages = array();
646
647   if (!sizeof($sa_languages))
648     {
7cc38e 649     @include($INSTLL_PATH.'program/localization/index.inc');
9fee0e 650
T 651     if ($dh = @opendir($INSTLL_PATH.'program/localization'))
652       {
653       while (($name = readdir($dh)) !== false)
654         {
655         if ($name{0}=='.' || !is_dir($INSTLL_PATH.'program/localization/'.$name))
656           continue;
657
658         if ($label = $rcube_languages[$name])
659           $sa_languages[$name] = $label ? $label : $name;
660         }
661       closedir($dh);
662       }
663     }
664
665   return $sa_languages;
666   }
667
4e17e6 668
10a699 669 // add a localized label to the client environment
T 670 function rcube_add_label()
671   {
672   global $OUTPUT, $JS_OBJECT_NAME;
673   
674   $arg_list = func_get_args();
675   foreach ($arg_list as $i => $name)
676     $OUTPUT->add_script(sprintf("%s.add_label('%s', '%s');",
677                                 $JS_OBJECT_NAME,
678                                 $name,
679                                 rep_specialchars_output(rcube_label($name), 'js')));  
1cded8 680   }
T 681
682
683 // remove temp files of a session
684 function rcmail_clear_session_temp($sess_id)
685   {
686   global $CONFIG;
687
688   $temp_dir = $CONFIG['temp_dir'].(!eregi('\/$', $CONFIG['temp_dir']) ? '/' : '');
689   $cache_dir = $temp_dir.$sess_id;
690
691   if (is_dir($cache_dir))
692     {
693     clear_directory($cache_dir);
694     rmdir($cache_dir);
695     }  
696   }
697
698
699
3f9edb 700 // convert a string from one charset to another
T 701 // this function is not complete and not tested well
702 function rcube_charset_convert($str, $from, $to=NULL)
0af7e8 703   {
3f9edb 704   $from = strtolower($from);
T 705   $to = $to==NULL ? strtolower($GLOBALS['CHARSET']) : strtolower($to);
0af7e8 706   
3f9edb 707   if ($from==$to)
T 708     return $str;
709   
710   // decode characters
711   if ($from=='utf-7')
712     $str = UTF7DecodeString($str);
713   else if ($from=='utf-8' && function_exists('utf8_decode'))
714     $str = utf8_decode($str);
715   else if ($from=="koi8-r")
716     $str = convert_cyr_string($str, 'k', 'i');
717   else if ($from=="windows-1251" || $from=="win-1251")
718     $str = convert_cyr_string($str, 'w', 'i');
719   else if ($from=="x-cp866")
720     $str = convert_cyr_string($str, 'a', 'i');
721   else if ($from=="x-mac-cyrillic")
722     $str = convert_cyr_string($str, 'm', 'i');
0af7e8 723
3f9edb 724   // encode string for output
T 725   if ($to=='utf-8' && function_exists('utf8_encode'))
726     return utf8_encode($str);
727   else if ($to=='utf-7')
728     return UTF7EncodeString($str);
729
730   // return raw string
731   return $str;
0af7e8 732   }
3f9edb 733
0af7e8 734
T 735
1cded8 736 // replace specials characters to a specific encoding type
T 737 function rep_specialchars_output($str, $enctype='', $mode='', $newlines=TRUE)
738   {
3f9edb 739   global $OUTPUT_TYPE, $OUTPUT;
1cded8 740   static $html_encode_arr, $js_rep_table, $rtf_rep_table, $xml_rep_table;
T 741
742   if (!$enctype)
743     $enctype = $GLOBALS['OUTPUT_TYPE'];
744
745   // convert nbsps back to normal spaces if not html
746   if ($enctype!='html')
747     $str = str_replace(chr(160), ' ', $str);
748
749   // encode for plaintext
750   if ($enctype=='text')
751     return str_replace("\r\n", "\n", $mode=='remove' ? strip_tags($str) : $str);
752
753   // encode for HTML output
754   if ($enctype=='html')
755     {
756     if (!$html_encode_arr)
757       {
0af7e8 758       $html_encode_arr = get_html_translation_table(HTML_SPECIALCHARS);        
1cded8 759       unset($html_encode_arr['?']);
T 760       unset($html_encode_arr['&']);
761       }
762
763     $ltpos = strpos($str, '<');
764     $encode_arr = $html_encode_arr;
765
766     // don't replace quotes and html tags
767     if (($mode=='show' || $mode=='') && $ltpos!==false && strpos($str, '>', $ltpos)!==false)
768       {
769       unset($encode_arr['"']);
770       unset($encode_arr['<']);
771       unset($encode_arr['>']);
772       }
773     else if ($mode=='remove')
774       $str = strip_tags($str);
775       
776     $out = strtr($str, $encode_arr);
0af7e8 777       
1cded8 778     return $newlines ? nl2br($out) : $out;
T 779     }
780
781
782   if ($enctype=='url')
783     return rawurlencode($str);
784
785
786   // if the replace tables for RTF, XML and JS are not yet defined
787   if (!$js_rep_table)
788     {
789     $js_rep_table = $rtf_rep_table = $xml_rep_table = array();
790
791     for ($c=160; $c<256; $c++)  // can be increased to support more charsets
792       {
793       $hex = dechex($c);
794       $rtf_rep_table[Chr($c)] = "\\'$hex";
795       $xml_rep_table[Chr($c)] = "&#$c;";
796       
3f9edb 797       if ($OUTPUT->get_charset()=='ISO-8859-1')
1cded8 798         $js_rep_table[Chr($c)] = sprintf("\u%s%s", str_repeat('0', 4-strlen($hex)), $hex);
T 799       }
800
801     $js_rep_table['"'] = sprintf("\u%s%s", str_repeat('0', 4-strlen(dechex(34))), dechex(34));
802     $xml_rep_table['"'] = '&quot;';
803     }
804
805   // encode for RTF
806   if ($enctype=='xml')
807     return strtr($str, $xml_rep_table);
808
809   // encode for javascript use
810   if ($enctype=='js')
811     return preg_replace(array("/\r\n/", '/"/', "/([^\\\])'/"), array('\n', '\"', "$1\'"), strtr($str, $js_rep_table));
812
813   // encode for RTF
814   if ($enctype=='rtf')
815     return preg_replace("/\r\n/", "\par ", strtr($str, $rtf_rep_table));
816
817   // no encoding given -> return original string
818   return $str;
10a699 819   }
T 820
821
4e17e6 822
T 823 // ************** template parsing and gui functions **************
824
825
826 // return boolean if a specific template exists
827 function template_exists($name)
828   {
829   global $CONFIG, $OUTPUT;
830   $skin_path = $CONFIG['skin_path'];
831
832   // check template file
833   return is_file("$skin_path/templates/$name.html");
834   }
835
836
837 // get page template an replace variable
838 // similar function as used in nexImage
839 function parse_template($name='main', $exit=TRUE)
840   {
841   global $CONFIG, $OUTPUT;
842   $skin_path = $CONFIG['skin_path'];
843
844   // read template file
845   $templ = '';
846   $path = "$skin_path/templates/$name.html";
847
848   if($fp = @fopen($path, 'r'))
849     {
850     $templ = fread($fp, filesize($path));
851     fclose($fp);
852     }
853   else
854     {
855     raise_error(array('code' => 500,
856                       'type' => 'php',
857                       'line' => __LINE__,
858                       'file' => __FILE__,
859                       'message' => "Error loading template for '$name'"), TRUE, TRUE);
860     return FALSE;
861     }
862
863
864   // parse for specialtags
865   $output = parse_rcube_xml($templ);
866   
867   $OUTPUT->write(trim(parse_with_globals($output)), $skin_path);
868
869   if ($exit)
870     exit;
871   }
872
873
874
875 // replace all strings ($varname) with the content of the according global variable
876 function parse_with_globals($input)
877   {
b595c9 878   $GLOBALS['__comm_path'] = $GLOBALS['COMM_PATH'];
4e17e6 879   $output = preg_replace('/\$(__[a-z0-9_\-]+)/e', '$GLOBALS["\\1"]', $input);
T 880   return $output;
881   }
882
883
884
885 function parse_rcube_xml($input)
886   {
887   $output = preg_replace('/<roundcube:([-_a-z]+)\s+([^>]+)>/Uie', "rcube_xml_command('\\1', '\\2')", $input);
888   return $output;
889   }
890
891
892 function rcube_xml_command($command, $str_attrib, $a_attrib=NULL)
893   {
0af7e8 894   global $IMAP, $CONFIG, $OUTPUT;
4e17e6 895   
T 896   $attrib = array();
897   $command = strtolower($command);
898
899   preg_match_all('/\s*([-_a-z]+)=["]([^"]+)["]?/i', stripslashes($str_attrib), $regs, PREG_SET_ORDER);
900
901   // convert attributes to an associative array (name => value)
902   if ($regs)
903     foreach ($regs as $attr)
904       $attrib[strtolower($attr[1])] = $attr[2];
905   else if ($a_attrib)
906     $attrib = $a_attrib;
907
908   // execute command
909   switch ($command)
910     {
911     // return a button
912     case 'button':
913       if ($attrib['command'])
914         return rcube_button($attrib);
915       break;
916
917     // show a label
918     case 'label':
919       if ($attrib['name'] || $attrib['command'])
7dd801 920         return rep_specialchars_output(rcube_label($attrib));
4e17e6 921       break;
T 922
923     // create a menu item
924     case 'menu':
925       if ($attrib['command'] && $attrib['group'])
926         rcube_menu($attrib);
927       break;
928
929     // include a file 
930     case 'include':
931       $path = realpath($CONFIG['skin_path'].$attrib['file']);
932       
933       if($fp = @fopen($path, 'r'))
934         {
935         $incl = fread($fp, filesize($path));
936         fclose($fp);        
937         return parse_rcube_xml($incl);
938         }
939       break;
940
941     // return code for a specific application object
942     case 'object':
943       $object = strtolower($attrib['name']);
944
1cded8 945       $object_handlers = array(
15a9d1 946         // GENERAL
T 947         'loginform' => 'rcmail_login_form',
948         'username'  => 'rcmail_current_username',
949         
1cded8 950         // MAIL
T 951         'mailboxlist' => 'rcmail_mailbox_list',
15a9d1 952         'message' => 'rcmail_message_container',
1cded8 953         'messages' => 'rcmail_message_list',
T 954         'messagecountdisplay' => 'rcmail_messagecount_display',
955         'messageheaders' => 'rcmail_message_headers',
956         'messagebody' => 'rcmail_message_body',
957         'messageattachments' => 'rcmail_message_attachments',
958         'blockedobjects' => 'rcmail_remote_objects_msg',
959         'messagecontentframe' => 'rcmail_messagecontent_frame',
960         'messagepartframe' => 'rcmail_message_part_frame',
961         'messagepartcontrols' => 'rcmail_message_part_controls',
962         'composeheaders' => 'rcmail_compose_headers',
963         'composesubject' => 'rcmail_compose_subject',
964         'composebody' => 'rcmail_compose_body',
965         'composeattachmentlist' => 'rcmail_compose_attachment_list',
966         'composeattachmentform' => 'rcmail_compose_attachment_form',
967         'composeattachment' => 'rcmail_compose_attachment_field',
968         'priorityselector' => 'rcmail_priority_selector',
969         'charsetselector' => 'rcmail_charset_selector',
970         
971         // ADDRESS BOOK
972         'addresslist' => 'rcmail_contacts_list',
973         'addressframe' => 'rcmail_contact_frame',
974         'recordscountdisplay' => 'rcmail_rowcount_display',
975         'contactdetails' => 'rcmail_contact_details',
976         'contacteditform' => 'rcmail_contact_editform',
d1d2c4 977         'ldappublicsearch' => 'rcmail_ldap_public_search_form',
S 978         'ldappublicaddresslist' => 'rcmail_ldap_public_list',
1cded8 979
T 980         // USER SETTINGS
981         'userprefs' => 'rcmail_user_prefs_form',
982         'itentitieslist' => 'rcmail_identities_list',
983         'identityframe' => 'rcmail_identity_frame',
984         'identityform' => 'rcube_identity_form',
985         'foldersubscription' => 'rcube_subscription_form',
986         'createfolder' => 'rcube_create_folder_form',
987         'composebody' => 'rcmail_compose_body'
988       );
989
990       
991       // execute object handler function
15a9d1 992       if ($object_handlers[$object] && function_exists($object_handlers[$object]))
1cded8 993         return call_user_func($object_handlers[$object], $attrib);
4e17e6 994
T 995       else if ($object=='pagetitle')
996         {
997         $task = $GLOBALS['_task'];
15a9d1 998         $title = !empty($CONFIG['product_name']) ? $CONFIG['product_name'].' :: ' : '';
T 999         
4e17e6 1000         if ($task=='mail' && isset($GLOBALS['MESSAGE']['subject']))
15a9d1 1001           $title .= $GLOBALS['MESSAGE']['subject'];
4e17e6 1002         else if (isset($GLOBALS['PAGE_TITLE']))
15a9d1 1003           $title .= $GLOBALS['PAGE_TITLE'];
4e17e6 1004         else if ($task=='mail' && ($mbox_name = $IMAP->get_mailbox_name()))
3f9edb 1005           $title .= rcube_charset_convert($mbox_name, 'UTF-7', 'UTF-8');
4e17e6 1006         else
15a9d1 1007           $title .= $task;
T 1008           
1009         return rep_specialchars_output($title, 'html', 'all');
4e17e6 1010         }
T 1011
1012       break;
1013     }
1014
1015   return '';
1016   }
1017
1018
1019 // create and register a button
1020 function rcube_button($attrib)
1021   {
14eafe 1022   global $CONFIG, $OUTPUT, $JS_OBJECT_NAME, $BROWSER;
4e17e6 1023   static $sa_buttons = array();
T 1024   static $s_button_count = 100;
1025   
1026   $skin_path = $CONFIG['skin_path'];
1027   
1028   if (!($attrib['command'] || $attrib['name']))
1029     return '';
1030
1031   // try to find out the button type
1032   if ($attrib['type'])
1033     $attrib['type'] = strtolower($attrib['type']);
1034   else
1035     $attrib['type'] = ($attrib['image'] || $attrib['imagepas'] || $arg['imagect']) ? 'image' : 'link';
1036   
1037   
1038   $command = $attrib['command'];
1039   
1040   // take the button from the stack
1041   if($attrib['name'] && $sa_buttons[$attrib['name']])
1042     $attrib = $sa_buttons[$attrib['name']];
1043
1044   // add button to button stack
1045   else if($attrib['image'] || $arg['imagect'] || $attrib['imagepas'] || $attrib['class'])
1046     {
1047     if(!$attrib['name'])
1048       $attrib['name'] = $command;
1049
1050     if (!$attrib['image'])
1051       $attrib['image'] = $attrib['imagepas'] ? $attrib['imagepas'] : $attrib['imageact'];
1052
1053     $sa_buttons[$attrib['name']] = $attrib;
1054     }
1055
1056   // get saved button for this command/name
1057   else if ($command && $sa_buttons[$command])
1058     $attrib = $sa_buttons[$command];
1059
1060   //else
1061   //  return '';
1062
1063
1064   // set border to 0 because of the link arround the button
1065   if ($attrib['type']=='image' && !isset($attrib['border']))
1066     $attrib['border'] = 0;
1067     
1068   if (!$attrib['id'])
1069     $attrib['id'] =  sprintf('rcmbtn%d', $s_button_count++);
1070
1071   // get localized text for labels and titles
1072   if ($attrib['title'])
1073     $attrib['title'] = rep_specialchars_output(rcube_label($attrib['title']));
1074   if ($attrib['label'])
1075     $attrib['label'] = rep_specialchars_output(rcube_label($attrib['label']));
1076
1077   if ($attrib['alt'])
1078     $attrib['alt'] = rep_specialchars_output(rcube_label($attrib['alt']));
14eafe 1079
T 1080   // set title to alt attribute for IE browsers
1081   if ($BROWSER['ie'] && $attrib['title'] && !$attrib['alt'])
1082     {
1083     $attrib['alt'] = $attrib['title'];
1084     unset($attrib['title']);
1085     }
1086
4e17e6 1087   // add empty alt attribute for XHTML compatibility
T 1088   if (!isset($attrib['alt']))
1089     $attrib['alt'] = '';
1090
1091
1092   // register button in the system
1093   if ($attrib['command'])
1094     $OUTPUT->add_script(sprintf("%s.register_button('%s', '%s', '%s', '%s', '%s', '%s');",
1095                                 $JS_OBJECT_NAME,
1096                                 $command,
1097                                 $attrib['id'],
1098                                 $attrib['type'],
1099                                 $attrib['imageact'] ? $skin_path.$attrib['imageact'] : $attrib['classact'],
1100                                 $attirb['imagesel'] ? $skin_path.$attirb['imagesel'] : $attrib['classsel'],
1101                                 $attrib['imageover'] ? $skin_path.$attrib['imageover'] : ''));
1102
1103   // overwrite attributes
1104   if (!$attrib['href'])
1105     $attrib['href'] = '#';
1106
1107   if ($command)
1108     $attrib['onclick'] = sprintf("return %s.command('%s','%s',this)", $JS_OBJECT_NAME, $command, $attrib['prop']);
1109     
1110   if ($command && $attrib['imageover'])
1111     {
1112     $attrib['onmouseover'] = sprintf("return %s.button_over('%s','%s')", $JS_OBJECT_NAME, $command, $attrib['id']);
1113     $attrib['onmouseout'] = sprintf("return %s.button_out('%s','%s')", $JS_OBJECT_NAME, $command, $attrib['id']);
1114     }
1115
1116
1117   $out = '';
1118
1119   // generate image tag
1120   if ($attrib['type']=='image')
1121     {
1cded8 1122     $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id', 'width', 'height', 'border', 'hspace', 'vspace', 'align', 'alt'));
4e17e6 1123     $img_tag = sprintf('<img src="%%s"%s />', $attrib_str);
T 1124     $btn_content = sprintf($img_tag, $skin_path.$attrib['image']);
1125     if ($attrib['label'])
1126       $btn_content .= ' '.$attrib['label'];
1127     
1128     $link_attrib = array('href', 'onclick', 'onmouseover', 'onmouseout', 'title');
1129     }
1130   else if ($attrib['type']=='link')
1131     {
1132     $btn_content = $attrib['label'] ? $attrib['label'] : $attrib['command'];
1133     $link_attrib = array('href', 'onclick', 'title', 'id', 'class', 'style');
1134     }
1135   else if ($attrib['type']=='input')
1136     {
1137     $attrib['type'] = 'button';
1138     
1139     if ($attrib['label'])
1140       $attrib['value'] = $attrib['label'];
1141       
1142     $attrib_str = create_attrib_string($attrib, array('type', 'value', 'onclick', 'id', 'class', 'style'));
1143     $out = sprintf('<input%s disabled />', $attrib_str);
1144     }
1145
1146   // generate html code for button
1147   if ($btn_content)
1148     {
1149     $attrib_str = create_attrib_string($attrib, $link_attrib);
1150     $out = sprintf('<a%s>%s</a>', $attrib_str, $btn_content);
1151     }
1152
1153   return $out;
1154   }
1155
1156
1157 function rcube_menu($attrib)
1158   {
1159   
1160   return '';
1161   }
1162
1163
1164
d1d2c4 1165 function rcube_table_output($attrib, $table_data, $a_show_cols, $id_col)
4e17e6 1166   {
T 1167   global $DB;
1168   
1169   // allow the following attributes to be added to the <table> tag
1170   $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id', 'cellpadding', 'cellspacing', 'border', 'summary'));
1171   
1172   $table = '<table' . $attrib_str . ">\n";
1173     
1174   // add table title
1175   $table .= "<thead><tr>\n";
1176
1177   foreach ($a_show_cols as $col)
1038d5 1178     $table .= '<td class="'.$col.'">' . rep_specialchars_output(rcube_label($col)) . "</td>\n";
4e17e6 1179
T 1180   $table .= "</tr></thead>\n<tbody>\n";
1181   
1182   $c = 0;
d1d2c4 1183
S 1184   if (!is_array($table_data)) 
4e17e6 1185     {
d1d2c4 1186     while ($table_data && ($sql_arr = $DB->fetch_assoc($table_data)))
4e17e6 1187       {
d1d2c4 1188       $zebra_class = $c%2 ? 'even' : 'odd';
4e17e6 1189
d1d2c4 1190       $table .= sprintf('<tr id="rcmrow%d" class="contact '.$zebra_class.'">'."\n", $sql_arr[$id_col]);
S 1191
1192       // format each col
1193       foreach ($a_show_cols as $col)
1194         {
1195         $cont = rep_specialchars_output($sql_arr[$col]);
1196         $table .= '<td class="'.$col.'">' . $cont . "</td>\n";
1197         }
1198
1199       $table .= "</tr>\n";
1200       $c++;
1201       }
1202     }
1203   else 
1204     {
1205     foreach ($table_data as $row_data)
1206       {
1207       $zebra_class = $c%2 ? 'even' : 'odd';
1208
1209       $table .= sprintf('<tr id="rcmrow%d" class="contact '.$zebra_class.'">'."\n", $row_data[$id_col]);
1210
1211       // format each col
1212       foreach ($a_show_cols as $col)
1213         {
1214         $cont = rep_specialchars_output($row_data[$col]);
1215         $table .= '<td class="'.$col.'">' . $cont . "</td>\n";
1216         }
1217
1218       $table .= "</tr>\n";
1219       $c++;
1220       }
4e17e6 1221     }
T 1222
1223   // complete message table
1224   $table .= "</tbody></table>\n";
1225   
1226   return $table;
1227   }
1228
1229
1230
1231 function rcmail_get_edit_field($col, $value, $attrib, $type='text')
1232   {
1233   $fname = '_'.$col;
1234   $attrib['name'] = $fname;
1235   
1236   if ($type=='checkbox')
1237     {
1238     $attrib['value'] = '1';
1239     $input = new checkbox($attrib);
1240     }
1241   else if ($type=='textarea')
1242     {
1243     $attrib['cols'] = $attrib['size'];
1244     $input = new textarea($attrib);
1245     }
1246   else
1247     $input = new textfield($attrib);
1248
1249   // use value from post
597170 1250   if (!empty($_POST[$fname]))
4e17e6 1251     $value = $_POST[$fname];
T 1252
1253   $out = $input->show($value);
1254          
1255   return $out;
1256   }
1257
1258
1259 function create_attrib_string($attrib, $allowed_attribs=array('id', 'class', 'style'))
1260   {
1261   // allow the following attributes to be added to the <iframe> tag
1262   $attrib_str = '';
1263   foreach ($allowed_attribs as $a)
1264     if (isset($attrib[$a]))
1265       $attrib_str .= sprintf(' %s="%s"', $a, $attrib[$a]);
1266
1267   return $attrib_str;
1268   }
1269
1270
1271
1272 function format_date($date, $format=NULL)
1273   {
1274   global $CONFIG, $sess_user_lang;
1275   
1276   if (is_numeric($date))
1277     $ts = $date;
b076a4 1278   else if (!empty($date))
4e17e6 1279     $ts = strtotime($date);
b076a4 1280   else
T 1281     return '';
4e17e6 1282
T 1283   // convert time to user's timezone
1284   $timestamp = $ts - date('Z', $ts) + ($CONFIG['timezone'] * 3600);
1285   
1286   // get current timestamp in user's timezone
1287   $now = time();  // local time
1288   $now -= (int)date('Z'); // make GMT time
1289   $now += ($CONFIG['timezone'] * 3600); // user's time
749b07 1290   $now_date = getdate();
4e17e6 1291
749b07 1292   //$day_secs = 60*((int)date('H', $now)*60 + (int)date('i', $now));
T 1293   //$week_secs = 60 * 60 * 24 * 7;
1294   //$diff = $now - $timestamp;
1295   $today_limit = mktime(0, 0, 0, $now_date['mon'], $now_date['mday'], $now_date['year']);
1296   $week_limit = mktime(0, 0, 0, $now_date['mon'], $now_date['mday']-6, $now_date['year']);
4e17e6 1297
30233b 1298   // define date format depending on current time  
749b07 1299   if ($CONFIG['prettydate'] && !$format && $timestamp > $today_limit)
4e17e6 1300     return sprintf('%s %s', rcube_label('today'), date('H:i', $timestamp));
749b07 1301   else if ($CONFIG['prettydate'] && !$format && $timestamp > $week_limit)
4e17e6 1302     $format = $CONFIG['date_short'] ? $CONFIG['date_short'] : 'D H:i';
T 1303   else if (!$format)
1304     $format = $CONFIG['date_long'] ? $CONFIG['date_long'] : 'd.m.Y H:i';
1305
1306
1307   // parse format string manually in order to provide localized weekday and month names
1308   // an alternative would be to convert the date() format string to fit with strftime()
1309   $out = '';
1310   for($i=0; $i<strlen($format); $i++)
1311     {
1312     if ($format{$i}=='\\')  // skip escape chars
1313       continue;
1314     
1315     // write char "as-is"
1316     if ($format{$i}==' ' || $format{$i-1}=='\\')
1317       $out .= $format{$i};
1318     // weekday (short)
1319     else if ($format{$i}=='D')
1320       $out .= rcube_label(strtolower(date('D', $timestamp)));
1321     // weekday long
1322     else if ($format{$i}=='l')
1323       $out .= rcube_label(strtolower(date('l', $timestamp)));
1324     // month name (short)
1325     else if ($format{$i}=='M')
1326       $out .= rcube_label(strtolower(date('M', $timestamp)));
1327     // month name (long)
1328     else if ($format{$i}=='F')
1329       $out .= rcube_label(strtolower(date('F', $timestamp)));
1330     else
1331       $out .= date($format{$i}, $timestamp);
1332     }
1333   
1334   return $out;
1335   }
1336
1337
1338 // ************** functions delivering gui objects **************
1339
1340
1341
1342 function rcmail_message_container($attrib)
1343   {
1344   global $OUTPUT, $JS_OBJECT_NAME;
1345
1346   if (!$attrib['id'])
1347     $attrib['id'] = 'rcmMessageContainer';
1348
1349   // allow the following attributes to be added to the <table> tag
1350   $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id'));
1351   $out = '<div' . $attrib_str . "></div>";
1352   
1353   $OUTPUT->add_script("$JS_OBJECT_NAME.gui_object('message', '$attrib[id]');");
1354   
1355   return $out;
1356   }
1357
1358
15a9d1 1359 // return the IMAP username of the current session
T 1360 function rcmail_current_username($attrib)
1361   {
1362   global $DB;
1363   static $s_username;
1364
1365   // alread fetched  
1366   if (!empty($s_username))
1367     return $s_username;
1368
1369   // get e-mail address form default identity
1370   $sql_result = $DB->query("SELECT email AS mailto
1371                             FROM ".get_table_name('identities')."
1372                             WHERE  user_id=?
1373                             AND    standard=1
1374                             AND    del<>1",
1375                             $_SESSION['user_id']);
1376                                    
1377   if ($DB->num_rows($sql_result))
1378     {
1379     $sql_arr = $DB->fetch_assoc($sql_result);
1380     $s_username = $sql_arr['mailto'];
1381     }
1382   else if (strstr($_SESSION['username'], '@'))
1383     $s_username = $_SESSION['username'];
1384   else
1385     $s_username = $_SESSION['username'].'@'.$_SESSION['imap_host'];
1386
1387   return $s_username;
1388   }
1389
1390
4e17e6 1391 // return code for the webmail login form
T 1392 function rcmail_login_form($attrib)
1393   {
1394   global $CONFIG, $OUTPUT, $JS_OBJECT_NAME, $SESS_HIDDEN_FIELD;
1395   
1396   $labels = array();
1397   $labels['user'] = rcube_label('username');
1398   $labels['pass'] = rcube_label('password');
1399   $labels['host'] = rcube_label('server');
1400   
1401   $input_user = new textfield(array('name' => '_user', 'size' => 30));
1402   $input_pass = new passwordfield(array('name' => '_pass', 'size' => 30));
1403   $input_action = new hiddenfield(array('name' => '_action', 'value' => 'login'));
1404     
1405   $fields = array();
1406   $fields['user'] = $input_user->show($_POST['_user']);
1407   $fields['pass'] = $input_pass->show();
1408   $fields['action'] = $input_action->show();
1409   
1410   if (is_array($CONFIG['default_host']))
1411     {
1412     $select_host = new select(array('name' => '_host'));
42b113 1413     
T 1414     foreach ($CONFIG['default_host'] as $key => $value)
1415       $select_host->add($value, (is_numeric($key) ? $value : $key));
1416       
4e17e6 1417     $fields['host'] = $select_host->show($_POST['_host']);
T 1418     }
1419   else if (!strlen($CONFIG['default_host']))
1420     {
1421     $input_host = new textfield(array('name' => '_host', 'size' => 30));
1422     $fields['host'] = $input_host->show($_POST['_host']);
1423     }
1424
1425   $form_name = strlen($attrib['form']) ? $attrib['form'] : 'form';
1426   $form_start = !strlen($attrib['form']) ? '<form name="form" action="./" method="post">' : '';
1427   $form_end = !strlen($attrib['form']) ? '</form>' : '';
1428   
1429   if ($fields['host'])
1430     $form_host = <<<EOF
1431     
1432 </tr><tr>
1433
1434 <td class="title">$labels[host]</td>
1435 <td>$fields[host]</td>
1436
1437 EOF;
1438
1439   $OUTPUT->add_script("$JS_OBJECT_NAME.gui_object('loginform', '$form_name');");
1440   
1441   $out = <<<EOF
1442 $form_start
1443 $SESS_HIDDEN_FIELD
1444 $fields[action]
1445 <table><tr>
1446
1447 <td class="title">$labels[user]</td>
1448 <td>$fields[user]</td>
1449
1450 </tr><tr>
1451
1452 <td class="title">$labels[pass]</td>
1453 <td>$fields[pass]</td>
1454 $form_host
1455 </tr></table>
1456 $form_end
1457 EOF;
1458
1459   return $out;
1460   }
1461
1462
1cded8 1463
T 1464 function rcmail_charset_selector($attrib)
1465   {
1466   // pass the following attributes to the form class
1467   $field_attrib = array('name' => '_charset');
1468   foreach ($attrib as $attr => $value)
1469     if (in_array($attr, array('id', 'class', 'style', 'size', 'tabindex')))
1470       $field_attrib[$attr] = $value;
1471       
1472   $charsets = array(
1473     'US-ASCII'     => 'ASCII (English)',
1474     'X-EUC-JP'     => 'EUC-JP (Japanese)',
1475     'EUC-KR'       => 'EUC-KR (Korean)',
1476     'BIG5'         => 'BIG5 (Chinese)',
1477     'GB2312'       => 'GB2312 (Chinese)',
1478     'ISO-8859-1'   => 'ISO-8859-1 (Latin-1)',
1479     'ISO-8859-2'   => 'ISO-8895-2 (Central European)',
1480     'ISO-8859-7'   => 'ISO-8859-7 (Greek)',
1481     'ISO-8859-9'   => 'ISO-8859-9 (Turkish)',
1482     'Windows-1251' => 'Windows-1251 (Cyrillic)',
1483     'Windows-1252' => 'Windows-1252 (Western)',
1484     'Windows-1255' => 'Windows-1255 (Hebrew)',
1485     'Windows-1256' => 'Windows-1256 (Arabic)',
1486     'Windows-1257' => 'Windows-1257 (Baltic)',
1487     'UTF-8'        => 'UTF-8'
1488     );
1489
1490   $select = new select($field_attrib);
1491   $select->add(array_values($charsets), array_keys($charsets));
1492   
1493   $set = $_POST['_charset'] ? $_POST['_charset'] : $GLOBALS['CHARSET'];
1494   return $select->show($set);
1495   }
1496
1497
15a9d1 1498 function rcube_timer()
T 1499   {
1500   list($usec, $sec) = explode(" ", microtime());
1501   return ((float)$usec + (float)$sec);
1502   }
1503   
1504
1505 function rcube_print_time($timer, $label='Timer')
1506   {
1507   static $print_count = 0;
1508   
1509   $print_count++;
1510   $now = rcube_timer();
1511   $diff = $now-$timer;
1512   
1513   if (empty($label))
1514     $label = 'Timer '.$print_count;
1515   
1516   console(sprintf("%s: %0.4f sec", $label, $diff));
1517   }
1518
d1d2c4 1519 ?>