svncommit
2007-02-26 e17702395e1aa0fa54eea36817e1150c231d49eb
program/include/rcube_shared.inc
@@ -5,7 +5,7 @@
 | rcube_shared.inc                                                      |
 |                                                                       |
 | This file is part of the RoundCube PHP suite                          |
 | Copyright (C) 2005, RoundCube Dev. - Switzerland                      |
 | Copyright (C) 2005-2006, RoundCube Dev. - Switzerland                 |
 | Licensed under the GNU GPL                                            |
 |                                                                       |
 | CONTENTS:                                                             |
@@ -28,12 +28,15 @@
  
  var $scripts_path = '';
  var $script_files = array();
  var $external_scripts = array();
  var $scripts = array();
  var $charset = 'ISO-8859-1';
  
  var $script_tag_file = "<script type=\"text/javascript\" src=\"%s%s\"></script>\n";
  var $script_tag      = "<script type=\"text/javascript\">\n<!--\n%s\n\n//-->\n</script>\n";
  var $default_template = "<html>\n<head><title></title></head>\n<body></body>\n</html>";
  var $tag_format_external_script = "<script type=\"text/javascript\" src=\"%s\"></script>\n";
  var $title = '';
  var $header = '';
  var $footer = '';
@@ -68,48 +71,78 @@
    $this->script_files[$position][] = $file;
    }
    
  function include_external_script($script_location, $position='head')
  {
     if (!is_array($this->external_scripts[$position]))
     {
        $this->external_scripts[$position] = array();
     }
     $this->external_scripts[$position][] = $script_location;
  }
  function add_script($script, $position='head')
    {
    if (!isset($this->scripts[$position]))
      $this->scripts[$position] = '';
    $this->scripts[$position] .= "\n$script";
      $this->scripts[$position] = "\n$script";
    else
      $this->scripts[$position] .= "\n$script";
    }
  function set_title()
  function set_title($t)
    {
    $this->title = $t;
    }
  function set_charset($charset)
    {
    $this->charset = $charset;
    }
    global $MBSTRING;
    
    $this->charset = $charset;
    if ($MBSTRING && function_exists("mb_internal_encoding"))
      {
      if(!@mb_internal_encoding($charset))
        $MBSTRING = FALSE;
      }
    }
  function get_charset()
    {
    return $this->charset;
    }
  function reset()
    {
    $this->css = new rcube_css();
    $this->script_files = array();
    $this->scripts = array();
    $this->title = '';
    }
  function write($templ='', $base_path='')
    {
    $output = trim($templ);
    $output = empty($templ) ? $this->default_template : trim($templ);
    // set default page title
    if (!strlen($this->title))
      $this->title = 'RoundCube|Mail';
    if (empty($this->title))
      $this->title = 'RoundCube Mail';
  
    // replace specialchars in content
    $__page_title = rep_specialchars_output($this->title, 'html', 'show', FALSE);
    $__page_title = Q($this->title, 'show', FALSE);
    $__page_header = $__page_body = $__page_footer = '';
    
    
    // include meta tag with charset
    if (!empty($this->charset))
      $__page_header = '<meta http-equiv="content-type" content="text/html; charset='.$this->charset.'" />'."\n";;
      {
      header('Content-Type: text/html; charset='.$this->charset);
      $__page_header = '<meta http-equiv="content-type" content="text/html; charset='.$this->charset.'" />'."\n";
      }
  
  
    // definition of the code to be placed in the document header and footer
@@ -117,28 +150,39 @@
      foreach ($this->script_files['head'] as $file)
        $__page_header .= sprintf($this->script_tag_file, $this->scripts_path, $file);
    if (strlen($this->scripts['head']))
   if (is_array($this->external_scripts['head']))
   {
      foreach ($this->external_scripts['head'] as $xscript)
      {
         $__page_header .= sprintf($this->tag_format_external_script, $xscript);
      }
   }
    if (!empty($this->scripts['head']))
      $__page_header .= sprintf($this->script_tag, $this->scripts['head']);
          
    if (is_array($this->script_files['foot']))
      {
      foreach ($this->script_files['foot'] as $file)
        $__page_footer .= sprintf($this->script_tag_file, $this->scripts_path, $file);
      }
    if (strlen($this->scripts['foot']))
    if (!empty($this->scripts['foot']))
      $__page_footer .= sprintf($this->script_tag, $this->scripts['foot']);
    if ($this->footer)
      $__page_footer .= "\n" . $this->footer;
    $__page_header .= $this->css->show();
  
    // find page header
    if($hpos = strpos(strtolower($output), '</head>'))
    if($hpos = rc_strpos(rc_strtolower($output), '</head>'))
      $__page_header .= "\n";
    else 
      {
      if (!is_numeric($hpos))
        $hpos = strpos(strtolower($output), '<body');
      if (!is_numeric($hpos) && ($hpos = strpos(strtolower($output), '<html')))
        $hpos = rc_strpos(rc_strtolower($output), '<body');
      if (!is_numeric($hpos) && ($hpos = rc_strpos(rc_strtolower($output), '<html')))
        {
        while($output[$hpos]!='>')
        $hpos++;
@@ -150,28 +194,30 @@
  
    // add page hader
    if($hpos)
      $output = substr($output,0,$hpos) . $__page_header . substr($output,$hpos,strlen($output));
      $output = rc_substr($output,0,$hpos) . $__page_header . rc_substr($output,$hpos,rc_strlen($output));
    else
      $output = $__page_header . $output;
  
  
    // find page body
    if($bpos = strpos(strtolower($output), '<body'))
    if($bpos = rc_strpos(rc_strtolower($output), '<body'))
      {
      while($output[$bpos]!='>') $bpos++;
      $bpos++;
      }
    else
      $bpos = strpos(strtolower($output), '</head>')+7;
      $bpos = rc_strpos(rc_strtolower($output), '</head>')+7;
  
    // add page body
    if($bpos && $__page_body)
      $output = substr($output,0,$bpos) . "\n$__page_body\n" . substr($output,$bpos,strlen($output));
      $output = rc_substr($output,0,$bpos) . "\n$__page_body\n" . rc_substr($output,$bpos,rc_strlen($output));
  
  
    // find and add page footer
    if(($fpos = strpos(strtolower($output), '</body>')) || ($fpos = strpos(strtolower($output), '</html>')))
      $output = substr($output,0,$fpos) . "$__page_footer\n" . substr($output,$fpos,strlen($output));
    $output_lc = rc_strtolower($output);
    if(($fpos = strrstr($output_lc, '</body>')) ||
       ($fpos = strrstr($output_lc, '</html>')))
      $output = substr($output, 0, $fpos) . "$__page_footer\n" . substr($output, $fpos);
    else
      $output .= "\n$__page_footer";
  
@@ -180,8 +226,9 @@
    $__page_header = $__page_footer = '';
  
  
    // correct absolute pathes in images and other tags
    // correct absolute paths in images and other tags
    $output = preg_replace('/(src|href|background)=(["\']?)(\/[a-z0-9_\-]+)/Ui', "\\1=\\2$base_path\\3", $output);
    $output = str_replace('$__skin_path', $base_path, $output);
  
    print rcube_charset_convert($output, 'UTF-8', $this->charset);
    }
@@ -678,7 +725,7 @@
      // encode textarea content
      if ($key=='value')
        $value = rep_specialchars_output($value, 'html', 'replace', FALSE);
        $value = Q($value, 'strict', FALSE);
      // attributes with no value
      if (in_array($key, array('checked', 'multiple', 'disabled', 'selected')))
@@ -831,9 +878,9 @@
    if (isset($this->attrib['value']))
      unset($this->attrib['value']);
    if (strlen($value))
      $value = rep_specialchars_output($value, 'html', 'replace', FALSE);
    if (!empty($value) && !isset($this->attrib['mce_editable']))
      $value = Q($value, 'strict', FALSE);
    // return final tag
    return sprintf('<%s%s>%s</%s>%s',
                   $this->_conv_case('textarea', 'tag'),
@@ -962,20 +1009,22 @@
    
    if (!is_array($select))
      $select = array((string)$select);
    foreach ($this->options as $option)
      {
      $selected = ((strlen($option['value']) && in_array($option['value'], $select, TRUE)) ||
                   (in_array($option['text'], $select, TRUE))) ? $this->_conv_case(' selected', 'attrib') : '';
      $selected = ((isset($option['value']) &&
                    in_array($option['value'], $select, TRUE)) ||
                   (in_array($option['text'], $select, TRUE))) ?
        $this->_conv_case(' selected', 'attrib') : '';
                  
      $options_str .= sprintf("<%s%s%s>%s</%s>\n",
                             $this->_conv_case('option', 'tag'),
                             strlen($option['value']) ? sprintf($value_str, $option['value']) : '',
                             isset($option['value']) ? sprintf($value_str, $option['value']) : '',
                             $selected, 
                             rep_specialchars_output($option['text'], 'html', 'replace', FALSE),
                             Q($option['text'], 'strict', FALSE),
                             $this->_conv_case('option', 'tag'));
      }
    // return final tag
    return sprintf('<%s%s>%s</%s>%s',
                   $this->_conv_case('select', 'tag'),
@@ -1057,7 +1106,7 @@
  $nr = is_numeric($attrib['nr']) ? $attrib['nr'] : 1;
  $vars = isset($attrib['vars']) ? $attrib['vars'] : '';
  $command_name = strlen($attrib['command']) ? $attrib['command'] : NULL;
  $command_name = !empty($attrib['command']) ? $attrib['command'] : NULL;
  $alias = $attrib['name'] ? $attrib['name'] : ($command_name && $command_label_map[$command_name] ? $command_label_map[$command_name] : '');
@@ -1148,8 +1197,6 @@
    return strtoupper($text);
  else if ($attrib['lowercase'])
    return strtolower($text);
  else
    return $text;
  return $text;
  }
@@ -1171,8 +1218,12 @@
// send header with expire date 30 days in future
function send_future_expire_header()
  {
  if (!headers_sent())
    header("Expires: ".gmdate("D, d M Y H:i:s", mktime()+2600000)." GMT");
  if (headers_sent())
    return;
  header("Expires: ".gmdate("D, d M Y H:i:s", mktime()+2600000)." GMT");
  header("Cache-Control: ");
  header("Pragma: ");
  }
@@ -1210,7 +1261,7 @@
        if (!ereg("^[_a-zA-Z]{1}[_a-zA-Z0-9]*$", $key) /* || is_js_reserved_word($key) */)
          $key = "'$key'";
        if (!is_array($value))
        if (!is_array($value) && is_string($value))
          {
          $value = str_replace("\r\n", '\n', $value);
          $value = str_replace("\n", '\n', $value);
@@ -1221,7 +1272,12 @@
          {
          if ($type=='string')
            $is_string = true;
          else if ((($type=='mixed' && is_numeric($value)) || $type=='int') && strlen($value)<16)   // js interprets numbers with digits >15 as ...e+...
          else if (($type == 'mixed' && is_bool($value)) || $type == 'bool')
            {
            $is_string = false;
            $value = $value ? "true" : "false";
            }
          else if ((($type=='mixed' && is_numeric($value)) || $type=='int') && rc_strlen($value)<16)   // js interprets numbers with digits >15 as ...e+...
            $is_string = FALSE;
          else
            $is_string = TRUE;
@@ -1247,7 +1303,9 @@
      }
    }
  else
    {
    return $arr;
    }
  }
@@ -1276,12 +1334,51 @@
  }
function show_bytes($numbytes)
// parse a human readable string for a number of bytes
function parse_bytes($str)
  {
  if ($numbytes > 1024)
    return sprintf('%d KB', round($numbytes/1024));
  if (is_numeric($str))
    return intval($str);
  if (preg_match('/([0-9]+)([a-z])/i', $str, $regs))
    {
      $bytes = floatval($regs[1]);
      switch (strtolower($regs[2]))
      {
        case 'g':
          $bytes *= 1073741824;
          break;
        case 'm':
          $bytes *= 1048576;
          break;
        case 'k':
          $bytes *= 1024;
          break;
      }
    }
  return intval($bytes);
  }
// create a human readable string for a number of bytes
function show_bytes($bytes)
  {
  if ($bytes > 1073741824)
    {
    $gb = $bytes/1073741824;
    $str = sprintf($gb>=10 ? "%d GB" : "%.1f GB", $gb);
    }
  else if ($bytes > 1048576)
    {
    $mb = $bytes/1048576;
    $str = sprintf($mb>=10 ? "%d MB" : "%.1f MB", $mb);
    }
  else if ($bytes > 1024)
    $str = sprintf("%d KB",  round($bytes/1024));
  else
    return sprintf('%d B', $numbytes);
    $str = sprintf('%d B', $bytes);
  return $str;
  }
@@ -1322,21 +1419,82 @@
    }
