Fixed security issues in file uploads.
[fa-stable.git] / reporting / includes / class.graphic.inc
index 19572288bec1c13fae1d174d4155d72732e80593..31f0105fdb7276d4f40722d72e7270d57b65d0d2 100644 (file)
@@ -147,11 +147,18 @@ class graph
     var $h3d = 15; // 3D height
        var $built_in = true;
        var $fontfile = "";
-
+       var $encoding;
+       
     function graph()
     {
-        $this->x = $this->y = $this->z = array();
+               $this->encoding = $_SESSION['language']->encoding;
+               $path = dirname(__FILE__).'/../fonts/';
+
+               // If you use utf-8 encoding you have to download and install FreeSans.ttf font.
+               // It is not bundled with application due to its size.
+               $this->fontfile = $this->encoding=='UTF-8' ? $path.'FreeSans.ttf' : $path.'LiberationSans-Regular.ttf';
 
+        $this->x = $this->y = $this->z = array();
         $this->biggest_x        = NULL;
         $this->biggest_y        = NULL;
         $this->alternate_x      = false;
@@ -164,7 +171,7 @@ class graph
 
     function display($save="", $border=false)
     {
-        $this->legend_exists        = (ereg("(5|6)", $this->type)) ? true : false;
+        $this->legend_exists        = (preg_match("/(5|6)/", $this->type)) ? true : false;
         $this->biggest_graphic_name = (strlen($this->graphic_1) > strlen($this->graphic_2)) ? $this->graphic_1 : $this->graphic_2;
         $this->height_title         = (!empty($this->title)) ? ($this->string_height($this->tsize) + 15) : 0;
         $this->space_between_bars   = ($this->type == 1) ? 40 : 30;
@@ -181,7 +188,7 @@ class graph
         $this->graphic_area_x2     = $this->graphic_area_x1 + $this->graphic_area_width;
         $this->graphic_area_y2     = $this->graphic_area_y1 + $this->graphic_area_height;
 
-               if (count($this->z) && (ereg("(1|2|3|4)", $this->type)))
+               if (count($this->z) && (preg_match("/(1|2|3|4)/", $this->type)))
             $this->graphic_2_exists = true;
         $this->total_parameters    = count($this->x);
                for ($i = 0; $i < $this->total_parameters; $i++)
@@ -203,6 +210,8 @@ class graph
         }
 
         $this->sum_total           = array_sum($this->y);
+        if ($this->sum_total == 0)
+               $this->sum_total = 1;
         $this->space_between_bars += ($this->graphic_2_exists == true) ? 10 : 0;
 
         $this->calculate_higher_value();
@@ -222,8 +231,8 @@ class graph
         // Fill background
         imagefill($this->img, 0, 0, $this->color['background']);
         //imagefilledrectangle($this->img, 0, 0, $this->width, $this->height, $this->color['background']);
-        if ($border)
-               imagerectangle($this->img, 0, 0, $this->width-1, $this->height-1, $this->color['bars']);
+        //if ($border)
+        //     imagerectangle($this->img, 0, 0, $this->width-1, $this->height-1, imagecolorallocate($this->img, 100, 150, 200));
 
         // Draw title
         if (!empty($this->title))
@@ -234,7 +243,7 @@ class graph
 
 
         // Draw axis and background lines for "vertical bars", "dots" and "lines"
-        if (ereg("^(1|3|4)$", $this->type))
+        if (preg_match("/^(1|3|4)$/", $this->type))
         {
             if ($this->legend_exists == true)
             {
@@ -312,7 +321,7 @@ class graph
 
 
         // Draw legend box for "pie" or "donut"
-        else if (ereg("^(5|6)$", $this->type))
+        else if (preg_match("/^(5|6)$/", $this->type))
         {
             $this->draw_legend();
         }
@@ -396,7 +405,8 @@ class graph
                 imagefilledrectangle($this->img, $x1, $y1, $x2, $y2, $this->color['bars']);
                 $this->_imagestring($this->img, $this->size, ($x2+7), ($y1+2), $this->number_formated($this->y[$i], $this->dec2), $this->color['bars_shadow']);
 
-                $this->_imagestring($this->img, $this->size, ($x1 - ((strlen($parameter)*7)+7)), ($y1+2), $parameter, $this->color['axis_values']);
+                //$this->_imagestring($this->img, $this->size, ($x1 - ((strlen($parameter)*7)+7)), ($y1+2), $parameter, $this->color['axis_values']);
+                $this->_imagestring($this->img, $this->size, 30, ($y1+2), $parameter, $this->color['axis_values']);
             }
         }
 
@@ -404,7 +414,7 @@ class graph
         /**
         * Draw graphic: DOTS or LINE
         */
-        else if (ereg("^(3|4)$", $this->type))
+        else if (preg_match("/^(3|4)$/", $this->type))
         {
 
             $x[0] = $this->graphic_area_x1+1;
@@ -475,7 +485,7 @@ class graph
         /**
         * Draw graphic: PIE or DONUT
         */
-        else if (ereg("^(5|6)$", $this->type))
+        else if (preg_match("/^(5|6)$/", $this->type))
         {
             $center_x = ($this->graphic_area_x1 + $this->graphic_area_x2) / 2;
             $center_y = ($this->graphic_area_y1 + $this->graphic_area_y2) / 2;
@@ -487,6 +497,7 @@ class graph
             foreach ($this->x as $i => $parameter)
             {
                 $size    = $this->y[$i] * 360 / $this->sum_total;
+                               $size = round2($size, 0);
                 $sizes[] = $size;
                 $start  += $size;
             }
@@ -508,7 +519,8 @@ class graph
                     for ($i = $this->h3d; $i >= 0; $i--)
                     {
                         //imagearc($this->img, $center_x, ($center_y+$i), $width, $height, $start, ($start+$size), $this->color[$color]);
-                        imagefilledarc($this->img, $center_x, ($center_y+$i), $width, $height, $start, ($start+$size), $this->color[$color], IMG_ARC_NOFILL);
+                        if ($size >= 1)
+                               imagefilledarc($this->img, $center_x, ($center_y+$i), $width, $height, $start, ($start+$size), $this->color[$color], IMG_ARC_NOFILL);
                     }
                     $start += $size;
                 }
@@ -524,8 +536,8 @@ class graph
                         $num_color -= 5;
                     }
                     $color = 'arc_' . $num_color;
-
-                    imagefilledarc($this->img, $center_x, $center_y, ($width+2), ($height+2), $start, ($start+$size), $this->color[$color], IMG_ARC_EDGED);
+                    if ($size >= 1)
+                       imagefilledarc($this->img, $center_x, $center_y, ($width+2), ($height+2), $start, ($start+$size), $this->color[$color], IMG_ARC_EDGED);
                     $start += $size;
                 }
             }
