Changeset 97bd2c0 in github


Ignore:
Timestamp:
Sep 29, 2007 2:15:05 PM (6 years ago)
Author:
thomascube <thomas@…>
Branches:
master, HEAD, courier-fix, dev-browser-capabilities, pdo, release-0.6, release-0.7, release-0.8
Children:
ee18f5f
Parents:
ca2b4dd
Message:

Filter linked/imported CSS files (#1484056)

Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • CHANGELOG

    re2f3738f r97bd2c0  
    11CHANGELOG RoundCube Webmail 
    22--------------------------- 
     3 
     42007/09/29 (thomasb) 
     5---------- 
     6- Filter linked/imported CSS files (closes #1484056) 
     7 
    38 
    492007/09/27 (tomekp) 
  • program/include/main.inc

    r5e80457 r97bd2c0  
    3030require_once('lib/utf7.inc'); 
    3131require_once('lib/utf8.class.php'); 
     32require_once('include/rcube_shared.inc'); 
    3233require_once('include/rcmail_template.inc'); 
    3334 
     
    14471448 
    14481449  return $domain; 
     1450  } 
     1451 
     1452 
     1453/** 
     1454 * Replace all css definitions with #container [def] 
     1455 * 
     1456 * @param string CSS source code 
     1457 * @param string Container ID to use as prefix 
     1458 * @return string Modified CSS source 
     1459 */ 
     1460function rcmail_mod_css_styles($source, $container_id, $base_url = '') 
     1461  { 
     1462  $a_css_values = array(); 
     1463  $last_pos = 0; 
     1464 
     1465  // cut out all contents between { and } 
     1466  while (($pos = strpos($source, '{', $last_pos)) && ($pos2 = strpos($source, '}', $pos))) 
     1467  { 
     1468    $key = sizeof($a_css_values); 
     1469    $a_css_values[$key] = substr($source, $pos+1, $pos2-($pos+1)); 
     1470    $source = substr($source, 0, $pos+1) . "<<str_replacement[$key]>>" . substr($source, $pos2, strlen($source)-$pos2); 
     1471    $last_pos = $pos+2; 
     1472  } 
     1473 
     1474  // remove html commends and add #container to each tag selector. 
     1475  // also replace body definition because we also stripped off the <body> tag 
     1476  $styles = preg_replace( 
     1477    array( 
     1478      '/(^\s*<!--)|(-->\s*$)/', 
     1479      '/(^\s*|,\s*|\}\s*)([a-z0-9\._#][a-z0-9\.\-_]*)/im', 
     1480      '/@import\s+(url\()?[\'"]?([^\)\'"]+)[\'"]?(\))?/ime', 
     1481      '/<<str_replacement\[([0-9]+)\]>>/e', 
     1482      "/$container_id\s+body/i" 
     1483    ), 
     1484    array( 
     1485      '', 
     1486      "\\1#$container_id \\2", 
     1487      "sprintf(\"@import url('./bin/modcss.php?u=%s&c=%s')\", urlencode(make_absolute_url('\\2','$base_url')), urlencode($container_id))", 
     1488      "\$a_css_values[\\1]", 
     1489      "$container_id div.rcmBody" 
     1490    ), 
     1491    $source); 
     1492 
     1493  return $styles; 
    14491494  } 
    14501495 
  • program/include/rcube_shared.inc

    r5a6efff r97bd2c0  
    435435  $host_url = $base_url; 
    436436  $abs_path = $path; 
     437   
     438  // check if path is an absolute URL 
     439  if (preg_match('/^[fhtps]+:\/\//', $path)) 
     440    return $path; 
    437441 
    438442  // cut base_url to the last directory 
  • program/steps/mail/func.inc

    r5eee009 r97bd2c0  
    947947  $body = preg_replace('/\x00/', '', $body); 
    948948   
     949  $base_url = ""; 
    949950  $last_style_pos = 0; 
    950951  $body_lc = strtolower($body); 
    951952   
     953  // check for <base href> 
     954  if (preg_match(($base_reg = '/(<base.*href=["\']?)([hftps]{3,5}:\/{2}[^"\'\s]+)([^<]*>)/i'), $body, $base_regs)) 
     955    $base_url = $base_regs[2]; 
     956   
    952957  // find STYLE tags 
    953958  while (($pos = strpos($body_lc, '<style', $last_style_pos)) && ($pos2 = strpos($body_lc, '</style>', $pos))) 
     
    956961 
    957962    // replace all css definitions with #container [def] 
    958     $styles = rcmail_mod_css_styles(substr($body, $pos, $pos2-$pos), $container_id); 
     963    $styles = rcmail_mod_css_styles(substr($body, $pos, $pos2-$pos), $container_id, $base_url); 
    959964 
    960965    $body = substr($body, 0, $pos) . $styles . substr($body, $pos2); 
     
    984989 
    985990  // resolve <base href> 
    986   $base_reg = '/(<base.*href=["\']?)([hftps]{3,5}:\/{2}[^"\'\s]+)([^<]*>)/i'; 
    987   if (preg_match($base_reg, $body, $regs)) 
    988     { 
    989     $base_url = $regs[2]; 
     991  if ($base_url) 
     992    { 
    990993    $body = preg_replace('/(src|background|href)=(["\']?)([\.\/]+[^"\'\s]+)(\2|\s|>)/Uie', "'\\1=\"'.make_absolute_url('\\3', '$base_url').'\"'", $body); 
    991994    $body = preg_replace('/(url\s*\()(["\']?)([\.\/]+[^"\'\)\s]+)(\2)\)/Uie', "'\\1\''.make_absolute_url('\\3', '$base_url').'\')'", $body); 
     
    994997     
    995998  // modify HTML links to open a new window if clicked 
    996   $body = preg_replace('/<a\s+([^>]+)>/Uie', "rcmail_alter_html_link('\\1');", $body); 
     999  $body = preg_replace('/<(a|link)\s+([^>]+)>/Uie', "rcmail_alter_html_link('\\1','\\2', '$container_id');", $body); 
    9971000 
    9981001  // add comments arround html and other tags 
     
    10061009    $body); 
    10071010 
    1008   $out = preg_replace(array('/<body([^>]*)>/i', 
    1009                             '/<\/body>/i'), 
    1010                       array('<div class="rcmBody"\\1>', 
    1011                             '</div>'), 
    1012                       $out); 
     1011  $out = preg_replace( 
     1012    array( 
     1013      '/<body([^>]*)>/i', 
     1014      '/<\/body>/i', 
     1015    ), 
     1016    array( 
     1017      '<div class="rcmBody"\\1>', 
     1018      '</div>', 
     1019    ), 
     1020    $out); 
    10131021 
    10141022  // quote <? of php and xml files that are specified as text/html 
     
    10201028 
    10211029// parse link attributes and set correct target 
    1022 function rcmail_alter_html_link($in) 
     1030function rcmail_alter_html_link($tag, $attrs, $container_id) 
    10231031  { 
    10241032  $in = preg_replace('/=([^("|\'|\s)]+)(\s|$)/', '="\1"', $in); 
    1025   $attrib = parse_attrib_string($in); 
    1026  
    1027   if (stristr((string)$attrib['href'], 'mailto:')) 
    1028     $attrib['onclick'] = sprintf("return %s.command('compose','%s',this)", 
    1029                                  JS_OBJECT_NAME, 
    1030                                  JQ(substr($attrib['href'], 7))); 
     1033  $attrib = parse_attrib_string($attrs); 
     1034   
     1035  if ($tag == 'link' && preg_match('/^https?:\/\//i', $attrib['href'])) 
     1036    $attrib['href'] = "./bin/modcss.php?u=" . urlencode($attrib['href']) . "&amp;c=" . urlencode($container_id); 
     1037 
     1038  else if (stristr((string)$attrib['href'], 'mailto:')) 
     1039    $attrib['onclick'] = sprintf( 
     1040      "return %s.command('compose','%s',this)", 
     1041      JS_OBJECT_NAME, 
     1042      JQ(substr($attrib['href'], 7))); 
     1043   
    10311044  else if (!empty($attrib['href']) && $attrib['href']{0}!='#') 
    10321045    $attrib['target'] = '_blank'; 
    1033    
    1034   return '<a' . create_attrib_string($attrib, array('href', 'name', 'target', 'onclick', 'id', 'class', 'style', 'title')) . '>'; 
    1035   } 
    1036  
    1037  
    1038 // replace all css definitions with #container [def] 
    1039 function rcmail_mod_css_styles($source, $container_id) 
    1040   { 
    1041   $a_css_values = array(); 
    1042   $last_pos = 0; 
    1043    
    1044   // cut out all contents between { and } 
    1045   while (($pos = strpos($source, '{', $last_pos)) && ($pos2 = strpos($source, '}', $pos))) 
    1046     { 
    1047     $key = sizeof($a_css_values); 
    1048     $a_css_values[$key] = substr($source, $pos+1, $pos2-($pos+1)); 
    1049     $source = substr($source, 0, $pos+1) . "<<str_replacement[$key]>>" . substr($source, $pos2, strlen($source)-$pos2); 
    1050     $last_pos = $pos+2; 
    1051     } 
    1052  
    1053   // remove html commends and add #container to each tag selector. 
    1054   // also replace body definition because we also stripped off the <body> tag 
    1055   $styles = preg_replace(array('/(^\s*<!--)|(-->\s*$)/', '/(^\s*|,\s*|\}\s*)([a-z0-9\._][a-z0-9\.\-_]*)/im', '/<<str_replacement\[([0-9]+)\]>>/e', "/$container_id\s+body/i"), 
    1056                          array('', "\\1#$container_id \\2", "\$a_css_values[\\1]", "$container_id div.rcmBody"), 
    1057                          $source); 
    1058  
    1059   return $styles; 
     1046 
     1047  return "<$tag" . create_attrib_string($attrib, array('href','name','target','onclick','id','class','style','title','rel','type','media')) . ' />'; 
    10601048  } 
    10611049 
Note: See TracChangeset for help on using the changeset viewer.