Index: trunk/expressoMail1_2/inc/class.imap_functions.inc.php
===================================================================
--- trunk/expressoMail1_2/inc/class.imap_functions.inc.php	(revision 1000)
+++ trunk/expressoMail1_2/inc/class.imap_functions.inc.php	(revision 1005)
@@ -1052,5 +1052,5 @@
 	    		if (($file_type == 'image/jpeg') || ($file_type == 'image/pjpeg') || ($file_type == 'image/gif') || ($file_type == 'image/png')) {
 	    			$img = "<IMG id='".$msg_folder.";;".$msg_number.";;".$i.";;".$msg->pid[$msg_number][$index].";;".$msg->encoding[$msg_number][$index]."' style='border:2px solid #fde7bc;padding:5px' title='".$this->functions->getLang("Click here do view (+)")."'src=./inc/show_thumbs.php?file_type=".$file_type."&msg_num=".$msg_number."&msg_folder=".$msg_folder."&msg_part=".$msg->pid[$msg_number][$index].">";
-	    			$href = "<a onMouseDown='save_image(event,this)' href='#".$msg_folder.";;".$msg_number.";;".$i.";;".$msg->pid[$msg_number][$index].";;".$msg->encoding[$msg_number][$index]."' onClick=\"window.open('./inc/show_img.php?msg_num=".$msg_number."&msg_folder=".$msg_folder."&msg_part=".$msg->pid[$msg_number][$index]."','mywindow','width=700,height=600,scrollbars=yes');\">". $img ."</a>";
+	    			$href = "<a onMouseDown='save_image(event,this,\"".$file_type."\")' href='#".$msg_folder.";;".$msg_number.";;".$i.";;".$msg->pid[$msg_number][$index].";;".$msg->encoding[$msg_number][$index]."' onClick=\"window.open('./inc/show_img.php?msg_num=".$msg_number."&msg_folder=".$msg_folder."&msg_part=".$msg->pid[$msg_number][$index]."','mywindow','width=700,height=600,scrollbars=yes');\">". $img ."</a>";
 					$thumbs_array[] = $href;
 	    		}
@@ -2032,7 +2032,48 @@
 	
 		//	Build Uploading Attachments!!!
