[ Index ]
 

Code source de vtiger CRM 5.0.2

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/modules/Webmails/ -> Webmail.php (source)

   1  <?php
   2  /*********************************************************************************
   3   ** The contents of this file are subject to the vtiger CRM Public License Version 1.0
   4    * ("License"); You may not use this file except in compliance with the License
   5    * The Initial Developer of the Original Code is FOSS Labs.
   6    * Portions created by FOSS Labs are Copyright (C) FOSS Labs.
   7    * Portions created by vtiger are Copyright (C) vtiger.
   8    * All Rights Reserved.
   9    *
  10    ********************************************************************************/
  11  
  12  
  13  include_once ('config.php');
  14  require_once ('include/logging.php');
  15  require_once ('include/database/PearDatabase.php');
  16  require_once ('data/SugarBean.php');
  17  require_once ('data/CRMEntity.php');
  18  
  19  class Webmail extends CRMEntity {
  20          var $log;
  21          var $db;
  22  
  23      var $headers;
  24        var $mailid;
  25          var $to = array();
  26          var $to_name = array();
  27          var $from;
  28          var $fromname;
  29          var $fromaddr;
  30      var $reply_to = array();
  31      var $reply_to_name = array();
  32      var $cc_list = array();
  33      var $cc_list_name = array();
  34      var $subject;
  35      var $date;
  36      var $body_type;
  37      var $body;
  38      var $attachments = array();
  39      var $inline = array();
  40      var $mbox;
  41      var $email;
  42      var $relationship = array();
  43      var $has_attachments = false;
  44  
  45  
  46   	function Webmail($mbox,$mailid) {
  47  
  48          $this->db = new PearDatabase();
  49          $this->db->println("Entering Webmail($mbox,$mailid)");
  50          $this->log = &LoggerManager::getLogger('WEBMAILS');
  51          $this->mbox=$mbox;
  52          $this->mailid=$mailid;
  53  
  54          $this->headers = $this->load_headers();
  55  
  56          $this->to = $this->headers["theader"]["to"];
  57          $this->to_name = $this->headers["theader"]["to_name"];
  58          $this->db->println("Webmail TO: $this->to");
  59  
  60          $this->from = $this->headers["theader"]["from"];
  61          $this->fromname = $this->headers["theader"]["from_name"];
  62          $this->fromaddr = $this->headers["theader"]["fromaddr"];
  63  
  64          $this->reply_to = $this->headers["theader"]["reply_to"];
  65          $this->reply_to_name = $this->headers["theader"]["reply_to_name"];
  66  
  67          $this->cc_list = $this->headers["cc_list"];
  68          $this->cc_list_name = $this->headers["cc_list_name"];
  69  
  70          $this->subject = $this->headers["theader"]["subject"];
  71          $this->date = $this->headers["theader"]["date"];
  72  
  73          $this->has_attachments = $this->get_attachments();
  74          $this->db->println("Exiting Webmail($mbox,$mailid)");
  75          }
  76  
  77  	function delete() {
  78          imap_delete($this->mbox, $this->mailid);
  79      }
  80  
  81  	function loadMail() {
  82          $this->email = $this->load_mail();
  83          $this->inline = $this->email["inline"];
  84          $this->attachments = $this->email["attachments"];
  85          $this->body = $this->email["content"]["body"];
  86          $this->relationship = $this->find_relationships();
  87      }
  88  
  89  	function replyBody() {
  90          $tmp = "<br><br><p style='font-weight:bold'>In reply to the message sent by ".$this->reply_name." on ".$this->date."</p>";
  91          $tmp .= "<blockquote style='border-left:1px solid blue;padding-left:5px'>".$this->body."</blockquote>";
  92          return $tmp;
  93      }
  94  
  95  	function unDeleteMsg() {
  96          imap_undelete($this->mbox, $this->mailid);
  97      }
  98  
  99  	function setFlag() {
 100          $status=imap_setflag_full($this->mbox,$this->mailid,"\\Flagged");
 101      }
 102  
 103  	function delFlag() {
 104          $status=imap_clearflag_full($this->mbox,$this->mailid,"\\Flagged");
 105      }
 106  
 107  	function getBodyType() {
 108          return $this->body_type;
 109      }
 110  
 111  	function downloadInlineAttachments() {
 112          return $this->dl_inline();
 113      }
 114  
 115  	function downloadAttachments() {
 116          return $this->dl_attachments($this->mailid,$this->mbox);
 117      }
 118  
 119      function load_headers() {
 120      // get the header info
 121      $mailHeader=Array();
 122      $theader = @imap_headerinfo($this->mbox, $this->mailid);
 123      $tmp = imap_mime_header_decode($theader->fromaddress);
 124  
 125      for($p=0;$p<count($theader->to);$p++) {
 126          $mailHeader['to'][] = $theader->to[$p]->mailbox.'@'.$theader->to[$p]->host;
 127          $mailHeader['to_name'][] = $theader->to[$p]->personal;
 128      }
 129      $mailHeader['from'] = $theader->from[0]->mailbox.'@'.$theader->from[0]->host;    
 130      $mailHeader['from_name'] = $theader->from[0]->personal;
 131      $mailHeader['fromaddr'] = $theader->fromaddress;
 132  
 133      $mailHeader['subject'] = strip_tags($theader->subject);
 134      $mailHeader['date'] = $theader->date;
 135  
 136      for($p=0;$p<count($theader->reply_to);$p++) {
 137          $mailHeader['reply_to'][] = $theader->reply_to[$p]->mailbox.'@'.$theader->reply_to[$p]->host;
 138          $mailHeader['reply_to_name'][] = $theader->reply_to[$p]->personal;
 139      }
 140      for($p=0;$p<count($theader->cc);$p++) {
 141          $mailHeader['cc_list'][] = $theader->cc[$p]->mailbox.'@'.$theader->cc[$p]->host;
 142          $mailHeader['cc_list_name'][] = $theader->cc[$p]->personal;
 143      }
 144          return $ret = Array("theader"=>$mailHeader);
 145      }
 146  
 147      private function get_attachments() {
 148         $struct = imap_fetchstructure($this->mbox, $this->mailid);
 149         $parts = $struct->parts;
 150  
 151          $done="false";
 152          $i = 0;
 153          if (!$parts)
 154                  return false; // simple message
 155          else  {
 156          $stack = array();
 157          $inline = array();
 158  
 159          $endwhile = false;
 160  
 161          while (!$endwhile) {
 162             if (!$parts[$i]) {
 163               if (count($stack) > 0) {
 164                 $parts = $stack[count($stack)-1]["p"];
 165                 $i    = $stack[count($stack)-1]["i"] + 1;
 166                 array_pop($stack);
 167               } else {
 168                 $endwhile = true;
 169               }
 170          }
 171             if (!$endwhile) {
 172  
 173               $partstring = "";
 174               foreach ($stack as $s) {
 175                 $partstring .= ($s["i"]+1) . ".";
 176               }
 177               $partstring .= ($i+1);
 178  
 179               if (strtoupper($parts[$i]->disposition) == "INLINE" || strtoupper($parts[$i]->disposition) == "ATTACHMENT")
 180                          return true;
 181               }
 182             if ($parts[$i]->parts) {
 183               $stack[] = array("p" => $parts, "i" => $i);
 184               $parts = $parts[$i]->parts;
 185               $i = 0;
 186             } else {
 187               $i++;
 188             }
 189           }
 190         }
 191          return false;
 192      }
 193  
 194      private function find_relationships() {
 195      // leads search
 196      $sql = "SELECT * from vtiger_leaddetails left join vtiger_crmentity on vtiger_crmentity.crmid=vtiger_leaddetails.leadid where vtiger_leaddetails.email = '".trim($this->from)."' AND vtiger_crmentity.deleted='0'";
 197      $res = $this->db->query($sql,true,"Error: "."<BR>$query");
 198      $numRows = $this->db->num_rows($res);
 199      if($numRows > 0)
 200          return array('type'=>"Leads",'id'=>$this->db->query_result($res,0,"leadid"),'name'=>$this->db->query_result($res,0,"firstname")." ".$this->db->query_result($res,0,"lastname"));
 201  
 202      // contacts search
 203      $sql = "SELECT * from vtiger_contactdetails left join vtiger_crmentity on vtiger_crmentity.crmid=vtiger_contactdetails.contactid where vtiger_contactdetails.email = '".trim($this->from)."'  AND vtiger_crmentity.deleted='0'";
 204      $res = $this->db->query($sql,true,"Error: "."<BR>$query");
 205      $numRows = $this->db->num_rows($res);
 206      if($numRows > 0)
 207          return array('type'=>"Contacts",'id'=>$this->db->query_result($res,0,"contactid"),'name'=>$this->db->query_result($res,0,"firstname")." ".$this->db->query_result($res,0,"lastname"));
 208  
 209      // vtiger_accounts search
 210      $sql = "SELECT * from vtiger_account left join vtiger_crmentity on vtiger_crmentity.crmid=vtiger_account.accountid where vtiger_account.email1 = '".trim($this->from)."' OR vtiger_account.email1='".trim($this->from)."'  AND vtiger_crmentity.deleted='0'";
 211      $res = $this->db->query($sql,true,"Error: "."<BR>$query");
 212      $numRows = $this->db->num_rows($res);
 213      if($numRows > 0)
 214          return array('type'=>"Accounts",'id'=>$this->db->query_result($res,0,"accountid"),'name'=>$this->db->query_result($res,0,"accountname"));
 215  
 216      return 0;
 217      }
 218  
 219      private function dl_inline() {
 220          $struct = imap_fetchstructure($this->mbox, $this->mailid);
 221          $parts = $struct->parts;
 222  
 223          $i = 0;
 224          if (!$parts) 
 225          return;
 226          else {
 227  
 228          $stack = array();
 229          $inline = array();
 230  
 231          $endwhile = false;
 232  
 233          while (!$endwhile) {
 234             if (!$parts[$i]) {
 235               if (count($stack) > 0) {
 236                 $parts = $stack[count($stack)-1]["p"];
 237                 $i    = $stack[count($stack)-1]["i"] + 1;
 238                 array_pop($stack);
 239               } else {
 240                 $endwhile = true;
 241               }
 242          }
 243             if (!$endwhile) {
 244  
 245               $partstring = "";
 246               foreach ($stack as $s) {
 247                 $partstring .= ($s["i"]+1) . ".";
 248               }
 249               $partstring .= ($i+1);
 250  
 251               if (strtoupper($parts[$i]->disposition) == "INLINE")
 252                          $inline[] = array("filename" => $parts[$i]->dparameters[0]->value,"filedata"=>imap_fetchbody($this->mbox, $this->mailid, $partstring),"subtype"=>$parts[$i]->subtype,"filesize"=>$parts[$i]->bytes);
 253               } 
 254             if ($parts[$i]->parts) {
 255               $stack[] = array("p" => $parts, "i" => $i);
 256               $parts = $parts[$i]->parts;
 257               $i = 0;
 258             } else {
 259               $i++;
 260             }
 261           }
 262         }
 263      return $inline;
 264      }
 265  
 266      private function dl_attachments() {
 267          $struct = imap_fetchstructure($this->mbox, $this->mailid);
 268          $parts = $struct->parts;
 269  
 270          $content = array();
 271          $i = 0;
 272          if (!$parts)
 273          return;
 274          else {
 275  
 276          $stack = array();
 277          $attachment = array();
 278  
 279          $endwhile = false;
 280  
 281          while (!$endwhile) {
 282             if (!$parts[$i]) {
 283               if (count($stack) > 0) {
 284                 $parts = $stack[count($stack)-1]["p"];
 285                 $i    = $stack[count($stack)-1]["i"] + 1;
 286                 array_pop($stack);
 287               } else {
 288                 $endwhile = true;
 289               }
 290          }
 291             if (!$endwhile) {
 292  
 293               $partstring = "";
 294               foreach ($stack as $s) {
 295                 $partstring .= ($s["i"]+1) . ".";
 296               }
 297               $partstring .= ($i+1);
 298  
 299               if (strtoupper($parts[$i]->disposition) == "ATTACHMENT")
 300                          $attachment[] = array("filename" => $parts[$i]->dparameters[0]->value,"filedata"=>imap_fetchbody($this->mbox, $this->mailid, $partstring),"subtype"=>$parts[$i]->subtype,"filesize"=>$parts[$i]->bytes);
 301               } 
 302             if ($parts[$i]->parts) {
 303               $stack[] = array("p" => $parts, "i" => $i);
 304               $parts = $parts[$i]->parts;
 305               $i = 0;
 306             } else {
 307               $i++;
 308             }
 309           }
 310         }
 311      return $attachment;
 312      }
 313      private function load_mail() {
 314      // parse the message
 315      $struct = imap_fetchstructure($this->mbox, $this->mailid);
 316             $parts = $struct->parts;
 317  
 318          $content = array();
 319          $i = 0;
 320          if (!$parts) { /* Simple message, only 1 piece */
 321           $attachment = array(); /* No vtiger_attachments */
 322           $bod=imap_body($this->mbox, $this->mailid);
 323           if(preg_match("/\<br\>/",$bod))
 324                      $content['body'] = $bod;
 325           else 
 326                      $content['body'] = nl2br($bod);
 327          } else {
 328  
 329          $stack = array(); 
 330          $attachment = array();
 331  
 332          $endwhile = false;
 333  
 334          while (!$endwhile) {
 335             if (!$parts[$i]) {
 336               if (count($stack) > 0) {
 337                 $parts = $stack[count($stack)-1]["p"];
 338                 $i    = $stack[count($stack)-1]["i"] + 1;
 339                 array_pop($stack);
 340               } else {
 341                 $endwhile = true;
 342               }
 343          }
 344          $search = array("/=20=/","/=20/","/=\r\n/","/=3D/","@&(<a|<A);@i","/=0A/i","/=A0/i");
 345          $replace = array("","","","=","<a target='_blank' ","");
 346             if (!$endwhile) {
 347  
 348               $partstring = "";
 349               foreach ($stack as $s) {
 350                 $partstring .= ($s["i"]+1) . ".";
 351               }
 352               $partstring .= ($i+1);
 353  
 354               $type='';
 355           if (strtoupper($parts[$i]->disposition) == "INLINE" && strtoupper($parts[$i]->subtype) != "PLAIN") {
 356                          $inline[] = array("filename" => $parts[$i]->dparameters[0]->value,"subtype"=>$parts[$i]->subtype,"filesize"=>$parts[$i]->bytes);
 357           } elseif (strtoupper($parts[$i]->disposition) == "ATTACHMENT") {
 358                          $attachment[] = array("filename" => $parts[$i]->dparameters[0]->value,"subtype"=>$parts[$i]->subtype,"filesize"=>$parts[$i]->bytes);
 359  
 360               } elseif (strtoupper($parts[$i]->subtype) == "HTML") {
 361                          $content['body'] = preg_replace($search,$replace,imap_fetchbody($this->mbox, $this->mailid, $partstring));
 362              $stat="done";
 363               } elseif (strtoupper($parts[$i]->subtype) == "TEXT" && !$stat == "done") {
 364                          $content['body'] = nl2br(imap_fetchbody($this->mbox, $this->mailid, $partstring));
 365              $stat="done";
 366               } elseif (strtoupper($parts[$i]->subtype) == "PLAIN" && !$stat == "done") {
 367                          $content['body'] = nl2br(imap_fetchbody($this->mbox, $this->mailid, $partstring));
 368              $stat="done";
 369               } elseif (!$stat == "done") {
 370                          $content['body'] = nl2br(imap_fetchbody($this->mbox, $this->mailid, $partstring));
 371               }
 372             }
 373  
 374             if ($parts[$i]->parts) {
 375               $stack[] = array("p" => $parts, "i" => $i);
 376               $parts = $parts[$i]->parts;
 377               $i = 0;
 378             } else {
 379               $i++;
 380             }
 381           } 
 382         } 
 383          if($struct->encoding==3)
 384          $content['body'] = base64_decode($content['body']);
 385          if($struct->encoding==4)
 386          $content['body'] = quoted_printable_decode($content['body']);
 387  
 388          $ret = Array("content" => $content,"attachments"=>$attachment,"inline"=>$inline);
 389          return $ret;
 390      }
 391  }
 392  ?>


Généré le : Sun Feb 25 10:22:19 2007 par Balluche grâce à PHPXref 0.7