@@ -542,7 +554,8 @@ class graph
                     }
                     $color        = 'arc_' . $num_color;
                     $color_shadow = 'arc_' . $num_color . '_shadow';
-                    imagefilledarc($this->img, $center_x, $center_y, $width, $height, $start, ($start+$size), $this->color[$color], IMG_ARC_PIE);
+                    if ($size >= 1)
+                       imagefilledarc($this->img, $center_x, $center_y, $width, $height, $start, ($start+$size), $this->color[$color], IMG_ARC_PIE);
                     $start += $size;
                 }
                 imagefilledarc($this->img, $center_x, $center_y, 100, 100, 0, 360, $this->color['background'], IMG_ARC_PIE);
@@ -594,7 +607,7 @@ class graph
                 $this->graphic_area_width = ($this->space_between_dots * $this->total_parameters) - 10;
                 $this->graphic_area_x1   += $this->string_width(($this->higher_value_str), $this->size);
                 $this->width += $this->graphic_area_x1 + 20;
-                $this->width += ($this->legend_exists == true) ? 40 : ((7 * strlen($this->axis_x)) + 10);
+                $this->width += ($this->legend_exists == true) ? 40 : ((7 * strlen($this->axis_x)) + 30);
                 break;
 
             // Lines
@@ -603,7 +616,7 @@ class graph
                 $this->graphic_area_width = ($this->space_between_dots * $this->total_parameters) - 10;
                 $this->graphic_area_x1   += $this->string_width(($this->higher_value_str), $this->size);
                 $this->width += $this->graphic_area_x1 + 20;
-                $this->width += ($this->legend_exists == true) ? 40 : ((7 * strlen($this->axis_x)) + 10);
+                $this->width += ($this->legend_exists == true) ? 40 : ((7 * strlen($this->axis_x)) + 30);
                 break;
 
             // Pie