-		if (count($attachments))
-			foreach ($attachments as $attach)
-				$mail->AddAttachment($attach['tmp_name'], $attach['name'], "base64", $this->get_file_type($attach['name']));  // optional name			
+		$sizeof_attachments = count($attachments);
+		if ($sizeof_attachments)
+			foreach ($attachments as $numb => $attach){
+				if ($numb == ($sizeof_attachments-1) && $params['insertImg'] == 'true'){ // Auto-resize image
+					list($width, $height,$image_type) = getimagesize($attach['tmp_name']);
+					switch ($image_type)
+					{
+					case 1: $image_big = imagecreatefromgif($attach['tmp_name']); break;
+					case 2: $image_big = imagecreatefromjpeg($attach['tmp_name']);  break;
+					case 3: $image_big = imagecreatefrompng($attach['tmp_name']); break;
+					case 6:
+						include_once("gd_functions.php");
+						$image_big = imagecreatefrombmp($attach['tmp_name']); break;
+					default:
+						$mail->AddAttachment($attach['tmp_name'], $attach['name'], "base64", $this->get_file_type($attach['name']));
+						break;
+					}
+					header('Content-type: image/jpeg');
+					$max_resolution = $_SESSION['phpgw_info']['user']['preferences']['expressoMail']['image_size'];
+					$max_resolution = ($max_resolution==""?'65536':$max_resolution);
+					if ($width < $max_resolution && $height < $max_resolution){
+						$new_width = $width;
+						$new_height = $height;
+					}
+					else if ($width > $max_resolution){
+						$new_width = $max_resolution;
+						$new_height = $height*($new_width/$width);
+					}
+					else {
+						$new_height = $max_resolution;
+						$new_width = $width*($new_height/$height);
+					}
+					$image_new = imagecreatetruecolor($new_width, $new_height);
+					imagecopyresampled($image_new, $image_big, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
+					$tmpDir = ini_get("session.save_path");
+					$_file = "cid_image_".base_convert(microtime(), 10, 36).".dat"; 
+					imagejpeg($image_new,$tmpDir.$_file, 85);
+					$mail->AddAttachment($tmpDir.$_file, $attach['name'], "base64", $this->get_file_type($tmpDir.$_file));
+				}
+				else
+					$mail->AddAttachment($attach['tmp_name'], $attach['name'], "base64", $this->get_file_type($attach['name']));
+				// optional name
+				}
+	
 	
 	
Index: trunk/expressoMail1_2/inc/gotodownload.php
===================================================================
--- trunk/expressoMail1_2/inc/gotodownload.php	(revision 178)
+++ trunk/expressoMail1_2/inc/gotodownload.php	(revision 1005)
@@ -1,3 +1,6 @@
 <?
+	if (! array_key_exists('phpgw_info', $_SESSION))
+		exit;
+
 	$msg_number = $_GET['msg_number'];
 	$idx_file = $_GET['idx_file'];
@@ -25,9 +28,12 @@
 		$mbox_stream = imap_open("{".$imap_server.":".$imap_port.$imap_options."}".$msg_folder, $username, $password);   	
    		$fileContent = imap_fetchbody($mbox_stream, $msg_number, $msg_part, FT_UID);
-		include("class.imap_attachment.inc.php");
-		$imap_attachment = new imap_attachment();	   		
-		$a = $imap_attachment->download_attachment($mbox_stream, $msg_number);
-		
-		$filename = $a[$idx_file]['name'];
+		/*
+		*Removed by Bug #546
+		*include("class.imap_attachment.inc.php");
+		*$imap_attachment = new imap_attachment();	   		
+		*$a = $imap_attachment->download_attachment($mbox_stream, $msg_number);
+		*$filename = $a[$idx_file]['name'];
+		*/
+		$filename = $newfilename;
 	}
 	else
