<?php

/**
*
* This code has been written by Alexis Ulrich (http://alx2002.free.fr)
* This code is in the public domain.
*
*/

// the light parsing is done again here to list the pictures available in the email
// and to find which one is to be displayed

// gets parameters
$file = substr($_GET['file'],0,100);
$file = urldecode($file);
$imageLocation = substr($_GET['imageLocation'],0,50);
$imageLocation = urldecode($imageLocation);

// opens file
if (!($content = fread(fopen($file, 'r'), filesize($file))))
    die(
'Bulletin introuvable ('.$file.')');

// two kinds of separators
if (strpos($content,'------_') !== false) $separator = '------_';
else
$separator = '------=_';
$aContent = explode($separator,$content);

$aImages = array();
foreach(
$aContent as $thisContent) {
    if (
strpos($thisContent,'Content-Type: image/gif;') !== false) {
        
// base64 gif picture
        
$img_id = substr($thisContent,strpos($thisContent,'Content-ID: <')+13,strpos($thisContent,'>')-strpos($thisContent,'Content-ID: <')-13);
        
$img_name = substr($thisContent,strpos($thisContent,'name="')+6,strpos($thisContent,'.gif"')-strpos($thisContent,'name="')-6);
        if (
strpos($thisContent,'Content-Location: ') !== false) {
            
$img_location = substr($thisContent,strpos($thisContent,'Content-Location: ')+18);
            
$img_location = substr($img_location,0,strpos($img_location,'.gif'));
            
$searched = 'Content-Location: ' . $img_location . '.gif';
            
$img_base64 = substr($thisContent,strpos($thisContent,$searched)+strlen($searched));
        }
        else {
            
$img_location = $img_name;
            
$searched = 'Content-ID: <' . $img_id . '>';
            
$Content_ID_pos = strpos($thisContent,'Content-ID: <');
            
$end_content = substr($thisContent,$Content_ID_pos);
            
$end_Content_ID_pos = strpos($end_content,'>');
            
$img_base64 = substr($end_content,$end_Content_ID_pos + 1);
        }
        
$aThisImage = array('id'=>$img_id, 'name'=>$img_name, 'location'=>$img_location, 'type'=>'gif', 'base64'=>$img_base64);
        
$aImages[] = $aThisImage;
    }
    if (
strpos($thisContent,'Content-Type: image/jpeg;') !== false) {
        
// base64 jpg picture
        
$img_id = substr($thisContent,strpos($thisContent,'Content-ID: <')+13,strpos($thisContent,'>')-strpos($thisContent,'Content-ID: <')-13);
        
$img_name = substr($thisContent,strpos($thisContent,'name="')+6,strpos($thisContent,'.jpg"')-strpos($thisContent,'name="')-6);
        if (
strpos($thisContent,'Content-Location: ') !== false) {
            
$img_location = substr($thisContent,strpos($thisContent,'Content-Location: ')+18);
            
$img_location = substr($img_location,0,strpos($img_location,'.jpg'));
            
$searched = 'Content-Location: ' . $img_location . '.jpg';
            
$img_base64 = substr($thisContent,strpos($thisContent,$searched)+strlen($searched));
        }
        else {
            
$img_location = $img_name;
            
$searched = 'Content-ID: <' . $img_id . '>';
            
$Content_ID_pos = strpos($thisContent,'Content-ID: <');
            
$end_content = substr($thisContent,$Content_ID_pos);
            
$end_Content_ID_pos = strpos($end_content,'>');
            
$img_base64 = substr($end_content,$end_Content_ID_pos + 1);
        }
        
$aThisImage = array('id'=>$img_id, 'name'=>$img_name, 'location'=>$img_location, 'type'=>'jpg', 'base64'=>$img_base64);
        
$aImages[] = $aThisImage;
    }
}
//print_r($aImages);
foreach($aImages as $image) {
    if (
$image['location'] == $imageLocation) {
        if (
$image['type'] == 'gif') header("Content-type: image/gif");
        else if (
$image['type'] == 'jpg') header("Content-type: image/jpg");
        echo
base64_decode($image['base64']);
    }
}
?>