svncommit
2005-10-27 66773789e392305bba4cdf7ed8e6ae3b8380de51
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');
23
24
25 // register session and connect to server
26 function rcmail_startup($task='mail')
27   {
28   global $sess_id, $sess_auth, $sess_user_lang;
29   global $CONFIG, $INSTALL_PATH, $BROWSER, $OUTPUT, $_SESSION, $IMAP, $DB, $JS_OBJECT_NAME;
30
31   // check client
32   $BROWSER = rcube_browser();
de2e1e 33
4e17e6 34   // load config file
T 35   include_once('config/main.inc.php');
36   $CONFIG = is_array($rcmail_config) ? $rcmail_config : array();
37   $CONFIG['skin_path'] = $CONFIG['skin_path'] ? preg_replace('/\/$/', '', $CONFIG['skin_path']) : 'skins/default';
38
39   // load db conf
40   include_once('config/db.inc.php');
41   $CONFIG = array_merge($CONFIG, $rcmail_config);
42
43
44   // set PHP error logging according to config
45   if ($CONFIG['debug_level'] & 1)
46     {
47     ini_set('log_errors', 1);
48     ini_set('error_log', $INSTALL_PATH.'logs/errors');  
49     }
50   if ($CONFIG['debug_level'] & 4)
51     ini_set('display_errors', 1);
52   else
53     ini_set('display_errors', 0);
7902df 54   
T 55   // set session garbage collecting time according to session_lifetime
56   if (!empty($CONFIG['session_lifetime']))
57     ini_set('session.gc_maxlifetime', ($CONFIG['session_lifetime']+2)*60);
4e17e6 58
T 59
60   // prepare DB connection
f45ec7 61   require_once('include/rcube_'.(empty($CONFIG['db_backend']) ? 'db' : $CONFIG['db_backend']).'.inc');
S 62   
1676e1 63   $DB = new rcube_db($CONFIG['db_dsnw'], $CONFIG['db_dsnr']);
42b113 64   $DB->sqlite_initials = $INSTALL_PATH.'SQL/sqlite.initial.sql';
4e17e6 65
T 66   // we can use the database for storing session data
42b113 67   if (is_object($DB) && $DB->db_provider!='sqlite')
4e17e6 68     include_once('include/session.inc');
T 69
70
71   // init session
72   session_start();
73   $sess_id = session_id();
74   
75   // create session and set session vars
76   if (!$_SESSION['client_id'])
77     {
78     $_SESSION['client_id'] = $sess_id;
f3b659 79     $_SESSION['user_lang'] = substr($CONFIG['locale_string'], 0, 2);
4e17e6 80     $_SESSION['auth_time'] = mktime();
T 81     $_SESSION['auth'] = rcmail_auth_hash($sess_id, $_SESSION['auth_time']);
82     unset($GLOBALS['_auth']);
83     }
84
85   // set session vars global
86   $sess_auth = $_SESSION['auth'];
87   $sess_user_lang = $_SESSION['user_lang'];
88
89
90   // overwrite config with user preferences
91   if (is_array($_SESSION['user_prefs']))
92     $CONFIG = array_merge($CONFIG, $_SESSION['user_prefs']);
93
94
95   // reset some session parameters when changing task
96   if ($_SESSION['task'] != $task)
97     unset($_SESSION['page']);
98
99   // set current task to session
100   $_SESSION['task'] = $task;
101
102
103   // create IMAP object
104   if ($task=='mail')
105     rcmail_imap_init();
106
107
108   // set localization
109   if ($CONFIG['locale_string'])
110     setlocale(LC_ALL, $CONFIG['locale_string']);
111   else if ($sess_user_lang)
112     setlocale(LC_ALL, $sess_user_lang);
113
114
115   register_shutdown_function('rcmail_shutdown');
116   }
117   
118
119 // create authorization hash
120 function rcmail_auth_hash($sess_id, $ts)
121   {
122   global $CONFIG;
123   
124   $auth_string = sprintf('rcmail*sess%sR%s*Chk:%s;%s',
125                          $sess_id,
126                          $ts,
127                          $CONFIG['ip_check'] ? $_SERVER['REMOTE_ADDR'] : '***.***.***.***',
128                          $_SERVER['HTTP_USER_AGENT']);
129   
130   if (function_exists('sha1'))
131     return sha1($auth_string);
132   else
133     return md5($auth_string);
134   }
135
136
137
138 // create IMAP object and connect to server
139 function rcmail_imap_init($connect=FALSE)
140   {
141   global $CONFIG, $IMAP;
6dc026 142
4e17e6 143   $IMAP = new rcube_imap();
T 144
7902df 145   // connect with stored session data
T 146   if ($connect)
147     {
148     if (!($conn = $IMAP->connect($_SESSION['imap_host'], $_SESSION['username'], decrypt_passwd($_SESSION['password']), $_SESSION['imap_port'], $_SESSION['imap_ssl'])))
149       show_message('imaperror', 'error');
150       
151     rcmail_set_imap_prop();
152     }
153
6dc026 154   // enable caching of imap data
T 155   if ($CONFIG['enable_caching']===TRUE)
156     $IMAP->set_caching(TRUE);
157
4e17e6 158   if (is_array($CONFIG['default_imap_folders']))
T 159     $IMAP->set_default_mailboxes($CONFIG['default_imap_folders']);
160
161   // set pagesize from config
162   if (isset($CONFIG['pagesize']))
163     $IMAP->set_pagesize($CONFIG['pagesize']);
7902df 164   }
4e17e6 165
T 166
7902df 167 // set root dir and last stored mailbox
T 168 // this must be done AFTER connecting to the server
169 function rcmail_set_imap_prop()
170   {
171   global $CONFIG, $IMAP;
172
173   // set root dir from config
174   if (strlen($CONFIG['imap_root']))
175     $IMAP->set_rootdir($CONFIG['imap_root']);
176
177   if (strlen($_SESSION['mbox']))
178     $IMAP->set_mailbox($_SESSION['mbox']);
179     
180   if (isset($_SESSION['page']))
181     $IMAP->set_page($_SESSION['page']);
4e17e6 182   }
T 183
184
185 // do these things on script shutdown
186 function rcmail_shutdown()
187   {
188   global $IMAP;
189   
190   if (is_object($IMAP))
191     {
192     $IMAP->close();
193     $IMAP->write_cache();
194     }
195   }
196
197
198 // destroy session data and remove cookie
199 function rcmail_kill_session()
200   {
201 /* $sess_name = session_name();
202   if (isset($_COOKIE[$sess_name]))
203    setcookie($sess_name, '', time()-42000, '/');
204 */
205   $_SESSION = array();
206   session_destroy();
207   }
208
209
210 // return correct name for a specific database table
211 function get_table_name($table)
212   {
213   global $CONFIG;
214   
215   // return table name if configured
216   $config_key = 'db_table_'.$table;
217
218   if (strlen($CONFIG[$config_key]))
219     return $CONFIG[$config_key];
220   
221   return $table;
222   }
223
224
225
226 // init output object for GUI and add common scripts
227 function load_gui()
228   {
229   global $CONFIG, $OUTPUT, $COMM_PATH, $IMAP, $JS_OBJECT_NAME;
230
231   // init output page
232   $OUTPUT = new rcube_html_page();
233   
234   // add common javascripts
235   $javascript = "var $JS_OBJECT_NAME = new rcube_webmail();\n";
236   $javascript .= "$JS_OBJECT_NAME.set_env('comm_path', '$COMM_PATH');\n";
237
597170 238   if (!empty($GLOBALS['_framed']))
4e17e6 239     $javascript .= "$JS_OBJECT_NAME.set_env('framed', true);\n";
T 240
241   $OUTPUT->add_script($javascript);
242   $OUTPUT->include_script('program/js/common.js');
243   $OUTPUT->include_script('program/js/app.js');  
244   }  
245
246
247 // perfom login to the IMAP server and to the webmail service
248 function rcmail_login($user, $pass, $host=NULL)
249   {
250   global $CONFIG, $IMAP, $DB, $sess_user_lang;
42b113 251   $user_id = NULL;
4e17e6 252   
T 253   if (!$host)
254     $host = $CONFIG['default_host'];
255
f619de 256   // parse $host URL
T 257   $a_host = parse_url($host);
258   if ($a_host['host'])
259     {
260     $host = $a_host['host'];
261     $imap_ssl = (isset($a_host['scheme']) && in_array($a_host['scheme'], array('ssl','imaps','tls'))) ? TRUE : FALSE;
262     $imap_port = isset($a_host['port']) ? $a_host['port'] : ($imap_ssl ? 993 : $CONFIG['default_port']);
263     }
264
4e17e6 265   // query if user already registered
d7cb77 266   $sql_result = $DB->query("SELECT user_id, username, language, preferences
S 267                             FROM ".get_table_name('users')."
268                             WHERE  mail_host=? AND (username=? OR alias=?)",
269                             $host,
270                             $user,
271                             $user);
4e17e6 272
42b113 273   // user already registered -> overwrite username
4e17e6 274   if ($sql_arr = $DB->fetch_assoc($sql_result))
T 275     {
276     $user_id = $sql_arr['user_id'];
42b113 277     $user = $sql_arr['username'];
T 278     }
279
280   // exit if IMAP login failed
281   if (!($imap_login  = $IMAP->connect($host, $user, $pass, $imap_port, $imap_ssl)))
282     return FALSE;
283
284   // user already registered
285   if ($user_id && !empty($sql_arr))
286     {
4e17e6 287     // get user prefs
T 288     if (strlen($sql_arr['preferences']))
289       {
290       $user_prefs = unserialize($sql_arr['preferences']);
291       $_SESSION['user_prefs'] = $user_prefs;
292       array_merge($CONFIG, $user_prefs);
293       }
294
f3b659 295
4e17e6 296     // set user specific language
T 297     if (strlen($sql_arr['language']))
298       $sess_user_lang = $_SESSION['user_lang'] = $sql_arr['language'];
f3b659 299       
4e17e6 300     // update user's record
d7cb77 301     $DB->query("UPDATE ".get_table_name('users')."
667737 302                 SET    last_login=now()
d7cb77 303                 WHERE  user_id=?",
S 304                 $user_id);
4e17e6 305     }
T 306   // create new system user
307   else if ($CONFIG['auto_create_user'])
308     {
309     $user_id = rcmail_create_user($user, $host);
310     }
311
312   if ($user_id)
313     {
314     $_SESSION['user_id']   = $user_id;
315     $_SESSION['imap_host'] = $host;
7902df 316     $_SESSION['imap_port'] = $imap_port;
T 317     $_SESSION['imap_ssl']  = $imap_ssl;
4e17e6 318     $_SESSION['username']  = $user;
f3b659 319     $_SESSION['user_lang'] = $sess_user_lang;
4e17e6 320     $_SESSION['password']  = encrypt_passwd($pass);
T 321
322     // force reloading complete list of subscribed mailboxes    
323     $IMAP->clear_cache('mailboxes');
324
325     return TRUE;
326     }
327
328   return FALSE;
329   }
330
331
332 // create new entry in users and identities table
333 function rcmail_create_user($user, $host)
334   {
335   global $DB, $CONFIG, $IMAP;
f3b659 336   
d7cb77 337   $DB->query("INSERT INTO ".get_table_name('users')."
S 338               (created, last_login, username, mail_host, language)
667737 339               VALUES (now(), now(), ?, ?, ?)",
d7cb77 340               $user,
S 341               $host,
342               $_SESSION['user_lang']);
4e17e6 343
d7cb77 344   if ($user_id = $DB->insert_id('user_ids'))
4e17e6 345     {
52c1f2 346     $user_email = strstr($user, '@') ? $user : sprintf('%s@%s', $user, $host);
T 347     $user_name = $user!=$user_email ? $user : '';
348     
4e17e6 349     // also create a new identity record
d7cb77 350     $DB->query("INSERT INTO ".get_table_name('identities')."
S 351                 (user_id, `default`, name, email)
352                 VALUES (?, '1', ?, ?)",
353                 $user_id,
354                 $user_name,
355                 $user_email);
4e17e6 356                        
T 357     // get existing mailboxes
358     $a_mailboxes = $IMAP->list_mailboxes();
359     
360     // check if the configured mailbox for sent messages exists
361     if ($CONFIG['sent_mbox'] && !in_array_nocase($CONFIG['sent_mbox'], $a_mailboxes))
362       $IMAP->create_mailbox($CONFIG['sent_mbox'], TRUE);
363
364     // check if the configured mailbox for sent messages exists
365     if ($CONFIG['trash_mbox'] && !in_array_nocase($CONFIG['trash_mbox'], $a_mailboxes))
366       $IMAP->create_mailbox($CONFIG['trash_mbox'], TRUE);
42b113 367     }
T 368   else
369     {
370     raise_error(array('code' => 500,
371                       'type' => 'php',
372                       'line' => __LINE__,
373                       'file' => __FILE__,
374                       'message' => "Failed to create new user"), TRUE, FALSE);
4e17e6 375     }
T 376     
377   return $user_id;
378   }
379
380
381 function show_message($message, $type='notice')
382   {
383   global $OUTPUT, $JS_OBJECT_NAME, $REMOTE_REQUEST;
384
597170 385   $framed = $GLOBALS['_framed'];
4e17e6 386   $command = sprintf("display_message('%s', '%s');",
T 387                      addslashes(rep_specialchars_output(rcube_label($message))),
388                      $type);
389                      
390   if ($REMOTE_REQUEST)
391     return 'this.'.$command;
392   
393   else
394     $OUTPUT->add_script(sprintf("%s%s.%s",
395                                 $framed ? sprintf('if(parent.%s)parent.', $JS_OBJECT_NAME) : '',
396                                 $JS_OBJECT_NAME,
397                                 $command));
398   
399   // console(rcube_label($message));
400   }
401
402
403 function console($msg, $type=1)
404   {
520c36 405   if ($GLOBALS['REMOTE_REQUEST'])
T 406     print "// $msg\n";
407   else
408     {
409     print $msg;
410     print "\n<hr>\n";
411     }
4e17e6 412   }
T 413
414
415 function encrypt_passwd($pass)
416   {
417   $cypher = des('rcmail?24BitPwDkeyF**ECB', $pass, 1, 0, NULL);
418   return base64_encode($cypher);
419   }
420
421
422 function decrypt_passwd($cypher)
423   {
424   $pass = des('rcmail?24BitPwDkeyF**ECB', base64_decode($cypher), 0, 0, NULL);
425   return trim($pass);
426   }
427
428
429 // send correct response on a remote request
430 function rcube_remote_response($js_code)
431   {
432   send_nocacheing_headers();
433   //header('Content-Type: text/javascript');
434   header('Content-Type: application/x-javascript');
435
436   print '/** remote response ['.date('d/M/Y h:i:s O')."] **/\n";
437   print $js_code;
438   exit;
439   }
440
441
9fee0e 442 // read directory program/localization/ and return a list of available languages
T 443 function rcube_list_languages()
444   {
445   global $CONFIG, $INSTALL_PATH;
446   static $sa_languages = array();
447
448   if (!sizeof($sa_languages))
449     {
450     @include_once($INSTLL_PATH.'program/localization/index.inc');
451
452     if ($dh = @opendir($INSTLL_PATH.'program/localization'))
453       {
454       while (($name = readdir($dh)) !== false)
455         {
456         if ($name{0}=='.' || !is_dir($INSTLL_PATH.'program/localization/'.$name))
457           continue;
458
459         if ($label = $rcube_languages[$name])
460           $sa_languages[$name] = $label ? $label : $name;
461         }
462       closedir($dh);
463       }
464     }
465
466   return $sa_languages;
467   }
468
4e17e6 469
T 470
471 // ************** template parsing and gui functions **************
472
473
474 // return boolean if a specific template exists
475 function template_exists($name)
476   {
477   global $CONFIG, $OUTPUT;
478   $skin_path = $CONFIG['skin_path'];
479
480   // check template file
481   return is_file("$skin_path/templates/$name.html");
482   }
483
484
485 // get page template an replace variable
486 // similar function as used in nexImage
487 function parse_template($name='main', $exit=TRUE)
488   {
489   global $CONFIG, $OUTPUT;
490   $skin_path = $CONFIG['skin_path'];
491
492   // read template file
493   $templ = '';
494   $path = "$skin_path/templates/$name.html";
495
496   if($fp = @fopen($path, 'r'))
497     {
498     $templ = fread($fp, filesize($path));
499     fclose($fp);
500     }
501   else
502     {
503     raise_error(array('code' => 500,
504                       'type' => 'php',
505                       'line' => __LINE__,
506                       'file' => __FILE__,
507                       'message' => "Error loading template for '$name'"), TRUE, TRUE);
508     return FALSE;
509     }
510
511
512   // parse for specialtags
513   $output = parse_rcube_xml($templ);
514   
515   $OUTPUT->write(trim(parse_with_globals($output)), $skin_path);
516
517   if ($exit)
518     exit;
519   }
520
521
522
523 // replace all strings ($varname) with the content of the according global variable
524 function parse_with_globals($input)
525   {
526   $output = preg_replace('/\$(__[a-z0-9_\-]+)/e', '$GLOBALS["\\1"]', $input);
527   return $output;
528   }
529
530
531
532 function parse_rcube_xml($input)
533   {
534   $output = preg_replace('/<roundcube:([-_a-z]+)\s+([^>]+)>/Uie', "rcube_xml_command('\\1', '\\2')", $input);
535   return $output;
536   }
537
538
539 function rcube_xml_command($command, $str_attrib, $a_attrib=NULL)
540   {
541   global $IMAP, $CONFIG;
542   
543   $attrib = array();
544   $command = strtolower($command);
545
546   preg_match_all('/\s*([-_a-z]+)=["]([^"]+)["]?/i', stripslashes($str_attrib), $regs, PREG_SET_ORDER);
547
548   // convert attributes to an associative array (name => value)
549   if ($regs)
550     foreach ($regs as $attr)
551       $attrib[strtolower($attr[1])] = $attr[2];
552   else if ($a_attrib)
553     $attrib = $a_attrib;
554
555   // execute command
556   switch ($command)
557     {
558     // return a button
559     case 'button':
560       if ($attrib['command'])
561         return rcube_button($attrib);
562       break;
563
564     // show a label
565     case 'label':
566       if ($attrib['name'] || $attrib['command'])
7dd801 567         return rep_specialchars_output(rcube_label($attrib));
4e17e6 568       break;
T 569
570     // create a menu item
571     case 'menu':
572       if ($attrib['command'] && $attrib['group'])
573         rcube_menu($attrib);
574       break;
575
576     // include a file 
577     case 'include':
578       $path = realpath($CONFIG['skin_path'].$attrib['file']);
579       
580       if($fp = @fopen($path, 'r'))
581         {
582         $incl = fread($fp, filesize($path));
583         fclose($fp);        
584         return parse_rcube_xml($incl);
585         }
586       break;
587
588     // return code for a specific application object
589     case 'object':
590       $object = strtolower($attrib['name']);
591
592       if ($object=='loginform')
593         return rcmail_login_form($attrib);
594
595       else if ($object=='message')
596         return rcmail_message_container($attrib);
597
598       // MAIL
599       else if ($object=='mailboxlist' && function_exists('rcmail_mailbox_list'))
600         return rcmail_mailbox_list($attrib);
601         
602       else if ($object=='messages' && function_exists('rcmail_message_list'))
603         return rcmail_message_list($attrib);
604
605       else if ($object=='messagecountdisplay' && function_exists('rcmail_messagecount_display'))
606         return rcmail_messagecount_display($attrib);
607
608       else if ($object=='messageheaders' && function_exists('rcmail_message_headers'))
609         return rcmail_message_headers($attrib);
610
611       else if ($object=='messageattachments' && function_exists('rcmail_message_attachments'))
612         return rcmail_message_attachments($attrib);
613
614       else if ($object=='messagebody' && function_exists('rcmail_message_body'))
615         return rcmail_message_body($attrib);
616         
617       else if ($object=='blockedobjects' && function_exists('rcmail_remote_objects_msg'))
618         return rcmail_remote_objects_msg($attrib);
619
620       else if ($object=='messagecontentframe' && function_exists('rcmail_messagecontent_frame'))
621         return rcmail_messagecontent_frame($attrib);
622
623       else if ($object=='messagepartframe' && function_exists('rcmail_message_part_frame'))
624         return rcmail_message_part_frame($attrib);
625
626       else if ($object=='messagepartcontrols' && function_exists('rcmail_message_part_controls'))
627         return rcmail_message_part_controls($attrib);
628
629       else if ($object=='composeheaders' && function_exists('rcmail_compose_headers'))
630         return rcmail_compose_headers($attrib);
631
632       else if ($object=='composesubject' && function_exists('rcmail_compose_subject'))
633         return rcmail_compose_subject($attrib);
634
635       else if ($object=='composebody' && function_exists('rcmail_compose_body'))
636         return rcmail_compose_body($attrib);
637
638       else if ($object=='composeattachmentlist' && function_exists('rcmail_compose_attachment_list'))
639         return rcmail_compose_attachment_list($attrib);
640
641       else if ($object=='composeattachmentform' && function_exists('rcmail_compose_attachment_form'))
642         return rcmail_compose_attachment_form($attrib);
643
644       else if ($object=='composeattachment' && function_exists('rcmail_compose_attachment_field'))
645         return rcmail_compose_attachment_field($attrib);
646
647       else if ($object=='priorityselector' && function_exists('rcmail_priority_selector'))
648         return rcmail_priority_selector($attrib);
649         
650       else if ($object=='priorityselector' && function_exists('rcmail_priority_selector'))
651         return rcmail_priority_selector($attrib);
652
653
654       // ADDRESS BOOK
655       else if ($object=='addresslist' && function_exists('rcmail_contacts_list'))
656         return rcmail_contacts_list($attrib);
657
658       else if ($object=='addressframe' && function_exists('rcmail_contact_frame'))
659         return rcmail_contact_frame($attrib);
660
661       else if ($object=='recordscountdisplay' && function_exists('rcmail_rowcount_display'))
662         return rcmail_rowcount_display($attrib);
663         
664       else if ($object=='contactdetails' && function_exists('rcmail_contact_details'))
665         return rcmail_contact_details($attrib);
666
667       else if ($object=='contacteditform' && function_exists('rcmail_contact_editform'))
668         return rcmail_contact_editform($attrib);
669
670
671       // USER SETTINGS
672       else if ($object=='userprefs' && function_exists('rcmail_user_prefs_form'))
673         return rcmail_user_prefs_form($attrib);
674
675       else if ($object=='itentitieslist' && function_exists('rcmail_identities_list'))
676         return rcmail_identities_list($attrib);
677
678       else if ($object=='identityframe' && function_exists('rcmail_identity_frame'))
679         return rcmail_identity_frame($attrib);
680
681       else if ($object=='identityform' && function_exists('rcube_identity_form'))
682         return rcube_identity_form($attrib);
683
684       else if ($object=='foldersubscription' && function_exists('rcube_subscription_form'))
685         return rcube_subscription_form($attrib);
686
687       else if ($object=='createfolder' && function_exists('rcube_create_folder_form'))
688         return rcube_create_folder_form($attrib);
689
690
691       else if ($object=='pagetitle')
692         {
693         $task = $GLOBALS['_task'];
694         if ($task=='mail' && isset($GLOBALS['MESSAGE']['subject']))
695           return rep_specialchars_output("RoundCube|Mail :: ".$GLOBALS['MESSAGE']['subject']);
696         else if (isset($GLOBALS['PAGE_TITLE']))
697           return rep_specialchars_output("RoundCube|Mail :: ".$GLOBALS['PAGE_TITLE']);
698         else if ($task=='mail' && ($mbox_name = $IMAP->get_mailbox_name()))
a95e0e 699           return "RoundCube|Mail :: ".rep_specialchars_output(UTF7DecodeString($mbox_name), 'html', 'all');
4e17e6 700         else
T 701           return "RoundCube|Mail :: $task";
702         }
703
704       else if ($object=='about')
705         return '';
706
707       break;
708     }
709
710   return '';
711   }
712
713
714 // create and register a button
715 function rcube_button($attrib)
716   {
14eafe 717   global $CONFIG, $OUTPUT, $JS_OBJECT_NAME, $BROWSER;
4e17e6 718   static $sa_buttons = array();
T 719   static $s_button_count = 100;
720   
721   $skin_path = $CONFIG['skin_path'];
722   
723   if (!($attrib['command'] || $attrib['name']))
724     return '';
725
726   // try to find out the button type
727   if ($attrib['type'])
728     $attrib['type'] = strtolower($attrib['type']);
729   else
730     $attrib['type'] = ($attrib['image'] || $attrib['imagepas'] || $arg['imagect']) ? 'image' : 'link';
731   
732   
733   $command = $attrib['command'];
734   
735   // take the button from the stack
736   if($attrib['name'] && $sa_buttons[$attrib['name']])
737     $attrib = $sa_buttons[$attrib['name']];
738
739   // add button to button stack
740   else if($attrib['image'] || $arg['imagect'] || $attrib['imagepas'] || $attrib['class'])
741     {
742     if(!$attrib['name'])
743       $attrib['name'] = $command;
744
745     if (!$attrib['image'])
746       $attrib['image'] = $attrib['imagepas'] ? $attrib['imagepas'] : $attrib['imageact'];
747
748     $sa_buttons[$attrib['name']] = $attrib;
749     }
750
751   // get saved button for this command/name
752   else if ($command && $sa_buttons[$command])
753     $attrib = $sa_buttons[$command];
754
755   //else
756   //  return '';
757
758
759   // set border to 0 because of the link arround the button
760   if ($attrib['type']=='image' && !isset($attrib['border']))
761     $attrib['border'] = 0;
762     
763   if (!$attrib['id'])
764     $attrib['id'] =  sprintf('rcmbtn%d', $s_button_count++);
765
766   // get localized text for labels and titles
767   if ($attrib['title'])
768     $attrib['title'] = rep_specialchars_output(rcube_label($attrib['title']));
769   if ($attrib['label'])
770     $attrib['label'] = rep_specialchars_output(rcube_label($attrib['label']));
771
772   if ($attrib['alt'])
773     $attrib['alt'] = rep_specialchars_output(rcube_label($attrib['alt']));
14eafe 774
T 775   // set title to alt attribute for IE browsers
776   if ($BROWSER['ie'] && $attrib['title'] && !$attrib['alt'])
777     {
778     $attrib['alt'] = $attrib['title'];
779     unset($attrib['title']);
780     }
781
4e17e6 782   // add empty alt attribute for XHTML compatibility
T 783   if (!isset($attrib['alt']))
784     $attrib['alt'] = '';
785
786
787   // register button in the system
788   if ($attrib['command'])
789     $OUTPUT->add_script(sprintf("%s.register_button('%s', '%s', '%s', '%s', '%s', '%s');",
790                                 $JS_OBJECT_NAME,
791                                 $command,
792                                 $attrib['id'],
793                                 $attrib['type'],
794                                 $attrib['imageact'] ? $skin_path.$attrib['imageact'] : $attrib['classact'],
795                                 $attirb['imagesel'] ? $skin_path.$attirb['imagesel'] : $attrib['classsel'],
796                                 $attrib['imageover'] ? $skin_path.$attrib['imageover'] : ''));
797
798   // overwrite attributes
799   if (!$attrib['href'])
800     $attrib['href'] = '#';
801
802   if ($command)
803     $attrib['onclick'] = sprintf("return %s.command('%s','%s',this)", $JS_OBJECT_NAME, $command, $attrib['prop']);
804     
805   if ($command && $attrib['imageover'])
806     {
807     $attrib['onmouseover'] = sprintf("return %s.button_over('%s','%s')", $JS_OBJECT_NAME, $command, $attrib['id']);
808     $attrib['onmouseout'] = sprintf("return %s.button_out('%s','%s')", $JS_OBJECT_NAME, $command, $attrib['id']);
809     }
810
811
812   $out = '';
813
814   // generate image tag
815   if ($attrib['type']=='image')
816     {
817     $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id', 'width', 'height', 'border', 'hspace', 'vspace', 'alt'));
818     $img_tag = sprintf('<img src="%%s"%s />', $attrib_str);
819     $btn_content = sprintf($img_tag, $skin_path.$attrib['image']);
820     if ($attrib['label'])
821       $btn_content .= ' '.$attrib['label'];
822     
823     $link_attrib = array('href', 'onclick', 'onmouseover', 'onmouseout', 'title');
824     }
825   else if ($attrib['type']=='link')
826     {
827     $btn_content = $attrib['label'] ? $attrib['label'] : $attrib['command'];
828     $link_attrib = array('href', 'onclick', 'title', 'id', 'class', 'style');
829     }
830   else if ($attrib['type']=='input')
831     {
832     $attrib['type'] = 'button';
833     
834     if ($attrib['label'])
835       $attrib['value'] = $attrib['label'];
836       
837     $attrib_str = create_attrib_string($attrib, array('type', 'value', 'onclick', 'id', 'class', 'style'));
838     $out = sprintf('<input%s disabled />', $attrib_str);
839     }
840
841   // generate html code for button
842   if ($btn_content)
843     {
844     $attrib_str = create_attrib_string($attrib, $link_attrib);
845     $out = sprintf('<a%s>%s</a>', $attrib_str, $btn_content);
846     }
847
848   return $out;
849   }
850
851
852 function rcube_menu($attrib)
853   {
854   
855   return '';
856   }
857
858
859
860 function rcube_table_output($attrib, $sql_result, $a_show_cols, $id_col)
861   {
862   global $DB;
863   
864   // allow the following attributes to be added to the <table> tag
865   $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id', 'cellpadding', 'cellspacing', 'border', 'summary'));
866   
867   $table = '<table' . $attrib_str . ">\n";
868     
869   // add table title
870   $table .= "<thead><tr>\n";
871
872   foreach ($a_show_cols as $col)
1038d5 873     $table .= '<td class="'.$col.'">' . rep_specialchars_output(rcube_label($col)) . "</td>\n";
4e17e6 874
T 875   $table .= "</tr></thead>\n<tbody>\n";
876   
877   $c = 0;
878   while ($sql_result && ($sql_arr = $DB->fetch_assoc($sql_result)))
879     {
880     $zebra_class = $c%2 ? 'even' : 'odd';
881
882     $table .= sprintf('<tr id="rcmrow%d" class="contact '.$zebra_class.'">'."\n", $sql_arr[$id_col]);
883
884     // format each col
885     foreach ($a_show_cols as $col)
886       {
887       $cont = rep_specialchars_output($sql_arr[$col]);
888       $table .= '<td class="'.$col.'">' . $cont . "</td>\n";
889       }
890
891     $table .= "</tr>\n";
892     $c++;
893     }
894
895   // complete message table
896   $table .= "</tbody></table>\n";
897   
898   return $table;
899   }
900
901
902
903 function rcmail_get_edit_field($col, $value, $attrib, $type='text')
904   {
905   $fname = '_'.$col;
906   $attrib['name'] = $fname;
907   
908   if ($type=='checkbox')
909     {
910     $attrib['value'] = '1';
911     $input = new checkbox($attrib);
912     }
913   else if ($type=='textarea')
914     {
915     $attrib['cols'] = $attrib['size'];
916     $input = new textarea($attrib);
917     }
918   else
919     $input = new textfield($attrib);
920
921   // use value from post
597170 922   if (!empty($_POST[$fname]))
4e17e6 923     $value = $_POST[$fname];
T 924
925   $out = $input->show($value);
926          
927   return $out;
928   }
929
930
931 function create_attrib_string($attrib, $allowed_attribs=array('id', 'class', 'style'))
932   {
933   // allow the following attributes to be added to the <iframe> tag
934   $attrib_str = '';
935   foreach ($allowed_attribs as $a)
936     if (isset($attrib[$a]))
937       $attrib_str .= sprintf(' %s="%s"', $a, $attrib[$a]);
938
939   return $attrib_str;
940   }
941
942
943
944 function format_date($date, $format=NULL)
945   {
946   global $CONFIG, $sess_user_lang;
947   
948   if (is_numeric($date))
949     $ts = $date;
b076a4 950   else if (!empty($date))
4e17e6 951     $ts = strtotime($date);
b076a4 952   else
T 953     return '';
4e17e6 954
T 955   // convert time to user's timezone
956   $timestamp = $ts - date('Z', $ts) + ($CONFIG['timezone'] * 3600);
957   
958   // get current timestamp in user's timezone
959   $now = time();  // local time
960   $now -= (int)date('Z'); // make GMT time
961   $now += ($CONFIG['timezone'] * 3600); // user's time
962
963   $day_secs = 60*((int)date('H', $now)*60 + (int)date('i', $now));
964   $week_secs = 60 * 60 * 24 * 7;
965   $diff = $now - $timestamp;
966
30233b 967   // define date format depending on current time  
T 968   if ($CONFIG['prettydate'] && !$format && $diff < $day_secs)
4e17e6 969     return sprintf('%s %s', rcube_label('today'), date('H:i', $timestamp));
30233b 970   else if ($CONFIG['prettydate'] && !$format && $diff < $week_secs)
4e17e6 971     $format = $CONFIG['date_short'] ? $CONFIG['date_short'] : 'D H:i';
T 972   else if (!$format)
973     $format = $CONFIG['date_long'] ? $CONFIG['date_long'] : 'd.m.Y H:i';
974
975
976   // parse format string manually in order to provide localized weekday and month names
977   // an alternative would be to convert the date() format string to fit with strftime()
978   $out = '';
979   for($i=0; $i<strlen($format); $i++)
980     {
981     if ($format{$i}=='\\')  // skip escape chars
982       continue;
983     
984     // write char "as-is"
985     if ($format{$i}==' ' || $format{$i-1}=='\\')
986       $out .= $format{$i};
987     // weekday (short)
988     else if ($format{$i}=='D')
989       $out .= rcube_label(strtolower(date('D', $timestamp)));
990     // weekday long
991     else if ($format{$i}=='l')
992       $out .= rcube_label(strtolower(date('l', $timestamp)));
993     // month name (short)
994     else if ($format{$i}=='M')
995       $out .= rcube_label(strtolower(date('M', $timestamp)));
996     // month name (long)
997     else if ($format{$i}=='F')
998       $out .= rcube_label(strtolower(date('F', $timestamp)));
999     else
1000       $out .= date($format{$i}, $timestamp);
1001     }
1002   
1003   return $out;
1004   }
1005
1006
1007 // ************** functions delivering gui objects **************
1008
1009
1010
1011 function rcmail_message_container($attrib)
1012   {
1013   global $OUTPUT, $JS_OBJECT_NAME;
1014
1015   if (!$attrib['id'])
1016     $attrib['id'] = 'rcmMessageContainer';
1017
1018   // allow the following attributes to be added to the <table> tag
1019   $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id'));
1020   $out = '<div' . $attrib_str . "></div>";
1021   
1022   $OUTPUT->add_script("$JS_OBJECT_NAME.gui_object('message', '$attrib[id]');");
1023   
1024   return $out;
1025   }
1026
1027
1028 // return code for the webmail login form
1029 function rcmail_login_form($attrib)
1030   {
1031   global $CONFIG, $OUTPUT, $JS_OBJECT_NAME, $SESS_HIDDEN_FIELD;
1032   
1033   $labels = array();
1034   $labels['user'] = rcube_label('username');
1035   $labels['pass'] = rcube_label('password');
1036   $labels['host'] = rcube_label('server');
1037   
1038   $input_user = new textfield(array('name' => '_user', 'size' => 30));
1039   $input_pass = new passwordfield(array('name' => '_pass', 'size' => 30));
1040   $input_action = new hiddenfield(array('name' => '_action', 'value' => 'login'));
1041     
1042   $fields = array();
1043   $fields['user'] = $input_user->show($_POST['_user']);
1044   $fields['pass'] = $input_pass->show();
1045   $fields['action'] = $input_action->show();
1046   
1047   if (is_array($CONFIG['default_host']))
1048     {
1049     $select_host = new select(array('name' => '_host'));
42b113 1050     
T 1051     foreach ($CONFIG['default_host'] as $key => $value)
1052       $select_host->add($value, (is_numeric($key) ? $value : $key));
1053       
4e17e6 1054     $fields['host'] = $select_host->show($_POST['_host']);
T 1055     }
1056   else if (!strlen($CONFIG['default_host']))
1057     {
1058     $input_host = new textfield(array('name' => '_host', 'size' => 30));
1059     $fields['host'] = $input_host->show($_POST['_host']);
1060     }
1061
1062   $form_name = strlen($attrib['form']) ? $attrib['form'] : 'form';
1063   $form_start = !strlen($attrib['form']) ? '<form name="form" action="./" method="post">' : '';
1064   $form_end = !strlen($attrib['form']) ? '</form>' : '';
1065   
1066   if ($fields['host'])
1067     $form_host = <<<EOF
1068     
1069 </tr><tr>
1070
1071 <td class="title">$labels[host]</td>
1072 <td>$fields[host]</td>
1073
1074 EOF;
1075
1076   $OUTPUT->add_script("$JS_OBJECT_NAME.gui_object('loginform', '$form_name');");
1077   
1078   $out = <<<EOF
1079 $form_start
1080 $SESS_HIDDEN_FIELD
1081 $fields[action]
1082 <table><tr>
1083
1084 <td class="title">$labels[user]</td>
1085 <td>$fields[user]</td>
1086
1087 </tr><tr>
1088
1089 <td class="title">$labels[pass]</td>
1090 <td>$fields[pass]</td>
1091 $form_host
1092 </tr></table>
1093 $form_end
1094 EOF;
1095
1096   return $out;
1097   }
1098
1099
1100 ?>