Index: trunk/expressoMail1_2/inc/gd_functions.php
===================================================================
--- trunk/expressoMail1_2/inc/gd_functions.php	(revision 1005)
+++ trunk/expressoMail1_2/inc/gd_functions.php	(revision 1005)
@@ -0,0 +1,329 @@
+<?/*
+*Author:
+*    JPEXS  from http://www.hotscripts.com* ImageCreateFromBmp
+*/
+
+function imagecreatefrombmp($file)
+{
+global  $CurrentBit, $echoMode;
+
+$f=fopen($file,"r");
+$Header=fread($f,2);
+
+if($Header=="BM")
+{
+ $Size=freaddword($f);
+ $Reserved1=freadword($f);
+ $Reserved2=freadword($f);
+ $FirstByteOfImage=freaddword($f);
+
+ $SizeBITMAPINFOHEADER=freaddword($f);
+ $Width=freaddword($f);
+ $Height=freaddword($f);
+ $biPlanes=freadword($f);
+ $biBitCount=freadword($f);
+ $RLECompression=freaddword($f);
+ $WidthxHeight=freaddword($f);
+ $biXPelsPerMeter=freaddword($f);
+ $biYPelsPerMeter=freaddword($f);
+ $NumberOfPalettesUsed=freaddword($f);
+ $NumberOfImportantColors=freaddword($f);
+
+if($biBitCount<24)
+ {
+  $img=imagecreate($Width,$Height);
+  $Colors=pow(2,$biBitCount);
+  for($p=0;$p<$Colors;$p++)
+   {
+    $B=freadbyte($f);
+    $G=freadbyte($f);
+    $R=freadbyte($f);
+    $Reserved=freadbyte($f);
+    $Palette[]=imagecolorallocate($img,$R,$G,$B);
+   };
+
+
+
+
+if($RLECompression==0)
+{
+   $Zbytek=(4-ceil(($Width/(8/$biBitCount)))%4)%4;
+
+for($y=$Height-1;$y>=0;$y--)
+    {
+     $CurrentBit=0;
+     for($x=0;$x<$Width;$x++)
+      {
+         $C=freadbits($f,$biBitCount);
+       imagesetpixel($img,$x,$y,$Palette[$C]);
+      };
+    if($CurrentBit!=0) {freadbyte($f);};
+    for($g=0;$g<$Zbytek;$g++)
+     freadbyte($f);
+     };
+
+ };
+};
+
+
+if($RLECompression==1) //$BI_RLE8
+{
+$y=$Height;
+
+$pocetb=0;
+
+while(true)
+{
+$y--;
+$prefix=freadbyte($f);
+$suffix=freadbyte($f);
+$pocetb+=2;
+
+$echoit=false;
+
+if($echoit)echo "Prefix: $prefix Suffix: $suffix<BR>";
+if(($prefix==0)and($suffix==1)) break;
+if(feof($f)) break;
+
+while(!(($prefix==0)and($suffix==0)))
+{
+ if($prefix==0)
+  {
+   $pocet=$suffix;
+   $Data.=fread($f,$pocet);
+   $pocetb+=$pocet;
+   if($pocetb%2==1) {freadbyte($f); $pocetb++;};
+  };
+ if($prefix>0)
+  {
+   $pocet=$prefix;
+   for($r=0;$r<$pocet;$r++)
+    $Data.=chr($suffix);
+  };
+ $prefix=freadbyte($f);
+ $suffix=freadbyte($f);
+ $pocetb+=2;
+ if($echoit) echo "Prefix: $prefix Suffix: $suffix<BR>";
+};
+
+for($x=0;$x<strlen($Data);$x++)
+ {
+  imagesetpixel($img,$x,$y,$Palette[ord($Data[$x])]);
+ };
+$Data="";
+
+};
+
+};
+
+
+if($RLECompression==2) //$BI_RLE4
+{
+$y=$Height;
+$pocetb=0;
+
+/*while(!feof($f))
+ echo freadbyte($f)."_".freadbyte($f)."<BR>";*/
+while(true)
+{
+//break;
+$y--;
+$prefix=freadbyte($f);
+$suffix=freadbyte($f);
+$pocetb+=2;
+
+$echoit=false;
+
+if($echoit)echo "Prefix: $prefix Suffix: $suffix<BR>";
+if(($prefix==0)and($suffix==1)) break;
+if(feof($f)) break;
+
+while(!(($prefix==0)and($suffix==0)))
+{
+ if($prefix==0)
+  {
+   $pocet=$suffix;
+
+   $CurrentBit=0;
+   for($h=0;$h<$pocet;$h++)
+    $Data.=chr(freadbits($f,4));
+   if($CurrentBit!=0) freadbits($f,4);
+   $pocetb+=ceil(($pocet/2));
+   if($pocetb%2==1) {freadbyte($f); $pocetb++;};
+  };
+ if($prefix>0)
+  {
+   $pocet=$prefix;
+   $i=0;
+   for($r=0;$r<$pocet;$r++)
+    {
+    if($i%2==0)
+     {
+      $Data.=chr($suffix%16);
+     }
+     else
+     {
+      $Data.=chr(floor($suffix/16));
+     };
+    $i++;
+    };
+  };
+ $prefix=freadbyte($f);
+ $suffix=freadbyte($f);
+ $pocetb+=2;
+ if($echoit) echo "Prefix: $prefix Suffix: $suffix<BR>";
+};
+
+for($x=0;$x<strlen($Data);$x++)
+ {
+  imagesetpixel($img,$x,$y,$Palette[ord($Data[$x])]);
+ };
+$Data="";
+
+};
+
+};
+
+
+ if($biBitCount==24)
+{
+ $img=imagecreatetruecolor($Width,$Height);
+ $Zbytek=$Width%4;
+
+   for($y=$Height-1;$y>=0;$y--)
+    {
+     for($x=0;$x<$Width;$x++)
+      {
+       $B=freadbyte($f);
+       $G=freadbyte($f);
+       $R=freadbyte($f);
+       $color=imagecolorexact($img,$R,$G,$B);
+       if($color==-1) $color=imagecolorallocate($img,$R,$G,$B);
+       imagesetpixel($img,$x,$y,$color);
+      }
+    for($z=0;$z<$Zbytek;$z++)
+     freadbyte($f);
+   };
+};
+return $img;
+
+};
+
+
+fclose($f);
+
+
+};
+
+
+/*
+* Helping functions:
+*-------------------------
+*
+* freadbyte($file) - reads 1 byte from $file
+* freadword($file) - reads 2 bytes (1 word) from $file
+* freaddword($file) - reads 4 bytes (1 dword) from $file
+* freadlngint($file) - same as freaddword($file)
+* decbin8($d) - returns binary string of d zero filled to 8
+* RetBits($byte,$start,$len) - returns bits $start->$start+$len from $byte
+* freadbits($file,$count) - reads next $count bits from $file
+* RGBToHex($R,$G,$B) - convert $R, $G, $B to hex
+* int_to_dword($n) - returns 4 byte representation of $n
+* int_to_word($n) - returns 2 byte representation of $n
+*/
+
+function freadbyte($f)
+{
+ return ord(fread($f,1));
+};
+
+function freadword($f)
+{
+ $b1=freadbyte($f);
+ $b2=freadbyte($f);
+ return $b2*256+$b1;
+};
+
+
+function freadlngint($f)
+{
+return freaddword($f);
+};
+
+function freaddword($f)
+{
+ $b1=freadword($f);
+ $b2=freadword($f);
+ return $b2*65536+$b1;
+};
+
+
+
+function RetBits($byte,$start,$len)
+{
+$bin=decbin8($byte);
+$r=bindec(substr($bin,$start,$len));
+return $r;
+
+};
+
+
+
+$CurrentBit=0;
+function freadbits($f,$count)
+{
+ global $CurrentBit,$SMode;
+ $Byte=freadbyte($f);
+ $LastCBit=$CurrentBit;
+ $CurrentBit+=$count;
+ if($CurrentBit==8)
+  {
+   $CurrentBit=0;
+  }
+ else
+  {
+   fseek($f,ftell($f)-1);
+  };
+ return RetBits($Byte,$LastCBit,$count);
+};
+
+
+
+function RGBToHex($Red,$Green,$Blue)
+  {
+   $hRed=dechex($Red);if(strlen($hRed)==1) $hRed="0$hRed";
+   $hGreen=dechex($Green);if(strlen($hGreen)==1) $hGreen="0$hGreen";
+   $hBlue=dechex($Blue);if(strlen($hBlue)==1) $hBlue="0$hBlue";
+   return($hRed.$hGreen.$hBlue);
+  };
+
+        function int_to_dword($n)
+        {
+                return chr($n & 255).chr(($n >> 8) & 255).chr(($n >> 16) & 255).chr(($n >> 24) & 255);
+        }
+        function int_to_word($n)
+        {
+                return chr($n & 255).chr(($n >> 8) & 255);
+        }
+
+
+function decbin8($d)
+{
+return decbinx($d,8);
+};
+
+function decbinx($d,$n)
+{
+$bin=decbin($d);
+$sbin=strlen($bin);
+for($j=0;$j<$n-$sbin;$j++)
+ $bin="0$bin";
+return $bin;
+};
+
+function inttobyte($n)
+{
+return chr($n);
+};
+
+?>
Index: trunk/expressoMail1_2/inc/hook_settings.inc.php
===================================================================
--- trunk/expressoMail1_2/inc/hook_settings.inc.php	(revision 1002)
+++ trunk/expressoMail1_2/inc/hook_settings.inc.php	(revision 1005)
@@ -67,4 +67,14 @@
 create_check_box(lang('Use shortcuts?'),'use_shotcuts','');
 create_check_box(lang('Auto save draft'),'auto_save_draft','');
+$default = array(
+	'65536' => lang('unlimited'),
+	'640' => '640',
+	'768' => '768',
+	'800' => '800',
+	'1024' => '1024',
+	'1080' => '1080'
+);
+
+create_select_box(lang('What is the maximum size of embedded images')."?",'image_size',$default,'');
 
 $default = array(
