source: github/program/include/main.inc @ d7cb774

HEADcourier-fixdev-browser-capabilitiespdorelease-0.6release-0.7release-0.8
Last change on this file since d7cb774 was d7cb774, checked in by svncommit <devs@…>, 8 years ago

more pear/mdb2 integration

  • Property mode set to 100644
File size: 31.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
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);
54 
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);
58
59
60  // prepare DB connection
61  require_once('include/rcube_'.(empty($CONFIG['db_backend']) ? 'db' : $CONFIG['db_backend']).'.inc');
62 
63  $DB = new rcube_db($CONFIG['db_dsnw'], $CONFIG['db_dsnr']);
64  $DB->sqlite_initials = $INSTALL_PATH.'SQL/sqlite.initial.sql';
65
66  // we can use the database for storing session data
67  if (is_object($DB) && $DB->db_provider!='sqlite')
68    include_once('include/session.inc');
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;
79    $_SESSION['user_lang'] = substr($CONFIG['locale_string'], 0, 2);
80    $_SESSION['auth_time'] = mktime();
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
120function 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
139function rcmail_imap_init($connect=FALSE)
140  {
141  global $CONFIG, $IMAP;
142
143  $IMAP = new rcube_imap();
144
145  // connect with stored session data
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
154  // enable caching of imap data
155  if ($CONFIG['enable_caching']===TRUE)
156    $IMAP->set_caching(TRUE);
157
158  if (is_array($CONFIG['default_imap_folders']))
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']);
164  }
165
166
167// set root dir and last stored mailbox
168// this must be done AFTER connecting to the server
169function 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']);
182  }
183
184
185// do these things on script shutdown
186function 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
199function 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
211function 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
227function 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
238  if (!empty($GLOBALS['_framed']))
239    $javascript .= "$JS_OBJECT_NAME.set_env('framed', true);\n";
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
248function rcmail_login($user, $pass, $host=NULL)
249  {
250  global $CONFIG, $IMAP, $DB, $sess_user_lang;
251  $user_id = NULL;
252 
253  if (!$host)
254    $host = $CONFIG['default_host'];
255
256  // parse $host URL
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
265  // query if user already registered
266  $sql_result = $DB->query("SELECT user_id, username, language, preferences
267                            FROM ".get_table_name('users')."
268                            WHERE  mail_host=? AND (username=? OR alias=?)",
269                            $host,
270                            $user,
271                            $user);
272
273  // user already registered -> overwrite username
274  if ($sql_arr = $DB->fetch_assoc($sql_result))
275    {
276    $user_id = $sql_arr['user_id'];
277    $user = $sql_arr['username'];
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    {
287    // get user prefs
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
295
296    // set user specific language
297    if (strlen($sql_arr['language']))
298      $sess_user_lang = $_SESSION['user_lang'] = $sql_arr['language'];
299     
300    // update user's record
301    $DB->query("UPDATE ".get_table_name('users')."
302                SET    last_login=NOW()
303                WHERE  user_id=?",
304                $user_id);
305    }
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;
316    $_SESSION['imap_port'] = $imap_port;
317    $_SESSION['imap_ssl']  = $imap_ssl;
318    $_SESSION['username']  = $user;
319    $_SESSION['user_lang'] = $sess_user_lang;
320    $_SESSION['password']  = encrypt_passwd($pass);
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
333function rcmail_create_user($user, $host)
334  {
335  global $DB, $CONFIG, $IMAP;
336 
337  $DB->query("INSERT INTO ".get_table_name('users')."
338              (created, last_login, username, mail_host, language)
339              VALUES (NOW(), NOW(), ?, ?, ?)",
340              $user,
341              $host,
342                      $_SESSION['user_lang']);
343
344  if ($user_id = $DB->insert_id('user_ids'))
345    {
346    $user_email = strstr($user, '@') ? $user : sprintf('%s@%s', $user, $host);
347    $user_name = $user!=$user_email ? $user : '';
348   
349    // also create a new identity record
350    $DB->query("INSERT INTO ".get_table_name('identities')."
351                (user_id, `default`, name, email)
352                VALUES (?, '1', ?, ?)",
353                $user_id,
354                $user_name,
355                $user_email);
356                       
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);
367    }
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);
375    }
376   
377  return $user_id;
378  }
379
380
381function show_message($message, $type='notice')
382  {
383  global $OUTPUT, $JS_OBJECT_NAME, $REMOTE_REQUEST;
384
385  $framed = $GLOBALS['_framed'];
386  $command = sprintf("display_message('%s', '%s');",
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
403function console($msg, $type=1)
404  {
405  if ($GLOBALS['REMOTE_REQUEST'])
406    print "// $msg\n";
407  else
408    {
409    print $msg;
410    print "\n<hr>\n";
411    }
412  }
413
414
415function encrypt_passwd($pass)
416  {
417  $cypher = des('rcmail?24BitPwDkeyF**ECB', $pass, 1, 0, NULL);
418  return base64_encode($cypher);
419  }
420
421
422function 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
430function 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
442// read directory program/localization/ and return a list of available languages
443function 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
469
470
471// ************** template parsing and gui functions **************
472
473
474// return boolean if a specific template exists
475function 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
487function 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
524function parse_with_globals($input)
525  {
526  $output = preg_replace('/\$(__[a-z0-9_\-]+)/e', '$GLOBALS["\\1"]', $input);
527  return $output;
528  }
529
530
531
532function 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
539function 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'])
567        return rep_specialchars_output(rcube_label($attrib));
568      break;
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()))
699          return "RoundCube|Mail :: ".rep_specialchars_output(UTF7DecodeString($mbox_name), 'html', 'all');
700        else
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
715function rcube_button($attrib)
716  {
717  global $CONFIG, $OUTPUT, $JS_OBJECT_NAME, $BROWSER;
718  static $sa_buttons = array();
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']));
774
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
782  // add empty alt attribute for XHTML compatibility
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
852function rcube_menu($attrib)
853  {
854 
855  return '';
856  }
857
858
859
860function 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)
873    $table .= '<td class="'.$col.'">' . rep_specialchars_output(rcube_label($col)) . "</td>\n";
874
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
903function 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
922  if (!empty($_POST[$fname]))
923    $value = $_POST[$fname];
924
925  $out = $input->show($value);
926         
927  return $out;
928  }
929
930
931function 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
944function format_date($date, $format=NULL)
945  {
946  global $CONFIG, $sess_user_lang;
947 
948  if (is_numeric($date))
949    $ts = $date;
950  else
951    $ts = strtotime($date);
952
953  // convert time to user's timezone
954  $timestamp = $ts - date('Z', $ts) + ($CONFIG['timezone'] * 3600);
955 
956  // get current timestamp in user's timezone
957  $now = time();  // local time
958  $now -= (int)date('Z'); // make GMT time
959  $now += ($CONFIG['timezone'] * 3600); // user's time
960
961  $day_secs = 60*((int)date('H', $now)*60 + (int)date('i', $now));
962  $week_secs = 60 * 60 * 24 * 7;
963  $diff = $now - $timestamp;
964
965  // define date format depending on current time 
966  if ($CONFIG['prettydate'] && !$format && $diff < $day_secs)
967    return sprintf('%s %s', rcube_label('today'), date('H:i', $timestamp));
968  else if ($CONFIG['prettydate'] && !$format && $diff < $week_secs)
969    $format = $CONFIG['date_short'] ? $CONFIG['date_short'] : 'D H:i';
970  else if (!$format)
971    $format = $CONFIG['date_long'] ? $CONFIG['date_long'] : 'd.m.Y H:i';
972
973
974  // parse format string manually in order to provide localized weekday and month names
975  // an alternative would be to convert the date() format string to fit with strftime()
976  $out = '';
977  for($i=0; $i<strlen($format); $i++)
978    {
979    if ($format{$i}=='\\')  // skip escape chars
980      continue;
981   
982    // write char "as-is"
983    if ($format{$i}==' ' || $format{$i-1}=='\\')
984      $out .= $format{$i};
985    // weekday (short)
986    else if ($format{$i}=='D')
987      $out .= rcube_label(strtolower(date('D', $timestamp)));
988    // weekday long
989    else if ($format{$i}=='l')
990      $out .= rcube_label(strtolower(date('l', $timestamp)));
991    // month name (short)
992    else if ($format{$i}=='M')
993      $out .= rcube_label(strtolower(date('M', $timestamp)));
994    // month name (long)
995    else if ($format{$i}=='F')
996      $out .= rcube_label(strtolower(date('F', $timestamp)));
997    else
998      $out .= date($format{$i}, $timestamp);
999    }
1000 
1001  return $out;
1002  }
1003
1004
1005// ************** functions delivering gui objects **************
1006
1007
1008
1009function rcmail_message_container($attrib)
1010  {
1011  global $OUTPUT, $JS_OBJECT_NAME;
1012
1013  if (!$attrib['id'])
1014    $attrib['id'] = 'rcmMessageContainer';
1015
1016  // allow the following attributes to be added to the <table> tag
1017  $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id'));
1018  $out = '<div' . $attrib_str . "></div>";
1019 
1020  $OUTPUT->add_script("$JS_OBJECT_NAME.gui_object('message', '$attrib[id]');");
1021 
1022  return $out;
1023  }
1024
1025
1026// return code for the webmail login form
1027function rcmail_login_form($attrib)
1028  {
1029  global $CONFIG, $OUTPUT, $JS_OBJECT_NAME, $SESS_HIDDEN_FIELD;
1030 
1031  $labels = array();
1032  $labels['user'] = rcube_label('username');
1033  $labels['pass'] = rcube_label('password');
1034  $labels['host'] = rcube_label('server');
1035 
1036  $input_user = new textfield(array('name' => '_user', 'size' => 30));
1037  $input_pass = new passwordfield(array('name' => '_pass', 'size' => 30));
1038  $input_action = new hiddenfield(array('name' => '_action', 'value' => 'login'));
1039   
1040  $fields = array();
1041  $fields['user'] = $input_user->show($_POST['_user']);
1042  $fields['pass'] = $input_pass->show();
1043  $fields['action'] = $input_action->show();
1044 
1045  if (is_array($CONFIG['default_host']))
1046    {
1047    $select_host = new select(array('name' => '_host'));
1048   
1049    foreach ($CONFIG['default_host'] as $key => $value)
1050      $select_host->add($value, (is_numeric($key) ? $value : $key));
1051     
1052    $fields['host'] = $select_host->show($_POST['_host']);
1053    }
1054  else if (!strlen($CONFIG['default_host']))
1055    {
1056        $input_host = new textfield(array('name' => '_host', 'size' => 30));
1057        $fields['host'] = $input_host->show($_POST['_host']);
1058    }
1059
1060  $form_name = strlen($attrib['form']) ? $attrib['form'] : 'form';
1061  $form_start = !strlen($attrib['form']) ? '<form name="form" action="./" method="post">' : '';
1062  $form_end = !strlen($attrib['form']) ? '</form>' : '';
1063 
1064  if ($fields['host'])
1065    $form_host = <<<EOF
1066   
1067</tr><tr>
1068
1069<td class="title">$labels[host]</td>
1070<td>$fields[host]</td>
1071
1072EOF;
1073
1074  $OUTPUT->add_script("$JS_OBJECT_NAME.gui_object('loginform', '$form_name');");
1075 
1076  $out = <<<EOF
1077$form_start
1078$SESS_HIDDEN_FIELD
1079$fields[action]
1080<table><tr>
1081
1082<td class="title">$labels[user]</td>
1083<td>$fields[user]</td>
1084
1085</tr><tr>
1086
1087<td class="title">$labels[pass]</td>
1088<td>$fields[pass]</td>
1089$form_host
1090</tr></table>
1091$form_end
1092EOF;
1093
1094  return $out;
1095  }
1096
1097
1098?>
Note: See TracBrowser for help on using the repository browser.