Limit size of image data in stylesheets
authorMichael M Slusarz <slusarz@curecanti.org>
Wed, 20 Jan 2010 17:18:42 +0000 (10:18 -0700)
committerMichael M Slusarz <slusarz@curecanti.org>
Wed, 20 Jan 2010 17:18:42 +0000 (10:18 -0700)
framework/Core/lib/Horde.php

index 86f418a..ef3f8bb 100644 (file)
@@ -1502,19 +1502,26 @@ HTML;
     /**
      * Generate RFC 2397-compliant image data strings.
      *
-     * @param string $file  Filename containing image data.
+     * @param string $file    Filename containing image data.
+     * @param integer $limit  Sets a hard size limit for image data; if
+     *                        exceeded, will not string encode.
      *
      * @return string  The string to use in the image 'src' attribute; either
      *                 the image data if the browser supports, or the filepath
      *                 if not.
      */
-    public function base64ImgData($file)
+    public function base64ImgData($file, $limit = null)
     {
         $dataurl = $GLOBALS['browser']->hasFeature('dataurl');
         if (!$dataurl) {
             return $file;
         }
 
+        if (!is_null($limit) &&
+            (is_bool($dataurl) || ($limit < $dataurl))) {
+            $dataurl = $limit;
+        }
+
         /* Only encode image files if they are below the dataurl limit. */
         $filename = realpath($GLOBALS['registry']->get('fileroot', 'horde')) . preg_replace('/^' . preg_quote($GLOBALS['registry']->get('webroot', 'horde'), '/') . '/', '', $file);
         if (!file_exists($filename)) {
@@ -1755,7 +1762,8 @@ HTML;
      */
     public function stylesheetCallback($matches)
     {
-        return $matches[1] . self::base64ImgData($matches[2]) . $matches[3];
+        /* Limit data to 16 KB in stylesheets. */
+        return $matches[1] . self::base64ImgData($matches[2], 16384) . $matches[3];
     }
 
     /**