// // vgr_unbase64_mail.php // //VGR23062007 Création // // TODO : Nil // if (!isset($_GET['fn'])) echo "give me a filename parameter that I can fopen()
"; else { // work $filename1=$_GET['fn']; /* read data (binary) this is FAST but works only on non-multipart base64-encoded data (ex a simple jpeg) $ifp = fopen( $filename1, 'rb' ); $imageData = fread( $ifp, filesize( $filename1 ) ); fclose( $ifp ); // encode & write data (binary) $ifp = fopen( $filename2, 'wb' ); fwrite( $ifp, base64_decode( $imageData ) ); fclose( $ifp ); */ // read file1 until boundary=" is found. Get the $boundary. // repeat // read blocks enclosed in --$boundary // get Content-Type: text/plain; charset="iso-8859-1" or Content-Type: image/jpeg; name="zendlogo.jpg"; // this gives us the file2's name // skip until last Content-* header is found ; skip one line ; // read blocks' contents until next $boundary ; // if Content-Transfer-Encoding: base64 was found, base64_decode() // save transformed contents to file2 // until file1 is done $debug=TRUE; // read file1 until boundary=" is found. Get the $boundary. $file1=fopen($filename1,'r'); // read in text mode, not binary mode ; emails are pure text. echo "$filename1 opened
"; $boundary=''; $done=FALSE; $compteur=array(); do { $ligne=fgets($file1,4096); $done=($ligne===FALSE); if (! $done) { // analyse if (($a=strpos($ligne,'boundary="'))!==FALSE) { // boundary found $a+=strlen('boundary="'); if ($debug) echo "start of boundary found at pos=$a
"; $b=$a+1; while ($ligne[$b]<>'"') $b++; $boundary=substr($ligne,$a,$b-$a); echo "boundary found as '$boundary'
"; } else if ($debug) echo "skipped line '$ligne'
"; } } while (($boundary=='') AND (!$done)); if ($done) die("sorry, no boundary found"); $givemeachance=TRUE; // repeat do { // read blocks enclosed in --$boundary $block=FALSE; do { $ligne=fgets($file1,4096); $done=($ligne===FALSE); if (! $done) { $block=(strpos($ligne,$boundary)!==FALSE); if ((!$block)AND $givemeachance) { // try to fix invalid emails if (substr($ligne,0,2)=='--') { $boundary=trim(substr($ligne,3)); //VGR04092007 3 au lieu de 2 echo "REAL boundary seems to be '$boundary' in stead...
"; $block=TRUE; $givemeachance=FALSE; } } } } while ((! $block) AND (!$done)); if ($block) { if ($debug) echo "block found
"; $contents=""; // get Content-Type: text/plain; charset="iso-8859-1" or Content-Type: image/jpeg; name="zendlogo.jpg"; $letype=FALSE; do { $ligne=fgets($file1,4096); $done=($ligne===FALSE); if (! $done) $letype=(($a=strpos($ligne,'Content-Type:'))!==FALSE); if ($debug) echo "skipping line '$ligne'
"; } while ((! $letype) AND (!$done)); if ($letype) { // extract type and/or original filename if ($debug) echo "Content-Type found in '$ligne'
"; $a+=strlen('Content-Type:')+1; // one extra space $b=$a+1; while ($ligne[$b]<>';') $b++; $letype=substr($ligne,$a,$b-$a); echo "mime type found as '$letype'
"; // this gives us the file2's name switch($letype) { case 'text/html' : $ext='.htm'; break; case 'text/plain' : $ext='.txt'; break; case 'image/jpeg' : $ext='.jpg'; break; default : $ext='.txt'; break; } @$compteur[$letype]++; $filename2=$filename1.$compteur[$letype].$ext; $file2=fopen($filename2,'w'); // skip until last Content-* header is found ; // if Content-Transfer-Encoding: base64 was found, base64_decode() // if Content-Transfer-Encoding: quoted-printable in stead, NOP $base64=FALSE; while (strlen($ligne)>2) { if (strpos($ligne,'Content-Transfer-Encoding: base64')!==FALSE) { $base64=TRUE; echo "base64 found
"; } if ($debug) echo "skipping header line '$ligne'
"; $ligne=fgets($file1,4096); $done=($ligne===FALSE); } // on sort sur la 1ère ligne, donc vide if (strlen($ligne)>2) echo "WARNING cette ligne '$ligne' devrait être vide!
"; // skip one line $ligne=fgets($file1,4096); $done=($ligne===FALSE); if ($debug) echo "first contents line '$ligne'
"; // read blocks' contents until next $boundary ; $contents=""; while (strpos($ligne,$boundary)===FALSE) { if (!$done) if ($debug) echo "found contents line '$ligne'
"; $ligne=trim(str_replace("\n","",str_replace("\r","",$ligne))); if (!$done) if ($debug) echo "added contents line '$ligne'
"; if (strlen($ligne)>0) $contents.=$ligne; else if ($debug) echo "skipped empty line
"; $ligne=fgets($file1,4096); $done=($ligne===FALSE); } if ($base64) { $contents=base64_decode($contents); echo "base64_decode
"; } // save transformed contents to file2 fputs($file2,$contents); fclose($file2); echo "wrote file $filename2
"; } // else NOP ($done=.T.) else if ($debug) echo "sorry, no type found
"; } // else NOP ($done=.T.) else if ($debug) echo "sorry, no more blocks
"; } while (! $done); // until file is done fclose($file1); echo "done.
"; }