Exemplo n.º 1
0
 /**
  * Special handler for POP3 boxes.  Standard IMAP commands are useless.
  * This will fetch only partial emails for POP3 and hence needs to be call again and again based on status it returns
  */
 function pop3_checkPartialEmail($synch = false)
 {
     require_once 'include/utils/array_utils.php';
     global $current_user;
     global $sugar_config;
     $cacheDataExists = false;
     $diff = array();
     $results = array();
     $cacheFilePath = clean_path("{$this->EmailCachePath}/{$this->id}/folders/MsgNOToUIDLData.php");
     if (file_exists($cacheFilePath)) {
         $cacheDataExists = true;
         if ($fh = @fopen($cacheFilePath, "rb")) {
             $data = "";
             $chunksize = 1 * (1024 * 1024);
             // how many bytes per chunk
             while (!feof($fh)) {
                 $buf = fgets($fh, $chunksize);
                 // 8kb max buffer - shouldn't be more than 80 chars via pop3...
                 $data = $data . $buf;
                 flush();
             }
             // while
             fclose($fh);
             $diff = unserialize($data);
             if (!empty($diff)) {
                 if (count($diff) > 50) {
                     $newDiff = array_slice($diff, 50, count($diff), true);
                 } else {
                     $newDiff = array();
                 }
                 $results = array_slice(array_keys($diff), 0, 50);
                 $data = serialize($newDiff);
                 if ($fh = @fopen($cacheFilePath, "w")) {
                     fputs($fh, $data);
                     fclose($fh);
                 }
                 // if
             }
         }
         // if
     }
     // if
     if (!$cacheDataExists) {
         if ($synch) {
             $this->deletePop3Cache();
         }
         $UIDLs = $this->pop3_getUIDL();
         if (count($UIDLs) > 0) {
             // get cached UIDLs
             $cacheUIDLs = $this->pop3_getCacheUidls();
             // new email cache values we should deal with
             $diff = array_diff_assoc($UIDLs, $cacheUIDLs);
             $diff = $this->pop3_shiftCache($diff, $cacheUIDLs);
             require_once 'modules/Emails/EmailUI.php';
             EmailUI::preflightEmailCache("{$this->EmailCachePath}/{$this->id}");
             if (count($diff) > 50) {
                 $newDiff = array_slice($diff, 50, count($diff), true);
             } else {
                 $newDiff = array();
             }
             $results = array_slice(array_keys($diff), 0, 50);
             $data = serialize($newDiff);
             if ($fh = @fopen($cacheFilePath, "w")) {
                 fputs($fh, $data);
                 fclose($fh);
             }
             // if
         } else {
             $GLOBALS['log']->debug("*** INBOUNDEMAIL: could not open socket connection to POP3 server");
             return "could not open socket connection to POP3 server";
         }
         // else
     }
     // if
     // build up msgNo request
     if (count($diff) > 0) {
         // remove dirty cache entries
         $startingNo = 0;
         if (isset($_REQUEST['currentCount']) && $_REQUEST['currentCount'] > -1) {
             $startingNo = $_REQUEST['currentCount'];
         }
         $this->mailbox = 'INBOX';
         $this->connectMailserver();
         //$searchResults = array_keys($diff);
         //$fetchedOverviews = array();
         //$chunkArraySerachResults = array_chunk($searchResults, 50);
         $concatResults = implode(",", $results);
         $GLOBALS['log']->info('$$$$ ' . $concatResults);
         $GLOBALS['log']->info("[EMAIL] Start POP3 fetch overview on mailbox [{$this->mailbox}] for user [{$current_user->user_name}] on 50 data");
         $fetchedOverviews = imap_fetch_overview($this->conn, $concatResults);
         $GLOBALS['log']->info("[EMAIL] End POP3 fetch overview on mailbox [{$this->mailbox}] for user [{$current_user->user_name}] on " . sizeof($fetchedOverviews) . " data");
         // clean up cache entry
         foreach ($fetchedOverviews as $k => $overview) {
             $overview->message_id = trim($diff[$overview->msgno]);
             $fetchedOverviews[$k] = $overview;
         }
         $GLOBALS['log']->info("[EMAIL] Start updating overview cache for pop3 mailbox [{$this->mailbox}] for user [{$current_user->user_name}]");
         $this->updateOverviewCacheFile($fetchedOverviews);
         $GLOBALS['log']->info("[EMAIL] Start updating overview cache for pop3 mailbox [{$this->mailbox}] for user [{$current_user->user_name}]");
         return array('status' => "In Progress", 'mbox' => $this->mailbox, 'count' => count($results) + $startingNo, 'totalcount' => count($diff), 'ieid' => $this->id);
     }
     // if
     unlink($cacheFilePath);
     return array('status' => "done");
 }