@@ -621,6 +634,7 @@ class graph
                 break;
         }
 
+        $this->graphic_area_width = max($this->graphic_area_width, $this->string_width($this->title, $this->size));
         $this->width += $this->graphic_area_width;
         $this->width += $this->legend_box_width;
 
@@ -702,10 +716,10 @@ class graph
 
 
         // Draw legend values for VERTICAL BARS, HORIZONTAL BARS, DOTS and LINES
-        if (ereg("^(1|2|3|4)$", $this->type))
+        if (preg_match("/^(1|2|3|4)$/", $this->type))
         {
-            $color_1 = (ereg("^(1|2)$", $this->type)) ? $this->color['bars']   : $this->color['line'];
-            $color_2 = (ereg("^(1|2)$", $this->type)) ? $this->color['bars_2'] : $this->color['line_2'];
+            $color_1 = (preg_match("/^(1|2)$/", $this->type)) ? $this->color['bars']   : $this->color['line'];
+            $color_2 = (preg_match("/^(1|2)$/", $this->type)) ? $this->color['bars_2'] : $this->color['line_2'];
 
             imagefilledrectangle($this->img, $x, $y, ($x+10), ($y+10), $color_1);
             imagerectangle($this->img, $x, $y, ($x+10), ($y+10), $this->color['title']);
@@ -717,11 +731,12 @@ class graph
         }
 
         // Draw legend values for PIE or DONUT
