source: subversion/trunk/roundcubemail/program/include/main.inc @ 88

Last change on this file since 88 was 88, checked in by roundcube, 7 years ago

Re-design of caching (new database table added\!); some bugfixes; Postgres support

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