<?php

error_reporting
(E_ALL);


$print_form 1;

function 
deltree($dir){
    
$d dir($dir);
    while(
$f $d->read() ){
#    print $dir.$f . "<br/>";
    
if($f != "." && $f != ".."){
        if(
is_dir($dir.$f)){
        
deltree($dir.$f."/");
        
rmdir($dir.$f);
        }
        if(
is_file($dir.$f)){
        
unlink($dir.$f);
        }
    }
    }
    
$d->close();
    
rmdir($dir);
}


function 
process_pbm($final_remote_name$local_filename$original_remote_name){
    
$f fopen($local_filename"r");
    if(
$f){
    
$magic_num fread($f2);
    
fclose($f);
    if(
$magic_num != "P4"){
        print(
"File \"$original_remote_name\" doesn't look like it is in .pbm format<br/>\n");
        return 
0;
    }
    
    
# now actually do the conversion
    
$p popen("/usr/bin/ppmtobmp " $local_filename"r");
    if(
$p){
        
header("Content-type: image/pbm\n");
        
header("Content-Disposition: attachment; filename=\"" $final_remote_name "\"\n");
        
        while (!
feof($p)){
        
$read fread($p1024);
        echo 
$read;
        }
        
pclose($p);
        return 
1;
    }
    else{
        print(
"Couldn't open ppmtobmp converter utility<br/>\n");
    }
    }
    else{
    print(
"Couldn't open temporary file \"$local_filename\" on server<br/>\n");
    }
    return 
0;
}

function 
process_zip($final_remote_name$local_filename$original_remote_name){
    
$f fopen($local_filename"r");
    if(
$f){
    
$magic_num fread($f2);
    
fclose($f);
    if(
$magic_num != "PK"){
        print(
"File \"$original_remote_name\" doesn't look like it is in .zip format<br/>\n");
        return 
0;
    }

    
$rand microtime();
    
$rand md5($rand);
    
$work_dir "/tmp/" $rand "/";
    if(!
mkdir($work_dir)){
        print(
"Couldn't make working directory: " $work_dir);
        return 
0;
    }

    
# get stuff out of the zip file
    
$p popen("/usr/bin/unzip " $local_filename " -d " $work_dir"r");
    if(!
$p){
        print(
"Couldn't open unzip utility<br/>\n");
        
deltree($work_dir);
        return 
0;
    }
    
pclose($p);
    
    
# loop over *.pbm files and convert them to *.bmp
    
$d dir($work_dir);
    while(
$f $d->read() ){
        if(
$f != "." && $f != ".."){
#        print $f . "<br/>";
        
if(is_file($work_dir $f)){
            
$p popen("/usr/bin/ppmtobmp " $work_dir $f 
                   
" > " $work_dir $f ".bmp""r");
            if(
$p){
            while(!
feof($p)){
                
$read fread($p1024);
                print 
$read;
            }
            
pclose($p);
            }
            else{
            print(
"Error on conversion...<br/>\n");
            }
        }
        }
    }
    
$d->close();
    
# and then zip them back up
    #put all *.pbm.bmp in zip file
    
$p popen("/usr/bin/zip -j - " $work_dir "*.pbm.bmp""r");
    if(
$p){
        
header("Content-type: application/zip\n");
        
header("Content-Disposition: attachment; filename=\"" $final_remote_name "\"\n");
        while(!
feof($p)){
        
$read fread($p1024);
        print 
$read;
        }
        
pclose($p);
    }
    else{
        print(
"Error on zipping files...<br/>\n");
    }
    
    
    
deltree($work_dir);
    return 
1;
    }
    else{
    print(
"Couldn't open temporary file \"$local_filename\" on server<br/>\n");
    }
    return 
0;
}


if(
$_FILES){
    
$org_name $_FILES['userfile']['name'];
    
$local_filename $_FILES['userfile']['tmp_name'];

    if(
== preg_match("/\.pbm$/i"$org_name$matchesPREG_OFFSET_CAPTURE)){
    
$final_name substr($org_name0$matches[0][1]) . ".bmp";
    
$enc_final_name urlencode($final_name);
    if(
$enc_final_name != $final_name)
        
$final_name "picture.bmp";

    
$print_form process_pbm($final_name$local_filename$org_name);
    }
    else if(
== preg_match("/\.zip$/i"$org_name$matchesPREG_OFFSET_CAPTURE)){
    
$final_name "converted.zip";
    
$print_form process_zip($final_name$local_filename$org_name);
    }
    else{
    print(
"Please only upload a file that has a .pbm or .zip extension<br/>\n");
    }
}

if(
$print_form){
    print 
"Send me a .pbm file, or a .zip file containing multiple .pbm files<br/>\n";
    print 
"<form enctype='multipart/form-data' action='pbmconvert.php' method='post'>\n";
    print 
"  <input type='hidden' name='MAX_FILE_SIZE' value='30000' />\n";
    print 
"  Send this file: <input size=75 name='userfile' type='file'/>\n";
    print 
"  <input type='submit' value='Send File' />\n";
    print 
"</form>\n";
}

?>