// wrapper function for strlen
function rc_strlen($str)
  {
    if (function_exists('mb_strlen'))
      return mb_strlen($str);
    else
      return strlen($str);
  }
// wrapper function for strtolower
function rc_strtolower($str)
  {
    if (function_exists('mb_strtolower'))
      return mb_strtolower($str);
    else
      return strtolower($str);
  }
// wrapper function for substr
function rc_substr($str, $start, $len)
  {
  if (function_exists('mb_substr'))
    return mb_substr($str, $start, $len);
  else
    return substr($str, $start, $len);
  }
// wrapper function for strpos
function rc_strpos($haystack, $needle, $offset=0)
  {
  if (function_exists('mb_strpos'))
    return mb_strpos($haystack, $needle, $offset);
  else
    return strpos($haystack, $needle, $offset);
  }
// wrapper function for strrpos
function rc_strrpos($haystack, $needle, $offset=0)
  {
  if (function_exists('mb_strrpos'))
    return mb_strrpos($haystack, $needle, $offset);
  else
    return strrpos($haystack, $needle, $offset);
  }
// replace the middle part of a string with ...
// if it is longer than the allowed length
function abbrevate_string($str, $maxlength, $place_holder='...')
  {
  $length = strlen($str);
  $first_part_length = floor($maxlength/2) - strlen($place_holder);
  $length = rc_strlen($str);
  $first_part_length = floor($maxlength/2) - rc_strlen($place_holder);
  
  if ($length > $maxlength)
    {
    $second_starting_location = $length - $maxlength + $first_part_length + 1;
    $str = substr($str, 0, $first_part_length) . $place_holder . substr($str, $second_starting_location, $length);
    $str = rc_substr($str, 0, $first_part_length) . $place_holder . rc_substr($str, $second_starting_location, $length);
    }
  return $str;
  }
// make sure the string ends with a slash
function slashify($str)
  {
  return unslashify($str).'/';
  }
// remove slash at the end of the string
function unslashify($str)
  {
  return preg_replace('/\/$/', '', $str);
  }
// delete all files within a folder
function clear_directory($dir_path)
@@ -1386,4 +1544,31 @@
  }
?>
/**
 * strrstr
 *
 * return the last occurence of a string in another string
 * @param haystack string string in which to search
 * @param needle string string for which to search
 * @return index of needle within haystack, or false if not found
 */
function strrstr($haystack, $needle)
  {
    $pver = phpversion();
    if ($pver[0] >= 5)
      {
        return strrpos($haystack, $needle);
      }
    else
      {
        $index = strpos(strrev($haystack), strrev($needle));
        if($index === false) {
            return false;
        }
        $index = strlen($haystack) - strlen($needle) - $index;
        return $index;
      }
  }
?>