Part of the EllisLab Network
   
 
Multipart decoding issues
Posted: 19 August 2008 03:35 AM   [ Ignore ]  
Research Assistant
RankRankRank
Total Posts:  499
Joined  05-30-2008

I’m having problems decoding files from emails.

<?php

            
######################################

            #Coded By Jijo Last Update Date[Jan/19/06]

            #####################################




            ##########################################################


            ###################### Class readattachment ###############
class Readattachment
{
 
var $username;
 var 
$encrypted;
 var 
$pic;
 var 
$time;
 var 
$url;
 var 
$caption;
 var 
$tag;
 var 
$mes;

     function 
Readattachment()
     
{
       $this
->CI=& get_instance();

     
}
     
     
function checkExists($username$password)
     
{
       $query 
$this->CI->db->get_where('project_users', array('username' => $username'password' => $password));
       if (
$query->num_rows() > 0)
       
{
         
return true;
       
else {
         
return false;
         exit;
       
}
     }


     
function checkAndUpload($arg$pic)
     
{

preg_match
("#username: ([a-zA-Z0-9]+)#i",$arg,$username);
$username $username[1];
preg_match("#password: ([a-zA-Z0-9]+)#i",$arg,$password);
$password $password[1];
preg_match("#caption: ([a-zA-Z0-9]+)#i",$arg,$caption);
$caption 'Default';
$encrypted md5($password);
$this->checkExists($username$encrypted);
$this->uploadData($username$encrypted$pictime(), ''$caption'nsfw');
     
}


        
function getdecodevalue($message,$coding)
        
{
        
if ($coding == 0)
        
{
           $message 
imap_8bit($message);
        
}
        
elseif ($coding == 1)
        
{
          $message 
imap_8bit($message);
        
}
        
elseif ($coding == 2)
        
{
           $message 
imap_binary($message);
        
}
        
elseif ($coding == 3)
        
{
       $message
=imap_base64($message);
       
}
        
elseif ($coding == 4)
        
{
           $message 
imap_qprint($message);
        
}
        
elseif ($coding == 5)
        
{
         $message 
imap_base64($message);
        
}
        
return $message;
        
}

            
function getdata($host,$login,$password,$savedirpath)
            
{
 $mbox 
imap_open ($host,  $login$password) or die("can't connect: " imap_last_error());
         
$message = array();
         
$message["attachment"]["type"][0] "text";
         
$message["attachment"]["type"][1] "multipart";
         
$message["attachment"]["type"][2] "message";
         
$message["attachment"]["type"][3] "application";
         
$message["attachment"]["type"][4] "audio";
         
$message["attachment"]["type"][5] "image";
         
$message["attachment"]["type"][6] "video";
         
$message["attachment"]["type"][7] "other";
         
         for (
$jk 1$jk <= imap_num_msg($mbox); $jk++)
         
{
        $headerinfo 
imap_header($mbox$jk);

        
$subject $headerinfo->Subject;
                        
$matches = array();
                        if (
preg_match('/^=\?[^?]*\?B\?(.*)\?=/m'$subject$matches))
                                
$subject base64_decode($matches[1]);

         
$structure imap_fetchstructure($mbox$jk);   
         
$parts $structure->parts;
         
$fpos=1;
    for(
$i 0$i count($parts); $i++)
{
                  $message[
"pid"][$i] = ($i);
                  
$part $parts[$i];
if (
$part->type == 0{
        $fpos
+=1;
        continue;
    
}


                  
if(TRUE)
                     
{
                      
                      $message[
"type"][$i] $message["attachment"]["type"][$part->type] "/" strtolower($part->subtype);
                     
$message["subtype"][$i] strtolower($part->subtype);
                     
$ext=$part->subtype;
                if (
$part->ifdparameters)
                  foreach(
$part->dparameters as $dparam)
                    if (
strtolower($dparam->attribute) == 'filename'$filename $dparam->value;
                if (empty(
$filename))
                  if (
$part->ifparameters)
                    foreach(
$part->parameters as $param)
                      if (
strtolower($param->attribute) == 'name'$filename $param->value;

if (empty(
$filename) && $part->ifid)
        
$filename trim($part->id'<>');


     if (empty(
$filename)) $filename 'nfn' rand() . '.' strtolower($part->subtype);



                               
$mege="";
                               
$data="";
                               
$mege imap_fetchbody($mbox,$jk,$fpos);
                               
$body imap_body($mbox$jk);
                           
//$this->checkAndUpload($body, $filename);
                           
$fp=fopen($savedirpath $filename,w);
                           
$data=$this->getdecodevalue($mege,$part->type);
                           
fputs($fp,$data);
                           
fclose($fp);
                           
$fpos+=1;



                     
}

               }
         imap_delete
($mbox,$jk);
         
}
         imap_expunge
($mbox);
         
imap_close($mbox);
         
}

         
           
function uploadData($username$encrypted$pic$time$url$caption$tag)
            
{
        $this
->CI->db->insert('pb_photos', array('time' => time(), 'pic' => $pic'url' => base_url() . 'system/application/views/uploads/''caption' => $caption'poster' => $username'tag' => $tag));

            
}


            }
?> 

It saves normal files from the email and uploads… but it doesn’t save it when the email is from a mobile phone… it saves the file as ‘.alternative’ and it ranges from 0kb-5kb.

Note: I commented out the upload to db line for now so it won’t do that while testing it. wink

Profile