-        else if (ereg("^(5|6)$", $this->type))
+        else if (preg_match("/^(5|6)$/", $this->type))
         {
             if (!empty($this->axis_x))
             {
-                $this->_imagestring($this->img, $this->size, ((($x1+$x2)/2) - (strlen($this->axis_x)*7/2)), $y, $this->axis_x, $this->color['title']);
+                $this->_imagestring($this->img, $this->size, ((($x1+$x2)/2) - (strlen($this->axis_x)*7/2)), $y, 
+                       $this->axis_x." (".$this->graphic_1.")", $this->color['title']);
                 $y += 25;
             }
 
@@ -811,26 +826,28 @@ class graph
         {
             // Office
             case 1:
-                $this->color['title']       = imagecolorallocate($this->img,  50,  50,  50);
-                $this->color['background']  = imagecolorallocate($this->img, 238, 255, 238);
+                //$this->color['title']       = imagecolorallocate($this->img,  50,  50,  50);
+                $this->color['title']       = imagecolorallocate($this->img,  40,  70,  130);
+                //$this->color['background']  = imagecolorallocate($this->img, 238, 255, 238);
+                $this->color['background']  = imagecolorallocate($this->img, 255, 255, 255);
                 $this->color['axis_values'] = imagecolorallocate($this->img,  50,  50,  50);
                 $this->color['axis_line']   = imagecolorallocate($this->img, 100, 100, 100);
                 $this->color['bg_lines']    = imagecolorallocate($this->img, 220, 220, 220);
                 $this->color['bg_legend']   = imagecolorallocate($this->img, 255, 255, 255);
 
-                if (ereg("^(1|2)$", $this->type))
+                if (preg_match("/^(1|2)$/", $this->type))
                 {
                     $this->color['bars']          = imagecolorallocate($this->img, 100, 150, 200);
                     $this->color['bars_shadow']   = imagecolorallocate($this->img,  50, 100, 150);
                     $this->color['bars_2']        = imagecolorallocate($this->img, 200, 250, 150);
                     $this->color['bars_2_shadow'] = imagecolorallocate($this->img, 120, 170,  70);
                 }
-                else if (ereg("^(3|4)$", $this->type))
+                else if (preg_match("/^(3|4)$/", $this->type))
                 {
                     $this->color['line']   = imagecolorallocate($this->img, 100, 150, 200);
                     $this->color['line_2'] = imagecolorallocate($this->img, 230, 100, 100);
                 }
-                else if (ereg("^(5|6)$", $this->type))
+                else if (preg_match("/^(5|6)$/", $this->type))
                 {
                     $this->color['arc_1']        = imagecolorallocate($this->img, 255, 150,   0);
                     $this->color['arc_2']        = imagecolorallocate($this->img, 150,   0, 255);
@@ -858,19 +875,19 @@ class graph
                 $this->color['bg_lines']    = imagecolorallocate($this->img, 100, 100, 100);
                 $this->color['bg_legend']   = imagecolorallocate($this->img,  70,  70,  70);
 
-                if (ereg("^(1|2)$", $this->type))
+                if (preg_match("/^(1|2)$/", $this->type))
                 {
                     $this->color['bars']          = imagecolorallocate($this->img,  50, 200,  50);
                     $this->color['bars_shadow']   = imagecolorallocate($this->img,   0, 150,   0);
                     $this->color['bars_2']        = imagecolorallocate($this->img, 255, 255, 255);
                     $this->color['bars_2_shadow'] = imagecolorallocate($this->img, 220, 220, 220);
                 }
-                else if (ereg("^(3|4)$", $this->type))
+                else if (preg_match("/^(3|4)$/", $this->type))
                 {
                     $this->color['line']   = imagecolorallocate($this->img, 220, 220, 220);
                     $this->color['line_2'] = imagecolorallocate($this->img,   0, 180,   0);
                 }
-                else if (ereg("^(5|6)$", $this->type))
+                else if (preg_match("/^(5|6)$/", $this->type))
                 {
                     $this->color['arc_1']        = imagecolorallocate($this->img, 255, 255, 255);
                     $this->color['arc_2']        = imagecolorallocate($this->img, 200, 220, 200);
@@ -893,25 +910,27 @@ class graph
             // Spring
             case 3:
                 $this->color['title']       = imagecolorallocate($this->img, 250,  50,  50);
-                $this->color['background']  = imagecolorallocate($this->img, 250, 250, 220);
+                //$this->color['background']  = imagecolorallocate($this->img, 250, 250, 220);
+                $this->color['background']  = imagecolorallocate($this->img, 255, 255, 255);
                 $this->color['axis_values'] = imagecolorallocate($this->img,  50, 150,  50);
                 $this->color['axis_line']   = imagecolorallocate($this->img,  50, 100,  50);
                 $this->color['bg_lines']    = imagecolorallocate($this->img, 200, 224, 180);
-                $this->color['bg_legend']   = imagecolorallocate($this->img, 230, 230, 200);
+                //$this->color['bg_legend']   = imagecolorallocate($this->img, 230, 230, 200);
+                $this->color['bg_legend']   = imagecolorallocate($this->img, 255, 255, 255);
 
-                if (ereg("^(1|2)$", $this->type))
+                if (preg_match("/^(1|2)$/", $this->type))
                 {
                     $this->color['bars']          = imagecolorallocate($this->img, 255, 170,  80);
                     $this->color['bars_shadow']   = imagecolorallocate($this->img, 200, 120,  30);
                     $this->color['bars_2']        = imagecolorallocate($this->img, 250, 230,  80);
                     $this->color['bars_2_shadow'] = imagecolorallocate($this->img, 180, 150,   0);
                 }
-                else if (ereg("^(3|4)$", $this->type))
+                else if (preg_match("/^(3|4)$/", $this->type))
                 {
                     $this->color['line']   = imagecolorallocate($this->img, 230, 100,   0);
                     $this->color['line_2'] = imagecolorallocate($this->img, 220, 200,  50);
                 }
-                else if (ereg("^(5|6)$", $this->type))
+                else if (preg_match("/^(5|6)$/", $this->type))
                 {
                     $this->color['arc_1']        = imagecolorallocate($this->img, 100, 150, 200);
                     $this->color['arc_2']        = imagecolorallocate($this->img, 200, 250, 150);
@@ -937,6 +956,14 @@ class graph
     {
                if ($alt && strlen($string) > 12)
                        $string = substr($string, 0, 12);
+
+               if ($this->encoding != 'UTF-8') {
+                       if (function_exists('iconv'))
+                               $string = iconv($this->encoding, 'UTF-8', $string);
+                       else
+                               $string = mb_convert_encoding($string, 'UTF-8', $this->encoding);
+               }
+
        if ($this->built_in)
        {
                imagestring($img, $size, $x, $y + $alt, $string, $col);
@@ -958,6 +985,7 @@ class graph
                //      $angle = -15;
                //else
                                $angle = 0;
+
                imagettftext($img, $size, $angle, $x, $y + $alt, $col, $this->fontfile, $string);
        }
     }